This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_fbelo.c
462 lines (386 loc) · 10.1 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2006 Sam Lantinga
4
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.
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Lesser General Public License for more details.
14
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19
Sam Lantinga
20
slouken@libsdl.org
21
*/
22
#include "SDL_config.h"
23
24
25
26
27
#include <unistd.h>
#include <sys/time.h>
#include <ctype.h>
28
#include "SDL_stdinc.h"
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "SDL_fbvideo.h"
#include "SDL_fbelo.h"
/*
calibration default values
values are read from the following environment variables:
SDL_ELO_MIN_X
SDL_ELO_MAX_X
SDL_ELO_MIN_Y
SDL_ELO_MAX_Y
*/
static int ELO_MIN_X = 400;
static int ELO_MAX_X = 3670;
static int ELO_MIN_Y = 500;
static int ELO_MAX_Y = 3540;
#define ELO_SNAP_SIZE 6
48
#define ELO_TOUCH_BYTE 'T'
49
50
51
52
#define ELO_ID 'I'
#define ELO_MODE 'M'
#define ELO_PARAMETER 'P'
#define ELO_REPORT 'B'
53
#define ELO_ACK 'A'
54
55
56
#define ELO_INIT_CHECKSUM 0xAA
57
#define ELO_BTN_PRESS 0x01
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#define ELO_STREAM 0x02
#define ELO_BTN_RELEASE 0x04
#define ELO_TOUCH_MODE 0x01
#define ELO_STREAM_MODE 0x02
#define ELO_UNTOUCH_MODE 0x04
#define ELO_RANGE_CHECK_MODE 0x40
#define ELO_TRIM_MODE 0x02
#define ELO_CALIB_MODE 0x04
#define ELO_SCALING_MODE 0x08
#define ELO_TRACKING_MODE 0x40
#define ELO_SERIAL_MASK 0xF8
#define ELO_SERIAL_IO '0'
#define ELO_MAX_TRIALS 3
#define ELO_MAX_WAIT 100000
#define ELO_UNTOUCH_DELAY 5
#define ELO_REPORT_DELAY 1
/* eloParsePacket
*/
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
int
eloParsePacket(unsigned char *mousebuf, int *dx, int *dy, int *button_state)
{
static int elo_button = 0;
static int last_x = 0;
static int last_y = 0;
int x, y;
/* Check if we have a touch packet */
if (mousebuf[1] != ELO_TOUCH_BYTE) {
return 0;
}
x = ((mousebuf[4] << 8) | mousebuf[3]);
y = ((mousebuf[6] << 8) | mousebuf[5]);
if ((SDL_abs(x - last_x) > ELO_SNAP_SIZE)
|| (SDL_abs(y - last_y) > ELO_SNAP_SIZE)) {
*dx = ((mousebuf[4] << 8) | mousebuf[3]);
*dy = ((mousebuf[6] << 8) | mousebuf[5]);
} else {
*dx = last_x;
*dy = last_y;
}
last_x = *dx;
last_y = *dy;
if ((mousebuf[2] & 0x07) == ELO_BTN_PRESS) {
elo_button = 1;
}
if ((mousebuf[2] & 0x07) == ELO_BTN_RELEASE) {
elo_button = 0;
}
*button_state = elo_button;
return 1;
118
119
120
121
122
}
/* Convert the raw coordinates from the ELO controller
to a screen position.
*/
123
124
125
126
127
128
129
130
131
132
133
134
void
eloConvertXY(_THIS, int *dx, int *dy)
{
int input_x = *dx;
int input_y = *dy;
int width = ELO_MAX_X - ELO_MIN_X;
int height = ELO_MAX_Y - ELO_MIN_Y;
*dx =
((int) cache_vinfo.xres -
((int) cache_vinfo.xres * (input_x - ELO_MIN_X)) / width);
*dy = (cache_vinfo.yres * (input_y - ELO_MIN_Y)) / height;
135
136
137
138
139
}
/* eloGetPacket
*/
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
int
eloGetPacket(unsigned char *buffer, int *buffer_p, int *checksum, int fd)
{
int num_bytes;
int ok;
if (fd == 0) {
num_bytes = ELO_PACKET_SIZE;
} else {
num_bytes = read(fd,
(char *) (buffer + *buffer_p),
ELO_PACKET_SIZE - *buffer_p);
}
if (num_bytes < 0) {
155
#ifdef DEBUG_MOUSE
156
157
fprintf(stderr,
"System error while reading from Elographics touchscreen.\n");
158
#endif
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
return 0;
}
while (num_bytes) {
if ((*buffer_p == 0) && (buffer[0] != ELO_START_BYTE)) {
SDL_memcpy(&buffer[0], &buffer[1], num_bytes - 1);
} else {
if (*buffer_p < ELO_PACKET_SIZE - 1) {
*checksum = *checksum + buffer[*buffer_p];
*checksum = *checksum % 256;
}
(*buffer_p)++;
}
num_bytes--;
}
if (*buffer_p == ELO_PACKET_SIZE) {
ok = (*checksum == buffer[ELO_PACKET_SIZE - 1]);
*checksum = ELO_INIT_CHECKSUM;
*buffer_p = 0;
if (!ok) {
return 0;
}
return 1;
} else {
return 0;
}
188
189
190
191
192
}
/* eloSendPacket
*/
193
194
int
eloSendPacket(unsigned char *packet, int fd)
195
{
196
197
int i, result;
int sum = ELO_INIT_CHECKSUM;
198
199
200
201
202
203
204
packet[0] = ELO_START_BYTE;
for (i = 0; i < ELO_PACKET_SIZE - 1; i++) {
sum += packet[i];
sum &= 0xFF;
}
packet[ELO_PACKET_SIZE - 1] = sum;
205
206
result = write(fd, packet, ELO_PACKET_SIZE);
207
208
if (result != ELO_PACKET_SIZE) {
209
#ifdef DEBUG_MOUSE
210
printf("System error while sending to Elographics touchscreen.\n");
211
#endif
212
213
214
215
return 0;
} else {
return 1;
}
216
217
218
219
220
}
/* eloWaitForInput
*/
221
222
int
eloWaitForInput(int fd, int timeout)
223
{
224
225
226
fd_set readfds;
struct timeval to;
int r;
227
228
229
230
231
FD_ZERO(&readfds);
FD_SET(fd, &readfds);
to.tv_sec = 0;
to.tv_usec = timeout;
232
233
234
r = select(FD_SETSIZE, &readfds, NULL, NULL, &to);
return r;
235
236
237
238
}
/* eloWaitReply
*/
239
240
241
242
243
244
245
int
eloWaitReply(unsigned char type, unsigned char *reply, int fd)
{
int ok;
int i, result;
int reply_p = 0;
int sum = ELO_INIT_CHECKSUM;
246
247
248
249
i = ELO_MAX_TRIALS;
do {
ok = 0;
250
251
result = eloWaitForInput(fd, ELO_MAX_WAIT);
252
253
254
if (result > 0) {
ok = eloGetPacket(reply, &reply_p, &sum, fd);
255
256
if (ok && reply[1] != type && type != ELO_PARAMETER) {
257
#ifdef DEBUG_MOUSE
258
fprintf(stderr, "Wrong reply received\n");
259
#endif
260
261
262
ok = 0;
}
} else {
263
#ifdef DEBUG_MOUSE
264
fprintf(stderr, "No input!\n");
265
#endif
266
}
267
268
269
270
if (result == 0) {
i--;
}
271
} while (!ok && (i > 0));
272
273
return ok;
274
275
276
277
278
279
}
/* eloWaitAck
*/
280
281
282
283
284
285
286
287
288
289
290
291
int
eloWaitAck(int fd)
{
unsigned char packet[ELO_PACKET_SIZE];
int i, nb_errors;
if (eloWaitReply(ELO_ACK, packet, fd)) {
for (i = 0, nb_errors = 0; i < 4; i++) {
if (packet[2 + i] != '0') {
nb_errors++;
}
}
292
293
if (nb_errors != 0) {
294
#ifdef DEBUG_MOUSE
295
296
297
fprintf(stderr,
"Elographics acknowledge packet reports %d errors\n",
nb_errors);
298
#endif
299
300
301
302
303
}
return 1;
} else {
return 0;
}
304
305
306
307
308
}
/* eloSendQuery --
*/
309
310
311
312
313
314
315
316
317
318
319
320
321
322
int
eloSendQuery(unsigned char *request, unsigned char *reply, int fd)
{
int ok;
if (eloSendPacket(request, fd)) {
ok = eloWaitReply(toupper(request[1]), reply, fd);
if (ok) {
ok = eloWaitAck(fd);
}
return ok;
} else {
return 0;
}
323
324
325
326
327
}
/* eloSendControl
*/
328
329
330
331
332
333
334
335
int
eloSendControl(unsigned char *control, int fd)
{
if (eloSendPacket(control, fd)) {
return eloWaitAck(fd);
} else {
return 0;
}
336
337
338
339
}
/* eloInitController
*/
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
int
eloInitController(int fd)
{
unsigned char req[ELO_PACKET_SIZE];
unsigned char reply[ELO_PACKET_SIZE];
const char *buffer = NULL;
int result = 0;
struct termios mouse_termios;
/* try to read the calibration values */
buffer = SDL_getenv("SDL_ELO_MIN_X");
if (buffer) {
ELO_MIN_X = SDL_atoi(buffer);
}
buffer = SDL_getenv("SDL_ELO_MAX_X");
if (buffer) {
ELO_MAX_X = SDL_atoi(buffer);
}
buffer = SDL_getenv("SDL_ELO_MIN_Y");
if (buffer) {
ELO_MIN_Y = SDL_atoi(buffer);
}
buffer = SDL_getenv("SDL_ELO_MAX_Y");
if (buffer) {
ELO_MAX_Y = SDL_atoi(buffer);
}
367
#ifdef DEBUG_MOUSE
368
369
370
fprintf(stderr,
"ELO calibration values:\nmin_x: %i\nmax_x: %i\nmin_y: %i\nmax_y: %i\n",
ELO_MIN_X, ELO_MAX_X, ELO_MIN_Y, ELO_MAX_Y);
371
372
#endif
373
374
375
376
377
/* set comm params */
SDL_memset(&mouse_termios, 0, sizeof(mouse_termios));
mouse_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL;
mouse_termios.c_cc[VMIN] = 1;
result = tcsetattr(fd, TCSANOW, &mouse_termios);
378
379
if (result < 0) {
380
#ifdef DEBUG_MOUSE
381
fprintf(stderr, "Unable to configure Elographics touchscreen port\n");
382
#endif
383
384
return 0;
}
385
386
387
388
SDL_memset(req, 0, ELO_PACKET_SIZE);
req[1] = tolower(ELO_PARAMETER);
if (!eloSendQuery(req, reply, fd)) {
389
#ifdef DEBUG_MOUSE
390
391
fprintf(stderr,
"Not at the specified rate or model 2310, will continue\n");
392
#endif
393
}
394
395
396
397
SDL_memset(req, 0, ELO_PACKET_SIZE);
req[1] = tolower(ELO_ID);
if (eloSendQuery(req, reply, fd)) {
398
#ifdef DEBUG_MOUSE
399
fprintf(stderr, "Ok, controller configured!\n");
400
#endif
401
} else {
402
#ifdef DEBUG_MOUSE
403
404
fprintf(stderr,
"Unable to ask Elographics touchscreen identification\n");
405
#endif
406
407
408
409
410
411
412
413
return 0;
}
SDL_memset(req, 0, ELO_PACKET_SIZE);
req[1] = ELO_MODE;
req[3] = ELO_TOUCH_MODE | ELO_STREAM_MODE | ELO_UNTOUCH_MODE;
req[4] = ELO_TRACKING_MODE;
if (!eloSendControl(req, fd)) {
414
#ifdef DEBUG_MOUSE
415
416
fprintf(stderr,
"Unable to change Elographics touchscreen operating mode\n");
417
#endif
418
419
420
421
422
423
424
425
return 0;
}
SDL_memset(req, 0, ELO_PACKET_SIZE);
req[1] = ELO_REPORT;
req[2] = ELO_UNTOUCH_DELAY;
req[3] = ELO_REPORT_DELAY;
if (!eloSendControl(req, fd)) {
426
#ifdef DEBUG_MOUSE
427
428
fprintf(stderr,
"Unable to change Elographics touchscreen reports timings\n");
429
#endif
430
431
return 0;
}
432
433
return 1;
434
}
435
436
437
438
439
440
441
442
int
eloReadPosition(_THIS, int fd, int *x, int *y, int *button_state, int *realx,
int *realy)
{
unsigned char buffer[ELO_PACKET_SIZE];
int pointer = 0;
int checksum = ELO_INIT_CHECKSUM;
443
444
445
446
while (pointer < ELO_PACKET_SIZE) {
if (eloGetPacket(buffer, &pointer, &checksum, fd)) {
break;
447
}
448
}
449
450
451
452
453
454
455
if (!eloParsePacket(buffer, realx, realy, button_state)) {
return 0;
}
*x = *realx;
*y = *realy;
456
457
eloConvertXY(this, x, y);
458
459
return 1;
460
}
461
462
/* vi: set ts=4 sw=4 expandtab: */