DS_CAP_BUTTON - denis-stepanov/esp-ds-system GitHub Wiki
This capability adds support for an on-board button. It is a thin interface to AceButton for specific case of boards which come with a button on board, such as NodeMCU or WittyCloud. In principle, you could use it with an external button too, if you hook it up to ESP8266 GPIO0 in "active low" mode. The library initializes the button pin as INPUT_PULLUP
. Pin number is determined by a macro BUTTON_BUILTIN
(0
by default); if your pin is different, set this macro in MySystem.h
.
The following fields are implemented:
Field | Description | Default Value |
---|---|---|
ace_button::AceButton System::button |
Builtin button | BUTTON_BUILTIN |
void (*System::onButtonInit)() |
User code to initialize button | nullptr |
void (*System::onButtonPress)(ace_button::AceButton* button, uint8_t event_type, uint8_t button_state) |
Hook to be called when button is operated | nullptr |
button
object provides direct access to AceButton API, should you need it. AceButton, by default, initializes button to respond on short key presses. If you need to override this, or to add some other modes, put your code in a function (say, void myButtonInit()
) and assign it in your sketch to onButtonInit
as follows:
void (*System::onButtonInit)() = myButtonInit;
What the button will do is determined by the onButtonPress
function. The function signature follows AceButton signature for this hook; check AceButton documentation for more details.
- AceButton — button support implementation
- DS_CAP_WIFIMANAGER — if Wi-Fi manager is enabled, button long press will trigger Wi-Fi manager.
None.
MySystem.h
:
#define DS_CAP_BUTTON // Enable button
#include "System.h" // System global definitions
sketch.ino
:
#include "MySystem.h"
using namespace ds;
using namespace ace_button;
// Button event handler
void myButtonEvent(AceButton* /* button */, uint8_t event_type, uint8_t /* button_state */) {
if (event_type == AceButton::kEventPressed) {
// ... React on button press
}
}
void (*System::onButtonPress)(AceButton*, uint8_t, uint8_t) = myButtonEvent;
void setup() {
System::begin();
}
void loop() {
System::update();
}
System::begin() |
Required |
System::update() |
Required |
Note that AceButton library puts pretty strict limits on how often System::update()
must be called in order for button to work reliably. Avoid any blocking code in your loop()
when using a button.
None.
Version 1.0 or later.
None.