-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathgeniePi.c
More file actions
1037 lines (847 loc) · 26 KB
/
geniePi.c
File metadata and controls
1037 lines (847 loc) · 26 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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.
*
* Written by Gordon Henderson, December 2012, <projects@drogon.net>
* Updated/Maintained by 4D Systems Pty Ltd, www.4dsystems.com.au
* Copyright (c) 2020 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 <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <float.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
union FloatLongFrame {
float floatValue;
int32_t longValue;
uint32_t ulongValue;
int16_t wordValue[2];
};
// Input buffer:
// max. unprocessed replys from the display
#define MAX_GENIE_REPLYS 16
static struct genieReplyStruct genieReplys [MAX_GENIE_REPLYS] ;
static struct genieMagicReplyStruct genieMagicReplys [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=0, lsb=0, csum ;
unsigned int totalLength = 0, readLength;
unsigned int byteData[100];
struct genieReplyStruct *reply ;
struct genieMagicReplyStruct *magicByteReply ;
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 ;
// Check if data received is for magic bytes. If not proceed to normal process in 'else' routine
if(cmd == GENIE_REPORT_MAGIC_BYTES || cmd == GENIE_REPORT_DOUBLE_BYTES)
{
// total receivable bytes for Double Byte is twice the length specified
totalLength = index;
if(cmd == GENIE_REPORT_DOUBLE_BYTES) totalLength = index * 2;
for(readLength = 0; readLength < totalLength; readLength ++)
{
if ((byteData[readLength] = genieGetchar ()) == -1) { ++genieTimeouts ; continue ; } ; csum ^= byteData[readLength] ;
}
}
else
{
if ((msb = genieGetchar ()) == -1) { ++genieTimeouts ; continue ; } ; csum ^= msb ;
if ((lsb = genieGetchar ()) == -1) { ++genieTimeouts ; continue ; } ; csum ^= lsb ;
}
// Check resulting checksum value and compare with received checksum byte
if (genieGetchar () != csum)
{
++genieChecksumErrors ;
continue ;
}
// We have valid data - store it into the buffer
next = (genieReplysHead + 1) & (MAX_GENIE_REPLYS - 1) ;
if(cmd == GENIE_REPORT_MAGIC_BYTES || cmd == GENIE_REPORT_DOUBLE_BYTES)
{
if (next != genieReplysTail) // Discard rather than overflow
{
magicByteReply = &genieMagicReplys[genieReplysHead] ;
magicByteReply->cmd = cmd ;
magicByteReply->index = object ;
magicByteReply->length = index ;
if(cmd == GENIE_REPORT_MAGIC_BYTES)
{
for(readLength = 0; readLength < totalLength; readLength ++)
{
magicByteReply->data[readLength] = byteData[readLength];
}
}
if(cmd == GENIE_REPORT_DOUBLE_BYTES)
{
for(readLength = 1; readLength < totalLength; readLength ++)
{
magicByteReply->data[readLength] = byteData[readLength * 2] << 8 | byteData[(readLength * 2) + 1];
}
}
genieReplysHead = next ;
}
}
else
{
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 ;
}
int genieWriteShortToIntLedDigits (int index, int16_t data) {
return genieWriteObj(GENIE_OBJ_ILED_DIGITS_L, index, data);
}
int genieWriteFloatToIntLedDigits (int index, float data) {
union FloatLongFrame frame;
frame.floatValue = data;
int retval;
retval = genieWriteObj(GENIE_OBJ_ILED_DIGITS_H, index, frame.wordValue[1]);
if (retval != 1) return retval;
return genieWriteObj(GENIE_OBJ_ILED_DIGITS_L, index, frame.wordValue[0]);
}
int genieWriteLongToIntLedDigits (int index, int32_t data) {
union FloatLongFrame frame;
frame.longValue = data;
int retval;
retval = genieWriteObj(GENIE_OBJ_ILED_DIGITS_H, index, frame.wordValue[1]);
if (retval != 1) return retval;
return genieWriteObj(GENIE_OBJ_ILED_DIGITS_L, index, frame.wordValue[0]);
}
/*
* 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:
* 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 ;
}
int genieWriteStr (int index, char *string)
{
int result ;
pthread_mutex_lock (&genieMutex) ;
result = _genieWriteStr (index, string) ;
pthread_mutex_unlock (&genieMutex) ;
return result ;
}
/*
* genieWriteStrU:
* Write a string to the display (ASCII, or Unicode)
* There is only one string type object.
*********************************************************************************
*/
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)>> 8); checksum ^= ((*p) >> 8) ;
geniePutchar ((*p)&0xFF); checksum ^= ((*p)&0xFF) ;
}
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 genieWriteStrU (int index, char *string)
{
int result ;
pthread_mutex_lock (&genieMutex) ;
result = _genieWriteStrU (index, string) ;
pthread_mutex_unlock (&genieMutex) ;
return result ;
}
/*
* genieWriteStrD:
* Write a decimal value to the display (ASCII)
* There is only one string type object.
*********************************************************************************
*/
static int _genieMakeStr (int index, long n, int base)
{
char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
char *str = &buf[sizeof(buf) - 1];
int neg = 0;
if(n < 0) neg = 1;
n = abs(n);
*str = '\0';
do {
unsigned long m = n;
n /= base;
char c = m - base * n;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
if(neg) *--str = '-';
_genieWriteStr (index,str);
return 0 ;
}
int genieWriteStrHex (int index, long n)
{
int result ;
pthread_mutex_lock (&genieMutex) ;
result = _genieMakeStr (index, n, 16);
pthread_mutex_unlock (&genieMutex) ;
return result ;
}
int genieWriteStrOct (int index, long n)
{
int result ;
pthread_mutex_lock (&genieMutex) ;
result = _genieMakeStr (index, n, 8);
pthread_mutex_unlock (&genieMutex) ;
return result ;
}
int genieWriteStrBin (int index, long n)
{
int result ;
pthread_mutex_lock (&genieMutex) ;
result = _genieMakeStr (index, n, 2);
pthread_mutex_unlock (&genieMutex) ;
return result ;
}
int genieWriteStrBase (int index, long n, int base)
{
int result ;
pthread_mutex_lock (&genieMutex) ;
result = _genieMakeStr (index, n, base);
pthread_mutex_unlock (&genieMutex) ;
return result ;
}
int genieWriteStrDec (int index, long n)
{
int result ;
pthread_mutex_lock (&genieMutex) ;
result = _genieMakeStr (index, n, 10);
pthread_mutex_unlock (&genieMutex) ;
return result ;
}
/*
* genieWriteStrFloat:
* Write a float string to the display.
* There is only one byte per index in array.
*********************************************************************************
*/
static int _genieWriteStrFloat (int index, float n, int precision)
{
char str[sizeof(long)];
gcvt(n, precision, str);
_genieWriteStr(index, str);
return 0;
}
int genieWriteStrFloat (int index, float n, int precision)
{
int result ;
pthread_mutex_lock (&genieMutex) ;
result = _genieWriteStrFloat (index,n,precision);
pthread_mutex_unlock (&genieMutex) ;
return result ;
}
int genieWriteInhLabelDefault (int index) {
return genieWriteObj(GENIE_OBJ_ILABELB, index, -1);
}
/*
* genieWriteInhLabel:
* Write a string to a inherent label on the display
*********************************************************************************
*/
static int _genieWriteInhLabel (int index, char *string)
{
char *p ;
unsigned int checksum ;
int len = strlen (string) ;
if (len > 255)
return -1 ;
genieAck = genieNak = FALSE ;
geniePutchar (GENIE_WRITE_INH_LABEL) ; checksum = GENIE_WRITE_INH_LABEL ;
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 genieWriteInhLabel (int index, char *string)
{
int result ;
pthread_mutex_lock (&genieMutex) ;
result = _genieWriteInhLabel (index, string) ;
pthread_mutex_unlock (&genieMutex) ;
return result ;
}
/*
* genieWriteInhLabelDec:
* Write a decimal value to the display (ASCII)
* There is only one string type object.
*********************************************************************************
*/
static int _genieMakeInhLabel (int index, long n, int base)
{
char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
char *str = &buf[sizeof(buf) - 1];
int neg = 0;
if(n < 0) neg = 1;
n = abs(n);
*str = '\0';
do {
unsigned long m = n;
n /= base;
char c = m - base * n;
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
if(neg) *--str = '-';
_genieWriteInhLabel (index,str);
return 0 ;
}
int genieWriteInhLabelHex (int index, long n)
{
int result ;
pthread_mutex_lock (&genieMutex) ;
result = _genieMakeInhLabel (index, n, 16);
pthread_mutex_unlock (&genieMutex) ;
return result ;
}
int genieWriteInhLabelOct (int index, long n)
{
int result ;
pthread_mutex_lock (&genieMutex) ;
result = _genieMakeInhLabel (index, n, 8);
pthread_mutex_unlock (&genieMutex) ;
return result ;
}
int genieWriteInhLabelBin (int index, long n)
{
int result ;
pthread_mutex_lock (&genieMutex) ;
result = _genieMakeInhLabel (index, n, 2);
pthread_mutex_unlock (&genieMutex) ;
return result ;
}
int genieWriteInhLabelBase (int index, long n, int base)
{
int result ;
pthread_mutex_lock (&genieMutex) ;
result = _genieMakeInhLabel (index, n, base);
pthread_mutex_unlock (&genieMutex) ;
return result ;
}
int genieWriteInhLabelDec (int index, long n)
{
int result ;
pthread_mutex_lock (&genieMutex) ;
result = _genieMakeInhLabel (index, n, 10);
pthread_mutex_unlock (&genieMutex) ;
return result ;
}
/*
* genieWriteInhLabelFloat:
* Write a float string to the display.
* There is only one byte per index in array.
*********************************************************************************
*/
static int _genieWriteInhLabelFloat (int index, float n, int precision)
{
char str[sizeof(long)];
gcvt(n, precision, str);
_genieWriteInhLabel(index, str);
return 0;
}
int genieWriteInhLabelFloat (int index, float n, int precision)
{
int result ;
pthread_mutex_lock (&genieMutex) ;
result = _genieWriteInhLabelFloat (index,n,precision);
pthread_mutex_unlock (&genieMutex) ;
return result ;
}
/*
* genieWriteMagicBytes:
* Write a byte array to the display.
* There is only one byte per index in array.
*********************************************************************************
*/
static int _genieWriteMagicBytes (int magic_index, unsigned int *byteArray)
{
unsigned int *p ;
unsigned int checksum ;
int len = sizeof(byteArray) / sizeof(int);
// int len = sizeof(byteArray) / sizeof(byteArray[0]);
if (len > 255)
return -1 ;
genieAck = genieNak = FALSE ;
geniePutchar (GENIE_MAGIC_BYTES) ; checksum = GENIE_MAGIC_BYTES ;
geniePutchar (magic_index) ; checksum ^= magic_index ;
geniePutchar ((unsigned char)len) ; checksum ^= len ;
for (p = byteArray ; *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 genieWriteMagicBytes (int magic_index,unsigned int *byteArray)
{
int result ;
pthread_mutex_lock (&genieMutex) ;
result = _genieWriteMagicBytes (magic_index, byteArray) ;
pthread_mutex_unlock (&genieMutex) ;
return result ;
}
/*
* genieWriteDoubleBytes:
* Write a double byte array to the display.
* There are two bytes per index in array.
*********************************************************************************
*/
static int _genieWriteDoubleBytes (int magic_index,unsigned int *doubleByteArray)
{
unsigned int *p ;
unsigned int checksum ;
int len = sizeof (doubleByteArray) / sizeof(int);
// int len = sizeof (doubleByteArray) / sizeof(doubleByteArray[0]);
if (len > 255)
return -1 ;
genieAck = genieNak = FALSE ;
geniePutchar (GENIE_DOUBLE_BYTES) ; checksum = GENIE_MAGIC_BYTES ;
geniePutchar (magic_index) ; checksum ^= magic_index ;
geniePutchar ((unsigned char)len) ; checksum ^= len ;
for (p = doubleByteArray ; *p ; ++p)
{
geniePutchar ((*p) >> 8) ;
checksum ^= (*p) >> 8 ;
geniePutchar ((*p) &0xFF) ;
checksum ^= (*p) &0xFF ;
}
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 genieWriteDoubleBytes (int magic_index,unsigned int *doubleByteArray)
{
int result ;
pthread_mutex_lock (&genieMutex) ;
result = _genieWriteDoubleBytes (magic_index, doubleByteArray) ;
pthread_mutex_unlock (&genieMutex) ;
return result ;