-
Notifications
You must be signed in to change notification settings - Fork 4
/
ILI9341_kbv.cpp
390 lines (365 loc) · 12.1 KB
/
ILI9341_kbv.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
#include "ILI9341_kbv.h"
#include "serial_kbv.h"
ILI9341_kbv::ILI9341_kbv():Adafruit_GFX(240, 320)
{
}
static uint8_t done_reset;
void ILI9341_kbv::reset(void)
{
done_reset = 1;
INIT();
CS_IDLE;
RESET_IDLE;
wait_ms(50);
RESET_ACTIVE;
wait_ms(100);
RESET_IDLE;
wait_ms(100);
}
void ILI9341_kbv::pushCommand(uint16_t cmd, uint8_t * block, int8_t N)
{
CS_ACTIVE;
WriteCmd(cmd);
write8_block(block, N);
CS_IDLE;
}
#define ILI9341_CMD_NOP 0x00
#define ILI9341_CMD_SOFTWARE_RESET 0x01
#define ILI9341_CMD_READ_DISP_ID 0x04
#define ILI9341_CMD_READ_DISP_STATUS 0x09
#define ILI9341_CMD_READ_DISP_MADCTRL 0x0B
#define ILI9341_CMD_READ_DISP_PIXEL_FORMAT 0x0C
#define ILI9341_CMD_READ_DISP_IMAGE_FORMAT 0x0D
#define ILI9341_CMD_READ_DISP_SIGNAL_MODE 0x0E
#define ILI9341_CMD_READ_DISP_SELF_DIAGNOSTIC 0x0F
#define ILI9341_CMD_ENTER_SLEEP_MODE 0x10
#define ILI9341_CMD_SLEEP_OUT 0x11
#define ILI9341_CMD_PARTIAL_MODE_ON 0x12
#define ILI9341_CMD_NORMAL_DISP_MODE_ON 0x13
#define ILI9341_CMD_DISP_INVERSION_OFF 0x20
#define ILI9341_CMD_DISP_INVERSION_ON 0x21
#define ILI9341_CMD_GAMMA_SET 0x26
#define ILI9341_CMD_DISPLAY_OFF 0x28
#define ILI9341_CMD_DISPLAY_ON 0x29
#define ILI9341_CMD_COLUMN_ADDRESS_SET 0x2A
#define ILI9341_CMD_PAGE_ADDRESS_SET 0x2B
#define ILI9341_CMD_MEMORY_WRITE 0x2C
#define ILI9341_CMD_COLOR_SET 0x2D
#define ILI9341_CMD_MEMORY_READ 0x2E
#define ILI9341_CMD_PARTIAL_AREA 0x30
#define ILI9341_CMD_VERT_SCROLL_DEFINITION 0x33
#define ILI9341_CMD_TEARING_EFFECT_LINE_OFF 0x34
#define ILI9341_CMD_TEARING_EFFECT_LINE_ON 0x35
#define ILI9341_CMD_MEMORY_ACCESS_CONTROL 0x36
#define ILI9341_CMD_VERT_SCROLL_START_ADDRESS 0x37
#define ILI9341_CMD_IDLE_MODE_OFF 0x38
#define ILI9341_CMD_IDLE_MODE_ON 0x39
#define ILI9341_CMD_COLMOD_PIXEL_FORMAT_SET 0x3A
#define ILI9341_CMD_WRITE_MEMORY_CONTINUE 0x3C
#define ILI9341_CMD_READ_MEMORY_CONTINUE 0x3E
#define ILI9341_CMD_SET_TEAR_SCANLINE 0x44
#define ILI9341_CMD_GET_SCANLINE 0x45
#define ILI9341_CMD_WRITE_DISPLAY_BRIGHTNESS 0x51
#define ILI9341_CMD_READ_DISPLAY_BRIGHTNESS 0x52
#define ILI9341_CMD_WRITE_CTRL_DISPLAY 0x53
#define ILI9341_CMD_READ_CTRL_DISPLAY 0x54
#define ILI9341_CMD_WRITE_CONTENT_ADAPT_BRIGHTNESS 0x55
#define ILI9341_CMD_READ_CONTENT_ADAPT_BRIGHTNESS 0x56
#define ILI9341_CMD_WRITE_MIN_CAB_LEVEL 0x5E
#define ILI9341_CMD_READ_MIN_CAB_LEVEL 0x5F
#define ILI9341_CMD_READ_ID1 0xDA
#define ILI9341_CMD_READ_ID2 0xDB
#define ILI9341_CMD_READ_ID3 0xDC
static uint8_t readReg8(uint8_t reg, uint8_t dat)
{
uint8_t ret;
CD_COMMAND;
CS_ACTIVE;
xchg8(reg);
CD_DATA; //should do a flush()
ret = xchg8(dat); //only safe to read @ SCK=16MHz
CS_IDLE;
return ret;
}
uint8_t ILI9341_kbv::readcommand8(uint8_t reg, uint8_t idx) //this is the same as Adafruit_ILI9341
{
readReg8(0xD9, 0x10 | idx);
return readReg8(reg, 0xFF);
}
uint16_t ILI9341_kbv::readID(void) //{ return 0x9341; }
{
if (!done_reset) reset();
return (readcommand8(0xD3, 2) << 8) | readcommand8(0xD3, 3);
}
uint16_t ILI9341_kbv::readReg(uint16_t reg, uint8_t idx) //note that this reads pairs of data bytes
{
uint8_t h, l;
idx <<= 1;
h = readcommand8(reg, idx);
l = readcommand8(reg, idx + 1);
return (h << 8) | l;
}
int16_t ILI9341_kbv::readGRAM(int16_t x, int16_t y, uint16_t * block, int16_t w, int16_t h)
{
uint8_t r, g, b;
int16_t n = w * h; // we are NEVER going to read > 32k pixels at once
setAddrWindow(x, y, x + w - 1, y + h - 1);
CS_ACTIVE;
WriteCmd(ILI9341_CMD_MEMORY_READ);
// needs 1 dummy read
r = xchg8(0xFF);
while (n-- > 0) {
r = xchg8(0xFF);
g = xchg8(0xFF);
b = xchg8(0xFF);
*block++ = color565(r, g, b);
}
CS_IDLE;
setAddrWindow(0, 0, width() - 1, height() - 1);
return 0;
}
void ILI9341_kbv::setRotation(uint8_t r)
{
uint8_t mac = 0x00;
Adafruit_GFX::setRotation(r & 3);
switch (rotation) {
case 0:
mac = 0x08;
break;
case 1: //LANDSCAPE 90 degrees
mac = 0x68;
break;
case 2:
mac = 0xD8;
break;
case 3:
mac = 0xB8;
break;
}
pushCommand(ILI9341_CMD_MEMORY_ACCESS_CONTROL, &mac, 1);
}
void ILI9341_kbv::drawPixel(int16_t x, int16_t y, uint16_t color)
{
// ILI934X just plots at edge if you try to write outside of the box:
if (x < 0 || y < 0 || x >= width() || y >= height())
return;
CS_ACTIVE;
WriteCmd(ILI9341_CMD_COLUMN_ADDRESS_SET);
write16(x);
WriteCmd(ILI9341_CMD_PAGE_ADDRESS_SET);
write16(y);
WriteCmd(ILI9341_CMD_MEMORY_WRITE);
write16(color);
CS_IDLE;
}
void ILI9341_kbv::setAddrWindow(int16_t x, int16_t y, int16_t x1, int16_t y1)
{
CS_ACTIVE;
WriteCmd(ILI9341_CMD_COLUMN_ADDRESS_SET);
write16(x);
write16(x1);
WriteCmd(ILI9341_CMD_PAGE_ADDRESS_SET);
write16(y);
write16(y1);
CS_IDLE;
}
void ILI9341_kbv::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
{
int16_t end;
if (w < 0) {
w = -w;
x -= w;
} //+ve w
end = x + w;
if (x < 0)
x = 0;
if (end > width())
end = width();
w = end - x;
if (h < 0) {
h = -h;
y -= h;
} //+ve h
end = y + h;
if (y < 0)
y = 0;
if (end > height())
end = height();
h = end - y;
setAddrWindow(x, y, x + w - 1, y + h - 1);
CS_ACTIVE;
WriteCmd(ILI9341_CMD_MEMORY_WRITE);
if (h > w) { end = h; h = w; w = end; }
while (h-- > 0) {
write16_N(color, w);
}
CS_IDLE;
setAddrWindow(0, 0, width() - 1, height() - 1);
}
void ILI9341_kbv::pushColors(uint16_t * block, int16_t n, bool first)
{
uint16_t color;
CS_ACTIVE;
if (first) {
WriteCmd(ILI9341_CMD_MEMORY_WRITE);
}
while (n-- > 0) {
color = *block++;
write16(color);
}
CS_IDLE;
}
void ILI9341_kbv::pushColors(uint8_t * block, int16_t n, bool first)
{
uint16_t color;
uint8_t h, l;
CS_ACTIVE;
if (first) {
WriteCmd(ILI9341_CMD_MEMORY_WRITE);
}
while (n-- > 0) {
h = (*block++);
l = (*block++);
color = (h << 8) | l;
write16(color);
}
CS_IDLE;
}
void ILI9341_kbv::pushColors(const uint8_t * block, int16_t n, bool first, bool bigend)
{
uint16_t color;
uint8_t h, l;
CS_ACTIVE;
if (first) {
WriteCmd(ILI9341_CMD_MEMORY_WRITE);
}
while (n-- > 0) {
l = pgm_read_byte(block++);
h = pgm_read_byte(block++);
color = (bigend) ? (l << 8 ) | h : (h << 8) | l;
write16(color);
}
CS_IDLE;
}
void ILI9341_kbv::invertDisplay(boolean i)
{
pushCommand(i ? ILI9341_CMD_DISP_INVERSION_ON : ILI9341_CMD_DISP_INVERSION_OFF, NULL, 0);
}
void ILI9341_kbv::vertScroll(int16_t top, int16_t scrollines, int16_t offset)
{
int16_t bfa = HEIGHT - top - scrollines; // bottom fixed area
int16_t vsp;
vsp = top + offset; // vertical start position
if (offset < 0)
vsp += scrollines; //keep in unsigned range
CS_ACTIVE;
WriteCmd( 0x0033);
write16(top); //TOP
write16(scrollines); //VSA
write16(bfa); //BFA
WriteCmd(0x0037)
write16(vsp); //VLSP
CS_IDLE;
}
#define TFTLCD_DELAY8 0xFF
const uint8_t PROGMEM ILI9341_regValues_kbv[] = {
// (COMMAND_BYTE), n, data_bytes....
(0x01), 0, //ILI9341_CMD_SOFTWARE_RESET
TFTLCD_DELAY8, 50, // .kbv
(0xCF), 3, //ILI9341_CMD_POWER_CONTROL_B
0x00, 0x8B, 0x30,
(0xED), 4, //ILI9341_CMD_POWER_ON_SEQ_CONTROL
0x67, 0x03, 0x12, 0x81,
(0xE8), 3, //ILI9341_CMD_DRIVER_TIMING_CONTROL_A
0x85, 0x10, 0x7A,
(0xCB), 5, //ILI9341_CMD_POWER_CONTROL_A
0x39, 0x2C, 0x00, 0x34, 0x02,
(0xF7), 1, //ILI9341_CMD_PUMP_RATIO_CONTROL
0x20,
(0xEA), 2, //ILI9341_CMD_DRIVER_TIMING_CONTROL_B
0x00, 0x00,
(0xC0), 1, //ILI9341_CMD_POWER_CONTROL_1
0x1B, /* VRH[5:0] */
(0xC1), 1, //ILI9341_CMD_POWER_CONTROL_2
0x10, /* SAP[2:0];BT[3:0] */
(0xC5), 2, //ILI9341_CMD_VCOM_CONTROL_1
0x3F, 0x3C,
(0xC7), 1, //ILI9341_CMD_VCOM_CONTROL_2
0xB7,
(0x36), 1, //ILI9341_CMD_MEMORY_ACCESS_CONTROL
0x08,
(0x3A), 1, //ILI9341_CMD_COLMOD_PIXEL_FORMAT_SET
0x55,
(0xB1), 2, //ILI9341_CMD_FRAME_RATE_CONTROL_NORMAL
0x00, 0x1B,
(0xB4), 1, 0x00, //Inversion Control [02] .kbv NLA=1, NLB=1, NLC=1 Extended anyway
(0xB6), 2, //ILI9341_CMD_DISPLAY_FUNCTION_CONTROL
0x0A, 0xA2,
(0xF2), 1, //ILI9341_CMD_ENABLE_3_GAMMA_CONTROL
0x00, /* 3Gamma Function Disable */
(0x26), 1, //ILI9341_CMD_GAMMA_SET
0x01,
(0xE0), 15, //ILI9341_CMD_POSITIVE_GAMMA_CORRECTION
0x0F, 0x2A, 0x28, 0x08, 0x0E, 0x08, 0x54, 0XA9, 0x43, 0x0A, 0x0F, 0x00,
0x00, 0x00, 0x00,
(0xE1), 15, //ILI9341_CMD_NEGATIVE_GAMMA_CORRECTION
0x00, 0x15, 0x17, 0x07, 0x11, 0x06, 0x2B, 0x56, 0x3C, 0x05, 0x10, 0x0F,
0x3F, 0x3F, 0x0F,
(0x11), 0, //ILI9341_CMD_SLEEP_OUT
TFTLCD_DELAY8, 150, // .kbv
(0x29), 0, //ILI9341_CMD_DISPLAY_ON
};
static const uint8_t ILI9341_regValues_2_4[] PROGMEM = { // BOE 2.4"
0x01, 0, // software reset
TFTLCD_DELAY8, 50, // .kbv
0xCF, 3, 0x00, 0x81, 0x30, //Power Control B [00 81 30]
0xED, 4, 0x64, 0x03, 0x12, 0x81, //Power On Seq [55 01 23 01]
0xE8, 3, 0x85, 0x10, 0x78, //Driver Timing A [04 11 7A]
0xCB, 5, 0x39, 0x2C, 0x00, 0x34, 0x02, //Power Control A [39 2C 00 34 02]
0xF7, 1, 0x20, //Pump Ratio [10]
0xEA, 2, 0x00, 0x00, //Driver Timing B [66 00]
0xB1, 2, 0x00, 0x1B, //Frame Control [00 1B]
0xB6, 2, 0x0A, 0xA2, 0x27, //Display Function [0A 82 27 XX] .kbv SS=1
0xB4, 1, 0x00, //Inversion Control [02] .kbv NLA=1, NLB=1, NLC=1
0xC0, 1, 0x21, //Power Control 1 [26]
0xC1, 1, 0x11, //Power Control 2 [00]
0xC5, 2, 0x3F, 0x3C, //VCOM 1 [31 3C]
0xC7, 1, 0xB5, //VCOM 2 [C0]
0x36, 1, 0x48, //Memory Access [00]
0xF2, 1, 0x00, //Enable 3G [02]
0x26, 1, 0x01, //Gamma Set [01]
0xE0, 15, 0x0f, 0x26, 0x24, 0x0b, 0x0e, 0x09, 0x54, 0xa8, 0x46, 0x0c, 0x17, 0x09, 0x0f, 0x07, 0x00,
0xE1, 15, 0x00, 0x19, 0x1b, 0x04, 0x10, 0x07, 0x2a, 0x47, 0x39, 0x03, 0x06, 0x06, 0x30, 0x38, 0x0f,
0x11, 0, //Sleep Out
TFTLCD_DELAY8, 150,
0x29, 0, //Display On
0x3A, 1, 0x55, //Pixel Format [66]
};
// init_table(ILI9341_regValues_2_4, sizeof(ILI9341_regValues_2_4)); //
//#define tableNNNN ILI9341_regValues_2_4
#define tableNNNN ILI9341_regValues_kbv
void ILI9341_kbv::begin(uint16_t ID)
{
_lcd_ID = ID;
uint8_t *p = (uint8_t *) tableNNNN;
int16_t size = sizeof(tableNNNN);
reset();
while (size > 0) {
uint8_t cmd = pgm_read_byte(p++);
uint8_t len = pgm_read_byte(p++);
if (cmd == TFTLCD_DELAY8) {
delay(len);
len = 0;
} else {
CS_ACTIVE;
WriteCmd(cmd);
for (uint8_t d = 0; d < len; d++) {
uint8_t x = pgm_read_byte(p++);
xchg8(x);
}
CS_IDLE;
}
size -= len + 2;
}
setRotation(0); //PORTRAIT
}