![]() |
InputEvents v1.4.0
An easy to use but comprehensive Event Library for Buttons, Encoders, Encoder Buttons, Analog Inputs, Joysticks and Switches.
|
The EventSwitch class is for standard on/off inputs. Like the EventButton the switch must be wired between the pin and GND.
When the switch is closed (LOW) its state will be ON and when open (HIGH) it will be OFF although this behaviour can be reversed with the reverseOnOff() method.
A quick explainer: The switch pin is configured with the microcontroller's internal INPUT_PULLUP resistor. In this configuration, without anything attached, the pin will read as HIGH because it has been 'pulled up' to the voltage of the board (either 3.3V or 5V). When you attach a switch (or button) that is wired from the pin to GND, if the switch is closed (or the button pressed), the weak pullup resistor is overcome by the closed switch 'pulling' the pin to GND (0V) or LOW.
The following InputEventTypes are fired by EventSwitch:
ENABLED
& DISABLED
) has been fired for a specified time. Each input can define its own idle timeout. Default is 10 seconds.#include <EventSwitch.h>
Public Member Functions | |
Constructors | |
EventSwitch (byte switchPin, bool useDefaultDebouncer=true) | |
Construct an EventSwitch input. More... | |
EventSwitch (PinAdapter *_pinAdapter, bool useDefaultDebouncer=true) | |
Construct a new EventSwitch with a PinAdapter and optionally use the default debouncer. More... | |
EventSwitch (PinAdapter *_pinAdapter, DebounceAdapter *debounceAdapter) | |
Construct a new EventSwitch with a PinAdapter and a DebounceAdapter. More... | |
Common Methods | |
These methods are common to all InputEvent classes. Additional methods for input enable, timeout, event blocking and user ID/value are also inherited from the EventInputBase class. | |
void | begin () |
Initialise the EventButton. More... | |
void | setCallback (CallbackFunction f) |
Set the Callback function. More... | |
template<typename T > | |
void | setCallback (T *instance, void(T::*method)(InputEventType, EventSwitch &)) |
Set the Callback function to a class method. More... | |
void | unsetCallback () override |
Unset a previously set callback function or method. More... | |
void | update () |
Update the state from the switch pin input. More... | |
Getting the State | |
These methods return the current state of the input (as set during the last update() ) | |
bool | isOn () |
Return true if switch is on. More... | |
bool | isOff () |
Return true if switch is off. More... | |
unsigned long | currentDuration () |
Duration in milliseconds of the current state. | |
unsigned long | previousDuration () |
Duration in milliseconds of the previous state. | |
Other Configuration Settings | |
void | setDebouncer (DebounceAdapter *debounceAdapter) |
Set the debouncer. Note: When planning to use setDebouncer() you must ensure useDefaultDebouncer is set to false in the button or switch constructor. Previously set debouncers are not deleted. More... | |
bool | setDebounceInterval (unsigned int intervalMs=10) |
Set the DebounceAdapter debounce interval. Default is 10ms. More... | |
void | setOnState (bool state=LOW) |
Set the pin state that represents 'on'. By default this is LOW (ie pulled down). More... | |
![]() | |
bool | isCallbackSet () |
Returns true if the callback is set. | |
void | update () |
Update the state of the input. More... | |
bool | isEnabled () |
Returns true if input is enabled. More... | |
void | enable (bool e=true) |
Enable or disable an input. More... | |
void | setIdleTimeout (unsigned int timeoutMs=10000) |
Set the idle timeout in milliseconds (default 10000, 10 seconds) More... | |
unsigned long | msSinceLastEvent () |
Returns the number of ms since any event was fired for this input. | |
bool | isIdle () |
Return true if no activity for longer than setIdleTimeout - irrespective of whether the idle (or changed) callback has been fired. More... | |
void | resetIdleTimer () |
Reset the idle timer. The IDLE event will fire setIdleTimeout ms after this is called. More... | |
void | blockEvent (InputEventType et) |
Stop an event from firing. More... | |
void | allowEvent (InputEventType et) |
Allow a a previously blocked event tto fire. More... | |
void | blockAllEvents () |
Stop all events from firing - usually used in conjunction with allowEvent() | |
void | allowAllEvents () |
Clear all blocked events. | |
bool | isEventAllowed (InputEventType et) |
Returns true if the event is not blocked. | |
void | setInputId (uint8_t id) |
Set the input ID (for use by user, not used internally). Not unique, default is 0. | |
uint8_t | getInputId () |
Get the input ID (for use by user, not used internally). Not unique, default is 0. | |
void | setInputValue (uint8_t val) |
Set the input value (for use by user, not used internally). Not unique, default is 0. | |
uint8_t | getInputValue () |
Get the input value (for use by user, not used internally). Not unique, default is 0. | |
Protected Types | |
typedef std::function< void(InputEventType et, EventSwitch &ie)> | CallbackFunction |
If std::function is supported, this creates the callback type. | |
typedef void(* | CallbackFunction) (InputEventType et, EventSwitch &) |
Used to create the callback type as pointer if std::function is not supported. | |
Protected Member Functions | |
void | invoke (InputEventType et) override |
Override of the EventInputBase::invoke() virtual method. More... | |
bool | changedState () |
Returns true if pinAdapter changed the switch state. More... | |
bool | changedPinState () |
Returns true if pinAdapter read() has changed since last call. More... | |
void | changeState (bool newState) |
Change the switch state and flag as changed. More... | |
bool | turningOff () |
Returns true if state has changed and previous state is onState. More... | |
bool | turningOn () |
Returns true if state has changed and previous state is not onState. More... | |
![]() | |
bool | isInvokable (InputEventType et) |
virtual void | invoke (InputEventType et)=0 |
To be overriden by derived classes. More... | |
virtual void | onEnabled () |
Can be ovrriden by derived classes but base method must be called. More... | |
virtual void | onDisabled () |
Can be ovrriden by derived classes but base method must be called. More... | |
virtual void | onIdle () |
Can be ovrriden by derived classes but base method must be called. More... | |
Protected Attributes | |
CallbackFunction | callbackFunction = nullptr |
The callback function member. | |
![]() | |
uint8_t | input_id = 0 |
Input ID, not used internally. | |
uint8_t | input_value = 0 |
Input value, not used internally. | |
bool | _enabled = true |
Input enabled flag. | |
bool | idleFlagged = true |
True if input is idle. | |
unsigned long | lastEventMs = millis() |
number of milliseconds since the last event | |
unsigned long | idleTimeout = 10000 |
The idle timeout in milliseconds. | |
bool | callbackIsSet = false |
Required because in C/C++ callback has to be defined in derived classes... :-/. | |
EventSwitch::EventSwitch | ( | byte | pin, |
bool | useDefaultDebouncer = true |
||
) |
Construct an EventSwitch input.
switchPin | A pin that connects to GNG via the switch |
GPLv2 Licence https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
Copyright (c) 2024 Philip Fletcher phili.nosp@m.p.fl.nosp@m.etche.nosp@m.r@st.nosp@m.utchb.nosp@m.ury..nosp@m.com
EventSwitch::EventSwitch | ( | PinAdapter * | _pinAdapter, |
bool | useDefaultDebouncer = true |
||
) |
Construct a new EventSwitch with a PinAdapter and optionally use the default debouncer.
pinAdapter |
EventSwitch::EventSwitch | ( | PinAdapter * | _pinAdapter, |
DebounceAdapter * | debounceAdapter | ||
) |
Construct a new EventSwitch with a PinAdapter and a DebounceAdapter.
pinAdapter | |
debounceAdapter |
|
virtual |
|
protected |
Returns true if pinAdapter read() has changed since last call.
|
protected |
Returns true if pinAdapter changed the switch state.
|
protected |
Change the switch state and flag as changed.
newState |
|
overrideprotectedvirtual |
Override of the EventInputBase::invoke()
virtual method.
et | Enum of type InputEventType |
Implements EventInputBase.
|
inline |
Return true if switch is off.
|
inline |
Return true if switch is on.
|
inline |
Set the Callback function.
f | A function of type EventSwitch::CallbackFunction type. |
|
inline |
Set the Callback function to a class method.
Note: This method is only available if std:function
is supported.
instance | The instance of a class implementing a CallbackFunction method. |
method | The class method of type EventSwitch::CallbackFunction type. |
bool EventSwitch::setDebounceInterval | ( | unsigned int | intervalMs = 10 | ) |
Set the DebounceAdapter debounce interval. Default is 10ms.
void EventSwitch::setDebouncer | ( | DebounceAdapter * | debounceAdapter | ) |
Set the debouncer. Note: When planning to use setDebouncer()
you must ensure useDefaultDebouncer
is set to false
in the button or switch constructor. Previously set debouncers are not deleted.
debounceAdapter |
|
inline |
Set the pin state that represents 'on'. By default this is LOW
(ie pulled down).
If you set this value to HIGH
, you must also pass INPUT_PULLDOWN
to the GpioPinAdapter constructor.
If your board does not support INPUT_PULLDOWN
, pass INPUT
and use an external resistor.
state | Either LOW (default) or HIGH |
|
inlineprotected |
Returns true if state has changed and previous state is onState.
|
inlineprotected |
Returns true if state has changed and previous state is not onState.
|
overridevirtual |
Unset a previously set callback function or method.
Must be called before the set function or method is destoyed.
Reimplemented from EventInputBase.
void EventSwitch::update | ( | ) |
Update the state from the switch pin input.
Must be called from within loop()