-
Notifications
You must be signed in to change notification settings - Fork 13
/
dcled.c
2035 lines (1813 loc) · 57.8 KB
/
dcled.c
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
/* dcled - userland driver for Dream Cheeky (Dream Link?) USB LED Message Board
* Copyright 2009,2010 Jeff Jahr <[email protected]>
* This is free software. G'head, use it all you want.
* Version 1.0 took ~12 hours to write, and was my first foray into usb
* device programming. This is probably not a real good example of how to
* do ANYTHING. Sun Jan 4 00:18:41 PST 2009 -jsj
*/
/* dcled contains contributions from -
* Andy Scheller
* Michael Wensley
* Glen Smith
* Robert Flick
* Stefan Misch
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>
#include <time.h>
#include <hid.h>
#include <sys/select.h>
#include <glob.h>
#define VENDOR 0x1d34
#define PRODUCT 0x0013
/* heh heh. ledsx. thats almost dirty. */
#define LEDSX 21
#define LEDSY 7
#define FONTX 5
#define FONTY 7
#define MAXTESTPAT 8
/* if it isn't defined in the make file... */
#ifndef FONTDIR
#define FONTDIR "."
#endif
#ifndef DCLEDVERSION
#define DCLEDVERSION "0.0"
#endif
/* This is the usage that gets sent to the device, from the docs supplied by
* Alvin Wong. */
struct ledpkt {
unsigned char brightness;
unsigned char row;
unsigned char data1[3];
unsigned char data2[3];
};
/* struct for a version 0 font definition */
struct ledfont {
char *name;
char *description;
char *author;
int dataformat;
int dispwidth;
int dispheight;
char data[256][FONTY];
};
/* list of available fonts. */
struct ledfontlist {
struct ledfont *font;
struct ledfontlist *next;
};
/* This is a basic definition of the led display. 0,0 is the upper left. */
struct ledscreen {
HIDInterface *hid;
unsigned char path_in_len;
int *path_in;
int brightness;
int scrolldelay;
int scrolldir;
int preamble;
time_t lastupdate;
int led[LEDSX][LEDSY];
struct ledfont *font;
};
struct preamble {
const char *name;
const char *description;
const char *author;
};
struct preamble preambles[] = {
{ "none" ,"The default","Jeff Jahr" },
{ "dots" ,"A string of random dots","Jeff Jahr" },
{ "static" ,"Warms up like an old TV","Jeff Jahr" },
{ "squiggle","A squiggly line","Jeff Jahr" },
{ "clock24" ,"Shows the 24 hour time", "Andy Scheller"},
{ "clock" ,"Shows the time", "Andy Scheller" },
{ "spiral" ,"Draws a spiral", "Glen Smith" },
{ "fire" ,"A nice warm hearth", "Glen Smith" },
{ "bcdclock" ,"Shows the time in binary", "Jeff Jahr" },
{ NULL,NULL,NULL }
};
void clearscreen(int mode,struct ledscreen *disp);
void scroll(int dir, struct ledscreen *disp);
void scrollmsg(struct ledscreen *disp, char* buf);
void scrollrndfade(struct ledscreen *disp, int isend, int width);
void scrollpreamble(int isend, struct ledscreen *disp);
void staticwarmup(struct ledscreen *disp, int isend, int width);
void scrollsquiggle(struct ledscreen *disp, int isend, int width);
void printtime(struct ledscreen *disp,int mode);
void spiral(struct ledscreen *disp);
void fire(struct ledscreen *disp, int isend);
struct ledfont *allocfont(void);
struct ledfont *initfont1(struct ledfont *target);
struct ledfont *initfont2(struct ledfont *target);
struct ledfont *loadfont(char *filename);
void savefonts(struct ledfontlist *fontlist);
int debug = 0;
int echo = 0;
int fastprint = 0;
int nodev = 0;
int repeat=0;
char version[] = DCLEDVERSION;
/* This sends a copy of the ledscreen to stdout. Useful as a debug. */
void print_screen (struct ledscreen *sc) {
int x,y;
for (y=0;y<LEDSY;y++) {
for(x=0;x<LEDSX;x++) {
if(sc->led[x][y] == 1) {
printf("O");
} else {
printf(".");
}
}
printf("\n");
};
printf("\n");
return;
}
/* This sends the led array in the ledscreen structure to the device. Call it
* when you want to update the display. Seems like the device wants to clear
* itself after about a second, so you might want to keep calling this to
* refesh the device. */
void send_screen (struct ledscreen *disp) {
char bigpkt[sizeof(struct ledpkt) * 4];
struct ledpkt pkt;
int row, col, bytep, bitp;
if(debug) print_screen(disp);
time(&(disp->lastupdate));
for(row=0;row<LEDSY;row+=2) {
pkt.brightness = disp->brightness;
pkt.row = row;
for(bytep=0;bytep<=2;bytep++) {
pkt.data1[bytep] = 0xff;
pkt.data2[bytep] = 0xff;
}
bytep=2;
bitp=0;
for(col=0;col<LEDSX;col++) {
if (disp->led[col][row] == 1) {
pkt.data1[bytep] &= ~(1<<bitp);
}
if (disp->led[col][row+1] == 1) {
pkt.data2[bytep] &= ~(1<<bitp);
}
bitp++;
if(bitp==8) {
bitp=0;
bytep--;
}
}
memcpy(bigpkt+((row/2)*sizeof(struct ledpkt)), &pkt, sizeof(struct ledpkt));
}
if (!nodev) {
const unsigned int chunk_count = 4;
const unsigned int chunk_size = sizeof(bigpkt) / chunk_count;
int chunk;
for (chunk=0;chunk<chunk_count;chunk++) {
hid_set_output_report(disp->hid, disp->path_in, disp->path_in_len, bigpkt+(chunk*chunk_size), chunk_size);
}
}
}
/* clears the screen either by flasing it to zeros, or by scrolling it away.*/
void clearscreen(int mode,struct ledscreen *disp) {
int x,y;
switch (mode) {
case 1: /* west scroll */
for(x=0;x<=LEDSX;x++) {
send_screen(disp);
usleep(disp->scrolldelay);
scroll(3,disp);
}
break;
default:
case 0: /* clear */
for (x=0;x<LEDSX;x++) {
for(y=0;y<LEDSY;y++) {
disp->led[x][y] = 0;
}
}
break;
break;
}
return;
}
int open_hiddev(struct ledscreen *disp) {
/* Oh my oh my. Need to find the output report descriptor for the device.
* Using the output of 'lsusb -d 1d34:0013 -vvv', one can determin that:
*
* The report descriptor usage page is 65280. The usage we want is #1.
* Why? Dunno! Seems to work though. That usage isnt rooted under any
* items so the path length is 1.
*
* So to make an input path that works with libhid:
*
* ((65280 << 16) + 1) == 0xff000001 */
unsigned char const pathlen = 1;
const int PATH_IN[] = { 0xff000001 };
HIDInterface* hid = NULL;
hid_return ret;
/* go search for a matching device. I'm not clear on how this works if
* there is more than one message board installed. -jsj */
HIDInterfaceMatcher leds = { VENDOR, PRODUCT, NULL, NULL, 0 };
if ((ret = hid_init()) != HID_RET_SUCCESS) {
fprintf(stderr,"hid_init failed with return code %d\n",ret);
return EXIT_FAILURE;
}
hid = hid_new_HIDInterface();
if (hid == NULL) {
fprintf(stderr,"hid_new_HIDInterface() failed.\n",ret);
return EXIT_FAILURE;
}
/* This call will forceably remove control of the device from the
* kernel hid driver. The fourth argument is the number of times the
* call should try to close the device and snag it for our own devious
* purposes before giving up. It is what was used in the libhid
* example code. */
if ((ret=hid_force_open(hid, 0, &leds, 3)) != HID_RET_SUCCESS) {
fprintf(stderr,"hid_force_open failed with return code %d\n",ret);
return EXIT_FAILURE;
}
disp->hid = hid;
disp->path_in_len = pathlen;
disp->path_in = (int*)malloc(pathlen * sizeof(int));
memcpy(disp->path_in,PATH_IN,pathlen * sizeof(int));
return(EXIT_SUCCESS);
}
/* shift the disp one pixel in the given direction.*/
/* not all directions are implemented yet.*/
void scroll(int dir, struct ledscreen *disp) {
int x,y;
switch (dir) {
case 0: /* north - up */
case 1: /* east - right */
case 2: /* south - down */
case 3: /* west - left */
default:
for(x=0;x<LEDSX-1;x++) {
for(y=0;y<LEDSY;y++) {
disp->led[x][y] = disp->led[x+1][y];
}
}
for(y=0;y<LEDSY;y++) {
disp->led[LEDSX-1][y] = 0;
}
}
return;
}
/* overlay a character onto the display with the leftmost edge of the char
* starting at xloc. */
void printchar(struct ledscreen *disp, char c, int xloc) {
int cx,cy,x,y;
char *f;
f=(char*)disp->font->data;
for(y=0;y<(disp->font->dispheight);y++) {
for (cx=0,x=xloc;x<LEDSX && cx<=(disp->font->dispwidth);cx++,x++) {
disp->led[x][y] =
((*(f+(c*FONTY)+y) & (1<<cx))!=0)? 1:disp->led[x][y];
}
}
}
/* print a single decimal digit as a vertical line of binary bits. */
void printBCDchar(struct ledscreen *disp, char c, int xloc, int baseline) {
int val;
if (baseline >= LEDSY || baseline-5 < 0) {
baseline = LEDSY-1;
}
if (!isdigit(c)) {
if ( c == '|' ) {
val = 15;
} else {
val = 0;
}
} else {
val = c - '0';
}
disp->led[xloc][baseline-0]=(val&1)?1:0;
disp->led[xloc][baseline-1]=(val&2)?1:0;
disp->led[xloc][baseline-2]=(val&4)?1:0;
disp->led[xloc][baseline-3]=(val&8)?1:0;
return;
}
/* make one of those BCD clock displays that every geek seems to have.*/
void bcdclock(struct ledscreen *disp, int forever) {
time_t rawtime;
time_t firsttime;
struct tm* timeinfo;
char strtime[11];
int x,y;
int digit;
char *p;
time(&firsttime);
time(&rawtime);
while(forever || (rawtime - firsttime) < 3) {
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(strtime, 11, " %I %M %S ", timeinfo);
clearscreen(0, disp);
/* draw the digits */
for(p=strtime,x=1;*p;p++,x+=2) {
printBCDchar(disp, *p, x, LEDSY-3);
}
send_screen(disp);
usleep(disp->scrolldelay * 10);
}
}
/* Contributed by Andy Scheller */
/* displays the time in HH:MM format, set mode to 1 for 24-hour display.*/
void printtime(struct ledscreen *disp, int mode) {
time_t rawtime;
struct tm* timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
char strtime[6];
strftime(strtime, 6, (mode==1?"%H:%M":"%I:%M"), timeinfo);
clearscreen(0, disp);
printchar(disp,strtime[0],0);
printchar(disp,strtime[1],4);
printchar(disp,strtime[2],8);
printchar(disp,strtime[3],12);
printchar(disp,strtime[4],16);
}
/* This is Andy Scheller's printtime code, modified to run forever and wiggle
* the colon. Heh heh. Thats almost dirty.*/
void clockmode(struct ledscreen *disp, int mode, int forever) {
time_t rawtime;
time_t firsttime;
struct tm* timeinfo;
int oddsec;
char strtime[6];
if (mode == 3) {
bcdclock(disp,forever);
return;
}
time(&firsttime);
time(&rawtime);
while(forever || (rawtime - firsttime) < 3) {
time(&rawtime);
timeinfo = localtime(&rawtime);
oddsec = rawtime%2;
if(mode == 1) {
strftime(strtime, 6, "%H:%M", timeinfo);
} else {
strftime(strtime, 6, "%I:%M", timeinfo);
}
clearscreen(0, disp);
printchar(disp,strtime[0],0);
printchar(disp,strtime[1],4);
printchar(disp,strtime[2],8+oddsec);
printchar(disp,strtime[3],12);
printchar(disp,strtime[4],16);
send_screen(disp);
usleep(100000);
}
}
/* Prints a test pattern to the screen. pattern 0 and 1 are the all-on
* and all-off patterns, which are pretty useful. */
void testpatern (int which, struct ledscreen *disp) {
int x, y;
/*char font[256*FONTY];*/
switch(which) {
case 0: /* all off */
for (x=0;x<LEDSX;x++) {
for(y=0;y<LEDSY;y++) {
disp->led[x][y] = 0;
}
}
break;
case 1: /* all on */
for (x=0;x<LEDSX;x++) {
for(y=0;y<LEDSY;y++) {
disp->led[x][y] = 1;
}
}
break;
case 2: /* box borders */
for(x=0;x<LEDSX;x++) {
disp->led[x][0] = 1;
disp->led[x][LEDSY-1] = 1;
}
for(y=0;y<LEDSY;y++) {
disp->led[0][y] = 1;
disp->led[LEDSX-1][y] = 1;
}
break;
case 3: /* box corners */
disp->led[0][0] = 1;
disp->led[LEDSX-1][0] = 1;
disp->led[LEDSX-1][LEDSY-1] = 1;
disp->led[0][LEDSY-1] = 1;
break;
case 4: /* simple diag */
for(x=0,y=0;x<LEDSX && y<LEDSY;x++, y++) {
disp->led[x][y] = 1;
}
break;
case 5: /* random */
for (x=0;x<LEDSX;x++) {
for(y=0;y<LEDSY;y++) {
disp->led[x][y] = (rand()>=(RAND_MAX/2))?1:0;
}
}
break;
case 6: /* Jeff */
printchar(disp,'J',0);
printchar(disp,'e',4);
printchar(disp,'f',8);
printchar(disp,'f',12);
printchar(disp,'!',16);
break;
case 7: /* 12h time */
printtime(disp,0);
break;
case 8: /* 24h time */
printtime(disp,1);
break;
}
return;
}
/* scrolls random data. kinda cool, used during development. */
void scrolltest(struct ledscreen *disp) {
int x,y;
clearscreen(0,disp);
while(1) {
for(y=0;y<LEDSY;y++) {
disp->led[LEDSX-1][y] = (rand()<(RAND_MAX/3))?1:0;
}
send_screen(disp);
usleep(disp->scrolldelay);
scroll(3,disp);
}
}
/* call the right graphic header depending on style and direction. */
void scrollpreamble(int isend, struct ledscreen *disp) {
int miltime=0;
switch (disp->preamble) {
case 7:
fire(disp,isend);
return;
case 6:
spiral(disp);
return;
case 8:
if (!isend) {
clockmode(disp,3,0);
}
return;
case 5:
if (!isend) {
clockmode(disp,2,0);
}
return;
case 4:
if (!isend) {
clockmode(disp,1,0);
}
return;
case 3:
scrollsquiggle(disp,isend,5*LEDSX);
return;
case 2:
staticwarmup(disp,isend,5*LEDSX);
return;
case 1:
scrollrndfade(disp,isend,3*LEDSX);
return;
case 0:
default:
if (isend && !fastprint) {
clearscreen(1,disp);
}
return;
}
}
/* make the display warm up with static, like an old tv. */
void staticwarmup(struct ledscreen *disp, int isend, int width) {
int count, origbright;
int x,y;
if (!isend) {
origbright = disp->brightness;
for (count=0;count<width;count++) {
for (x=0;x<LEDSX;x++) {
for(y=0;y<LEDSY;y++) {
disp->led[x][y] =
(1.8*(float)rand()*width/(float)(RAND_MAX)<count)?1:0;
disp->brightness =
(int)(3.0*(((float)count/(float)width)+0.3*(float)rand()/(float)(RAND_MAX)));
}
}
send_screen(disp);
}
disp->brightness = origbright;
clearscreen(0,disp);
send_screen(disp);
usleep(disp->scrolldelay * 5);
} else {
clearscreen(1,disp);
}
}
/* A random banner */
void scrollrndfade(struct ledscreen *disp, int isend, int width) {
int x,y;
int odds;
if (!isend) {
for(odds=0;odds<width;odds++){
for(y=0;y<LEDSY;y++) {
disp->led[LEDSX-1][y] =
(1.8*(float)rand()*width/(float)(RAND_MAX)<odds)?1:0;
}
send_screen(disp);
usleep(disp->scrolldelay);
scroll(3,disp);
}
scroll(3,disp);
scroll(3,disp);
} else {
scroll(3,disp);
scroll(3,disp);
for(odds=width;odds>0;odds--){
for(y=0;y<LEDSY;y++) {
disp->led[LEDSX-1][y] =
(1.8*(float)rand()*width/(float)(RAND_MAX)<odds)?1:0;
}
send_screen(disp);
usleep(disp->scrolldelay);
scroll(3,disp);
}
clearscreen(1,disp);
}
}
/* its a squiggly line. */
void scrollsquiggle(struct ledscreen *disp, int isend, int width) {
int count;
float yf,diff;
int y;
scroll(3,disp);
yf = y = LEDSY/2;
for(count=0;count<width;count++){
diff = (2.0*((float)rand()/(float)(RAND_MAX))-1.0);
yf+=diff;
if(yf > LEDSY-1 || yf < 0.0) {
yf-=1.5*diff;
}
y = yf;
disp->led[LEDSX-1][y] = 1;
scroll(3,disp);
send_screen(disp);
usleep(disp->scrolldelay);
}
scroll(3,disp);
usleep(disp->scrolldelay);
}
/* its a spiral. Contributed by Glen Smith. */
void spiral(struct ledscreen *disp) {
int offset,count=0,state,x,y;
clearscreen(0, disp);
offset=0;
state=1;
while(count<2) {
while(offset<4) {
for (x=0;x<(LEDSX-offset);x++) {
disp->led[x][y+offset] = state;
send_screen(disp);
usleep(100);
}
for (y=0;y<(LEDSY-offset);y++) {
disp->led[(LEDSX-1)-offset][y] = state;
send_screen(disp);
usleep(100);
}
for (x=(LEDSX-1-offset);x>=0;x--) {
disp->led[x][(LEDSY-1)-offset] = state;
send_screen(disp);
usleep(100);
}
for (y=(LEDSY-1-offset);y>=(1+offset);y--) {
disp->led[x+1+offset][y] = state;
send_screen(disp);
usleep(100);
}
y=0;
x=0;
offset++;
}
offset=0;
count++;
state=0;
}
}
/* Its a fire. Contributed by Glen Smith */
/* Made to fade in and out for a preamble by Jeff Jahr */
void fire(struct ledscreen *disp, int isend) {
int count, x, y, origbright, height;
float scale,burnrate;
origbright = disp->brightness;
if (isend) {
scale = 1.0;
} else {
scale = 0.1;
}
while (scale <= 1.0 && scale >= 0) {
count=0;
while(count<4) {
clearscreen(0, disp);
for (x=0;x<LEDSX;x++) {
height = scale * (rand()/(((double)RAND_MAX + 1) / LEDSY));
for (y=LEDSY-1;y>=(LEDSY-height);y--) {
disp->led[x][y] = 1;
}
}
disp->brightness = rand()/(((double)RAND_MAX + 1) / 2);
send_screen(disp);
usleep(disp->scrolldelay / 10);
count++;
}
if (scale>0.9) {
burnrate = 0.005 * ((isend)?-1:1);
} else {
burnrate = 0.06 * ((isend)?-1:1);
}
scale = scale + burnrate;
}
disp->brightness = origbright;
clearscreen(0, disp);
}
/* Runs a bunch of test patterns. */
void fancytest(struct ledscreen *disp, struct ledfontlist *fontlist) {
int count, tp, b, preamble;
char msg[8192];
struct ledfontlist *flp;
struct ledfont *origfont;
int origspeed;
/* in case you want to write out the compiled in fonts. */
/* savefonts(fontlist); */
for (count=0;count<1;count++){
for (tp=1;tp<=MAXTESTPAT;tp++) {
testpatern(0,disp);
testpatern(tp,disp);
for (b=0;b<=2;b++) {
disp->brightness = b;
send_screen(disp);
usleep(disp->scrolldelay);
}
}
}
testpatern(0,disp);
/* now demo all of the preambles. */
preamble = 1;
disp->scrolldelay = 10000;
origspeed=disp->scrolldelay;
disp->scrolldelay = origspeed * 4;
scrollmsg(disp," Preambles \n");
disp->scrolldelay = origspeed;
while ( preambles[preamble].name != NULL) {
snprintf(msg,8192,"%s - %-10s <%s>",
preambles[preamble].name,
preambles[preamble].description,
preambles[preamble].author
);
disp->preamble = preamble;
scrollpreamble(0,disp);
scrollmsg(disp,msg);
scrollpreamble(1,disp);
preamble++;
}
disp->preamble = 0;
origfont = disp->font;
origspeed=disp->scrolldelay;
disp->scrolldelay = origspeed * 4;
scrollmsg(disp," Fonts \n");
disp->scrolldelay = origspeed;
for(flp=fontlist;flp;flp=flp->next) {
snprintf(msg,8192,"'%s' - %s <%s>\n",
flp->font->name,
flp->font->description,
flp->font->author
);
disp->font = flp->font;
scrollmsg(disp,msg);
}
disp->preamble = 0;
disp->font = origfont;
disp->scrolldelay = 10000;
disp->brightness = 0;
sprintf(msg,"*** dcled %s Copyright 2011 Jeff Jahr <[email protected]> *** ",version);
scrollmsg(disp,msg);
}
/* scroll a character onto the display. Not all directions are implemented
* yet.*/
void scrollchar(struct ledscreen *disp, char ch) {
int w;
for (w=1;w<=(disp->font->dispwidth);w++) {
scroll(3,disp);
printchar(disp,ch,LEDSX-w);
send_screen(disp);
usleep(disp->scrolldelay);
}
if(echo){
fputc(ch,stdout);
fflush(stdout);
}
}
/* char by char, scroll a string onto the screen. This is pretty much what
* we're here for, isn't it? */
void scrollmsg(struct ledscreen *disp, char* buf) {
char *p;
p=buf;
while(*p){
scrollchar(disp,*p++);
}
return;
}
void keep_lit(struct ledscreen *disp) {
while (1) {
send_screen(disp);
usleep(250000);
}
}
/* Jump to the end of the message and print just the chars that will fit on the
* screen without scrolling. Contributed by Robert Flick */
void printmsg(struct ledscreen *disp, char* msg) {
char* p = msg;
int i = 0;
int numchars, start;
/* set pack to 0 if you want space between letters and only four chars per display. */
int pack = 1;
clearscreen(0, disp);
numchars = LEDSX / ((disp->font->dispwidth)-pack);
start = strlen(msg);
if (start > 0) {
/* trim a trailing newline, which would otherwise be rendered as a
* space by the line-by-line IO reader.*/
if ((*(msg+start-1)) == '\n') {
start = start - numchars -1;
} else {
start = start - numchars;
}
}
/* don't backtrack too far. */
if(start<0) {
start=0;
}
p = msg + start;
/* put 'em on the screen. */
while(*p != '\0') {
printchar(disp,*p, i*((disp->font->dispwidth)-pack));
p++;
i++;
}
/* take care of echoing */
p=msg;
if(echo){
while(*p != '\0') {
fputc(*p++,stdout);
}
fflush(stdout);
}
send_screen(disp);
/* line print speed can be controlled with the --speed parameter */
usleep(disp->scrolldelay);
}
/* Same thing, bigger scope. Scroll a whole file. */
void scrollfile(struct ledscreen *disp, FILE* cin) {
char buf[8192];
char *nl;
time_t now;
time_t temptime;
fd_set inset;
struct timeval timeout;
while(1) {
FD_ZERO(&inset);
FD_SET(fileno(cin),&inset);
/* How long to wait for IO before coming back to refresh the display. */
timeout.tv_sec = 0;
timeout.tv_usec = 250000;
select(fileno(cin)+1,&inset,NULL,NULL,&timeout);
/* does not work well on stdin/tail -f */
/* if ( ! FD_ISSET(fileno(cin),&inset) ) { */
if ( (! FD_ISSET(fileno(cin),&inset)) && (cin != stdin)) {
if (repeat) {
/* send_screen twiddles the last update time. just undo it.*/
temptime = disp->lastupdate;
send_screen(disp);
disp->lastupdate = temptime;
}
} else {
if(fgets(buf,8192,cin)) {
time(&now);
if(disp->preamble!= 0 &&
(now - disp->lastupdate) > 10) {
scrollpreamble(0,disp);
}
if(fastprint) {
printmsg(disp,buf);
} else {
scrollmsg(disp,buf);
}
} else {
/* end of file. */
return;
}
}
}
}
struct ledfont *allocfont(void) {
struct ledfont *f;
int i,j;
f = malloc(sizeof(struct ledfont));
f->name = NULL;
f->description = NULL;
f->author = NULL;
f->dataformat = 0;
f->dispwidth = FONTX;
f->dispheight = FONTY;
/* fill it with a pattern. */
for(i=0;i<256;i++) {
for(j=0;j<FONTY;j++) {
f->data[i][j]=(j&1)?0xaa:0x55;
}
}
return(f);
}
/* called via atexit. */
void bye(void) {
/* if I wanted to clean it up right, this is what I'd do.*/
/* if(hid) {
hid_close(hid);
hid_delete_HIDInterface(&hid);
hid_cleanup();
}
*/
}
struct ledfontlist *initfonts(char *dirname) {