Revision: 817
http://svn.sourceforge.net/hackndev/?rev=817&view=rev
Author: phiren
Date: 2007-02-14 18:50:59 -0800 (Wed, 14 Feb 2007)
Log Message:
-----------
Added runtime calibration
Modified Paths:
--------------
tsk/trunk/tsk.c
Modified: tsk/trunk/tsk.c
===================================================================
--- tsk/trunk/tsk.c 2007-02-14 21:13:08 UTC (rev 816)
+++ tsk/trunk/tsk.c 2007-02-15 02:50:59 UTC (rev 817)
@@ -23,25 +23,86 @@
#include "messagease.h"
-//You will need to calibrate, the digitizer to the sticker
-//Sorry, later I will include a feature to do this automaticly
-#define TOP 1180 //Top of sticker
-#define BOTTOM 200 // Bottom of sticker
-#define LEFT 240 // Left side of sticker
-#define RIGHT 3760 // Right side
-
//Don't touch these, they automaticly adjust to the above settings
#define HEIGHT (int) ((TOP-BOTTOM)/BOX_Y)
#define WIDTH (int) ((RIGHT-LEFT)/BOX_X)
-int box(int startx, int starty, int endx, int endy)
+struct calibration_
{
- int start = ((startx - LEFT) / WIDTH + (starty - BOTTOM) / HEIGHT * BOX_X);
- if (start > BOX_X*BOX_Y)
+ int left;
+ int bottom;
+ int width;
+ int height;
+};
+
+static struct calibration_ calibration;
+
+struct point
+{
+ int x;
+ int y;
+};
+
+/* Tap(touchscreen): return a single tap to the touchscreen
+ *
+ * FIXME: This function has a lot of duplcation with the main loop,
+ * prehaps we should intergrate them.
+ */
+struct point tap(int touchscreen)
+{
+struct input_event ev;
+int z = 1;
+struct point xy;
+struct point ret;
+while(z != 0){
+ do {
+ read(touchscreen, &ev, sizeof(struct input_event));
+ if (ev.type == 3)
+ switch(ev.code)
+ {
+ case absX:
+ xy.x = ev.value;
+ break;
+ case absY:
+ xy.y = ev.value;
+ break;
+ case absZ: // Pen pressure
+ z = ev.value;
+ }
+ } while (ev.type != 0);
+ if( z == DOWN )
+ {
+ z = 1;
+ ret = xy;
+ }
+}
+return ret;
+}
+
+void calibrate(int touchscreen)
+{
+ printf("To calibrate the digitizer, Please tap on the following places in order:\n");
+ printf("In the middle of 1,2,4,5\n");
+ struct point A = tap(touchscreen);
+ printf("In the middle of 7,8,?,0\n");
+ struct point B = tap(touchscreen);
+ printf("In the middle of O,R,E,S\n");
+ struct point C = tap(touchscreen);
+ calibration.height = (A.y - B.y) / 2;
+ calibration.width = (C.x - A.x) / 5;
+ calibration.left = A.x - calibration.width * 2;
+ calibration.bottom = B.y - calibration.height;
+ printf("Calibration done.");
+}
+
+int box(struct point start, struct point end)
+{
+ int startBox = ((start.x - calibration.left) / calibration.width + (start.y - calibration.bottom) / calibration.height * BOX_X);
+ if (startBox > BOX_X*BOX_Y)
return -1;
- int end = ((endx - LEFT) / WIDTH + (endy - BOTTOM) / HEIGHT * BOX_X);
- int ret = start*9;
- switch( start-end )
+ int endBox = ((end.x - calibration.left) / calibration.width + (end.y - calibration.bottom) / calibration.height * BOX_X);
+ int ret = startBox*9;
+ switch( startBox-endBox )
{
case 1: //Mid left
ret += 8;
@@ -79,12 +140,11 @@
{
struct input_event ev;
struct input_event event;
- int x = 0;
- int y = 0;
+ struct point xy;
int z = 0;
int i;
- int startx = 0;
- int starty = 0;
+ struct point start;
+ pid_t pid = 0;
//Seting up uinput
struct uinput_user_dev uidev;
@@ -121,17 +181,22 @@
if(touch == -1){
printf("Error, Could not open touchscreen");
return 1;}
- do { // Main program loop
+ calibrate(touch);
+
+
+ pid = fork(); // fork into background
+ if( pid == 0 ) { // This is the background process
+ while (1) { // Main program loop
do { // We loop through the events reading x, y and pressure values untill we get a blank event.
read(touch, &ev, sizeof(struct input_event)); //Read from event interface
if (ev.type == 3)
switch(ev.code)
{
case absX:
- x = ev.value;
+ xy.x = ev.value;
break;
case absY:
- y = ev.value;
+ xy.y = ev.value;
break;
case absZ: // Pen pressure
z = ev.value;
@@ -139,14 +204,13 @@
} while (ev.type != 0);
if(z == DOWN) // If the pen has just been put down, then we record the start x and y cordnates.
{
- startx = x;
- starty = y;
+ start = xy;
z = 1; // Clear z so we don't record startx and starty untill the pen goes down again
}
if(z == 0) // Pen up
{
z = 1; //clear z again
- int code = box(startx, starty, x, y);
+ int code = box(start, xy);
if (code != -1 && lookup[code] != 0)
{
event.type = EV_KEY; //indicates the keyboard event
@@ -157,6 +221,7 @@
write (uinput, &event, sizeof event);
}
}
- } while(1);
- return 1; //Right now we will never get here.
-;}
+ }
+ }
+ return 1;
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|