-
Notifications
You must be signed in to change notification settings - Fork 27
/
TFT_eFEX.cpp
1675 lines (1444 loc) · 54.2 KB
/
TFT_eFEX.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
/***************************************************************************************/
// The following class class adds additional functions to the TFT_eSPI library.
// The class inherits the generic drawing graphics functions from the TFT_eSPI class.
// Created by Bodmer 15/11/2018
// See license.txt in root folder of library
/***************************************************************************************/
#include "TFT_eFEX.h"
/***************************************************************************************
** Function name: TFT_eFEX
** Description: Class constructor
***************************************************************************************/
TFT_eFEX::TFT_eFEX(TFT_eSPI *tft)
{
_tft = tft; // Pointer to tft class so we can call member functions
}
/***************************************************************************************
** Function name: drawQuadraticBezier
** Description: Draw a bezier curve between points
***************************************************************************************/
// Plot any quadratic Bezier curve, no restrictions on point positions
// See source code http://members.chello.at/~easyfilter/bresenham.c
void TFT_eFEX::drawBezier(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint16_t color)
{
int32_t x = x0 - x1, y = y0 - y1;
double t = x0 - 2 * x1 + x2, r;
if (x * (x2 - x1) > 0) {
if (y * (y2 - y1) > 0)
if (fabs((y0 - 2 * y1 + y2) / t * x) > abs(y)) {
x0 = x2; x2 = x + x1; y0 = y2; y2 = y + y1;
}
t = (x0 - x1) / t;
r = (1 - t) * ((1 - t) * y0 + 2.0 * t * y1) + t * t * y2;
t = (x0 * x2 - x1 * x1) * t / (x0 - x1);
x = floor(t + 0.5); y = floor(r + 0.5);
r = (y1 - y0) * (t - x0) / (x1 - x0) + y0;
drawBezierSegment(x0, y0, x, floor(r + 0.5), x, y, color);
r = (y1 - y2) * (t - x2) / (x1 - x2) + y2;
x0 = x1 = x; y0 = y; y1 = floor(r + 0.5);
}
if ((y0 - y1) * (y2 - y1) > 0) {
t = y0 - 2 * y1 + y2; t = (y0 - y1) / t;
r = (1 - t) * ((1 - t) * x0 + 2.0 * t * x1) + t * t * x2;
t = (y0 * y2 - y1 * y1) * t / (y0 - y1);
x = floor(r + 0.5); y = floor(t + 0.5);
r = (x1 - x0) * (t - y0) / (y1 - y0) + x0;
drawBezierSegment(x0, y0, floor(r + 0.5), y, x, y, color);
r = (x1 - x2) * (t - y2) / (y1 - y2) + x2;
x0 = x; x1 = floor(r + 0.5); y0 = y1 = y;
}
drawBezierSegment(x0, y0, x1, y1, x2, y2, color);
}
/***************************************************************************************
** Function name: drawBezierSegment
** Description: Draw a bezier segment curve between points
***************************************************************************************/
// x0, y0 defines p0 etc.
// coordinates for p0-p3 must be sequentially increasing or decreasing so
// n0 <= n1 <= n2 or n0 >= n1 >= n2 where n is x or y, e.g.
//
// p1 x .x p2 p2 x.
// . . x p1
// . .
// . .
// . .
// . .
// . .
// p0 x x p0
//
void TFT_eFEX::drawBezierSegment(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint16_t color)
{
// Check if coordinates are sequential (replaces assert)
if (((x2 >= x1 && x1 >= x0) || (x2 <= x1 && x1 <= x0))
&& ((y2 >= y1 && y1 >= y0) || (y2 <= y1 && y1 <= y0)))
{
// Coordinates are sequential
int32_t sx = x2 - x1, sy = y2 - y1;
int32_t xx = x0 - x1, yy = y0 - y1, xy;
float dx, dy, err, cur = xx * sy - yy * sx;
if (sx * (int32_t)sx + sy * (int32_t)sy > xx * xx + yy * yy) {
x2 = x0; x0 = sx + x1; y2 = y0; y0 = sy + y1; cur = -cur;
}
_tft->startWrite();
if (cur != 0) {
xx += sx; xx *= sx = x0 < x2 ? 1 : -1;
yy += sy; yy *= sy = y0 < y2 ? 1 : -1;
xy = 2 * xx * yy; xx *= xx; yy *= yy;
if (cur * sx * sy < 0) {
xx = -xx; yy = -yy; xy = -xy; cur = -cur;
}
dx = 4.0 * sy * cur * (x1 - x0) + xx - xy;
dy = 4.0 * sx * cur * (y0 - y1) + yy - xy;
xx += xx; yy += yy; err = dx + dy + xy;
do {
_tft->drawPixel(x0, y0, color);
if (x0 == x2 && y0 == y2)
{
_tft->endWrite();
return;
}
y1 = 2 * err < dx;
if (2 * err > dy) {
x0 += sx;
dx -= xy;
err += dy += yy;
}
if ( y1 ) {
y0 += sy;
dy -= xy;
err += dx += xx;
}
yield();
} while (dy < dx );
}
_tft->drawLine(x0, y0, x2, y2, color);
_tft->endWrite();
}
else Serial.println("Bad coordinate set - non-sequential!");
}
/***************************************************************************************
** Function name: drawBmp
** Description: draw a bitmap stored in SPIFFS onto the TFT or in a Sprite
***************************************************************************************/
//void TFT_eFEX::drawBmp(const char *filename, int16_t x, int16_t y, TFT_eSprite *_spr) {
void TFT_eFEX::drawBmp(String filename, int16_t x, int16_t y, TFT_eSprite *_spr) {
if ( (_spr == nullptr) && ((x >= _tft->width()) || (y >= _tft->height()))) return;
fs::File bmpFS;
// Note: ESP32 passes "open" test even if file does not exist, whereas ESP8266 returns NULL
if ( !SPIFFS.exists(filename) )
{
Serial.println(F(" File not found")); // Can comment out if not needed
return;
}
// Open requested file
bmpFS = SPIFFS.open(filename, "r");
if (!bmpFS)
{
Serial.print("Bitmap file not found");
return;
}
uint32_t seekOffset;
uint16_t w, h, row, col;
uint8_t r, g, b;
//uint32_t startTime = millis();
if (read16(bmpFS) == 0x4D42)
{
read32(bmpFS);
read32(bmpFS);
seekOffset = read32(bmpFS);
read32(bmpFS);
w = read32(bmpFS);
h = read32(bmpFS);
if ((read16(bmpFS) == 1) && (read16(bmpFS) == 24) && (read32(bmpFS) == 0))
{
y += h - 1;
bool tftSwapBytes = _tft->getSwapBytes();
_tft->setSwapBytes(_spr == nullptr);
bmpFS.seek(seekOffset);
uint16_t padding = (4 - ((w * 3) & 3)) & 3;
uint8_t lineBuffer[w * 3 + padding];
for (row = 0; row < h; row++) {
bmpFS.read(lineBuffer, sizeof(lineBuffer));
uint8_t* bptr = lineBuffer;
uint16_t* tptr = (uint16_t*)lineBuffer;
// Convert 24 to 16 bit colours
for (col = 0; col < w; col++)
{
b = *bptr++;
g = *bptr++;
r = *bptr++;
*tptr++ = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}
// Push the pixel row to screen, pushImage will crop the line if needed
// y is decremented as the BMP image is drawn bottom up
if (_spr == nullptr) _tft->pushImage(x, y--, w, 1, (uint16_t*)lineBuffer);
else _spr->pushImage(x, y--, w, 1, (uint16_t*)lineBuffer);
}
_tft->setSwapBytes(tftSwapBytes); // Restore original setting
//Serial.print("Loaded in "); Serial.print(millis() - startTime);
//Serial.println(" ms");
}
else Serial.println("BMP format not recognised.");
}
bmpFS.close();
}
// These read 16 and 32-bit types from the file.
// BMP data is stored little-endian, Arduino is little-endian too.
// May need to reverse subscript order if porting elsewhere.
uint16_t TFT_eFEX::read16(fs::File &f) {
uint16_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read(); // MSB
return result;
}
uint32_t TFT_eFEX::read32(fs::File &f) {
uint32_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read();
((uint8_t *)&result)[2] = f.read();
((uint8_t *)&result)[3] = f.read(); // MSB
return result;
}
/***************************************************************************************
** Function name: drawJpeg
** Description: draw a jpeg stored in SPIFFS onto the TFT
***************************************************************************************/
void TFT_eFEX::drawJpeg(String filename, int16_t xpos, int16_t ypos, TFT_eSprite *_spr) {
if ( (_spr == nullptr) && ((xpos >= _tft->width()) || (ypos >= _tft->height()))) return;
// Note: ESP32 passes "open" test even if file does not exist, whereas ESP8266 returns NULL
if ( !SPIFFS.exists(filename) )
{
Serial.println(F(" Jpeg file not found")); // Can comment out if not needed
return;
}
// Open the named file (the Jpeg decoder library will close it after rendering image)
fs::File jpegFile = SPIFFS.open( filename, "r"); // File handle reference for SPIFFS
// File jpegFile = SD.open( filename, FILE_READ); // or, file handle reference for SD library
// Use one of the three following methods to initialise the decoder:
//boolean decoded = JpegDec.decodeFsFile(jpegFile); // Pass a SPIFFS file handle to the decoder,
//boolean decoded = JpegDec.decodeSdFile(jpegFile); // or pass the SD file handle to the decoder,
boolean decoded = JpegDec.decodeFsFile(filename); // or pass the filename (leading / distinguishes SPIFFS files)
// Note: the filename can be a String or character array type
if (decoded) {
// render the image onto the screen at given coordinates
// retrieve information about the image
uint16_t *pImg;
uint16_t mcu_w = JpegDec.MCUWidth;
uint16_t mcu_h = JpegDec.MCUHeight;
int32_t max_x = JpegDec.width;
int32_t max_y = JpegDec.height;
// Jpeg images are draw as a set of image block (tiles) called Minimum Coding Units (MCUs)
// Typically these MCUs are 16x16 pixel blocks
// Determine the width and height of the right and bottom edge image blocks
int32_t min_w = jpg_min(mcu_w, max_x % mcu_w);
int32_t min_h = jpg_min(mcu_h, max_y % mcu_h);
// save the current image block size
int32_t win_w = mcu_w;
int32_t win_h = mcu_h;
// record the current time so we can measure how long it takes to draw an image
uint32_t drawTime = millis();
bool tftSwapBytes = _tft->getSwapBytes();
bool sprSwapBytes;
// Retrieve the width and height of the display/Sprite
//int32_t disp_w = _tft->width();
int32_t disp_h = _tft->height();
if (_spr != nullptr)
{
//disp_w = _spr->width();
disp_h = _spr->height();
sprSwapBytes = _spr->getSwapBytes();
_spr->setSwapBytes(false);
}
_tft->setSwapBytes(false);
// save the coordinate of the right and bottom edges to assist image cropping
// to the screen size
max_x += xpos;
max_y += ypos;
// read each MCU block until there are no more
while ( JpegDec.read())
{ // Normal byte order read
// save a pointer to the image block
pImg = JpegDec.pImage;
// calculate where the image block should be drawn on the screen
int mcu_x = JpegDec.MCUx * mcu_w + xpos;
int mcu_y = JpegDec.MCUy * mcu_h + ypos;
if ( mcu_y < disp_h )
{
// check if the image block size needs to be changed for the right edge
if (mcu_x + mcu_w <= max_x) win_w = mcu_w;
else win_w = min_w;
// check if the image block size needs to be changed for the bottom edge
if (mcu_y + mcu_h <= max_y) win_h = mcu_h;
else win_h = min_h;
// copy pixels into a smaller block
if (win_w != mcu_w)
{
for (int h = 1; h < win_h; h++)
{
memcpy(pImg + h * win_w, pImg + h * mcu_w, win_w << 1);
}
}
if (_spr == nullptr) _tft->pushImage(mcu_x, mcu_y, win_w, win_h, pImg);
else _spr->pushImage(mcu_x, mcu_y, win_w, win_h, pImg);
}
else
{
JpegDec.abort();
}
}
if (_spr != nullptr) _spr->setSwapBytes(sprSwapBytes); // Restore original setting
_tft->setSwapBytes(tftSwapBytes); // Restore original setting
// calculate how long it took to draw the image
drawTime = millis() - drawTime; // Calculate the time it took
// print the results to the serial port
//Serial.print ("Total render time was : "); Serial.print(drawTime); Serial.println(" ms");
//Serial.println("=====================================");
}
else
{
Serial.println("Jpeg file format not supported!");
}
}
/***************************************************************************************
** Function name: drawJpeg
** Description: draw a jpeg stored in FLASH onto the TFT
***************************************************************************************/
void TFT_eFEX::drawJpeg(const uint8_t arrayname[], uint32_t array_size, int16_t xpos, int16_t ypos, TFT_eSprite *_spr) {
if ( (_spr == nullptr) && ((xpos >= _tft->width()) || (ypos >= _tft->height()))) return;
boolean decoded = JpegDec.decodeArray(arrayname, array_size);
if (decoded) {
// render the image onto the screen at given coordinates
// retrieve information about the image
uint16_t *pImg;
uint16_t mcu_w = JpegDec.MCUWidth;
uint16_t mcu_h = JpegDec.MCUHeight;
int32_t max_x = JpegDec.width;
int32_t max_y = JpegDec.height;
// Jpeg images are draw as a set of image block (tiles) called Minimum Coding Units (MCUs)
// Typically these MCUs are 16x16 pixel blocks
// Determine the width and height of the right and bottom edge image blocks
int32_t min_w = jpg_min(mcu_w, max_x % mcu_w);
int32_t min_h = jpg_min(mcu_h, max_y % mcu_h);
// save the current image block size
int32_t win_w = mcu_w;
int32_t win_h = mcu_h;
// record the current time so we can measure how long it takes to draw an image
uint32_t drawTime = millis();
// Retrieve the width and height of the display/Sprite
//int32_t disp_w = _tft->width();
int32_t disp_h = _tft->height();
bool tftSwapBytes = _tft->getSwapBytes();
_tft->setSwapBytes(true);
// save the coordinate of the right and bottom edges to assist image cropping
// to the screen size
max_x += xpos;
max_y += ypos;
// read each MCU block until there are no more
while ( JpegDec.read())
{ // Normal byte order read
// save a pointer to the image block
pImg = JpegDec.pImage;
// calculate where the image block should be drawn on the screen
int mcu_x = JpegDec.MCUx * mcu_w + xpos;
int mcu_y = JpegDec.MCUy * mcu_h + ypos;
if ( mcu_y < disp_h )
{
// check if the image block size needs to be changed for the right edge
if (mcu_x + mcu_w <= max_x) win_w = mcu_w;
else win_w = min_w;
// check if the image block size needs to be changed for the bottom edge
if (mcu_y + mcu_h <= max_y) win_h = mcu_h;
else win_h = min_h;
// copy pixels into a smaller block
if (win_w != mcu_w)
{
for (int h = 1; h < win_h; h++)
{
memcpy(pImg + h * win_w, pImg + h * mcu_w, win_w << 1);
}
}
if (_spr == nullptr) _tft->pushImage(mcu_x, mcu_y, win_w, win_h, pImg);
else _spr->pushImage(mcu_x, mcu_y, win_w, win_h, pImg);
}
else
{
JpegDec.abort();
}
}
_tft->setSwapBytes(tftSwapBytes); // Restore original setting
// calculate how long it took to draw the image
drawTime = millis() - drawTime; // Calculate the time it took
// print the results to the serial port
//Serial.print ("Total render time was : "); Serial.print(drawTime); Serial.println(" ms");
//Serial.println("=====================================");
}
else
{
Serial.println("Jpeg file format not supported!");
}
}
/***************************************************************************************
** Function name: jpegInfo
** Description: Print information decoded from the Jpeg image
***************************************************************************************/
void TFT_eFEX::jpegInfo(String filename) {
// Note: ESP32 passes "open" test even if file does not exist, whereas ESP8266 returns NULL
if ( !SPIFFS.exists(filename) )
{
Serial.println(F(" Jpeg file not found")); // Can comment out if not needed
return;
}
// Open the named file (the Jpeg decoder library abort will close it after reading image info)
fs::File jpegFile = SPIFFS.open( filename, "r"); // File handle reference for SPIFFS
// File jpegFile = SD.open( filename, FILE_READ); // or, file handle reference for SD library
// Use one of the three following methods to initialise the decoder:
//boolean decoded = JpegDec.decodeFsFile(jpegFile); // Pass a SPIFFS file handle to the decoder,
//boolean decoded = JpegDec.decodeSdFile(jpegFile); // or pass the SD file handle to the decoder,
boolean decoded = JpegDec.decodeFsFile(filename); // or pass the filename (leading / distinguishes SPIFFS files)
static const char line[] PROGMEM = "===============";
if (decoded) {
Serial.println(FPSTR(line));
Serial.println(F("JPEG image info"));
Serial.println(FPSTR(line));
Serial.print (F("Width :")); Serial.println(JpegDec.width);
Serial.print (F("Height :")); Serial.println(JpegDec.height);
Serial.print (F("Components :")); Serial.println(JpegDec.comps);
Serial.print (F("MCU / row :")); Serial.println(JpegDec.MCUSPerRow);
Serial.print (F("MCU / col :")); Serial.println(JpegDec.MCUSPerCol);
Serial.print (F("Scan type :")); Serial.println(JpegDec.scanType);
Serial.print (F("MCU width :")); Serial.println(JpegDec.MCUWidth);
Serial.print (F("MCU height :")); Serial.println(JpegDec.MCUHeight);
Serial.println(FPSTR(line));
Serial.println("");
JpegDec.abort(); // Close file and tidy up
}
}
/***************************************************************************************
** Function name: jpegInfo
** Description: Print information decoded from the Jpeg image
***************************************************************************************/
void TFT_eFEX::jpegInfo(const uint8_t arrayname[], uint32_t array_size) {
boolean decoded = JpegDec.decodeArray(arrayname, array_size);
static const char line[] PROGMEM = "===============";
if (decoded) {
Serial.println(FPSTR(line));
Serial.println(F("JPEG image info"));
Serial.println(FPSTR(line));
Serial.print (F("Width :")); Serial.println(JpegDec.width);
Serial.print (F("Height :")); Serial.println(JpegDec.height);
Serial.print (F("Components :")); Serial.println(JpegDec.comps);
Serial.print (F("MCU / row :")); Serial.println(JpegDec.MCUSPerRow);
Serial.print (F("MCU / col :")); Serial.println(JpegDec.MCUSPerCol);
Serial.print (F("Scan type :")); Serial.println(JpegDec.scanType);
Serial.print (F("MCU width :")); Serial.println(JpegDec.MCUWidth);
Serial.print (F("MCU height :")); Serial.println(JpegDec.MCUHeight);
Serial.println(FPSTR(line));
Serial.println("");
JpegDec.abort(); // Close file and tidy up
}
}
/***************************************************************************************
** Function name: drawProgressBar
** Description: Draw a progress bar - increasing percentage only
***************************************************************************************/
void TFT_eFEX::drawProgressBar(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t percent, uint16_t frameColor, uint16_t barColor) {
if (percent == 0) {
_tft->fillRoundRect(x, y, w, h, 3, TFT_BLACK);
}
uint8_t margin = 2;
uint16_t barHeight = h - 2 * margin;
uint16_t barWidth = w - 2 * margin;
_tft->drawRoundRect(x, y, w, h, 3, frameColor);
_tft->fillRect(x + margin, y + margin, barWidth * percent / 100.0, barHeight, barColor);
}
/***************************************************************************************
** Function name: luminance
** Description: return a 16 bit colour with reduced luminance
***************************************************************************************/
uint16_t TFT_eFEX::luminance(uint16_t color, uint8_t luminance)
{
// Extract rgb colours and stretch range to 0 - 255
uint16_t r = (color & 0xF800) >> 8; r |= (r >> 5);
uint16_t g = (color & 0x07E0) >> 3; g |= (g >> 6);
uint16_t b = (color & 0x001F) << 3; b |= (b >> 5);
b = ((b * (uint16_t)luminance + 255) >> 8) & 0x00F8;
g = ((g * (uint16_t)luminance + 255) >> 8) & 0x00FC;
r = ((r * (uint16_t)luminance + 255) >> 8) & 0x00F8;
return (r << 8) | (g << 3) | (b >> 3);
}
/***************************************************************************************
** Function name: luminance
** Description: return a 16 bit colour from an RGB with reduced luminance
***************************************************************************************/
uint16_t TFT_eFEX::luminance(uint8_t red, uint8_t green, uint8_t blue, uint8_t luminance)
{
uint16_t b = (((uint16_t)blue * (uint16_t)luminance + 255) >> 8) & 0x00F8;
uint16_t g = (((uint16_t)green * (uint16_t)luminance + 255) >> 8) & 0x00FC;
uint16_t r = (((uint16_t)red * (uint16_t)luminance + 255) >> 8) & 0x00F8;
return (r << 8) | (g << 3) | (b >> 3);
}
/***************************************************************************************
** Function name: rainbowColor
** Description: Return a 16 bit rainbow colour
***************************************************************************************/
// If 'spectrum' is in the range 0-159 it is converted to a spectrum colour
// from 0 = red through to 127 = blue to 159 = violet
// Extending the range to 0-191 adds a further violet to red band
uint16_t TFT_eFEX::rainbowColor(uint8_t spectrum)
{
spectrum = spectrum%192;
uint8_t red = 0; // Red is the top 5 bits of a 16 bit colour spectrum
uint8_t green = 0; // Green is the middle 6 bits, but only top 5 bits used here
uint8_t blue = 0; // Blue is the bottom 5 bits
uint8_t sector = spectrum >> 5;
uint8_t amplit = spectrum & 0x1F;
switch (sector)
{
case 0:
red = 0x1F;
green = amplit; // Green ramps up
blue = 0;
break;
case 1:
red = 0x1F - amplit; // Red ramps down
green = 0x1F;
blue = 0;
break;
case 2:
red = 0;
green = 0x1F;
blue = amplit; // Blue ramps up
break;
case 3:
red = 0;
green = 0x1F - amplit; // Green ramps down
blue = 0x1F;
break;
case 4:
red = amplit; // Red ramps up
green = 0;
blue = 0x1F;
break;
case 5:
red = 0x1F;
green = 0;
blue = 0x1F - amplit; // Blue ramps down
break;
}
return red << 11 | green << 6 | blue;
}
/***************************************************************************************
** Function name: listSPIFFS
** Description: Listing SPIFFS files
***************************************************************************************/
#ifdef ESP8266
void TFT_eFEX::listSPIFFS(void) {
Serial.println(F("\r\nListing SPIFFS files:"));
fs::Dir dir = SPIFFS.openDir("/"); // Root directory
static const char line[] PROGMEM = "=================================================";
Serial.println(FPSTR(line));
Serial.println(F(" File name Size"));
Serial.println(FPSTR(line));
while (dir.next()) {
String fileName = dir.fileName();
Serial.print(fileName);
int spaces = 33 - fileName.length(); // Tabulate nicely
if (spaces < 1) spaces = 1;
while (spaces--) Serial.print(" ");
fs::File f = dir.openFile("r");
String fileSize = (String) f.size();
spaces = 10 - fileSize.length(); // Tabulate nicely
if (spaces < 1) spaces = 1;
while (spaces--) Serial.print(" ");
Serial.println(fileSize + " bytes");
}
Serial.println(FPSTR(line));
Serial.println();
delay(1000);
}
//====================================================================================
#elif defined ESP32
// TODO: add sub directories
void TFT_eFEX::listSPIFFS(void) {
Serial.println(F("\r\nListing SPIFFS files:"));
static const char line[] PROGMEM = "=================================================";
Serial.println(FPSTR(line));
Serial.println(F(" File name Size"));
Serial.println(FPSTR(line));
fs::File root = SPIFFS.open("/");
if (!root) {
Serial.println(F("Failed to open directory"));
return;
}
if (!root.isDirectory()) {
Serial.println(F("Not a directory"));
return;
}
fs::File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
Serial.print("DIR : ");
String fileName = file.name();
Serial.print(fileName);
} else {
String fileName = file.name();
Serial.print(" " + fileName);
// File path can be 31 characters maximum in SPIFFS
int spaces = 33 - fileName.length(); // Tabulate nicely
if (spaces < 1) spaces = 1;
while (spaces--) Serial.print(" ");
String fileSize = (String) file.size();
spaces = 10 - fileSize.length(); // Tabulate nicely
if (spaces < 1) spaces = 1;
while (spaces--) Serial.print(" ");
Serial.println(fileSize + " bytes");
}
file = root.openNextFile();
}
Serial.println(FPSTR(line));
Serial.println();
delay(1000);
}
#else
void TFT_eFEX::listSPIFFS(void) {}
#endif
#ifdef SMOOTH_FONT // These functions require smooth fonts to be enabled
/***************************************************************************************
** Function name: setCursorRTL
** Description: Set the RTL cursor position
***************************************************************************************/
// Set the RTL cursor position
void TFT_eFEX::setCursorRTL(int32_t cx, int32_t cy)
{
rtl_cursorX = cx;
rtl_cursorY = cy;
}
/***************************************************************************************
** Function name: drawStringRTL
** Description: Draw the string RTL and update cursor coordinates
***************************************************************************************/
void TFT_eFEX::drawStringRTL(const String& string)
{
int16_t len = string.length() + 2;
char sbuffer[len];
string.toCharArray(sbuffer, len);
drawStringRTL(sbuffer, &rtl_cursorX, &rtl_cursorY);
}
/***************************************************************************************
** Function name: drawStringRTL
** Description: Draw the string RTL at defined coordinates
***************************************************************************************/
// Must call with variables, &x and &y
void TFT_eFEX::drawStringRTL(const char *string, int32_t *x, int32_t *y)
{
int32_t poX = *x;
int32_t poY = *y;
int16_t len = strlen(string);
if (_tft->fontLoaded)
{
uint8_t datum = _tft->getTextDatum();
int16_t cx = _tft->getCursorX();
int16_t cy = _tft->getCursorY();
_tft->setTextDatum(TL_DATUM);
uint16_t n = 0;
while (n < len)
{
uint16_t uniCode = _tft->decodeUTF8((uint8_t*)string, &n, len - n);
uint16_t gNum = 0;
bool found = _tft->getUnicodeIndex(uniCode, &gNum);
if (uniCode == 0x20)
{
poX -= _tft->gFont.spaceWidth;
}
else if (uniCode == '\n')
{
poX = _tft->width() - 1;
poY += _tft->gFont.yAdvance;
}
if (found)
{
poX -= ( _tft->gWidth[gNum] + _tft->gdX[gNum] );
if (poX < 0)
{
poX = _tft->width() - ( _tft->gWidth[gNum] + _tft->gdX[gNum] );
poY += _tft->gFont.yAdvance;
if (poY >= _tft->height()) poY = 0;
}
_tft->setCursor(poX, poY);
_tft->drawGlyph(uniCode);
}
}
_tft->setTextDatum(datum);
_tft->setCursor(cx, cy);
*x = poX;
*y = poY;
}
}
/***************************************************************************************
** Function name: drawStringRTLAr
** Description: Draw the string Arabic RTL and update cursor coordinates
***************************************************************************************/
void TFT_eFEX::drawStringRTLAr(const String &string)
{
int16_t len = string.length() + 2;
// Serial.print("Size:");
// Serial.println((String)len);
char sbuffer[len];
string.toCharArray(sbuffer, len);
drawStringRTLAr(sbuffer, &rtl_cursorX, &rtl_cursorY);
}
/***************************************************************************************
** Function name: isFrom
** Description: If the font has a From
***************************************************************************************/
#define ISOLATED 1
#define INITIAL 2
#define MEDIAL 3
#define FINAL 4
const uint8_t CODECOUNT = 180;
const uint16_t codes[CODECOUNT][5] PROGMEM = {
//'<isolated>', '<initial>', '<medial>', '<final>'
//#ARABIC LETTER HAMZA
0x0621,0xFE80, 0, 0, 0,
//#ARABIC LETTER ALEF WITH MADDA ABOVE
0x0622,0x0622, 0, 0, 0xFE82,
//#ARABIC LETTER ALEF WITH HAMZA ABOVE
0x0623,0x0623, 0, 0, 0xFE84,
//#ARABIC LETTER WAW WITH HAMZA ABOVE
0x0624,0x0624, 0, 0, 0xFE86,
//#ARABIC LETTER ALEF WITH HAMZA BELOW
0x0625,0x0625, 0, 0, 0xFE88,
//#ARABIC LETTER YEH WITH HAMZA ABOVE
0x0626,0x0626, 0xFE8B, 0xFE8C, 0xFE8A,
//#ARABIC LETTER ALEF
0x0627,0x0627, 0, 0, 0xFE8E,
//#ARABIC LETTER BEH
0x0628,0x0628, 0xFE91, 0xFE92, 0xFE90,
//#ARABIC LETTER TEH MARBUTA
0x0629,0x0629, 0, 0, 0xFE94,
//#ARABIC LETTER TEH
0x062A,0x062A, 0xFE97, 0xFE98, 0xFE96,
//#ARABIC LETTER THEH
0x062B,0x062B, 0xFE9B, 0xFE9C, 0xFE9A,
//#ARABIC LETTER JEEM
0x062C,0x062C, 0xFE9F, 0xFEA0, 0xFE9E,
//#ARABIC LETTER HAH
0x062D,0xFEA1, 0xFEA3, 0xFEA4, 0xFEA2,
//#ARABIC LETTER KHAH
0x062E,0x062E, 0xFEA7, 0xFEA8, 0xFEA6,
//#ARABIC LETTER DAL
0x062F,0x062F, 0, 0, 0xFEAA,
//#ARABIC LETTER THAL
0x0630,0x0630, 0, 0, 0xFEAC,
//#ARABIC LETTER REH
0x0631,0x0631, 0, 0, 0xFEAE,
//#ARABIC LETTER ZAIN
0x0632,0x0632, 0, 0, 0xFEB0,
//#ARABIC LETTER SEEN
0x0633,0x0633, 0xFEB3, 0xFEB4, 0xFEB2,
//#ARABIC LETTER SHEEN
0x0634,0x0634, 0xFEB7, 0xFEB8, 0xFEB6,
//#ARABIC LETTER SAD
0x0635,0x0635, 0xFEBB, 0xFEBC, 0xFEBA,
//#ARABIC LETTER DAD
0x0636,0x0636, 0xFEBF, 0xFEC0, 0xFEBE,
//#ARABIC LETTER TAH
0x0637,0x0637, 0xFEC3, 0xFEC4, 0xFEC2,
//#ARABIC LETTER ZAH
0x0638,0x0638, 0xFEC7, 0xFEC8, 0xFEC6,
//#ARABIC LETTER AIN
0x0639,0x0639, 0xFECB, 0xFECC, 0xFECA,
//#ARABIC LETTER GHAIN
0x063A,0x063A, 0xFECF, 0xFED0, 0xFECE,
//#ARABIC TATWEEL
0x0640,0x0640, 0x0640, 0x0640, 0x0640,
//#ARABIC LETTER FEH
0x0641,0x0641, 0xFED3, 0xFED4, 0xFED2,
//#ARABIC LETTER QAF
0x0642,0x0642, 0xFED7, 0xFED8, 0xFED6,
//#ARABIC LETTER KAF
0x0643,0x0643, 0xFEDB, 0xFEDC, 0xFEDA,
//#ARABIC LETTER LAM
0x0644,0x0644, 0xFEDF, 0xFEE0, 0xFEDE,
//#ARABIC LETTER MEEM
0x0645,0x0645, 0xFEE3, 0xFEE4, 0xFEE2,
//#ARABIC LETTER NOON
0x0646,0x0646, 0xFEE7, 0xFEE8, 0xFEE6,
//#ARABIC LETTER HEH
0x0647,0x0647, 0xFEEB, 0xFEEC, 0xFEEA,
//#ARABIC LETTER WAW
0x0648,0x0648, 0, 0, 0xFEEE,
//#ARABIC LETTER(UIGHUR KAZAKH KIRGHIZ) ? ALEF MAKSURA
0x0649,0x0649, 0xFBE8, 0xFBE9, 0xFEF0,
//#ARABIC LETTER YEH
0x064A,0x064A, 0xFEF3, 0xFEF4, 0xFEF2,
//#ARABIC LETTER ALEF WASLA
0x0671,0x0671, 0, 0, 0xFB51,
//#ARABIC LETTER U WITH HAMZA ABOVE
0x0677,0x0677, 0, 0, 0,
//#ARABIC LETTER TTEH
0x0679,0x0679, 0xFB68, 0xFB69, 0xFB67,
//#ARABIC LETTER TTEHEH
0x067A,0x067A, 0xFB60, 0xFB61, 0xFB5F,
//#ARABIC LETTER BEEH
0x067B,0x067B, 0xFB54, 0xFB55, 0xFB53,
//#ARABIC LETTER PEH
0x067E,0x067E, 0xFB58, 0xFB59, 0xFB57,
//#ARABIC LETTER TEHEH
0x067F,0x067F, 0xFB64, 0xFB65, 0xFB63,
//#ARABIC LETTER BEHEH
0x0680,0x0680, 0xFB5C, 0xFB5D, 0xFB5B,
//#ARABIC LETTER NYEH
0x0683,0x0683, 0xFB78, 0xFB79, 0xFB77,
//#ARABIC LETTER DYEH
0x0684,0x0684, 0xFB74, 0xFB75, 0xFB73,
//#ARABIC LETTER TCHEH
0x0686,0x0686, 0xFB7C, 0xFB7D, 0xFB7B,
//#ARABIC LETTER TCHEHEH
0x0687,0x0687, 0xFB80, 0xFB81, 0xFB7F,
//#ARABIC LETTER DDAL
0x0688,0x0688, 0, 0, 0xFB89,
//#ARABIC LETTER DAHAL
0x068C,0x068C, 0, 0, 0xFB85,
//#ARABIC LETTER DDAHAL
0x068D,0x068D, 0, 0, 0xFB83,
//#ARABIC LETTER DUL
0x068E,0x068E, 0, 0, 0xFB87,
//#ARABIC LETTER RREH
0x0691,0x0691, 0, 0, 0xFB8D,
//#ARABIC LETTER JEH
0x0698,0x0698, 0, 0, 0xFB8B,
//#ARABIC LETTER VEH
0x06A4,0x06A4, 0xFB6C, 0xFB6D, 0xFB6B,
//#ARABIC LETTER PEHEH
0x06A6,0x06A6, 0xFB70, 0xFB71, 0xFB6F,
//#ARABIC LETTER KEHEH
0x06A9,0x06A9, 0xFB90, 0xFB91, 0xFB8F,
//#ARABIC LETTER NG
0x06AD,0x06AD, 0xFBD5, 0xFBD6, 0xFBD4,
//#ARABIC LETTER GAF
0x06AF,0x06AF, 0xFB94, 0xFB95, 0xFB93,
//#ARABIC LETTER NGOEH
0x06B1,0x06B1, 0xFB9C, 0xFB9D, 0xFB9B,
//#ARABIC LETTER GUEH
0x06B3,0x06B3, 0xFB98, 0xFB99, 0xFB97,
//#ARABIC LETTER NOON GHUNNA
0x06BA,0x06BA, 0, 0, 0xFB9F,
//#ARABIC LETTER RNOON
0x06BB,0x06BB, 0xFBA2, 0xFBA3, 0xFBA1,
//#ARABIC LETTER HEH DOACHASHMEE
0x06BE,0x06BE, 0xFBAC, 0xFBAD, 0xFBAB,
//#ARABIC LETTER HEH WITH YEH ABOVE
0x06C0,0x06C0, 0, 0, 0xFBA5,
//#ARABIC LETTER HEH GOAL
0x06C1,0x06C1, 0xFBA8, 0xFBA9, 0xFBA7,
//#ARABIC LETTER KIRGHIZ OE
0x06C5,0x06C5, 0, 0, 0xFBE1,
//#ARABIC LETTER OE
0x06C6,0x06C6, 0, 0, 0xFBDA,
//#ARABIC LETTER U
0x06C7,0x06C7, 0, 0, 0xFBD8,
//#ARABIC LETTER YU
0x06C8,0x06C8, 0, 0, 0xFBDC,
//#ARABIC LETTER KIRGHIZ YU
0x06C9,0x06C9, 0, 0, 0xFBE3,
//#ARABIC LETTER VE
0x06CB,0x06CB, 0, 0, 0xFBDF,
//#ARABIC LETTER FARSI YEH
0x06CC,0x06CC, 0xFBFE, 0xFBFF, 0xFBFD,
//#ARABIC LETTER E
0x06D0,0x06D0, 0xFBE6, 0xFBE7, 0xFBE5,
//#ARABIC LETTER YEH BARREE
0x06D2,0x06D2, 0, 0, 0xFBAF,
//#ARABIC LETTER YEH BARREE WITH HAMZA ABOVE
0x06D3,0x06D3, 0, 0, 0xFBB1,
//#Kurdish letter YEAH
0x06ce,0xE004, 0xE005, 0xE006, 0xE004,
//#Kurdish letter Hamza same as arabic Teh without the point
0x06d5,0x06d5, 0, 0, 0xE000,
};