MD_UISwitch Library 2.2
Library for different types of User Interface switches
|
#include <MD_UISwitch.h>
Public Types | |
Enumerated values and Typedefs. | |
enum | keyResult_t { KEY_NULL , KEY_DOWN , KEY_UP , KEY_PRESS , KEY_DPRESS , KEY_LONGPRESS , KEY_RPTPRESS } |
Public Member Functions | |
Class constructor and destructor. | |
MD_UISwitch (void) | |
~MD_UISwitch () | |
Methods for core object control. | |
virtual void | begin (void)=0 |
virtual keyResult_t | read (void)=0 |
virtual uint8_t | getKey (void) |
Methods for object parameters and options. | |
void | setPressTime (uint16_t t) |
void | setDoublePressTime (uint16_t t) |
void | setLongPressTime (uint16_t t) |
void | setRepeatTime (uint16_t t) |
void | enableDoublePress (boolean f) |
void | enableLongPress (boolean f) |
void | enableRepeat (boolean f) |
void | enableRepeatResult (boolean f) |
Protected Types | |
enum | state_fsm { S_IDLE , S_PRESS , S_PRESS2A , S_PRESS2B , S_PRESSL , S_REPEAT , S_WAIT } |
enum | state_db { S_WAIT_START , S_DEBOUNCE , S_WAIT_RELEASE } |
Protected Member Functions | |
keyResult_t | processFSM (bool swState, bool reset=false) |
bool | debounce (bool curStatus, bool reset=false) |
Protected Attributes | |
state_fsm | _state |
the FSM current state | |
keyResult_t | _kPush |
storage for pushed key in FSM | |
uint32_t | _timeActive |
the millis() time switch was last activated | |
uint8_t | _enableFlags |
functions enabled/disabled | |
uint8_t | _RC = 0 |
RC integrator value. | |
bool | _prevStatus |
previous 'active' status for edge detection | |
state_db | _RCstate |
current RC debouning state | |
uint16_t | _timePress |
press time in milliseconds | |
uint16_t | _timeDoublePress |
double press detection time in milliseconds | |
uint16_t | _timeLongPress |
long press time in milliseconds | |
uint16_t | _timeRepeat |
repeat time delay in milliseconds | |
uint8_t | _lastKey |
persists the last key value until a new one is detected | |
int16_t | _lastKeyIdx |
internal index of the last key read | |
Core object for the MD_UISwitch library
Return values for switch status
The read() method returns one of these enumerated values as the result of the switch transition detection.
|
protected |
|
protected |
FSM state values
States for the internal Finite State Machine to recognized the key press
MD_UISwitch::MD_UISwitch | ( | void | ) |
Class Constructor.
Instantiate a new instance of the class. The main function for the core object is to initialize the internal shared variables and timers to default values.
MD_UISwitch::~MD_UISwitch | ( | ) |
Class Destructor.
Release any allocated memory and clean up anything else.
|
pure virtual |
Initialize the object.
Initialize the object data. This needs to be called during setup() to initialize new data for the class that cannot be done during the object creation. This method must be replaced in the derived class.
Implemented in MD_UISwitch_Digital, MD_UISwitch_User, MD_UISwitch_Analog, MD_UISwitch_Matrix, and MD_UISwitch_4017KM.
|
protected |
Switch debounce using Edge Detection & Resistor-Capacitor Digital Filter.
A digital filter that mimics an analogue RC filter with first-order recursive low pass filter. It has good EMI filtering and quick response, with a nearly continuous output like an analogue circuit.
curStatus | current active status for the switch. |
reset | an optional identifier to reset the debounce detection. |
void MD_UISwitch::enableDoublePress | ( | boolean | f | ) |
Enable double press detection
Enable or disable double press detection. If disabled, two single press are detected instead of a double press. Default is to detect double press events.
f | true to enable, false to disable. |
void MD_UISwitch::enableLongPress | ( | boolean | f | ) |
Enable long press detection
Enable or disable long press detection. If disabled, the long press notification is skipped when the event is detected and either a simple press or repeats are returned, depending on the setting of the other options. Default is to detect long press events.
f | true to enable, false to disable. |
void MD_UISwitch::enableRepeat | ( | boolean | f | ) |
Enable repeat detection
Enable or disable repeat detection. If disabled, the long press notification is returned as soon as the long press time has expired. Default is to detect repeat events.
f | true to enable, false to disable. |
void MD_UISwitch::enableRepeatResult | ( | boolean | f | ) |
Modify repeat notification
Modify the result returned from a repeat detection. If enabled, the first repeat will return a KS_PRESS and subsequent repeats will return KS_RPTPRESS. If disabled (default) the repeats will be stream of KS_PRESS values.
f | true to enable, false to disable (default). |
|
virtual |
Read the key identifier for the last switch
Return the id for the last active switch. This is useful to know which key was actually pressed when there could be more than one key (ie, a key matrix).
|
protected |
Process the key using FSM
Process the key read using a finite state machine to detect the current type of keypress and return one of the keypress types.
The timing for each keypress starts when the first transition of the switch from inactive to active state and is recognized by a finite state machine invoked in process() whose operation is directed by the timer and option values specified.
If the reset parameter is specified the FSM is reset to the idle state and no other processing is performed (ie, the bState parameter is ignored).
swState | true if the switch is active, false otherwise. |
reset | an optional identifier to reset the FSM. |
|
pure virtual |
Read input and return the state of the switch
Read the input key switch and invoke the process method to determine what the keystroke means. This method must be replaced in the derived class with a hardware specific method to read the key switch.
Implemented in MD_UISwitch_Digital, MD_UISwitch_User, MD_UISwitch_Analog, MD_UISwitch_Matrix, and MD_UISwitch_4017KM.
void MD_UISwitch::setDoublePressTime | ( | uint16_t | t | ) |
Set the double press detection time
Set the time between each press time in milliseconds. A double press is detected if the switch is released and depressed within this time, measured from when the first press is detected. The default value is set by the KEY_DPRESS_TIME constant.
t | the specified time in milliseconds. |
void MD_UISwitch::setLongPressTime | ( | uint16_t | t | ) |
Set the long press detection time
Set the time in milliseconds after which a continuous press and release is deemed a long press, measured from when the first press is detected. The default value is set by the KEY_LONGPRESS_TIME constant.
Note that the relationship between timer values should be Press Time < Long Press Time < Repeat time. No checking is done in the to enforce this relationship.
t | the specified time in milliseconds. |
void MD_UISwitch::setPressTime | ( | uint16_t | t | ) |
Set the press time
Set the switch press time in milliseconds. The default value is set by the KEY_PRESS_TIME constant.
Note that the relationship between timer values should be Press Time < Long Press Time < Repeat time. No checking is done in the to enforce this relationship.
t | the specified time in milliseconds. |
void MD_UISwitch::setRepeatTime | ( | uint16_t | t | ) |
Set the repeat time
Set the time in milliseconds after which a continuous press and hold is treated as a stream of repeated presses, measured from when the first press is detected.
Note that the relationship between timer values should be Press Time < Long Press Time < Repeat time. No checking is done in the to enforce this relationship.
t | the specified time in milliseconds. |