-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCD.c
1160 lines (1048 loc) · 34.6 KB
/
LCD.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
#include "LCD.h"
#include "inc/tm4c123gh6pm.h"
#include "delay.h"
#include "tiva-gc-inc.h"
#define DATAMODE_ACTIVESTATE HIGH
#define RESET_ACTIVESTATE LOW
#define LCD_PIXEL_FORMAT_444 3 /* 12-bit/pixel */
#define LCD_PIXEL_FORMAT_565 5 /* 16-bit/pixel */
#define LCD_PIXEL_FORMAT_666 6 /* 18-bit/pixel */
#define LCD_GAMMA_PREDEFINED_1 (1<<0) /* Gamma Curve 1 */
#define LCD_GAMMA_PREDEFINED_2 (1<<1) /* Gamma Curve 2 */
#define LCD_GAMMA_PREDEFINED_3 (1<<2) /* Gamma Curve 3 */
#define LCD_GAMMA_PREDEFINED_4 (1<<3) /* Gamma Curve 4 */
#define LCD_MADCTL_MY (1<<7) /* Row Address Order */
#define LCD_MADCTL_MX (1<<6) /* Column Address Order */
#define LCD_MADCTL_MV (1<<5) /* Row / Column Exchange */
#define LCD_MADCTL_ML (1<<4) /* Vertical Refresh Order */
#define LCD_MADCTL_BGR (1<<3) /* RGB or BGR Order */
#define LCD_MADCTL_MH (1<<2) /* Horizontal Refresh Order */
#define LCD_MADCTL_DEFAULT 0 /* Default state */
/* MV flag default state based on the Reset Table, see: (pdf v1.4 p89-91) */
#define FLAG_MADCTL_MV_DEFAULT 0
/* default pixel format; Based on the Reset Table, see: (pdf v1.4 p89-91) */
#define INTERFACE_PIXEL_FORMAT_DEFAULT LCD_PIXEL_FORMAT_666
/* ST7735S driver commands (pdf v1.4 p5) */
#define LCD_SWRESET 0x01
#define LCD_SLPIN 0x10
#define LCD_SLPOUT 0x11
#define LCD_INVOFF 0x20
#define LCD_INVON 0x21
#define LCD_GAMSET 0x26
#define LCD_DISPOFF 0x28
#define LCD_DISPON 0x29
#define LCD_CASET 0x2A
#define LCD_RASET 0x2B
#define LCD_RAMWR 0x2C
#define LCD_TEOFF 0x34
#define LCD_TEON 0x35
#define LCD_MADCTL 0x36
#define LCD_IDMOFF 0x38
#define LCD_IDMON 0x39
#define LCD_COLMOD 0x3A
#define LCD_RGBSET 0x2D
#define LCD_FRMCTR1 0xB1
#define LCD_PWCTR1 0xC0
#define LCD_PWCTR2 0xC1
#define LCD_PWCTR3 0xC2
#define LCD_PWCTR4 0xC3
#define LCD_PWCTR5 0xC4
/* Active settings */
static LCD_Settings _active_settings = {0};
// standard ascii 5x7 font
// originally from glcdfont.c from Adafruit project
static const uint8_t Font[] = {
0x00, 0x00, 0x00, 0x00, 0x00,
0x3E, 0x5B, 0x4F, 0x5B, 0x3E,
0x3E, 0x6B, 0x4F, 0x6B, 0x3E,
0x1C, 0x3E, 0x7C, 0x3E, 0x1C,
0x18, 0x3C, 0x7E, 0x3C, 0x18,
0x1C, 0x57, 0x7D, 0x57, 0x1C,
0x1C, 0x5E, 0x7F, 0x5E, 0x1C,
0x00, 0x18, 0x3C, 0x18, 0x00,
0xFF, 0xE7, 0xC3, 0xE7, 0xFF,
0x00, 0x18, 0x24, 0x18, 0x00,
0xFF, 0xE7, 0xDB, 0xE7, 0xFF,
0x30, 0x48, 0x3A, 0x06, 0x0E,
0x26, 0x29, 0x79, 0x29, 0x26,
0x40, 0x7F, 0x05, 0x05, 0x07,
0x40, 0x7F, 0x05, 0x25, 0x3F,
0x5A, 0x3C, 0xE7, 0x3C, 0x5A,
0x7F, 0x3E, 0x1C, 0x1C, 0x08,
0x08, 0x1C, 0x1C, 0x3E, 0x7F,
0x14, 0x22, 0x7F, 0x22, 0x14,
0x5F, 0x5F, 0x00, 0x5F, 0x5F,
0x06, 0x09, 0x7F, 0x01, 0x7F,
0x00, 0x66, 0x89, 0x95, 0x6A,
0x60, 0x60, 0x60, 0x60, 0x60,
0x94, 0xA2, 0xFF, 0xA2, 0x94,
0x08, 0x04, 0x7E, 0x04, 0x08,
0x10, 0x20, 0x7E, 0x20, 0x10,
0x08, 0x08, 0x2A, 0x1C, 0x08,
0x08, 0x1C, 0x2A, 0x08, 0x08,
0x1E, 0x10, 0x10, 0x10, 0x10,
0x0C, 0x1E, 0x0C, 0x1E, 0x0C,
0x30, 0x38, 0x3E, 0x38, 0x30,
0x06, 0x0E, 0x3E, 0x0E, 0x06,
0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x5F, 0x00, 0x00,
0x00, 0x07, 0x00, 0x07, 0x00,
0x14, 0x7F, 0x14, 0x7F, 0x14,
0x24, 0x2A, 0x7F, 0x2A, 0x12,
0x23, 0x13, 0x08, 0x64, 0x62,
0x36, 0x49, 0x56, 0x20, 0x50,
0x00, 0x08, 0x07, 0x03, 0x00,
0x00, 0x1C, 0x22, 0x41, 0x00,
0x00, 0x41, 0x22, 0x1C, 0x00,
0x2A, 0x1C, 0x7F, 0x1C, 0x2A,
0x08, 0x08, 0x3E, 0x08, 0x08,
0x00, 0x80, 0x70, 0x30, 0x00,
0x08, 0x08, 0x08, 0x08, 0x08,
0x00, 0x00, 0x60, 0x60, 0x00,
0x20, 0x10, 0x08, 0x04, 0x02,
0x3E, 0x51, 0x49, 0x45, 0x3E, // 0
0x00, 0x42, 0x7F, 0x40, 0x00, // 1
0x72, 0x49, 0x49, 0x49, 0x46, // 2
0x21, 0x41, 0x49, 0x4D, 0x33, // 3
0x18, 0x14, 0x12, 0x7F, 0x10, // 4
0x27, 0x45, 0x45, 0x45, 0x39, // 5
0x3C, 0x4A, 0x49, 0x49, 0x31, // 6
0x41, 0x21, 0x11, 0x09, 0x07, // 7
0x36, 0x49, 0x49, 0x49, 0x36, // 8
0x46, 0x49, 0x49, 0x29, 0x1E, // 9
0x00, 0x00, 0x14, 0x00, 0x00,
0x00, 0x40, 0x34, 0x00, 0x00,
0x00, 0x08, 0x14, 0x22, 0x41,
0x14, 0x14, 0x14, 0x14, 0x14,
0x00, 0x41, 0x22, 0x14, 0x08,
0x02, 0x01, 0x59, 0x09, 0x06,
0x3E, 0x41, 0x5D, 0x59, 0x4E,
0x7C, 0x12, 0x11, 0x12, 0x7C, // A
0x7F, 0x49, 0x49, 0x49, 0x36, // B
0x3E, 0x41, 0x41, 0x41, 0x22, // C
0x7F, 0x41, 0x41, 0x41, 0x3E, // D
0x7F, 0x49, 0x49, 0x49, 0x41, // E
0x7F, 0x09, 0x09, 0x09, 0x01, // F
0x3E, 0x41, 0x41, 0x51, 0x73, // G
0x7F, 0x08, 0x08, 0x08, 0x7F, // H
0x00, 0x41, 0x7F, 0x41, 0x00, // I
0x20, 0x40, 0x41, 0x3F, 0x01, // J
0x7F, 0x08, 0x14, 0x22, 0x41, // K
0x7F, 0x40, 0x40, 0x40, 0x40, // L
0x7F, 0x02, 0x1C, 0x02, 0x7F, // M
0x7F, 0x04, 0x08, 0x10, 0x7F, // N
0x3E, 0x41, 0x41, 0x41, 0x3E, // O
0x7F, 0x09, 0x09, 0x09, 0x06, // P
0x3E, 0x41, 0x51, 0x21, 0x5E, // Q
0x7F, 0x09, 0x19, 0x29, 0x46, // R
0x26, 0x49, 0x49, 0x49, 0x32, // S
0x03, 0x01, 0x7F, 0x01, 0x03, // T
0x3F, 0x40, 0x40, 0x40, 0x3F, // U
0x1F, 0x20, 0x40, 0x20, 0x1F, // V
0x3F, 0x40, 0x38, 0x40, 0x3F, // W
0x63, 0x14, 0x08, 0x14, 0x63, // X
0x03, 0x04, 0x78, 0x04, 0x03, // Y
0x61, 0x59, 0x49, 0x4D, 0x43, // Z
0x00, 0x7F, 0x41, 0x41, 0x41,
0x02, 0x04, 0x08, 0x10, 0x20,
0x00, 0x41, 0x41, 0x41, 0x7F,
0x04, 0x02, 0x01, 0x02, 0x04,
0x40, 0x40, 0x40, 0x40, 0x40,
0x00, 0x03, 0x07, 0x08, 0x00,
0x20, 0x54, 0x54, 0x78, 0x40, // a
0x7F, 0x28, 0x44, 0x44, 0x38, // b
0x38, 0x44, 0x44, 0x44, 0x28, // c
0x38, 0x44, 0x44, 0x28, 0x7F, // d
0x38, 0x54, 0x54, 0x54, 0x18, // e
0x00, 0x08, 0x7E, 0x09, 0x02, // f
0x18, 0xA4, 0xA4, 0x9C, 0x78, // g
0x7F, 0x08, 0x04, 0x04, 0x78, // h
0x00, 0x44, 0x7D, 0x40, 0x00, // i
0x20, 0x40, 0x40, 0x3D, 0x00, // j
0x7F, 0x10, 0x28, 0x44, 0x00, // k
0x00, 0x41, 0x7F, 0x40, 0x00, // l
0x7C, 0x04, 0x78, 0x04, 0x78, // m
0x7C, 0x08, 0x04, 0x04, 0x78, // n
0x38, 0x44, 0x44, 0x44, 0x38, // o
0xFC, 0x18, 0x24, 0x24, 0x18, // p
0x18, 0x24, 0x24, 0x18, 0xFC, // q
0x7C, 0x08, 0x04, 0x04, 0x08, // r
0x48, 0x54, 0x54, 0x54, 0x24, // s
0x04, 0x04, 0x3F, 0x44, 0x24, // t
0x3C, 0x40, 0x40, 0x20, 0x7C, // u
0x1C, 0x20, 0x40, 0x20, 0x1C, // v
0x3C, 0x40, 0x30, 0x40, 0x3C, // w
0x44, 0x28, 0x10, 0x28, 0x44, // x
0x4C, 0x90, 0x90, 0x90, 0x7C, // y
0x44, 0x64, 0x54, 0x4C, 0x44, // z
0x00, 0x08, 0x36, 0x41, 0x00,
0x00, 0x00, 0x77, 0x00, 0x00,
0x00, 0x41, 0x36, 0x08, 0x00,
0x02, 0x01, 0x02, 0x04, 0x02,
0x3C, 0x26, 0x23, 0x26, 0x3C,
0x1E, 0xA1, 0xA1, 0x61, 0x12,
0x3A, 0x40, 0x40, 0x20, 0x7A,
0x38, 0x54, 0x54, 0x55, 0x59,
0x21, 0x55, 0x55, 0x79, 0x41,
0x21, 0x54, 0x54, 0x78, 0x41,
0x21, 0x55, 0x54, 0x78, 0x40,
0x20, 0x54, 0x55, 0x79, 0x40,
0x0C, 0x1E, 0x52, 0x72, 0x12,
0x39, 0x55, 0x55, 0x55, 0x59,
0x39, 0x54, 0x54, 0x54, 0x59,
0x39, 0x55, 0x54, 0x54, 0x58,
0x00, 0x00, 0x45, 0x7C, 0x41,
0x00, 0x02, 0x45, 0x7D, 0x42,
0x00, 0x01, 0x45, 0x7C, 0x40,
0xF0, 0x29, 0x24, 0x29, 0xF0,
0xF0, 0x28, 0x25, 0x28, 0xF0,
0x7C, 0x54, 0x55, 0x45, 0x00,
0x20, 0x54, 0x54, 0x7C, 0x54,
0x7C, 0x0A, 0x09, 0x7F, 0x49,
0x32, 0x49, 0x49, 0x49, 0x32,
0x32, 0x48, 0x48, 0x48, 0x32,
0x32, 0x4A, 0x48, 0x48, 0x30,
0x3A, 0x41, 0x41, 0x21, 0x7A,
0x3A, 0x42, 0x40, 0x20, 0x78,
0x00, 0x9D, 0xA0, 0xA0, 0x7D,
0x39, 0x44, 0x44, 0x44, 0x39,
0x3D, 0x40, 0x40, 0x40, 0x3D,
0x3C, 0x24, 0xFF, 0x24, 0x24,
0x48, 0x7E, 0x49, 0x43, 0x66,
0x2B, 0x2F, 0xFC, 0x2F, 0x2B,
0xFF, 0x09, 0x29, 0xF6, 0x20,
0xC0, 0x88, 0x7E, 0x09, 0x03,
0x20, 0x54, 0x54, 0x79, 0x41,
0x00, 0x00, 0x44, 0x7D, 0x41,
0x30, 0x48, 0x48, 0x4A, 0x32,
0x38, 0x40, 0x40, 0x22, 0x7A,
0x00, 0x7A, 0x0A, 0x0A, 0x72,
0x7D, 0x0D, 0x19, 0x31, 0x7D,
0x26, 0x29, 0x29, 0x2F, 0x28,
0x26, 0x29, 0x29, 0x29, 0x26,
0x30, 0x48, 0x4D, 0x40, 0x20,
0x38, 0x08, 0x08, 0x08, 0x08,
0x08, 0x08, 0x08, 0x08, 0x38,
0x2F, 0x10, 0xC8, 0xAC, 0xBA,
0x2F, 0x10, 0x28, 0x34, 0xFA,
0x00, 0x00, 0x7B, 0x00, 0x00,
0x08, 0x14, 0x2A, 0x14, 0x22,
0x22, 0x14, 0x2A, 0x14, 0x08,
0xAA, 0x00, 0x55, 0x00, 0xAA,
0xAA, 0x55, 0xAA, 0x55, 0xAA,
0x00, 0x00, 0x00, 0xFF, 0x00,
0x10, 0x10, 0x10, 0xFF, 0x00,
0x14, 0x14, 0x14, 0xFF, 0x00,
0x10, 0x10, 0xFF, 0x00, 0xFF,
0x10, 0x10, 0xF0, 0x10, 0xF0,
0x14, 0x14, 0x14, 0xFC, 0x00,
0x14, 0x14, 0xF7, 0x00, 0xFF,
0x00, 0x00, 0xFF, 0x00, 0xFF,
0x14, 0x14, 0xF4, 0x04, 0xFC,
0x14, 0x14, 0x17, 0x10, 0x1F,
0x10, 0x10, 0x1F, 0x10, 0x1F,
0x14, 0x14, 0x14, 0x1F, 0x00,
0x10, 0x10, 0x10, 0xF0, 0x00,
0x00, 0x00, 0x00, 0x1F, 0x10,
0x10, 0x10, 0x10, 0x1F, 0x10,
0x10, 0x10, 0x10, 0xF0, 0x10,
0x00, 0x00, 0x00, 0xFF, 0x10,
0x10, 0x10, 0x10, 0x10, 0x10,
0x10, 0x10, 0x10, 0xFF, 0x10,
0x00, 0x00, 0x00, 0xFF, 0x14,
0x00, 0x00, 0xFF, 0x00, 0xFF,
0x00, 0x00, 0x1F, 0x10, 0x17,
0x00, 0x00, 0xFC, 0x04, 0xF4,
0x14, 0x14, 0x17, 0x10, 0x17,
0x14, 0x14, 0xF4, 0x04, 0xF4,
0x00, 0x00, 0xFF, 0x00, 0xF7,
0x14, 0x14, 0x14, 0x14, 0x14,
0x14, 0x14, 0xF7, 0x00, 0xF7,
0x14, 0x14, 0x14, 0x17, 0x14,
0x10, 0x10, 0x1F, 0x10, 0x1F,
0x14, 0x14, 0x14, 0xF4, 0x14,
0x10, 0x10, 0xF0, 0x10, 0xF0,
0x00, 0x00, 0x1F, 0x10, 0x1F,
0x00, 0x00, 0x00, 0x1F, 0x14,
0x00, 0x00, 0x00, 0xFC, 0x14,
0x00, 0x00, 0xF0, 0x10, 0xF0,
0x10, 0x10, 0xFF, 0x10, 0xFF,
0x14, 0x14, 0x14, 0xFF, 0x14,
0x10, 0x10, 0x10, 0x1F, 0x00,
0x00, 0x00, 0x00, 0xF0, 0x10,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0xFF, 0xFF, 0xFF, 0x00, 0x00,
0x00, 0x00, 0x00, 0xFF, 0xFF,
0x0F, 0x0F, 0x0F, 0x0F, 0x0F,
0x38, 0x44, 0x44, 0x38, 0x44,
0x7C, 0x2A, 0x2A, 0x3E, 0x14,
0x7E, 0x02, 0x02, 0x06, 0x06,
0x02, 0x7E, 0x02, 0x7E, 0x02,
0x63, 0x55, 0x49, 0x41, 0x63,
0x38, 0x44, 0x44, 0x3C, 0x04,
0x40, 0x7E, 0x20, 0x1E, 0x20,
0x06, 0x02, 0x7E, 0x02, 0x02,
0x99, 0xA5, 0xE7, 0xA5, 0x99,
0x1C, 0x2A, 0x49, 0x2A, 0x1C,
0x4C, 0x72, 0x01, 0x72, 0x4C,
0x30, 0x4A, 0x4D, 0x4D, 0x30,
0x30, 0x48, 0x78, 0x48, 0x30,
0xBC, 0x62, 0x5A, 0x46, 0x3D,
0x3E, 0x49, 0x49, 0x49, 0x00,
0x7E, 0x01, 0x01, 0x01, 0x7E,
0x2A, 0x2A, 0x2A, 0x2A, 0x2A,
0x44, 0x44, 0x5F, 0x44, 0x44,
0x40, 0x51, 0x4A, 0x44, 0x40,
0x40, 0x44, 0x4A, 0x51, 0x40,
0x00, 0x00, 0xFF, 0x01, 0x03,
0xE0, 0x80, 0xFF, 0x00, 0x00,
0x08, 0x08, 0x6B, 0x6B, 0x08,
0x36, 0x12, 0x36, 0x24, 0x36,
0x06, 0x0F, 0x09, 0x0F, 0x06,
0x00, 0x00, 0x18, 0x18, 0x00,
0x00, 0x00, 0x10, 0x10, 0x00,
0x30, 0x40, 0xFF, 0x01, 0x01,
0x00, 0x1F, 0x01, 0x01, 0x1E,
0x00, 0x19, 0x1D, 0x17, 0x12,
0x00, 0x3C, 0x3C, 0x3C, 0x3C,
0x00, 0x00, 0x00, 0x00, 0x00,
};
// Because compiler complained
void InitSPI(void);
void WriteSPI(uint8_t data);
void LCD_Command(uint8_t command);
void LCD_Data(uint8_t data);
void LCD_DataBuffer(uint8_t *buffer, uint32_t count);
void LCD_gCharT(int16_t x, int16_t y, char c, pixel textColor, uint8_t size);
// Initializes SSI as SPI to EDUMKII display
void InitSPI(void)
{
SYSCTL_RCGCSSI_R |= (1 << 2); // Enable SSI2
SYSCTL_RCGCGPIO_R |= 0x03; // Enable GPIOA & B
while (!(SYSCTL_PRGPIO_R & 0x03)); // Wait for enabled signal
GPIO_PORTB_AFSEL_R |= (1 << 4) | (1 << 7); // AF for PB4, PB7 which are Clk and MOSI
GPIO_PORTB_PCTL_R |= (2 << 16) | (2 << 28); // Select right AF pins
GPIO_PORTB_DEN_R |= (1 << 4) | (1 << 7); // Digital enable
GPIO_PORTA_CR_R |= (1 << 4); // Enable pin 4
GPIO_PORTA_AFSEL_R &= ~(1 << 4); // Disable AF for PA4 which is CS
GPIO_PORTA_DIR_R |= (1 << 4); // PA4 is output
GPIO_PORTA_DEN_R |= (1 << 4); // Digital enable
GPIO_PORTA_DATA_R |= (1 << 4); // CS high disables transmission
SSI2_CR1_R &= ~(1 << 1); // Ensure SSE bit is 0 before making changes
SSI2_CR1_R = 0x0; // Set SSI as master
SSI2_CC_R = 0x0; // Set SSI clock source
SSI2_CPSR_R = 2; // Clock prescale divisor
SSI2_CR0_R = (0 << 8) | 0x07; // Set serial clock rate, clock phase/polarity, protocol mode, data size (DSS)
SSI2_CR1_R |= (1 << 1); // Enable SSI by setting SSE bit
SYSCTL_RCGCGPIO_R |= (1 << 5); // Enable GPIOF
while (!(SYSCTL_PRGPIO_R & (1 << 5))); // Wait for enabled signal
GPIO_PORTF_LOCK_R = 0x4C4F434B; //Unlock Port F
GPIO_PORTF_CR_R |= 1 | (1 << 4); // PF0 is Reset negative, low is reset, PF4 is Register select, or D/C pin
GPIO_PORTF_AMSEL_R = 0x00; //Disable analog functions
GPIO_PORTF_AFSEL_R = 0x00; //Clear alt functions
GPIO_PORTF_PUR_R &= ~(1 | (1 << 4)); // No pull up, in case buttons were set up
GPIO_PORTF_DIR_R |= 1 | (1 << 4); // Output
GPIO_PORTF_DEN_R |= 1 | (1 << 4); // Digital enable
}
// LCD initialization
// Color mode: 18-bit/6-6-6 RGB
void LCD_Init(void)
{
InitSPI();
GPIO_PORTF_DATA_R |= HIGH; // Pull reset down, is negative logic
delay(100);
LCD_CS(LOW);
LCD_Command(LCD_SWRESET);
delay(150); // 120 ms or more wait after SW reset
LCD_Command(LCD_SLPOUT); // Turn off sleep mode
delay(150); // 120 ms or more wait after sleep out
LCD_Command(LCD_FRMCTR1); // Framerate control
LCD_Data(0x01);
LCD_Data(0x06);
LCD_Data(0x03);
LCD_Command(LCD_PWCTR1);
LCD_Data(0xA2);
LCD_Data(0x02);
LCD_Data(0x84);
LCD_Command(LCD_PWCTR2);
LCD_Data(0xC5);
LCD_Command(LCD_PWCTR3);
LCD_Data(0x0A);
LCD_Data(0x00);
LCD_Command(LCD_PWCTR4);
LCD_Data(0x8A);
LCD_Data(0x2A);
LCD_Command(LCD_PWCTR5);
LCD_Data(0xEE);
LCD_Data(0x8A);
_active_settings.MemoryAccessCTL = LCD_MADCTL_MX | LCD_MADCTL_MY | LCD_MADCTL_BGR;
LCD_Command(LCD_MADCTL); // Set memory access control
LCD_Data(_active_settings.MemoryAccessCTL);
_active_settings.ColorMode = LCD_PIXEL_FORMAT_666;
LCD_Command(LCD_COLMOD); // Set interface pixel format
LCD_Data(_active_settings.ColorMode);
delay(10);
_active_settings.InversionMode = LCD_INVOFF; // Screen inversion off
LCD_Command(_active_settings.InversionMode);
LCD_Command(LCD_GAMMA_PREDEFINED_4); // Gamma curve 4
LCD_Command(LCD_TEOFF);
LCD_Command(LCD_DISPON); // Turn LCD on
delay(150);
_active_settings.BGColor = LCD_BLACK;
LCD_CS(HIGH);
}
// Get LCD settings
// Return:
// struct LCD_Settings: currently active settings
LCD_Settings LCD_GetSettings()
{
return _active_settings;
}
// Set LCD Background color
// Used by some drawing primitives
// Param:
// pixel: new background color
void LCD_SetBGColor(pixel bgColor)
{
_active_settings.BGColor = bgColor;
}
// Turn display inversion on or off
// Param:
// flag: 1 or 0, ON or OFF
void LCD_SetInversion(uint8_t flag)
{
_active_settings.InversionMode = flag;
LCD_Command(flag ? LCD_INVON : LCD_INVOFF);
}
// Write a byte of data to the SPI buffer and wait for it to be sent
void WriteSPI(uint8_t data)
{
SSI2_DR_R = data;
while (!(SSI2_SR_R & 0x1));
}
// LCD send command byte
// Sets Register select to Command mode and sends a byte
// Param:
// command: opcode
void LCD_Command(uint8_t command)
{
GPIO_PORTF_DATA_R &= ~(1 << 4); // Command mode
WriteSPI(command);
}
// LCD send data byte
// Sets Register select to Data mode and sends a byte
// Param:
// data: data byte
void LCD_Data(uint8_t data)
{
GPIO_PORTF_DATA_R |= (1 << 4); // Data mode
WriteSPI(data);
}
// LCD send data bytes
// Sets Register select to Data mode and sends multiple bytes
// Param:
// buffer: data byte array
// count: buffer element count
void LCD_DataBuffer(uint8_t *buffer, uint32_t count)
{
GPIO_PORTF_DATA_R |= (1 << 4); // Data mode
for (uint32_t i = 0; i < count; i++)
{
WriteSPI(buffer[i]);
}
}
// LCD SPI Chip Select
// Tell LCD whether to process transmitted data
// Param:
// flag: HIGH = Deselect, LOW = Select
void LCD_CS(uint8_t flag)
{
if (!flag)
GPIO_PORTA_DATA_R &= ~(1 << 4);
else
{
for (int i = 0; i < 15; i++); // Small delay to end transmission with enough time to spare
GPIO_PORTA_DATA_R |= (1 << 4);
}
}
// LCD set window position / area
// Define area to draw inside of. Out of range vaues ignored
// Param:
// colStart: starting column
// rowStart: starting row
// colEnd: ending column < LCD_WIDTH
// rowEnd: ending row < LCD_HEIGHT
void LCD_SetArea(int16_t colStart, int16_t rowStart, int16_t colEnd, int16_t rowEnd)
{
uint8_t buffer[4];
// Swap values if invalid
if (colEnd < colStart)
{
buffer[0] = colEnd;
colEnd = colStart;
colStart = buffer[0];
}
if (rowEnd < rowStart)
{
buffer[0] = rowEnd;
rowEnd = rowStart;
rowStart = buffer[0];
}
// Check for range
if (colStart < 0)
colStart = 0;
if (rowStart < 0)
rowStart = 0;
if (colEnd >= LCD_WIDTH)
colEnd = LCD_WIDTH - 1;
if (rowEnd >= LCD_HEIGHT)
rowEnd = LCD_HEIGHT - 1;
colStart += 2;
colEnd += 2;
rowStart += 3;
rowEnd += 3;
/* write column address; requires 4 bytes of the buffer */
buffer[0] = (colStart >> 8) & 0x00FF; /* MSB */ /* =0 for ST7735S */
buffer[1] = colStart & 0x00FF; /* LSB */
buffer[2] = (colEnd >> 8) & 0x00FF; /* MSB */ /* =0 for ST7735S */
buffer[3] = colEnd & 0x00FF; /* LSB */
LCD_Command(LCD_CASET);
LCD_DataBuffer(buffer, 4);
/* write row address; requires 4 bytes of the buffer */
buffer[0] = (rowStart >> 8) & 0x00FF; /* MSB */ /* =0 for ST7735S */
buffer[1] = rowStart & 0x00FF; /* LSB */
buffer[2] = (rowEnd >> 8) & 0x00FF; /* MSB */ /* =0 for ST7735S */
buffer[3] = rowEnd & 0x00FF; /* LSB */
LCD_Command(LCD_RASET);
LCD_DataBuffer(buffer, 4);
}
// LCD activate memory write
// Sends RAM write command, after which any number of pixels can be sent
void LCD_ActivateWrite(void)
{
LCD_Command(LCD_RAMWR);
}
// LCD Draw pixel
// Sets area of 1 pixel and sends pixel data.
// Param:
// x, y: screenspace coordinates
// red, green, blue: color value. Bits [5:0] (6 bits) are sent
void LCD_gDrawPixelE(uint8_t x, uint8_t y, uint8_t red, uint8_t green, uint8_t blue)
{
LCD_SetArea(x, y, x, y);
LCD_ActivateWrite();
LCD_PushPixel(red, green, blue);
}
// LCD Draw pixel simplified
// Sets area of 1 pixel and sends pixel data.
// Param:
// x, y: screenspace coordinates
// color: pixel containing color information
void LCD_gDrawPixelS(uint8_t x, uint8_t y, pixel color)
{
LCD_gDrawPixelE(x, y, color.r, color.g, color.b);
}
// LCD write pixel data
// Sends pixel data to current active window. Requires LCD_ActivateWrite
// to have been the last command
// Param:
// red, green, blue: color value. Bits [5:0] (6 bits) are sent
void LCD_PushPixel(uint8_t red, uint8_t green, uint8_t blue)
{
LCD_Data(red << 2);
LCD_Data(green << 2);
LCD_Data(blue << 2);
}
// Convert a 3 byte pixel (Eg #FF004A) uint32_t into pixel
// Precision loss: 8-bit -> 6-bit
// Param:
// p: 32-bit integer. Bits [23:0] are used
pixel LCD_Ui32ToPixel(uint32_t p)
{
pixel pixel;
pixel.r = ((p >> 16) & 0xFF) >> 2;
pixel.g = ((p >> 8) & 0xFF) >> 2;
pixel.b = (p & 0xFF) >> 2;
return pixel;
}
// Clear screen to background color
void LCD_gClear()
{
LCD_gFillRect(0, 0, LCD_WIDTH, LCD_HEIGHT, _active_settings.BGColor);
}
void LCD_gVLine(int16_t x, int16_t y1, int16_t y2, uint8_t stroke, pixel color)
{
int16_t aux;
if (stroke == 0)
return;
if (y1 > y2)
{
aux = y1;
y1 = y2;
y2 = aux;
}
// Check that the line is inside the screen. If not, no point clipping it to the edge
if ((x - ((stroke - 1) >> 1) < 0 && x + (stroke >> 1) < 0) || (x - ((stroke - 1) >> 1) > (LCD_WIDTH - 1) && x + (stroke >> 1) > (LCD_WIDTH - 1)))
return;
x = max(0, x - ((stroke - 1) >> 1));
aux = min(LCD_WIDTH - 1, x + (stroke >> 1));
LCD_gFillRect(x, y1, aux - x + 1, y2 - y1 + 1, color);
}
void LCD_gHLine(int16_t x1, int16_t x2, int16_t y, uint8_t stroke, pixel color)
{
int16_t aux;
if (stroke == 0)
return;
if (x1 > x2)
{
aux = x1;
x1 = x2;
x2 = aux;
}
// Check that the line is inside the screen. If not, no point clipping it to the edge
if ((y - ((stroke - 1) >> 1) < 0 && y + (stroke >> 1) < 0) || (y - ((stroke - 1) >> 1) > (LCD_HEIGHT - 1) && y + (stroke >> 1) > (LCD_HEIGHT - 1)))
return;
y = max(0, y - ((stroke - 1) >> 1));
aux = min(LCD_HEIGHT - 1, y + (stroke >> 1));
LCD_gFillRect(x1, y, x2 - x1 + 1, aux - y + 1, color);
}
void LCD_gLine(int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t stroke, pixel color)
{
float m;
int16_t aux;
uint8_t octant;
int16_t x, y;
// 1. Define octant
// Stay within one half to simplify calculations. This can be done by
// swapping x's and y's when needed
// straight lines are faster with their dedicated primitives
if (x1 == x2)
return LCD_gVLine(x1, y1, y2, stroke, color);
if (y1 == y2)
return LCD_gHLine(x1, x2, y1, stroke, color);
// move everything to quadrants 1 & 4
if (x2 < x1)
{
aux = x2;
x2 = x1;
x1 = aux;
aux = y2;
y2 = y1;
y1 = aux;
}
// find slope avoiding division by 0 with vertical lines
m = (float) (y2 - y1) / (x2 - x1);
// find octant
if (m > 1.0f)
octant = 2;
else if (m > 0.0f)
octant = 1;
else if (m > -1.0f)
octant = 8;
else
octant = 7;
// 2. Bresenham's
x = x1;
y = y1;
switch (octant)
{
case 2:
for (y = y1; y <= y2 && y < LCD_HEIGHT; y++)
{
x = (y - y1) / m + x1;
LCD_gDrawPixelE((uint8_t) x, (uint8_t) y, color.r, color.g, color.b);
}
break;
case 1:
for (x = x1; x <= x2 && x < LCD_WIDTH; x++)
{
y = m * (x - x1) + y1;
LCD_gDrawPixelE((uint8_t) x, (uint8_t) y, color.r, color.g, color.b);
}
break;
case 8:
for (x = x1; x <= x2 && x < LCD_WIDTH; x++)
{
y = m * (x - x1) + y1;
LCD_gDrawPixelE((uint8_t) x, (uint8_t) y, color.r, color.g, color.b);
}
break;
case 7:
for (y = y1; y >= y2 && y >= 0; y--)
{
x = (y - y1) / m + x1;
LCD_gDrawPixelE((uint8_t) x, (uint8_t) y, color.r, color.g, color.b);
}
break;
default:
return;
}
// 3. If stroke is not complete, define shift in position for next line
if (stroke > 1)
{
for (int i = - (stroke >> 1); i <= ((stroke - 1) >> 1); i++)
{
// original line is already drawn
if (i == 0)
continue;
// move coordinates somewhere else and draw line with stroke 1
switch (octant)
{
case 2: case 7:
x1++;
x2++;
break;
case 1: case 8:
y1++;
y2++;
break;
default:
break;
}
LCD_gLine(x1, y1, x2, y2, 1, color);
}
return;
}
}
// Filled rectangle
// Param:
// x, y: column and row of first corner
// w, h: width and height
// color: pixel
void LCD_gFillRect(int16_t x, int16_t y, uint8_t w, uint8_t h, pixel color)
{
LCD_SetArea(x, y, x + w - 1, y + h - 1);
LCD_ActivateWrite();
for (int i = 0; i <= h; i++)
for (int j = 0; j <= w; j++)
LCD_PushPixel(color.r, color.g, color.b);
}
// Rectangle outline
// Param:
// x, y: column and row of first corner
// w, h: width and height
// color: pixel
void LCD_gRect(int16_t x, int16_t y, uint8_t w, uint8_t h, uint8_t stroke, pixel color)
{
if (w == 0 || h == 0)
return;
LCD_gVLine(x, y, y + h, stroke, color);
LCD_gVLine(x + w - (stroke >> 1), y, y + h, stroke, color);
LCD_gHLine(x, x + w, y, stroke, color);
LCD_gHLine(x, x + w, y + h - (stroke >> 1), stroke, color);
}
// Triangle outline
// Param:
// v1: first vertex
// v2: second vertex
// v3: third vertex
// stroke: edge width
// color: pixel
void LCD_gTriangle(point v1, point v2, point v3, uint8_t stroke, pixel color)
{
LCD_gLine(v1.x, v1.y, v2.x, v2.y, stroke, color);
LCD_gLine(v2.x, v2.y, v3.x, v3.y, stroke, color);
LCD_gLine(v3.x, v3.y, v1.x, v1.y, stroke, color);
}
// Filled Triangle
// Param:
// v1: first vertex
// v2: second vertex
// v3: third vertex
// color: pixel
void LCD_gFillTriangle(point v1, point v2, point v3, pixel color)
{
// Algorithm taken from https://www.sunshine2k.de/coding/java/TriangleRasterization/TriangleRasterization.html
// Note that in this function top is higher y, when in reality this is not necessarily the case.
// This does not matter though
point top_v, bottom_v, middle_v, aux_v;
float invslope1, invslope2;
float curx1, curx2;
// Sort points
if (v1.y < v2.y && v1.y < v3.y)
{
bottom_v = v1;
// cmp v2 v3 for top
if (v2.y > v3.y)
{
top_v = v2;
middle_v = v3;
} else
{
top_v = v3;
middle_v = v2;
}
} else if (v1.y > v2.y && v1.y > v3.y)
{
top_v = v1;
// cmp v2 v3 for bottom
if (v2.y < v3.y)
{
bottom_v = v2;
middle_v = v3;
} else
{
bottom_v = v3;
middle_v = v2;
}
} else
{
middle_v = v1;
// cmp v2 v3 for top and bottom
if (v2.y > v3.y)
{
top_v = v2;
bottom_v = v3;
} else
{
top_v = v3;
bottom_v = v2;
}
}
// auxiliary point to divide the triangle into 2 sections
aux_v.x = (int)(top_v.x + ((float)(middle_v.y - top_v.y) / (float)(bottom_v.y - top_v.y)) * (bottom_v.x - top_v.x));
aux_v.y = middle_v.y;
// Draw flat bottom triangle
if (middle_v.y - top_v.y != 0 && aux_v.y - top_v.y != 0)
{
invslope1 = (float) (middle_v.x - top_v.x) / (middle_v.y - top_v.y);
invslope2 = (float) (aux_v.x - top_v.x) / (aux_v.y - top_v.y);
curx1 = top_v.x;
curx2 = top_v.x;
for (int16_t scanlineY = top_v.y; scanlineY > middle_v.y; scanlineY--)
{
LCD_gHLine((int16_t) curx1, (int16_t) curx2, scanlineY, 1, color);
curx1 += invslope1;
curx2 += invslope2;
}
}
// Draw flat top triangle
if (bottom_v.y - middle_v.y != 0 && bottom_v.y - aux_v.y != 0)
{
invslope1 = (float) (bottom_v.x - middle_v.x) / (bottom_v.y - middle_v.y);
invslope2 = (float) (bottom_v.x - aux_v.x) / (bottom_v.y - aux_v.y);
curx1 = bottom_v.x;
curx2 = bottom_v.x;
for (int16_t scanlineY = bottom_v.y; scanlineY <= middle_v.y; scanlineY++)
{
LCD_gHLine((int16_t) curx1, (int16_t) curx2, scanlineY, 1, color);
curx1 -= invslope1;
curx2 -= invslope2;
}
}
}
// Arbitrary polygon outline
// Edges are drawn in order from the first to the last and back to the first
// Param:
// vertices: array of vertices
// n_vertices: size of vertices array
// stroke: edge width
// color: pixel
void LCD_gPolygon(point *vertices, int n_vertices, uint8_t stroke, pixel color)
{
int i;
for (i = 0; i < n_vertices - 1; i++)
{
LCD_gLine(vertices[i].x, vertices[i].y, vertices[i + 1].x, vertices[i + 1].y, stroke, color);
}
LCD_gLine(vertices[i].x, vertices[i].y, vertices[0].x, vertices[0].y, stroke, color);
}
// Circle outline
// Param:
// x, y: circle center position
// r: circle radius
// stroke: outline width
// color: pixel
void LCD_gCircle(int16_t x, int16_t y, float r, uint8_t stroke, pixel color)
{
int16_t x_ = 1, y_ = r;
int16_t r2 = r * r + 1;
int16_t res, res2;
if (r == 0.0f)
return;
// intersections with X and Y are easy using the radius
LCD_gDrawPixelE(x + r, y, color.r, color.g, color.b);
LCD_gDrawPixelE(x - r, y, color.r, color.g, color.b);
LCD_gDrawPixelE(x, y + r, color.r, color.g, color.b);
LCD_gDrawPixelE(x, y - r, color.r, color.g, color.b);
while (y_ / x_ >= 1)
{
// calculate next point, which will be down 1 pixel or on the same height
// see which one satisfies x^2 + y^2 >= r^2 and maximizes x^2 + y^2
// draw point, copy over to other octants, update x_ and y_
res = x_ * x_ + y_ * y_;
res2 = x_ * x_ + (y_ - 1) * (y_ - 1);
if (res > r2 || res2 > res)
{
y_--;
}
LCD_gDrawPixelE(x + x_, y - y_, color.r, color.g, color.b);
LCD_gDrawPixelE(x - x_, y - y_, color.r, color.g, color.b);
LCD_gDrawPixelE(x + x_, y + y_, color.r, color.g, color.b);
LCD_gDrawPixelE(x - x_, y + y_, color.r, color.g, color.b);
LCD_gDrawPixelE(x + y_, y - x_, color.r, color.g, color.b);
LCD_gDrawPixelE(x - y_, y - x_, color.r, color.g, color.b);
LCD_gDrawPixelE(x + y_, y + x_, color.r, color.g, color.b);
LCD_gDrawPixelE(x - y_, y + x_, color.r, color.g, color.b);
x_++;
}
return;
}
// Filled cricle
// Param:
// x, y: circle center position
// r: circle radius
// color: pixel
void LCD_gFillCircle(int16_t x, int16_t y, float r, pixel color)
{
int16_t x_ = 1, y_ = r;
int16_t r2 = r * r + 1;
int16_t res, res2;
if (r == 0.0f)
return;
// intersections with X and Y are easy using the radius