-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathArduino_ST7789_STM.cpp
More file actions
475 lines (415 loc) · 12.6 KB
/
Arduino_ST7789_STM.cpp
File metadata and controls
475 lines (415 loc) · 12.6 KB
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
// Fast ST7789 IPS 240x240 SPI display library
// (c) 2019 by Pawel A. Hernik
#include "Arduino_ST7789_STM.h"
#include <limits.h>
#include "pins_arduino.h"
#include "wiring_private.h"
#include <SPI.h>
// Initialization commands for ST7789 240x240 1.3" IPS
// taken from Adafruit
static const uint8_t PROGMEM init_240x240[] = {
9, // 9 commands in list:
ST7789_SWRESET, ST_CMD_DELAY, // 1: Software reset, no args, w/delay
150, // 150 ms delay
ST7789_SLPOUT , ST_CMD_DELAY, // 2: Out of sleep mode, no args, w/delay
255, // 255 = 500 ms delay
ST7789_COLMOD , 1+ST_CMD_DELAY, // 3: Set color mode, 1 arg + delay:
0x55, // 16-bit color
10, // 10 ms delay
ST7789_MADCTL , 1, // 4: Memory access ctrl (directions), 1 arg:
0x00, // Row addr/col addr, bottom to top refresh
ST7789_CASET , 4, // 5: Column addr set, 4 args, no delay:
0,0, // XSTART = 0
0,240, // XEND = 240
ST7789_RASET , 4, // 6: Row addr set, 4 args, no delay:
0,0, // YSTART = 0
320>>8,320&0xff, // YEND = 240
ST7789_INVON , ST_CMD_DELAY, // 7: Inversion ON
10,
ST7789_NORON , ST_CMD_DELAY, // 8: Normal display on, no args, w/delay
10, // 10 ms delay
ST7789_DISPON , ST_CMD_DELAY, // 9: Main screen turn on, no args, w/delay
10
};
// macros for fast DC and CS state changes
#ifdef COMPATIBILITY_MODE
#define DC_DATA digitalWrite(dcPin, HIGH)
#define DC_COMMAND digitalWrite(dcPin, LOW)
#define CS_IDLE digitalWrite(csPin, HIGH)
#define CS_ACTIVE digitalWrite(csPin, LOW)
#else
#define DC_DATA digitalWrite(dcPin, HIGH)
#define DC_COMMAND digitalWrite(dcPin, LOW)
#define CS_IDLE digitalWrite(csPin, HIGH)
#define CS_ACTIVE digitalWrite(csPin, LOW)
#endif
// if CS always connected to the ground then don't do anything for better performance
#ifdef CS_ALWAYS_LOW
#define CS_IDLE
#define CS_ACTIVE
#endif
// ----------------------------------------------------------
Arduino_ST7789::Arduino_ST7789(int8_t dc, int8_t rst, int8_t cs) : Adafruit_GFX(ST7789_TFTWIDTH, ST7789_TFTHEIGHT)
{
csPin = cs;
dcPin = dc;
rstPin = rst;
}
// ----------------------------------------------------------
void Arduino_ST7789::init(uint16_t width, uint16_t height)
{
_ystart = _xstart = 0;
_colstart = _rowstart = 0;
pinMode(dcPin, OUTPUT);
#ifndef CS_ALWAYS_LOW
pinMode(csPin, OUTPUT);
#endif
SPI.beginTransaction(SPISettings(SPI_FREQ, MSBFIRST, SPI_MODE3, DATA_SIZE_8BIT));
CS_ACTIVE;
if(rstPin != -1) {
pinMode(rstPin, OUTPUT);
digitalWrite(rstPin, HIGH);
delay(50);
digitalWrite(rstPin, LOW);
delay(50);
digitalWrite(rstPin, HIGH);
delay(50);
}
if((width == 240) && (height == 240)) {
_colstart = 0;
_rowstart = 80;
} else {
_colstart = 0;
_rowstart = 0;
}
_width = width;
_height = height;
displayInit(init_240x240);
setRotation(2);
SPI.setDataSize(DATA_SIZE_16BIT);
}
// ----------------------------------------------------------
void Arduino_ST7789::writeCmd(uint16_t c)
{
DC_COMMAND;
CS_ACTIVE;
SPI.write(c);
CS_IDLE;
}
// ----------------------------------------------------------
void Arduino_ST7789::writeData(uint16_t c)
{
DC_DATA;
CS_ACTIVE;
SPI.write(c);
CS_IDLE;
}
// ----------------------------------------------------------
void Arduino_ST7789::displayInit(const uint8_t *addr)
{
uint8_t numCommands, numArgs;
uint16_t ms;
numCommands = pgm_read_byte(addr++); // Number of commands to follow
while(numCommands--) { // For each command...
writeCmd(pgm_read_byte(addr++)); // Read, issue command
numArgs = pgm_read_byte(addr++); // Number of args to follow
ms = numArgs & ST_CMD_DELAY; // If hibit set, delay follows args
numArgs &= ~ST_CMD_DELAY; // Mask out delay bit
while(numArgs--) writeData(pgm_read_byte(addr++));
if(ms) {
ms = pgm_read_byte(addr++); // Read post-command delay time (ms)
if(ms == 255) ms = 500; // If 255, delay for 500 ms
delay(ms);
}
}
}
// ----------------------------------------------------------
#define swap(a, b) { int16_t t = a; a = b; b = t; }
void Arduino_ST7789::setRotation(uint8_t m)
{
SPI.setDataSize(DATA_SIZE_8BIT);
writeCmd(ST7789_MADCTL);
rotation = m & 3;
switch (rotation) {
case 0:
writeData(ST7789_MADCTL_MX | ST7789_MADCTL_MY | ST7789_MADCTL_RGB);
_xstart = _colstart;
_ystart = _rowstart;
break;
case 1:
writeData(ST7789_MADCTL_MY | ST7789_MADCTL_MV | ST7789_MADCTL_RGB);
_ystart = _colstart;
_xstart = _rowstart;
swap(_width,_height);
break;
case 2:
writeData(ST7789_MADCTL_RGB);
_xstart = 0;
_ystart = 0;
break;
case 3:
writeData(ST7789_MADCTL_MX | ST7789_MADCTL_MV | ST7789_MADCTL_RGB);
_xstart = 0;
_ystart = 0;
swap(_width,_height);
break;
}
SPI.setDataSize(DATA_SIZE_16BIT);
}
// ----------------------------------------------------------
void Arduino_ST7789::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
{
x0+=_xstart; x1+=_xstart;
y0+=_ystart; y1+=_ystart;
CS_ACTIVE;
DC_COMMAND; SPI.write(ST7789_CASET);
DC_DATA; SPI.write(x0); SPI.write(x1);
DC_COMMAND; SPI.write(ST7789_RASET);
DC_DATA; SPI.write(y0); SPI.write(y1);
DC_COMMAND; SPI.write(ST7789_RAMWR);
CS_IDLE;
DC_DATA;
}
// ----------------------------------------------------------
void Arduino_ST7789::pushColor(uint16_t color)
{
DC_DATA;
CS_ACTIVE;
SPI.write(color);
CS_IDLE;
}
// ----------------------------------------------------------
void Arduino_ST7789::drawPixel(int16_t x, int16_t y, uint16_t color)
{
if(x<0 ||x>=_width || y<0 || y>=_height) return;
x+=_xstart;
y+=_ystart;
CS_ACTIVE;
DC_COMMAND; SPI.write(ST7789_CASET);
DC_DATA; SPI.write(x); SPI.write(x+1);
DC_COMMAND; SPI.write(ST7789_RASET);
DC_DATA; SPI.write(y); SPI.write(y+1);
DC_COMMAND; SPI.write(ST7789_RAMWR);
DC_DATA; SPI.write(color);
CS_IDLE;
}
// ----------------------------------------------------------
void Arduino_ST7789::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color)
{
if(x>=_width || y>=_height || h<=0) return;
if(y+h-1>=_height) h=_height-y;
if(h<2) { drawPixel(x, y, color); return; }
setAddrWindow(x, y, x, y+h-1);
CS_ACTIVE;
#ifndef COMPATIBILITY_MODE
if(h>DMA_MIN) {
dmaBuf[0] = color;
SPI.dmaSend(dmaBuf, h, 0);
} else
#endif
SPI.write(color, h);
CS_IDLE;
}
// ----------------------------------------------------------
void Arduino_ST7789::drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color)
{
if(x>=_width || y>=_height || w<=0) return;
if(x+w-1>=_width) w=_width-x;
if(w<2) { drawPixel(x, y, color); return; }
setAddrWindow(x, y, x+w-1, y);
CS_ACTIVE;
#ifndef COMPATIBILITY_MODE
if(w>DMA_MIN) {
dmaBuf[0] = color;
SPI.dmaSend(dmaBuf, w, 0);
} else
#endif
SPI.write(color, w);
CS_IDLE;
}
// ----------------------------------------------------------
void Arduino_ST7789::fillScreen(uint16_t color)
{
fillRect(0, 0, _width, _height, color);
}
// ----------------------------------------------------------
void Arduino_ST7789::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
{
if(x>=_width || y>=_height || w<=0 || h<=0) return;
if(x+w-1>=_width) w=_width -x;
if(y+h-1>=_height) h=_height-y;
setAddrWindow(x, y, x+w-1, y+h-1);
dmaBuf[0] = color;
CS_ACTIVE;
uint32_t num = w * h;
#ifndef COMPATIBILITY_MODE
if(num>DMA_MIN) {
while(num>DMA_MAX ) {
num -= DMA_MAX;
SPI.dmaSend(dmaBuf, DMA_MAX, 0);
}
SPI.dmaSend(dmaBuf, num, 0);
} else
#endif
SPI.write(color, num);
CS_IDLE;
}
// ----------------------------------------------------------
// draws image from RAM
void Arduino_ST7789::drawImage(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t *img16)
{
if(x>=_width || y>=_height || w<=0 || h<=0) return;
setAddrWindow(x, y, x+w-1, y+h-1);
CS_ACTIVE;
uint32_t num = (uint32_t)w*h;
SPI.write(img16, num);
CS_IDLE;
}
// ----------------------------------------------------------
// draws image from flash (PROGMEM)
void Arduino_ST7789::drawImageF(int16_t x, int16_t y, int16_t w, int16_t h, const uint16_t *img16)
{
if(x>=_width || y>=_height || w<=0 || h<=0) return;
setAddrWindow(x, y, x+w-1, y+h-1);
CS_ACTIVE;
uint32_t num = (uint32_t)w*h;
SPI.write(img16, num);
CS_IDLE;
}
// ----------------------------------------------------------
// Pass 8-bit (each) R,G,B, get back 16-bit packed color
uint16_t Arduino_ST7789::Color565(uint8_t r, uint8_t g, uint8_t b)
{
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}
// ----------------------------------------------------------
// ----------------------------------------------------------
void Arduino_ST7789::invertDisplay(boolean mode)
{
writeCmd(!mode ? ST7789_INVON : ST7789_INVOFF); // modes inverted
}
// ----------------------------------------------------------
void Arduino_ST7789::partialDisplay(boolean mode)
{
writeCmd(mode ? ST7789_PTLON : ST7789_NORON);
}
// ----------------------------------------------------------
void Arduino_ST7789::sleepDisplay(boolean mode)
{
writeCmd(mode ? ST7789_SLPIN : ST7789_SLPOUT);
delay(5);
}
// ----------------------------------------------------------
void Arduino_ST7789::enableDisplay(boolean mode)
{
writeCmd(mode ? ST7789_DISPON : ST7789_DISPOFF);
}
// ----------------------------------------------------------
void Arduino_ST7789::idleDisplay(boolean mode)
{
writeCmd(mode ? ST7789_IDMON : ST7789_IDMOFF);
}
// ----------------------------------------------------------
void Arduino_ST7789::resetDisplay()
{
writeCmd(ST7789_SWRESET);
delay(5);
}
// ----------------------------------------------------------
void Arduino_ST7789::setScrollArea(uint16_t tfa, uint16_t bfa)
{
uint16_t vsa = 320-tfa-bfa; // ST7789 320x240 VRAM
writeCmd(ST7789_VSCRDEF); // SETSCROLLAREA = 0x33
writeData(tfa);
writeData(vsa);
writeData(bfa);
}
// ----------------------------------------------------------
void Arduino_ST7789::setScroll(uint16_t vsp)
{
writeCmd(ST7789_VSCRSADD); // VSCRSADD = 0x37
writeData(vsp);
}
// ----------------------------------------------------------
void Arduino_ST7789::setPartArea(uint16_t sr, uint16_t er)
{
writeCmd(ST7789_PTLAR); // SETPARTAREA = 0x30
writeData(sr);
writeData(er);
}
// ----------------------------------------------------------
// doesn't work
void Arduino_ST7789::setBrightness(uint8_t br)
{
//writeCmd(ST7789_WRCACE);
//writeData(0xb1); // 80,90,b0, or 00,01,02,03
//writeCmd(ST7789_WRCABCMB);
//writeData(120);
//BCTRL=0x20, dd=0x08, bl=0x04
int val = 0x04;
writeCmd(ST7789_WRCTRLD);
writeData(val);
writeCmd(ST7789_WRDISBV);
writeData(br);
}
// ----------------------------------------------------------
// 0 - off
// 1 - idle
// 2 - normal
// 4 - display off
void Arduino_ST7789::powerSave(uint8_t mode)
{
if(mode==0) {
writeCmd(ST7789_POWSAVE);
writeData(0xec|3);
writeCmd(ST7789_DLPOFFSAVE);
writeData(0xff);
return;
}
int is = (mode&1) ? 0 : 1;
int ns = (mode&2) ? 0 : 2;
writeCmd(ST7789_POWSAVE);
writeData(0xec|ns|is);
if(mode&4) {
writeCmd(ST7789_DLPOFFSAVE);
writeData(0xfe);
}
}
// ------------------------------------------------
// Input a value 0 to 511 (85*6) to get a color value.
// The colours are a transition R - Y - G - C - B - M - R.
void Arduino_ST7789::rgbWheel(int idx, uint8_t *_r, uint8_t *_g, uint8_t *_b)
{
idx &= 0x1ff;
if(idx < 85) { // R->Y
*_r = 255; *_g = idx * 3; *_b = 0;
return;
} else if(idx < 85*2) { // Y->G
idx -= 85*1;
*_r = 255 - idx * 3; *_g = 255; *_b = 0;
return;
} else if(idx < 85*3) { // G->C
idx -= 85*2;
*_r = 0; *_g = 255; *_b = idx * 3;
return;
} else if(idx < 85*4) { // C->B
idx -= 85*3;
*_r = 0; *_g = 255 - idx * 3; *_b = 255;
return;
} else if(idx < 85*5) { // B->M
idx -= 85*4;
*_r = idx * 3; *_g = 0; *_b = 255;
return;
} else { // M->R
idx -= 85*5;
*_r = 255; *_g = 0; *_b = 255 - idx * 3;
return;
}
}
uint16_t Arduino_ST7789::rgbWheel(int idx)
{
uint8_t r,g,b;
rgbWheel(idx, &r,&g,&b);
return RGBto565(r,g,b);
}
// ------------------------------------------------