-
Notifications
You must be signed in to change notification settings - Fork 38
/
TFTv2_extended.cpp
702 lines (604 loc) · 17 KB
/
TFTv2_extended.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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/*
Edited by João Vilaça.
Currently an work in progress, a few changes were done:
- LED pin moved to pin 9 on Atmega 328;
- Screen orientation rotated 90/270º on initialization;
- Added a few new methods;
- fixed spelling errors on method names
- removed dead code
- ...
*/
/*
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_extended.h"
#include <SPI.h>
#define FONT_SPACE 6
#define FONT_X 8
void TFT::sendCMD(INT8U index)
{
TFT_DC_LOW;
TFT_CS_LOW;
SPI.transfer(index);
TFT_CS_HIGH;
}
void TFT::sendDATA(INT8U data)
{
TFT_DC_HIGH;
TFT_CS_LOW;
SPI.transfer(data);
TFT_CS_HIGH;
}
void TFT::sendData(INT16U data)
{
INT8U data1 = data>>8;
INT8U data2 = data&0xff;
TFT_DC_HIGH;
TFT_CS_LOW;
SPI.transfer(data1);
SPI.transfer(data2);
TFT_CS_HIGH;
}
/*
void TFT::sendPackage(INT16U *data, INT8U howmany)
{
INT16U data1 = 0;
INT8U data2 = 0;
TFT_DC_HIGH;
TFT_CS_LOW;
INT8U count=0;
for(count=0;count<howmany;count++)
{
data1 = data[count]>>8;
data2 = data[count]&0xff;
SPI.transfer(data1);
SPI.transfer(data2);
}
TFT_CS_HIGH;
}
*/
INT8U TFT::readRegister(INT8U Addr, INT8U xParameter)
{
INT8U data=0;
sendCMD(0xd9); /* ext command */
sendDATA(0x10+xParameter); /* 0x11 is the first Parameter */
TFT_DC_LOW;
TFT_CS_LOW;
SPI.transfer(Addr);
TFT_DC_HIGH;
data = SPI.transfer(0);
TFT_CS_HIGH;
return data;
}
void TFT::init (void)
{
SPI.begin();
TFT_CS_HIGH;
TFT_DC_HIGH;
INT8U i=0, TFTDriver=0;
TFT_RST_ON;
delay(10);
TFT_RST_OFF;
for(i=0;i<3;i++)
{
TFTDriver = readID();
}
sendCMD(0xCB);
sendDATA(0x39);
sendDATA(0x2C);
sendDATA(0x00);
sendDATA(0x34);
sendDATA(0x02);
sendCMD(0xCF);
sendDATA(0x00);
sendDATA(0XC1);
sendDATA(0X30);
sendCMD(0xE8);
sendDATA(0x85);
sendDATA(0x00);
sendDATA(0x78);
sendCMD(0xEA);
sendDATA(0x00);
sendDATA(0x00);
sendCMD(0xED);
sendDATA(0x64);
sendDATA(0x03);
sendDATA(0X12);
sendDATA(0X81);
sendCMD(0xF7);
sendDATA(0x20);
sendCMD(0xC0); //Power control
sendDATA(0x23); //VRH[5:0]
sendCMD(0xC1); //Power control
sendDATA(0x10); //SAP[2:0];BT[3:0]
sendCMD(0xC5); //VCM control
sendDATA(0x3e); //Contrast
sendDATA(0x28);
sendCMD(0xC7); //VCM control2
sendDATA(0x86); //--
sendCMD(0x36); // Memory Access Control
//orientation
sendDATA(0xE8); //C8 //48 68绔栧睆//28 E8 妯睆
sendCMD(0x3A);
sendDATA(0x55); // 16 bit pixel
sendCMD(0xB1);
sendDATA(0x00);
sendDATA(0x18);
sendCMD(0xB6); // Display Function Control
sendDATA(0x08);
sendDATA(0x82);
sendDATA(0x27);
sendCMD(0xF2); // 3Gamma Function Disable
sendDATA(0x00);
sendCMD(0x26); //Gamma curve selected
sendDATA(0x01);
sendCMD(0xE0); //Set Gamma
sendDATA(0x0F);
sendDATA(0x31);
sendDATA(0x2B);
sendDATA(0x0C);
sendDATA(0x0E);
sendDATA(0x08);
sendDATA(0x4E);
sendDATA(0xF1);
sendDATA(0x37);
sendDATA(0x07);
sendDATA(0x10);
sendDATA(0x03);
sendDATA(0x0E);
sendDATA(0x09);
sendDATA(0x00);
sendCMD(0XE1); //Set Gamma
sendDATA(0x00);
sendDATA(0x0E);
sendDATA(0x14);
sendDATA(0x03);
sendDATA(0x11);
sendDATA(0x07);
sendDATA(0x31);
sendDATA(0xC1);
sendDATA(0x48);
sendDATA(0x08);
sendDATA(0x0F);
sendDATA(0x0C);
sendDATA(0x31);
sendDATA(0x36);
sendDATA(0x0F);
sendCMD(0x11); //Exit Sleep
delay(120);
sendCMD(0x29); //Display on
sendCMD(0x2c);
}
INT8U TFT::readID(void)
{
INT8U i=0;
INT8U data[3] ;
INT8U ID[3] = {0x00, 0x93, 0x41};
INT8U ToF=1;
for(i=0;i<3;i++)
{
data[i]=readRegister(0xd3,i+1);
if(data[i] != ID[i])
{
ToF=0;
}
}
/*
if(!ToF)
{
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 color)
{
Tft.setCol(0, MAX_X);
Tft.setPage(0, MAX_Y);
Tft.sendCMD(0x2c); /* start to write to display ra */
/* m */
TFT_DC_HIGH;
TFT_CS_LOW;
INT8U Hcolor = color>>8;
INT8U Lcolor = color&0xff;
for(INT16U i=0; i<(MAX_X+1)/2*(MAX_Y+1); i++)
{
SPI.transfer(Hcolor);
SPI.transfer(Lcolor);
SPI.transfer(Hcolor);
SPI.transfer(Lcolor);
}
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)
{
setXY(poX, poY);
sendData(color);
}
void TFT::drawChar( INT8U ascii, INT16U poX, INT16U poY,INT16U size, INT16U fgcolor)
{
if((ascii>=32)&&(ascii<=127))
{
;
}
else
{
ascii = '?'-32;
}
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 */
}
}
}
void TFT::drawCenteredString(char *string,INT16U poy, INT16U size,INT16U fgcolor)
{
int len = strlen (string)* FONT_SPACE * size;
int left = (MAX_X - len ) / 2;
drawString( string, left, poy, size, fgcolor);
}
void TFT::drawStringWithShadow(char *string,INT16U poX, INT16U poY, INT16U size,INT16U fgcolor, INT16U shcolor)
{
while(*string)
{
drawChar(*string, poX-1, poY+1, size, shcolor);
drawChar(*string, poX, poY, size, fgcolor);
*string++;
if(poX < MAX_X)
{
poX += FONT_SPACE*size; /* Move cursor right */
}
}
}
void TFT::fillRectangle(INT16U poX, INT16U poY, INT16U width, INT16U height, INT16U color)
{
INT16U XL = poX;
INT16U XR = poX+width;
INT16U YU = poY;
INT16U YD = poY+height;
unsigned long XY=0;
unsigned long i=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); /* start to write to display ra */
/* m */
TFT_DC_HIGH;
TFT_CS_LOW;
INT8U Hcolor = color>>8;
INT8U Lcolor = color&0xff;
for(i=0; i < XY; i++)
{
SPI.transfer(Hcolor);
SPI.transfer(Lcolor);
}
TFT_CS_HIGH;
}
void TFT::fillRectangleUseBevel(INT16U x, INT16U y, INT16U w, INT16U h, INT16U color)
{
INT16U x2 = x + w;
INT16U y2 = y + h;
if ( x2 > MAX_X ) x2 = MAX_X;
if ( y2 > MAX_Y ) y2 = MAX_Y;
Tft.setCol(x,x2);
Tft.setPage(y, y2);
Tft.sendCMD(0x2c);
TFT_DC_HIGH;
TFT_CS_LOW;
INT8U Hcolor = color>>8;
INT8U Lcolor = color&0xff;
int t = ( x2 - x ) * (y2-y);
for(int j=0; j < t; j++)
{
SPI.transfer(Hcolor);
SPI.transfer(Lcolor);
}
TFT_CS_HIGH;
}
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::drawRectangle(INT16U poX, INT16U poY, INT16U length, INT16U width,INT16U color, byte thickness)
{
for (int i = 0; i < thickness - 1; i++)
{
drawHorizontalLine(poX + i, poY + i, length - i * 2, color);
drawHorizontalLine(poX + i, poY + width - i, length -i * 2, color);
drawVerticalLine(poX + i, poY + i, width -i * 2,color);
drawVerticalLine(poX + length - i, poY + i, width -i * 2,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;
}
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
*********************************************************************************************************/