forked from Bodmer/TFT_HX8357
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TFT_HX8357.cpp
2629 lines (2317 loc) · 78.9 KB
/
TFT_HX8357.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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************
Arduino TFT graphics library targetted at the Mega
board when used with one of these two display shields
from Banggood China:
3.2 Inch 320 X 480 TFT LCD Display Module Support Arduino Mega2560
http://www.banggood.com/3_2-Inch-320-X-480-TFT-LCD-Display-Module-Support-Arduino-Mega2560-p-963574.html
or:
3.0 Inch 320 X 480 TFT LCD Display Module Support Arduino Mega2560
http://www.banggood.com/3_0-Inch-320-X-480-TFT-LCD-Display-Module-Support-Arduino-Mega2560-p-963573.html
These displays are also available with a Mega board:
http://www.banggood.com/Mega2560-R3-Board-With-USB-Cable-3_2-Inch-TFT-LCD-Display-Module-p-965164.html
http://www.banggood.com/Mega2560-R3-Board-With-USB-Cable-3_0-Inch-TFT-LCD-Display-Module-p-967224.html
This library has been derived from the Adafruit_GFX
library and the associated driver library. See text
at the end of this file.
This is a standalone library that contains the
hardware driver, the graphics funtions and the
proportional fonts.
This library also contains a set of fonts for fast
rendering. The larger fonts are Run Length Encoded
to reduce their size.
Added 2/1/16
This library now includes the full font set of custom
fonts from the GFX library.
****************************************************/
#include "TFT_HX8357.h"
#include <avr/pgmspace.h>
/***************************************************************************************
** Function name: TFT_HX8357
** Description: Constructor , we must use hardware SPI pins
***************************************************************************************/
TFT_HX8357::TFT_HX8357(int16_t w, int16_t h)
{
_cs = 40; //PORT G bit _BV(1)
#ifdef FAST_RS
_rs = 4; //PORT G bit _BV(5)
#else
_rs = 38; //PORT D bit _BV(7)
#endif
_rst = 41; //PORT G bit _BV(0)
_wr = 39; //PORT G bit _BV(2)
_fcs = 44; //FLASH chip select?
pinMode(_rst, OUTPUT);
digitalWrite(_rst, HIGH);
pinMode(_rs, OUTPUT);
pinMode(_cs, OUTPUT);
pinMode(_wr, OUTPUT);
digitalWrite(_rs, HIGH);
#ifndef KEEP_CS_LOW
digitalWrite(_cs, HIGH);
#else
digitalWrite(_cs, LOW);
#endif
digitalWrite(_wr, HIGH);
pinMode(_fcs, OUTPUT);
digitalWrite(_fcs, HIGH); // Stop line floating
DDRA = 0xFF; // Set direction for the 2 8 bit data ports
DDRC = 0xFF;
_width = w;
_height = h;
rotation = 0;
cursor_y = cursor_x = 0;
textfont = 1;
textsize = 1;
textcolor = 0xFFFF;
textbgcolor = 0x0000;
padX = 0;
textwrap = true;
textdatum = TL_DATUM; // Top left text datum is default
fontsloaded = 0;
#ifdef LOAD_GLCD
fontsloaded = 0x0002; // Bit 1 set
#endif
#ifdef LOAD_FONT2
fontsloaded |= 0x0004; // Bit 2 set
#endif
#ifdef LOAD_FONT4
fontsloaded |= 0x0010; // Bit 4 set
#endif
#ifdef LOAD_FONT6
fontsloaded |= 0x0040; // Bit 6 set
#endif
#ifdef LOAD_FONT7
fontsloaded |= 0x0080; // Bit 7 set
#endif
#ifdef LOAD_FONT8
fontsloaded |= 0x0100; // Bit 8 set
#endif
#ifdef LOAD_GFXFF
gfxFont = NULL; // Set the font to the GLCD
#endif
}
/***************************************************************************************
** Function name: writecommand
** Description: Send an 8 bit command to the TFT
***************************************************************************************/
void TFT_HX8357::writecommand(uint8_t c)
{
SETUP_CS_L;
RS_L;
PORTA = 0;
PORTC = c;
WR_STB;
RS_H;
SETUP_CS_H;
}
/***************************************************************************************
** Function name: writedata
** Description: Send a 8 bit data value to the TFT
***************************************************************************************/
void TFT_HX8357::writedata(uint8_t c)
{
SETUP_CS_L;
PORTA = c>>8;
PORTC = c;
WR_STB;
SETUP_CS_H;
}
/***************************************************************************************
** Function name: begin
** Description: Included for backwards compatibility
***************************************************************************************/
void TFT_HX8357::begin(void)
{
init();
}
/***************************************************************************************
** Function name: init
** Description: Reset, then initialise the TFT display registers
***************************************************************************************/
void TFT_HX8357::init(void)
{
// toggle RST low to reset
digitalWrite(_rst, HIGH);
delay(50);
digitalWrite(_rst, LOW);
delay(10);
digitalWrite(_rst, HIGH);
delay(10);
#ifndef HX8357C
#ifdef ILI9486
writecommand(0x01);
writedata(0x00);
delay(50);
writecommand(0x28);
writedata(0x00);
writecommand(0xC0); // Power Control 1
writedata(0x0d);
writedata(0x0d);
writecommand(0xC1); // Power Control 2
writedata(0x43);
writedata(0x00);
writecommand(0xC2); // Power Control 3
writedata(0x00);
writecommand(0xC5); // VCOM Control
writedata(0x00);
writedata(0x48);
writecommand(0xB6); // Display Function Control
writedata(0x00);
writedata(0x22); // 0x42 = Rotate display 180 deg.
writedata(0x3B);
writecommand(0xE0); // PGAMCTRL (Positive Gamma Control)
writedata(0x0f);
writedata(0x24);
writedata(0x1c);
writedata(0x0a);
writedata(0x0f);
writedata(0x08);
writedata(0x43);
writedata(0x88);
writedata(0x32);
writedata(0x0f);
writedata(0x10);
writedata(0x06);
writedata(0x0f);
writedata(0x07);
writedata(0x00);
writecommand(0xE1); // NGAMCTRL (Negative Gamma Control)
writedata(0x0F);
writedata(0x38);
writedata(0x30);
writedata(0x09);
writedata(0x0f);
writedata(0x0f);
writedata(0x4e);
writedata(0x77);
writedata(0x3c);
writedata(0x07);
writedata(0x10);
writedata(0x05);
writedata(0x23);
writedata(0x1b);
writedata(0x00);
writecommand(0x20); // Display Inversion OFF, 0x21 = ON
writecommand(0x36); // Memory Access Control
writedata(0x0A);
writecommand(0x3A); // Interface Pixel Format
writedata(0x55);
writecommand(0x11);
delay(150);
writecommand(0x29);
delay(25);
#else
// Configure HX8357-B display
writecommand(0x11);
delay(20);
writecommand(0xD0);
writedata(0x07);
writedata(0x42);
writedata(0x18);
writecommand(0xD1);
writedata(0x00);
writedata(0x07);
writedata(0x10);
writecommand(0xD2);
writedata(0x01);
writedata(0x02);
writecommand(0xC0);
writedata(0x10);
writedata(0x3B);
writedata(0x00);
writedata(0x02);
writedata(0x11);
writecommand(0xC5);
writedata(0x08);
writecommand(0xC8);
writedata(0x00);
writedata(0x32);
writedata(0x36);
writedata(0x45);
writedata(0x06);
writedata(0x16);
writedata(0x37);
writedata(0x75);
writedata(0x77);
writedata(0x54);
writedata(0x0C);
writedata(0x00);
writecommand(0x36);
writedata(0x0a);
writecommand(0x3A);
writedata(0x55);
writecommand(0x2A);
writedata(0x00);
writedata(0x00);
writedata(0x01);
writedata(0x3F);
writecommand(0x2B);
writedata(0x00);
writedata(0x00);
writedata(0x01);
writedata(0xDF);
delay(120);
writecommand(0x29);
delay(25);
// End of HX8357-B display configuration
#endif
#else
// HX8357-C display initialisation
writecommand(0xB9); // Enable extension command
writedata(0xFF);
writedata(0x83);
writedata(0x57);
delay(50);
writecommand(0xB6); //Set VCOM voltage
writedata(0x2C); //0x52 for HSD 3.0"
writecommand(0x11); // Sleep off
delay(200);
writecommand(0x35); // Tearing effect on
writedata(0x00); // Added parameter
writecommand(0x3A); // Interface pixel format
writedata(0x55); // 16 bits per pixel
//writecommand(0xCC); // Set panel characteristic
//writedata(0x09); // S960>S1, G1>G480, R-G-B, normally black
//writecommand(0xB3); // RGB interface
//writedata(0x43);
//writedata(0x00);
//writedata(0x06);
//writedata(0x06);
writecommand(0xB1); // Power control
writedata(0x00);
writedata(0x15);
writedata(0x0D);
writedata(0x0D);
writedata(0x83);
writedata(0x48);
writecommand(0xC0); // Does this do anything?
writedata(0x24);
writedata(0x24);
writedata(0x01);
writedata(0x3C);
writedata(0xC8);
writedata(0x08);
writecommand(0xB4); // Display cycle
writedata(0x02);
writedata(0x40);
writedata(0x00);
writedata(0x2A);
writedata(0x2A);
writedata(0x0D);
writedata(0x4F);
writecommand(0xE0); // Gamma curve
writedata(0x00);
writedata(0x15);
writedata(0x1D);
writedata(0x2A);
writedata(0x31);
writedata(0x42);
writedata(0x4C);
writedata(0x53);
writedata(0x45);
writedata(0x40);
writedata(0x3B);
writedata(0x32);
writedata(0x2E);
writedata(0x28);
writedata(0x24);
writedata(0x03);
writedata(0x00);
writedata(0x15);
writedata(0x1D);
writedata(0x2A);
writedata(0x31);
writedata(0x42);
writedata(0x4C);
writedata(0x53);
writedata(0x45);
writedata(0x40);
writedata(0x3B);
writedata(0x32);
writedata(0x2E);
writedata(0x28);
writedata(0x24);
writedata(0x03);
writedata(0x00);
writedata(0x01);
writecommand(0x36); // MADCTL Memory access control
writedata(0x48);
delay(20);
writecommand(0x21); //Display inversion on
delay(20);
writecommand(0x29); // Display on
delay(120);
#endif
#ifdef KEEP_CS_LOW
SETUP_CS_L;
#endif
}
/***************************************************************************************
** Function name: drawCircle
** Description: Draw a circle outline
***************************************************************************************/
void TFT_HX8357::drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color)
{
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = - r - r;
int16_t x = 0;
drawPixel(x0 , y0 + r, color);
drawPixel(x0 , y0 - r, color);
drawPixel(x0 + r, y0, color);
drawPixel(x0 - r, y0, color);
while (x < r) {
if (f >= 0) {
r--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
drawPixel(x0 + x, y0 + r, color);
drawPixel(x0 - x, y0 + r, color);
drawPixel(x0 + x, y0 - r, color);
drawPixel(x0 - x, y0 - r, color);
drawPixel(x0 + r, y0 + x, color);
drawPixel(x0 - r, y0 + x, color);
drawPixel(x0 + r, y0 - x, color);
drawPixel(x0 - r, y0 - x, color);
}
}
/***************************************************************************************
** Function name: drawCircleHelper
** Description: Support function for circle drawing
***************************************************************************************/
void TFT_HX8357::drawCircleHelper( int16_t x0, int16_t y0, int16_t r, uint8_t cornername, uint16_t color)
{
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -r -r;
int16_t x = 0;
while (x < r) {
if (f >= 0) {
r--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
if (cornername & 0x4) {
drawPixel(x0 + x, y0 + r, color);
drawPixel(x0 + r, y0 + x, color);
}
if (cornername & 0x2) {
drawPixel(x0 + x, y0 - r, color);
drawPixel(x0 + r, y0 - x, color);
}
if (cornername & 0x8) {
drawPixel(x0 - r, y0 + x, color);
drawPixel(x0 - x, y0 + r, color);
}
if (cornername & 0x1) {
drawPixel(x0 - r, y0 - x, color);
drawPixel(x0 - x, y0 - r, color);
}
}
}
/***************************************************************************************
** Function name: fillCircle
** Description: draw a filled circle
***************************************************************************************/
void TFT_HX8357::fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color)
{
drawFastVLine(x0, y0 - r, r + r + 1, color);
fillCircleHelper(x0, y0, r, 3, 0, color);
}
/***************************************************************************************
** Function name: fillCircleHelper
** Description: Support function for filled circle drawing
***************************************************************************************/
// Used to do circles and roundrects
void TFT_HX8357::fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, uint16_t color)
{
int16_t f = 1 - r;
int16_t ddF_x = 1;
int16_t ddF_y = -r - r;
int16_t x = 0;
delta++;
while (x < r) {
if (f >= 0) {
r--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
if (cornername & 0x1) {
drawFastVLine(x0 + x, y0 - r, r + r + delta, color);
drawFastVLine(x0 + r, y0 - x, x + x + delta, color);
}
if (cornername & 0x2) {
drawFastVLine(x0 - x, y0 - r, r + r + delta, color);
drawFastVLine(x0 - r, y0 - x, x + x + delta, color);
}
}
}
/***************************************************************************************
** Function name: drawEllipse
** Description: Draw a ellipse outline
***************************************************************************************/
void TFT_HX8357::drawEllipse(int16_t x0, int16_t y0, int16_t rx, int16_t ry, uint16_t color)
{
if (rx<2) return;
if (ry<2) return;
int16_t x, y;
int32_t rx2 = (int32_t)rx * (int32_t)rx;
int32_t ry2 = (int32_t)ry * (int32_t)ry;
int32_t fx2 = 4 * rx2;
int32_t fy2 = 4 * ry2;
int32_t s;
for (x = 0, y = ry, s = 2*ry2+rx2*(1-2*ry); ry2*x <= rx2*y; x++)
{
drawPixel(x0 + x, y0 + y, color);
drawPixel(x0 - x, y0 + y, color);
drawPixel(x0 + x, y0 - y, color);
drawPixel(x0 - x, y0 - y, color);
if (s >= 0)
{
s += fx2 * (1 - y);
y--;
}
s += ry2 * ((4 * x) + 6);
}
for (x = rx, y = 0, s = 2*rx2+ry2*(1-2*rx); rx2*y <= ry2*x; y++)
{
drawPixel(x0 + x, y0 + y, color);
drawPixel(x0 - x, y0 + y, color);
drawPixel(x0 + x, y0 - y, color);
drawPixel(x0 - x, y0 - y, color);
if (s >= 0)
{
s += fy2 * (1 - x);
x--;
}
s += rx2 * ((4 * y) + 6);
}
}
/***************************************************************************************
** Function name: fillEllipse
** Description: draw a filled ellipse
***************************************************************************************/
void TFT_HX8357::fillEllipse(int16_t x0, int16_t y0, int16_t rx, int16_t ry, uint16_t color)
{
if (rx<2) return;
if (ry<2) return;
int16_t x, y;
int32_t rx2 = (int32_t)rx * (int32_t)rx;
int32_t ry2 = (int32_t)ry * (int32_t)ry;
int32_t fx2 = 4 * rx2;
int32_t fy2 = 4 * ry2;
int32_t s;
for (x = 0, y = ry, s = 2*ry2+rx2*(1-2*ry); ry2*x <= rx2*y; x++)
{
drawFastHLine(x0 - x, y0 - y, x + x + 1, color);
drawFastHLine(x0 - x, y0 + y, x + x + 1, color);
if (s >= 0)
{
s += fx2 * (1 - y);
y--;
}
s += ry2 * ((4 * x) + 6);
}
for (x = rx, y = 0, s = 2*rx2+ry2*(1-2*rx); rx2*y <= ry2*x; y++)
{
drawFastHLine(x0 - x, y0 - y, x + x + 1, color);
drawFastHLine(x0 - x, y0 + y, x + x + 1, color);
if (s >= 0)
{
s += fy2 * (1 - x);
x--;
}
s += rx2 * ((4 * y) + 6);
}
}
/***************************************************************************************
** Function name: fillScreen
** Description: Clear the screen to defined colour
***************************************************************************************/
void TFT_HX8357::fillScreen(uint16_t color)
{
fillRect(0, 0, _width, _height, color);
}
/***************************************************************************************
** Function name: drawRect
** Description: Draw a rectangle outline
***************************************************************************************/
// Draw a rectangle
void TFT_HX8357::drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t 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);
}
/***************************************************************************************
** Function name: drawRoundRect
** Description: Draw a rounded corner rectangle outline
***************************************************************************************/
// Draw a rounded rectangle
void TFT_HX8357::drawRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, uint16_t color)
{
// smarter version
drawFastHLine(x + r , y , w - r - r, color); // Top
drawFastHLine(x + r , y + h - 1, w - r - r, color); // Bottom
drawFastVLine(x , y + r , h - r - r, color); // Left
drawFastVLine(x + w - 1, y + r , h - r - r, color); // Right
// draw four corners
drawCircleHelper(x + r , y + r , r, 1, color);
drawCircleHelper(x + w - r - 1, y + r , r, 2, color);
drawCircleHelper(x + w - r - 1, y + h - r - 1, r, 4, color);
drawCircleHelper(x + r , y + h - r - 1, r, 8, color);
}
/***************************************************************************************
** Function name: fillRoundRect
** Description: Draw a rounded corner filled rectangle
***************************************************************************************/
// Fill a rounded rectangle
void TFT_HX8357::fillRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, uint16_t color)
{
// smarter version
fillRect(x + r, y, w - r - r, h, color);
// draw four corners
fillCircleHelper(x + w - r - 1, y + r, r, 1, h - r - r - 1, color);
fillCircleHelper(x + r , y + r, r, 2, h - r - r - 1, color);
}
/***************************************************************************************
** Function name: drawTriangle
** Description: Draw a triangle outline using 3 arbitrary points
***************************************************************************************/
// Draw a triangle
void TFT_HX8357::drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color)
{
drawLine(x0, y0, x1, y1, color);
drawLine(x1, y1, x2, y2, color);
drawLine(x2, y2, x0, y0, color);
}
/***************************************************************************************
** Function name: fillTriangle
** Description: Draw a filled triangle using 3 arbitrary points
***************************************************************************************/
// Fill a triangle - uses faster Bressenham method (original Adafruit function overflowed on big triangles!)
void TFT_HX8357::fillTriangle (int16_t x1,int16_t y1,int16_t x2,int16_t y2,int16_t x3,int16_t y3, uint16_t c)
{
int16_t t1x,t2x,y,minx,maxx,t1xp,t2xp;
bool changed1 = false;
bool changed2 = false;
int16_t signx1,signx2,dx1,dy1,dx2,dy2;
uint16_t e1,e2;
// Sort vertices
if (y1>y2) { swap(y1,y2); swap(x1,x2); }
if (y1>y3) { swap(y1,y3); swap(x1,x3); }
if (y2>y3) { swap(y2,y3); swap(x2,x3); }
t1x=t2x=x1; y=y1; // Starting points
dx1 = x2 - x1; if(dx1<0) { dx1=-dx1; signx1=-1; } else signx1=1;
dy1 = y2 - y1;
dx2 = x3 - x1; if(dx2<0) { dx2=-dx2; signx2=-1; } else signx2=1;
dy2 = y3 - y1;
if (dy1 > dx1) { // swap values
swap(dx1,dy1);
changed1 = true;
}
if (dy2 > dx2) { // swap values
swap(dy2,dx2);
changed2 = true;
}
e2 = dx2>>1;
// Flat top, just process the second half
if(y1==y2) goto next;
e1 = dx1>>1;
for (uint16_t i = 0; i < dx1;) {
t1xp=0; t2xp=0;
if(t1x<t2x) { minx=t1x; maxx=t2x; }
else { minx=t2x; maxx=t1x; }
// process first line until y value is about to change
while(i<dx1) {
i++;
e1 += dy1;
while (e1 >= dx1) {
e1 -= dx1;
if (changed1) t1xp=signx1;//t1x += signx1;
else goto next1;
}
if (changed1) break;
else t1x += signx1;
}
// Move line
next1:
// process second line until y value is about to change
while (1) {
e2 += dy2;
while (e2 >= dx2) {
e2 -= dx2;
if (changed2) t2xp=signx2;//t2x += signx2;
else goto next2;
}
if (changed2) break;
else t2x += signx2;
}
next2:
if(minx>t1x) minx=t1x; if(minx>t2x) minx=t2x;
if(maxx<t1x) maxx=t1x; if(maxx<t2x) maxx=t2x;
drawFastHLine(minx, y, maxx-minx, c); // Draw line from min to max points found on the y
// Now increase y
if(!changed1) t1x += signx1;
t1x+=t1xp;
if(!changed2) t2x += signx2;
t2x+=t2xp;
y += 1;
if(y==y2) break;
}
next:
// Second half
dx1 = x3 - x2; if(dx1<0) { dx1=-dx1; signx1=-1; } else signx1=1;
dy1 = y3 - y2;
t1x=x2;
if (dy1 > dx1) { // swap values
swap(dy1,dx1);
changed1 = true;
} else changed1=false;
e1 = dx1>>1;
for (uint16_t i = 0; i<=dx1; i++) {
t1xp=0; t2xp=0;
if(t1x<t2x) { minx=t1x; maxx=t2x; }
else { minx=t2x; maxx=t1x; }
// process first line until y value is about to change
while(i<dx1) {
e1 += dy1;
while (e1 >= dx1) {
e1 -= dx1;
if (changed1) { t1xp=signx1; break; }//t1x += signx1;
else goto next3;
}
if (changed1) break;
else t1x += signx1;
if(i<dx1) i++;
}
next3:
// process second line until y value is about to change
while (t2x!=x3) {
e2 += dy2;
while (e2 >= dx2) {
e2 -= dx2;
if(changed2) t2xp=signx2;
else goto next4;
}
if (changed2) break;
else t2x += signx2;
}
next4:
if(minx>t1x) minx=t1x; if(minx>t2x) minx=t2x;
if(maxx<t1x) maxx=t1x; if(maxx<t2x) maxx=t2x;
drawFastHLine(minx, y, maxx-minx, c); // Draw line from min to max points found on the y
// Now increase y
if(!changed1) t1x += signx1;
t1x+=t1xp;
if(!changed2) t2x += signx2;
t2x+=t2xp;
y += 1;
if(y>y3) return;
}
}
/***************************************************************************************
** Function name: drawBitmap
** Description: Draw an image stored in an array on the TFT
***************************************************************************************/
void TFT_HX8357::drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) {
int16_t i, j, byteWidth = (w + 7) / 8;
uint8_t byte;
for(j=0; j<h; j++) {
for(i=0; i<w; i++) {
if(i & 7) byte <<= 1;
else byte = pgm_read_byte(bitmap + j * byteWidth + i / 8);
if(byte & 0x80) drawPixel(x+i, y+j, color);
}
}
}
/*
int16_t i, j, byteWidth = (w + 7) / 8;
for (j = 0; j < h; j++) {
for (i = 0; i < w; i++ ) {
if (pgm_read_byte(bitmap + j * byteWidth + i / 8) & (128 >> (i & 7))) {
drawPixel(x + i, y + j, color);
}
}
}
}
*/
/***************************************************************************************
** Function name: setCursor
** Description: Set the text cursor x,y position
***************************************************************************************/
void TFT_HX8357::setCursor(int16_t x, int16_t y)
{
cursor_x = x;
cursor_y = y;
}
/***************************************************************************************
** Function name: setCursor
** Description: Set the text cursor x,y position and font
***************************************************************************************/
void TFT_HX8357::setCursor(int16_t x, int16_t y, uint8_t font)
{
textfont = font;
cursor_x = x;
cursor_y = y;
}
/***************************************************************************************
** Function name: setTextSize
** Description: Set the text size multiplier
***************************************************************************************/
void TFT_HX8357::setTextSize(uint8_t s)
{
if (s>7) s = 7; // Limit the maximum size multiplier so byte variables can be used for rendering
textsize = (s > 0) ? s : 1; // Don't allow font size 0
}
/***************************************************************************************
** Function name: setTextFont
** Description: Set the font for the print stream
***************************************************************************************/
void TFT_HX8357::setTextFont(uint8_t f)
{
textfont = (f > 0) ? f : 1; // Don't allow font 0
#ifdef LOAD_GFXFF
gfxFont = NULL;
#endif
}
/***************************************************************************************
** Function name: setTextColor
** Description: Set the font foreground colour (background is transparent)
***************************************************************************************/
void TFT_HX8357::setTextColor(uint16_t c)
{
// For 'transparent' background set the bg the same as fg
textcolor = textbgcolor = c;
}
/***************************************************************************************
** Function name: setTextColor
** Description: Set the font foreground and background colour
***************************************************************************************/
void TFT_HX8357::setTextColor(uint16_t c, uint16_t b)
{
textcolor = c;
textbgcolor = b;
}
/***************************************************************************************
** Function name: setTextWrap
** Description: Define if text should wrap at end of line
***************************************************************************************/
void TFT_HX8357::setTextWrap(boolean w)
{
textwrap = w;
}
/***************************************************************************************
** Function name: setTextDatum
** Description: Set the text position reference datum
***************************************************************************************/
void TFT_HX8357::setTextDatum(uint8_t d)
{
textdatum = d;
}
/***************************************************************************************
** Function name: setTextPadding
** Description: Define padding width (aids erasing old text and numbers)
***************************************************************************************/
void TFT_HX8357::setTextPadding(uint16_t x_width)
{
padX = x_width;
}
/***************************************************************************************
** Function name: getRotation
** Description: Return the rotation value (as used by setRotation())
***************************************************************************************/
uint8_t TFT_HX8357::getRotation(void)
{
return rotation;
}
/***************************************************************************************
** Function name: width
** Description: Return the pixel width of display (per current rotation)
***************************************************************************************/
// Return the size of the display (per current rotation)
int16_t TFT_HX8357::width(void)
{
return _width;
}
/***************************************************************************************
** Function name: height
** Description: Return the pixel height of display (per current rotation)
***************************************************************************************/
int16_t TFT_HX8357::height(void)
{
return _height;
}
/***************************************************************************************
** Function name: textWidth
** Description: Return the width in pixels of a string in a given font
***************************************************************************************/
int16_t TFT_HX8357::textWidth(char *string, int font)
{
int16_t str_width = 0;