-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXanCraft21_ST7687.cpp
507 lines (430 loc) · 12.2 KB
/
XanCraft21_ST7687.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
/* Arduino library to drive the DFRobot 2.2 inch 128x128 round TFT LCD display with ST7687S driver.
*
* This library uses Adafruit_GFX and must be installed with this library in order for this to work.
*
* This display is getting very hard to find online and may be discontinued.
* If you can find the display by itself, it may be possible to make your own with a 74HC595 shift register.
* As of right now the only place to get this display by itself is only one seller on AliExpress, coming from china.
*
* Most of the credit goes to Adafruit for their graphics library and to DFRobot for their original library code.
*/
#include "XanCraft21_ST7687.h"
// Software SPI
XanCraft21_ST7687::XanCraft21_ST7687(int8_t cs_pin, int8_t dc_pin, int8_t mosi_pin, int8_t sclk_pin, int8_t lck_pin, int8_t wr_pin) : Adafruit_SPITFT(TFTWIDTH, TFTHEIGHT, cs_pin, dc_pin, mosi_pin, sclk_pin, -1, -1) {
_lck_pin = lck_pin;
_wr_pin = wr_pin;
}
// Hardware SPI
XanCraft21_ST7687::XanCraft21_ST7687(SPIClass *spi, int8_t cs_pin, int8_t dc_pin, int8_t lck_pin, int8_t wr_pin)
:
#if defined(ESP8266)
Adafruit_SPITFT(TFTWIDTH, TFTHEIGHT, cs_pin, dc_pin, -1) {
#elif defined(__SAM3X8E__)
Adafruit_SPITFT(TFTWIDTH, TFTHEIGHT, cs_pin, dc_pin, -1) {
#else
Adafruit_SPITFT(TFTWIDTH, TFTHEIGHT, spi, cs_pin, dc_pin, -1) {
#endif
_lck_pin = lck_pin;
_wr_pin = wr_pin;
}
// Destructor
XanCraft21_ST7687::~XanCraft21_ST7687(void) {}
// Init display
void XanCraft21_ST7687::begin(uint32_t freq) {
initSPI(freq);
pinMode(_lck_pin, OUTPUT);
pinMode(_wr_pin, OUTPUT);
digitalWrite(_lck_pin, LOW);
digitalWrite(_wr_pin, HIGH);
delay(120);
sendToDisplay(0xd7, false);
//writeClock();
sendToDisplay(0x9f, true);
//writeClock();
sendToDisplay(0xe0, false);
//writeClock();
sendToDisplay(0x00, true);
//writeClock();
delay(10);
sendToDisplay(0xfa, false);
//writeClock();
sendToDisplay(0x01, true);
//writeClock();
delay(20);
sendToDisplay(0xe3, false);
//writeClock();
delay(20);
sendToDisplay(0xe1, false);
//writeClock();
sendToDisplay(0x28, false);
//writeClock();
sendToDisplay(0x11, false);
//writeClock();
delay(30);
sendToDisplay(0xc0, false);
//writeClock();
sendToDisplay(0x17, true); // ctrL=0x1b 080416 5PCS 0X1E; 8PCS 0X2A
//writeClock();
sendToDisplay(0x01, true);
//writeClock();
sendToDisplay(0x25, false);
//writeClock();
sendToDisplay(0x1e, true);
//writeClock();
sendToDisplay(0xc3, false);
//writeClock();
sendToDisplay(0x03, true);
//writeClock();
sendToDisplay(0xc4, false);
//writeClock();
sendToDisplay(0x07, true);
//writeClock();
sendToDisplay(0xc5, false);
//writeClock();
sendToDisplay(0x01, true);
//writeClock();
sendToDisplay(0xcb, false);
//writeClock();
sendToDisplay(0x01, true);
//writeClock();
sendToDisplay(0xb7, false);
//writeClock();
sendToDisplay(0x00, true);
//writeClock();
sendToDisplay(0xd0, false);
//writeClock();
sendToDisplay(0x1d, true);
//writeClock();
sendToDisplay(0xb5, false);
//writeClock();
sendToDisplay(0x89, true);
//writeClock();
sendToDisplay(0xbd, false);
//writeClock();
sendToDisplay(0x02, true);
//writeClock();
sendToDisplay(0xf0, false);
//writeClock();
sendToDisplay(0x07, true);
//writeClock();
sendToDisplay(0x0c, true);
//writeClock();
sendToDisplay(0x0c, true);
//writeClock();
sendToDisplay(0x12, true);
//writeClock();
sendToDisplay(0xf4, false);
//writeClock();
sendToDisplay(0x33, true);
//writeClock();
sendToDisplay(0x33, true);
//writeClock();
sendToDisplay(0x33, true);
//writeClock();
sendToDisplay(0x00, true);
//writeClock();
sendToDisplay(0x33, true);
//writeClock();
sendToDisplay(0x66, true);
//writeClock();
sendToDisplay(0x66, true);
//writeClock();
sendToDisplay(0x66, true);
//writeClock();
sendToDisplay(0x20, false);
//writeClock();
sendToDisplay(0x2a, false);
//writeClock();
sendToDisplay(0x00, true);
//writeClock();
sendToDisplay(0x7f, true);
//writeClock();
sendToDisplay(0x2b, false);
//writeClock();
sendToDisplay(0x00, true);
//writeClock();
sendToDisplay(0x7f, true);
//writeClock();
sendToDisplay(0x3a, false);
//writeClock();
sendToDisplay(0x05, true);
//writeClock();
sendToDisplay(0x36, false);
//writeClock();
sendToDisplay(0x80, true); // 0xc3
//writeClock();
sendToDisplay(0xb0, false);
//writeClock();
sendToDisplay(0x7f, true);
//writeClock();
sendToDisplay(0x29, false);
//writeClock();
////////
sendToDisplay(0xf9, false);
//writeClock();
sendToDisplay(0x00, true);
//writeClock();
sendToDisplay(0x02, true);
//writeClock();
sendToDisplay(0x04, true);
//writeClock();
sendToDisplay(0x06, true);
//writeClock();
sendToDisplay(0x08, true);
//writeClock();
sendToDisplay(0x0a, true);
//writeClock();
sendToDisplay(0x0c, true);
//writeClock();
sendToDisplay(0x0e, true);
//writeClock();
sendToDisplay(0x10, true);
//writeClock();
sendToDisplay(0x12, true);
//writeClock();
sendToDisplay(0x14, true);
//writeClock();
sendToDisplay(0x16, true);
//writeClock();
sendToDisplay(0x18, true);
//writeClock();
sendToDisplay(0x1a, true);
//writeClock();
sendToDisplay(0x1c, true);
//writeClock();
sendToDisplay(0x1e, true);
//writeClock();
sendToDisplay(0x29, false);
//writeClock();
}
// Set address window
void XanCraft21_ST7687::setAddrWindow(uint16_t x1, uint16_t y1, uint16_t w, uint16_t h) {
uint16_t x2 = x1 + w - 1, y2 = y1 + h - 1;
sendToDisplay(0x2a, false);
//writeClock();
sendToDisplay(x1, true);
//writeClock();
sendToDisplay(x2, true);
//writeClock();
sendToDisplay(0x2b, false);
//writeClock();
sendToDisplay(y1, true);
//writeClock();
sendToDisplay(y2, true);
//writeClock();
sendToDisplay(0x2c, false);
//writeClock();
}
// Swap two 16-bit int values
#ifndef swapInt16
#define swapInt16(int_a, int_b) { \
int16_t temp = int_a; \
int_a = int_b; \
int_b = temp; \
}
#endif
// Fill the entire screen a solid color
/*void XanCraft21_ST7687::fillScreen(uint16_t color) {
fillRect(0, 0, _width, _height, color);
//fillRectS(0, 0, _width, _height, color);
}*/
// Draw a single pixel
void XanCraft21_ST7687::drawPixel(int16_t x, int16_t y, uint16_t color) {
// Clip first...
if ((x >= 0) && (x < _width) && (y >= 0) && (y < _height)) {
// THEN set up transaction (if needed) and draw...
startWrite();
setAddrWindow(x, y, 1, 1);
sendToDisplay16(color, true);
endWrite();
}
}
/*
// Draw a line from 2 points
void XanCraft21_ST7687::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color) {
if (x0 == x1) {
if (y0 > y1)
swapInt16(y0, y1);
drawFastVLine(x0, y0, y1 - y0 + 1, color);
} else if (y0 == y1) {
if (x0 > x1)
swapInt16(x0, x1);
drawFastHLine(x0, y0, x1 - x0 + 1, color);
} else {
startWrite();
writeLine(x0, y0, x1, y1, color);
endWrite();
}
}*/
// Draw a straight line from left to right
void XanCraft21_ST7687::drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color) {
startWrite();
writeLine(x, y, x + w - 1, y, color);
endWrite();
}
// Draw a straight line from top to bottom
void XanCraft21_ST7687::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) {
startWrite();
writeLine(x, y, x, y + h - 1, color);
endWrite();
}
// Draw a rectangle
void XanCraft21_ST7687::drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) {
startWrite();
writeFastHLine(x, y, w, color);
writeFastHLine(x, y + h - 1, w, color);
writeFastVLine(x, y, h, color);
writeFastVLine(x + w - 1, y, h, color);
//drawFastHLine(x, y, w, color);
//drawFastHLine(x, y + h - 1, w, color);
//drawFastVLine(x, y, h, color);
//drawFastVLine(x + w - 1, y, h, color);
endWrite();
}
// Fill a rectangle
void XanCraft21_ST7687::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) {
startWrite();
for (int16_t i = x; i < x + w; i++) {
writeFastVLine(i, y, h, color);
//drawFastVLine(i, y, h, color);
}
endWrite();
}
/*
// Draw a rectangle, modded
void XanCraft21_ST7687::drawRectS(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) {
startWrite();
writeFastHLine(x, y, w, color);
writeFastHLine(x, y + h - 1, w, color);
writeFastVLine(x, y, h, color);
writeFastVLine(x + w - 1, y, h, color);
//drawFastHLine(x, y, w, color);
//drawFastHLine(x, y + h - 1, w, color);
//drawFastVLine(x, y, h, color);
//drawFastVLine(x + w - 1, y, h, color);
endWrite();
}
// Fill a rectangle, modded
void XanCraft21_ST7687::fillRectS(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) {
startWrite();
for (int16_t i = x; i < x + w; i++) {
writeFastVLine(i, y, h, color);
//drawFastVLine(i, y, h, color);
}
endWrite();
}*/
// Write a single pixel
void XanCraft21_ST7687::writePixel(int16_t x, int16_t y, uint16_t color) {
if ((x >= 0) && (x < _width) && (y >= 0) && (y < _height)) {
setAddrWindow(x, y, 1, 1);
sendToDisplay16(color, true);
}
}
// Write a line from 2 points
void XanCraft21_ST7687::writeLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color) {
#if defined(ESP8266)
yield();
#endif
int16_t steep = abs(y1 - y0) > abs(x1 - x0);
if (steep) {
swapInt16(x0, y0);
swapInt16(x1, y1);
}
if (x0 > x1) {
swapInt16(x0, x1);
swapInt16(y0, y1);
}
int16_t dx, dy;
dx = x1 - x0;
dy = abs(y1 - y0);
int16_t err = dx / 2;
int16_t ystep;
if (y0 < y1) {
ystep = 1;
} else {
ystep = -1;
}
for (; x0 <= x1; x0++) {
if (steep) {
writePixel(y0, x0, color);
} else {
writePixel(x0, y0, color);
}
err -= dy;
if (err < 0) {
y0 += ystep;
err += dx;
}
}
}
// Write a straight line from left to right
void XanCraft21_ST7687::writeFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color) {
// Example: writeLine(x, y, x+w-1, y, color);
// or writeFillRect(x, y, w, 1, color);
//drawFastHLine(x, y, w, color);
//startWrite();
writeLine(x, y, x+w-1, y, color);
//endWrite();
}
// Write a straight line from top to bottom
void XanCraft21_ST7687::writeFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) {
// Can be just writeLine(x, y, x, y+h-1, color);
// or writeFillRect(x, y, 1, h, color);
//drawFastVLine(x, y, h, color);
//startWrite();
writeLine(x, y, x, y+h-1, color);
//endWrite();
}
// Write a filled rectangle
void XanCraft21_ST7687::writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) {
fillRect(x, y, w, h, color);
}
// Swap two 16-bit int values
//#ifndef swapInt16
//#define swapInt16(int_a, int_b) { \
// uint16_t temp = int_a; \
// int_a = int_b; \
// int_b = temp; \
//}
//#endif
//void XanCraft21_ST7687::swapInt16(int16_t int_a, int16_t int_b) {
// int16_t temp = int_a;
// int_a = int_b;
// int_b = temp;
//}
// Emulate sending data to the display with hardware SPI
void XanCraft21_ST7687::sendToDisplay(uint8_t _data, bool _dc) {
SPI_CS_LOW();
if (_dc) {
SPI_DC_HIGH();
} else {
SPI_DC_LOW();
}
//SPI.transfer(_data);
spiWrite(_data);
writeClock();
SPI_CS_HIGH();
}
// Emulate sending data to the display with hardware SPI, 16-bit
void XanCraft21_ST7687::sendToDisplay16(uint16_t _data, bool _dc) {
SPI_CS_LOW();
if (_dc) {
SPI_DC_HIGH();
} else {
SPI_DC_LOW();
}
//SPI.transfer(_data >> 8);
spiWrite(_data >> 8);
writeClock();
//SPI.transfer(_data);
spiWrite(_data);
writeClock();
SPI_CS_HIGH();
}
// Write commands and data to the display
void XanCraft21_ST7687::writeClock() {
digitalWrite(_lck_pin, HIGH);
digitalWrite(_lck_pin, LOW);
digitalWrite(_wr_pin, LOW);
digitalWrite(_wr_pin, HIGH);
}