-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
2814 lines (2465 loc) · 74.6 KB
/
main.cpp
File metadata and controls
2814 lines (2465 loc) · 74.6 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
/*
* Casio Calculator Watch for Teensy 4.1
*
* Circuit:
* * 4 push buttons: pulled low. Attached to digitial pins 12, 11, 10, 9
* * Hex keypad: 4 wires go into output digital pins HKA, HKB, HKC, HKC,
* 4 wires go into digitial input pins HK1, HK2, HK3, HK4
* * piezo buzzor. digitial output pin (pin 24) goes through a resitor and into buzzer positive terminal
* ground terminal.
* * 128x64 oled display, hooked up to I2C lines SDA (pin 18)/SCL (pin 19)
* pull up resistors are on each pin
* The I2C slave address of the display is TARGET (0x3C).
*
* Code Description:
* This program is divided into these sections:
* Disp routines - Talk to the OLED display, send frame buffer to display, set pixels
* Debug routines - flash led light debug routines
* Device routines - talk to all the connected devices and interrupt service routines
* Draw routines - fonts and other graphic operations
* Casio routines - emmulate the casio calculator watch
* main
*
* Dependencies:
* - The 'IntervalTimer' class is used to provide the 1/100 second time signal.
* - The 'Wire' class is used to talk to the display using I2C protocol
* - tone() to generate casio watch sounds
*
*/
#include <Arduino.h>
#include <Wire.h>
#include <stdlib.h>
//////////////////////////////////////////////////////////////////////
//
// begin DISP
//
//////////////////////////////////////////////////////////////////////
const int TARGET = 0x3C; // I2C slave address for display
#define FRAME_SIZE 1024 // # of bytes to contain a frame 128x64
#define BLACK_ON_WHITE yes
#ifdef BLACK_ON_WHITE
# define CLR_MASK 0xff
# define PIXEL_ON(p) ((p) == 0)
# define PIXEL_VALUE_ON 0
# define PIXEL_VALUE_OFF 1
#else
# define CLR_MASK 0x00
# define PIXEL_ON(p) ((p) != 0)
# define PIXEL_VALUE_ON 1
# define PIXEL_VALUE_OFF 0
#endif
void disp_setup()
{
Wire.setSDA(18);
Wire.setSCL(19);
Wire.begin();
Wire.setClock(400000); // overwritten by begin()
}
int disp_init()
{
/*
Set MUX Ratio [$A8, $3F]
Set display offset [$D3, $00]
Set start line [$40]
Set segment re-map $A0 / $A1
Set COM output scan direction $C0 / $C8
Set COM pin hardware configuration [$DA, $02]
Set contrast [$81, $7F]
Resume the display $A4
Set Oscillator frequency [$D5, $80]
Enable charge pump [$8D, $14]
Turn the display on $AF
*/
const char databuf[] = {
0x00,
0xae, // display off
0xa8, 0x3f,
0xd3, 0x00,
0x40,
0xa0,
0xc0,
// 0xda, 0x02, // BAD: introduces skipped lines BAD. Use 0x12 to not have skipped lines
0xda, 0x12, // KJS testing line skip Shit this was huge, fixed skip lines
0x81, 0x7f,
0xa4,
0xd5, 0x80,
0x8d, 0x14,
0x20, 0x00, // set adreessing mode: Horizontal
0x2e, // scroll off, kjs testing
0xaf,
};
int datalen = sizeof(databuf);
int len;
// Transmit to Slave
Wire.beginTransmission(TARGET); // Slave address
len = Wire.write(databuf, datalen); // Write string to I2C Tx buffer
Wire.endTransmission(); // Transmit to Slave
// Check if error occured
if( len != datalen )
{
return -1;
} else {
return 0;
}
}
int disp_set_contrast(int x)
{
char buf[] = {
0x00,
0x81,
0x00,
};
int len, rc;
Wire.beginTransmission(TARGET);
buf[2] = (char)x;
len = Wire.write(buf, sizeof(buf));
rc = Wire.endTransmission();
if( len != 1 ) {
return len;
}
return rc;
}
int disp_set_range()
{
char buf[] = {
0x00,
0x21, 0x00, 0x7f, // set column start/end range (0-127)
0x22, 0x00, 0x07, // set page start/end range (0-7)
};
int len, rc;
Wire.beginTransmission(TARGET);
len = Wire.write(buf, sizeof(buf));
rc = Wire.endTransmission();
if( len != sizeof(buf)) {
return len;
}
return rc;
}
int disp_update(const char *frame)
{
const char buf[] = {
0x40,
};
int i, len, err, rc;
const char *p;
err = 0;
p = frame;
for(i=0; i < FRAME_SIZE/128; i++)
{
Wire.beginTransmission(TARGET);
len = Wire.write(buf, sizeof(buf));
if( len != sizeof(buf) )
{
err = 2000 + len;
break;
}
len = Wire.write(p, 128);
if( len != 128 )
{
err = 2000 + len;
break;
}
rc = Wire.endTransmission();
if( rc )
{
err = 3000 + rc;
break;
}
p += 128;
}
return err;
}
void disp_clear()
{
static int init = 0;
static char blank[FRAME_SIZE];
if( init == 0 )
{
memset(blank, CLR_MASK, sizeof(blank));
init = 1;
}
disp_update(blank);
}
void disp_pset(char *frame, int x, int y, int p)
{
const int WIDTH = 128;
// const int HEIGHT = 64;
if( PIXEL_ON(p) )
{
frame[ x + (y>>3) * WIDTH ] |= (1 << (y % 8));
}
else
{
frame[ x + (y>>3) * WIDTH ] &= ~(1 << (y % 8));
}
}
int disp_pget(char *frame, int x, int y)
{
const int WIDTH = 128;
// const int HEIGHT = 64;
int byte, mask;
byte = frame[ x + (y>>3) * WIDTH ];
mask = (1 << (y % 8));
return (byte & mask) ? PIXEL_VALUE_ON : PIXEL_VALUE_OFF;
}
void disp_invert(char *frame)
{
const int WIDTH = 128;
const int HEIGHT = 64;
int x, y, p;
for(x=0; x < WIDTH; x++)
{
for(y=0; y < HEIGHT; y++)
{
p = disp_pget(frame, x, y);
p = (p == 1) ? 0 : 1;
disp_pset(frame, x, y, p);
}
}
}
//////////////////////////////////////////////////////////////////////
// end DISP
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//
// begin DEBUG
//
//////////////////////////////////////////////////////////////////////
void debug_flash(int rc)
{
int i, N;
if( rc == 0 )
N = 3;
else
N = 30;
for(i=0; i < N; i++)
{
digitalWriteFast(13, HIGH);
delay(20);
digitalWriteFast(13, LOW);
delay(20);
}
}
void debug_halt()
{
digitalWriteFast(13, HIGH);
for(;;);
}
//////////////////////////////////////////////////////////////////////
//
// end DEBUG
//
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//
// begin DEVICE
//
//////////////////////////////////////////////////////////////////////
typedef enum {
E_NONE,
E_BUTTONA,
E_BUTTONL,
E_BUTTONB,
E_BUTTONC,
E_BUTTONA_RELEASE,
E_BUTTONL_RELEASE,
E_BUTTONB_RELEASE,
E_BUTTONC_RELEASE,
E_HEX_BUTTON_0, // 0
E_HEX_BUTTON_1, // 1
E_HEX_BUTTON_2, // 2
E_HEX_BUTTON_3, // 3
E_HEX_BUTTON_4, // 4
E_HEX_BUTTON_5, // 5
E_HEX_BUTTON_6, // 6
E_HEX_BUTTON_7, // 7
E_HEX_BUTTON_8, // 8
E_HEX_BUTTON_9, // 9
E_HEX_BUTTON_A, // A
E_HEX_BUTTON_B, // B
E_HEX_BUTTON_C, // C
E_HEX_BUTTON_D, // D
E_HEX_BUTTON_STAR, // *
E_HEX_BUTTON_POUND, // #
E_HEX_BUTTON_0_RELEASE, // 0
E_HEX_BUTTON_1_RELEASE, // 1
E_HEX_BUTTON_2_RELEASE, // 2
E_HEX_BUTTON_3_RELEASE, // 3
E_HEX_BUTTON_4_RELEASE, // 4
E_HEX_BUTTON_5_RELEASE, // 5
E_HEX_BUTTON_6_RELEASE, // 6
E_HEX_BUTTON_7_RELEASE, // 7
E_HEX_BUTTON_8_RELEASE, // 8
E_HEX_BUTTON_9_RELEASE, // 9
E_HEX_BUTTON_A_RELEASE, // A
E_HEX_BUTTON_B_RELEASE, // B
E_HEX_BUTTON_C_RELEASE, // C
E_HEX_BUTTON_D_RELEASE, // D
E_HEX_BUTTON_STAR_RELEASE, // *
E_HEX_BUTTON_POUND_RELEASE, // #
E_SECONDS_TIMER,
E_SECONDS15, // 1/6th second every 100/15th seconds
E_LIGHT_OFF, // triggered when DEVICE.light goes to 0
} EVENT;
static volatile struct
{
IntervalTimer it;
long clock; // 1/100th of seconds counter
long epoch; // seconds since jan 1, 1970
int xxx; // push button state
int hex; // hex button key press
int hex_row; // key keypad scan row
int counter100; // counter 1/100th second
int counter15; // counter 1/6.6th second
int counter15_enable; // enable E_SECOND15 event
int light;
} DEVICE;
void isr_xxx1()
{
DEVICE.xxx = 0x01;
}
void isr_xxx2()
{
DEVICE.xxx = 0x02;
}
void isr_xxx3()
{
DEVICE.xxx = 0x04;
}
void isr_xxx4()
{
DEVICE.xxx = 0x08;
}
//////////////////////////////////////////////////////////////////////
// hex keypad
//
// pins 25
// 26
// 27
// 28
//
// 29
// 30
// 31
// 32
//
// 1 2 3 4
// A 1 2 3 A
// B 4 5 6 B
// C 7 8 9 C
// D * 0 # D
//
#define HK1 25
#define HK2 26
#define HK3 27
#define HK4 28
#define HKA 29
#define HKB 30
#define HKC 31
#define HKD 32
int PMAP(int hex)
{
// i think this is just: "return HK1 + (hex % 10)-1;"
switch(hex)
{
case 1: return HK1; // D
case 2: return HK2; // #
case 3: return HK3; // 0
case 4: return HK4; // *
case 11: return HK1; // C
case 12: return HK2; // 9
case 13: return HK3; // 8
case 14: return HK4; // 7
case 21: return HK1; // B
case 22: return HK2; // 6
case 23: return HK3; // 5
case 24: return HK4; // 4
case 31: return HK1; // A
case 32: return HK2; // 3
case 33: return HK3; // 2
case 34: return HK4; // 1
}
return 0;
}
//
// trigger this on a timer, every 1/100th seconds.
// - Increments all the clock and timer variables.
// - Implements the hex keypad scan logic.
//
void isr_hex_scan()
{
int v, pin;
DEVICE.clock += 1;
DEVICE.counter100 += 1;
if( DEVICE.counter100 > 99 ) {
DEVICE.counter100 = 0;
DEVICE.epoch += 1;
}
DEVICE.counter15 += 1;
if( DEVICE.counter15 > 14 ) {
DEVICE.counter15 = 0;
}
if( DEVICE.light > 0 ) {
DEVICE.light -= 1;
}
if( DEVICE.hex != 0 ) {
v = digitalRead( PMAP(DEVICE.hex) );
if( v != 0 )
{
return;
}
else
{
DEVICE.hex = 0;
}
}
if( DEVICE.xxx != 0 ) {
switch(DEVICE.xxx) {
case 0x01: pin = 12; break;
case 0x02: pin = 11; break;
case 0x04: pin = 10; break;
case 0x08: pin = 9; break;
}
v = digitalRead(pin);
if( v == 0 )
DEVICE.xxx = 0;
}
DEVICE.hex_row += 1;
if( DEVICE.hex_row > 3 )
DEVICE.hex_row = 0;
digitalWriteFast(HKA, DEVICE.hex_row == 0 ? HIGH : LOW);
digitalWriteFast(HKB, DEVICE.hex_row == 1 ? HIGH : LOW);
digitalWriteFast(HKC, DEVICE.hex_row == 2 ? HIGH : LOW);
digitalWriteFast(HKD, DEVICE.hex_row == 3 ? HIGH : LOW);
}
void isr_hk1()
{
DEVICE.hex = DEVICE.hex_row*10 + 1;
}
void isr_hk2()
{
DEVICE.hex = DEVICE.hex_row*10 + 2;
}
void isr_hk3()
{
DEVICE.hex = DEVICE.hex_row*10 + 3;
}
void isr_hk4()
{
DEVICE.hex = DEVICE.hex_row*10 + 4;
}
void device_setup()
{
int rc, rc2;
Serial.begin(9600);
pinMode(13, OUTPUT); // built-in led
pinMode(24, OUTPUT); // buzzer
pinMode(12, INPUT); // push button 1 (upper left)
pinMode(11, INPUT); // push button 2 (upper right)
pinMode(10, INPUT); // push button 3 (lower left)
pinMode(9, INPUT); // push button 4 (lower right)
attachInterrupt(12, isr_xxx1, RISING);
attachInterrupt(11, isr_xxx2, RISING);
attachInterrupt(10, isr_xxx3, RISING);
attachInterrupt(9, isr_xxx4, RISING);
//
// hex keypad
//
pinMode(HKA, OUTPUT);
pinMode(HKB, OUTPUT);
pinMode(HKC, OUTPUT);
pinMode(HKD, OUTPUT);
digitalWriteFast(HKA, LOW);
digitalWriteFast(HKB, LOW);
digitalWriteFast(HKC, LOW);
digitalWriteFast(HKD, LOW);
pinMode(HK1, INPUT_PULLDOWN);
pinMode(HK2, INPUT_PULLDOWN);
pinMode(HK3, INPUT_PULLDOWN);
pinMode(HK4, INPUT_PULLDOWN);
attachInterrupt(HK1, isr_hk1, RISING);
attachInterrupt(HK2, isr_hk2, RISING);
attachInterrupt(HK3, isr_hk3, RISING);
attachInterrupt(HK4, isr_hk4, RISING);
disp_setup();
rc = disp_init();
if( rc )
{
Serial.println("disp_init failed");
debug_flash(rc);
}
rc2 = disp_set_range();
if( rc2 )
{
Serial.println("disp_set_range failed");
debug_flash(rc2);
}
DEVICE.it.begin(isr_hex_scan, 10000); // 1/100th of a second
interrupts();
}
//
// wait for event to occur. Monitor global variables for a change.
//
int device_get_event()
{
static int saved_xxx = 0;
static int saved_epoch = 0;
static int saved_counter15 = 0;
static int saved_hex = 0;
static int saved_light = 0;
int e;
e = E_NONE;
while( e == E_NONE )
{
if( saved_xxx != DEVICE.xxx )
{
if( DEVICE.xxx == 0 ) {
if( saved_xxx & 0x01 ) {
e = E_BUTTONL_RELEASE;
} else if( saved_xxx & 0x02 ) {
e = E_BUTTONC_RELEASE;
} else if( saved_xxx & 0x04 ) {
e = E_BUTTONB_RELEASE;
} else if( saved_xxx & 0x08 ) {
e = E_BUTTONA_RELEASE;
}
} else if( DEVICE.xxx & 0x01 ) {
e = E_BUTTONL;
} else if( DEVICE.xxx & 0x02 ) {
e = E_BUTTONC;
} else if( DEVICE.xxx & 0x04 ) {
e = E_BUTTONB;
} else if( DEVICE.xxx & 0x08 ) {
e = E_BUTTONA;
}
saved_xxx = DEVICE.xxx;
}
else if( saved_hex != DEVICE.hex )
{
switch(DEVICE.hex)
{
case 0:
switch(saved_hex) {
case 1: e = E_HEX_BUTTON_D_RELEASE; break; // D
case 2: e = E_HEX_BUTTON_POUND_RELEASE; break; // #
case 3: e = E_HEX_BUTTON_0_RELEASE; break; // 0
case 4: e = E_HEX_BUTTON_STAR_RELEASE; break; // *
case 11: e = E_HEX_BUTTON_C_RELEASE; break; // C
case 12: e = E_HEX_BUTTON_9_RELEASE; break; // 9
case 13: e = E_HEX_BUTTON_8_RELEASE; break; // 8
case 14: e = E_HEX_BUTTON_7_RELEASE; break; // 7
case 21: e = E_HEX_BUTTON_B_RELEASE; break; // B
case 22: e = E_HEX_BUTTON_6_RELEASE; break; // 6
case 23: e = E_HEX_BUTTON_5_RELEASE; break; // 5
case 24: e = E_HEX_BUTTON_4_RELEASE; break; // 4
case 31: e = E_HEX_BUTTON_A_RELEASE; break; // A
case 32: e = E_HEX_BUTTON_3_RELEASE; break; // 3
case 33: e = E_HEX_BUTTON_2_RELEASE; break; // 2
case 34: e = E_HEX_BUTTON_1_RELEASE; break; // 1
}
break;
case 1: e = E_HEX_BUTTON_D; break; // D
case 2: e = E_HEX_BUTTON_POUND; break; // #
case 3: e = E_HEX_BUTTON_0; break; // 0
case 4: e = E_HEX_BUTTON_STAR; break; // *
case 11: e = E_HEX_BUTTON_C; break; // C
case 12: e = E_HEX_BUTTON_9; break; // 9
case 13: e = E_HEX_BUTTON_8; break; // 8
case 14: e = E_HEX_BUTTON_7; break; // 7
case 21: e = E_HEX_BUTTON_B; break; // B
case 22: e = E_HEX_BUTTON_6; break; // 6
case 23: e = E_HEX_BUTTON_5; break; // 5
case 24: e = E_HEX_BUTTON_4; break; // 4
case 31: e = E_HEX_BUTTON_A; break; // A
case 32: e = E_HEX_BUTTON_3; break; // 3
case 33: e = E_HEX_BUTTON_2; break; // 2
case 34: e = E_HEX_BUTTON_1; break; // 1
}
saved_hex = DEVICE.hex;
}
else if( saved_light != DEVICE.light )
{
if( DEVICE.light == 0 )
{
e = E_LIGHT_OFF;
}
saved_light = DEVICE.light;
}
else if( saved_epoch != DEVICE.epoch )
{
saved_epoch = DEVICE.epoch;
e = E_SECONDS_TIMER;
}
else if( saved_counter15 != DEVICE.counter15 )
{
saved_counter15 = DEVICE.counter15;
if( DEVICE.counter15_enable )
e = E_SECONDS15;
}
}
return e;
}
//////////////////////////////////////////////////////////////////////
// end DEVICE
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//
// begin DRAW
//
//////////////////////////////////////////////////////////////////////
void draw_filled_block(char *frame, int x1, int y1, int x2, int y2)
{
int x, y;
for(x=x1; x < x2; x++) {
for(y=y1; y < y2; y++) {
disp_pset(frame, x, y, 1);
}
}
}
void draw_rect(char *frame, int x1, int y1, int width, int height)
{
int x, y;
int x2, y2;
x2 = x1 + width;
y2 = y1 + height;
for(x=x1; x <= x2; x++) {
disp_pset(frame, x, y1, 1);
disp_pset(frame, x, y2, 1);
}
for(y=y1; y <= y2; y++) {
disp_pset(frame, x1, y, 1);
disp_pset(frame, x2, y, 1);
}
}
//
// (x0, y0)
// 0x01 ======
// || ||
// 0x02 || ## || 0x04
// || ||
// 0x08 ======
// || ||
// 0x10 || ## || 0x20
// || ||
// 0x40 ====== ## 0x80
//
// Top dot: 0x100
// bottom dot: 0x200
// decimal point: 0x80
//
void draw_segments(char *frame, int x, int y, int width, int height, int thick, int segments)
{
int x1, x2, y1, y2;
if( segments & 0x01 ) {
x1 = x + thick;
x2 = x + thick + width;
y1 = y;
y2 = y + thick;
draw_filled_block(frame, x1, y1, x2, y2);
}
if( segments & 0x02 ) {
x1 = x;
x2 = x + thick;
y1 = y + thick;
y2 = y + thick + height;
draw_filled_block(frame, x1, y1, x2, y2);
}
if( segments & 0x04 ) {
x1 = x + thick + width;
x2 = x + thick + width + thick;
y1 = y + thick;
y2 = y + thick + height;
draw_filled_block(frame, x1, y1, x2, y2);
}
if( segments & 0x08 ) {
x1 = x + thick;
x2 = x + thick + width;
y1 = y + thick + height;
y2 = y + thick + height + thick;
draw_filled_block(frame, x1, y1, x2, y2);
}
if( segments & 0x10 ) {
x1 = x;
x2 = x + thick;
y1 = y + thick + height + thick;
y2 = y + thick + height + thick + height;
draw_filled_block(frame, x1, y1, x2, y2);
}
if( segments & 0x20 ) {
x1 = x + thick + width;
x2 = x + thick + width + thick;
y1 = y + thick + height + thick;
y2 = y + thick + height + thick + height;
draw_filled_block(frame, x1, y1, x2, y2);
}
if( segments & 0x40 ) {
x1 = x + thick;
x2 = x + thick + width;
y1 = y + thick + height + thick + height;
y2 = y + thick + height + thick + height + thick;
draw_filled_block(frame, x1, y1, x2, y2);
}
if( segments & 0x80 ) {
// decimal
const int SPC = 2; // 1 looked okay
x1 = x + thick + width + thick + SPC;
x2 = x + thick + width + thick + SPC + thick;
y1 = y + thick + height + thick + height;
y2 = y + thick + height + thick + height + thick;
draw_filled_block(frame, x1, y1, x2, y2);
}
if( segments & 0x100 ) {
// top dot
x1 = x + thick + width/2 - thick/2;
x2 = x + thick + width/2 - thick/2 + thick;
y1 = y + thick + height/2 - thick/2;
y2 = y + thick + height/2 - thick/2 + thick;
draw_filled_block(frame, x1, y1, x2, y2);
}
if( segments & 0x200 ) {
// bottom dot
x1 = x + thick + width/2 - thick/2;
x2 = x + thick + width/2 - thick/2 + thick;
y1 = y + thick + height + thick + height/2 - thick/2;
y2 = y + thick + height + thick + height/2 - thick/2 + thick;
draw_filled_block(frame, x1, y1, x2, y2);
}
}
void draw_digit(char *frame, int x, int y, int width, int height, int thick, char digit, int dp)
{
int segments;
switch(digit)
{
case '0':
segments = 0x01 | 0x02 | 0x04 | 0x10 | 0x20 | 0x40;
break;
case '1':
segments = 0x04 | 0x20;
break;
case '2':
segments = 0x01 | 0x04 | 0x08 | 0x10 | 0x40;
break;
case '3':
segments = 0x01 | 0x04 | 0x08 | 0x020 | 0x40;
break;
case '4':
segments = 0x02 | 0x08 | 0x04 | 0x020;
break;
case '5':
segments = 0x01 | 0x02 | 0x08 | 0x20 | 0x40;
break;
case '6':
segments = 0x01 | 0x02 | 0x08 | 0x10 | 0x20 | 0x40;
break;
case '7':
segments = 0x01 | 0x02 | 0x04 | 0x20;
break;
case '8':
segments = 0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40;
break;
case '9':
case 'g':
segments = 0x01 | 0x02 | 0x04 | 0x08 | 0x20 | 0x40;
break;
case 'A':
case 'a':
segments = 0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20;
break;
case 'B':
case 'b':
segments = 0x02 | 0x10 | 0x40 | 0x20 | 0x08;
break;
case 'C':
case 'c':
segments = 0x01 | 0x02 | 0x10 | 0x40;
break;
case 'D':
case 'd':
segments = 0x04 | 0x20 | 0x08 | 0x10 | 0x40;
break;
case 'E':
case 'e':
segments = 0x01 | 0x02 | 0x08 | 0x10 | 0x40;
break;
case 'F':
case 'f':
segments = 0x01 | 0x02 | 0x08 | 0x10;
break;
case 'H':
segments = 0x02 | 0x04 | 0x08 | 0x10 | 0x20;
break;
case 'h':
segments = 0x02 | 0x08 | 0x10 | 0x20;
break;
case 'i':
segments = 0x20;
break;
case 'J':
segments = 0x04 | 0x20 | 0x40 | 0x10;
break;
case 'L':
segments = 0x02 | 0x10 | 0x40;
break;
case 'n':
segments = 0x08 | 0x10 | 0x20;
break;
case 'o':
segments = 0x08 | 0x10 | 0x20 | 0x40;
break;
case 'P':
segments = 0x01 | 0x02 | 0x04 | 0x08 | 0x10;
break;
case 'U':
segments = 0x02 | 0x04 | 0x10 | 0x20 | 0x40;
break;
case '-':
segments = 0x08;
break;
case '_':
segments = 0x04;
break;
case ':':
segments = 0x200 | 0x100;
break;
case '.':
segments = 0x80;
break;
case ' ':
segments = 0x00; /* BLANK */
break;
default:
segments = 0x00;
break;
}
if(dp) {
segments |= 0x80;
}
draw_segments(frame, x, y, width, height, thick, segments);
}
void draw_segstr(char *frame, int x0, int y0, int width, int height, int thick, const char *str)
{
int x;
const char *p;
const int SPACING = 3;
x = x0;
for(p=str; *p; p++)
{
if( *p == '.' )
continue;
draw_digit(frame, x, y0, width, height, thick, *p, p[1]=='.');
x = x + width + thick*2 + SPACING;
}
}
void draw_number(char *frame, int x0, int y0, int width, int height, int thick, int num)
{
char buf[20];
snprintf(buf, sizeof(buf), "%3d", num);
draw_segstr(frame, x0, y0, width, height, thick, buf);
}
//////////////////////////////////////////////////////////////////////
//
// 7x5 font
//
//////////////////////////////////////////////////////////////////////
const int CHAR_WIDTH = 5;
const int CHAR_HEIGHT = 7;
const int CHAR_SPACING = 1;
// render a 5x7 font character into a frame buffer
//