-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdafruit_TFTLCD_ILI9342.cpp
548 lines (475 loc) · 13.1 KB
/
Adafruit_TFTLCD_ILI9342.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
// IMPORTANT: LIBRARY MUST BE SPECIFICALLY CONFIGURED FOR EITHER TFT SHIELD
// OR BREAKOUT BOARD USAGE. SEE RELEVANT COMMENTS IN Adafruit_TFTLCD.h
// Modified for ILI9342 by Milo vd Zee
// Graphics library by ladyada/adafruit with init code from Rossum
// MIT license
#if defined(__SAM3X8E__)
#include <include/pio.h>
#define PROGMEM
#define pgm_read_byte(addr) (*(const unsigned char *)(addr))
#define pgm_read_word(addr) (*(const unsigned short *)(addr))
#endif
#ifdef __AVR__
#include <avr/pgmspace.h>
#endif
#include "pins_arduino.h"
#include "wiring_private.h"
#include "Adafruit_TFTLCD_ILI9342.h"
#include "pin_magic_ILI9342.h"
//#define TFTWIDTH 320
//#define TFTHEIGHT 480
#define TFTWIDTH 320
#define TFTHEIGHT 240
#include "registers_ILI9342.h"
#define INVERT_X
//#define INVERT_Y
#ifdef INVERT_X
#define X(x) (_width - x - 1)
#define I_X(x) (x)
#else
#define X(x) (x)
#define I_X(x) (_width - x - 1)
#endif
#ifdef INVERT_Y
#define Y(y) (_height - y - 1)
#define I_Y(y) (y)
#else
#define Y(y) (y)
#define I_Y(y) (_height - y - 1)
#endif
// Constructor for breakout board (configurable LCD control lines).
// Can still use this w/shield, but parameters are ignored.
Adafruit_TFTLCD_ILI9342::Adafruit_TFTLCD_ILI9342(uint8_t cs, uint8_t cd, uint8_t wr, uint8_t rd, uint8_t reset) : Adafruit_GFX(TFTWIDTH, TFTHEIGHT) {
#ifndef USE_ADAFRUIT_SHIELD_PINOUT
// Convert pin numbers to registers and bitmasks
_reset = reset;
#ifdef __AVR__
csPort = portOutputRegister(digitalPinToPort(cs));
cdPort = portOutputRegister(digitalPinToPort(cd));
wrPort = portOutputRegister(digitalPinToPort(wr));
rdPort = portOutputRegister(digitalPinToPort(rd));
#endif
#if defined(__SAM3X8E__)
csPort = digitalPinToPort(cs);
cdPort = digitalPinToPort(cd);
wrPort = digitalPinToPort(wr);
rdPort = digitalPinToPort(rd);
#endif
csPinSet = digitalPinToBitMask(cs);
cdPinSet = digitalPinToBitMask(cd);
wrPinSet = digitalPinToBitMask(wr);
rdPinSet = digitalPinToBitMask(rd);
csPinUnset = ~csPinSet;
cdPinUnset = ~cdPinSet;
wrPinUnset = ~wrPinSet;
rdPinUnset = ~rdPinSet;
#ifdef __AVR__
*csPort |= csPinSet; // Set all control bits to HIGH (idle)
*cdPort |= cdPinSet; // Signals are ACTIVE LOW
*wrPort |= wrPinSet;
*rdPort |= rdPinSet;
#endif
#if defined(__SAM3X8E__)
csPort->PIO_SODR |= csPinSet; // Set all control bits to HIGH (idle)
cdPort->PIO_SODR |= cdPinSet; // Signals are ACTIVE LOW
wrPort->PIO_SODR |= wrPinSet;
rdPort->PIO_SODR |= rdPinSet;
#endif
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_ILI9342::Adafruit_TFTLCD_ILI9342(void) : Adafruit_GFX(TFTWIDTH, TFTHEIGHT) {
init();
}
// Initialization common to both shield & breakout configs
void Adafruit_TFTLCD_ILI9342::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;
textsize_x = 1;
textsize_y = 1;
textcolor = 0xFFFF;
_width = TFTWIDTH;
_height = TFTHEIGHT;
}
void Adafruit_TFTLCD_ILI9342::begin() {
uint8_t i = 0;
reset();
delay(200);
uint16_t a, d;
CS_ACTIVE;
writeRegister8(ILI9341_SOFTRESET, 0);
delay(50);
writeRegister8(ILI9341_DISPLAYOFF, 0);
writeRegister8(ILI9341_POWERCONTROL1, 0x23);
writeRegister8(ILI9341_POWERCONTROL2, 0x10);
writeRegister16(ILI9341_VCOMCONTROL1, 0x2B2B);
writeRegister8(ILI9341_VCOMCONTROL2, 0xC0);
writeRegister8(ILI9341_MEMCONTROL, ILI9341_MADCTL_MY | ILI9341_MADCTL_BGR);
writeRegister8(ILI9341_PIXELFORMAT, 0x55);
writeRegister16(ILI9341_FRAMECONTROL, 0x001B);
writeRegister8(ILI9341_ENTRYMODE, 0x07);
/* writeRegister32(ILI9341_DISPLAYFUNC, 0x0A822700);*/
writeRegister8(ILI9341_SLEEPOUT, 0);
delay(150);
writeRegister8(ILI9341_DISPLAYON, 0);
delay(500);
setAddrWindow(0, 0, TFTWIDTH-1, TFTHEIGHT-1);
return;
}
void Adafruit_TFTLCD_ILI9342::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
// Data transfer sync
CS_ACTIVE;
CD_COMMAND;
write8(0x00);
for(uint8_t i=0; i<3; i++) WR_STROBE; // Three extra 0x00s
CS_IDLE;
}
// 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_ILI9342::setAddrWindow(int x1, int y1, int x2, int y2) {
CS_ACTIVE;
uint32_t t;
// map x and y coordinates
x1 = X(x1);
x2 = X(x2);
y1 = Y(y1);
y2 = Y(y2);
if(x1 > x2) {
int x = x1;
x1 = x2;
x2 = x;
}
if(y1 > y2) {
int y = y1;
y1 = y2;
y2 = y;
}
t = x1;
t <<= 16;
t |= x2;
writeRegister32(ILI9341_COLADDRSET, t);
t = y1;
t <<= 16;
t |= y2;
writeRegister32(ILI9341_PAGEADDRSET, t);
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_ILI9342::flood(uint16_t color, uint32_t len) {
color = 0xffff - color;
uint16_t blocks;
uint8_t i, hi = color >> 8, lo = color;
CS_ACTIVE;
CD_COMMAND;
write8(0x2C);
// 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_ILI9342::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);
}
void Adafruit_TFTLCD_ILI9342::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);
}
void Adafruit_TFTLCD_ILI9342::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);
}
void Adafruit_TFTLCD_ILI9342::fillScreen(uint16_t color) {
setAddrWindow(0, 0, _width - 1, _height - 1);
flood(color, (long)TFTWIDTH * (long)TFTHEIGHT);
}
void Adafruit_TFTLCD_ILI9342::drawPixel(int16_t x, int16_t y, uint16_t color) {
// Clip
if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
CS_ACTIVE;
setAddrWindow(x, y, x, y);
CS_ACTIVE;
CD_COMMAND;
write8(0x2C);
CD_DATA;
color = 0xffff - color;
write8(color >> 8); write8(color);
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_ILI9342::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;
write8(0x2C);
}
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_ILI9342::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;
uint16_t t;
switch (rotation) {
case 2:
t = ILI9341_MADCTL_MX | ILI9341_MADCTL_BGR;
break;
case 3:
t = ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR;
break;
case 0:
t = ILI9341_MADCTL_MY | ILI9341_MADCTL_BGR;
break;
case 1:
t = ILI9341_MADCTL_MX | ILI9341_MADCTL_MY | ILI9341_MADCTL_MV | ILI9341_MADCTL_BGR;
break;
}
writeRegister8(ILI9341_MADCTL, t ); // MADCTL
// For 9341, init default full-screen address window:
setAddrWindow(0, 0, _width - 1, _height - 1); // CS_IDLE happens here
}
#ifdef read8isFunctionalized
#define read8(x) x=read8fn()
#endif
// Ditto with the read/write port directions, as above.
uint16_t Adafruit_TFTLCD_ILI9342::readID(void) {
uint8_t hi, lo;
delay(100);
uint16_t id = readReg(0xD3);
Serial.print("0xD3 = 0x");
Serial.print(id, HEX);
Serial.println(" (id)");
return id;
}
uint32_t Adafruit_TFTLCD_ILI9342::readReg(uint8_t r) {
uint32_t value;
uint8_t x;
// try reading register #4
CS_ACTIVE;
CD_COMMAND;
write8(r);
setReadDir(); // Set up LCD data port(s) for READ operations
CD_DATA;
delayMicroseconds(50);
read8(x);
value = x;
value <<= 8;
read8(x);
value |= x;
value <<= 8;
read8(x);
value |= x;
value <<= 8;
read8(x);
value |= x;
CS_IDLE;
setWriteDir(); // Restore LCD data port(s) to WRITE configuration
return value;
}
// Pass 8-bit (each) R,G,B, get back 16-bit packed color
uint16_t Adafruit_TFTLCD_ILI9342::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_ILI9342::write8(uint8_t value) {
write8inline(value);
}
#endif
#ifdef read8isFunctionalized
uint8_t Adafruit_TFTLCD_ILI9342::read8fn(void) {
uint8_t result;
read8inline(result);
return result;
}
#endif
#ifndef setWriteDir
void Adafruit_TFTLCD_ILI9342::setWriteDir(void) {
setWriteDirInline();
}
#endif
#ifndef setReadDir
void Adafruit_TFTLCD_ILI9342::setReadDir(void) {
setReadDirInline();
}
#endif
#ifndef writeRegister8
void Adafruit_TFTLCD_ILI9342::writeRegister8(uint8_t a, uint8_t d) {
writeRegister8inline(a, d);
}
#endif
#ifndef writeRegister16
void Adafruit_TFTLCD_ILI9342::writeRegister16(uint16_t a, uint16_t d) {
writeRegister16inline(a, d);
}
#endif
#ifndef writeRegisterPair
void Adafruit_TFTLCD_ILI9342::writeRegisterPair(uint8_t aH, uint8_t aL, uint16_t d) {
writeRegisterPairInline(aH, aL, d);
}
#endif
void Adafruit_TFTLCD_ILI9342::writeRegister24(uint8_t r, uint32_t d) {
CS_ACTIVE;
CD_COMMAND;
write8(r);
CD_DATA;
delayMicroseconds(10);
write8(d >> 16);
delayMicroseconds(10);
write8(d >> 8);
delayMicroseconds(10);
write8(d);
CS_IDLE;
}
void Adafruit_TFTLCD_ILI9342::writeRegister32(uint8_t r, uint32_t d) {
CS_ACTIVE;
CD_COMMAND;
write8(r);
CD_DATA;
delayMicroseconds(10);
write8(d >> 24);
delayMicroseconds(10);
write8(d >> 16);
delayMicroseconds(10);
write8(d >> 8);
delayMicroseconds(10);
write8(d);
CS_IDLE;
}