-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathSingleButtonUsingIEventHandler.ino
90 lines (73 loc) · 2.7 KB
/
SingleButtonUsingIEventHandler.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* A demo of a single AceButton which handles events using the IEventHandler
* object, instead of the EventHandler function. Similar to SingleButton.ino.
*/
#include <AceButton.h>
using namespace ace_button;
// The pin number attached to the button.
const int BUTTON_PIN = 2;
#ifdef ESP32
// Different ESP32 boards use different pins
const int LED_PIN = 2;
#else
const int LED_PIN = LED_BUILTIN;
#endif
// LED states. Some microcontrollers wire their built-in LED the reverse.
const int LED_ON = HIGH;
const int LED_OFF = LOW;
// One button wired to the pin at BUTTON_PIN. Automatically uses the default
// ButtonConfig. The alternative is to call the AceButton::init() method in
// setup() below.
AceButton button(BUTTON_PIN);
// An object that handles the event.
class ButtonHandler: public IEventHandler {
public:
void handleEvent(AceButton* /* button */, uint8_t eventType,
uint8_t buttonState) override {
// Print out a message for all events.
Serial.print(F("handleEvent(): eventType: "));
Serial.print(AceButton::eventName(eventType));
Serial.print(F("; buttonState: "));
Serial.println(buttonState);
// Control the LED only for the Pressed and Released events.
// Notice that if the MCU is rebooted while the button is pressed down, no
// event is triggered and the LED remains off.
switch (eventType) {
case AceButton::kEventPressed:
digitalWrite(LED_PIN, LED_ON);
break;
case AceButton::kEventReleased:
digitalWrite(LED_PIN, LED_OFF);
break;
}
}
};
ButtonHandler handleEvent;
void setup() {
delay(1000); // some microcontrollers reboot twice
Serial.begin(115200);
while (! Serial); // Wait until Serial is ready - Leonardo/Micro
Serial.println(F("setup(): begin"));
// initialize built-in LED as an output
pinMode(LED_PIN, OUTPUT);
// Button uses the built-in pull up register.
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Configure the ButtonConfig with the event handler, and enable all higher
// level events.
ButtonConfig* buttonConfig = button.getButtonConfig();
buttonConfig->setIEventHandler(&handleEvent);
buttonConfig->setFeature(ButtonConfig::kFeatureClick);
buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick);
buttonConfig->setFeature(ButtonConfig::kFeatureLongPress);
buttonConfig->setFeature(ButtonConfig::kFeatureRepeatPress);
// Check if the button was pressed while booting
if (button.isPressedRaw()) {
Serial.println(F("setup(): button was pressed while booting"));
}
Serial.println(F("setup(): ready"));
}
void loop() {
// Should be called every 4-5ms or faster, for the default debouncing time
// of ~20ms.
button.check();
}