DebounceSwitchRK
|
Configuration and state for a single switch. More...
#include <DebounceSwitchRK.h>
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... | |
DebounceSwitchState & | withConfig (const DebounceConfiguration &config) |
Sets the configuration for this switch. More... | |
Public Member Functions inherited from DebounceConfiguration | |
DebounceConfiguration & | withDebounceMs (unsigned long ms) |
Set the debounce press and release time in milliseconds (default: 20) More... | |
DebounceConfiguration & | withDebouncePressMs (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) | |
DebounceConfiguration & | withDebounceReleaseMs (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) | |
DebounceConfiguration & | withInterTapMs (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) | |
DebounceConfiguration & | withLongPressMs (unsigned long ms) |
Set the long press duration in milliseconds (default: 3000, 3 seconds) More... | |
DebounceConfiguration & | withNoLongPress () |
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) | |
DebounceConfiguration & | withVeryLongPressMs (unsigned long ms) |
Set the very long press duration in milliseconds (default: 10000, 10 seconds) More... | |
DebounceConfiguration & | withNoVeryLongPress () |
Disables support for very long press. More... | |
unsigned long | getVeryLongPressMs () const |
Gets the very long press duration in milliseconds (default: 10000, 10 seconds) | |
DebounceConfiguration & | operator= (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. | |
DebounceSwitchState & | operator= (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 |
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.
|
protected |
Constructor (used internally)
pin | The pin to add a switch to (D2, D3, ...) or a special constant: DebounceSwitch::VIRTUAL_PIN or DebounceSwitch::NOTIFY_PIN. |
style | The style of button (push button or toggle), with or without pull-up or pull-down. |
config | The configuration to use |
callback | The function to call |
context | Optional pointer to pass to the callback. |
pollCallback | The callback to poll for the value. Not used for notify callbacks. This is called every checkMs (5 milliseconds). |
pollContext | Optional 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.
|
protectedvirtual |
Destructor - used internally.
This object is only deleted by the DebounceSwitch class.
|
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.
|
inline |
Gets the debounced version of lastSignal, essentially the current debounced state of this pin.
See also isPressed()
|
inline |
Get the pin this object is configured for.
In addition to real pins (D2, D3, A4, ...) it can also be a constant:
|
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.
|
inline |
Returns a readable name for the current pressed state.
Returned value is a short English string that matches the constant name.
|
static |
Gets a readable name for a pressState value.
pressState | the value to convert |
|
inline |
When a TAP state is sent to the callback, this determines how many.
You can count as many taps are you want, but it's a little unwieldy for more than 3.
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.
void DebounceSwitchState::notify | ( | bool | signal | ) |
In notify mode, addNotifySwitch() used, tells the library when the signal changes state.
signal | true = HIGH and false = LOW. Whether this is pressed or not depends on the DebounceSwitchStyle for this input. |
|
protected |
Used internally to poll the value of this pin.
|
protected |
Used internally to set pressState and optionally call the callback.
pressState | The new state to set |
callCallback | True 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.
|
static |
Converts a signal value (false = LOW, true = HIGH) to a DebouncePressState.
signal | the value to convert |
|
inline |
Sets the configuration for this switch.
config | a 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.
|
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.
|
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.
|
protected |
pin The pin being monitored (D2, D3, ...) or a special constant
|
protected |
millis() value when the button was pressed
Even though millis() rolls over to 0 every 49 days, this variable works correctly on rollover.
|
protected |
The current state of the button state machine.
Use setSwitchState() to change the state.
|
protected |
millis() value when the button was released
Even though millis() rolls over to 0 every 49 days, this variable works correctly on rollover.