forked from KrisKasprzak/ILI9341_t3_controls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ILI9341_t3_Controls.h
1186 lines (907 loc) · 28.2 KB
/
ILI9341_t3_Controls.h
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 MIT License (MIT)
library writen by Kris Kasprzak
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
On a personal note, if you develop an application or product using this library
and make millions of dollars, I'm happy for you!
rev date author change
1.0 9/2019 kasprzak initial code
2.0 9/2020 kasprzak added shapes and sizes for handles
3.0 10/2020 kasprzak fixed some repaint issues in CGraph, added marker support, added Button class with tons of methods
4.0 11/2020 kasprzak fixed bugs added Button, Checkbox, OptionButton classes
5.0 11/2020 kasprzak modified sliders, option and check to return true/false if pressed, and actual value stored in value property
5.1 4/2021 kasprzak added changed() back into sliderH and SliderV improved touch/control accuracy
5.2 4/2021 kasprzak added changed back to SliderV and SliderH, improved touch / control location
5.3 4/2021 kasprzak added redraw code to the change handle size and shape, that way the old gets painted out upon a change (after init())
5.31 4/2021 kasprzak fixed snap issue in horizontal slider and draw issue with double triangle in horizontal slider
*/
#ifndef ILI9341_T3_CONTROLS_H
#define ILI9341_T3_CONTROLS_H
#if ARDUINO >= 100
#include "Arduino.h"
#include "Print.h"
#else
#include "WProgram.h"
#endif
#ifdef __cplusplus
#include "Arduino.h"
#endif
#include <ILI9341_t3.h>
#define G_REPAINT 0
#define G_DRAWOVER 1
#define BELOW 0
#define ABOVE 1
#define SLIDER_HANDLE_SIZE 16
#define HANDLE_NONE 0
#define HANDLE_CIRCLE 1
#define HANDLE_SQUARE 2
#define HANDLE_TRIANGLE_1 3
#define HANDLE_TRIANGLE_2 4
#define HANDLE_TRIANGLE_3 5
#define C_BLACK 0x0000
#define C_BLUE 0x001F
#define C_RED 0xF800
#define C_GREEN 0x07E0
#define C_CYAN 0x07FF
#define C_MAGENTA 0xF81F
#define C_YELLOW 0xFFE0
#define C_WHITE 0xFFFF
#define C_LTBLUE 0xB6DF
#define C_LTTEAL 0xBF5F
#define C_LTGREEN 0xBFF7
#define C_LTCYAN 0xC7FF
#define C_LTRED 0xFD34
#define C_LTMAGENTA 0xFD5F
#define C_LTYELLOW 0xFFF8
#define C_LTORANGE 0xFE73
#define C_LTPINK 0xFDDF
#define C_LTPURPLE 0xCCFF
#define C_LTGREY 0xE71C
#define C_TEAL 0x0438
#define C_ORANGE 0xFD20
#define C_PINK 0xF81F
#define C_PURPLE 0x801F
#define C_GREY 0xC618
#define C_DKBLUE 0x000D
#define C_DKTEAL 0x020C
#define C_DKGREEN 0x03E0
#define C_DKCYAN 0x03EF
#define C_DKRED 0x6000
#define C_DKMAGENTA 0x8008
#define C_DKYELLOW 0x8400
#define C_DKORANGE 0x8200
#define C_DKPINK 0x9009
#define C_DKPURPLE 0x4010
#define C_DKGREY 0x3186
#define C_MDGREY 0x7BCF
#define B_PRESSED true
#define B_RELEASED false
#define TFT_DEBOUNCE 100 // debounce delay to minimize screen repress
#define CORNER_AUTO -1
#define CORNER_SQUARE 0
#define MAX_OPTION 20
#define OPTION_BUTTON_RADIUS 10
#define CHECKBOX_SIZE 20
#define C_DISABLE_LIGHT 0xC618
#define C_DISABLE_MED 0x7BCF
#define C_DISABLE_DARK 0x3186
class BarChartH {
public:
BarChartH(ILI9341_t3 *Display);
void init(float GraphXLoc, float GraphYLoc, float GraphWidth, float GraphHeight, float ScaleLow, float ScaleHigh, float ScaleInc, const char *Title, uint16_t TextColor, uint16_t BorderColor, uint16_t BarColor, uint16_t BarBColor, uint16_t BackColor,const ILI9341_t3_font_t &TitleFont , const ILI9341_t3_font_t &ScaleFont );
void setBarColor(uint16_t val = 0xF800);
void draw(float val);
void setScale(float ScaleLow, float ScaleHigh, float ScaleInc);
void showTitle(bool val);
void showScale(bool val);
private:
ILI9341_t3 *d;
ILI9341_t3_font_t tf;
ILI9341_t3_font_t sf;
bool st = true, ss = true;
char ti[20];
char sc[20];
char cc[2] = "D";
char text[30];
float XLow;
float XHigh;
float XInc;
int Dec, tLen, tHi;
float Low;
float High;
float Inc;
float gx;
float gy;
float gw;
float gh;
uint16_t tc;
uint16_t oc;
uint16_t rc;
uint16_t bc;
uint16_t ac;
bool redraw;
float stepval, range, TempY, level, i, data;
float MapFloat(float x, float in_min, float in_max, float out_min, float out_max);
};
class BarChartV {
public:
BarChartV(ILI9341_t3 *Display);
void init(float GraphXLoc, float GraphYLoc, float GraphWidth, float GraphHeight, float ScaleLow, float ScaleHigh, float ScaleInc, const char *Title, uint16_t TextColor, uint16_t BorderColor, uint16_t BarColor, uint16_t BarBlankColor, uint16_t BackgroundColor,const ILI9341_t3_font_t &TitleFont , const ILI9341_t3_font_t &ScaleFont );
void setBarColor(uint16_t val = 0xF800);
void draw(float val);
void setScale(float ScaleLow, float ScaleHigh, float ScaleInc);
void showTitle(bool val);
void showScale(bool val);
private:
ILI9341_t3 *d;
ILI9341_t3_font_t tf;
ILI9341_t3_font_t sf;
bool st = true, ss = true;
char ti[20];
char sc[20];
char cc[2] = "D";
char text[30];
float XLow;
float XHigh;
float XInc;
int Dec, tLen, tHi;
float Low;
float High;
float Inc;
float gx;
float gy;
float gw;
float gh;
uint16_t tc;
uint16_t oc;
uint16_t rc;
uint16_t bc;
uint16_t ac;
bool redraw;
float stepval, range, TempY, level, i, data;
float MapFloat(float x, float in_min, float in_max, float out_min, float out_max);
};
class CGraph {
public:
CGraph(ILI9341_t3 *disp, float GraphXLoc, float GraphYLoc, float GraphWidth, float GraphHeight, float XAxisLow, float XAxisHigh, float XAxisInc, float YAxisLow, float YAxisHigh, float YAxisInc);
void init(const char *Title, const char *XAxis, const char *YAxis, uint16_t TextColor, uint16_t GridColor, uint16_t AxisColor, uint16_t BackColor,uint16_t PlotkColor, const ILI9341_t3_font_t &TitleFont , const ILI9341_t3_font_t &AxisFont );
void plot(int ID, float y);
void setX(float x);
int add(const char * DataLabel, uint16_t DataColor );
void setYAxis(float Ylow, float YHigh, float YInc);
void showTitle(bool val);
void showLegend(bool val);
void showXScale(bool val);
void showYScale(bool val);
void setMarkerSize(int ID, byte val);
void drawGraph();
private:
ILI9341_t3 *d;
ILI9341_t3_font_t tf;
ILI9341_t3_font_t af;
int ID = 0;
float x, y;
float i, j;
bool st, sl, sxs, sys;
float Delta;
int k;
float XLow, XHigh, XInc;
float YLow, YHigh, YInc;
bool RedrawGraph = true;
bool HaveFirstPoint[10];
float XPoint, YPoint, oXPoint[10], oYPoint[10], TextHeight;
float XDec = 0.0, YDec = 0.0;
char text[30];
byte oOrientation = 0;
float gx, gy, gw, gh;
int StartPointX, StartPointY;
char buf0[20], buf1[20], buf2[20], buf3[20], buf4[20], buf5[20], buf6[20], buf7[20], buf8[20], buf9[20];
char *dl[20] = {buf0, buf1, buf2, buf3, buf4, buf5, buf6, buf6, buf8, buf9};
char t[20];
char xa[20];
char ya[20];
uint16_t tc;
uint16_t dc[10];
uint16_t ac;
uint16_t gc;
uint16_t bc;
uint16_t pc;
byte pdia[20];
float MapFloat(float x, float in_min, float in_max, float out_min, float out_max);
};
class Dial {
public:
Dial(ILI9341_t3 *disp, int CenterX, int CenterY, int DialRadius, float LowVal , float HiVal , float ValInc, float SweepAngle);
void init(uint16_t NeedleColor, uint16_t DialColor, uint16_t TextColor, uint16_t TickColor, const char *Title, const ILI9341_t3_font_t &TitleFont , const ILI9341_t3_font_t &DataFont );
void draw(float val);
private:
bool Redraw = true;
ILI9341_t3 *d;
ILI9341_t3_font_t tf;
ILI9341_t3_font_t df;
char t[10];
int cx;
int cy;
int dr;
float lv;
float hv;
float inc;
float sa;
uint16_t nc;
uint16_t dc;
uint16_t tc;
uint16_t ic;
float degtorad;
float offset, stepval, angle, data;
float i;
//variables to track new needle values
float ix;
float iy;
float ox;
float oy;
float tx;
float ty;
float dx;
float dy;
float lx;
float rx;
float ly;
float ry;
int tLen, tHi;
char buf[38];
int dec;
// variables to track previous needle values
float px;
float py;
float pix;
float piy;
float plx;
float ply;
float prx;
float pry;
};
class SliderH {
public:
SliderH(ILI9341_t3 *Display); // class constructor
void init(uint16_t SliderX, uint16_t SliderY, uint16_t SliderW, float ScaleLow, float ScaleHi, float Scale, float Snap, uint16_t SliderColor, uint16_t BackgroundColor, uint16_t HandleColor); // initializer
void draw(float val); // method to draw complete slider
bool slide(float ScreenX, float ScreenY); // method to move handle as user drags finger over handle, this method automatically looks for a valid range press
void setColors(uint16_t SliderColor, uint16_t BackgroundColor, uint16_t HandleColor); // way to reset colors (useful for drawing enabled or disabled)
void setHandleColor(uint16_t HandleColor); // method to just draw the handle (useful for showing handle in green for OK value
void setDisableColor(uint16_t HandleColor, uint16_t SliderColor); // method to just draw the handle (useful for showing handle in green for OK value
void setHandleSize(int size);
void disable();
void enable();
void show();
void hide();
bool changed();
void setHandleShape(byte shape);
void drawSliderColor(bool color);
void setPressDebounce(byte Debounce);
float value;
private:
ILI9341_t3 *d; // the display object
uint16_t sColor; // the slider color
uint16_t bColor; // the slider background color
uint16_t hColor; // the sliders drag handle
uint16_t dsColor;
uint16_t dhColor;
uint16_t tsColor;
uint16_t ssColor;
uint16_t thColor;
uint16_t x;
uint16_t y;
uint16_t l; // the left coordinate of the scale
uint16_t t; // the top coordinate of the scale
uint16_t w; // the width of the scale
float ox; // the old screen x value where user pressed
bool enabled;
float sl; // the scale lower value
float sh; // the scale upper value
float pos; // the position on the scale
float sn; // the snap increment
float sc; // the scale increment
float ce; // the tick mark where zero is (for drawing heavy line on +/- scales
float i; // loop counter
int handlesize;
byte handleshape;
bool visible;
bool colorscale; // flag to draw slider in handle color
float MapFloat(float x, float fromLow, float fromHigh, float toLow, float toHigh); // why Arduino has no mapping for floats is beyond me, here it is...
byte debounce;
bool pressed = false;
bool SliderIsDrawn = false;
};
class SliderV {
public:
SliderV(ILI9341_t3 *Display); // class constructor
void init(uint16_t SliderX, uint16_t SliderY, uint16_t SliderH, float ScaleLow, float ScaleHi, float scale, float snap, uint16_t SliderColor, uint16_t BackgroundColor, uint16_t HandleColor); // initializer
void draw(float val); // method to draw complete slider
bool slide(uint16_t ScreenX, uint16_t ScreenY); // method to move handle as user drags finger over handle, this method automatically looks for a valid range press
void setColors(uint16_t SliderColor, uint16_t BackgroundColor, uint16_t HandleColor); // way to reset colors (useful for drawing enabled or disabled)
void setHandleColor(uint16_t HandleColor); // method to just draw the handle (useful for showing handle in green for OK value
void setHandleSize(int val);
void setHandleShape(byte val);
void drawSliderColor(bool val);
void setDisableColor(uint16_t HandleColor, uint16_t SliderColor); // method to just draw the handle (useful for showing handle in green for OK value
void setScale(float ScaleLow, float ScaleHi, float scale = 0.0, float snap= 0.0);
void disable();
void enable();
void show();
bool changed();
void hide();
void setPressDebounce(byte Debounce);
float value;
private:
ILI9341_t3 *d; // the display object
uint16_t sColor; // the slider color
uint16_t bColor; // the slider background color
uint16_t hColor; // the sliders drag handle
uint16_t dsColor;
uint16_t dhColor;
uint16_t tsColor;
uint16_t thColor;
uint16_t x; // the left coordinate of the scale
uint16_t y; // the top coordinate of the scale
uint16_t l; // the left coordinate of the scale
uint16_t t; // the top coordinate of the scale
uint16_t w; // the with of the scale
uint16_t h; // the with of the scale
float oy; // the old screen y value where user pressed
float sl; // the scale lower value
float sh; // the scale upper value
float pos; // the screen coordinate position
float sn; // the snap increment
float sc; // the scale increment
float ce; // the tick mark where zero is (for drawing heavy line on +/- scales
float i; // loop counter
byte tl;
bool colorscale; // flag to draw slider in handle color
float MapFloat(float x, float fromLow, float fromHigh, float toLow, float toHigh);// why Arduino has no mapping for floats is beyond me, here it is...
int tLen, tHi;
int handlesize;
byte handleshape;
bool enabled;
bool visible;
byte debounce;
bool pressed = false;
bool SliderIsDrawn = false;
};
class SliderOnOff {
public:
SliderOnOff(ILI9341_t3 *display, uint16_t SliderX, uint16_t SliderY, uint16_t SliderW, uint16_t SliderH, uint16_t SliderColor, uint16_t BackColor, uint16_t OnColor, uint16_t OffColor);// class constructor
void draw(bool state); // method to draw complete slider
bool slide(float ScreenX,float ScreenY); // method to move handle as user drags finger over handle, this method automatically looks for a valid range press
bool changed(); // method to return if state change, useful for determining if a something should be done but not done unless state change
bool getValue();
private:
ILI9341_t3 *_d; // the display object
uint16_t _sColor; // the slider color
uint16_t _bColor; // the slider background color
uint16_t _onColor; // the sliders on color
uint16_t _offColor; // the sliders on color
uint16_t _l; // the left coordinate of the scale
uint16_t _t; // the top coordinate of the scale
uint16_t _w; // the with of the scale
uint16_t _h; // the with of the scale
bool _pos; // the screen coordinate position
bool _changed; //flag to track if button was just changed
};
/*
Button class inspired by Adafruit, but added several methods
*/
class Button {
public:
Button(ILI9341_t3 *Display) {d = Display; }
void init(int16_t ButtonX, int16_t ButtonY, uint8_t ButtonWidth, uint8_t ButtonHeight,
uint16_t OutlineColor, uint16_t ButtonColor, uint16_t TextColor, uint16_t BackgroundColor,
const char *ButtonText, int TextOffsetX, int TextOffsetY, const ILI9341_t3_font_t &TextFont ) {
x = ButtonX;
y = ButtonY;
w = ButtonWidth;
h = ButtonHeight;
outlinecolor = OutlineColor;
fillcolor = ButtonColor;
textcolor = TextColor;
backcolor = BackgroundColor;
disablecolorfill = C_DISABLE_DARK;
disablecolortext = C_DISABLE_MED;
x_offset = TextOffsetX;
y_offset = TextOffsetY;
strncpy(label, ButtonText, 20);
f = TextFont;
ct = CORNER_AUTO;
drawit = true;
enabled = true;
visible = true;
debounce = TFT_DEBOUNCE;
value = 0; // user controlled for whatever....
newcorner = false;
}
void draw(bool inverted = false) {
uint16_t fill, outline, text;
if (!visible) {
return;
}
if (! inverted) {
drawit = true;
fill = fillcolor;
outline = outlinecolor;
text = textcolor;
}
else {
if (drawit == false) {
return;
}
drawit = false;
fill = disablecolorfill;
outline = outlinecolor;
text = fillcolor;
}
if (newcorner){
newcorner = false;
d->fillRect(x - (w/2), y - (h/2), w, h, backcolor);
}
if (!enabled) {
if (ct == CORNER_AUTO){
d->fillRoundRect(x - (w/2), y - (h/2), w, h, min(w,h)/4, disablecolorfill);
d->drawRoundRect(x - (w/2), y - (h/2), w, h, min(w,h)/4, disablecolortext);
}
else if(ct == CORNER_SQUARE) {
d->fillRect(x - (w/2), y - (h/2), w, h, disablecolorfill);
d->drawRect(x - (w/2), y - (h/2), w, h, disablecolortext);
}
else {
d->fillRoundRect(x - (w/2), y - (h/2), w, h, ct, disablecolorfill);
d->drawRoundRect(x - (w/2), y - (h/2), w, h, ct, disablecolortext);
}
d->setCursor(x + x_offset - (strlen(label)*3) , y + y_offset);
d->setFont(f);
d->setTextColor(disablecolortext);
d->print(label);
}
else{
if (ct == CORNER_AUTO){
d->fillRoundRect(x - (w/2), y - (h/2), w, h, min(w,h)/4, fill);
d->drawRoundRect(x - (w/2), y - (h/2), w, h, min(w,h)/4, outline);
}
else if(ct == CORNER_SQUARE) {
d->fillRect(x - (w/2), y - (h/2), w, h, fill);
d->drawRect(x - (w/2), y - (h/2), w, h, outline);
}
else {
d->fillRoundRect(x - (w/2), y - (h/2), w, h, ct, fill);
d->drawRoundRect(x - (w/2), y - (h/2), w, h, ct, outline);
}
d->setCursor(x + x_offset - (strlen(label)*3) , y + y_offset);
d->setFont(f);
d->setTextColor(text);
d->print(label);
}
}
bool press(int16_t ScreenX, int16_t ScreenY) {
if ((!enabled) || (!visible)) {
delay(debounce);
return false;
}
if ((ScreenX < (x - w/2)) || (ScreenX > (x + w/2))) {
return false;
}
if ((ScreenY < (y - h/2)) || (ScreenY > (y + h/2))) {
return false;
}
delay(debounce);
return true;
}
void show() {
visible = true;
}
void hide() {
if (ct == CORNER_AUTO){
d->fillRoundRect(x - (w/2), y - (h/2), w, h, min(w,h)/4,backcolor);
}
else if(ct == CORNER_SQUARE) {
d->fillRect(x - (w/2), y - (h/2), w, h, backcolor);
}
else {
d->fillRoundRect(x - (w/2), y - (h/2), w, h, ct, backcolor);
}
visible = false;
}
void disable() {
enabled = false;
}
void enable() {
enabled = true;
}
void resize(int16_t ButtonX, int16_t ButtonY, uint8_t ButtonW, uint8_t ButtonH) {
hide();
draw();
show();
x = ButtonX;
y = ButtonY;
w = ButtonW;
h = ButtonH;
}
void setColors(uint16_t OutlineColor, uint16_t ButtonColor, uint16_t TextColor, uint16_t BackgroundColor, uint16_t DisabledTextColor, uint16_t DisabledButtonColor) {
outlinecolor = OutlineColor;
fillcolor = ButtonColor;
textcolor = TextColor;
backcolor = BackgroundColor;
disablecolorfill = DisabledButtonColor;
disablecolortext = DisabledTextColor;
}
void setFont(int TextOffsetX, int TextOffsetY, const ILI9341_t3_font_t &TextFont) {
x_offset = TextOffsetX;
y_offset = TextOffsetY;
f = TextFont;
}
void setText(const char *ButtonText) {
strncpy(label, ButtonText, 20);
}
void setCornerRadius(int radius) {
newcorner = true;
ct = radius;
}
bool isEnabled() {
return enabled;
}
bool isVisible() {
return visible;
}
void setPressDebounce(byte Debounce) {
debounce = Debounce;
}
int value;
private:
ILI9341_t3 *d;
ILI9341_t3_font_t f;
int16_t x, y;
uint16_t w, h;
int x_offset, y_offset;
bool redraw;
uint16_t outlinecolor, fillcolor, textcolor, backcolor, disablecolorfill, disablecolortext;
char label[20];
boolean drawit;
bool enabled;
int ct;
bool visible;
byte debounce;
bool newcorner;
};
/*
Checkbox class
*/
class CheckBox {
public:
CheckBox(ILI9341_t3 *Display) {d = Display; }
void init(int16_t ButtonX, uint16_t ButtonY, uint16_t OutlineColor, uint16_t UPColor, uint16_t DownColor, uint16_t TextColor, uint16_t BackgroundColor, int TextOffsetX,int TextOffsetY, const char *Text, const ILI9341_t3_font_t &TextFont ) {
x = ButtonX ;
y = ButtonY + CHECKBOX_SIZE;
s = CHECKBOX_SIZE;
ct = 3;
oc = OutlineColor;
uc = UPColor;
dc = DownColor;
tc = TextColor;
bc = BackgroundColor;
doc = C_DISABLE_LIGHT;
duc = C_DISABLE_MED;
ddc = C_DISABLE_DARK;
dtc = C_DISABLE_MED;
strncpy(label,Text, 60);
tox = TextOffsetX;
toy = TextOffsetY;
f = TextFont;
enabled = true;
state = true; // true=up, false=down
visible = true;
value = -1;
debounce = TFT_DEBOUNCE;
}
void draw(bool val) {
uint16_t fill, outline, tcolor;
if (!visible) {
d->fillRoundRect(x, y-s, s, s, ct, bc);
return;
}
if (val){
// last exit was no press so restore val from internal saved state
val = state;
}
state = val;
// store the variable in the public variable
value = val;
if (state) {
fill = uc;
outline = oc;
tcolor = tc;
}
else {
fill = dc;
outline = oc;
tcolor = tc;
}
if (!enabled){
if (state) {
fill = duc;
outline = doc;
tcolor = dtc;
}
else {
fill = ddc;
outline = doc;
tcolor = dtc;
}
}
d->fillRoundRect(x, y-s, s, s, ct, fill);
d->drawRoundRect(x, y-s, s, s, ct, outline);
d->setCursor(x + tox+(s/2), y - s + toy);
d->setFont(f);
d->setTextColor(tcolor);
d->print(label);
}
bool press(int16_t ScreenX, int16_t ScreenY) {
bool pressed = false;
if ((!visible) || (!enabled)) {
return pressed;
}
if ( ( (ScreenX >= x) && (ScreenX <= (x + s) )) && ((ScreenY >= y-s) && (ScreenY <= (y))) ) {
state = !state;
draw(state);
delay(debounce);
// store the variable in the public variable
value = state;
pressed = true;
}
return pressed;
}
void show() {
visible = true;
}
void hide() {
visible = false;
}
void disable() {
enabled = false;
}
void enable() {
enabled = true;
}
void resize(int16_t ButtonX, int16_t ButtonY, uint8_t Size) {
hide();
draw(state);
x = ButtonX;
y = ButtonY;
s = Size;
show();
draw(state);
}
void setColors(uint16_t OutlineColor, uint16_t UPColor, uint16_t DownColor, uint16_t BackgroundColor, uint16_t DisableOutlineColor, uint16_t DisableTextColor, uint16_t DisableUPColor, uint16_t DisableDownColor) {
oc = OutlineColor;
uc = UPColor;
dc = DownColor;
bc = BackgroundColor;
doc = DisableOutlineColor;
duc = DisableUPColor;
ddc = DisableDownColor;
dtc = DisableTextColor;
}
void setText(int TextOffsetX,int TextOffsetY, const char *Text, const ILI9341_t3_font_t &TextFont) {
tox = TextOffsetX;
toy = TextOffsetY;
strncpy(label,Text, 60);
f = TextFont;
}
void setCornerRadius(int val) {
ct = val;
}
bool isEnabled() {
return enabled;
}
bool isVisibled() {
return visible;
}
void setPressDebounce(byte Debounce) {
debounce = Debounce;
}
bool value;
private:
ILI9341_t3 *d;
char label[60];
ILI9341_t3_font_t f;
int16_t x, y;
uint16_t s, ct;
uint16_t oc, uc, dc, bc, doc, duc, ddc, dtc, tc;
bool state;
int tox, toy;
bool enabled;
bool visible;
byte debounce;
};
/*
Checkbox class
*/
class OptionButton {
public:
OptionButton(ILI9341_t3 *Display) {d = Display; }
void init(uint16_t OutlineColor, uint16_t SelectedColor, uint16_t UnSelectedColor, int16_t TextColor, uint16_t BackgroundColor, int TextOffsetX,int TextOffsetY, const ILI9341_t3_font_t &TextFont) {
r = OPTION_BUTTON_RADIUS;
oc = OutlineColor;
sc = SelectedColor;
uc = UnSelectedColor;
bc = BackgroundColor;
tc = TextColor;
dsc = C_DISABLE_MED;
duc = C_DISABLE_DARK;
doc = C_DISABLE_LIGHT;
tox = TextOffsetX;
toy = TextOffsetY;
dtc = C_DISABLE_MED;
f = TextFont;
enabled = true;
current = 0;
ID = 0;
visible = true;
value = current;
debounce = TFT_DEBOUNCE;
}
int add(uint16_t ButtonX, uint16_t ButtonY,const char *Text, float OptionValue = -32001 ) {
strncpy(label[ID],Text, 60);