forked from PaulStoffregen/ILI9341_t3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ILI9341_t3.cpp
executable file
·1961 lines (1719 loc) · 53 KB
/
ILI9341_t3.cpp
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
// https://github.com/PaulStoffregen/ILI9341_t3
// http://forum.pjrc.com/threads/26305-Highly-optimized-ILI9341-(320x240-TFT-color-display)-library
/***************************************************
This is our library for the Adafruit ILI9341 Breakout and Shield
----> http://www.adafruit.com/products/1651
Check out the links above for our tutorials and wiring diagrams
These displays use SPI to communicate, 4 or 5 pins are required to
interface (RST is optional)
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
// <SoftEgg>
//Additional graphics routines by Tim Trzepacz, SoftEgg LLC added December 2015
//(And then accidentally deleted and rewritten March 2016. Oops!)
//Gradient support
//----------------
// fillRectVGradient - fills area with vertical gradient
// fillRectHGradient - fills area with horizontal gradient
// fillScreenVGradient - fills screen with vertical gradient
// fillScreenHGradient - fills screen with horizontal gradient
//Additional Color Support
//------------------------
// color565toRGB - converts 565 format 16 bit color to RGB
// color565toRGB14 - converts 16 bit 565 format color to 14 bit RGB (2 bits clear for math and sign)
// RGB14tocolor565 - converts 14 bit RGB back to 16 bit 565 format color
//Low Memory Bitmap Support
//-------------------------
// writeRect8BPP - write 8 bit per pixel paletted bitmap
// writeRect4BPP - write 4 bit per pixel paletted bitmap
// writeRect2BPP - write 2 bit per pixel paletted bitmap
// writeRect1BPP - write 1 bit per pixel paletted bitmap
//TODO: transparent bitmap writing routines for sprites
//String Pixel Length support
//---------------------------
// strPixelLen - gets pixel length of given ASCII string
// <\SoftEgg>
#include "ILI9341_t3.h"
#include <SPI.h>
// Teensy 3.1 can only generate 30 MHz SPI when running at 120 MHz (overclock)
// At all other speeds, SPI.beginTransaction() will use the fastest available clock
#define SPICLOCK 30000000
#define WIDTH ILI9341_TFTWIDTH
#define HEIGHT ILI9341_TFTHEIGHT
// Constructor when using hardware SPI. Faster, but must use SPI pins
// specific to each board type (e.g. 11,13 for Uno, 51,52 for Mega, etc.)
ILI9341_t3::ILI9341_t3(uint8_t cs, uint8_t dc, uint8_t rst, uint8_t mosi, uint8_t sclk, uint8_t miso)
{
_cs = cs;
_dc = dc;
_rst = rst;
_mosi = mosi;
_sclk = sclk;
_miso = miso;
_width = WIDTH;
_height = HEIGHT;
rotation = 0;
cursor_y = cursor_x = 0;
textsize = 1;
textcolor = textbgcolor = 0xFFFF;
wrap = true;
setClipRect();
font = NULL;
}
void ILI9341_t3::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
{
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
setAddr(x0, y0, x1, y1);
writecommand_last(ILI9341_RAMWR); // write to RAM
SPI.endTransaction();
}
void ILI9341_t3::pushColor(uint16_t color)
{
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
writedata16_last(color);
SPI.endTransaction();
}
void ILI9341_t3::drawPixel(int16_t x, int16_t y, uint16_t color) {
if((x < _clipx1) ||(x >= _clipx2) || (y < _clipy1) || (y >= _clipy2)) return;
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
setAddr(x, y, x, y);
writecommand_cont(ILI9341_RAMWR);
writedata16_last(color);
SPI.endTransaction();
}
void ILI9341_t3::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color)
{
// Rudimentary clipping
if((x < _clipx1) || (x >= _clipx2) || (y >= _clipy2)) return;
if(y < _clipy1) { h = h - (_clipy1 - y); y = _clipy1;}
if((y+h-1) >= _clipy2) h = _clipy2-y;
if(h<1) return;
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
setAddr(x, y, x, y+h-1);
writecommand_cont(ILI9341_RAMWR);
while (h-- > 1) {
writedata16_cont(color);
}
writedata16_last(color);
SPI.endTransaction();
}
void ILI9341_t3::drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color)
{
// Rudimentary clipping
if((y < _clipy1) || (x >= _clipx2) || (y >= _clipy2)) return;
if(x<_clipx1) { w = w - (_clipx1 - x); x = _clipx1; }
if((x+w-1) >= _clipx2) w = _clipx2-x;
if (w<1) return;
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
setAddr(x, y, x+w-1, y);
writecommand_cont(ILI9341_RAMWR);
while (w-- > 1) {
writedata16_cont(color);
}
writedata16_last(color);
SPI.endTransaction();
}
void ILI9341_t3::fillScreen(uint16_t color)
{
fillRect(_clipx1, _clipy1, _clipx2, _clipy2, color);
}
// fill a rectangle
void ILI9341_t3::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
{
// rudimentary clipping (drawChar w/big text requires this)
if((x >= _clipx2) || (y >= _clipy2)) return;
if((x + w - 1) >= _clipx2) w = _clipx2 - x;
if((y + h - 1) >= _clipy2) h = _clipy2 - y;
if(x < _clipx1) x = _clipx1;
if(y < _clipy1) y = _clipy1;
// TODO: this can result in a very long transaction time
// should break this into multiple transactions, even though
// it'll cost more overhead, so we don't stall other SPI libs
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
setAddr(x, y, x+w-1, y+h-1);
writecommand_cont(ILI9341_RAMWR);
for(y=h; y>0; y--) {
for(x=w; x>1; x--) {
writedata16_cont(color);
}
writedata16_last(color);
if (y > 1 && (y & 1)) {
SPI.endTransaction();
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
}
}
SPI.endTransaction();
}
// fillRectVGradient - fills area with vertical gradient
void ILI9341_t3::fillRectVGradient(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color1, uint16_t color2)
{
// rudimentary clipping (drawChar w/big text requires this)
if((x >= _width) || (y >= _height)) return;
if((x + w - 1) >= _width) w = _width - x;
if((y + h - 1) >= _height) h = _height - y;
int16_t r1, g1, b1, r2, g2, b2, dr, dg, db, r, g, b;
color565toRGB14(color1,r1,g1,b1);
color565toRGB14(color2,r2,g2,b2);
dr=(r2-r1)/h; dg=(g2-g1)/h; db=(b2-b1)/h;
r=r1;g=g1;b=b1;
// TODO: this can result in a very long transaction time
// should break this into multiple transactions, even though
// it'll cost more overhead, so we don't stall other SPI libs
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
setAddr(x, y, x+w-1, y+h-1);
writecommand_cont(ILI9341_RAMWR);
for(y=h; y>0; y--) {
uint16_t color = RGB14tocolor565(r,g,b);
for(x=w; x>1; x--) {
writedata16_cont(color);
}
writedata16_last(color);
if (y > 1 && (y & 1)) {
SPI.endTransaction();
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
}
r+=dr;g+=dg; b+=db;
}
SPI.endTransaction();
}
// fillRectHGradient - fills area with horizontal gradient
void ILI9341_t3::fillRectHGradient(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color1, uint16_t color2)
{
// rudimentary clipping (drawChar w/big text requires this)
if((x >= _width) || (y >= _height)) return;
if((x + w - 1) >= _width) w = _width - x;
if((y + h - 1) >= _height) h = _height - y;
int16_t r1, g1, b1, r2, g2, b2, dr, dg, db, r, g, b;
color565toRGB14(color1,r1,g1,b1);
color565toRGB14(color2,r2,g2,b2);
dr=(r2-r1)/h; dg=(g2-g1)/h; db=(b2-b1)/h;
r=r1;g=g1;b=b1;
// TODO: this can result in a very long transaction time
// should break this into multiple transactions, even though
// it'll cost more overhead, so we don't stall other SPI libs
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
setAddr(x, y, x+w-1, y+h-1);
writecommand_cont(ILI9341_RAMWR);
for(y=h; y>0; y--) {
uint16_t color;
for(x=w; x>1; x--) {
color = RGB14tocolor565(r,g,b);
writedata16_cont(color);
r+=dr;g+=dg; b+=db;
}
color = RGB14tocolor565(r,g,b);
writedata16_last(color);
if (y > 1 && (y & 1)) {
SPI.endTransaction();
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
}
r=r1;g=g1;b=b1;
}
SPI.endTransaction();
}
// fillScreenVGradient - fills screen with vertical gradient
void ILI9341_t3::fillScreenVGradient(uint16_t color1, uint16_t color2)
{
fillRectVGradient(0,0,_width,_height,color1,color2);
}
// fillScreenHGradient - fills screen with horizontal gradient
void ILI9341_t3::fillScreenHGradient(uint16_t color1, uint16_t color2)
{
fillRectHGradient(0,0,_width,_height,color1,color2);
}
#define MADCTL_MY 0x80
#define MADCTL_MX 0x40
#define MADCTL_MV 0x20
#define MADCTL_ML 0x10
#define MADCTL_RGB 0x00
#define MADCTL_BGR 0x08
#define MADCTL_MH 0x04
void ILI9341_t3::setRotation(uint8_t m)
{
rotation = m % 4; // can't be higher than 3
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
writecommand_cont(ILI9341_MADCTL);
switch (rotation) {
case 0:
writedata8_last(MADCTL_MX | MADCTL_BGR);
_width = ILI9341_TFTWIDTH;
_height = ILI9341_TFTHEIGHT;
break;
case 1:
writedata8_last(MADCTL_MV | MADCTL_BGR);
_width = ILI9341_TFTHEIGHT;
_height = ILI9341_TFTWIDTH;
break;
case 2:
writedata8_last(MADCTL_MY | MADCTL_BGR);
_width = ILI9341_TFTWIDTH;
_height = ILI9341_TFTHEIGHT;
break;
case 3:
writedata8_last(MADCTL_MX | MADCTL_MY | MADCTL_MV | MADCTL_BGR);
_width = ILI9341_TFTHEIGHT;
_height = ILI9341_TFTWIDTH;
break;
}
SPI.endTransaction();
setClipRect();
cursor_x = 0;
cursor_y = 0;
}
void ILI9341_t3::setScroll(uint16_t offset)
{
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
writecommand_cont(ILI9341_VSCRSADD);
writedata16_last(offset);
SPI.endTransaction();
}
void ILI9341_t3::invertDisplay(boolean i)
{
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
writecommand_last(i ? ILI9341_INVON : ILI9341_INVOFF);
SPI.endTransaction();
}
/*
uint8_t ILI9341_t3::readdata(void)
{
uint8_t r;
// Try to work directly with SPI registers...
// First wait until output queue is empty
uint16_t wTimeout = 0xffff;
while (((KINETISK_SPI0.SR) & (15 << 12)) && (--wTimeout)) ; // wait until empty
// KINETISK_SPI0.MCR |= SPI_MCR_CLR_RXF; // discard any received data
// KINETISK_SPI0.SR = SPI_SR_TCF;
// Transfer a 0 out...
writedata8_cont(0);
// Now wait until completed.
wTimeout = 0xffff;
while (((KINETISK_SPI0.SR) & (15 << 12)) && (--wTimeout)) ; // wait until empty
r = KINETISK_SPI0.POPR; // get the received byte... should check for it first...
return r;
}
*/
uint8_t ILI9341_t3::readcommand8(uint8_t c, uint8_t index)
{
uint16_t wTimeout = 0xffff;
uint8_t r=0;
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
while (((KINETISK_SPI0.SR) & (15 << 12)) && (--wTimeout)) ; // wait until empty
// Make sure the last frame has been sent...
KINETISK_SPI0.SR = SPI_SR_TCF; // dlear it out;
wTimeout = 0xffff;
while (!((KINETISK_SPI0.SR) & SPI_SR_TCF) && (--wTimeout)) ; // wait until it says the last frame completed
// clear out any current received bytes
wTimeout = 0x10; // should not go more than 4...
while ((((KINETISK_SPI0.SR) >> 4) & 0xf) && (--wTimeout)) {
r = KINETISK_SPI0.POPR;
}
//writecommand(0xD9); // sekret command
KINETISK_SPI0.PUSHR = 0xD9 | (pcs_command << 16) | SPI_PUSHR_CTAS(0) | SPI_PUSHR_CONT;
// while (((KINETISK_SPI0.SR) & (15 << 12)) > (3 << 12)) ; // wait if FIFO full
// writedata(0x10 + index);
KINETISK_SPI0.PUSHR = (0x10 + index) | (pcs_data << 16) | SPI_PUSHR_CTAS(0);
// while (((KINETISK_SPI0.SR) & (15 << 12)) > (3 << 12)) ; // wait if FIFO full
// writecommand(c);
KINETISK_SPI0.PUSHR = c | (pcs_command << 16) | SPI_PUSHR_CTAS(0) | SPI_PUSHR_CONT;
// while (((KINETISK_SPI0.SR) & (15 << 12)) > (3 << 12)) ; // wait if FIFO full
// readdata
KINETISK_SPI0.PUSHR = 0 | (pcs_data << 16) | SPI_PUSHR_CTAS(0);
// while (((KINETISK_SPI0.SR) & (15 << 12)) > (3 << 12)) ; // wait if FIFO full
// Now wait until completed.
wTimeout = 0xffff;
while (((KINETISK_SPI0.SR) & (15 << 12)) && (--wTimeout)) ; // wait until empty
// Make sure the last frame has been sent...
KINETISK_SPI0.SR = SPI_SR_TCF; // dlear it out;
wTimeout = 0xffff;
while (!((KINETISK_SPI0.SR) & SPI_SR_TCF) && (--wTimeout)) ; // wait until it says the last frame completed
wTimeout = 0x10; // should not go more than 4...
// lets get all of the values on the FIFO
while ((((KINETISK_SPI0.SR) >> 4) & 0xf) && (--wTimeout)) {
r = KINETISK_SPI0.POPR;
}
SPI.endTransaction();
return r; // get the received byte... should check for it first...
}
// Read Pixel at x,y and get back 16-bit packed color
uint16_t ILI9341_t3::readPixel(int16_t x, int16_t y)
{
uint8_t dummy __attribute__((unused));
uint8_t r,g,b;
SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0));
setAddr(x, y, x, y);
writecommand_cont(ILI9341_RAMRD); // read from RAM
waitTransmitComplete();
// Push 4 bytes over SPI
KINETISK_SPI0.PUSHR = 0 | (pcs_data << 16) | SPI_PUSHR_CTAS(0)| SPI_PUSHR_CONT;
waitFifoEmpty(); // wait for both queues to be empty.
KINETISK_SPI0.PUSHR = 0 | (pcs_data << 16) | SPI_PUSHR_CTAS(0)| SPI_PUSHR_CONT;
KINETISK_SPI0.PUSHR = 0 | (pcs_data << 16) | SPI_PUSHR_CTAS(0)| SPI_PUSHR_CONT;
KINETISK_SPI0.PUSHR = 0 | (pcs_data << 16) | SPI_PUSHR_CTAS(0)| SPI_PUSHR_EOQ;
// Wait for End of Queue
while ((KINETISK_SPI0.SR & SPI_SR_EOQF) == 0) ;
KINETISK_SPI0.SR = SPI_SR_EOQF; // make sure it is clear
// Read Pixel Data
dummy = KINETISK_SPI0.POPR; // Read a DUMMY byte of GRAM
r = KINETISK_SPI0.POPR; // Read a RED byte of GRAM
g = KINETISK_SPI0.POPR; // Read a GREEN byte of GRAM
b = KINETISK_SPI0.POPR; // Read a BLUE byte of GRAM
SPI.endTransaction();
return color565(r,g,b);
}
// Now lets see if we can read in multiple pixels
void ILI9341_t3::readRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t *pcolors)
{
uint8_t dummy __attribute__((unused));
uint8_t r,g,b;
uint16_t c = w * h;
SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0));
setAddr(x, y, x+w-1, y+h-1);
writecommand_cont(ILI9341_RAMRD); // read from RAM
waitTransmitComplete();
KINETISK_SPI0.PUSHR = 0 | (pcs_data << 16) | SPI_PUSHR_CTAS(0)| SPI_PUSHR_CONT | SPI_PUSHR_EOQ;
while ((KINETISK_SPI0.SR & SPI_SR_EOQF) == 0) ;
KINETISK_SPI0.SR = SPI_SR_EOQF; // make sure it is clear
while ((KINETISK_SPI0.SR & 0xf0)) {
dummy = KINETISK_SPI0.POPR; // Read a DUMMY byte but only once
}
c *= 3; // number of bytes we will transmit to the display
while (c--) {
if (c) {
KINETISK_SPI0.PUSHR = 0 | (pcs_data << 16) | SPI_PUSHR_CTAS(0)| SPI_PUSHR_CONT;
} else {
KINETISK_SPI0.PUSHR = 0 | (pcs_data << 16) | SPI_PUSHR_CTAS(0)| SPI_PUSHR_EOQ;
}
// If last byte wait until all have come in...
if (c == 0) {
while ((KINETISK_SPI0.SR & SPI_SR_EOQF) == 0) ;
KINETISK_SPI0.SR = SPI_SR_EOQF; // make sure it is clear
}
if ((KINETISK_SPI0.SR & 0xf0) >= 0x30) { // do we have at least 3 bytes in queue if so extract...
r = KINETISK_SPI0.POPR; // Read a RED byte of GRAM
g = KINETISK_SPI0.POPR; // Read a GREEN byte of GRAM
b = KINETISK_SPI0.POPR; // Read a BLUE byte of GRAM
*pcolors++ = color565(r,g,b);
}
// like waitFiroNotFull but does not pop our return queue
while ((KINETISK_SPI0.SR & (15 << 12)) > (3 << 12)) ;
}
SPI.endTransaction();
}
// Now lets see if we can writemultiple pixels
void ILI9341_t3::writeRect(int16_t x, int16_t y, int16_t w, int16_t h, const uint16_t *pcolors)
{
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
setAddr(x, y, x+w-1, y+h-1);
writecommand_cont(ILI9341_RAMWR);
for(y=h; y>0; y--) {
for(x=w; x>1; x--) {
writedata16_cont(*pcolors++);
}
writedata16_last(*pcolors++);
}
SPI.endTransaction();
}
// writeRect8BPP - write 8 bit per pixel paletted bitmap
// bitmap data in array at pixels, one byte per pixel
// color palette data in array at palette
void ILI9341_t3::writeRect8BPP(int16_t x, int16_t y, int16_t w, int16_t h, const uint8_t *pixels, const uint16_t * palette )
{
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
setAddr(x, y, x+w-1, y+h-1);
writecommand_cont(ILI9341_RAMWR);
for(y=h; y>0; y--) {
for(x=w; x>1; x--) {
writedata16_cont(palette[*pixels++]);
}
writedata16_last(palette[*pixels++]);
}
SPI.endTransaction();
}
// writeRect4BPP - write 4 bit per pixel paletted bitmap
// bitmap data in array at pixels, 4 bits per pixel
// color palette data in array at palette
// width must be at least 2 pixels
void ILI9341_t3::writeRect4BPP(int16_t x, int16_t y, int16_t w, int16_t h, const uint8_t *pixels, const uint16_t * palette )
{
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
setAddr(x, y, x+w-1, y+h-1);
writecommand_cont(ILI9341_RAMWR);
for(y=h; y>0; y--) {
for(x=w; x>2; x-=2) {
writedata16_cont(palette[((*pixels)>>4)&0xF]);
writedata16_cont(palette[(*pixels++)&0xF]);
}
writedata16_cont(palette[((*pixels)>>4)&0xF]);
writedata16_last(palette[(*pixels++)&0xF]);
}
SPI.endTransaction();
}
// writeRect2BPP - write 2 bit per pixel paletted bitmap
// bitmap data in array at pixels, 4 bits per pixel
// color palette data in array at palette
// width must be at least 4 pixels
void ILI9341_t3::writeRect2BPP(int16_t x, int16_t y, int16_t w, int16_t h, const uint8_t *pixels, const uint16_t * palette )
{
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
setAddr(x, y, x+w-1, y+h-1);
writecommand_cont(ILI9341_RAMWR);
for(y=h; y>0; y--) {
for(x=w; x>4; x-=4) {
//unrolled loop might be faster?
writedata16_cont(palette[((*pixels)>>6)&0x3]);
writedata16_cont(palette[((*pixels)>>4)&0x3]);
writedata16_cont(palette[((*pixels)>>2)&0x3]);
writedata16_cont(palette[(*pixels++)&0x3]);
}
writedata16_cont(palette[((*pixels)>>6)&0x3]);
writedata16_cont(palette[((*pixels)>>4)&0x3]);
writedata16_cont(palette[((*pixels)>>2)&0x3]);
writedata16_last(palette[(*pixels++)&0x3]);
}
SPI.endTransaction();
}
// writeRect1BPP - write 1 bit per pixel paletted bitmap
// bitmap data in array at pixels, 4 bits per pixel
// color palette data in array at palette
// width must be at least 8 pixels
void ILI9341_t3::writeRect1BPP(int16_t x, int16_t y, int16_t w, int16_t h, const uint8_t *pixels, const uint16_t * palette )
{
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
setAddr(x, y, x+w-1, y+h-1);
writecommand_cont(ILI9341_RAMWR);
for(y=h; y>0; y--) {
for(x=w; x>8; x-=8) {
//unrolled loop might be faster?
writedata16_cont(palette[((*pixels)>>7)&0x1]);
writedata16_cont(palette[((*pixels)>>6)&0x1]);
writedata16_cont(palette[((*pixels)>>5)&0x1]);
writedata16_cont(palette[((*pixels)>>4)&0x1]);
writedata16_cont(palette[((*pixels)>>3)&0x1]);
writedata16_cont(palette[((*pixels)>>2)&0x1]);
writedata16_cont(palette[((*pixels)>>1)&0x1]);
writedata16_cont(palette[(*pixels++)&0x1]);
}
writedata16_cont(palette[((*pixels)>>7)&0x1]);
writedata16_cont(palette[((*pixels)>>6)&0x1]);
writedata16_cont(palette[((*pixels)>>5)&0x1]);
writedata16_cont(palette[((*pixels)>>4)&0x1]);
writedata16_cont(palette[((*pixels)>>3)&0x1]);
writedata16_cont(palette[((*pixels)>>2)&0x1]);
writedata16_cont(palette[((*pixels)>>1)&0x1]);
writedata16_last(palette[(*pixels++)&0x1]);
}
SPI.endTransaction();
}
static const uint8_t init_commands[] = {
4, 0xEF, 0x03, 0x80, 0x02,
4, 0xCF, 0x00, 0XC1, 0X30,
5, 0xED, 0x64, 0x03, 0X12, 0X81,
4, 0xE8, 0x85, 0x00, 0x78,
6, 0xCB, 0x39, 0x2C, 0x00, 0x34, 0x02,
2, 0xF7, 0x20,
3, 0xEA, 0x00, 0x00,
2, ILI9341_PWCTR1, 0x23, // Power control
2, ILI9341_PWCTR2, 0x10, // Power control
3, ILI9341_VMCTR1, 0x3e, 0x28, // VCM control
2, ILI9341_VMCTR2, 0x86, // VCM control2
2, ILI9341_MADCTL, 0x48, // Memory Access Control
2, ILI9341_PIXFMT, 0x55,
3, ILI9341_FRMCTR1, 0x00, 0x18,
4, ILI9341_DFUNCTR, 0x08, 0x82, 0x27, // Display Function Control
2, 0xF2, 0x00, // Gamma Function Disable
2, ILI9341_GAMMASET, 0x01, // Gamma curve selected
16, ILI9341_GMCTRP1, 0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08,
0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00, // Set Gamma
16, ILI9341_GMCTRN1, 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07,
0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F, // Set Gamma
3, 0xb1, 0x00, 0x10, // FrameRate Control 119Hz
0
};
void ILI9341_t3::begin(void)
{
// verify SPI pins are valid;
#if defined(__MK64FX512__) || defined(__MK66FX1M0__)
if ((_mosi == 11 || _mosi == 7 || _mosi == 28) && (_miso == 12 || _miso == 8 || _miso == 39)
&& (_sclk == 13 || _sclk == 14 || _sclk == 27)) {
#else
if ((_mosi == 11 || _mosi == 7) && (_miso == 12 || _miso == 8) && (_sclk == 13 || _sclk == 14)) {
#endif
SPI.setMOSI(_mosi);
SPI.setMISO(_miso);
SPI.setSCK(_sclk);
} else
return; // not valid pins...
SPI.begin();
if (SPI.pinIsChipSelect(_cs, _dc)) {
pcs_data = SPI.setCS(_cs);
pcs_command = pcs_data | SPI.setCS(_dc);
} else {
pcs_data = 0;
pcs_command = 0;
return;
}
// toggle RST low to reset
if (_rst < 255) {
pinMode(_rst, OUTPUT);
digitalWrite(_rst, HIGH);
delay(5);
digitalWrite(_rst, LOW);
delay(20);
digitalWrite(_rst, HIGH);
delay(150);
}
/*
uint8_t x = readcommand8(ILI9341_RDMODE);
Serial.print("\nDisplay Power Mode: 0x"); Serial.println(x, HEX);
x = readcommand8(ILI9341_RDMADCTL);
Serial.print("\nMADCTL Mode: 0x"); Serial.println(x, HEX);
x = readcommand8(ILI9341_RDPIXFMT);
Serial.print("\nPixel Format: 0x"); Serial.println(x, HEX);
x = readcommand8(ILI9341_RDIMGFMT);
Serial.print("\nImage Format: 0x"); Serial.println(x, HEX);
x = readcommand8(ILI9341_RDSELFDIAG);
Serial.print("\nSelf Diagnostic: 0x"); Serial.println(x, HEX);
*/
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
const uint8_t *addr = init_commands;
while (1) {
uint8_t count = *addr++;
if (count-- == 0) break;
writecommand_cont(*addr++);
while (count-- > 0) {
writedata8_cont(*addr++);
}
}
writecommand_last(ILI9341_SLPOUT); // Exit Sleep
SPI.endTransaction();
delay(120);
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
writecommand_last(ILI9341_DISPON); // Display on
SPI.endTransaction();
}
/*
This is the core graphics library for all our displays, providing a common
set of graphics primitives (points, lines, circles, etc.). It needs to be
paired with a hardware-specific library for each display device we carry
(to handle the lower-level functions).
Adafruit invests time and resources providing this open source code, please
support Adafruit & open-source hardware by purchasing products from Adafruit!
Copyright (c) 2013 Adafruit Industries. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
//#include "glcdfont.c"
extern "C" const unsigned char glcdfont[];
// Draw a circle outline
void ILI9341_t3::drawCircle(int16_t x0, int16_t y0, int16_t r,
uint16_t color) {
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
int16_t x = 0;
int16_t y = r;
drawPixel(x0 , y0+r, color);
drawPixel(x0 , y0-r, color);
drawPixel(x0+r, y0 , color);
drawPixel(x0-r, y0 , color);
while (x<y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
drawPixel(x0 + x, y0 + y, color);
drawPixel(x0 - x, y0 + y, color);
drawPixel(x0 + x, y0 - y, color);
drawPixel(x0 - x, y0 - y, color);
drawPixel(x0 + y, y0 + x, color);
drawPixel(x0 - y, y0 + x, color);
drawPixel(x0 + y, y0 - x, color);
drawPixel(x0 - y, y0 - x, color);
}
}
void ILI9341_t3::drawCircleHelper( int16_t x0, int16_t y0,
int16_t r, uint8_t cornername, uint16_t color) {
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
int16_t x = 0;
int16_t y = r;
while (x<y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
if (cornername & 0x4) {
drawPixel(x0 + x, y0 + y, color);
drawPixel(x0 + y, y0 + x, color);
}
if (cornername & 0x2) {
drawPixel(x0 + x, y0 - y, color);
drawPixel(x0 + y, y0 - x, color);
}
if (cornername & 0x8) {
drawPixel(x0 - y, y0 + x, color);
drawPixel(x0 - x, y0 + y, color);
}
if (cornername & 0x1) {
drawPixel(x0 - y, y0 - x, color);
drawPixel(x0 - x, y0 - y, color);
}
}
}
void ILI9341_t3::fillCircle(int16_t x0, int16_t y0, int16_t r,
uint16_t color) {
drawFastVLine(x0, y0-r, 2*r+1, color);
fillCircleHelper(x0, y0, r, 3, 0, color);
}
// Used to do circles and roundrects
void ILI9341_t3::fillCircleHelper(int16_t x0, int16_t y0, int16_t r,
uint8_t cornername, int16_t delta, uint16_t color) {
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -2 * r;
int16_t x = 0;
int16_t y = r;
while (x<y) {
if (f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
if (cornername & 0x1) {
drawFastVLine(x0+x, y0-y, 2*y+1+delta, color);
drawFastVLine(x0+y, y0-x, 2*x+1+delta, color);
}
if (cornername & 0x2) {
drawFastVLine(x0-x, y0-y, 2*y+1+delta, color);
drawFastVLine(x0-y, y0-x, 2*x+1+delta, color);
}
}
}
void ILI9341_t3::drawEllipse(int16_t x0, int16_t y0, int16_t r_x, int16_t r_y, uint16_t color)
{
int16_t x, y;
int32_t e, e2, dx, dy, rx, ry;
x = -r_x;
y = 0;
dx = (1+(2*x))*r_y*r_y;
dy = x*x;
e = dx+dy;
rx = 2*r_x*r_x;
ry = 2*r_y*r_y;
while(x <= 0)
{
drawPixel(x0 - x, y0 + y, color);
drawPixel(x0 + x, y0 + y, color);
drawPixel(x0 + x, y0 - y, color);
drawPixel(x0 - x, y0 - y, color);
e2 = 2*e;
if(e2 >= dx) { x++; dx += ry; e += dx; }
if(e2 <= dy) { y++; dy += rx; e += dy; }
}
while(y++ < r_y)
{
drawPixel(x0, y0 + y, color);
drawPixel(x0, y0 - y, color);
}
return;
}
void ILI9341_t3::fillEllipse(int16_t x0, int16_t y0, int16_t r_x, int16_t r_y, uint16_t color)
{
int16_t x, y;
int32_t e, e2, dx, dy, rx, ry;
x = -r_x;
y = 0;
dx = (1+(2*x))*r_y*r_y;
dy = x*x;
e = dx+dy;
rx = 2*r_x*r_x;
ry = 2*r_y*r_y;
while(x <= 0)
{
fillRect(x0 + x, y0 + y, 2*-x, 1, color);
fillRect(x0 + x, y0 - y, 2*-x, 1, color);
e2 = 2*e;
if(e2 >= dx) { x++; dx += ry; e += dx; }
if(e2 <= dy) { y++; dy += rx; e += dy; }
}
while(y++ < r_y)
{
drawPixel(x0, y0 + y, color);
drawPixel(x0, y0 - y, color);
}
return;
}
// Bresenham's algorithm - thx wikpedia
void ILI9341_t3::drawLine(int16_t x0, int16_t y0,
int16_t x1, int16_t y1, uint16_t color)
{
if (y0 == y1) {
if (x1 > x0) {
drawFastHLine(x0, y0, x1 - x0 + 1, color);
} else if (x1 < x0) {
drawFastHLine(x1, y0, x0 - x1 + 1, color);
} else {
drawPixel(x0, y0, color);
}
return;
} else if (x0 == x1) {
if (y1 > y0) {
drawFastVLine(x0, y0, y1 - y0 + 1, color);
} else {
drawFastVLine(x0, y1, y0 - y1 + 1, color);
}
return;
}
bool steep = abs(y1 - y0) > abs(x1 - x0);
if (steep) {
swap(x0, y0);
swap(x1, y1);
}
if (x0 > x1) {
swap(x0, x1);
swap(y0, y1);
}
int16_t dx, dy;
dx = x1 - x0;
dy = abs(y1 - y0);
int16_t err = dx / 2;
int16_t ystep;
if (y0 < y1) {
ystep = 1;
} else {
ystep = -1;
}
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
int16_t xbegin = x0;
if (steep) {
for (; x0<=x1; x0++) {
err -= dy;
if (err < 0) {
int16_t len = x0 - xbegin;
if (len) {
VLine(y0, xbegin, len + 1, color);
} else {
Pixel(y0, x0, color);
}
xbegin = x0 + 1;
y0 += ystep;
err += dx;
}
}
if (x0 > xbegin + 1) {
VLine(y0, xbegin, x0 - xbegin, color);
}
} else {
for (; x0<=x1; x0++) {
err -= dy;
if (err < 0) {
int16_t len = x0 - xbegin;
if (len) {
HLine(xbegin, y0, len + 1, color);
} else {
Pixel(x0, y0, color);
}
xbegin = x0 + 1;
y0 += ystep;
err += dx;
}
}
if (x0 > xbegin + 1) {
HLine(xbegin, y0, x0 - xbegin, color);
}
}
writecommand_last(ILI9341_NOP);
SPI.endTransaction();
}
// Draw a rectangle
void ILI9341_t3::drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
{
SPI.beginTransaction(SPISettings(SPICLOCK, MSBFIRST, SPI_MODE0));
HLine(x, y, w, color);
HLine(x, y+h-1, w, color);
VLine(x, y, h, color);
VLine(x+w-1, y, h, color);