InputEvents v1.4.0
An easy to use but comprehensive Event Library for Buttons, Encoders, Encoder Buttons, Analog Inputs, Joysticks and Switches.
EventButton.h
1/*
2 * GPLv2 Licence https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
3 *
4 * Copyright (c) 2024 Philip Fletcher <philip.fletcher@stutchbury.com>
5 *
6 */
7
8#ifndef EVENT_BUTTON_H
9#define EVENT_BUTTON_H
10
11#include "Arduino.h"
12#include "EventInputBase.h"
13#include "PinAdapter/FoltmanDebounceAdapter.h"
14#include "PinAdapter/GpioPinAdapter.h"
15
36
37 protected:
38
39 #if defined(FUNCTIONAL_SUPPORTED)
43 typedef std::function<void(InputEventType et, EventButton &ie)> CallbackFunction;
44 #else
49 #endif
50
55
56
57 public:
58
60
68 EventButton(byte buttonPin, bool useDefaultDebouncer=true);
69
75 EventButton(PinAdapter* _pinAdapter, bool useDefaultDebouncer=true);
76
83 EventButton(PinAdapter* _pinAdapter, DebounceAdapter* debounceAdapter);
84
86
87
89
101 void begin();
102
110 callbackIsSet = true;
111 }
112
122 #if defined(FUNCTIONAL_SUPPORTED)
123 template <typename T>
124 void setCallback(T* instance, void (T::*method)(InputEventType, EventButton&)) {
125 // Wrap the method call in a lambda
126 callbackFunction = [instance, method](InputEventType et, EventButton& ie) {
127 (instance->*method)(et, ie); // Call the member function on the instance
128 };
129 callbackIsSet = true;
130 }
131 #endif
132
138 void unsetCallback() override;
139
145 void update();
146
148
149
151
161 uint8_t clickCount() { return prevClickCount; }
162
163
170 uint8_t longPressCount() { return longPressCounter; }
171
175 bool isPressed() { return currentState == pressedState; }
176
182 uint32_t currentDuration();
183
189 uint32_t previousDuration() { return durationOfPreviousState; }
190
192
193
195
203 void enableLongPressRepeat(bool repeat=true) { repeatLongPress = repeat; }
204
211 void setLongClickDuration(uint16_t longDurationMs=750) { longClickDuration = longDurationMs; }
212
219 void setLongPressInterval(uint16_t intervalMs=500) { longPressInterval = intervalMs; }
220
226 void setMultiClickInterval(uint16_t intervalMs=250) { multiClickInterval = intervalMs; }
227
234 void setDebouncer(DebounceAdapter* debounceAdapter);
235
242 bool setDebounceInterval(uint16_t intervalMs=10);
243
253 void setPressedState(bool state = LOW){ pressedState = state; }
254
256
257
258 protected:
259
265 void invoke(InputEventType et) override;
266
267
272 void onDisabled() override;
273
279 void changeState(bool newState);
280
287 bool changedState();
288
295 bool changedPinState();
296
303 bool releasing() { return stateChanged && previousState == pressedState; }
304
311 bool pressing() { return stateChanged && previousState != pressedState; }
312
313 private:
314
315 PinAdapter* pinAdapter;
316 DebounceAdapter* debouncer;
317
318 bool pressedState = LOW; //The state that represents 'pressed'
319
320 //state
321
322 bool currentPinState = HIGH;
323 bool previousPinState = HIGH;
324
325 bool currentState = HIGH; //set via PinAdapter->read()
326 bool previousState = HIGH;
327 bool stateChanged = false;
328 uint32_t stateChangeLastTime;
329 uint32_t durationOfPreviousState;
330
331 uint8_t clickCounter = 0;
332 uint8_t prevClickCount = 0;
333 bool clickFired = true;
334
335 //setup
336 uint16_t multiClickInterval = 250;
337 uint16_t longClickDuration = 750;
338 bool repeatLongPress = true;
339 uint16_t longPressInterval = 500;
340 uint16_t longPressCounter = 0;
341
342
343
344};
345
346
347#endif
348
349
350
351
352
353
354
355
356
357
InputEventType
A list of all events that can be fired by InputEvents classes.
Definition: InputEvents.h:55
This is the interface/base class for debounce adapters.
Definition: DebounceAdapter.h:11
The EventButton class is for momentary inputs. The momentary switch (button) must be wired between th...
Definition: EventButton.h:35
bool changedState()
Returns true if either pinAdapter, press() or release() changed the button state.
Definition: EventButton.cpp:110
bool pressing()
Returns true if state has changed and previous state is not pressedState.
Definition: EventButton.h:311
void begin()
Initialise the EventButton.
Definition: EventButton.cpp:35
void update()
Update the state from the pin input.
Definition: EventButton.cpp:49
uint8_t longPressCount()
The number of times the long press has occurred during a button press.
Definition: EventButton.h:170
bool isPressed()
Returns true if button is currently pressed.
Definition: EventButton.h:175
void setDebouncer(DebounceAdapter *debounceAdapter)
Set the debouncer. Note: When planning to use setDebouncer() you must ensure useDefaultDebouncer is s...
Definition: EventButton.cpp:137
bool releasing()
Returns true if state has changed and previous state is pressedState.
Definition: EventButton.h:303
void setPressedState(bool state=LOW)
Set the pin state that represents 'pressed'. By default this is LOW (ie pulled down for pressed).
Definition: EventButton.h:253
bool setDebounceInterval(uint16_t intervalMs=10)
Set the DebounceAdapter debounce interval. Default is 10ms.
Definition: EventButton.cpp:145
EventButton(byte buttonPin, bool useDefaultDebouncer=true)
Construct an EventButton with a GPIO pin.
Definition: EventButton.cpp:12
void setCallback(T *instance, void(T::*method)(InputEventType, EventButton &))
Set the Callback function to a class method.
Definition: EventButton.h:124
void unsetCallback() override
Unset a previously set callback function or method.
Definition: EventButton.cpp:44
void changeState(bool newState)
Change the button state and flag as changed.
Definition: EventButton.cpp:129
uint32_t previousDuration()
Duration in milliseconds of the previous state.
Definition: EventButton.h:189
uint8_t clickCount()
The number of clicks that have been fired in the MULTI_CLICKED event.
Definition: EventButton.h:161
std::function< void(InputEventType et, EventButton &ie)> CallbackFunction
If std::function is supported, this creates the callback type.
Definition: EventButton.h:43
CallbackFunction callbackFunction
The callback function member.
Definition: EventButton.h:54
bool changedPinState()
Returns true if pinAdapter read() has changed since last call.
Definition: EventButton.cpp:122
void setMultiClickInterval(uint16_t intervalMs=250)
Set the multi click interval.
Definition: EventButton.h:226
void setCallback(CallbackFunction f)
Set the Callback function.
Definition: EventButton.h:108
void setLongPressInterval(uint16_t intervalMs=500)
Set the number of milliseconds that define the subbsequent long click intervals.
Definition: EventButton.h:219
uint32_t currentDuration()
Duration in milliseconds of the current state.
Definition: EventButton.cpp:153
void invoke(InputEventType et) override
Override of the EventInputBase::invoke() virtual method.
Definition: EventButton.cpp:97
void enableLongPressRepeat(bool repeat=true)
Choose whether to repeat the long press callback. (true by default)
Definition: EventButton.h:203
void setLongClickDuration(uint16_t longDurationMs=750)
Set the number of milliseconds that define the first long click duration.
Definition: EventButton.h:211
void onDisabled() override
Override base method to reset click and pressed counts.
Definition: EventButton.cpp:103
The common base for InputEvents input classes.
Definition: EventInputBase.h:26
bool callbackIsSet
Required because in C/C++ callback has to be defined in derived classes... :-/.
Definition: EventInputBase.h:184
The interface specification for button, encoder button and switch pins.
Definition: PinAdapter.h:8