From c7d71753d5946d5cfb12588619a5d4b48cfb6ea2 Mon Sep 17 00:00:00 2001 From: josch Date: Thu, 26 Jun 2014 10:33:38 +0200 Subject: [PATCH] initial commit --- test.c | 20 ++++++++ tslib.c | 12 +++++ tslib.py | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 test.c create mode 100644 tslib.c create mode 100644 tslib.py diff --git a/test.c b/test.c new file mode 100644 index 0000000..e014ba8 --- /dev/null +++ b/test.c @@ -0,0 +1,20 @@ +#include +#include + +int main() +{ + + unsigned long bit[EV_CNT / (sizeof(long) * 8) + 1]; + unsigned long absbit[ABS_MAX / (sizeof(long) * 8) + 1]; + + printf("EV_CNT / (sizeof(long) * 8) + 1: %d\n", EV_CNT / (sizeof(long) * 8) + 1); + printf("ABS_MAX / (sizeof(long) * 8) + 1: %d\n", ABS_MAX / (sizeof(long) * 8) + 1); + printf("EVIOCGVERSION: %d\n", EVIOCGVERSION); + printf("EV_VERSION: %d\n", EV_VERSION); + printf("EVIOCGBIT(0, sizeof(bit)): %d\n", EVIOCGBIT(0, sizeof(bit))); + printf("EVIOCGBIT(EV_ABS, sizeof(absbit): %d\n", EVIOCGBIT(EV_ABS, sizeof(absbit))); + printf("EV_ABS: %d\n", EV_ABS); + printf("ABS_X: %d\n", ABS_X); + printf("ABS_Y: %d\n", ABS_Y); + printf("ABS_PRESSURE: %d\n", ABS_PRESSURE); +} diff --git a/tslib.c b/tslib.c new file mode 100644 index 0000000..af86e45 --- /dev/null +++ b/tslib.c @@ -0,0 +1,12 @@ +#include +#include + +int main() +{ + struct tsdev *ts; + ts = ts_open("/dev/input/event2", 0) + if (!ts) { + fprintf(stderr, "cannot open event2"); + return 1; + } +} diff --git a/tslib.py b/tslib.py new file mode 100644 index 0000000..316336d --- /dev/null +++ b/tslib.py @@ -0,0 +1,138 @@ +from ctypes import * + +tslib = cdll.LoadLibrary("libts-0.0.so.0") + +class tsdev(Structure): + pass + +class timeval(Structure): + _fields_ = [("tv_sec", c_long), + ("tv_usec", c_long)] + +class ts_sample(Structure): + _fields_ = [("x", c_int), + ("y", c_int), + ("pressure", c_uint), + ("tv", timeval)] + +ts_read_raw = tslib.ts_read_raw +ts_read_raw.restype = c_int +ts_read_raw.argtypes = [POINTER(tsdev), POINTER(ts_sample), c_int] + +ts_open = tslib.ts_open +ts_open.restype = POINTER(tsdev) +ts_open.argtypes = [c_char_p, c_int] + +ts_close = tslib.ts_close +ts_close.restype = c_int +ts_close.argtypes = [POINTER(tsdev)] + +ts_config = tslib.ts_config +ts_config.restype = c_int +ts_config.argtype = [POINTER(tsdev)] + + +ts = ts_open("/dev/input/event2", 0) +if ts == 0: + exit("ts_open failed") + +if ts_config(ts): + exit("ts_config failed") + +def get_xy(ts): + samples_x = list() + samples_y = list() + + s = ts_sample() + + # read until pressed + while True: + if ts_read_raw(ts, byref(s), 1) < 0: + exit("ts_read_raw failed") + if s.pressure != 0: + break + + # read until 128 values are gathered or no longer pressed + for i in range(128): + if ts_read_raw(ts, byref(s), 1) < 0: + exit("ts_read_raw failed") + if s.pressure == 0: + break + else: + samples_x.append(s.x) + samples_y.append(s.y) + + samples_x.sort() + samples_y.sort() + + middle = i/2 + + # return the median + if i%2 == 0: + return ((samples_x[middle-1]+samples_x[middle])/2, + (samples_y[middle-1]+samples_y[middle])/2) + else: + return (samples_x[middle], samples_y[middle]) + +cal = {(50, 50): get_xy(ts), + (480-50, 50): get_xy(ts), + (480-50, 640-50): get_xy(ts), + (50, 640-50): get_xy(ts), + (480/2, 640/2): get_xy(ts)} + +scaling = 65536.0 + +#print cal + +# get sums for matrix +n = 5.0 +x = sum([_x_ for _x_, _y_ in cal.values()]) +y = sum([_y_ for _x_, _y_ in cal.values()]) +x2 = sum([_x_*_x_ for _x_, _y_ in cal.values()]) +y2 = sum([_y_*_y_ for _x_, _y_ in cal.values()]) +xy = sum([_x_*_y_ for _x_, _y_ in cal.values()]) + +#print n, x, y, x2, y2, xy + +# get determinant +det = n*(x2*y2 - xy*xy) + x*(xy*y - x*y2) + y*(x*xy - y*x2) + +#print det + +# check determinant +if ((det < 0.1) and (det > -0.1)): + exit("determinant is too small") + +# calculate inverse matrix +a = (x2*y2 - xy*xy)/det +b = (xy*y - x*y2)/det +c = (x*xy - y*x2)/det +e = (n*y2 - y*y)/det +f = (x*y - n*xy)/det +i = (n*x2 - x*x)/det + +#print a, b, c, e, f, i + +# get sums for x calibration +z = sum([xfb for xfb, yfb in cal.keys()]) +zx = sum([xfb*x for (xfb, yfb), (x, y) in cal.items()]) +zy = sum([xfb*y for (xfb, yfb), (x, y) in cal.items()]) + +#print z, zx, zy + +print int((b*z + e*zx + f*zy)*scaling), int((c*z + f*zx + i*zy)*scaling), int((a*z + b*zx + c*zy)*scaling) + +# get sums for y calibration +z = sum([yfb for xfb, yfb in cal.keys()]) +zx = sum([yfb*x for (xfb, yfb), (x, y) in cal.items()]) +zy = sum([yfb*y for (xfb, yfb), (x, y) in cal.items()]) + +#print z, zx, zy + +print int((b*z + e*zx + f*zy)*scaling), int((c*z + f*zx + i*zy)*scaling), int((a*z + b*zx + c*zy)*scaling) + +print int(scaling) + +ts_close(ts) + +