forked from Bodmer/TFT_eSPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TFT_eSPI.cpp
4972 lines (4053 loc) · 133 KB
/
TFT_eSPI.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
/***************************************************
Arduino TFT graphics library targeted at ESP8266
and ESP32 based boards.
This is a standalone library that contains the
hardware driver, the graphics functions and the
proportional fonts.
The larger fonts are Run Length Encoded to reduce their
size.
Created by Bodmer 2/12/16
Bodmer: Added RPi 16 bit display support
****************************************************/
#include "TFT_eSPI.h"
#include <pgmspace.h>
#ifndef ESP32_PARALLEL
#include <SPI.h>
#endif
// SUPPORT_TRANSACTIONS is mandatory for ESP32 so the hal mutex is toggled
#if defined (ESP32) && !defined (SUPPORT_TRANSACTIONS)
#define SUPPORT_TRANSACTIONS
#endif
// If it is a 16bit serial display we must transfer 16 bits every time
#ifdef RPI_ILI9486_DRIVER
#define CMD_BITS 16-1
#else
#define CMD_BITS 8-1
#endif
// Fast block write prototype
void writeBlock(uint16_t color, uint32_t repeat);
// Byte read prototype
uint8_t readByte(void);
// GPIO parallel input/output control
void busDir(uint32_t mask, uint8_t mode);
// If the SPI library has transaction support, these functions
// establish settings and protect from interference from other
// libraries. Otherwise, they simply do nothing.
inline void TFT_eSPI::spi_begin(void){
#if defined (SPI_HAS_TRANSACTION) && defined (SUPPORT_TRANSACTIONS) && !defined(ESP32_PARALLEL)
if (locked) {locked = false; SPI.beginTransaction(SPISettings(SPI_FREQUENCY, MSBFIRST, TFT_SPI_MODE));}
#endif
}
inline void TFT_eSPI::spi_end(void){
#if defined (SPI_HAS_TRANSACTION) && defined (SUPPORT_TRANSACTIONS) && !defined(ESP32_PARALLEL)
if(!inTransaction) {if (!locked) {locked = true; SPI.endTransaction();}}
#endif
}
inline void TFT_eSPI::spi_begin_read(void){
#if defined (SPI_HAS_TRANSACTION) && defined (SUPPORT_TRANSACTIONS) && !defined(ESP32_PARALLEL)
if (locked) {locked = false; SPI.beginTransaction(SPISettings(SPI_READ_FREQUENCY, MSBFIRST, TFT_SPI_MODE));}
#else
#if !defined(ESP32_PARALLEL)
SPI.setFrequency(SPI_READ_FREQUENCY);
#endif
#endif
}
inline void TFT_eSPI::spi_end_read(void){
#if defined (SPI_HAS_TRANSACTION) && defined (SUPPORT_TRANSACTIONS) && !defined(ESP32_PARALLEL)
if(!inTransaction) {if (!locked) {locked = true; SPI.endTransaction();}}
#else
#if !defined(ESP32_PARALLEL)
SPI.setFrequency(SPI_FREQUENCY);
#endif
#endif
}
#if defined (TOUCH_CS) && defined (SPI_TOUCH_FREQUENCY) // && !defined(ESP32_PARALLEL)
inline void TFT_eSPI::spi_begin_touch(void){
#if defined (SPI_HAS_TRANSACTION) && defined (SUPPORT_TRANSACTIONS)
if (locked) {locked = false; SPI.beginTransaction(SPISettings(SPI_TOUCH_FREQUENCY, MSBFIRST, SPI_MODE0));}
#else
SPI.setFrequency(SPI_TOUCH_FREQUENCY);
#endif
}
inline void TFT_eSPI::spi_end_touch(void){
#if defined (SPI_HAS_TRANSACTION) && defined (SUPPORT_TRANSACTIONS)
if(!inTransaction) {if (!locked) {locked = true; SPI.endTransaction();}}
#else
SPI.setFrequency(SPI_FREQUENCY);
#endif
}
#endif
/***************************************************************************************
** Function name: TFT_eSPI
** Description: Constructor , we must use hardware SPI pins
***************************************************************************************/
TFT_eSPI::TFT_eSPI(int16_t w, int16_t h)
{
// The control pins are deliberately set to the inactive state (CS high) as setup()
// might call and initialise other SPI peripherals which would could cause conflicts
// if CS is floating or undefined.
#ifdef TFT_CS
digitalWrite(TFT_CS, HIGH); // Chip select high (inactive)
pinMode(TFT_CS, OUTPUT);
#endif
// Configure chip select for touchscreen controller if present
#ifdef TOUCH_CS
digitalWrite(TOUCH_CS, HIGH); // Chip select high (inactive)
pinMode(TOUCH_CS, OUTPUT);
#endif
#ifdef TFT_WR
digitalWrite(TFT_WR, HIGH); // Set write strobe high (inactive)
pinMode(TFT_WR, OUTPUT);
#endif
#ifdef TFT_DC
digitalWrite(TFT_DC, HIGH); // Data/Command high = data mode
pinMode(TFT_DC, OUTPUT);
#endif
#ifdef TFT_RST
if (TFT_RST >= 0) {
digitalWrite(TFT_RST, HIGH); // Set high, do not share pin with another SPI device
pinMode(TFT_RST, OUTPUT);
}
#endif
#ifdef ESP32_PARALLEL
// Create a bit set lookup table for data bus - wastes 1kbyte of RAM but speeds things up dramatically
for (int c = 0; c<256; c++)
{
xset_mask[c] = 0;
if ( c & 0x01 ) xset_mask[c] |= (1 << TFT_D0);
if ( c & 0x02 ) xset_mask[c] |= (1 << TFT_D1);
if ( c & 0x04 ) xset_mask[c] |= (1 << TFT_D2);
if ( c & 0x08 ) xset_mask[c] |= (1 << TFT_D3);
if ( c & 0x10 ) xset_mask[c] |= (1 << TFT_D4);
if ( c & 0x20 ) xset_mask[c] |= (1 << TFT_D5);
if ( c & 0x40 ) xset_mask[c] |= (1 << TFT_D6);
if ( c & 0x80 ) xset_mask[c] |= (1 << TFT_D7);
}
// Make sure read is high before we set the bus to output
digitalWrite(TFT_RD, HIGH);
pinMode(TFT_RD, OUTPUT);
GPIO.out_w1ts = set_mask(255); // Set data bus to 0xFF
// Set TFT data bus lines to output
busDir(dir_mask, OUTPUT);
#endif
_init_width = _width = w; // Set by specific xxxxx_Defines.h file or by users sketch
_init_height = _height = h; // Set by specific xxxxx_Defines.h file or by users sketch
rotation = 0;
cursor_y = cursor_x = 0;
textfont = 1;
textsize = 1;
textcolor = bitmap_fg = 0xFFFF; // White
textbgcolor = bitmap_bg = 0x0000; // Black
padX = 0; // No padding
textwrapX = true; // Wrap text at end of line when using print stream
textwrapY = false; // Wrap text at bottom of screen when using print stream
textdatum = TL_DATUM; // Top Left text alignment is default
fontsloaded = 0;
_swapBytes = false; // Do not swap colour bytes by default
locked = true; // ESP32 transaction mutex lock flags
inTransaction = false;
_booted = true;
addr_row = 0xFFFF;
addr_col = 0xFFFF;
#ifdef LOAD_GLCD
fontsloaded = 0x0002; // Bit 1 set
#endif
#ifdef LOAD_FONT2
fontsloaded |= 0x0004; // Bit 2 set
#endif
#ifdef LOAD_FONT4
fontsloaded |= 0x0010; // Bit 4 set
#endif
#ifdef LOAD_FONT6
fontsloaded |= 0x0040; // Bit 6 set
#endif
#ifdef LOAD_FONT7
fontsloaded |= 0x0080; // Bit 7 set
#endif
#ifdef LOAD_FONT8
fontsloaded |= 0x0100; // Bit 8 set
#endif
#ifdef LOAD_FONT8N
fontsloaded |= 0x0200; // Bit 9 set
#endif
#ifdef SMOOTH_FONT
fontsloaded |= 0x8000; // Bit 15 set
#endif
}
/***************************************************************************************
** Function name: begin
** Description: Included for backwards compatibility
***************************************************************************************/
void TFT_eSPI::begin(uint8_t tc)
{
init(tc);
}
/***************************************************************************************
** Function name: init (tc is tab colour for ST7735 displays only)
** Description: Reset, then initialise the TFT display registers
***************************************************************************************/
void TFT_eSPI::init(uint8_t tc)
{
if (_booted)
{
#if !defined (ESP32)
#ifdef TFT_CS
cspinmask = (uint32_t) digitalPinToBitMask(TFT_CS);
#endif
#ifdef TFT_DC
dcpinmask = (uint32_t) digitalPinToBitMask(TFT_DC);
#endif
#ifdef TFT_WR
wrpinmask = (uint32_t) digitalPinToBitMask(TFT_WR);
#endif
#ifdef TFT_SPI_OVERLAP
// Overlap mode SD0=MISO, SD1=MOSI, CLK=SCLK must use D3 as CS
// pins(int8_t sck, int8_t miso, int8_t mosi, int8_t ss);
//SPI.pins( 6, 7, 8, 0);
SPI.pins(6, 7, 8, 0);
#endif
SPI.begin(); // This will set HMISO to input
#else
#if !defined(ESP32_PARALLEL)
#if defined (TFT_MOSI) && !defined (TFT_SPI_OVERLAP)
SPI.begin(TFT_SCLK, TFT_MISO, TFT_MOSI, -1);
#else
SPI.begin();
#endif
#endif
#endif
inTransaction = false;
locked = true;
// SUPPORT_TRANSACTIONS is mandatory for ESP32 so the hal mutex is toggled
// so the code here is for ESP8266 only
#if !defined (SUPPORT_TRANSACTIONS) && defined (ESP8266)
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(TFT_SPI_MODE);
SPI.setFrequency(SPI_FREQUENCY);
#endif
#if defined(ESP32_PARALLEL)
digitalWrite(TFT_CS, LOW); // Chip select low permanently
pinMode(TFT_CS, OUTPUT);
#else
#ifdef TFT_CS
// Set to output once again in case D6 (MISO) is used for CS
digitalWrite(TFT_CS, HIGH); // Chip select high (inactive)
pinMode(TFT_CS, OUTPUT);
#else
SPI.setHwCs(1); // Use hardware SS toggling
#endif
#endif
// Set to output once again in case D6 (MISO) is used for DC
#ifdef TFT_DC
digitalWrite(TFT_DC, HIGH); // Data/Command high = data mode
pinMode(TFT_DC, OUTPUT);
#endif
_booted = false;
} // end of: if just _booted
// Toggle RST low to reset
spi_begin();
#ifdef TFT_RST
if (TFT_RST >= 0) {
digitalWrite(TFT_RST, HIGH);
delay(5);
digitalWrite(TFT_RST, LOW);
delay(20);
digitalWrite(TFT_RST, HIGH);
}
else writecommand(TFT_SWRST); // Software reset
#else
writecommand(TFT_SWRST); // Software reset
#endif
spi_end();
delay(150); // Wait for reset to complete
spi_begin();
// This loads the driver specific initialisation code <<<<<<<<<<<<<<<<<<<<< ADD NEW DRIVERS TO THE LIST HERE <<<<<<<<<<<<<<<<<<<<<<<
#if defined (ILI9341_DRIVER)
#include "TFT_Drivers/ILI9341_Init.h"
#elif defined (ST7735_DRIVER)
tabcolor = tc;
#include "TFT_Drivers/ST7735_Init.h"
#elif defined (ILI9163_DRIVER)
#include "TFT_Drivers/ILI9163_Init.h"
#elif defined (S6D02A1_DRIVER)
#include "TFT_Drivers/S6D02A1_Init.h"
#elif defined (RPI_ILI9486_DRIVER)
#include "TFT_Drivers/ILI9486_Init.h"
#elif defined (ILI9486_DRIVER)
#include "TFT_Drivers/ILI9486_Init.h"
#elif defined (ILI9481_DRIVER)
#include "TFT_Drivers/ILI9481_Init.h"
#elif defined (ILI9488_DRIVER)
#include "TFT_Drivers/ILI9488_Init.h"
#elif defined (HX8357D_DRIVER)
#include "TFT_Drivers/HX8357D_Init.h"
#elif defined (ST7789_DRIVER)
#include "TFT_Drivers/ST7789_Init.h"
#endif
spi_end();
setRotation(rotation);
}
/***************************************************************************************
** Function name: setRotation
** Description: rotate the screen orientation m = 0-3 or 4-7 for BMP drawing
***************************************************************************************/
void TFT_eSPI::setRotation(uint8_t m)
{
spi_begin();
// This loads the driver specific rotation code <<<<<<<<<<<<<<<<<<<<< ADD NEW DRIVERS TO THE LIST HERE <<<<<<<<<<<<<<<<<<<<<<<
#if defined (ILI9341_DRIVER)
#include "TFT_Drivers/ILI9341_Rotation.h"
#elif defined (ST7735_DRIVER)
#include "TFT_Drivers/ST7735_Rotation.h"
#elif defined (ILI9163_DRIVER)
#include "TFT_Drivers/ILI9163_Rotation.h"
#elif defined (S6D02A1_DRIVER)
#include "TFT_Drivers/S6D02A1_Rotation.h"
#elif defined (RPI_ILI9486_DRIVER)
#include "TFT_Drivers/ILI9486_Rotation.h"
#elif defined (ILI9486_DRIVER)
#include "TFT_Drivers/ILI9486_Rotation.h"
#elif defined (ILI9481_DRIVER)
#include "TFT_Drivers/ILI9481_Rotation.h"
#elif defined (ILI9488_DRIVER)
#include "TFT_Drivers/ILI9488_Rotation.h"
#elif defined (HX8357D_DRIVER)
#include "TFT_Drivers/HX8357D_Rotation.h"
#elif defined (ST7789_DRIVER)
#include "TFT_Drivers/ST7789_Rotation.h"
#endif
delayMicroseconds(10);
spi_end();
addr_row = 0xFFFF;
addr_col = 0xFFFF;
}
/***************************************************************************************
** Function name: commandList, used for FLASH based lists only (e.g. ST7735)
** Description: Get initialisation commands from FLASH and send to TFT
***************************************************************************************/
void TFT_eSPI::commandList (const uint8_t *addr)
{
uint8_t numCommands;
uint8_t numArgs;
uint8_t ms;
spi_begin();
numCommands = pgm_read_byte(addr++); // Number of commands to follow
while (numCommands--) // For each command...
{
writecommand(pgm_read_byte(addr++)); // Read, issue command
numArgs = pgm_read_byte(addr++); // Number of args to follow
ms = numArgs & TFT_INIT_DELAY; // If hibit set, delay follows args
numArgs &= ~TFT_INIT_DELAY; // Mask out delay bit
while (numArgs--) // For each argument...
{
writedata(pgm_read_byte(addr++)); // Read, issue argument
}
if (ms)
{
ms = pgm_read_byte(addr++); // Read post-command delay time (ms)
delay( (ms==255 ? 500 : ms) );
}
}
spi_end();
}
/***************************************************************************************
** Function name: spiwrite
** Description: Write 8 bits to SPI port (legacy support only)
***************************************************************************************/
void TFT_eSPI::spiwrite(uint8_t c)
{
tft_Write_8(c);
}
/***************************************************************************************
** Function name: writecommand
** Description: Send an 8 bit command to the TFT
***************************************************************************************/
void TFT_eSPI::writecommand(uint8_t c)
{
DC_C;
CS_L;
tft_Write_8(c);
CS_H;
DC_D;
}
/***************************************************************************************
** Function name: writedata
** Description: Send a 8 bit data value to the TFT
***************************************************************************************/
void TFT_eSPI::writedata(uint8_t d)
{
CS_L;
tft_Write_8(d);
CS_H;
}
/***************************************************************************************
** Function name: readcommand8
** Description: Read a 8 bit data value from an indexed command register
***************************************************************************************/
uint8_t TFT_eSPI::readcommand8(uint8_t cmd_function, uint8_t index)
{
uint8_t reg = 0;
#ifdef ESP32_PARALLEL
writecommand(cmd_function); // Sets DC and CS high
busDir(dir_mask, INPUT);
CS_L;
// Read nth parameter (assumes caller discards 1st parameter or points index to 2nd)
while(index--) reg = readByte();
busDir(dir_mask, OUTPUT);
CS_H;
#else
// for ILI9341 Interface II i.e. IM [3:0] = "1101"
spi_begin_read();
index = 0x10 + (index & 0x0F);
DC_C;
CS_L;
tft_Write_8(0xD9);
DC_D;
tft_Write_8(index);
CS_H;
DC_C;
CS_L;
tft_Write_8(cmd_function);
DC_D;
reg = tft_Write_8(0);
CS_H;
spi_end_read();
#endif
return reg;
}
/***************************************************************************************
** Function name: readcommand16
** Description: Read a 16 bit data value from an indexed command register
***************************************************************************************/
uint16_t TFT_eSPI::readcommand16(uint8_t cmd_function, uint8_t index)
{
uint32_t reg;
reg = (readcommand8(cmd_function, index + 0) << 8);
reg |= (readcommand8(cmd_function, index + 1) << 0);
return reg;
}
/***************************************************************************************
** Function name: readcommand32
** Description: Read a 32 bit data value from an indexed command register
***************************************************************************************/
uint32_t TFT_eSPI::readcommand32(uint8_t cmd_function, uint8_t index)
{
uint32_t reg;
reg = (readcommand8(cmd_function, index + 0) << 24);
reg |= (readcommand8(cmd_function, index + 1) << 16);
reg |= (readcommand8(cmd_function, index + 2) << 8);
reg |= (readcommand8(cmd_function, index + 3) << 0);
return reg;
}
/***************************************************************************************
** Function name: read pixel (for SPI Interface II i.e. IM [3:0] = "1101")
** Description: Read 565 pixel colours from a pixel
***************************************************************************************/
uint16_t TFT_eSPI::readPixel(int32_t x0, int32_t y0)
{
#if defined(ESP32_PARALLEL)
readAddrWindow(x0, y0, x0, y0); // Sets CS low
// Set masked pins D0- D7 to input
busDir(dir_mask, INPUT);
// Dummy read to throw away don't care value
readByte();
// Fetch the 16 bit BRG pixel
//uint16_t rgb = (readByte() << 8) | readByte();
#if defined (ILI9341_DRIVER) | defined (ILI9488_DRIVER) // Read 3 bytes
// Read window pixel 24 bit RGB values and fill in LS bits
uint16_t rgb = ((readByte() & 0xF8) << 8) | ((readByte() & 0xFC) << 3) | (readByte() >> 3);
CS_H;
// Set masked pins D0- D7 to output
busDir(dir_mask, OUTPUT);
return rgb;
#else // ILI9481 16 bit read
// Fetch the 16 bit BRG pixel
uint16_t bgr = (readByte() << 8) | readByte();
CS_H;
// Set masked pins D0- D7 to output
busDir(dir_mask, OUTPUT);
// Swap Red and Blue (could check MADCTL setting to see if this is needed)
return (bgr>>11) | (bgr<<11) | (bgr & 0x7E0);
#endif
#else // Not ESP32_PARALLEL
spi_begin_read();
readAddrWindow(x0, y0, x0, y0); // Sets CS low
// Dummy read to throw away don't care value
tft_Write_8(0);
// Read the 3 RGB bytes, colour is actually only in the top 6 bits of each byte
// as the TFT stores colours as 18 bits
#if !defined (ILI9488_DRIVER)
uint8_t r = tft_Write_8(0);
uint8_t g = tft_Write_8(0);
uint8_t b = tft_Write_8(0);
#else
// The 6 colour bits are in MS 6 bits of each byte, but the ILI9488 needs an extra clock pulse
// so bits appear shifted right 1 bit, so mask the middle 6 bits then shift 1 place left
uint8_t r = (tft_Write_8(0)&0x7E)<<1;
uint8_t g = (tft_Write_8(0)&0x7E)<<1;
uint8_t b = (tft_Write_8(0)&0x7E)<<1;
#endif
CS_H;
spi_end_read();
return color565(r, g, b);
#endif
}
/***************************************************************************************
** Function name: read byte - supports class functions
** Description: Read a byte from ESP32 8 bit data port
***************************************************************************************/
// Bus MUST be set to input before calling this function!
uint8_t readByte(void)
{
uint8_t b = 0;
#ifdef ESP32_PARALLEL
RD_L;
uint32_t reg; // Read all GPIO pins 0-31
reg = gpio_input_get(); // Read three times to allow for bus access time
reg = gpio_input_get();
reg = gpio_input_get(); // Data should be stable now
RD_H;
// Check GPIO bits used and build value
b = (((reg>>TFT_D0)&1) << 0);
b |= (((reg>>TFT_D1)&1) << 1);
b |= (((reg>>TFT_D2)&1) << 2);
b |= (((reg>>TFT_D3)&1) << 3);
b |= (((reg>>TFT_D4)&1) << 4);
b |= (((reg>>TFT_D5)&1) << 5);
b |= (((reg>>TFT_D6)&1) << 6);
b |= (((reg>>TFT_D7)&1) << 7);
#endif
return b;
}
/***************************************************************************************
** Function name: masked GPIO direction control - supports class functions
** Description: Set masked ESP32 GPIO pins to input or output
***************************************************************************************/
void busDir(uint32_t mask, uint8_t mode)
{
#ifdef ESP32_PARALLEL
// Supports GPIO 0 - 31 on ESP32 only
gpio_config_t gpio;
gpio.pin_bit_mask = mask;
gpio.mode = GPIO_MODE_INPUT;
gpio.pull_up_en = GPIO_PULLUP_ENABLE;
gpio.pull_down_en = GPIO_PULLDOWN_DISABLE;
gpio.intr_type = GPIO_INTR_DISABLE;
if (mode == OUTPUT) gpio.mode = GPIO_MODE_OUTPUT;
gpio_config(&gpio);
#endif
}
/***************************************************************************************
** Function name: read rectangle (for SPI Interface II i.e. IM [3:0] = "1101")
** Description: Read 565 pixel colours from a defined area
***************************************************************************************/
void TFT_eSPI::readRect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint16_t *data)
{
if ((x > _width) || (y > _height) || (w == 0) || (h == 0)) return;
#if defined(ESP32_PARALLEL)
readAddrWindow(x, y, x + w - 1, y + h - 1); // Sets CS low
// Set masked pins D0- D7 to input
busDir(dir_mask, INPUT);
// Dummy read to throw away don't care value
readByte();
// Total pixel count
uint32_t len = w * h;
#if defined (ILI9341_DRIVER) | defined (ILI9488_DRIVER) // Read 3 bytes
// Fetch the 24 bit RGB value
while (len--) {
// Assemble the RGB 16 bit colour
uint16_t rgb = ((readByte() & 0xF8) << 8) | ((readByte() & 0xFC) << 3) | (readByte() >> 3);
// Swapped byte order for compatibility with pushRect()
*data++ = (rgb<<8) | (rgb>>8);
}
#else // ILI9481 reads as 16 bits
// Fetch the 16 bit BRG pixels
while (len--) {
// Read the BRG 16 bit colour
uint16_t bgr = (readByte() << 8) | readByte();
// Swap Red and Blue (could check MADCTL setting to see if this is needed)
uint16_t rgb = (bgr>>11) | (bgr<<11) | (bgr & 0x7E0);
// Swapped byte order for compatibility with pushRect()
*data++ = (rgb<<8) | (rgb>>8);
}
#endif
CS_H;
// Set masked pins D0- D7 to output
busDir(dir_mask, OUTPUT);
#else // Not ESP32_PARALLEL
spi_begin_read();
readAddrWindow(x, y, x + w - 1, y + h - 1); // Sets CS low
// Dummy read to throw away don't care value
tft_Write_8(0);
// Read window pixel 24 bit RGB values
uint32_t len = w * h;
while (len--) {
// Read the 3 RGB bytes, colour is actually only in the top 6 bits of each byte
// as the TFT stores colours as 18 bits
#if !defined (ILI9488_DRIVER)
uint8_t r = tft_Write_8(0);
uint8_t g = tft_Write_8(0);
uint8_t b = tft_Write_8(0);
#else
// The 6 colour bits are in LS 6 bits of each byte but we do not include the extra clock pulse
// so we use a trick and mask the middle 6 bits of the byte, then only shift 1 place left
uint8_t r = (tft_Write_8(0)&0x7E)<<1;
uint8_t g = (tft_Write_8(0)&0x7E)<<1;
uint8_t b = (tft_Write_8(0)&0x7E)<<1;
#endif
// Swapped colour byte order for compatibility with pushRect()
*data++ = (r & 0xF8) | (g & 0xE0) >> 5 | (b & 0xF8) << 5 | (g & 0x1C) << 11;
}
CS_H;
spi_end_read();
#endif
}
/***************************************************************************************
** Function name: push rectangle (for SPI Interface II i.e. IM [3:0] = "1101")
** Description: push 565 pixel colours into a defined area
***************************************************************************************/
void TFT_eSPI::pushRect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint16_t *data)
{
// Function deprecated, remains for backwards compatibility
// pushImage() is better as it will crop partly off-screen image blocks
pushImage(x, y, w, h, data);
}
/***************************************************************************************
** Function name: pushImage
** Description: plot 16 bit colour sprite or image onto TFT
***************************************************************************************/
void TFT_eSPI::pushImage(int32_t x, int32_t y, uint32_t w, uint32_t h, uint16_t *data)
{
if ((x >= (int32_t)_width) || (y >= (int32_t)_height)) return;
int32_t dx = 0;
int32_t dy = 0;
int32_t dw = w;
int32_t dh = h;
if (x < 0) { dw += x; dx = -x; x = 0; }
if (y < 0) { dh += y; dy = -y; y = 0; }
if ((x + w) > _width ) dw = _width - x;
if ((y + h) > _height) dh = _height - y;
if (dw < 1 || dh < 1) return;
spi_begin();
inTransaction = true;
setAddrWindow(x, y, x + dw - 1, y + dh - 1); // Sets CS low and sent RAMWR
data += dx + dy * w;
while (dh--)
{
pushColors(data, dw, _swapBytes);
data += w;
}
CS_H;
inTransaction = false;
spi_end();
}
/***************************************************************************************
** Function name: pushImage
** Description: plot 16 bit sprite or image with 1 colour being transparent
***************************************************************************************/
void TFT_eSPI::pushImage(int32_t x, int32_t y, uint32_t w, uint32_t h, uint16_t *data, uint16_t transp)
{
if ((x >= (int32_t)_width) || (y >= (int32_t)_height)) return;
int32_t dx = 0;
int32_t dy = 0;
int32_t dw = w;
int32_t dh = h;
if (x < 0) { dw += x; dx = -x; x = 0; }
if (y < 0) { dh += y; dy = -y; y = 0; }
if ((x + w) > _width ) dw = _width - x;
if ((y + h) > _height) dh = _height - y;
if (dw < 1 || dh < 1) return;
spi_begin();
inTransaction = true;
data += dx + dy * w;
int32_t xe = x + dw - 1, ye = y + dh - 1;
uint16_t lineBuf[dw];
if (!_swapBytes) transp = transp >> 8 | transp << 8;
while (dh--)
{
int32_t len = dw;
uint16_t* ptr = data;
int32_t px = x;
boolean move = true;
uint16_t np = 0;
while (len--)
{
if (transp != *ptr)
{
if (move) { move = false; setAddrWindow(px, y, xe, ye); }
lineBuf[np] = *ptr;
np++;
}
else
{
move = true;
if (np)
{
pushColors((uint16_t*)lineBuf, np, _swapBytes);
np = 0;
}
}
px++;
ptr++;
}
if (np) pushColors((uint16_t*)lineBuf, np, _swapBytes);
y++;
data += w;
}
CS_H;
inTransaction = false;
spi_end();
}
/***************************************************************************************
** Function name: pushImage - for FLASH (PROGMEM) stored images
** Description: plot 16 bit image
***************************************************************************************/
void TFT_eSPI::pushImage(int32_t x, int32_t y, uint32_t w, uint32_t h, const uint16_t *data)
{
if ((x >= (int32_t)_width) || (y >= (int32_t)_height)) return;
int32_t dx = 0;
int32_t dy = 0;
int32_t dw = w;
int32_t dh = h;
if (x < 0) { dw += x; dx = -x; x = 0; }
if (y < 0) { dh += y; dy = -y; y = 0; }
if ((x + w) > _width ) dw = _width - x;
if ((y + h) > _height) dh = _height - y;
if (dw < 1 || dh < 1) return;
spi_begin();
inTransaction = true;
data += dx + dy * w;
uint16_t buffer[64];
uint16_t* pix_buffer = buffer;
setAddrWindow(x, y, x + dw - 1, y + dh - 1);
// Work out the number whole buffers to send
uint16_t nb = (dw * dh) / 64;
// Fill and send "nb" buffers to TFT
for (int i = 0; i < nb; i++) {
for (int j = 0; j < 64; j++) {
pix_buffer[j] = pgm_read_word(&data[i * 64 + j]);
}
pushColors(pix_buffer, 64, !_swapBytes);
}
// Work out number of pixels not yet sent
uint16_t np = (dw * dh) % 64;
// Send any partial buffer left over
if (np) {
for (int i = 0; i < np; i++)
{
pix_buffer[i] = pgm_read_word(&data[nb * 64 + i]);
}
pushColors(pix_buffer, np, !_swapBytes);
}
CS_H;
inTransaction = false;
spi_end();
}
/***************************************************************************************
** Function name: pushImage - for FLASH (PROGMEM) stored images
** Description: plot 16 bit image with 1 colour being transparent
***************************************************************************************/
void TFT_eSPI::pushImage(int32_t x, int32_t y, uint32_t w, uint32_t h, const uint16_t *data, uint16_t transp)
{
if ((x >= (int32_t)_width) || (y >= (int32_t)_height)) return;
int32_t dx = 0;
int32_t dy = 0;
int32_t dw = w;
int32_t dh = h;
if (x < 0) { dw += x; dx = -x; x = 0; }
if (y < 0) { dh += y; dy = -y; y = 0; }
if ((x + w) > _width ) dw = _width - x;
if ((y + h) > _height) dh = _height - y;