Add "actions" to buttons, allowing custom callbacks to be attached to buttons

This commit is contained in:
Jack Burgess 2022-03-13 00:47:05 +00:00
parent 1be1b4b05b
commit 4e7fb37a14
No known key found for this signature in database
GPG Key ID: 5078DE7F5C4CA72B
2 changed files with 22 additions and 0 deletions

View File

@ -8,6 +8,13 @@ TFT_eSPI_Button::TFT_eSPI_Button(void) {
_yd = 0;
_textdatum = MC_DATUM;
_label[9] = '\0';
_actionFunction = NULL;
}
// Set custom callback allowing arbitrary effects
void TFT_eSPI_Button::setButtonAction(ButtonActionFunction buttonActionFunction)
{
_actionFunction = buttonActionFunction;
}
// Classic initButton() function: pass center & size
@ -103,3 +110,10 @@ void TFT_eSPI_Button::press(bool p) {
bool TFT_eSPI_Button::isPressed() { return currstate; }
bool TFT_eSPI_Button::justPressed() { return (currstate && !laststate); }
bool TFT_eSPI_Button::justReleased() { return (!currstate && laststate); }
void TFT_eSPI_Button::action() {
if (_actionFunction != NULL) {
_actionFunction();
}
}

View File

@ -6,6 +6,8 @@
// within button
***************************************************************************************/
typedef void (*ButtonActionFunction)(void);
class TFT_eSPI_Button : public TFT_eSPI {
public:
@ -26,6 +28,10 @@ class TFT_eSPI_Button : public TFT_eSPI {
void drawButton(bool inverted = false, String long_name = "");
bool contains(int16_t x, int16_t y);
// Set custom callback allowing arbitrary effects
void setButtonAction(ButtonActionFunction actionFunction);
void action(void);
void press(bool p);
bool isPressed();
bool justPressed();
@ -41,4 +47,6 @@ class TFT_eSPI_Button : public TFT_eSPI {
char _label[10]; // Button text is 9 chars maximum unless long_name used
bool currstate, laststate; // Button states
ButtonActionFunction _actionFunction; // Action callback
};