2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
28 #include "SDL_stdinc.h"
29 #include "SDL_fbvideo.h"
30 #include "SDL_fbelo.h"
33 calibration default values
34 values are read from the following environment variables:
42 static int ELO_MIN_X = 400;
43 static int ELO_MAX_X = 3670;
44 static int ELO_MIN_Y = 500;
45 static int ELO_MAX_Y = 3540;
47 #define ELO_SNAP_SIZE 6
48 #define ELO_TOUCH_BYTE 'T'
51 #define ELO_PARAMETER 'P'
52 #define ELO_REPORT 'B'
55 #define ELO_INIT_CHECKSUM 0xAA
57 #define ELO_BTN_PRESS 0x01
58 #define ELO_STREAM 0x02
59 #define ELO_BTN_RELEASE 0x04
61 #define ELO_TOUCH_MODE 0x01
62 #define ELO_STREAM_MODE 0x02
63 #define ELO_UNTOUCH_MODE 0x04
64 #define ELO_RANGE_CHECK_MODE 0x40
65 #define ELO_TRIM_MODE 0x02
66 #define ELO_CALIB_MODE 0x04
67 #define ELO_SCALING_MODE 0x08
68 #define ELO_TRACKING_MODE 0x40
70 #define ELO_SERIAL_MASK 0xF8
72 #define ELO_SERIAL_IO '0'
74 #define ELO_MAX_TRIALS 3
75 #define ELO_MAX_WAIT 100000
76 #define ELO_UNTOUCH_DELAY 5
77 #define ELO_REPORT_DELAY 1
81 int eloParsePacket(unsigned char* mousebuf, int* dx, int* dy, int* button_state) {
82 static int elo_button = 0;
83 static int last_x = 0;
84 static int last_y = 0;
87 /* Check if we have a touch packet */
88 if (mousebuf[1] != ELO_TOUCH_BYTE) {
92 x = ((mousebuf[4] << 8) | mousebuf[3]);
93 y = ((mousebuf[6] << 8) | mousebuf[5]);
95 if((SDL_abs(x - last_x) > ELO_SNAP_SIZE) || (SDL_abs(y - last_y) > ELO_SNAP_SIZE)) {
96 *dx = ((mousebuf[4] << 8) | mousebuf[3]);
97 *dy = ((mousebuf[6] << 8) | mousebuf[5]);
107 if ( (mousebuf[2] & 0x07) == ELO_BTN_PRESS ) {
110 if ( (mousebuf[2] & 0x07) == ELO_BTN_RELEASE ) {
114 *button_state = elo_button;
118 /* Convert the raw coordinates from the ELO controller
119 to a screen position.
121 void eloConvertXY(_THIS, int *dx, int *dy) {
124 int width = ELO_MAX_X - ELO_MIN_X;
125 int height = ELO_MAX_Y - ELO_MIN_Y;
127 *dx = ((int)cache_vinfo.xres - ((int)cache_vinfo.xres * (input_x - ELO_MIN_X)) / width);
128 *dy = (cache_vinfo.yres * (input_y - ELO_MIN_Y)) / height;
134 int eloGetPacket(unsigned char* buffer, int* buffer_p, int* checksum, int fd) {
139 num_bytes = ELO_PACKET_SIZE;
143 (char *) (buffer + *buffer_p),
144 ELO_PACKET_SIZE - *buffer_p);
149 fprintf(stderr, "System error while reading from Elographics touchscreen.\n");
155 if ((*buffer_p == 0) && (buffer[0] != ELO_START_BYTE)) {
156 SDL_memcpy(&buffer[0], &buffer[1], num_bytes-1);
159 if (*buffer_p < ELO_PACKET_SIZE-1) {
160 *checksum = *checksum + buffer[*buffer_p];
161 *checksum = *checksum % 256;
168 if (*buffer_p == ELO_PACKET_SIZE) {
169 ok = (*checksum == buffer[ELO_PACKET_SIZE-1]);
170 *checksum = ELO_INIT_CHECKSUM;
187 int eloSendPacket(unsigned char* packet, int fd)
190 int sum = ELO_INIT_CHECKSUM;
192 packet[0] = ELO_START_BYTE;
193 for (i = 0; i < ELO_PACKET_SIZE-1; i++) {
197 packet[ELO_PACKET_SIZE-1] = sum;
199 result = write(fd, packet, ELO_PACKET_SIZE);
201 if (result != ELO_PACKET_SIZE) {
203 printf("System error while sending to Elographics touchscreen.\n");
215 int eloWaitForInput(int fd, int timeout)
222 FD_SET(fd, &readfds);
224 to.tv_usec = timeout;
226 r = select(FD_SETSIZE, &readfds, NULL, NULL, &to);
232 int eloWaitReply(unsigned char type, unsigned char *reply, int fd) {
236 int sum = ELO_INIT_CHECKSUM;
242 result = eloWaitForInput(fd, ELO_MAX_WAIT);
245 ok = eloGetPacket(reply, &reply_p, &sum, fd);
247 if (ok && reply[1] != type && type != ELO_PARAMETER) {
249 fprintf(stderr, "Wrong reply received\n");
256 fprintf(stderr, "No input!\n");
263 } while(!ok && (i>0));
272 int eloWaitAck(int fd) {
273 unsigned char packet[ELO_PACKET_SIZE];
276 if (eloWaitReply(ELO_ACK, packet, fd)) {
277 for (i = 0, nb_errors = 0; i < 4; i++) {
278 if (packet[2 + i] != '0') {
283 if (nb_errors != 0) {
285 fprintf(stderr, "Elographics acknowledge packet reports %d errors\n", nb_errors);
298 int eloSendQuery(unsigned char *request, unsigned char* reply, int fd) {
301 if (eloSendPacket(request, fd)) {
302 ok = eloWaitReply(toupper(request[1]), reply, fd);
316 int eloSendControl(unsigned char* control, int fd) {
317 if (eloSendPacket(control, fd)) {
318 return eloWaitAck(fd);
327 int eloInitController(int fd) {
328 unsigned char req[ELO_PACKET_SIZE];
329 unsigned char reply[ELO_PACKET_SIZE];
330 const char *buffer = NULL;
333 struct termios mouse_termios;
335 /* try to read the calibration values */
336 buffer = SDL_getenv("SDL_ELO_MIN_X");
338 ELO_MIN_X = SDL_atoi(buffer);
340 buffer = SDL_getenv("SDL_ELO_MAX_X");
342 ELO_MAX_X = SDL_atoi(buffer);
344 buffer = SDL_getenv("SDL_ELO_MIN_Y");
346 ELO_MIN_Y = SDL_atoi(buffer);
348 buffer = SDL_getenv("SDL_ELO_MAX_Y");
350 ELO_MAX_Y = SDL_atoi(buffer);
354 fprintf( stderr, "ELO calibration values:\nmin_x: %i\nmax_x: %i\nmin_y: %i\nmax_y: %i\n",
361 /* set comm params */
362 SDL_memset(&mouse_termios, 0, sizeof(mouse_termios));
363 mouse_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL;
364 mouse_termios.c_cc[VMIN] = 1;
365 result = tcsetattr(fd, TCSANOW, &mouse_termios);
369 fprintf( stderr, "Unable to configure Elographics touchscreen port\n");
374 SDL_memset(req, 0, ELO_PACKET_SIZE);
375 req[1] = tolower(ELO_PARAMETER);
376 if (!eloSendQuery(req, reply, fd)) {
378 fprintf( stderr, "Not at the specified rate or model 2310, will continue\n");
382 SDL_memset(req, 0, ELO_PACKET_SIZE);
383 req[1] = tolower(ELO_ID);
384 if (eloSendQuery(req, reply, fd)) {
386 fprintf(stderr, "Ok, controller configured!\n");
391 fprintf( stderr, "Unable to ask Elographics touchscreen identification\n");
396 SDL_memset(req, 0, ELO_PACKET_SIZE);
398 req[3] = ELO_TOUCH_MODE | ELO_STREAM_MODE | ELO_UNTOUCH_MODE;
399 req[4] = ELO_TRACKING_MODE;
400 if (!eloSendControl(req, fd)) {
402 fprintf( stderr, "Unable to change Elographics touchscreen operating mode\n");
407 SDL_memset(req, 0, ELO_PACKET_SIZE);
409 req[2] = ELO_UNTOUCH_DELAY;
410 req[3] = ELO_REPORT_DELAY;
411 if (!eloSendControl(req, fd)) {
413 fprintf( stderr, "Unable to change Elographics touchscreen reports timings\n");
421 int eloReadPosition(_THIS, int fd, int* x, int* y, int* button_state, int* realx, int* realy) {
422 unsigned char buffer[ELO_PACKET_SIZE];
424 int checksum = ELO_INIT_CHECKSUM;
426 while(pointer < ELO_PACKET_SIZE) {
427 if(eloGetPacket(buffer, &pointer, &checksum, fd)) {
432 if(!eloParsePacket(buffer, realx, realy, button_state)) {
439 eloConvertXY(this, x, y);