forked from Seeed-Studio/TFT_Touch_Shield_V2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TFTv2.cpp
626 lines (533 loc) · 16.2 KB
/
TFTv2.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
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
2012 Copyright (c) Seeed Technology Inc.
Authors: Albert.Miao & Loovee,
Visweswara R (with initializtion code from TFT vendor)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc.,51 Franklin St,Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <TFTv2.h>
#include <SPI.h>
#define FONT_SPACE 6
#define FONT_X 8
#define FONT_Y 8
#if defined(__LINKIT_ONE__)
uint8_t bg_buffer[153600];
#endif
void TFT::TFTinit (void)
{
SPI.begin();
#if defined(__LINKIT_ONE__)
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
digitalWrite(5, LOW);
digitalWrite(7, HIGH);
#elif defined(__arc__) /* Arduino101/Genuino101 specifics */
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
digitalWrite(5, LOW);
digitalWrite(7, HIGH);
#endif
TFT_CS_HIGH;
TFT_DC_HIGH;
INT8U i=0, TFTDriver=0;
for(i=0;i<3;i++)
{
TFTDriver = readID();
}
delay(500);
sendCMD(0x01);
delay(200);
sendCMD(0xCF);
WRITE_DATA(0x00);
WRITE_DATA(0x8B);
WRITE_DATA(0X30);
sendCMD(0xED);
WRITE_DATA(0x67);
WRITE_DATA(0x03);
WRITE_DATA(0X12);
WRITE_DATA(0X81);
sendCMD(0xE8);
WRITE_DATA(0x85);
WRITE_DATA(0x10);
WRITE_DATA(0x7A);
sendCMD(0xCB);
WRITE_DATA(0x39);
WRITE_DATA(0x2C);
WRITE_DATA(0x00);
WRITE_DATA(0x34);
WRITE_DATA(0x02);
sendCMD(0xF7);
WRITE_DATA(0x20);
sendCMD(0xEA);
WRITE_DATA(0x00);
WRITE_DATA(0x00);
sendCMD(0xC0); /* Power control */
WRITE_DATA(0x1B); /* VRH[5:0] */
sendCMD(0xC1); /* Power control */
WRITE_DATA(0x10); /* SAP[2:0];BT[3:0] */
sendCMD(0xC5); /* VCM control */
WRITE_DATA(0x3F);
WRITE_DATA(0x3C);
sendCMD(0xC7); /* VCM control2 */
WRITE_DATA(0XB7);
sendCMD(0x36); /* Memory Access Control */
WRITE_DATA(0x08);
sendCMD(0x3A);
WRITE_DATA(0x55);
sendCMD(0xB1);
WRITE_DATA(0x00);
WRITE_DATA(0x1B);
sendCMD(0xB6); /* Display Function Control */
WRITE_DATA(0x0A);
WRITE_DATA(0xA2);
sendCMD(0xF2); /* 3Gamma Function Disable */
WRITE_DATA(0x00);
sendCMD(0x26); /* Gamma curve selected */
WRITE_DATA(0x01);
sendCMD(0xE0); /* Set Gamma */
WRITE_DATA(0x0F);
WRITE_DATA(0x2A);
WRITE_DATA(0x28);
WRITE_DATA(0x08);
WRITE_DATA(0x0E);
WRITE_DATA(0x08);
WRITE_DATA(0x54);
WRITE_DATA(0XA9);
WRITE_DATA(0x43);
WRITE_DATA(0x0A);
WRITE_DATA(0x0F);
WRITE_DATA(0x00);
WRITE_DATA(0x00);
WRITE_DATA(0x00);
WRITE_DATA(0x00);
sendCMD(0XE1); /* Set Gamma */
WRITE_DATA(0x00);
WRITE_DATA(0x15);
WRITE_DATA(0x17);
WRITE_DATA(0x07);
WRITE_DATA(0x11);
WRITE_DATA(0x06);
WRITE_DATA(0x2B);
WRITE_DATA(0x56);
WRITE_DATA(0x3C);
WRITE_DATA(0x05);
WRITE_DATA(0x10);
WRITE_DATA(0x0F);
WRITE_DATA(0x3F);
WRITE_DATA(0x3F);
WRITE_DATA(0x0F);
sendCMD(0x11); /* Exit Sleep */
delay(120);
sendCMD(0x29); /* Display on */
fillScreen();
}
INT8U TFT::readID(void)
{
#if defined(__LINKIT_ONE__)
return 1;
#elif defined(__arc__) /* Arduino101/Genuino101 specifics */
return 1;
#endif
INT8U i=0;
INT8U data[3] ;
INT8U ID[3] = {0x00, 0x93, 0x41};
INT8U ToF=1;
for(i=0;i<3;i++)
{
data[i]=Read_Register(0xd3,i+1);
if(data[i] != ID[i])
{
ToF=0;
}
}
if(!ToF) /* data!=ID */
{
Serial.print("Read TFT ID failed, ID should be 0x09341, but read ID = 0x");
for(i=0;i<3;i++)
{
Serial.print(data[i],HEX);
}
Serial.println();
}
return ToF;
}
void TFT::setCol(INT16U StartCol,INT16U EndCol)
{
sendCMD(0x2A); /* Column Command address */
sendData(StartCol);
sendData(EndCol);
}
void TFT::setPage(INT16U StartPage,INT16U EndPage)
{
sendCMD(0x2B); /* Column Command address */
sendData(StartPage);
sendData(EndPage);
}
void TFT::fillScreen(INT16U XL, INT16U XR, INT16U YU, INT16U YD, INT16U color)
{
unsigned long XY = 0;
if(XL > XR)
{
XL = XL^XR;
XR = XL^XR;
XL = XL^XR;
}
if(YU > YD)
{
YU = YU^YD;
YD = YU^YD;
YU = YU^YD;
}
XL = constrain(XL, MIN_X,MAX_X);
XR = constrain(XR, MIN_X,MAX_X);
YU = constrain(YU, MIN_Y,MAX_Y);
YD = constrain(YD, MIN_Y,MAX_Y);
XY = (XR-XL+1);
XY = XY*(YD-YU+1);
Tft.setCol(XL, XR);
Tft.setPage(YU, YD);
Tft.sendCMD(0x2c);
TFT_DC_HIGH;
TFT_CS_LOW;
INT8U Hcolor = (color>>8)&0xff;
INT8U Lcolor = color&0xff;
#if defined(__LINKIT_ONE__)
for(unsigned long i=0; i<XY; i++)
{
bg_buffer[2*i] = Lcolor; //
bg_buffer[2*i+1] = Hcolor;
}
SPI.write(bg_buffer, 2*XY);
#else
for(unsigned long i=0; i < XY; i++)
{
SPI.transfer(Hcolor);
SPI.transfer(Lcolor);
}
#endif
TFT_CS_HIGH;
}
void TFT::fillScreen(void)
{
#if defined(__LINKIT_ONE__)
memset(bg_buffer, 0, 153600);
#endif
Tft.setCol(0, 239);
Tft.setPage(0, 319);
Tft.sendCMD(0x2c); /* start to write to display ram */
TFT_DC_HIGH;
TFT_CS_LOW;
#if defined(__LINKIT_ONE__)
SPI.write(bg_buffer, 153600);
#else
for(INT16U i=0; i<38400; i++)
{
SPI.transfer(0);
SPI.transfer(0);
SPI.transfer(0);
SPI.transfer(0);
}
#endif
TFT_CS_HIGH;
}
void TFT::setXY(INT16U poX, INT16U poY)
{
setCol(poX, poX);
setPage(poY, poY);
sendCMD(0x2c);
}
void TFT::setPixel(INT16U poX, INT16U poY,INT16U color)
{
sendCMD(0x2A); /* Column Command address */
sendData(poX);
sendData(poX);
sendCMD(0x2B); /* Column Command address */
sendData(poY);
sendData(poY);
sendCMD(0x2c);
sendData(color);
}
void TFT::drawChar( INT8U ascii, INT16U poX, INT16U poY,INT16U size, INT16U fgcolor)
{
if((ascii>=32)&&(ascii<=127))
{
;
}
else
{
ascii = '?';
}
for (int i =0; i<FONT_X; i++ ) {
INT8U temp = pgm_read_byte(&simpleFont[ascii-0x20][i]);
for(INT8U f=0;f<8;f++)
{
if((temp>>f)&0x01)
{
fillRectangle(poX+i*size, poY+f*size, size, size, fgcolor);
}
}
}
}
void TFT::drawString(char *string,INT16U poX, INT16U poY, INT16U size,INT16U fgcolor)
{
while(*string)
{
drawChar(*string, poX, poY, size, fgcolor);
*string++;
if(poX < MAX_X)
{
poX += FONT_SPACE*size; /* Move cursor right */
}
}
}
//fillRectangle(poX+i*size, poY+f*size, size, size, fgcolor);
void TFT::fillRectangle(INT16U poX, INT16U poY, INT16U length, INT16U width, INT16U color)
{
fillScreen(poX, poX+length, poY, poY+width, color);
}
void TFT::drawHorizontalLine( INT16U poX, INT16U poY,
INT16U length,INT16U color)
{
setCol(poX,poX + length);
setPage(poY,poY);
sendCMD(0x2c);
for(int i=0; i<length; i++)
sendData(color);
}
void TFT::drawLine( INT16U x0,INT16U y0,INT16U x1, INT16U y1,INT16U color)
{
int x = x1-x0;
int y = y1-y0;
int dx = abs(x), sx = x0<x1 ? 1 : -1;
int dy = -abs(y), sy = y0<y1 ? 1 : -1;
int err = dx+dy, e2; /* error value e_xy */
for (;;)
{ /* loop */
setPixel(x0,y0,color);
e2 = 2*err;
if (e2 >= dy) { /* e_xy+e_x > 0 */
if (x0 == x1) break;
err += dy; x0 += sx;
}
if (e2 <= dx) { /* e_xy+e_y < 0 */
if (y0 == y1) break;
err += dx; y0 += sy;
}
}
}
void TFT::drawVerticalLine( INT16U poX, INT16U poY, INT16U length,INT16U color)
{
setCol(poX,poX);
setPage(poY,poY+length);
sendCMD(0x2c);
for(int i=0; i<length; i++)
sendData(color);
}
void TFT::drawRectangle(INT16U poX, INT16U poY, INT16U length, INT16U width,INT16U color)
{
drawHorizontalLine(poX, poY, length, color);
drawHorizontalLine(poX, poY+width, length, color);
drawVerticalLine(poX, poY, width,color);
drawVerticalLine(poX + length, poY, width,color);
}
void TFT::drawCircle(int poX, int poY, int r,INT16U color)
{
int x = -r, y = 0, err = 2-2*r, e2;
do {
setPixel(poX-x, poY+y,color);
setPixel(poX+x, poY+y,color);
setPixel(poX+x, poY-y,color);
setPixel(poX-x, poY-y,color);
e2 = err;
if (e2 <= y) {
err += ++y*2+1;
if (-x == y && e2 <= x) e2 = 0;
}
if (e2 > x) err += ++x*2+1;
} while (x <= 0);
}
void TFT::fillCircle(int poX, int poY, int r,INT16U color)
{
int x = -r, y = 0, err = 2-2*r, e2;
do {
drawVerticalLine(poX-x, poY-y, 2*y, color);
drawVerticalLine(poX+x, poY-y, 2*y, color);
e2 = err;
if (e2 <= y) {
err += ++y*2+1;
if (-x == y && e2 <= x) e2 = 0;
}
if (e2 > x) err += ++x*2+1;
} while (x <= 0);
}
void TFT::drawTriangle( int poX1, int poY1, int poX2, int poY2, int poX3, int poY3, INT16U color)
{
drawLine(poX1, poY1, poX2, poY2,color);
drawLine(poX1, poY1, poX3, poY3,color);
drawLine(poX2, poY2, poX3, poY3,color);
}
INT8U TFT::drawNumber(long long_num,INT16U poX, INT16U poY,INT16U size,INT16U fgcolor)
{
INT8U char_buffer[10] = "";
INT8U i = 0;
INT8U f = 0;
if (long_num < 0)
{
f=1;
drawChar('-',poX, poY, size, fgcolor);
long_num = -long_num;
if(poX < MAX_X)
{
poX += FONT_SPACE*size; /* Move cursor right */
}
}
else if (long_num == 0)
{
f=1;
drawChar('0',poX, poY, size, fgcolor);
return f;
if(poX < MAX_X)
{
poX += FONT_SPACE*size; /* Move cursor right */
}
}
while (long_num > 0)
{
char_buffer[i++] = long_num % 10;
long_num /= 10;
}
f = f+i;
for(; i > 0; i--)
{
drawChar('0'+ char_buffer[i - 1],poX, poY, size, fgcolor);
if(poX < MAX_X)
{
poX+=FONT_SPACE*size; /* Move cursor right */
}
}
return f;
}
INT8U TFT::drawFloat(float floatNumber,INT8U decimal,INT16U poX, INT16U poY,INT16U size,INT16U fgcolor)
{
INT16U temp=0;
float decy=0.0;
float rounding = 0.5;
INT8U f=0;
if(floatNumber<0.0)
{
drawChar('-',poX, poY, size, fgcolor);
floatNumber = -floatNumber;
if(poX < MAX_X)
{
poX+=FONT_SPACE*size; /* Move cursor right */
}
f =1;
}
for (INT8U i=0; i<decimal; ++i)
{
rounding /= 10.0;
}
floatNumber += rounding;
temp = (INT16U)floatNumber;
INT8U howlong=drawNumber(temp,poX, poY, size, fgcolor);
f += howlong;
if((poX+8*size*howlong) < MAX_X)
{
poX+=FONT_SPACE*size*howlong; /* Move cursor right */
}
if(decimal>0)
{
drawChar('.',poX, poY, size, fgcolor);
if(poX < MAX_X)
{
poX+=FONT_SPACE*size; /* Move cursor right */
}
f +=1;
}
decy = floatNumber-temp; /* decimal part, 4 */
for(INT8U i=0;i<decimal;i++)
{
decy *=10; /* for the next decimal */
temp = decy; /* get the decimal */
drawNumber(temp,poX, poY, size, fgcolor);
floatNumber = -floatNumber;
if(poX < MAX_X)
{
poX+=FONT_SPACE*size; /* Move cursor right */
}
decy -= temp;
}
f +=decimal;
return f;
}
INT8U TFT::drawFloat(float floatNumber,INT16U poX, INT16U poY,INT16U size,INT16U fgcolor)
{
INT8U decimal=2;
INT16U temp=0;
float decy=0.0;
float rounding = 0.5;
INT8U f=0;
if(floatNumber<0.0) /* floatNumber < 0 */
{
drawChar('-',poX, poY, size, fgcolor); /* add a '-' */
floatNumber = -floatNumber;
if(poX < MAX_X)
{
poX+=FONT_SPACE*size; /* Move cursor right */
}
f =1;
}
for (INT8U i=0; i<decimal; ++i)
{
rounding /= 10.0;
}
floatNumber += rounding;
temp = (INT16U)floatNumber;
INT8U howlong=drawNumber(temp,poX, poY, size, fgcolor);
f += howlong;
if((poX+8*size*howlong) < MAX_X)
{
poX+=FONT_SPACE*size*howlong; /* Move cursor right */
}
if(decimal>0)
{
drawChar('.',poX, poY, size, fgcolor);
if(poX < MAX_X)
{
poX += FONT_SPACE*size; /* Move cursor right */
}
f +=1;
}
decy = floatNumber-temp; /* decimal part, */
for(INT8U i=0;i<decimal;i++)
{
decy *=10; /* for the next decimal */
temp = decy; /* get the decimal */
drawNumber(temp,poX, poY, size, fgcolor);
floatNumber = -floatNumber;
if(poX < MAX_X)
{
poX += FONT_SPACE*size; /* Move cursor right */
}
decy -= temp;
}
f += decimal;
return f;
}
TFT Tft=TFT();
/*********************************************************************************************************
END FILE
*********************************************************************************************************/