InputEvents v1.4.0
An easy to use but comprehensive Event Library for Buttons, Encoders, Encoder Buttons, Analog Inputs, Joysticks and Switches.
EventSwitch.h
1/*
2 *
3 * GPLv2 Licence https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
4 *
5 * Copyright (c) 2024 Philip Fletcher <philip.fletcher@stutchbury.com>
6 *
7 */
8
9#ifndef EVENT_SWITCH_H
10#define EVENT_SWITCH_H
11
12#include "Arduino.h"
13#include "EventInputBase.h"
14#include "PinAdapter/FoltmanDebounceAdapter.h"
15#include "PinAdapter/GpioPinAdapter.h"
16
34
35protected:
36
37 #if defined(FUNCTIONAL_SUPPORTED)
41 typedef std::function<void(InputEventType et, EventSwitch &ie)> CallbackFunction;
42 #else
47 #endif
48
53
59 void invoke(InputEventType et) override;
60
61public:
62
64
72 EventSwitch(byte switchPin, bool useDefaultDebouncer=true);
73
79 EventSwitch(PinAdapter* _pinAdapter, bool useDefaultDebouncer=true);
80
87 EventSwitch(PinAdapter* _pinAdapter, DebounceAdapter* debounceAdapter);
88
89
91
93
105 void begin();
106
114 callbackIsSet = true;
115 }
116
126 #if defined(FUNCTIONAL_SUPPORTED)
127 // Method to set callback with instance and class method
128 template <typename T>
129 void setCallback(T* instance, void (T::*method)(InputEventType, EventSwitch&)) {
130 // Wrap the method call in a lambda
131 callbackFunction = [instance, method](InputEventType et, EventSwitch &ie) {
132 (instance->*method)(et, ie); // Call the member function on the instance
133 };
134 callbackIsSet = true;
135 }
136 #endif
137
143 void unsetCallback() override;
144
150 void update();
152
154
165 bool isOn() { return currentState == onState; }
166
173 bool isOff() { return currentState != onState; }
174
178 unsigned long currentDuration();
179
183 unsigned long previousDuration() { return durationOfPreviousState; }
185
186
188
197 void setDebouncer(DebounceAdapter* debounceAdapter);
198
205 bool setDebounceInterval(unsigned int intervalMs=10);
206
216 void setOnState(bool state = LOW) { onState = state; }
218
219 protected:
226 bool changedState();
227
234 bool changedPinState();
235
241 void changeState(bool newState);
242
249 bool turningOff() { return stateChanged && previousState == onState; }
250
257 bool turningOn() { return stateChanged && previousState != onState; }
258
259
260private:
261
262 PinAdapter* pinAdapter;
263 DebounceAdapter* debouncer;
264
265 bool onState = LOW; //The state that represents 'pressed'
266
267 unsigned char currentState = HIGH;
268 bool previousState = HIGH;
269
270 bool currentPinState = HIGH;
271 bool previousPinState = HIGH;
272
273 bool stateChanged = false;
274 uint32_t stateChangeLastTime;
275 uint32_t durationOfPreviousState;
276
277
278
279};
280
281#endif
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 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 EventSwitch class is for standard on/off inputs. Like the EventButton the switch must be wired be...
Definition: EventSwitch.h:33
void unsetCallback() override
Unset a previously set callback function or method.
Definition: EventSwitch.cpp:45
unsigned long currentDuration()
Duration in milliseconds of the current state.
Definition: EventSwitch.cpp:114
void begin()
Initialise the EventButton.
Definition: EventSwitch.cpp:36
bool setDebounceInterval(unsigned int intervalMs=10)
Set the DebounceAdapter debounce interval. Default is 10ms.
Definition: EventSwitch.cpp:106
std::function< void(InputEventType et, EventSwitch &ie)> CallbackFunction
If std::function is supported, this creates the callback type.
Definition: EventSwitch.h:41
unsigned long previousDuration()
Duration in milliseconds of the previous state.
Definition: EventSwitch.h:183
void setCallback(CallbackFunction f)
Set the Callback function.
Definition: EventSwitch.h:112
void invoke(InputEventType et) override
Override of the EventInputBase::invoke() virtual method.
Definition: EventSwitch.cpp:66
bool turningOn()
Returns true if state has changed and previous state is not onState.
Definition: EventSwitch.h:257
CallbackFunction callbackFunction
The callback function member.
Definition: EventSwitch.h:52
bool changedPinState()
Returns true if pinAdapter read() has changed since last call.
Definition: EventSwitch.cpp:92
void changeState(bool newState)
Change the switch state and flag as changed.
Definition: EventSwitch.cpp:98
bool turningOff()
Returns true if state has changed and previous state is onState.
Definition: EventSwitch.h:249
void setDebouncer(DebounceAdapter *debounceAdapter)
Set the debouncer. Note: When planning to use setDebouncer() you must ensure useDefaultDebouncer is s...
Definition: EventSwitch.cpp:72
void setCallback(T *instance, void(T::*method)(InputEventType, EventSwitch &))
Set the Callback function to a class method.
Definition: EventSwitch.h:129
EventSwitch(byte switchPin, bool useDefaultDebouncer=true)
Construct an EventSwitch input.
Definition: EventSwitch.cpp:12
bool changedState()
Returns true if pinAdapter changed the switch state.
Definition: EventSwitch.cpp:80
void update()
Update the state from the switch pin input.
Definition: EventSwitch.cpp:50
bool isOn()
Return true if switch is on.
Definition: EventSwitch.h:165
void setOnState(bool state=LOW)
Set the pin state that represents 'on'. By default this is LOW (ie pulled down).
Definition: EventSwitch.h:216
bool isOff()
Return true if switch is off.
Definition: EventSwitch.h:173
The interface specification for button, encoder button and switch pins.
Definition: PinAdapter.h:8