Repository navigation
Expand file tree
/
Copy pathgeniePi.c
More file actions
642 lines (480 loc) · 14.9 KB
/
Copy pathgeniePi.c
File metadata and controls
642 lines (480 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/*
* geniePi.c:
* Library to utilise the 4D Systems Genie interface to displays
* that have been created using the Visi-Genie creator platform.
* Primarily aimed at the Raspberry Pi, however this may be used
* on most Linux platforms with a serial connection (USB on
* on-board) to the 4D Systems Intelligent displays.
*
* Gordon Henderson, December 2012, <projects@drogon.net>
* Copyright (c) 2012 4D Systems PTY Ltd, Sydney, Australia
***********************************************************************
* This file is part of geniePi:
* geniePi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* geniePi 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with geniePi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <sys/time.h>
#include <sched.h>
#include <pthread.h>
#include "geniePi.h"
#ifndef TRUE
# define TRUE (1==1)
# define FALSE (1==0)
#endif
// Input buffer:
// max. unprocessed replys from the display
#define MAX_GENIE_REPLYS 16
static struct genieReplyStruct genieReplys [MAX_GENIE_REPLYS] ;
static int genieReplysHead = 0 ;
static int genieReplysTail = 0 ;
#ifdef GENIE_DEBUG
int genieAck = FALSE ;
int genieNak = FALSE ;
int genieChecksumErrors = 0 ;
int genieTimeouts = 0 ;
#else
static int genieAck = FALSE ;
static int genieNak = FALSE ;
static int genieChecksumErrors = 0 ;
static int genieTimeouts = 0 ;
#endif
static pthread_mutex_t genieMutex ;
static int genieFd = -1;
/*
* genieOpen:
* Open and initialise the serial port, setting all the right
* port parameters - or as many as are required - hopefully!
*********************************************************************************
*/
static int genieOpen (char *device, int baud)
{
struct termios options ;
speed_t myBaud ;
int status, fd ;
switch (baud)
{
case 50: myBaud = B50 ; break ;
case 75: myBaud = B75 ; break ;
case 110: myBaud = B110 ; break ;
case 134: myBaud = B134 ; break ;
case 150: myBaud = B150 ; break ;
case 200: myBaud = B200 ; break ;
case 300: myBaud = B300 ; break ;
case 600: myBaud = B600 ; break ;
case 1200: myBaud = B1200 ; break ;
case 1800: myBaud = B1800 ; break ;
case 2400: myBaud = B2400 ; break ;
case 9600: myBaud = B9600 ; break ;
case 19200: myBaud = B19200 ; break ;
case 38400: myBaud = B38400 ; break ;
case 57600: myBaud = B57600 ; break ;
case 115200: myBaud = B115200 ; break ;
case 230400: myBaud = B230400 ; break ;
default:
return -2 ;
}
if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
return -1 ;
fcntl (fd, F_SETFL, O_RDWR) ;
// Get and modify current options:
tcgetattr (fd, &options) ;
cfmakeraw (&options) ;
cfsetispeed (&options, myBaud) ;
cfsetospeed (&options, myBaud) ;
options.c_cflag |= (CLOCAL | CREAD) ;
options.c_cflag &= ~PARENB ;
options.c_cflag &= ~CSTOPB ;
options.c_cflag &= ~CSIZE ;
options.c_cflag |= CS8 ;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
options.c_oflag &= ~OPOST ;
options.c_cc [VMIN] = 0 ;
options.c_cc [VTIME] = 100 ; // Ten seconds (100 deciseconds)
tcsetattr (fd, TCSANOW | TCSAFLUSH, &options) ;
ioctl (fd, TIOCMGET, &status);
status |= TIOCM_DTR ;
status |= TIOCM_RTS ;
ioctl (fd, TIOCMSET, &status);
usleep (10000) ; // 10mS
return fd ;
}
/*
* genieFlush:
* Flush the serial buffers (both tx & rx)
*********************************************************************************
*/
static void genieFlush (int fd)
{
tcflush (fd, TCIOFLUSH) ;
}
/*
* genieClose:
* Release the serial port and any other data we have.
*********************************************************************************
*/
void genieClose (void)
{
close (genieFd) ;
}
/*
* genieDataAvail:
* Return the number of bytes of data avalable to be read in the serial port
*********************************************************************************
*/
static int genieDataAvail (int fd)
{
int result ;
if (ioctl (fd, FIONREAD, &result) == -1)
return -1 ;
return result ;
}
/*
* Support timing functions. These are based on those in wiringPi
*********************************************************************************
*/
static unsigned long long epoch ;
static unsigned int millis (void)
{
struct timeval tv ;
unsigned long long t1 ;
gettimeofday (&tv, NULL) ;
t1 = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;
return (unsigned int)(t1 - epoch) ;
}
static void delay (unsigned int howLong)
{
struct timespec sleeper, dummy ;
sleeper.tv_sec = (time_t)(howLong / 1000) ;
sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
nanosleep (&sleeper, &dummy) ;
}
static void delayMicroseconds (unsigned int howLong)
{
struct timespec sleeper, dummy ;
sleeper.tv_sec = 0 ;
sleeper.tv_nsec = (long)(howLong * 1000) ;
nanosleep (&sleeper, &dummy) ;
}
/*
* genieGetchar:
* Return a single character from the device, or -1 if nothing
* avalable in 5mS.
*********************************************************************************
*/
static int genieGetchar (void)
{
unsigned int timeUp = millis () + 5 ;
unsigned char x ;
while (millis () < timeUp)
if (genieDataAvail (genieFd))
{
if (read (genieFd, &x, 1) == 1)
return ((int)x) & 0xFF ;
return -1 ;
}
else
delayMicroseconds (101) ;
return -1 ;
}
/*
* geniePutchar:
* Send a single character (byte) to the Genie display
*********************************************************************************
*/
static void geniePutchar (int data)
{
unsigned char c = (unsigned char)data ;
write (genieFd, &c, 1) ;
}
/*
* genieReplyListener:
* Listen for bytes from the Genie display and build them into
* messages, and store them in the message queue
*********************************************************************************
*/
static void *genieReplyListener (void *data)
{
struct sched_param sched ;
int pri = 20 ;
unsigned int cmd, object, index, msb, lsb, csum ;
struct genieReplyStruct *reply ;
int next ;
// Set to a real-time priority
memset (&sched, 0, sizeof(sched)) ;
if (pri > sched_get_priority_max (SCHED_RR))
pri = sched_get_priority_max (SCHED_RR) ;
sched.sched_priority = pri ;
sched_setscheduler (0, SCHED_RR, &sched) ;
// Make sure the serial port is actually open
while (genieFd == -1)
delay (1) ;
// Loop, forever, catching events coming back from the display
for (;;)
{
while ((cmd = genieGetchar ()) == -1)
;
if (cmd == GENIE_ACK)
{ genieAck = TRUE ; continue ; }
if (cmd == GENIE_NAK)
{ genieNak = TRUE ; continue ; }
csum = cmd ;
if ((object = genieGetchar ()) == -1) { ++genieTimeouts ; continue ; } ; csum ^= object ;
if ((index = genieGetchar ()) == -1) { ++genieTimeouts ; continue ; } ; csum ^= index ;
if ((msb = genieGetchar ()) == -1) { ++genieTimeouts ; continue ; } ; csum ^= msb ;
if ((lsb = genieGetchar ()) == -1) { ++genieTimeouts ; continue ; } ; csum ^= lsb ;
if (genieGetchar () != csum)
{
++genieChecksumErrors ;
continue ;
}
// We have valid data - store it into the buffer
next = (genieReplysHead + 1) & (MAX_GENIE_REPLYS - 1) ;
if (next != genieReplysTail) // Discard rather than overflow
{
reply = &genieReplys [genieReplysHead] ;
reply->cmd = cmd ;
reply->object = object ;
reply->index = index ;
reply->data = msb << 8 | lsb ;
genieReplysHead = next ;
}
}
return (void *)NULL ;
}
/*
* genieReplyAvail:
* Return TRUE if there are pending messages from the display
*********************************************************************************
*/
int genieReplyAvail (void)
{
return (genieReplysHead != genieReplysTail) ;
}
/*
* genieGetReply:
* Get the next message out of the Genie Reply queue, or
* wait until a message has been sent from the display
*********************************************************************************
*/
void genieGetReply (struct genieReplyStruct *reply)
{
while (!genieReplyAvail ())
delay (1) ;
memcpy (reply, &genieReplys [genieReplysTail], sizeof (struct genieReplyStruct)) ;
genieReplysTail = (genieReplysTail + 1) & (MAX_GENIE_REPLYS - 1) ;
}
/*
* genieReadObj:
* Send a read object command to the Genie display and get the result back
*********************************************************************************
*/
static int _genieReadObj (int object, int index)
{
struct genieReplyStruct reply ;
unsigned int timeUp, checksum ;
// Discard any pending replys
while (genieReplyAvail ())
genieGetReply (&reply) ;
genieAck = genieNak = FALSE ;
geniePutchar (GENIE_READ_OBJ) ; checksum = GENIE_READ_OBJ ;
geniePutchar (object) ; checksum ^= object ;
geniePutchar (index) ; checksum ^= index ;
geniePutchar (checksum) ;
// Wait up to 50mS for a reply
// Note: @9600 baud 5 characters will take 5mS!
for (timeUp = millis () + 50 ; millis () < timeUp ;)
{
if (genieNak)
return -1 ;
if (genieReplyAvail ())
{
genieGetReply (&reply) ;
if ((reply.cmd == GENIE_REPORT_OBJ) && (reply.object == object) && (reply.index == index))
return reply.data ;
}
delayMicroseconds (101) ;
}
return -1 ;
}
int genieReadObj (int object, int index)
{
int result ;
pthread_mutex_lock (&genieMutex) ;
result = _genieReadObj (object, index) ;
pthread_mutex_unlock (&genieMutex) ;
return result ;
}
/*
* genieWriteObj:
* Write data to an object on the display
*********************************************************************************
*/
static int _genieWriteObj (int object, int index, unsigned int data)
{
unsigned int checksum, msb, lsb ;
lsb = (data >> 0) & 0xFF ;
msb = (data >> 8) & 0xFF ;
genieAck = genieNak = FALSE ;
geniePutchar (GENIE_WRITE_OBJ) ; checksum = GENIE_WRITE_OBJ ;
geniePutchar (object) ; checksum ^= object ;
geniePutchar (index) ; checksum ^= index ;
geniePutchar (msb) ; checksum ^= msb ;
geniePutchar (lsb) ; checksum ^= lsb ;
geniePutchar (checksum) ;
// TODO: Really ought to timeout here, but if the display doesn't
// respond, then it's probably game over anyway.
while ((genieAck == FALSE) && (genieNak == FALSE))
delay (1) ;
return 0 ;
}
int genieWriteObj (int object, int index, unsigned int data)
{
int result ;
pthread_mutex_lock (&genieMutex) ;
result = _genieWriteObj (object, index, data) ;
pthread_mutex_unlock (&genieMutex) ;
return result ;
}
/*
* genieWriteContrast:
* Alter the display contrast (backlight)
*********************************************************************************
*/
static int _genieWriteContrast (int value)
{
unsigned int checksum ;
genieAck = genieNak = FALSE ;
geniePutchar (GENIE_WRITE_CONTRAST) ; checksum = GENIE_WRITE_CONTRAST ;
geniePutchar (value) ; checksum ^= value ;
geniePutchar (checksum) ;
// TODO: Really ought to timeout here, but if the display doesn't
// respond, then it's probably game over anyway.
while ((genieAck == FALSE) && (genieNak == FALSE))
delay (1) ;
return 0 ;
}
int genieWriteContrast (int value)
{
int result ;
pthread_mutex_lock (&genieMutex) ;
result = _genieWriteContrast (value) ;
pthread_mutex_unlock (&genieMutex) ;
return result ;
}
/*
* genieWriteStr:
* genieWriteStrU:
* Write a string to the display (ASCII, or Unicode)
* There is only one string type object.
*********************************************************************************
*/
static int _genieWriteStr (int index, char *string)
{
char *p ;
unsigned int checksum ;
int len = strlen (string) ;
if (len > 255)
return -1 ;
genieAck = genieNak = FALSE ;
geniePutchar (GENIE_WRITE_STR) ; checksum = GENIE_WRITE_STR ;
geniePutchar (index) ; checksum ^= index ;
geniePutchar ((unsigned char)len) ; checksum ^= len ;
for (p = string ; *p ; ++p)
{
geniePutchar (*p) ;
checksum ^= *p ;
}
geniePutchar (checksum) ;
// TODO: Really ought to timeout here, but if the display doesn't
// respond, then it's probably game over anyway.
while ((genieAck == FALSE) && (genieNak == FALSE))
delay (1) ;
return 0 ;
}
static int _genieWriteStrU (int index, char *string)
{
char *p ;
unsigned int checksum ;
int len = strlen (string) ;
if (len > 255)
return -1 ;
genieAck = genieNak = FALSE ;
geniePutchar (GENIE_WRITE_STRU) ; checksum = GENIE_WRITE_STRU ;
geniePutchar (index) ; checksum ^= index ;
geniePutchar ((unsigned char)len) ; checksum ^= len ;
for (p = string ; *p ; ++p)
{
geniePutchar (*p) ;
checksum ^= *p ;
}
geniePutchar (checksum) ;
// TODO: Really ought to timeout here, but if the display doesn't
// respond, then it's probably game over anyway.
while ((genieAck == FALSE) && (genieNak == FALSE))
delay (1) ;
return 0 ;
}
int genieWriteStr (int index, char *string)
{
int result ;
pthread_mutex_lock (&genieMutex) ;
result = _genieWriteStr (index, string) ;
pthread_mutex_unlock (&genieMutex) ;
return result ;
}
int genieWriteStrU (int index, char *string)
{
int result ;
pthread_mutex_lock (&genieMutex) ;
result = _genieWriteStrU (index, string) ;
pthread_mutex_unlock (&genieMutex) ;
return result ;
}
/*
* genieSetup:
* Initialise the Genie Display system
*********************************************************************************
*/
int genieSetup (char *device, int baud)
{
int i ;
pthread_t myThread ;
struct timeval tv ;
if ((genieFd = genieOpen (device, baud)) < 0)
return -1 ;
genieFlush (genieFd) ;
gettimeofday (&tv, NULL) ;
epoch = (tv.tv_sec * 1000000 + tv.tv_usec) / 1000 ;
// Try to overcome a bug with the Raspberry Pi (or indeed, any other serial
// port that sends a garbage character when you first open it),
// by sending out dummy characters until we get a NAK back, hopefully
// then the display sequencer will be in a stable state.
for (i = 0 ; i < 10 ; ++i)
{
geniePutchar ('X') ;
if (genieGetchar () == GENIE_NAK)
break ;
}
return pthread_create (&myThread, NULL, genieReplyListener, NULL) ;
}