DebounceSwitchRK
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Friends
DebounceSwitchState Class Reference

Configuration and state for a single switch. More...

#include <DebounceSwitchRK.h>

Inheritance diagram for DebounceSwitchState:
DebounceConfiguration

Public Member Functions

void notify (bool signal)
 In notify mode, addNotifySwitch() used, tells the library when the signal changes state. More...
 
bool isPressed () const
 Returns true if the switch is currently pressed. More...
 
bool getDebouncedLastSignal () const
 Gets the debounced version of lastSignal, essentially the current debounced state of this pin. More...
 
DebouncePressState getPressState () const
 Returns the current press state. More...
 
const char * getPressStateName () const
 Returns a readable name for the current pressed state. More...
 
int getTapCount () const
 When a TAP state is sent to the callback, this determines how many. More...
 
pin_t getPin () const
 Get the pin this object is configured for. More...
 
DebounceSwitchStatewithConfig (const DebounceConfiguration &config)
 Sets the configuration for this switch. More...
 
- Public Member Functions inherited from DebounceConfiguration
DebounceConfigurationwithDebounceMs (unsigned long ms)
 Set the debounce press and release time in milliseconds (default: 20) More...
 
DebounceConfigurationwithDebouncePressMs (unsigned long ms)
 Set the debounce press time in milliseconds (default: 20) More...
 
unsigned long getDebouncePressMs () const
 Gets the debounce time in milliseconds (default: 20)
 
DebounceConfigurationwithDebounceReleaseMs (unsigned long ms)
 Set the debounce release time in milliseconds (default: 20) More...
 
unsigned long getDebounceReleaseMs () const
 Gets the debounce time in milliseconds (default: 20)
 
DebounceConfigurationwithInterTapMs (unsigned long ms)
 Set the inter-tap time in milliseconds (default: 500) More...
 
unsigned long getInterTapMs () const
 Gets the inter-tap time in milliseconds (default: 500)
 
DebounceConfigurationwithLongPressMs (unsigned long ms)
 Set the long press duration in milliseconds (default: 3000, 3 seconds) More...
 
DebounceConfigurationwithNoLongPress ()
 Disables support for long and very long press. Only short press is returned.
 
unsigned long getLongPressMs () const
 Get the long press duration in milliseconds (default: 3000, 3 seconds)
 
DebounceConfigurationwithVeryLongPressMs (unsigned long ms)
 Set the very long press duration in milliseconds (default: 10000, 10 seconds) More...
 
DebounceConfigurationwithNoVeryLongPress ()
 Disables support for very long press. More...
 
unsigned long getVeryLongPressMs () const
 Gets the very long press duration in milliseconds (default: 10000, 10 seconds)
 
DebounceConfigurationoperator= (const DebounceConfiguration &src)
 Copy settings from another DebounceConfiguration. More...
 

Static Public Member Functions

static DebouncePressState signalToPressState (bool signal)
 Converts a signal value (false = LOW, true = HIGH) to a DebouncePressState. More...
 
static const char * getPressStateName (DebouncePressState pressState)
 Gets a readable name for a pressState value. More...
 

Protected Member Functions

 DebounceSwitchState (pin_t pin, DebounceSwitchStyle style, DebounceConfiguration *config, std::function< void(DebounceSwitchState *switchState, void *context)> callback, void *context, std::function< bool(DebounceSwitchState *switchState, void *pollContext)> pollCallback, void *pollContext)
 Constructor (used internally) More...
 
virtual ~DebounceSwitchState ()
 Destructor - used internally. More...
 
 DebounceSwitchState (const DebounceSwitchState &)=delete
 This class is not copyable.
 
DebounceSwitchStateoperator= (const DebounceSwitchState &)=delete
 This class is not copyable.
 
bool poll ()
 Used internally to poll the value of this pin. More...
 
void run ()
 Run the state machine for this pin.
 
void setPressState (DebouncePressState pressState, bool callCallback)
 Used internally to set pressState and optionally call the callback. More...
 
void checkDebounce ()
 Handle debouncing this pin. More...
 

Protected Attributes

pin_t pin
 pin The pin being monitored (D2, D3, ...) or a special constant More...
 
DebounceSwitchStyle style
 The style of switch (button or toggle) allow with whether it's active high or low and any pull.
 
std::function< void(DebounceSwitchState *switchState, void *context)> callback
 Function to call when the state of the button changes.
 
void * context
 Optional data to pass to callback. May be NULL. Not interpreted by this class.
 
std::function< bool(DebounceSwitchState *switchState, void *context)> pollCallback
 Function to call to determine the state of the switch using polling.
 
void * pollContext
 Optional data to pass to pollCallback. May be NULL. Not interpreted by this class.
 
DebouncePressState pressState = DebouncePressState::NOT_PRESSED
 The current state of the button state machine. More...
 
bool lastSignal = false
 Last state of the switch signal. More...
 
unsigned long pressMs = 0
 millis() value when the button was pressed More...
 
unsigned long releaseMs = 0
 millis() value when the button was released More...
 
int sequenceCount = 0
 Number of taps for multi-tap.
 
unsigned long debounceLastSameMs = 0
 The last millis() lastSignal and debouncedLastSignal were the same. Used in the debounce algorithm. More...
 
bool debouncedLastSignal = false
 Debounced version of lastSignal.
 
- Protected Attributes inherited from DebounceConfiguration
unsigned long debouncePressMs = 20
 Debounce period for press in milliseconds (default: 20) More...
 
unsigned long debounceReleaseMs = 20
 Debounce period for release in milliseconds (default: 20) More...
 
unsigned long interTapMs = 500
 How long to wait for double-tap, triple-tap, etc. in milliseconds (default: 500) More...
 
unsigned long longPressMs = 3000
 How long to wait for a long press in milliseconds (default: 3000, or 3 seconds) More...
 
unsigned long veryLongPressMs = 10000
 How long to wait for a very long press in milliseconds (default: 10000, or 10 seconds) More...
 

Friends

class DebounceSwitch
 

Detailed Description

Configuration and state for a single switch.

The DebounceSwitch class has one global singleton instance, but there's an instance of for each switch that stores information about that one switch. This allows efficient handling of multiple buttons while keeping their debouncing state separate.

You do not instantate these directly, the methods in DebounceSwitch such as addSwitch(), addVirtualSwitch(), and addNotifySwitch() will instantiate it for you.

Constructor & Destructor Documentation

◆ DebounceSwitchState()

DebounceSwitchState::DebounceSwitchState ( pin_t  pin,
DebounceSwitchStyle  style,
DebounceConfiguration config,
std::function< void(DebounceSwitchState *switchState, void *context)>  callback,
void *  context,
std::function< bool(DebounceSwitchState *switchState, void *pollContext)>  pollCallback,
void *  pollContext 
)
protected

Constructor (used internally)

Parameters
pinThe pin to add a switch to (D2, D3, ...) or a special constant: DebounceSwitch::VIRTUAL_PIN or DebounceSwitch::NOTIFY_PIN.
styleThe style of button (push button or toggle), with or without pull-up or pull-down.
configThe configuration to use
callbackThe function to call
contextOptional pointer to pass to the callback.
pollCallbackThe callback to poll for the value. Not used for notify callbacks. This is called every checkMs (5 milliseconds).
pollContextOptional pointer to pass to the pollCallback.

Do not instantate this directly, the methods in DebounceSwitch such as addSwitch(), addVirtualSwitch(), and addNotifySwitch() will instantiate it for you.

◆ ~DebounceSwitchState()

DebounceSwitchState::~DebounceSwitchState ( )
protectedvirtual

Destructor - used internally.

This object is only deleted by the DebounceSwitch class.

Member Function Documentation

◆ checkDebounce()

void DebounceSwitchState::checkDebounce ( )
protected

Handle debouncing this pin.

This is called every checkMs milliseconds (default: 5) to handle debouncing. Input is the lastSignal (which is updated either by polling or notification) and the output is debouncedLastSignal which is the debounced version.

◆ getDebouncedLastSignal()

bool DebounceSwitchState::getDebouncedLastSignal ( ) const
inline

Gets the debounced version of lastSignal, essentially the current debounced state of this pin.

See also isPressed()

◆ getPin()

pin_t DebounceSwitchState::getPin ( ) const
inline

Get the pin this object is configured for.

In addition to real pins (D2, D3, A4, ...) it can also be a constant:

◆ getPressState()

DebouncePressState DebounceSwitchState::getPressState ( ) const
inline

Returns the current press state.

You should avoid polling the press state because you can easily miss transitions. However, you will likely need to call this method from your callback so you can find out what happened.

◆ getPressStateName() [1/2]

const char* DebounceSwitchState::getPressStateName ( ) const
inline

Returns a readable name for the current pressed state.

Returned value is a short English string that matches the constant name.

◆ getPressStateName() [2/2]

const char * DebounceSwitchState::getPressStateName ( DebouncePressState  pressState)
static

Gets a readable name for a pressState value.

Parameters
pressStatethe value to convert
Returns
A constant c-string literal that corresponds to the C++ enum name, for example "PRESS_START".

◆ getTapCount()

int DebounceSwitchState::getTapCount ( ) const
inline

When a TAP state is sent to the callback, this determines how many.

Returns
1 for single tap, 2 for double tap, 3 for triple tap, ...

You can count as many taps are you want, but it's a little unwieldy for more than 3.

◆ isPressed()

bool DebounceSwitchState::isPressed ( ) const

Returns true if the switch is currently pressed.

This works like getDebouncedLastSignal() except it inverts the debouncedLastState if the button is a press = LOW type so pressed is always true.

This is the debounced signal and is fast as it just returns a variable. It can be called from an ISR.

◆ notify()

void DebounceSwitchState::notify ( bool  signal)

In notify mode, addNotifySwitch() used, tells the library when the signal changes state.

Parameters
signaltrue = HIGH and false = LOW. Whether this is pressed or not depends on the DebounceSwitchStyle for this input.

◆ poll()

bool DebounceSwitchState::poll ( )
protected

Used internally to poll the value of this pin.

Returns
The value of the pin (false = LOW, true = HIGH)

◆ setPressState()

void DebounceSwitchState::setPressState ( DebouncePressState  pressState,
bool  callCallback 
)
protected

Used internally to set pressState and optionally call the callback.

Parameters
pressStateThe new state to set
callCallbackTrue to call the callback

Normally you pass true for callCallback but for hidden, internal states, you pass false so the state will change but the callback is not called.

◆ signalToPressState()

DebouncePressState DebounceSwitchState::signalToPressState ( bool  signal)
static

Converts a signal value (false = LOW, true = HIGH) to a DebouncePressState.

Parameters
signalthe value to convert
Returns
Either TOGGLE_LOW (false or LOW), TOGGLE_HIGH (true or HIGH)

◆ withConfig()

DebounceSwitchState& DebounceSwitchState::withConfig ( const DebounceConfiguration config)
inline

Sets the configuration for this switch.

Parameters
configa A DebounceConfiguration object with the settings you want to use

This method is handy if you want to share the same configuration across several switches instead of calling methods like withDebouncePressMs() individually for each switch. The settings are copied from config.

Field Documentation

◆ debounceLastSameMs

unsigned long DebounceSwitchState::debounceLastSameMs = 0
protected

The last millis() lastSignal and debouncedLastSignal were the same. Used in the debounce algorithm.

Even though millis() rolls over to 0 every 49 days, this variable works correctly on rollover.

◆ lastSignal

bool DebounceSwitchState::lastSignal = false
protected

Last state of the switch signal.

This is updated by the poll() method when using polling periodically (default: 5 milliseconds based on checkMs).

If using notify, calling notify updates this variable.

◆ pin

pin_t DebounceSwitchState::pin
protected

pin The pin being monitored (D2, D3, ...) or a special constant

◆ pressMs

unsigned long DebounceSwitchState::pressMs = 0
protected

millis() value when the button was pressed

Even though millis() rolls over to 0 every 49 days, this variable works correctly on rollover.

◆ pressState

DebouncePressState DebounceSwitchState::pressState = DebouncePressState::NOT_PRESSED
protected

The current state of the button state machine.

Use setSwitchState() to change the state.

◆ releaseMs

unsigned long DebounceSwitchState::releaseMs = 0
protected

millis() value when the button was released

Even though millis() rolls over to 0 every 49 days, this variable works correctly on rollover.


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