![]() |
ButtonToSwitch Library for Arduino v4.0.1
A library that provides elaborated switch mechanism behavior simulation for digital signals inputs
|
Base class, models a Debounced Momentary Push Button (D-MPB). More...
#include <ButtonToSwitch.h>
Public Member Functions | |
DbncdMPBttn () | |
Default class constructor. | |
DbncdMPBttn (const uint8_t &mpbttnPin, const bool &pulledUp=true, const bool &typeNO=true, const unsigned long int &dbncTimeOrigSett=0) | |
Class constructor. | |
virtual | ~DbncdMPBttn () |
Default virtual destructor. | |
virtual bool | begin (const unsigned long int &pollDelayMs=_StdPollDelay) |
Attaches the instantiated object to a timer that monitors the input pins and updates the object status. | |
void | clrStatus (bool clrIsOn=true) |
Clears and resets flags, timers and counters modified through the object's signals processing. | |
void | disable () |
Disables the input signal processing, ignoring the changes of its values. | |
void | enable () |
Enables the input signal processing. | |
bool | end () |
Detaches the object from the timer that monitors the input pins, compute and updates the object's status. | |
const unsigned long int | getCurDbncTime () const |
Returns the current debounce period time set for the object. | |
fncPtrType | getFnWhnTrnOff () |
Returns the function that is set to execute every time the object enters the Off State. | |
fncPtrType | getFnWhnTrnOn () |
Returns the function that is set to execute every time the object enters the On State. | |
const bool | getIsEnabled () const |
Returns the value of the isEnabled attribute flag, indicating the Enabled or Disabled status of the object. | |
const bool | getIsOn () const |
Returns the value of the isOn attribute flag. | |
const bool | getIsOnDisabled () const |
Returns the value of the isOnDisabled attribute. | |
const unsigned long int | getLstPollTime () |
Returns the time stamp of the last state update for the object. | |
const uint32_t | getOtptsSttsPkgd () |
Returns the relevant attribute flags values for the object state encoded as a 32 bits value. | |
const bool | getOutputsChange () const |
Returns the value of the outputsChange attribute flag. | |
const unsigned long int | getPollPeriodMs () |
Returns the poll period time setting attribute's value. | |
unsigned long int | getStrtDelay () |
Returns the current value of strtDelay attribute. | |
bool | getUpdTmrAttchd () |
Returns the value of the Attached to the update timer attribute. | |
bool | init (const uint8_t &mpbttnPin, const bool &pulledUp=true, const bool &typeNO=true, const unsigned long int &dbncTimeOrigSett=0) |
Initializes an object instantiated by the default constructor. | |
bool | pause () |
Pauses the software timer updating the computation of the object's internal flags value (object's state). | |
void | resetDbncTime () |
Resets the debounce process time of the object to the value used at instantiation. | |
void | resetFda () |
Resets the MPB behavior automaton to it's Initial or Start State | |
bool | resume () |
Restarts the software timer updating the calculation of the object internal flags. | |
bool | setDbncTime (const unsigned long int &newDbncTime) |
Sets the debounce process time. | |
void | setFnWhnTrnOffPtr (void(*newFnWhnTrnOff)()) |
Sets the function that will be called to execute every time the object enters the Off State. | |
void | setFnWhnTrnOnPtr (void(*newFnWhnTrnOn)()) |
Sets the function that will be called to execute every time the object enters the On State. | |
void | setIsOnDisabled (const bool &newIsOnDisabled) |
Sets the value of the isOnDisabled attribute. | |
void | setOutputsChange (bool newOutputsChange) |
Sets the value of the attribute flag indicating if a change took place in any of the output attribute flags (IsOn included). | |
Base class, models a Debounced Momentary Push Button (D-MPB).
Being the facilities provided by the TimerOne library limited to the execution of only ONE timer interrupt, the setup selected for this development is the following:
With every pause() invocation, taking care of the special case if the "list of MPBs to keep updated" is emptied (**_updTimerPeriod** = 0 and Timer1 paused)
The callback function duties:
This class provides the resources needed to process a momentary digital input signal -as the one provided by a MPB (Momentary Push Button)- returning a clean signal to be used as a switch, implementing the needed services to replace a wide range of physical related switch characteristics: Debouncing, deglitching, disabling.
DbncdMPBttn::DbncdMPBttn | ( | const uint8_t & | mpbttnPin, |
const bool & | pulledUp = true, | ||
const bool & | typeNO = true, | ||
const unsigned long int & | dbncTimeOrigSett = 0 ) |
Class constructor.
mpbttnPin | Pin id number of the input signal pin |
pulledUp | (Optional) boolean, indicates if the input pin must be configured as INPUT_PULLUP (true, default value), or INPUT_PULLDOWN (false), to calculate correctly the expected voltage level in the input pin. The pin is configured by the constructor so no previous programming is needed. The pin must be free to be used as a digital input, and must be a pin with an internal pull-up circuit, as not every GPIO pin has the option. |
typeNO | (Optional) boolean, indicates if the MPB is a Normally Open (NO) switch (true, default value), or Normally Closed (NC) (false), to calculate correctly the expected level of voltage indicating the MPB is pressed. |
dbncTimeOrigSett | (Optional) unsigned long integer (uLong), indicates the time (in milliseconds) to wait for a stable input signal before considering the MPB to be pressed (or not pressed). If no value is passed the constructor will assign the minimum value provided in the class, that is 20 milliseconds as it is an empirical value obtained in various published tests. |
|
virtual |
Attaches the instantiated object to a timer that monitors the input pins and updates the object status.
The frequency of the periodic monitoring is passed as a parameter in milliseconds, and is a value that must be small (frequent) enough to keep the object updated, but not so frequent that wastes resources useful for other tasks. A default value is provided based on empirical results obtained in various published tests.
pollDelayMs | (Optional) unsigned long integer (ulong), the time between polls in milliseconds. |
true | the object could be attached to the timer -or it was already attached to a timer when the method was invoked-. |
false | the object could not be attached to the timer, because the parameter passed for the timer was invalid: 0 (zero). |
Reimplemented in DblActnLtchMPBttn, HntdTmLtchMPBttn, LtchMPBttn, SnglSrvcVdblMPBttn, TmVdblMPBttn, and XtrnUnltchMPBttn.
void DbncdMPBttn::clrStatus | ( | bool | clrIsOn = true | ) |
Clears and resets flags, timers and counters modified through the object's signals processing.
Resets object's attributes to its initialization values to safely resume operations, either after pausing the timer, enabling the object after disabling it or any disruptive activity that might generate unexpected distorsions. This avoids risky behavior of the object due to dangling flags or partially consumed time counters.
clrIsOn | Optional boolean value, indicates if the _isOn flag must be included to be cleared: |
void DbncdMPBttn::disable | ( | ) |
Disables the input signal processing, ignoring the changes of its values.
Invoking the disable() method sends a message to the object requesting it to enter the Disabled state. Due to security and stability of the object's behavior the message will not be processed at every stage of the input signals detection and output computations, but in every safe and stable stage. When the message is processed the object will:
void DbncdMPBttn::enable | ( | ) |
Enables the input signal processing.
Invoking the enable() method on a object in Disabled state sends it a message requesting to resume it's normal operation. The execution of the enabling of the object implies:
bool DbncdMPBttn::end | ( | ) |
Detaches the object from the timer that monitors the input pins, compute and updates the object's status.
true | the object detachment procedure and timer entry removal was successful. |
false | the object detachment and/or entry removal was rejected. |
const unsigned long int DbncdMPBttn::getCurDbncTime | ( | ) | const |
Returns the current debounce period time set for the object.
The original value for the debouncing process used at instantiation time might be changed with the setDbncTime(const unsigned long int) or with the resetDbncTime() methods. This method gets the current value of the attribute in use.
fncPtrType DbncdMPBttn::getFnWhnTrnOff | ( | ) |
Returns the function that is set to execute every time the object enters the Off State.
The function to be executed is an attribute that might be modified by the setFnWhnTrnOffPtr() method.
nullptr | if there is no function set to execute when the object enters the Off State. |
fncPtrType DbncdMPBttn::getFnWhnTrnOn | ( | ) |
Returns the function that is set to execute every time the object enters the On State.
The function to be executed is an attribute that might be modified by the setFnWhnTrnOnPtr() method.
nullptr | if there is no function set to execute when the object enters the On State. |
const bool DbncdMPBttn::getIsEnabled | ( | ) | const |
const bool DbncdMPBttn::getIsOn | ( | ) | const |
Returns the value of the isOn attribute flag.
The isOn attribute flag is the fundamental attribute of the object, it might be considered the "Raison d'etre" of all this classes design: the isOn signal is not just the detection of an expected voltage value of a mcu pin, but the combination of that voltage level, filtered and verified, for a determined period of time and until a new event modifies that situation. While other mechanism are provided to execute code when the status of the object changes, all but the isOn flag value update might be optionally executed.
true | The object is in On state. |
false | The object is in Off state. |
const bool DbncdMPBttn::getIsOnDisabled | ( | ) | const |
Returns the value of the isOnDisabled attribute.
When instantiated the class, the object is created in Enabled state. That might be changed when needed. In the Disabled state the input signals for the MPB are not processed, and the output will be set to the On state or the Off state depending on this attribute's value. The reasons to disable the ability to change the output, and keep it on either state until re-enabled are design and use dependent. The flag value might be changed by the use of the setIsOnDisabled() method.
true | the object is configured to be set to the On state while it is in Disabled state. |
false | the object is configured to be set to the Off state while it is in Disabled state. |
const unsigned long int DbncdMPBttn::getLstPollTime | ( | ) |
Returns the time stamp of the last state update for the object.
The use of a unique timer to keep updated the state of all MPB objects forces each object to keep track of the time passed since it's last update. This is due to the fact that each MPB can be configured to have it's status updated at it's own rate. Keeping a register of the last update gives each MPB the possibility to compute at each Timer interruption signaling if it's the time to get the state update done.
const uint32_t DbncdMPBttn::getOtptsSttsPkgd | ( | ) |
Returns the relevant attribute flags values for the object state encoded as a 32 bits value.
Having a standard single data unit holding the all the relevant information describing the object's status is useful to pass current state to a function and for transmission purposes. The function otptsSttsUnpkg(uint32_t) is provided to unpackage the data into a MpbOtpts_t structure holding the information decoded.
const bool DbncdMPBttn::getOutputsChange | ( | ) | const |
Returns the value of the outputsChange attribute flag.
The instantiated objects include attributes linked to their computed state, which represent the behavior expected from their respective electromechanical simulated counterparts. When any of those attributes values change, the outputsChange flag is set. The flag only signals changes have happened -not which flags, nor how many times changes have taken place- since the last outputsChange flag reset. The outputsChange flag must be reset through the setOutputsChange(false) method after the outputs processing code is executed.
true | any of the object's behavior flags have changed value since last time outputsChange flag was reseted. |
false | no object's behavior flags have changed value since last time outputsChange flag was reseted. |
const unsigned long int DbncdMPBttn::getPollPeriodMs | ( | ) |
Returns the poll period time setting attribute's value.
The poll period time in milliseconds (pollPeriodMs) attribute sets the time period between state updates for the current object. The time period, in milliseconds, is set when the object begin(unsigned long int) method is called, and will be held through the use of the pause() and resume() methods. The only way to change it is by calling the end() method and then restarting the timer polling with a new begin() with a different value as parameter.
unsigned long int DbncdMPBttn::getStrtDelay | ( | ) |
Returns the current value of strtDelay attribute.
Returns the current value of time used by the object to rise the isOn attribute flag, after the debouncing process ends, in milliseconds. If the MPB is released before completing the debounce and the strtDelay time, no press will be detected by the object, and the isOn flag will not be affected. The original value for the delay process used at instantiation time might be changed with the setStrtDelay() method, so this method is provided to get the current value in use.
bool DbncdMPBttn::getUpdTmrAttchd | ( | ) |
Returns the value of the Attached to the update timer attribute.
Even when the instantiated object might be included in the "list of MPBs to keep updated", the final element that defines if an object is intended to have it's status updated is the attribute "Attached to the updating timer" updTmrAttchd. This value is used to include or exclude the object from the updating process without having to eliminate it or include it in the mentioned array list -both activities are time and resources consuming-, specially useful for situations like the pause() and resume() methods.
true | The object is set up to be updated by the timer events |
false | The object is set up NOT to be updated by the timer events |
bool DbncdMPBttn::init | ( | const uint8_t & | mpbttnPin, |
const bool & | pulledUp = true, | ||
const bool & | typeNO = true, | ||
const unsigned long int & | dbncTimeOrigSett = 0 ) |
Initializes an object instantiated by the default constructor.
All the parameters correspond to the non-default constructor of the class, DbncdMPBttn(const uint8_t, const bool, const bool, const unsigned long int)
bool DbncdMPBttn::pause | ( | ) |
Pauses the software timer updating the computation of the object's internal flags value (object's state).
The immediate exclusion from the timer that keeps the object's state updated implies that the object's state will be kept, whatever that state is it. The same consideration as the end() method applies referring to options to modify the state in which the object will be while in the Pause state.
true | the object was set not to be updated by the timer. |
false | the object was not included in the "List of MPBttns to keep updated", so it couldn't be paused. |
void DbncdMPBttn::resetDbncTime | ( | ) |
Resets the debounce process time of the object to the value used at instantiation.
The debounce process time used at instantiation might be changed with the setDbncTime() as needed, as many times as needed. This method reverts the value to the instantiation time value. In case the value was not specified at instantiation time the default debounce time value will be used.
void DbncdMPBttn::resetFda | ( | ) |
Resets the MPB behavior automaton to it's Initial or Start State
This method is provided for security and for error handling purposes, so that in case of unexpected situations detected, the driving Deterministic Finite Automaton used to compute the MPB objects states might be reset to it's initial state to safely restart it, maybe as part of an Error Handling procedure.
bool DbncdMPBttn::resume | ( | ) |
Restarts the software timer updating the calculation of the object internal flags.
The timer will stop calling the functions for computing the flags values after calling the pause() method and will not be updated until the object is reatached to the timer with this method.
true | the object's timer updating could be resumed. |
false | the object was not included in the "List of MPBttns to keep updated", so it's updating couldn't be resumed. |
bool DbncdMPBttn::setDbncTime | ( | const unsigned long int & | newDbncTime | ) |
Sets the debounce process time.
Sets a new time for the debouncing period. The value must be equal or greater than the minimum empirical value set as a property for all the classes, that value is defined in the _HwMinDbncTime constant (20 milliseconds). A long debounce time will produce a delay in the press event generation, making it less "responsive".
newDbncTime | unsigned long integer, the new debounce value for the object. |
true | the new value is in the accepted range, the attribute value is updated. |
false | the value was out of the accepted range, no change was made. |
void DbncdMPBttn::setFnWhnTrnOffPtr | ( | void(* | newFnWhnTrnOff )() | ) |
Sets the function that will be called to execute every time the object enters the Off State.
The function to be executed must be of the form void (*newFnWhnTrnOff)(), meaning it must take no arguments and must return no value, it will be executed only once by the object (recursion must be handled with the usual precautions). When the object is instantiated the attribute value is set to nullptr.
newFnWhnTrnOff | Function pointer to the function intended to be called when the object enters the Off State. Passing nullptr as parameter deactivates the function execution mechanism. |
void DbncdMPBttn::setFnWhnTrnOnPtr | ( | void(* | newFnWhnTrnOn )() | ) |
Sets the function that will be called to execute every time the object enters the On State.
The function to be executed must be of the form void (*newFnWhnTrnOff)(), meaning it must take no arguments and must return no value, it will be executed only once by the object (recursion must be handled with the usual precautions). When instantiated the attribute value is set to nullptr.
newFnWhnTrnOn | function pointer to the function intended to be called when the object enters the On State. Passing nullptr as parameter deactivates the function execution mechanism. |
void DbncdMPBttn::setIsOnDisabled | ( | const bool & | newIsOnDisabled | ) |
Sets the value of the isOnDisabled attribute.
When instantiated the class, the object is created in Enabled state. That might be changed as needed. While in the Disabled state the input signals for the MPB are not processed, and the output will be set to the On state or the Off state depending on this attribute value.
void DbncdMPBttn::setOutputsChange | ( | bool | newOutputsChange | ) |
Sets the value of the attribute flag indicating if a change took place in any of the output attribute flags (IsOn included).
The usual path for the outputsChange flag is to be set by any method changing an output attribute flag, the callback function signaled to take care of the hardware actions because of this changes clears back outputsChange after taking care of them. In the unusual case the developer wants to "intercept" this sequence, this method is provided to set (true) or clear (false) outputsChange value.
newOutputChange | The new value to set the outputsChange flag to. |