forked from Jana-Marie/TFT_ILI9163C
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TFT_ILI9163C.cpp
413 lines (354 loc) · 8.91 KB
/
TFT_ILI9163C.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
#include "TFT_ILI9163C.h"
#include <limits.h>
#include "pins_arduino.h"
#include "wiring_private.h"
#include <SPI.h>
#if defined(SPI_HAS_TRANSACTION)
static SPISettings ILI9163C_SPI;
#endif
#define MEMSIZE 128*128*2
#if defined(FRAMEBUFFER)
uint8_t fbuff[MEMSIZE] __attribute__((aligned()));
#endif
TFT_ILI9163C::TFT_ILI9163C(uint8_t cspin,uint8_t dcpin,uint8_t rstpin) : Adafruit_GFX(_TFTWIDTH,_TFTHEIGHT)
{
_cs = cspin;
_rs = dcpin;
_rst = rstpin;
}
void TFT_ILI9163C::writecommand(uint8_t c)
{
digitalWrite(_rs,LOW);
digitalWrite(_cs,LOW);
SPI.transfer(c);
digitalWrite(_cs,HIGH);
digitalWrite(_rs,HIGH);
}
void TFT_ILI9163C::writedata(uint8_t c)
{
digitalWrite(_cs,LOW);
SPI.transfer(c);
digitalWrite(_cs,HIGH);
}
void TFT_ILI9163C::writedata16(uint16_t d)
{
digitalWrite(_cs,LOW);
SPI.transfer(d >> 8);
SPI.transfer(d);
digitalWrite(_cs,HIGH);
}
void TFT_ILI9163C::setBitrate(uint32_t n)
{
//nop
}
void TFT_ILI9163C::begin(void)
{
sleep = 0;
_initError = 0b00000000;
pinMode(_rs, OUTPUT);
pinMode(_cs, OUTPUT);
SPI.begin();
SPI.setHwCs(false);
ILI9163C_SPI = SPISettings(32000000, MSBFIRST, SPI_MODE0);//
SPI.setFrequency(32000000);
digitalWrite(_cs, LOW);
if (_rst != 255) {
pinMode(_rst, OUTPUT);
digitalWrite(_rst, HIGH);
delay(500);
digitalWrite(_rst, LOW);
delay(500);
digitalWrite(_rst, HIGH);
delay(500);
}
/*
7) MY: 1(bottom to top), 0(top to bottom) Row Address Order
6) MX: 1(R to L), 0(L to R) Column Address Order
5) MV: 1(Exchanged), 0(normal) Row/Column exchange
4) ML: 1(bottom to top), 0(top to bottom) Vertical Refresh Order
3) RGB: 1(BGR), 0(RGB) Color Space
2) MH: 1(R to L), 0(L to R) Horizontal Refresh Order
1)
0)
MY, MX, MV, ML,RGB, MH, D1, D0
0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 //normal
1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 //Y-Mirror
0 | 1 | 0 | 0 | 1 | 0 | 0 | 0 //X-Mirror
1 | 1 | 0 | 0 | 1 | 0 | 0 | 0 //X-Y-Mirror
0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 //X-Y Exchange
1 | 0 | 1 | 0 | 1 | 0 | 0 | 0 //X-Y Exchange, Y-Mirror
0 | 1 | 1 | 0 | 1 | 0 | 0 | 0 //XY exchange
1 | 1 | 1 | 0 | 1 | 0 | 0 | 0
*/
_Mactrl_Data = 0b00000000;
_colorspaceData = __COLORSPC;//start with default data;
chipInit();
}
uint8_t TFT_ILI9163C::errorCode(void)
{
return _initError;
}
void TFT_ILI9163C::chipInit() {
uint8_t i;
writecommand(CMD_SWRESET);//software reset
delay(500);
writecommand(CMD_SLPOUT);//exit sleep
delay(5);
writecommand(CMD_PIXFMT);//Set Color Format 16bit
writedata(0x05);
delay(5);
writecommand(CMD_GAMMASET);//default gamma curve 3
writedata(0x04);//0x04
delay(1);
writecommand(CMD_GAMRSEL);//Enable Gamma adj
writedata(0x01);
delay(1);
writecommand(CMD_NORML);
writecommand(CMD_DFUNCTR);
writedata(0b11111111);//
writedata(0b00000110);//
// meh not that great
/*
writecommand(CMD_PGAMMAC);//Positive Gamma Correction Setting
for (i=0;i<15;i++){
writedata(pGammaSet[i]);
}
writecommand(CMD_NGAMMAC);//Negative Gamma Correction Setting
for (i=0;i<15;i++){
writedata(nGammaSet[i]);
}
*/
writecommand(CMD_FRMCTR1);//Frame Rate Control (In normal mode/Full colors)
writedata(0x08);//0x0C//0x08
writedata(0x02);//0x14//0x08
delay(1);
writecommand(CMD_DINVCTR);//display inversion
writedata(0x07);
delay(1);
writecommand(CMD_PWCTR1);//Set VRH1[4:0] & VC[2:0] for VCI1 & GVDD
writedata(0x0A);//4.30 - 0x0A
writedata(0x02);//0x05
delay(1);
writecommand(CMD_PWCTR2);//Set BT[2:0] for AVDD & VCL & VGH & VGL
writedata(0x02);
delay(1);
writecommand(CMD_VCOMCTR1);//Set VMH[6:0] & VML[6:0] for VOMH & VCOML
writedata(0x50);//0x50
writedata(99);//0x5b
delay(1);
writecommand(CMD_VCOMOFFS);
writedata(0);//0x40
delay(1);
writecommand(CMD_CLMADRS);//Set Column Address
writedata16(0x00);
writedata16(_GRAMWIDTH);
writecommand(CMD_PGEADRS);//Set Page Address
writedata16(0X00);
writedata16(_GRAMHEIGH);
// set scroll area (thanks Masuda)
writecommand(CMD_VSCLLDEF);
writedata16(__OFFSET);
writedata16(_GRAMHEIGH - __OFFSET);
writedata16(0);
colorSpace(_colorspaceData);
setRotation(0);
writecommand(CMD_DISPON);//display ON
delay(1);
writecommand(CMD_RAMWR);//Memory Write
delay(1);
fillScreen(BLACK);
}
/*
Colorspace selection:
0: RGB
1: GBR
*/
void TFT_ILI9163C::colorSpace(uint8_t cspace) {
if (cspace < 1){
bitClear(_Mactrl_Data,3);
} else {
bitSet(_Mactrl_Data,3);
}
}
void TFT_ILI9163C::invertDisplay(boolean i) {
writecommand(i ? CMD_DINVON : CMD_DINVOF);
}
void TFT_ILI9163C::display(boolean onOff) {
if (onOff){
writecommand(CMD_DISPON);
} else {
writecommand(CMD_DISPOFF);
}
}
void TFT_ILI9163C::idleMode(boolean onOff) {
if (onOff){
writecommand(CMD_IDLEON);
} else {
writecommand(CMD_IDLEOF);
}
}
void TFT_ILI9163C::sleepMode(boolean mode) {
if (mode){
if (sleep == 1) return;//already sleeping
sleep = 1;
writecommand(CMD_SLPIN);
delay(5);//needed
} else {
if (sleep == 0) return; //Already awake
sleep = 0;
writecommand(CMD_SLPOUT);
delay(120);//needed
}
}
void TFT_ILI9163C::defineScrollArea(uint16_t tfa, uint16_t bfa){
tfa += __OFFSET;
int16_t vsa = _GRAMHEIGH - tfa - bfa;
if (vsa >= 0) {
writecommand(CMD_VSCLLDEF);
writedata16(tfa);
writedata16(vsa);
writedata16(bfa);
}
}
void TFT_ILI9163C::scroll(uint16_t adrs) {
if (adrs <= _GRAMHEIGH) {
writecommand(CMD_VSSTADRS);
writedata16(adrs + __OFFSET);
}
}
void TFT_ILI9163C::startPushData(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
setAddr(x0,y0,x1,y1);
}
void TFT_ILI9163C::pushData(uint16_t color) {
writedata16(color);
}
void TFT_ILI9163C::writeScreen24(const uint32_t *bitmap,uint16_t size) {
uint16_t color;
uint16_t px;
writecommand(CMD_RAMWR);
for (px = 0;px < size; px++){
color = Color24To565(bitmap[px]);
writedata16(color);
}
homeAddress();
}
#if defined(FRAMEBUFFER)
void TFT_ILI9163C::writeFramebuffer() {
homeAddress();
writeScreen16(fbuff,MEMSIZE);
}
#else
void TFT_ILI9163C::writeFramebuffer() {
//nop
}
#endif
void TFT_ILI9163C::writeRow(uint16_t row, uint16_t row_start, uint16_t row_len, uint8_t * data) {
#ifndef FRAMEBUFFER
setAddrWindow(row_start, row, row_start + row_len, row+1);
writeScreen16(data, row_len * 2);
#else
for (uint16_t i = 0; i < row_len; ++i) {
drawPixel(i + row_start, row, data[i * 2] << 8 | data[i * 2 + 1]);
}
#endif
}
#ifndef FRAMEBUFFER
void TFT_ILI9163C::drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color) {
uint8_t buf[w * 2];
uint16_t color_sw = uint8_t(color >> 8) | uint8_t(color) << 8;
for (int16_t i = 0; i < w; ++i) {
((uint16_t*)buf)[i] = color_sw;
}
writeRow(y, x, w, buf);
}
void TFT_ILI9163C::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) {
// Update in subclasses if desired!
startWrite();
for (int16_t i=y; i<y+h; i++) {
writeFastHLine(x, i, w, color);
}
endWrite();
}
#endif
void TFT_ILI9163C::writeScreen16(uint8_t *bitmap,uint32_t size) {
writecommand(CMD_RAMWR);
digitalWrite(_rs,HIGH);
digitalWrite(_cs,LOW);
SPI.writeBytes(bitmap,size);
digitalWrite(_cs,HIGH);
homeAddress();
}
void TFT_ILI9163C::homeAddress() {
setAddrWindow(0x00,0x00,_GRAMWIDTH,_GRAMHEIGH);
}
void TFT_ILI9163C::setCursor(int16_t x, int16_t y) {
if (boundaryCheck(x,y)) return;
setAddrWindow(0x00,0x00,x,y);
cursor_x = x;
cursor_y = y;
}
void TFT_ILI9163C::drawPixel(int16_t x, int16_t y, uint16_t color) {
if (boundaryCheck(x,y)) return;
if ((x < 0) || (y < 0)) return;
#if defined(FRAMEBUFFER)
fbuff[((128 * y) + x) * 2] = color >> 8;
fbuff[(((128 * y) + x) * 2) + 1] = color;
#else
setAddr(x,y,x+1,y+1);
writedata16(color);
#endif
}
bool TFT_ILI9163C::boundaryCheck(int16_t x,int16_t y){
if ((x >= _width) || (y >= _height)) return true;
return false;
}
void TFT_ILI9163C::setAddr(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1){
setAddrWindow(x0,y0,x1,y1);
}
void TFT_ILI9163C::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
writecommand(CMD_CLMADRS); // Column
if (rotation == 0 || rotation > 1){
writedata16(x0);
writedata16(x1);
} else {
writedata16(x0 + __OFFSET);
writedata16(x1 + __OFFSET);
}
writecommand(CMD_PGEADRS); // Page
if (rotation == 0){
writedata16(y0 + __OFFSET);
writedata16(y1 + __OFFSET);
} else {
writedata16(y0);
writedata16(y1);
}
writecommand(CMD_RAMWR); //Into RAM
}
void TFT_ILI9163C::setRotation(uint8_t m) {
rotation = m % 4; // can't be higher than 3
switch (rotation) {
case 0:
_Mactrl_Data = 0b00001000;
_width = _TFTWIDTH;
_height = _TFTHEIGHT;//-__OFFSET;
break;
case 1:
_Mactrl_Data = 0b01101000;
_width = _TFTHEIGHT;//-__OFFSET;
_height = _TFTWIDTH;
break;
case 2:
_Mactrl_Data = 0b11001000;
_width = _TFTWIDTH;
_height = _TFTHEIGHT;//-__OFFSET;
break;
case 3:
_Mactrl_Data = 0b10101000;
_width = _TFTWIDTH;
_height = _TFTHEIGHT;//-__OFFSET;
break;
}
colorSpace(_colorspaceData);
writecommand(CMD_MADCTL);
writedata(_Mactrl_Data);
}