-
Notifications
You must be signed in to change notification settings - Fork 36
/
ILI9341_due.cpp
3431 lines (3048 loc) · 94.5 KB
/
ILI9341_due.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
/*
ILI9341_due_.cpp - Arduino Due library for interfacing with ILI9341-based TFTs
Copyright (c) 2014 Marek Buriak
This library is based on ILI9341_t3 library from Paul Stoffregen
(https://github.com/PaulStoffregen/ILI9341_t3), Adafruit_ILI9341
and Adafruit_GFX libraries from Limor Fried/Ladyada
(https://github.com/adafruit/Adafruit_ILI9341).
This file is part of the Arduino ILI9341_due library.
Sources for this library can be found at https://github.com/marekburiak/ILI9341_Due.
ILI9341_due is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2.1 of the License, or
(at your option) any later version.
ILI9341_due is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with ILI9341_due. If not, see <http://www.gnu.org/licenses/>.
*/
/***************************************************
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.
****************************************************/
#include "ILI9341_due.h"
#if SPI_MODE_NORMAL | SPI_MODE_EXTENDED | defined(ILI_USE_SPI_TRANSACTION)
#include <SPI.h>
#endif
//#include "../Streaming/Streaming.h"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"
#pragma GCC diagnostic ignored "-Wswitch"
static const uint8_t init_commands[] PROGMEM = {
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
0
};
ILI9341_due::ILI9341_due(uint8_t cs, uint8_t dc, uint8_t rst)
{
_cs = cs;
_dc = dc;
_rst = rst;
_spiClkDivider = ILI9341_SPI_CLKDIVIDER;
_width = ILI9341_TFTWIDTH;
_height = ILI9341_TFTHEIGHT;
_area.x = 0;
_area.y = 0;
_area.w = ILI9341_TFTWIDTH;
_area.h = ILI9341_TFTHEIGHT;
_rotation = iliRotation0;
_arcAngleMax = DEFAULT_ARC_ANGLE_MAX;
_angleOffset = DEFAULT_ANGLE_OFFSET;
#ifdef ILI_USE_SPI_TRANSACTION
_isInTransaction = false;
#endif
_fontMode = gTextFontModeSolid;
_fontBgColor = ILI9341_BLACK;
_fontColor = ILI9341_WHITE;
_letterSpacing = DEFAULT_LETTER_SPACING;
_lineSpacing = DEFAULT_LINE_SPACING;
#ifdef TEXT_SCALING_ENABLED
_textScale = 1;
#endif
_isFirstChar = true;
setTextArea(0, 0, _width - 1, _height - 1);
}
bool ILI9341_due::begin(void)
{
if (pinIsChipSelect(_cs)) {
pinMode(_dc, OUTPUT);
_dcport = portOutputRegister(digitalPinToPort(_dc));
_dcpinmask = digitalPinToBitMask(_dc);
#if SPI_MODE_NORMAL | SPI_MODE_DMA
pinMode(_cs, OUTPUT);
_csport = portOutputRegister(digitalPinToPort(_cs));
_cspinmask = digitalPinToBitMask(_cs);
#endif
#if SPI_MODE_NORMAL
SPI.begin();
#elif SPI_MODE_EXTENDED
SPI.begin(_cs);
#elif SPI_MODE_DMA
dmaBegin();
#endif
setSPIClockDivider(ILI9341_SPI_CLKDIVIDER);
// 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);
}
const uint8_t *addr = init_commands;
while (1) {
uint8_t count = pgm_read_byte(addr++);
if (count-- == 0) break;
writecommand_cont(pgm_read_byte(addr++));
while (count-- > 0) {
writedata8_cont(pgm_read_byte(addr++));
}
}
writecommand_last(ILI9341_SLPOUT); // Exit Sleep
delay(120);
writecommand_last(ILI9341_DISPON); // Display on
delay(120);
_isInSleep = _isIdle = false;
//#ifdef ILI_USE_SPI_TRANSACTION
//#if SPI_MODE_NORMAL | SPI_MODE_EXTENDED
endTransaction();
//#endif
//#endif
return true;
}
else {
return false;
}
}
bool ILI9341_due::pinIsChipSelect(uint8_t cs)
{
#if SPI_MODE_EXTENDED
if (cs == 4 || cs == 10 || cs == 52) // in Extended SPI mode only these pins are valid
{
return true;
}
else
{
Serial.print("Pin ");
Serial.print(_cs);
Serial.println(" is not a valid Chip Select pin for SPI Extended Mode. Valid pins are 4, 10, 52");
return false;
}
#elif SPI_MODE_NORMAL | SPI_MODE_DMA
return true;
#endif
}
void ILI9341_due::getDisplayStatus(void)
{
beginTransaction();
uint8_t x = readcommand8(ILI9341_RDMODE);
Serial.print(F("\nDisplay Power Mode: 0x")); Serial.println(x, HEX);
Serial.print(F(" Booster: ")); Serial.println(x & 0x80 ? F("On and working OK") : F("Off or has a fault"));
Serial.print(F(" Idle Mode: ")); Serial.println(x & 0x40 ? F("On") : F("Off"));
Serial.print(F(" Partial Mode: ")); Serial.println(x & 0x20 ? F("On") : F("Off"));
Serial.print(F(" Sleep Mode: ")); Serial.println(x & 0x10 ? F("Off") : F("On"));
Serial.print(F(" Display Normal Mode: ")); Serial.println(x & 0x08 ? F("On") : F("Off"));
Serial.print(F(" Display: ")); Serial.println(x & 0x04 ? F("On") : F("Off"));
x = readcommand8(ILI9341_RDMADCTL);
Serial.print(F("MADCTL Mode: 0x")); Serial.println(x, HEX);
Serial.println(x & 0x80 ? F(" Bottom to Top") : F(" Top to Bottom"));
Serial.println(x & 0x40 ? F(" Right to Left") : F(" Left to Right"));
Serial.println(x & 0x20 ? F(" Normal Mode") : F(" Reverse Mode"));
Serial.println(x & 0x10 ? F(" LCD Refresh Bottom to Top") : F(" LCD Refresh Top to Bottom"));
Serial.println(x & 0x08 ? F(" BGR") : F("RGB"));
Serial.println(x & 0x04 ? F(" LCD Refresh Right to Left") : F(" LCD Refresh Left to Right"));
x = readcommand8(ILI9341_RDPIXFMT);
Serial.print(F("Pixel Format: 0x")); Serial.println(x, HEX);
if ((x & 0x07) == 0x05)
Serial.println(F(" 16 bits/pixel"));
if ((x & 0x07) == 0x06)
Serial.println(F(" 18 bits/pixel"));
x = readcommand8(ILI9341_RDIMGFMT);
Serial.print(F("Image Format: 0x")); Serial.println(x, HEX);
if ((x & 0x07) == 0x00)
Serial.println(F(" Gamma curve 1"));
x = readcommand8(ILI9341_RDDSPSGNMODE);
Serial.print(F("Display Signal Mode: 0x")); Serial.println(x, HEX);
Serial.print(F(" Tearing effect line: ")); Serial.println(x & 0x80 ? F("On") : F("Off"));
Serial.print(F(" Tearing effect line: mode ")); Serial.println(x & 0x40 ? F("2") : F("1"));
Serial.print(F(" Horizontal sync: ")); Serial.println(x & 0x20 ? F("On") : F("Off"));
Serial.print(F(" Vertical sync: ")); Serial.println(x & 0x10 ? F("On") : F("Off"));
Serial.print(F(" Pixel clock: ")); Serial.println(x & 0x08 ? F("On") : F("Off"));
Serial.print(F(" Data enable: ")); Serial.println(x & 0x04 ? F("On") : F("Off"));
x = readcommand8(ILI9341_RDSELFDIAG);
Serial.print(F("Self Diagnostic: 0x")); Serial.println(x, HEX);
Serial.print(F(" Register Loading: ")); Serial.println(x & 0x80 ? F("working") : F("not working"));
Serial.print(F(" Functionality: ")); Serial.println(x & 0x40 ? F("working") : F("not working"));
endTransaction();
}
void ILI9341_due::setSPIClockDivider(uint8_t divider)
{
_spiClkDivider = divider;
#ifdef ILI_USE_SPI_TRANSACTION
#if defined (ARDUINO_SAM_DUE)
_spiSettings = SPISettings(F_CPU / divider, MSBFIRST, SPI_MODE0);
#elif defined (ARDUINO_ARCH_AVR)
#if divider == SPI_CLOCK_DIV2
_spiSettings = SPISettings(F_CPU / 2, MSBFIRST, SPI_MODE0);
#elif divider == SPI_CLOCK_DIV4
_spiSettings = SPISettings(F_CPU / 4, MSBFIRST, SPI_MODE0);
#elif divider == SPI_CLOCK_DIV8
_spiSettings = SPISettings(F_CPU / 8, MSBFIRST, SPI_MODE0);
#elif divider == SPI_CLOCK_DIV16
_spiSettings = SPISettings(F_CPU / 16, MSBFIRST, SPI_MODE0);
#elif divider == SPI_CLOCK_DIV32
_spiSettings = SPISettings(F_CPU / 32, MSBFIRST, SPI_MODE0);
#elif divider == SPI_CLOCK_DIV64
_spiSettings = SPISettings(F_CPU / 64, MSBFIRST, SPI_MODE0);
#elif divider == SPI_CLOCK_DIV128
_spiSettings = SPISettings(F_CPU / 128, MSBFIRST, SPI_MODE0);
#endif
#endif
#endif
#ifdef ILI_USE_SPI_TRANSACTION
beginTransaction();
#else
#if SPI_MODE_NORMAL
SPI.setClockDivider(divider);
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
#elif SPI_MODE_EXTENDED
SPI.setClockDivider(_cs, divider);
SPI.setBitOrder(_cs, MSBFIRST);
SPI.setDataMode(_cs, SPI_MODE0);
#endif
#endif
#if SPI_MODE_DMA
dmaInit(divider);
#endif
}
void ILI9341_due::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
{
beginTransaction();
enableCS();
setAddrAndRW_cont(x0, y0, x1 - x0 + 1, y1 - y0 + 1);
disableCS();
endTransaction();
}
void ILI9341_due::setAddrWindowRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
{
beginTransaction();
enableCS();
setAddrAndRW_cont(x, y, w, h);
disableCS();
endTransaction();
}
void ILI9341_due::pushColor(uint16_t color)
{
beginTransaction();
enableCS();
setDCForData();
write16_last(color);
endTransaction();
}
//void ILI9341_due::pushColors(uint16_t *colors, uint16_t offset, uint16_t len) {
// beginTransaction();
// enableCS();
// setDCForData();
// colors = colors + offset * 2;
//#if SPI_MODE_EXTENDED
// uint16_t i;
// for (i = 0; i < len-1; i++) {
// write16_cont(colors[i]);
// }
// write16_last(colors[i]);
//#else
// for (uint16_t i = 0; i < (len << 1); i += 2) {
// uint16_t color = *colors;
// _scanline[i] = highByte(color);
// _scanline[i + 1] = lowByte(color);
// colors++;
// }
// writeScanline(len);
// disableCS();
//#endif
//
// endTransaction();
//}
// pushes pixels stored in the colors array (one color is 2 bytes)
// in big endian (high byte first)
// len should be the length of the array (so to push 320 pixels,
// you have to have a 640-byte array and len should be 640)
//void ILI9341_due::pushColors565(uint8_t *colors, uint16_t offset, uint32_t len) {
// beginTransaction();
// enableCS();
// setDCForData();
// colors = colors + offset;
//
// //#if SPI_MODE_NORMAL | SPI_MODE_EXTENDED
// // for (uint16_t i = 0; i < len; i++) {
// // write8_cont(colors[i]);
// // }
// //#elif SPI_MODE_DMA
// write_cont(colors, len);
// //#endif
// disableCS();
// endTransaction();
//}
void ILI9341_due::pushColors(const uint16_t *colors, uint16_t offset, uint32_t len) {
beginTransaction();
enableCS();
pushColors_noTrans_noCS(colors, offset, len);
disableCS();
endTransaction();
}
void ILI9341_due::pushColors(uint16_t *colors, uint16_t offset, uint32_t len) {
beginTransaction();
enableCS();
setDCForData();
colors = colors + offset;
write_cont(colors, len);
disableCS();
endTransaction();
}
void ILI9341_due::pushColors_noTrans_noCS(const uint16_t *colors, uint16_t offset, uint32_t len) {
setDCForData();
colors = colors + offset;
#if SPI_MODE_DMA
const uint32_t numLoops = len / (uint32_t)SCANLINE_PIXEL_COUNT;
for (uint32_t l = 0; l < numLoops; l++)
{
for (uint32_t i = 0; i < SCANLINE_PIXEL_COUNT; i++)
{
_scanline16[i] = colors[l*SCANLINE_PIXEL_COUNT + i];
}
writeScanline16(SCANLINE_PIXEL_COUNT);
}
uint16_t remainingPixels = len % SCANLINE_PIXEL_COUNT;
if (remainingPixels > 0) {
for (uint32_t i = 0; i < remainingPixels; i++)
{
_scanline16[i] = colors[numLoops*SCANLINE_PIXEL_COUNT + i];
}
writeScanline16(remainingPixels);
}
#else
write_cont(colors, len);
#endif
}
void ILI9341_due::drawPixel(int16_t x, int16_t y, uint16_t color) {
beginTransaction();
enableCS();
drawPixel_last(x, y, color);
disableCS();
endTransaction();
}
void ILI9341_due::drawImage(const uint16_t *colors, uint16_t x, uint16_t y, uint16_t w, uint16_t h) {
const uint32_t totalPixels = (uint32_t)w*(uint32_t)h;
beginTransaction();
enableCS();
setAddrAndRW_cont(x, y, w, h);
pushColors_noTrans_noCS(colors, 0, totalPixels);
disableCS();
endTransaction();
}
void ILI9341_due::drawFastVLine(int16_t x, int16_t y, uint16_t h, uint16_t color)
{
beginTransaction();
drawFastVLine_noTrans(x, y, h, color);
endTransaction();
}
void ILI9341_due::drawFastVLine_noTrans(int16_t x, int16_t y, uint16_t h, uint16_t color)
{
// Rudimentary clipping
if ((x >= _width) || (y >= _height)) return;
if ((y + (int16_t)h - 1) >= _height) h = _height - y;
fillScanline16(color, min(h, SCANLINE_PIXEL_COUNT));
enableCS();
setAddrAndRW_cont(x, y, 1, h);
setDCForData();
#ifdef ARDUINO_SAM_DUE
writeScanline16(h);
#elif defined ARDUINO_ARCH_AVR
writeScanlineLooped(h);
#endif
disableCS();
}
void ILI9341_due::drawFastVLine_cont_noFill(int16_t x, int16_t y, int16_t h, uint16_t color)
{
// Rudimentary clipping
// if ((x >= _width) || (y >= _height)) return;
// if ((y + h - 1) >= _height) h = _height - y;
//
// setAddrAndRW_cont(x, y, 1, h);
// setDCForData();
//#if SPI_MODE_NORMAL | SPI_MODE_EXTENDED
// while (h-- > 0) {
// write16_cont(color);
// }
//#elif SPI_MODE_DMA
// writeScanline(h);
//#endif
if ((x >= _width) || (y >= _height)) return;
if ((y + h - 1) >= _height) h = _height - y;
setAddrAndRW_cont(x, y, 1, h);
setDCForData();
#ifdef ARDUINO_SAM_DUE
writeScanline16(h);
#elif defined ARDUINO_ARCH_AVR
writeScanlineLooped(h);
#endif
}
void ILI9341_due::drawFastHLine(int16_t x, int16_t y, uint16_t w, uint16_t color)
{
beginTransaction();
drawFastHLine_noTrans(x, y, w, color);
endTransaction();
}
void ILI9341_due::drawFastHLine_noTrans(int16_t x, int16_t y, uint16_t w, uint16_t color)
{
// Rudimentary clipping
if ((x >= _width) || (y >= _height)) return;
if ((x + (int16_t)w - 1) >= _width) w = _width - x;
fillScanline16(color, min(w, SCANLINE_PIXEL_COUNT));
enableCS();
setAddrAndRW_cont(x, y, w, 1);
setDCForData();
#ifdef ARDUINO_SAM_DUE
writeScanline16(w);
#elif defined ARDUINO_ARCH_AVR
writeScanlineLooped(w);
#endif
disableCS();
}
void ILI9341_due::fillScreen(uint16_t color)
{
const uint32_t numLoops = (uint32_t)76800 / (uint32_t)SCANLINE_PIXEL_COUNT;
fillScanline16(color);
beginTransaction();
enableCS();
setAddrAndRW_cont(0, 0, _width, _height);
setDCForData();
for (uint32_t l = 0; l < numLoops; l++)
{
writeScanline16(SCANLINE_PIXEL_COUNT);
}
disableCS();
endTransaction();
//#endif
}
// fill a rectangle
void ILI9341_due::fillRect(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t color)
{
beginTransaction();
fillRect_noTrans(x, y, w, h, color);
endTransaction();
}
void ILI9341_due::fillRectWithShader(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t(*fillShader)(uint16_t rx, uint16_t ry))
{
beginTransaction();
fillRectWithShader_noTrans(x, y, w, h, fillShader);
endTransaction();
}
// fill a rectangle
void ILI9341_due::fillRect_noTrans(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t color)
{
//Serial << "x:" << x << " y:" << y << " w:" << x << " h:" << h << " width:" << _width << " height:" << _height <<endl;
// rudimentary clipping (drawChar w/big text requires this)
if ((x >= _width) || (y >= _height) || (x + w - 1 < 0) || (y + h - 1 < 0)) return;
if ((x + (int16_t)w - 1) >= _width) w = _width - x;
if ((y + (int16_t)h - 1) >= _height) h = _height - y;
const uint32_t totalPixels = (uint32_t)w*(uint32_t)h;
fillScanline16(color, min(totalPixels, SCANLINE_PIXEL_COUNT));
enableCS();
setAddrAndRW_cont(x, y, w, h);
setDCForData();
writeScanlineLooped(totalPixels);
disableCS();
}
void ILI9341_due::fillRectWithShader_noTrans(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t(*fillShader)(uint16_t rx, uint16_t ry))
{
//Serial << "x:" << x << " y:" << y << " w:" << x << " h:" << h << " width:" << _width << " height:" << _height <<endl;
// rudimentary clipping (drawChar w/big text requires this)
if ((x >= _width) || (y >= _height) || (x + w - 1 < 0) || (y + h - 1 < 0)) return;
if ((x + (int16_t)w - 1) >= _width) w = _width - x;
if ((y + (int16_t)h - 1) >= _height) h = _height - y;
enableCS();
setAddrAndRW_cont(x, y, w, h);
setDCForData();
for (uint16_t ry = 0; ry < h; ry++) {
for (uint16_t rx = 0; rx < w; rx++)
{
_scanline16[rx] = fillShader(rx, ry);
}
writeScanline16(w);
}
disableCS();
}
#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_due::setRotation(iliRotation r)
{
beginTransaction();
writecommand_cont(ILI9341_MADCTL);
_rotation = r;
switch (r) {
case iliRotation0:
writedata8_last(MADCTL_MX | MADCTL_BGR);
_width = ILI9341_TFTWIDTH;
_height = ILI9341_TFTHEIGHT;
break;
case iliRotation90:
writedata8_last(MADCTL_MV | MADCTL_BGR);
_width = ILI9341_TFTHEIGHT;
_height = ILI9341_TFTWIDTH;
break;
case iliRotation180:
writedata8_last(MADCTL_MY | MADCTL_BGR);
_width = ILI9341_TFTWIDTH;
_height = ILI9341_TFTHEIGHT;
break;
case iliRotation270:
writedata8_last(MADCTL_MX | MADCTL_MY | MADCTL_MV | MADCTL_BGR);
_width = ILI9341_TFTHEIGHT;
_height = ILI9341_TFTWIDTH;
break;
}
//_area.x = 0;
//_area.y = 0;
_area.w = _width;
_area.h = _height;
endTransaction();
}
void ILI9341_due::invertDisplay(boolean i)
{
beginTransaction();
writecommand_last(i ? ILI9341_INVON : ILI9341_INVOFF);
endTransaction();
}
// Reads one pixel/color from the TFT's GRAM
uint16_t ILI9341_due::readPixel(int16_t x, int16_t y)
{
beginTransaction();
//setAddr_cont(x, y, x + 1, y + 1); ? should it not be x,y,x,y?
setAddr_cont(x, y, 1, 1);
writecommand_cont(ILI9341_RAMRD); // read from RAM
readdata8_cont(); // dummy read
uint8_t red = read8_cont();
uint8_t green = read8_cont();
uint8_t blue = read8_last();
uint16_t color = color565(red, green, blue);
endTransaction();
return color;
}
//void ILI9341_due::drawArc(uint16_t cx, uint16_t cy, uint16_t radius, uint16_t thickness, uint16_t start, uint16_t end, uint16_t color) {
// //void graphics_draw_arc(GContext *ctx, GPoint p, int radius, int thickness, int start, int end) {
// start = start % 360;
// end = end % 360;
//
// while (start < 0) start += 360;
// while (end < 0) end += 360;
//
// if (end == 0) end = 360;
//
// //Serial << "start: " << start << " end:" << end << endl;
//
// // Serial << (float)cos_lookup(start * ARC_MAX_STEPS / 360) << " x " << (float)sin_lookup(start * ARC_MAX_STEPS / 360) << endl;
//
// float sslope = (float)cos_lookup(start * ARC_MAX_STEPS / 360) / (float)sin_lookup(start * ARC_MAX_STEPS / 360);
// float eslope = (float)cos_lookup(end * ARC_MAX_STEPS / 360) / (float)sin_lookup(end * ARC_MAX_STEPS / 360);
//
// //Serial << "sslope: " << sslope << " eslope:" << eslope << endl;
//
// if (end == 360) eslope = -1000000;
//
// int ir2 = (radius - thickness) * (radius - thickness);
// int or2 = radius * radius;
//
// for (int x = -radius; x <= radius; x++)
// for (int y = -radius; y <= radius; y++)
// {
// int x2 = x * x;
// int y2 = y * y;
//
// if (
// (x2 + y2 < or2 && x2 + y2 >= ir2) &&
// (
// (y > 0 && start < 180 && x <= y * sslope) ||
// (y < 0 && start > 180 && x >= y * sslope) ||
// (y < 0 && start <= 180) ||
// (y == 0 && start <= 180 && x < 0) ||
// (y == 0 && start == 0 && x > 0)
// ) &&
// (
// (y > 0 && end < 180 && x >= y * eslope) ||
// (y < 0 && end > 180 && x <= y * eslope) ||
// (y > 0 && end >= 180) ||
// (y == 0 && end >= 180 && x < 0) ||
// (y == 0 && start == 0 && x > 0)
// )
// )
// drawPixel_cont(cx+x, cy+y, color);
// }
//}
// DrawArc function thanks to Jnmattern and his Arc_2.0 (https://github.com/Jnmattern)
void ILI9341_due::fillArcOffsetted(uint16_t cx, uint16_t cy, uint16_t radius, uint16_t thickness, float start, float end, uint16_t color) {
int16_t xmin = 65535, xmax = -32767, ymin = 32767, ymax = -32767;
float cosStart, sinStart, cosEnd, sinEnd;
float r, t;
float startAngle, endAngle;
//Serial << "start: " << start << " end: " << end << endl;
startAngle = (start / _arcAngleMax) * 360; // 252
endAngle = (end / _arcAngleMax) * 360; // 807
//Serial << "startAngle: " << startAngle << " endAngle: " << endAngle << endl;
while (startAngle < 0) startAngle += 360;
while (endAngle < 0) endAngle += 360;
while (startAngle > 360) startAngle -= 360;
while (endAngle > 360) endAngle -= 360;
//Serial << "startAngleAdj: " << startAngle << " endAngleAdj: " << endAngle << endl;
//if (endAngle == 0) endAngle = 360;
if (startAngle > endAngle) {
fillArcOffsetted(cx, cy, radius, thickness, ((startAngle) / (float)360) * _arcAngleMax, _arcAngleMax, color);
fillArcOffsetted(cx, cy, radius, thickness, 0, ((endAngle) / (float)360) * _arcAngleMax, color);
}
else {
// Calculate bounding box for the arc to be drawn
cosStart = cosDegrees(startAngle);
sinStart = sinDegrees(startAngle);
cosEnd = cosDegrees(endAngle);
sinEnd = sinDegrees(endAngle);
//Serial << cosStart << " " << sinStart << " " << cosEnd << " " << sinEnd << endl;
r = radius;
// Point 1: radius & startAngle
t = r * cosStart;
if (t < xmin) xmin = t;
if (t > xmax) xmax = t;
t = r * sinStart;
if (t < ymin) ymin = t;
if (t > ymax) ymax = t;
// Point 2: radius & endAngle
t = r * cosEnd;
if (t < xmin) xmin = t;
if (t > xmax) xmax = t;
t = r * sinEnd;
if (t < ymin) ymin = t;
if (t > ymax) ymax = t;
r = radius - thickness;
// Point 3: radius-thickness & startAngle
t = r * cosStart;
if (t < xmin) xmin = t;
if (t > xmax) xmax = t;
t = r * sinStart;
if (t < ymin) ymin = t;
if (t > ymax) ymax = t;
// Point 4: radius-thickness & endAngle
t = r * cosEnd;
if (t < xmin) xmin = t;
if (t > xmax) xmax = t;
t = r * sinEnd;
if (t < ymin) ymin = t;
if (t > ymax) ymax = t;
//Serial << xmin << " " << xmax << " " << ymin << " " << ymax << endl;
// Corrections if arc crosses X or Y axis
if ((startAngle < 90) && (endAngle > 90)) {
ymax = radius;
}
if ((startAngle < 180) && (endAngle > 180)) {
xmin = -radius;
}
if ((startAngle < 270) && (endAngle > 270)) {
ymin = -radius;
}
// Slopes for the two sides of the arc
float sslope = (float)cosStart / (float)sinStart;
float eslope = (float)cosEnd / (float)sinEnd;
//Serial << "sslope2: " << sslope << " eslope2:" << eslope << endl;
if (endAngle == 360) eslope = -1000000;
int ir2 = (radius - thickness) * (radius - thickness);
int or2 = radius * radius;
//Serial << "ymin: " << ymin << " ymax: " << ymax << endl;
fillScanline16(color);
enableCS();
for (int x = xmin; x <= xmax; x++) {
bool y1StartFound = false, y2StartFound = false;
bool y1EndFound = false, y2EndSearching = false;
int y1s = 0, y1e = 0, y2s = 0;
for (int y = ymin; y <= ymax; y++)
{
int x2 = x * x;
int y2 = y * y;
if (
(x2 + y2 < or2 && x2 + y2 >= ir2) && (
(y > 0 && startAngle < 180 && x <= y * sslope) ||
(y < 0 && startAngle > 180 && x >= y * sslope) ||
(y < 0 && startAngle <= 180) ||
(y == 0 && startAngle <= 180 && x < 0) ||
(y == 0 && startAngle == 0 && x > 0)
) && (
(y > 0 && endAngle < 180 && x >= y * eslope) ||
(y < 0 && endAngle > 180 && x <= y * eslope) ||
(y > 0 && endAngle >= 180) ||
(y == 0 && endAngle >= 180 && x < 0) ||
(y == 0 && startAngle == 0 && x > 0)))
{
if (!y1StartFound) //start of the higher line found
{
y1StartFound = true;
y1s = y;
}
else if (y1EndFound && !y2StartFound) //start of the lower line found
{
//Serial << "Found y2 start x: " << x << " y:" << y << endl;
y2StartFound = true;
//drawPixel_cont(cx+x, cy+y, ILI9341_BLUE);
y2s = y;
y += y1e - y1s - 1; // calculate the most probable end of the lower line (in most cases the length of lower line is equal to length of upper line), in the next loop we will validate if the end of line is really there
if (y > ymax - 1) // the most probable end of line 2 is beyond ymax so line 2 must be shorter, thus continue with pixel by pixel search
{
y = y2s; // reset y and continue with pixel by pixel search
y2EndSearching = true;
}
//Serial << "Upper line length: " << (y1e - y1s) << " Setting y to " << y << endl;
}
else if (y2StartFound && !y2EndSearching)
{
// we validated that the probable end of the lower line has a pixel, continue with pixel by pixel search, in most cases next loop with confirm the end of lower line as it will not find a valid pixel
y2EndSearching = true;
}
//Serial << "x:" << x << " y:" << y << endl;
//drawPixel_cont(cx+x, cy+y, ILI9341_BLUE);
}
else
{
if (y1StartFound && !y1EndFound) //higher line end found
{
y1EndFound = true;
y1e = y - 1;
//Serial << "line: " << y1s << " - " << y1e << endl;
drawFastVLine_cont_noFill(cx + x, cy + y1s, y - y1s, color);
if (y < 0)
{
//Serial << x << " " << y << endl;
y = abs(y); // skip the empty middle
}
else
break;
}
else if (y2StartFound)
{
if (y2EndSearching)
{
//Serial << "Found final end at y: " << y << endl;
// we found the end of the lower line after pixel by pixel search
drawFastVLine_cont_noFill(cx + x, cy + y2s, y - y2s, color);
y2EndSearching = false;
break;
}
else
{
//Serial << "Expected end not found" << endl;
// the expected end of the lower line is not there so the lower line must be shorter
y = y2s; // put the y back to the lower line start and go pixel by pixel to find the end
y2EndSearching = true;
}
}
//else
//drawPixel_cont(cx+x, cy+y, ILI9341_RED);
}
//
//delay(75);
}
if (y1StartFound && !y1EndFound)
{
y1e = ymax;
//Serial << "line: " << y1s << " - " << y1e << endl;
drawFastVLine_cont_noFill(cx + x, cy + y1s, y1e - y1s + 1, color);
}
else if (y2StartFound && y2EndSearching) // we found start of lower line but we are still searching for the end
{ // which we haven't found in the loop so the last pixel in a column must be the end
drawFastVLine_cont_noFill(cx + x, cy + y2s, ymax - y2s + 1, color);
}
}
disableCS();
}
}
void ILI9341_due::screenshotToConsole()
{
uint8_t lastColor[3];
uint8_t color[3];
uint32_t sameColorPixelCount = 0;
uint16_t sameColorPixelCount16 = 0;
uint32_t sameColorStartIndex = 0;
uint32_t totalImageDataLength = 0;
Serial.println();
Serial.println(F("==== PIXEL DATA START ===="));
//uint16_t x=0;
//uint16_t y=0;
beginTransaction();
setAddr_cont(0, 0, _width, _height);
writecommand_cont(ILI9341_RAMRD); // read from RAM
readdata8_cont(); // dummy read, also sets DC high
#if SPI_MODE_DMA
read_cont(color, 3);
lastColor[0] = color[0];
lastColor[1] = color[1];
lastColor[2] = color[2];
#elif SPI_MODE_NORMAL | SPI_MODE_EXTENDED
lastColor[0] = color[0] = read8_cont();
lastColor[1] = color[1] = read8_cont();
lastColor[2] = color[2] = read8_cont();
#endif
printHex8(color, 3); //write color of the first pixel
totalImageDataLength += 6;
sameColorStartIndex = 0;
for (uint32_t i = 1; i < (uint32_t)_width*(uint32_t)_height; i++)
{
#if SPI_MODE_DMA
read_cont(color, 3);
#elif SPI_MODE_NORMAL | SPI_MODE_EXTENDED
color[0] = read8_cont();
color[1] = read8_cont();
color[2] = read8_cont();
#endif
if (color[0] != lastColor[0] ||
color[1] != lastColor[1] ||
color[2] != lastColor[2])
{
sameColorPixelCount = i - sameColorStartIndex;
if (sameColorPixelCount > 65535)
{
sameColorPixelCount16 = 65535;
printHex16(&sameColorPixelCount16, 1);
printHex8(lastColor, 3);
totalImageDataLength += 10;
sameColorPixelCount16 = sameColorPixelCount - 65535;
}
else
sameColorPixelCount16 = sameColorPixelCount;
printHex16(&sameColorPixelCount16, 1);
printHex8(color, 3);
totalImageDataLength += 10;
sameColorStartIndex = i;
lastColor[0] = color[0];
lastColor[1] = color[1];
lastColor[2] = color[2];
}
}
disableCS();
endTransaction();
sameColorPixelCount = (uint32_t)_width*(uint32_t)_height - sameColorStartIndex;
if (sameColorPixelCount > 65535)
{
sameColorPixelCount16 = 65535;
printHex16(&sameColorPixelCount16, 1);
printHex8(lastColor, 3);
totalImageDataLength += 10;
sameColorPixelCount16 = sameColorPixelCount - 65535;
}
else
sameColorPixelCount16 = sameColorPixelCount;
printHex16(&sameColorPixelCount16, 1);
totalImageDataLength += 4;
printHex32(&totalImageDataLength, 1);
Serial.println();
Serial.println(F("==== PIXEL DATA END ===="));
Serial.print(F("Total Image Data Length: "));
Serial.println(totalImageDataLength);
}
/*
This is the core graphics library for all our displays, providing a common
set of graphics primitives (points, lines, circles, etc.). It needs to bex
paired with a hardware-specific library for each display device we carry
(to handle the lower-level functions).