#Ifndef - Buttonwidgeth - #Define - Buttonwidgeth - #Include #Include
#Ifndef - Buttonwidgeth - #Define - Buttonwidgeth - #Include #Include
2 // The following button class has been ported over from the Adafruit_tft library.
3 // A slightly different implementation in this ButtonWidget library allows:
4 //
5 // 1. The button labels to be in any font
6 // 2. Allow longer labels
7 // 3. Allow text datum to be set
8 // 4. Allow invert state to be read via getState(), true = inverted
9 // 5. Define pressed and released callbacks per button instances
10 //
11 // The library uses functions present in TFT_eSPI
12 // https://github.com/Bodmer/TFT_eSPI
13 ***************************************************************************************/
14 #ifndef _ButtonWidgetH_
15 #define _ButtonWidgetH_
16
17 //Standard support
18 #include <Arduino.h>
19
20 #include <TFT_eSPI.h>
21
22 //#include <functional>
23 //typedef std::function<void(void)> actionCallback;
24 typedef void (*actionCallback)(void);
25 static void dummyButtonAction(void) { }; // In case user callback is not defined!
26
27 class ButtonWidget : public TFT_eSPI {
28
29 public:
30
31 ButtonWidget(TFT_eSPI *tft);
32
33 // "Classic" initButton() uses centre & size
34 void initButton(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t outline,
uint16_t fill, uint16_t textcolor, char *label, uint8_t textsize);
35
36 // New/alt initButton() uses upper-left corner & size
37 void initButtonUL(int16_t x1, int16_t y1, uint16_t w, uint16_t h, uint16_t outline,
uint16_t fill, uint16_t textcolor, char *label, uint8_t textsize);
38
39 // Adjust text datum and x, y deltas
40 void setLabelDatum(int16_t x_delta, int16_t y_delta, uint8_t datum = MC_DATUM);
41
42 actionCallback pressAction = dummyButtonAction; // Press action callback
43 actionCallback releaseAction = dummyButtonAction; // Release action callback
44
45 void setPressAction(actionCallback action);
46 void setReleaseAction(actionCallback action);
47
48 void setPressTime(uint32_t pressTime) { _pressTime = pressTime; }
49 void setReleaseTime(uint32_t releaseTime){ _releaseTime = releaseTime; }
50 uint32_t getPressTime(void) { return _pressTime; }
51 uint32_t getReleaseTime(void){ return _releaseTime; }
52
53
54
55 void drawButton(bool inverted = false, String long_name = "");
56 void drawSmoothButton(bool inverted = false, int16_t outlinewidth = -1, uint32_t
bgcolor = 0x00FFFFFF, String long_name = "");
57 bool contains(int16_t x, int16_t y);
58
59 void press(bool p);
60 bool isPressed();
61 bool justPressed();
62 bool justReleased();
63
64 bool getState(void) {return _inverted;} // Get inverted state, true =
inverted
65
66
67 private:
68 TFT_eSPI *_tft;
69 int16_t _x1, _y1; // Coordinates of top-left corner of button
70 int16_t _xd, _yd; // Button text datum offsets (wrt centre of button)
71 uint16_t _w, _h; // Width and height of button
72 uint8_t _textsize, _textdatum; // Text size multiplier and text datum for button
73 uint16_t _outlinecolor, _fillcolor, _textcolor, _outlinewidth, _bgcolor;
74 char _label[10]; // Button text is 9 chars maximum unless long_name used
75 uint32_t _pressTime, _releaseTime;
76 bool _inverted, _currstate, _laststate; // Button states
77 };
78
79 #endif
80