1 /* Dynapro SC3 touchscreen driver
2 Written by Jordan Crouse, September 5, 2001
4 Copyright 2001, Century Embedded Technologies
8 Add support for rotated displays
17 #include <sys/ioctl.h>
18 #include <sys/termios.h>
21 /* The current and saved termios */
22 static struct termios ios_saved, ios_current;
24 /* The file descriptor for the mouse device */
27 /* The calibration data */
28 /* We plug in some generic default data here, so if */
29 /* all else fails, the pointer should still work */
31 unsigned short calData[4] = { 73, 956, 883, 167 };
33 /* Flags to indicate if we should invert the values on the X and Y axis */
34 /* This does not mean rotation, just a backwards TS */
36 static int invX = 0, invY = 0;
38 extern SCREENDEVICE scrdev;
40 static int initSerial(char *dev) {
42 /* Open up the serial port */
43 int fd = open(dev, O_NONBLOCK);
46 EPRINTF("Error opening %s\n", dev);
50 /* Save the previous IO settings */
52 tcgetattr(fd, &ios_saved);
53 ios_current = ios_saved;
55 cfmakeraw(&ios_current);
57 /* Set the baud rate */
59 cfsetispeed(&ios_current, B2400);
60 cfsetospeed(&ios_current, B2400);
62 /* Set the data bits and remove the parity */
64 ios_current.c_cflag &= ~(CSIZE | PARENB);
65 ios_current.c_cflag |= CS8;
67 ios_current.c_cc[VMIN] = 3;
68 ios_current.c_cc[VTIME] = 1;
70 tcsetattr(fd, TCSANOW, &ios_current);
71 tcflush(fd, TCIOFLUSH);
76 static void closeSerial(int fd) {
78 /* Restore the saved settings */
80 tcsetattr(fd, TCSANOW, &ios_saved);
81 tcflush(fd, TCIOFLUSH);
86 /* Read a byte from the fd */
88 static int readSerial(int fd) {
91 int val = read(fd, &f, sizeof(f));
93 if (val <= 0) return(val);
98 /* Get some data points from the device */
100 static int getInput(int fd, int *data) {
105 /* Read the data coming in off the line */
113 if (errno == EAGAIN || errno == EINTR) return(0);
117 if (count++ > 150) return(0);
124 data[0] = (unsigned char) c;
128 /* This is a useless warning */
132 fprintf(stderr, "Non start byte recieved (%2.2x)\n", c);
139 data[1] = (unsigned char) c;
144 fprintf(stderr, "Got a start byte in the middle of the packet\n");
147 data[0] = (unsigned char) c;
157 data[2] = (unsigned char) c;
162 fprintf(stderr, "Got a start byte in the middle of the packet\n");
164 data[0] = (unsigned char) c;
176 /* Read calibration from a file */
178 static int readCalibration(char *filename) {
182 int calfd = open(filename, O_RDONLY);
184 /* If the file doesn't exist, then just use the default values */
185 if (calfd < 0) return(0);
187 /* The data will be formated as 4 values:
191 if (read(calfd, buffer, sizeof(buffer)) <= 0) {
196 sscanf(buffer, "%d %d %d %d\n", &calData[0], &calData[1], &calData[2], &calData[3]);
200 /* Check the calibration values, and reverse them if we have to */
202 if (calData[1] < calData[0]) {
203 int tmp = calData[1];
205 calData[1] = calData[0];
211 if (calData[3] < calData[2]) {
212 int tmp = calData[3];
213 calData[3] = calData[2];
222 static void doScale(int x, int y, int *dx, int *dy) {
224 /* Scale the data appropriately to the screen */
226 *dx = ((x - calData[0]) * scrdev.xres) / (calData[1] - calData[0]);
227 *dy = ((y - calData[2]) * scrdev.yres) / (calData[3] - calData[2]);
230 static int MOU_Open(MOUSEDEVICE *pmd) {
234 mouFd = initSerial("/dev/ttyS1");
237 EPRINTF("Unable to open /dev/ttyS1 for reading\n");
241 if (readCalibration("/etc/caldata") == -1) {
243 EPRINTF("Unable to get the calibration data\n");
249 GdHideCursor(&scrdev);
253 static int MOU_GetButtonInfo(void) {
257 static void MOU_GetDefaultAccel(int *pscale, int *pthresh) {
262 static void MOU_Close(void) {
270 static int MOU_Read(MWCOORD *px, MWCOORD *py, MWCOORD *pz, int *pb) {
274 int totalx = 0, totaly = 0;
277 /* For now, only grab one reading, this touchscreen is pretty */
278 /* good, so jitter isn't a problem */
280 for(i = 0; i < 1; i++) {
284 val = getInput(mouFd, data);
286 if (val == -1) return(0);
287 else if (val == 0) break;
289 x = data[1] | ((data[0] & 0x38) << 4);
290 y = data[2] | ((data[0] & 0x07) << 7);
292 /* Send the data in for calibration */
293 doScale(x, y, &sx, &sy);
295 /* Inver the axii if needed */
298 sx = scrdev.xres - sx;
300 sy = scrdev.yres - sy;
306 if (i == 0) return(0);
311 /* Record the last state of the mouse */
313 *pb = ( (data[0] & 0x40) ? MWBUTTON_L : 0 );
316 if (!*pb) return(3); else return(2);
319 MOUSEDEVICE mousedev = {