From 4e7fb37a14ba7186455760ecf30d766f289e9474 Mon Sep 17 00:00:00 2001 From: Jack Burgess Date: Sun, 13 Mar 2022 00:47:05 +0000 Subject: [PATCH] Add "actions" to buttons, allowing custom callbacks to be attached to buttons --- Extensions/Button.cpp | 14 ++++++++++++++ Extensions/Button.h | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/Extensions/Button.cpp b/Extensions/Button.cpp index 475c631..48dcc6c 100644 --- a/Extensions/Button.cpp +++ b/Extensions/Button.cpp @@ -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(); + } +} diff --git a/Extensions/Button.h b/Extensions/Button.h index 7e0c509..a21d28b 100644 --- a/Extensions/Button.h +++ b/Extensions/Button.h @@ -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 };