forked from adafruit/TFTLCD-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adafruit_TFTLCD.cpp
804 lines (709 loc) · 22.8 KB
/
Adafruit_TFTLCD.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
// IMPORTANT: LIBRARY MUST BE SPECIFICALLY CONFIGURED FOR EITHER TFT SHIELD
// OR BREAKOUT BOARD USAGE. SEE RELEVANT COMMENTS IN Adafruit_TFTLCD.h
// Graphics library by ladyada/adafruit with init code from Rossum
// MIT license
#include <avr/pgmspace.h>
#include "pins_arduino.h"
#include "wiring_private.h"
#include "Adafruit_TFTLCD.h"
#include "pin_magic.h"
#define TFTWIDTH 240
#define TFTHEIGHT 320
// LCD controller chip identifiers
#define ID_932X 0
#define ID_7575 1
#define ID_UNKNOWN 0xFF
// Register names from Peter Barrett's Microtouch code
#define ILI932X_START_OSC 0x00
#define ILI932X_DRIV_OUT_CTRL 0x01
#define ILI932X_DRIV_WAV_CTRL 0x02
#define ILI932X_ENTRY_MOD 0x03
#define ILI932X_RESIZE_CTRL 0x04
#define ILI932X_DISP_CTRL1 0x07
#define ILI932X_DISP_CTRL2 0x08
#define ILI932X_DISP_CTRL3 0x09
#define ILI932X_DISP_CTRL4 0x0A
#define ILI932X_RGB_DISP_IF_CTRL1 0x0C
#define ILI932X_FRM_MARKER_POS 0x0D
#define ILI932X_RGB_DISP_IF_CTRL2 0x0F
#define ILI932X_POW_CTRL1 0x10
#define ILI932X_POW_CTRL2 0x11
#define ILI932X_POW_CTRL3 0x12
#define ILI932X_POW_CTRL4 0x13
#define ILI932X_GRAM_HOR_AD 0x20
#define ILI932X_GRAM_VER_AD 0x21
#define ILI932X_RW_GRAM 0x22
#define ILI932X_POW_CTRL7 0x29
#define ILI932X_FRM_RATE_COL_CTRL 0x2B
#define ILI932X_GAMMA_CTRL1 0x30
#define ILI932X_GAMMA_CTRL2 0x31
#define ILI932X_GAMMA_CTRL3 0x32
#define ILI932X_GAMMA_CTRL4 0x35
#define ILI932X_GAMMA_CTRL5 0x36
#define ILI932X_GAMMA_CTRL6 0x37
#define ILI932X_GAMMA_CTRL7 0x38
#define ILI932X_GAMMA_CTRL8 0x39
#define ILI932X_GAMMA_CTRL9 0x3C
#define ILI932X_GAMMA_CTRL10 0x3D
#define ILI932X_HOR_START_AD 0x50
#define ILI932X_HOR_END_AD 0x51
#define ILI932X_VER_START_AD 0x52
#define ILI932X_VER_END_AD 0x53
#define ILI932X_GATE_SCAN_CTRL1 0x60
#define ILI932X_GATE_SCAN_CTRL2 0x61
#define ILI932X_GATE_SCAN_CTRL3 0x6A
#define ILI932X_PART_IMG1_DISP_POS 0x80
#define ILI932X_PART_IMG1_START_AD 0x81
#define ILI932X_PART_IMG1_END_AD 0x82
#define ILI932X_PART_IMG2_DISP_POS 0x83
#define ILI932X_PART_IMG2_START_AD 0x84
#define ILI932X_PART_IMG2_END_AD 0x85
#define ILI932X_PANEL_IF_CTRL1 0x90
#define ILI932X_PANEL_IF_CTRL2 0x92
#define ILI932X_PANEL_IF_CTRL3 0x93
#define ILI932X_PANEL_IF_CTRL4 0x95
#define ILI932X_PANEL_IF_CTRL5 0x97
#define ILI932X_PANEL_IF_CTRL6 0x98
#define HX8347G_COLADDRSTART_HI 0x02
#define HX8347G_COLADDRSTART_LO 0x03
#define HX8347G_COLADDREND_HI 0x04
#define HX8347G_COLADDREND_LO 0x05
#define HX8347G_ROWADDRSTART_HI 0x06
#define HX8347G_ROWADDRSTART_LO 0x07
#define HX8347G_ROWADDREND_HI 0x08
#define HX8347G_ROWADDREND_LO 0x09
#define HX8347G_MEMACCESS 0x16
// Constructor for breakout board (configurable LCD control lines).
// Can still use this w/shield, but parameters are ignored.
Adafruit_TFTLCD::Adafruit_TFTLCD(
uint8_t cs, uint8_t cd, uint8_t wr, uint8_t rd, uint8_t reset) {
#ifndef USE_ADAFRUIT_SHIELD_PINOUT
// Convert pin numbers to registers and bitmasks
_reset = reset;
csPort = portOutputRegister(digitalPinToPort(cs));
cdPort = portOutputRegister(digitalPinToPort(cd));
wrPort = portOutputRegister(digitalPinToPort(wr));
rdPort = portOutputRegister(digitalPinToPort(rd));
csPinSet = digitalPinToBitMask(cs);
cdPinSet = digitalPinToBitMask(cd);
wrPinSet = digitalPinToBitMask(wr);
rdPinSet = digitalPinToBitMask(rd);
csPinUnset = ~csPinSet;
cdPinUnset = ~cdPinSet;
wrPinUnset = ~wrPinSet;
rdPinUnset = ~rdPinSet;
*csPort |= csPinSet; // Set all control bits to HIGH (idle)
*cdPort |= cdPinSet; // Signals are ACTIVE LOW
*wrPort |= wrPinSet;
*rdPort |= rdPinSet;
pinMode(cs, OUTPUT); // Enable outputs
pinMode(cd, OUTPUT);
pinMode(wr, OUTPUT);
pinMode(rd, OUTPUT);
if(reset) {
digitalWrite(reset, HIGH);
pinMode(reset, OUTPUT);
}
#endif
init();
}
// Constructor for shield (fixed LCD control lines)
Adafruit_TFTLCD::Adafruit_TFTLCD(void) {
init();
}
// Initialization common to both shield & breakout configs
void Adafruit_TFTLCD::init(void) {
#ifdef USE_ADAFRUIT_SHIELD_PINOUT
CS_IDLE; // Set all control bits to idle state
WR_IDLE;
RD_IDLE;
CD_DATA;
digitalWrite(5, HIGH); // Reset line
pinMode(A3, OUTPUT); // Enable outputs
pinMode(A2, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A0, OUTPUT);
pinMode( 5, OUTPUT);
#endif
setWriteDir(); // Set up LCD data port(s) for WRITE operations
rotation = 0;
cursor_y = cursor_x = 0;
textsize = 1;
textcolor = 0xFFFF;
_width = TFTWIDTH;
_height = TFTHEIGHT;
}
// Initialization command tables for different LCD controllers
#define TFTLCD_DELAY 0xFF
static const uint8_t HX8347G_regValues[] PROGMEM = {
0x2E , 0x89,
0x29 , 0x8F,
0x2B , 0x02,
0xE2 , 0x00,
0xE4 , 0x01,
0xE5 , 0x10,
0xE6 , 0x01,
0xE7 , 0x10,
0xE8 , 0x70,
0xF2 , 0x00,
0xEA , 0x00,
0xEB , 0x20,
0xEC , 0x3C,
0xED , 0xC8,
0xE9 , 0x38,
0xF1 , 0x01,
// skip gamma, do later
0x1B , 0x1A,
0x1A , 0x02,
0x24 , 0x61,
0x25 , 0x5C,
0x18 , 0x36,
0x19 , 0x01,
0x1F , 0x88,
TFTLCD_DELAY , 5 , // delay 5 ms
0x1F , 0x80,
TFTLCD_DELAY , 5 ,
0x1F , 0x90,
TFTLCD_DELAY , 5 ,
0x1F , 0xD4,
TFTLCD_DELAY , 5 ,
0x17 , 0x05,
0x36 , 0x09,
0x28 , 0x38,
TFTLCD_DELAY , 40 ,
0x28 , 0x3C,
0x02 , 0x00,
0x03 , 0x00,
0x04 , 0x00,
0x05 , 0xEF,
0x06 , 0x00,
0x07 , 0x00,
0x08 , 0x01,
0x09 , 0x3F
};
static const uint16_t ILI932x_regValues[] PROGMEM = {
ILI932X_START_OSC , 0x0001, // Start oscillator
TFTLCD_DELAY , 50, // 50 millisecond delay
ILI932X_DRIV_OUT_CTRL , 0x0100,
ILI932X_DRIV_WAV_CTRL , 0x0700,
ILI932X_ENTRY_MOD , 0x1030,
ILI932X_RESIZE_CTRL , 0x0000,
ILI932X_DISP_CTRL2 , 0x0202,
ILI932X_DISP_CTRL3 , 0x0000,
ILI932X_DISP_CTRL4 , 0x0000,
ILI932X_RGB_DISP_IF_CTRL1, 0x0,
ILI932X_FRM_MARKER_POS , 0x0,
ILI932X_RGB_DISP_IF_CTRL2, 0x0,
ILI932X_POW_CTRL1 , 0x0000,
ILI932X_POW_CTRL2 , 0x0007,
ILI932X_POW_CTRL3 , 0x0000,
ILI932X_POW_CTRL4 , 0x0000,
TFTLCD_DELAY , 200,
ILI932X_POW_CTRL1 , 0x1690,
ILI932X_POW_CTRL2 , 0x0227,
TFTLCD_DELAY , 50,
ILI932X_POW_CTRL3 , 0x001A,
TFTLCD_DELAY , 50,
ILI932X_POW_CTRL4 , 0x1800,
ILI932X_POW_CTRL7 , 0x002A,
TFTLCD_DELAY , 50,
ILI932X_GAMMA_CTRL1 , 0x0000,
ILI932X_GAMMA_CTRL2 , 0x0000,
ILI932X_GAMMA_CTRL3 , 0x0000,
ILI932X_GAMMA_CTRL4 , 0x0206,
ILI932X_GAMMA_CTRL5 , 0x0808,
ILI932X_GAMMA_CTRL6 , 0x0007,
ILI932X_GAMMA_CTRL7 , 0x0201,
ILI932X_GAMMA_CTRL8 , 0x0000,
ILI932X_GAMMA_CTRL9 , 0x0000,
ILI932X_GAMMA_CTRL10 , 0x0000,
ILI932X_GRAM_HOR_AD , 0x0000,
ILI932X_GRAM_VER_AD , 0x0000,
ILI932X_HOR_START_AD , 0x0000,
ILI932X_HOR_END_AD , 0x00EF,
ILI932X_VER_START_AD , 0X0000,
ILI932X_VER_END_AD , 0x013F,
ILI932X_GATE_SCAN_CTRL1 , 0xA700, // Driver Output Control (R60h)
ILI932X_GATE_SCAN_CTRL2 , 0x0003, // Driver Output Control (R61h)
ILI932X_GATE_SCAN_CTRL3 , 0x0000, // Driver Output Control (R62h)
ILI932X_PANEL_IF_CTRL1 , 0X0010, // Panel Interface Control 1 (R90h)
ILI932X_PANEL_IF_CTRL2 , 0X0000,
ILI932X_PANEL_IF_CTRL3 , 0X0003,
ILI932X_PANEL_IF_CTRL4 , 0X1100,
ILI932X_PANEL_IF_CTRL5 , 0X0000,
ILI932X_PANEL_IF_CTRL6 , 0X0000,
ILI932X_DISP_CTRL1 , 0x0133, // Main screen turn on
};
void Adafruit_TFTLCD::begin(uint16_t id) {
uint8_t i = 0;
constructor(TFTWIDTH, TFTHEIGHT);
reset();
if((id == 0x9325) || (id == 0x9328)) {
uint16_t a, d;
driver = ID_932X;
CS_ACTIVE;
while(i < sizeof(ILI932x_regValues) / sizeof(uint16_t)) {
a = pgm_read_word(&ILI932x_regValues[i++]);
d = pgm_read_word(&ILI932x_regValues[i++]);
if(a == TFTLCD_DELAY) delay(d);
else writeRegister16(a, d);
}
setRotation(rotation);
setAddrWindow(0, 0, TFTWIDTH-1, TFTHEIGHT-1);
} else if(id == 0x7575) {
uint8_t a, d;
driver = ID_7575;
CS_ACTIVE;
while(i < sizeof(HX8347G_regValues)) {
a = pgm_read_byte(&HX8347G_regValues[i++]);
d = pgm_read_byte(&HX8347G_regValues[i++]);
if(a == TFTLCD_DELAY) delay(d);
else writeRegister8(a, d);
}
setRotation(rotation);
setLR(); // Lower-right corner of address window
} else {
driver = ID_UNKNOWN;
return;
}
}
void Adafruit_TFTLCD::reset(void) {
CS_IDLE;
CD_DATA;
WR_IDLE;
RD_IDLE;
#ifdef USE_ADAFRUIT_SHIELD_PINOUT
digitalWrite(5, LOW);
delay(2);
digitalWrite(5, HIGH);
#else
if(_reset) {
digitalWrite(_reset, LOW);
delay(2);
digitalWrite(_reset, HIGH);
}
#endif
CS_ACTIVE;
CD_DATA;
write8(0x00);
for(uint8_t i=0; i<7; i++) WR_STROBE;
CS_IDLE;
delay(100);
}
// Sets the LCD address window (and address counter, on 932X).
// Relevant to rect/screen fills and H/V lines. Input coordinates are
// assumed pre-sorted (e.g. x2 >= x1).
void Adafruit_TFTLCD::setAddrWindow(int x1, int y1, int x2, int y2) {
CS_ACTIVE;
if(driver == ID_932X) {
// Values passed are in current (possibly rotated) coordinate
// system. 932X requires hardware-native coords regardless of
// MADCTL, so rotate inputs as needed. The address counter is
// set to the top-left corner -- although fill operations can be
// done in any direction, the current screen rotation is applied
// because some users find it disconcerting when a fill does not
// occur top-to-bottom.
int x, y, t;
switch(rotation) {
default:
x = x1;
y = y1;
break;
case 1:
t = y1;
y1 = x1;
x1 = TFTWIDTH - 1 - y2;
y2 = x2;
x2 = TFTWIDTH - 1 - t;
x = x2;
y = y1;
break;
case 2:
t = x1;
x1 = TFTWIDTH - 1 - x2;
x2 = TFTWIDTH - 1 - t;
t = y1;
y1 = TFTHEIGHT - 1 - y2;
y2 = TFTHEIGHT - 1 - t;
x = x2;
y = y2;
break;
case 3:
t = x1;
x1 = y1;
y1 = TFTHEIGHT - 1 - x2;
x2 = y2;
y2 = TFTHEIGHT - 1 - t;
x = x1;
y = y2;
break;
}
writeRegister16(0x0050, x1); // Set address window
writeRegister16(0x0051, x2);
writeRegister16(0x0052, y1);
writeRegister16(0x0053, y2);
writeRegister16(0x0020, x ); // Set address counter to top left
writeRegister16(0x0021, y );
} else if(driver == ID_7575) {
writeRegisterPair(HX8347G_COLADDRSTART_HI, HX8347G_COLADDRSTART_LO, x1);
writeRegisterPair(HX8347G_ROWADDRSTART_HI, HX8347G_ROWADDRSTART_LO, y1);
writeRegisterPair(HX8347G_COLADDREND_HI , HX8347G_COLADDREND_LO , x2);
writeRegisterPair(HX8347G_ROWADDREND_HI , HX8347G_ROWADDREND_LO , y2);
}
CS_IDLE;
}
// Unlike the 932X drivers that set the address window to the full screen
// by default (using the address counter for drawPixel operations), the
// 7575 needs the address window set on all graphics operations. In order
// to save a few register writes on each pixel drawn, the lower-right
// corner of the address window is reset after most fill operations, so
// that drawPixel only needs to change the upper left each time.
void Adafruit_TFTLCD::setLR(void) {
CS_ACTIVE;
writeRegisterPair(HX8347G_COLADDREND_HI, HX8347G_COLADDREND_LO, _width - 1);
writeRegisterPair(HX8347G_ROWADDREND_HI, HX8347G_ROWADDREND_LO, _height - 1);
CS_IDLE;
}
// Fast block fill operation for fillScreen, fillRect, H/V line, etc.
// Requires setAddrWindow() has previously been called to set the fill
// bounds. 'len' is inclusive, MUST be >= 1.
void Adafruit_TFTLCD::flood(uint16_t color, uint32_t len) {
uint16_t blocks;
uint8_t i, hi = color >> 8,
lo = color;
CS_ACTIVE;
CD_COMMAND;
if(driver == ID_932X) write8(0x00); // High byte of GRAM register...
write8(0x22); // Write data to GRAM
// Write first pixel normally, decrement counter by 1
CD_DATA;
write8(hi);
write8(lo);
len--;
blocks = (uint16_t)(len / 64); // 64 pixels/block
if(hi == lo) {
// High and low bytes are identical. Leave prior data
// on the port(s) and just toggle the write strobe.
while(blocks--) {
i = 16; // 64 pixels/block / 4 pixels/pass
do {
WR_STROBE; WR_STROBE; WR_STROBE; WR_STROBE; // 2 bytes/pixel
WR_STROBE; WR_STROBE; WR_STROBE; WR_STROBE; // x 4 pixels
} while(--i);
}
// Fill any remaining pixels (1 to 64)
for(i = (uint8_t)len & 63; i--; ) {
WR_STROBE;
WR_STROBE;
}
} else {
while(blocks--) {
i = 16; // 64 pixels/block / 4 pixels/pass
do {
write8(hi); write8(lo); write8(hi); write8(lo);
write8(hi); write8(lo); write8(hi); write8(lo);
} while(--i);
}
for(i = (uint8_t)len & 63; i--; ) {
write8(hi);
write8(lo);
}
}
CS_IDLE;
}
void Adafruit_TFTLCD::drawFastHLine(int16_t x, int16_t y, int16_t length,
uint16_t color)
{
int16_t x2;
// Initial off-screen clipping
if((length <= 0 ) ||
(y < 0 ) || ( y >= _height) ||
(x >= _width) || ((x2 = (x+length-1)) < 0 )) return;
if(x < 0) { // Clip left
length += x;
x = 0;
}
if(x2 >= _width) { // Clip right
x2 = _width - 1;
length = x2 - x + 1;
}
setAddrWindow(x, y, x2, y);
flood(color, length);
if(driver == ID_932X) setAddrWindow(0, 0, _width - 1, _height - 1);
else setLR();
}
void Adafruit_TFTLCD::drawFastVLine(int16_t x, int16_t y, int16_t length,
uint16_t color)
{
int16_t y2;
// Initial off-screen clipping
if((length <= 0 ) ||
(x < 0 ) || ( x >= _width) ||
(y >= _height) || ((y2 = (y+length-1)) < 0 )) return;
if(y < 0) { // Clip top
length += y;
y = 0;
}
if(y2 >= _height) { // Clip bottom
y2 = _height - 1;
length = y2 - y + 1;
}
setAddrWindow(x, y, x, y2);
flood(color, length);
if(driver == ID_932X) setAddrWindow(0, 0, _width - 1, _height - 1);
else setLR();
}
void Adafruit_TFTLCD::fillRect(int16_t x1, int16_t y1, int16_t w, int16_t h,
uint16_t fillcolor) {
int16_t x2, y2;
// Initial off-screen clipping
if( (w <= 0 ) || (h <= 0 ) ||
(x1 >= _width) || (y1 >= _height) ||
((x2 = x1+w-1) < 0 ) || ((y2 = y1+h-1) < 0 )) return;
if(x1 < 0) { // Clip left
w += x1;
x1 = 0;
}
if(y1 < 0) { // Clip top
h += y1;
y1 = 0;
}
if(x2 >= _width) { // Clip right
x2 = _width - 1;
w = x2 - x1 + 1;
}
if(y2 >= _height) { // Clip bottom
y2 = _height - 1;
h = y2 - y1 + 1;
}
setAddrWindow(x1, y1, x2, y2);
flood(fillcolor, (uint32_t)w * (uint32_t)h);
if(driver == ID_932X) setAddrWindow(0, 0, _width - 1, _height - 1);
else setLR();
}
void Adafruit_TFTLCD::fillScreen(uint16_t color) {
if(driver == ID_932X) {
// For the 932X, a full-screen address window is already the default
// state, just need to set the address pointer to the top-left corner.
// Although we could fill in any direction, the code uses the current
// screen rotation because some users find it disconcerting when a
// fill does not occur top-to-bottom.
uint16_t x, y;
switch(rotation) {
default: x = 0 ; y = 0 ; break;
case 1 : x = TFTWIDTH - 1; y = 0 ; break;
case 2 : x = TFTWIDTH - 1; y = TFTHEIGHT - 1; break;
case 3 : x = 0 ; y = TFTHEIGHT - 1; break;
}
CS_ACTIVE;
writeRegister16(0x0020, x);
writeRegister16(0x0021, y);
} else if(driver == ID_7575) {
// For the 7575, there is no settable address pointer, instead the
// address window must be set for each drawing operation. However,
// this display takes rotation into account for the parameters, no
// need to do extra rotation math here.
setAddrWindow(0, 0, _width - 1, _height - 1);
}
flood(color, (long)TFTWIDTH * (long)TFTHEIGHT);
}
void Adafruit_TFTLCD::drawPixel(int16_t x, int16_t y, uint16_t color) {
// Clip
if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
CS_ACTIVE;
if(driver == ID_932X) {
int16_t t;
switch(rotation) {
case 1:
t = x;
x = TFTWIDTH - 1 - y;
y = t;
break;
case 2:
x = TFTWIDTH - 1 - x;
y = TFTHEIGHT - 1 - y;
break;
case 3:
t = x;
x = y;
y = TFTHEIGHT - 1 - t;
break;
}
writeRegister16(0x0020, x);
writeRegister16(0x0021, y);
writeRegister16(0x0022, color);
} else if(driver == ID_7575) {
uint8_t hi, lo;
switch(rotation) {
default: lo = 0 ; break;
case 1 : lo = 0x60; break;
case 2 : lo = 0xc0; break;
case 3 : lo = 0xa0; break;
}
writeRegister8( HX8347G_MEMACCESS , lo);
// Only upper-left is set -- bottom-right is full screen default
writeRegisterPair(HX8347G_COLADDRSTART_HI, HX8347G_COLADDRSTART_LO, x);
writeRegisterPair(HX8347G_ROWADDRSTART_HI, HX8347G_ROWADDRSTART_LO, y);
hi = color >> 8; lo = color;
CD_COMMAND; write8(0x22); CD_DATA; write8(hi); write8(lo);
}
CS_IDLE;
}
// Issues 'raw' an array of 16-bit color values to the LCD; used
// externally by BMP examples. Assumes that setWindowAddr() has
// previously been set to define the bounds. Max 255 pixels at
// a time (BMP examples read in small chunks due to limited RAM).
void Adafruit_TFTLCD::pushColors(uint16_t *data, uint8_t len, boolean first) {
uint16_t color;
uint8_t hi, lo;
CS_ACTIVE;
if(first == true) { // Issue GRAM write command only on first call
CD_COMMAND;
if(driver == ID_932X) write8(0x00);
write8(0x22);
}
CD_DATA;
while(len--) {
color = *data++;
hi = color >> 8; // Don't simplify or merge these
lo = color; // lines, there's macro shenanigans
write8(hi); // going on.
write8(lo);
}
CS_IDLE;
}
void Adafruit_TFTLCD::setRotation(uint8_t x) {
// Call parent rotation func first -- sets up rotation flags, etc.
Adafruit_GFX::setRotation(x);
// Then perform hardware-specific rotation operations...
CS_ACTIVE;
if(driver == ID_932X) {
uint16_t t;
switch(rotation) {
default: t = 0x1030; break;
case 1 : t = 0x1028; break;
case 2 : t = 0x1000; break;
case 3 : t = 0x1018; break;
}
writeRegister16(0x0003, t ); // MADCTL
// For 932X, init default full-screen address window:
setAddrWindow(0, 0, _width - 1, _height - 1); // CS_IDLE happens here
} if(driver == ID_7575) {
uint8_t t;
switch(rotation) {
default: t = 0 ; break;
case 1 : t = 0x60; break;
case 2 : t = 0xc0; break;
case 3 : t = 0xa0; break;
}
writeRegister8(HX8347G_MEMACCESS, t);
// 7575 has to set the address window on most drawing operations.
// drawPixel() cheats by setting only the top left...by default,
// the lower right is always reset to the corner.
setLR(); // CS_IDLE happens here
}
}
// Because this function is used infrequently, it configures the ports for
// the read operation, reads the data, then restores the ports to the write
// configuration. Write operations happen a LOT, so it's advantageous to
// leave the ports in that state as a default.
uint16_t Adafruit_TFTLCD::readPixel(int16_t x, int16_t y) {
uint16_t c;
if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return 0;
CS_ACTIVE;
if(driver == ID_932X) {
int16_t t;
switch(rotation) {
case 1:
t = x;
x = TFTWIDTH - 1 - y;
y = t;
break;
case 2:
x = TFTWIDTH - 1 - x;
y = TFTHEIGHT - 1 - y;
break;
case 3:
t = x;
x = y;
y = TFTHEIGHT - 1 - t;
break;
}
writeRegister16(0x0020, x);
writeRegister16(0x0021, y);
CD_COMMAND; write8(0x00); write8(0x22); // Read data from GRAM
} else if(driver == ID_7575) {
writeRegisterPair(HX8347G_COLADDRSTART_HI, HX8347G_COLADDRSTART_LO, x);
writeRegisterPair(HX8347G_ROWADDRSTART_HI, HX8347G_ROWADDRSTART_LO, y);
CD_COMMAND; write8(0x22); // Read data from GRAM
}
setReadDir(); // Set up LCD data port(s) for READ operations
CD_DATA;
c = read8(); // Do not merge or otherwise simplify
c <<= 8; // these lines. It's an unfortunate
delayMicroseconds(1); // artifact of the macro substitution
c |= read8(); // shenanigans that are going on.
setWriteDir(); // Restore LCD data port(s) to WRITE configuration
CS_IDLE;
return c;
}
// Ditto with the read/write port directions, as above.
uint16_t Adafruit_TFTLCD::readID(void) {
uint16_t id;
CS_ACTIVE;
CD_COMMAND;
write8(0x00);
WR_STROBE; // Extra strobe because high, low bytes are the same
setReadDir(); // Set up LCD data port(s) for READ operations
CD_DATA;
id = read8(); // Do not merge or otherwise simplify
id <<= 8; // these lines. It's an unfortunate
delayMicroseconds(1); // artifact of the macro substitution
id |= read8(); // shenanigans that are going on.
CS_IDLE;
setWriteDir(); // Restore LCD data port(s) to WRITE configuration
return id;
}
// Pass 8-bit (each) R,G,B, get back 16-bit packed color
uint16_t Adafruit_TFTLCD::color565(uint8_t r, uint8_t g, uint8_t b) {
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}
// For I/O macros that were left undefined, declare function
// versions that reference the inline macros just once:
#ifndef write8
void Adafruit_TFTLCD::write8(uint8_t value) {
write8inline(value);
}
#endif
#ifndef read8
uint8_t Adafruit_TFTLCD::read8(void) {
// Do not merge or simplify -- macro shenanigans going on!
uint8_t d = read8inline();
return d;
}
#endif
#ifndef setWriteDir
void Adafruit_TFTLCD::setWriteDir(void) {
setWriteDirInline();
}
#endif
#ifndef setReadDir
void Adafruit_TFTLCD::setReadDir(void) {
setReadDirInline();
}
#endif
#ifndef writeRegister8
void Adafruit_TFTLCD::writeRegister8(uint8_t a, uint8_t d) {
writeRegister8inline(a, d);
}
#endif
#ifndef writeRegister16
void Adafruit_TFTLCD::writeRegister16(uint16_t a, uint16_t d) {
writeRegister16inline(a, d);
}
#endif
#ifndef writeRegisterPair
void Adafruit_TFTLCD::writeRegisterPair(uint8_t aH, uint8_t aL, uint16_t d) {
writeRegisterPairInline(aH, aL, d);
}
#endif