InputEvents v1.4.0
An easy to use but comprehensive Event Library for Buttons, Encoders, Encoder Buttons, Analog Inputs, Joysticks and Switches.
Protected Types | Protected Member Functions | Protected Attributes | List of all members
EventSwitch Class Reference

Description

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:

#include <EventSwitch.h>

Inheritance diagram for EventSwitch:
Inheritance graph
[legend]

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...
 
- Public Member Functions inherited from EventInputBase
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...
 
- Protected Member Functions inherited from EventInputBase
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.
 
- Protected Attributes inherited from EventInputBase
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... :-/.
 

Constructor & Destructor Documentation

◆ EventSwitch() [1/3]

EventSwitch::EventSwitch ( byte  pin,
bool  useDefaultDebouncer = true 
)

Construct an EventSwitch input.

Parameters
switchPinA 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() [2/3]

EventSwitch::EventSwitch ( PinAdapter _pinAdapter,
bool  useDefaultDebouncer = true 
)

Construct a new EventSwitch with a PinAdapter and optionally use the default debouncer.

Parameters
pinAdapter

◆ EventSwitch() [3/3]

EventSwitch::EventSwitch ( PinAdapter _pinAdapter,
DebounceAdapter debounceAdapter 
)

Construct a new EventSwitch with a PinAdapter and a DebounceAdapter.

Parameters
pinAdapter
debounceAdapter

Member Function Documentation

◆ begin()

void EventSwitch::begin ( )
virtual

Initialise the EventButton.

Must be called from within setup()

Implements EventInputBase.

◆ changedPinState()

bool EventSwitch::changedPinState ( )
protected

Returns true if pinAdapter read() has changed since last call.

Returns
true
false

◆ changedState()

bool EventSwitch::changedState ( )
protected

Returns true if pinAdapter changed the switch state.

Returns
true
false

◆ changeState()

void EventSwitch::changeState ( bool  newState)
protected

Change the switch state and flag as changed.

Parameters
newState

◆ invoke()

void EventSwitch::invoke ( InputEventType  et)
overrideprotectedvirtual

Override of the EventInputBase::invoke() virtual method.

Parameters
etEnum of type InputEventType

Implements EventInputBase.

◆ isOff()

bool EventSwitch::isOff ( )
inline

Return true if switch is off.

Returns
true Switch is deemed OFF.
false Switch is deemed ON.

◆ isOn()

bool EventSwitch::isOn ( )
inline

Return true if switch is on.

Returns
true Switch is deemed ON.
false Switch is deemed OFF.

◆ setCallback() [1/2]

void EventSwitch::setCallback ( CallbackFunction  f)
inline

Set the Callback function.

Parameters
fA function of type EventSwitch::CallbackFunction type.

◆ setCallback() [2/2]

template<typename T >
void EventSwitch::setCallback ( T *  instance,
void(T::*)(InputEventType, EventSwitch &)  method 
)
inline

Set the Callback function to a class method.

Note: This method is only available if std:function is supported.

Parameters
instanceThe instance of a class implementing a CallbackFunction method.
methodThe class method of type EventSwitch::CallbackFunction type.

◆ setDebounceInterval()

bool EventSwitch::setDebounceInterval ( unsigned int  intervalMs = 10)

Set the DebounceAdapter debounce interval. Default is 10ms.

Returns
true If the debounce interval has been updated
false If the debouncer interval has not been updated (ie no debouncer set)

◆ setDebouncer()

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.

Parameters
debounceAdapter

◆ setOnState()

void EventSwitch::setOnState ( bool  state = LOW)
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.

Parameters
stateEither LOW (default) or HIGH

◆ turningOff()

bool EventSwitch::turningOff ( )
inlineprotected

Returns true if state has changed and previous state is onState.

Returns
true
false

◆ turningOn()

bool EventSwitch::turningOn ( )
inlineprotected

Returns true if state has changed and previous state is not onState.

Returns
true
false

◆ unsetCallback()

void EventSwitch::unsetCallback ( )
overridevirtual

Unset a previously set callback function or method.

Must be called before the set function or method is destoyed.

Reimplemented from EventInputBase.

◆ update()

void EventSwitch::update ( )

Update the state from the switch pin input.

Must be called from within loop()


The documentation for this class was generated from the following files: