Serial Wombat Arduino Library
Loading...
Searching...
No Matches
SerialWombatDebouncedInput Class Reference

A pin mode class that debounces inputs. More...

#include <SerialWombatDebouncedInput.h>

Inheritance diagram for SerialWombatDebouncedInput:
Collaboration diagram for SerialWombatDebouncedInput:

Public Member Functions

 SerialWombatDebouncedInput (SerialWombatChip &serialWombatChip)
 Constructor for the SerialWombatDebouncedInput class.
 
int16_t begin (uint8_t pin, uint16_t debounce_mS=30, bool invert=true, bool usePullUp=true)
 Initialize a debounced input.
 
bool digitalRead ()
 Returns the debounced state of the input.
 
uint16_t readDurationInTrueState_mS ()
 return the number of mS that the input has been in true state
 
uint16_t readDurationInFalseState_mS ()
 return the number of mS that the input has been in false state
 
bool readTransitionsState (bool resetTransitionCounts=true)
 Queries the number of transistions that have occured on the debounced input.
 
int16_t setDoubleClickPeriod (uint16_t doubleClickPeriod_mS)
 Configure the maximum period used to recognize a double click.
 
int16_t setOutputPin (uint8_t outputPin, uint16_t transitionOutputValue, uint16_t doubleClickOutputValue)
 Configure another Serial Wombat pin to receive values on button events.
 
uint16_t readDoubleClickCount (bool resetDoubleClickCount=true)
 Read the number of double clicks detected by the firmware.
 
int16_t setToggleSimulation (bool enabled, uint16_t transitionCount=0)
 Enable or disable toggle-switch simulation in the pin's public data.
 
- Public Member Functions inherited from SerialWombatAbstractButton
 SerialWombatAbstractButton ()
 
- Public Member Functions inherited from SerialWombatPin
 SerialWombatPin (SerialWombatChip &serialWombatChip)
 Instantiates a Serial Wombat Pin.
 
 SerialWombatPin (SerialWombatChip &serialWombatChip, uint8_t pin)
 Instantiates a Serial Wombat Pin.
 
uint16_t readPublicData ()
 Read the 16 Bit public data associated with this pin.
 
void pinMode (uint8_t mode, bool pullDown=false, bool openDrain=false)
 Set pin to INPUT or OUTPUT, with options for pull Ups and open Drain settings.
 
void digitalWrite (uint8_t val)
 Set output pin High or Low.
 
int digitalRead ()
 Reads the state of the Pin.
 
uint16_t writePublicData (uint16_t value)
 Write a 16 bit value to this pin.
 
uint8_t pin ()
 Returns the current SW pin number. Used primarily for virtual calls by derived classes.
 
uint8_t swPinModeNumber ()
 Returns the Mode number. Used primarily by derived classes to populate packet data.
 
int16_t initPacketNoResponse (uint8_t packetNumber, uint8_t param0=0x55, uint8_t param1=0x55, uint8_t param2=0x55, uint8_t param3=0x55, uint8_t param4=0x55)
 
int16_t initPacketNoResponse (uint8_t packetNumber, uint16_t param0=0x55, uint8_t param1=0x55, uint8_t param2=0x55, uint8_t param3=0x55)
 
int16_t initPacketNoResponse (uint8_t packetNumber, uint16_t param0, uint16_t param1, uint8_t param2=0x55)
 
int16_t disable ()
 Disables the pin mode (if applicable)
 
int16_t enablePullup (bool enabled)
 Enables the weak pull up on a pin. Implemented on SW18AB and SW8B only.
 
int16_t enablePulldown (bool enabled)
 Enables the weak pull down on a pin. Implemented on SW18AB and SW8B only.
 
int16_t enableOpenDrain (bool enabled)
 Enables open drain mode on a pin. Implemented on SW18AB and SW8B only.
 
int16_t forceDMA (bool enabled)
 Forces use of DMA instead of hardware timing resources on SW18AB.
 
void setPinNumberForTesting (uint8_t pin)
 A function designed for testing only. May have unpredictable results if used in real time. Not intended for general use.
 

Additional Inherited Members

- Public Attributes inherited from SerialWombatAbstractButton
uint16_t transitions = 0
 Number of transitions returned by last call to readTransitionsState()
 
- Protected Attributes inherited from SerialWombatPin
uint8_t _pin = 255
 
SerialWombatChip & _sw
 
uint8_t _pinMode = 0
 

Detailed Description

A pin mode class that debounces inputs.

The SerialWombatDebouncedInput class is used to debounce inputs such as switches.

See the example sw4b_ard_Debounce1 distributed with the Serial Wombat Arduino Library for an example.

Video Tutorial

https://youtu.be/R1KM0J2Ug-M

One SerialWombatDebouncedInput instance should be declared per debounced input.

The class sends commands and reads data from the Serial Wombat. The actual switch debouncing algorithm runs on the Serial Wombat.

The Serial Wombat's firmware algorithm polls the input every 1mS. After it has been different from the reported value for X consecutive mS, the reported value changes to the new polled value. The number of mS required for a change can be configured.

The Debounced Input mode keeps track of how long the debounced result has been in the current state (up to 65535 mS) and can report this to the host. This is useful for implementing user interfaces that react based on how long a button has been held.

The Debounced Input mode keeps track of how many transitions have occured. This can be used to poll the Debounced Input infrequently for status, but still process all button presses/releases that occured since the last poll.

On supported firmware the Debounced Input mode can also detect double clicks, provide a count of detected double clicks, write values to another pin when a debounced transition or double click occurs, and simulate a toggle switch in its public data output.

The pin mode has weak pull-up circuitry availble, and the ability to report inverted values. These are both enabled when the simpliest begin() call is used, allowing buttons that switch the Serial Wombat pin to ground to be used without additional hardware.

See also the SerialWombatButtonCounter class which can run on top of this one.

Definition at line 79 of file SerialWombatDebouncedInput.h.

Constructor & Destructor Documentation

◆ SerialWombatDebouncedInput()

SerialWombatDebouncedInput::SerialWombatDebouncedInput ( SerialWombatChip & serialWombatChip)
inline

Constructor for the SerialWombatDebouncedInput class.

Parameters
serialWombatChipa reference to the Serial Wombat Chip on which the Debounced Input will exist

Definition at line 87 of file SerialWombatDebouncedInput.h.

Member Function Documentation

◆ begin()

int16_t SerialWombatDebouncedInput::begin ( uint8_t pin,
uint16_t debounce_mS = 30,
bool invert = true,
bool usePullUp = true )
inline

Initialize a debounced input.

Parameters
pinThe Serial Wombat pin used for the debounced input
debounce_mSnumber of mS the pin must be stable to cause a transition
invertFALSE: pin reading is returned TRUE: inverted pin reading is returned
usePullUpWhether the pin's weak pull up is enabled

Definition at line 98 of file SerialWombatDebouncedInput.h.

◆ digitalRead()

bool SerialWombatDebouncedInput::digitalRead ( )
inlinevirtual

Returns the debounced state of the input.

This function reads from the public data of the pin which indicates the debounced and invert adjusted state of the input

Returns
TRUE or FALSE. Meaning depends on inversion setting

Implements SerialWombatAbstractButton.

Definition at line 113 of file SerialWombatDebouncedInput.h.

◆ readDoubleClickCount()

uint16_t SerialWombatDebouncedInput::readDoubleClickCount ( bool resetDoubleClickCount = true)
inline

Read the number of double clicks detected by the firmware.

Only supported on the Serial Wombat 18AB, and 8B TM1637 and Front Panel builds.

Parameters
resetDoubleClickCountIf true, reset the firmware double click count after reading it
Returns
Number of double clicks currently accumulated by the firmware

Definition at line 239 of file SerialWombatDebouncedInput.h.

◆ readDurationInFalseState_mS()

uint16_t SerialWombatDebouncedInput::readDurationInFalseState_mS ( )
inlinevirtual

return the number of mS that the input has been in false state

Note that this value starts incrementing after any debounce period, not after the physical pin transition.

Returns
returns a value in mS which saturates at 65535. Returns 0 if currently true.

Implements SerialWombatAbstractButton.

Definition at line 150 of file SerialWombatDebouncedInput.h.

◆ readDurationInTrueState_mS()

uint16_t SerialWombatDebouncedInput::readDurationInTrueState_mS ( )
inlinevirtual

return the number of mS that the input has been in true state

Note that this value starts incrementing after the debounce period, not after the physical pin transition.

Returns
returns a value in mS which saturates at 65535. Returns 0 if currently false.

Implements SerialWombatAbstractButton.

Definition at line 125 of file SerialWombatDebouncedInput.h.

◆ readTransitionsState()

bool SerialWombatDebouncedInput::readTransitionsState ( bool resetTransitionCounts = true)
inlinevirtual

Queries the number of transistions that have occured on the debounced input.

This function queries the debounced input for current state and transitions since last call. transition count is put in the global member transitions. The debounced input in the Serial Wombat resets its count to zero after this call.

Returns
TRUE or FALSE, current status of debounced input

Implements SerialWombatAbstractButton.

Definition at line 178 of file SerialWombatDebouncedInput.h.

◆ setDoubleClickPeriod()

int16_t SerialWombatDebouncedInput::setDoubleClickPeriod ( uint16_t doubleClickPeriod_mS)
inline

Configure the maximum period used to recognize a double click.

A double click is recognized when two releases occur within this period and no third press occurs during the following period. The firmware waits for the following period to expire before incrementing the double click count.

Setting the period to 0 disables double click detection. Calling this method also clears any double click sequence currently in progress.

Only supported on the Serial Wombat 18AB, and 8B TM1637 and Front Panel builds.

Parameters
doubleClickPeriod_mSdouble click timing period in mS
Returns
The result returned by SerialWombatChip::sendPacket()

Definition at line 202 of file SerialWombatDebouncedInput.h.

◆ setOutputPin()

int16_t SerialWombatDebouncedInput::setOutputPin ( uint8_t outputPin,
uint16_t transitionOutputValue,
uint16_t doubleClickOutputValue )
inline

Configure another Serial Wombat pin to receive values on button events.

The output pin's public data buffer is written with transitionOutputValue whenever a debounced transition occurs. If a double click is subsequently recognized, the output pin is written with doubleClickOutputValue.

Use an output pin value of 255 to disable this feature.

Only supported on the Serial Wombat 18AB, and 8B TM1637 and Front Panel builds.

Parameters
outputPinSerial Wombat pin whose public data buffer will be written
transitionOutputValueValue written when a debounced transition occurs
doubleClickOutputValueValue written when a double click is recognized
Returns
The result returned by SerialWombatChip::sendPacket()

Definition at line 224 of file SerialWombatDebouncedInput.h.

◆ setToggleSimulation()

int16_t SerialWombatDebouncedInput::setToggleSimulation ( bool enabled,
uint16_t transitionCount = 0 )
inline

Enable or disable toggle-switch simulation in the pin's public data.

When enabled, the public data output toggles between 0 and 65535 after each complete debounced press/release cycle. This command also sets the firmware transition counter. A transitionCount of 0 starts the toggle output low; a transitionCount of 2 starts it high.

Only supported on the Serial Wombat 18AB, and 8B TM1637 and Front Panel builds.

Parameters
enabledTRUE to enable toggle simulation, FALSE for normal debounced output
transitionCountValue loaded into the firmware transition counter
Returns
The result returned by SerialWombatChip::sendPacket()

Definition at line 262 of file SerialWombatDebouncedInput.h.