-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #655 from SignalK/button_ui
Button Handler
- Loading branch information
Showing
11 changed files
with
237 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#ifndef SENSESP_SRC_SENSESP_SYSTEM_BASE_BUTTON_H_ | ||
#define SENSESP_SRC_SENSESP_SYSTEM_BASE_BUTTON_H_ | ||
|
||
#include "AceButton.h" | ||
#include "elapsedMillis.h" | ||
#include "sensesp.h" | ||
#include "sensesp/system/configurable.h" | ||
#include "sensesp/system/startable.h" | ||
#include "sensesp_base_app.h" | ||
|
||
namespace sensesp { | ||
|
||
using namespace ace_button; | ||
|
||
/** | ||
* @brief Base class for button handlers. | ||
* | ||
* Button handlers are used to handle button presses. This is an abstract base | ||
* class that should be extended to implement a specific button handler. | ||
*/ | ||
class BaseButtonHandler : public Configurable, public Startable, public IEventHandler { | ||
public: | ||
BaseButtonHandler(int pin, String config_path = "") | ||
: Configurable{config_path}, Startable(20) { | ||
button_ = new AceButton(pin); | ||
pinMode(pin, INPUT_PULLUP); | ||
} | ||
|
||
virtual void start() override { | ||
ButtonConfig* button_config = button_->getButtonConfig(); | ||
button_config->setIEventHandler(this); | ||
button_config->setFeature(ButtonConfig::kFeatureLongPress); | ||
button_config->setFeature(ButtonConfig::kFeatureSuppressAfterLongPress); | ||
|
||
debugD("Button handler started"); | ||
|
||
ReactESP::app->onRepeat(4, [this]() { this->button_->check(); }); | ||
} | ||
|
||
protected: | ||
AceButton* button_; | ||
}; | ||
|
||
|
||
} // namespace sensesp | ||
|
||
#endif // SENSESP_SRC_SENSESP_SYSTEM_BASE_BUTTON_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include "sensesp/system/button.h" | ||
|
||
#include "sensesp.h" | ||
#include "sensesp_app.h" | ||
|
||
namespace sensesp { | ||
|
||
void ButtonHandler::handle_long_press() { | ||
debugD("Resetting network settings"); | ||
SensESPApp::get()->get_networking()->reset(); | ||
} | ||
|
||
} // namespace sensesp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef SENSESP_SRC_SENSESP_SYSTEM_BUTTON_H_ | ||
#define SENSESP_SRC_SENSESP_SYSTEM_BUTTON_H_ | ||
|
||
#define DEFAULT_BUTTON_PIN 2 | ||
|
||
#include "AceButton.h" | ||
#include "elapsedMillis.h" | ||
#include "sensesp.h" | ||
#include "sensesp/system/configurable.h" | ||
#include "sensesp/system/minimal_button.h" | ||
#include "sensesp/system/startable.h" | ||
|
||
namespace sensesp { | ||
|
||
using namespace ace_button; | ||
|
||
/** | ||
* @brief Default Button Handler for SensESPApp applications. | ||
* | ||
* This button handler implements restart on short press, network settings reset | ||
* on a long press, and factory reset on a very long press. | ||
*/ | ||
class ButtonHandler : public MinimalButtonHandler { | ||
public: | ||
ButtonHandler(int pin, String config_path = "") | ||
: MinimalButtonHandler(pin, config_path) {} | ||
|
||
protected: | ||
virtual void handle_long_press() override; | ||
}; | ||
|
||
} // namespace sensesp | ||
|
||
#endif // SENSESP_SRC_SENSESP_SYSTEM_BUTTON_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#ifndef SENSESP_SRC_SENSESP_SYSTEM_MINIMAL_BUTTON_H_ | ||
#define SENSESP_SRC_SENSESP_SYSTEM_MINIMAL_BUTTON_H_ | ||
|
||
#include "AceButton.h" | ||
#include "elapsedMillis.h" | ||
#include "sensesp.h" | ||
#include "sensesp/system/base_button.h" | ||
#include "sensesp/system/configurable.h" | ||
#include "sensesp/system/startable.h" | ||
#include "sensesp_base_app.h" | ||
|
||
namespace sensesp { | ||
|
||
using namespace ace_button; | ||
|
||
/** | ||
* @brief Minimal Button Handler | ||
* | ||
* This is a minimal button handler that implements restart on short press and | ||
* factory reset on a very long press. | ||
* | ||
* This class may be extended to implement more complex button handlers. | ||
*/ | ||
class MinimalButtonHandler : public BaseButtonHandler { | ||
public: | ||
MinimalButtonHandler(int pin, String config_path = "") | ||
: BaseButtonHandler(pin, config_path) {} | ||
|
||
virtual void handleEvent(AceButton* button, uint8_t event_type, | ||
uint8_t button_state) override { | ||
debugD("Button event: %d", event_type); | ||
switch (event_type) { | ||
case AceButton::kEventPressed: | ||
time_since_press_event = 0; | ||
debugD("Press"); | ||
break; | ||
case AceButton::kEventLongReleased: | ||
debugD("Long release, duration: %d", time_since_press_event); | ||
if (time_since_press_event > 5000) { | ||
this->handle_very_long_press(); | ||
} else if (time_since_press_event > 1000) { | ||
this->handle_long_press(); | ||
} | ||
break; | ||
case AceButton::kEventReleased: | ||
this->handle_button_press(); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
protected: | ||
elapsedMillis time_since_press_event; | ||
/** | ||
* @brief Handle a brief button press (less than one second). | ||
* | ||
*/ | ||
virtual void handle_button_press() { | ||
debugD("Short release, duration: %d", time_since_press_event); | ||
debugD("Restarting"); | ||
ESP.restart(); | ||
} | ||
/** | ||
* @brief Handle a long button press (over 1 second). | ||
* | ||
*/ | ||
virtual void handle_long_press() { | ||
handle_button_press(); | ||
} | ||
/** | ||
* @brief Handle a very long button press (over 5 seconds). | ||
* | ||
*/ | ||
virtual void handle_very_long_press() { | ||
debugD("Performing a factory reset"); | ||
SensESPBaseApp::get()->reset(); | ||
} | ||
}; | ||
|
||
|
||
} // namespace sensesp | ||
|
||
#endif // SENSESP_SRC_SENSESP_SYSTEM_BASE_BUTTON_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters