-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathArduino_ST7789.cpp
More file actions
546 lines (451 loc) · 14.6 KB
/
Arduino_ST7789.cpp
File metadata and controls
546 lines (451 loc) · 14.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
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
/***************************************************
This is a library for the ST7789 IPS SPI display.
Written by Ananev Ilya.
****************************************************/
#include "Arduino_ST7789.h"
#include <limits.h>
#include "pins_arduino.h"
#include "wiring_private.h"
#include <SPI.h>
#define ST7789_CASET 0x2A
#define ST7789_RASET 0x2B
#define ST7789_RAMWR 0x2C
#define ST7789_RAMRD 0x2E
#define ST7789_COLMOD 0x3A
#define ST7789_MADCTL 0x36
static const uint8_t PROGMEM
cmd_240x240[] = { // Initialization commands for 7789 screens
10, // 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:
0x00, ST7789_240x240_XSTART, // XSTART = 0
(240+ST7789_240x240_XSTART) >> 8,
(240+ST7789_240x240_XSTART) & 0xFF, // XEND = 240
ST7789_RASET , 4, // 6: Row addr set, 4 args, no delay:
0x00, ST7789_240x240_YSTART, // YSTART = 0
(240+ST7789_240x240_YSTART) >> 8,
(240+ST7789_240x240_YSTART) & 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
255 }; // 255 = 500 ms delay
static const uint8_t PROGMEM
cmd_240x280[] = {
10,
ST7789_SWRESET, ST_CMD_DELAY, 150,
ST7789_SLPOUT , ST_CMD_DELAY, 255,
ST7789_COLMOD , 1+ST_CMD_DELAY, 0x55, 10,
ST7789_MADCTL , 1, 0x00,
ST7789_CASET , 4, 0x00, 0x00, 0x00, 239,
// RASET: start row = 20 (0x0014), end row = 299 (0x012B)
ST7789_RASET , 4, 0x00, 0x14, 0x01, 0x2B,
ST7789_INVON , ST_CMD_DELAY, 10,
ST7789_NORON , ST_CMD_DELAY, 10,
ST7789_DISPON , ST_CMD_DELAY, 255
};
inline uint16_t swapcolor(uint16_t x) {
return (x << 11) | (x & 0x07E0) | (x >> 11);
}
#if defined (SPI_HAS_TRANSACTION)
static SPISettings mySPISettings;
#elif defined (__AVR__) || defined(CORE_TEENSY)
static uint8_t SPCRbackup;
static uint8_t mySPCR;
#endif
#if defined (SPI_HAS_TRANSACTION)
#define SPI_BEGIN_TRANSACTION() if (_hwSPI) SPI.beginTransaction(mySPISettings)
#define SPI_END_TRANSACTION() if (_hwSPI) SPI.endTransaction()
#else
#define SPI_BEGIN_TRANSACTION() (void)
#define SPI_END_TRANSACTION() (void)
#endif
// Constructor when using software SPI. All output pins are configurable.
Arduino_ST7789::Arduino_ST7789(int8_t dc, int8_t rst, int8_t sid, int8_t sclk, int8_t cs)
: Adafruit_GFX(ST7789_TFTWIDTH_240, ST7789_TFTHEIGHT_240)
{
_cs = cs;
_dc = dc;
_sid = sid;
_sclk = sclk;
_rst = rst;
_hwSPI = false;
}
// Constructor when using hardware SPI. Faster, but must use SPI pins
// specific to each board type (e.g. 11,13 for Uno, 51,52 for Mega, etc.)
Arduino_ST7789::Arduino_ST7789(int8_t dc, int8_t rst, int8_t cs)
: Adafruit_GFX(ST7789_TFTWIDTH_240, ST7789_TFTHEIGHT_240) {
_cs = cs;
_dc = dc;
_rst = rst;
_hwSPI = true;
_sid = _sclk = -1;
}
inline void Arduino_ST7789::spiwrite(uint8_t c) {
//Serial.println(c, HEX);
if (_hwSPI) {
#if defined (SPI_HAS_TRANSACTION)
SPI.transfer(c);
#elif defined (__AVR__) || defined(CORE_TEENSY)
SPCRbackup = SPCR;
SPCR = mySPCR;
SPI.transfer(c);
SPCR = SPCRbackup;
#elif defined (__arm__)
SPI.setClockDivider(21); //4MHz
SPI.setDataMode(SPI_MODE2);
SPI.transfer(c);
#endif
} else {
// Fast SPI bitbang swiped from LPD8806 library
for(uint8_t bit = 0x80; bit; bit >>= 1) {
#if defined(USE_FAST_IO)
*clkport &= ~clkpinmask;
if(c & bit) *dataport |= datapinmask;
else *dataport &= ~datapinmask;
*clkport |= clkpinmask;
#else
digitalWrite(_sclk, LOW);
if(c & bit) digitalWrite(_sid, HIGH);
else digitalWrite(_sid, LOW);
digitalWrite(_sclk, HIGH);
#endif
}
}
}
void Arduino_ST7789::writecommand(uint8_t c) {
DC_LOW();
CS_LOW();
SPI_BEGIN_TRANSACTION();
spiwrite(c);
CS_HIGH();
SPI_END_TRANSACTION();
}
void Arduino_ST7789::writedata(uint8_t c) {
SPI_BEGIN_TRANSACTION();
DC_HIGH();
CS_LOW();
spiwrite(c);
CS_HIGH();
SPI_END_TRANSACTION();
}
// Companion code to the above tables. Reads and issues
// a series of LCD commands stored in PROGMEM byte array.
void Arduino_ST7789::displayInit(const uint8_t *addr) {
uint8_t numCommands, numArgs;
uint16_t ms;
//<-----------------------------------------------------------------------------------------
DC_HIGH();
#if defined(USE_FAST_IO)
*clkport |= clkpinmask;
#else
digitalWrite(_sclk, HIGH);
#endif
//<-----------------------------------------------------------------------------------------
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 & ST_CMD_DELAY; // If hibit set, delay follows args
numArgs &= ~ST_CMD_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)
if(ms == 255) ms = 500; // If 255, delay for 500 ms
delay(ms);
}
}
}
// Initialization code common to all ST7789 displays
void Arduino_ST7789::commonInit(const uint8_t *cmdList) {
_ystart = _xstart = 0;
_colstart = _rowstart = 0; // May be overridden in init func
pinMode(_dc, OUTPUT);
if(_cs) {
pinMode(_cs, OUTPUT);
}
#if defined(USE_FAST_IO)
dcport = portOutputRegister(digitalPinToPort(_dc));
dcpinmask = digitalPinToBitMask(_dc);
if(_cs) {
csport = portOutputRegister(digitalPinToPort(_cs));
cspinmask = digitalPinToBitMask(_cs);
}
#endif
if(_hwSPI) { // Using hardware SPI
#if defined (SPI_HAS_TRANSACTION)
SPI.begin();
mySPISettings = SPISettings(24000000, MSBFIRST, SPI_MODE2);
#elif defined (__AVR__) || defined(CORE_TEENSY)
SPCRbackup = SPCR;
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV4);
SPI.setDataMode(SPI_MODE2);
mySPCR = SPCR; // save our preferred state
SPCR = SPCRbackup; // then restore
#elif defined (__SAM3X8E__)
SPI.begin();
SPI.setClockDivider(21); //4MHz
SPI.setDataMode(SPI_MODE2);
#endif
} else {
pinMode(_sclk, OUTPUT);
pinMode(_sid , OUTPUT);
digitalWrite(_sclk, LOW);
digitalWrite(_sid, LOW);
#if defined(USE_FAST_IO)
clkport = portOutputRegister(digitalPinToPort(_sclk));
dataport = portOutputRegister(digitalPinToPort(_sid));
clkpinmask = digitalPinToBitMask(_sclk);
datapinmask = digitalPinToBitMask(_sid);
#endif
}
// toggle RST low to reset; CS low so it'll listen to us
CS_LOW();
if (_rst != -1) {
pinMode(_rst, OUTPUT);
digitalWrite(_rst, HIGH);
delay(50);
digitalWrite(_rst, LOW);
delay(50);
digitalWrite(_rst, HIGH);
delay(50);
}
if(cmdList)
displayInit(cmdList);
}
void Arduino_ST7789::setRotation(uint8_t m) {
writecommand(ST7789_MADCTL);
rotation = m % 4;
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;
break;
case 2:
writedata(ST7789_MADCTL_RGB);
_xstart = _colstart;
_ystart = _rowstart;
break;
case 3:
writedata(ST7789_MADCTL_MX | ST7789_MADCTL_MV | ST7789_MADCTL_RGB);
_ystart = _colstart;
_xstart = _rowstart;
break;
}
if (rotation & 1) {
// 90° или 270°
Adafruit_GFX::_width = _physical_height;
Adafruit_GFX::_height = _physical_width;
} else {
Adafruit_GFX::_width = _physical_width;
Adafruit_GFX::_height = _physical_height;
}
}
void Arduino_ST7789::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
uint16_t x_start = x0 + _xstart, x_end = x1 + _xstart;
uint16_t y_start = y0 + _ystart, y_end = y1 + _ystart;
writecommand(ST7789_CASET); // Column addr set
writedata(x_start >> 8);
writedata(x_start & 0xFF); // XSTART
writedata(x_end >> 8);
writedata(x_end & 0xFF); // XEND
writecommand(ST7789_RASET); // Row addr set
writedata(y_start >> 8);
writedata(y_start & 0xFF); // YSTART
writedata(y_end >> 8);
writedata(y_end & 0xFF); // YEND
writecommand(ST7789_RAMWR); // write to RAM
}
void Arduino_ST7789::pushColor(uint16_t color) {
SPI_BEGIN_TRANSACTION();
DC_HIGH();
CS_LOW();
spiwrite(color >> 8);
spiwrite(color);
CS_HIGH();
SPI_END_TRANSACTION();
}
void Arduino_ST7789::drawPixel(int16_t x, int16_t y, uint16_t color) {
if((x < 0) ||(x >= _width) || (y < 0) || (y >= _height)) return;
setAddrWindow(x,y,x+1,y+1);
SPI_BEGIN_TRANSACTION();
DC_HIGH();
CS_LOW();
spiwrite(color >> 8);
spiwrite(color);
CS_HIGH();
SPI_END_TRANSACTION();
}
void Arduino_ST7789::drawFastVLine(int16_t x, int16_t y, int16_t h,
uint16_t color) {
// Rudimentary clipping
if((x >= _width) || (y >= _height)) return;
if((y+h-1) >= _height) h = _height-y;
setAddrWindow(x, y, x, y+h-1);
uint8_t hi = color >> 8, lo = color;
SPI_BEGIN_TRANSACTION();
DC_HIGH();
CS_LOW();
while (h--) {
spiwrite(hi);
spiwrite(lo);
}
CS_HIGH();
SPI_END_TRANSACTION();
}
void Arduino_ST7789::drawFastHLine(int16_t x, int16_t y, int16_t w,
uint16_t color) {
// Rudimentary clipping
if((x >= _width) || (y >= _height)) return;
if((x+w-1) >= _width) w = _width-x;
setAddrWindow(x, y, x+w-1, y);
uint8_t hi = color >> 8, lo = color;
SPI_BEGIN_TRANSACTION();
DC_HIGH();
CS_LOW();
while (w--) {
spiwrite(hi);
spiwrite(lo);
}
CS_HIGH();
SPI_END_TRANSACTION();
}
void Arduino_ST7789::fillScreen(uint16_t color) {
fillRect(0, 0, _width, _height, color);
}
// fill a rectangle
void Arduino_ST7789::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color) {
// rudimentary clipping (drawChar w/big text requires this)
if((x >= _width) || (y >= _height)) 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);
uint8_t hi = color >> 8, lo = color;
SPI_BEGIN_TRANSACTION();
DC_HIGH();
CS_LOW();
for(y=h; y>0; y--) {
for(x=w; x>0; x--) {
spiwrite(hi);
spiwrite(lo);
}
}
CS_HIGH();
SPI_END_TRANSACTION();
}
// 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 i) {
writecommand(i ? ST7789_INVON : ST7789_INVOFF);
}
/******** low level bit twiddling **********/
inline void Arduino_ST7789::CS_HIGH(void) {
if(_cs) {
#if defined(USE_FAST_IO)
*csport |= cspinmask;
#else
digitalWrite(_cs, HIGH);
#endif
}
}
inline void Arduino_ST7789::CS_LOW(void) {
if(_cs) {
#if defined(USE_FAST_IO)
*csport &= ~cspinmask;
#else
digitalWrite(_cs, LOW);
#endif
}
}
inline void Arduino_ST7789::DC_HIGH(void) {
#if defined(USE_FAST_IO)
*dcport |= dcpinmask;
#else
digitalWrite(_dc, HIGH);
#endif
}
inline void Arduino_ST7789::DC_LOW(void) {
#if defined(USE_FAST_IO)
*dcport &= ~dcpinmask;
#else
digitalWrite(_dc, LOW);
#endif
}
void Arduino_ST7789::init(uint16_t width, uint16_t height) {
commonInit(NULL);
_physical_width = width;
_physical_height = height;
Adafruit_GFX::_width = width;
Adafruit_GFX::_height = height;
if (width == 240 && height == 280) {
_colstart = 0;
_rowstart = 20;
displayInit(cmd_240x280);
} else {
_colstart = 0;
_rowstart = 0;
displayInit(cmd_240x240);
}
setRotation(2);
}
// ----------------------------------------------------------
// fast method to send multiple 16-bit values from RAM via SPI
inline void Arduino_ST7789::copyMulti(uint8_t *img, uint16_t num)
{
while(num--) { writedata(*(img+1)); writedata(*(img+0)); img+=2; }
}
// ----------------------------------------------------------
// draws image from RAM, only basic clipping
void Arduino_ST7789::drawImage(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t *img16)
{
// all clipping should be on the application side
if(w<=0 || h<=0) return; // left for compatibility
//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);
copyMulti((uint8_t *)img16, w*h); // assumed w*h will never be >64k
}
// ----------------------------------------------------------
// 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);
uint32_t num = (uint32_t)w*h;
uint16_t num16 = num>>3;
uint8_t *img = (uint8_t *)img16;
while(num16--) {
writedata(pgm_read_byte(img+1)); writedata(pgm_read_byte(img+0)); img+=2;
writedata(pgm_read_byte(img+1)); writedata(pgm_read_byte(img+0)); img+=2;
writedata(pgm_read_byte(img+1)); writedata(pgm_read_byte(img+0)); img+=2;
writedata(pgm_read_byte(img+1)); writedata(pgm_read_byte(img+0)); img+=2;
writedata(pgm_read_byte(img+1)); writedata(pgm_read_byte(img+0)); img+=2;
writedata(pgm_read_byte(img+1)); writedata(pgm_read_byte(img+0)); img+=2;
writedata(pgm_read_byte(img+1)); writedata(pgm_read_byte(img+0)); img+=2;
writedata(pgm_read_byte(img+1)); writedata(pgm_read_byte(img+0)); img+=2;
}
uint8_t num8 = num & 0x7;
while(num8--) { writedata(pgm_read_byte(img+1)); writedata(pgm_read_byte(img+0)); img+=2; }
}