MD_UISwitch Library  1.2
Library for different types of User Interface switches
MD_UISwitch Class Reference

#include <MD_UISwitch.h>

Inheritance diagram for MD_UISwitch:

Public Types

Enumerated values and Typedefs.
enum  keyResult_t {
  KEY_NULL, 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)
 
virtual keyResult_t read (void)
 
virtual uint8_t getKey (void)
 
Methods for object parameters and options.
void setDebounceTime (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_t {
  S_IDLE, S_DEBOUNCE1, S_DEBOUNCE2, S_PRESS,
  S_DPRESS, S_LPRESS, S_REPEAT, S_WAIT
}
 

Protected Member Functions

keyResult_t processFSM (bool bState, bool reset=0)
 

Protected Attributes

state_t _state
 the FSM current state
 
uint32_t _timeActive
 the millis() time it was last activated
 
uint8_t _enableFlags
 functions enabled/disabled
 
uint16_t _timeDebounce
 debounce time in milliseconds
 
uint16_t _timeDoublePress
 double press detection time in milliseconds
 
uint16_t _timeLongPress
 long press time in milliseconds
 
int16_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
 

Detailed Description

Core object for the MD_KeySwitch library

Member Enumeration Documentation

◆ keyResult_t

Return values for switch status

The read() method returns one of these enumerated values as the result of the switch transition detection.

Enumerator
KEY_NULL 

No key press.

KEY_PRESS 

Simple press, or a repeated press sequence if enableRepeatResult(false) (default)

KEY_DPRESS 

Double press.

KEY_LONGPRESS 

Long press.

KEY_RPTPRESS 

Repeated key press (only if enableRepeatResult(true))

◆ state_t

enum MD_UISwitch::state_t
protected

FSM state values

States for the internal Finite State Machine to recognised the key press

Enumerator
S_IDLE 

Idle state - waiting for key transition.

S_DEBOUNCE1 

First press debounce.

S_DEBOUNCE2 

Second (double) press debounce.

S_PRESS 

Detecting possible simple press.

S_DPRESS 

Detecting possible double press.

S_LPRESS 

Detecting possible long press.

S_REPEAT 

Outputting repeat keys when timer expires.

S_WAIT 

Waiting for key to be released after long press is detected.

Constructor & Destructor Documentation

◆ MD_UISwitch()

MD_UISwitch::MD_UISwitch ( void  )

Class Constructor.

Instantiate a new instance of the class. The main function for the core object is to initialise the internal shared variables and timers to default values.

◆ ~MD_UISwitch()

MD_UISwitch::~MD_UISwitch ( )

Class Destructor.

Release any allocated memory and clean up anything else.

Member Function Documentation

◆ begin()

virtual void MD_UISwitch::begin ( void  )
virtual

Initialize the object.

Initialise the object data. This needs to be called during setup() to initialise new data for the class that cannot be done during the object creation. This method should be replaced in the derived class.

Reimplemented in MD_UISwitch_4017KM, MD_UISwitch_Matrix, MD_UISwitch_Analog, and MD_UISwitch_Digital.

◆ enableDoublePress()

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.

Parameters
ftrue to enable, false to disable.

◆ enableLongPress()

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, depening on the setting of the other options. Default is to detect long press events.

Parameters
ftrue to enable, false to disable.

◆ enableRepeat()

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.

Parameters
ftrue to enable, false to disable.

◆ enableRepeatResult()

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.

Parameters
ftrue to enable, false to disable (default).

◆ getKey()

virtual uint8_t MD_UISwitch::getKey ( void  )
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).

Returns
an identifiying index for the key item, depending on implementation

◆ processFSM()

MD_UISwitch::keyResult_t MD_UISwitch::processFSM ( bool  bState,
bool  reset = 0 
)
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 recognised 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).

Parameters
bStatetrue if the switch is active, false otherwise.
resetan optional identifier to reset the FSM.
Returns
one of the keyResult_t enumerated values.

◆ read()

virtual keyResult_t MD_UISwitch::read ( void  )
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 needs to be replaced in the derived class with a hardware specific method to read the key switch.

See also
processFSM() method
Returns
the keyResult_t enumerated value from processFSM()

Reimplemented in MD_UISwitch_4017KM, MD_UISwitch_Matrix, MD_UISwitch_Analog, and MD_UISwitch_Digital.

◆ setDebounceTime()

void MD_UISwitch::setDebounceTime ( uint16_t  t)

Set the debounce time

Set the switch debounce time in milliseconds. The default value is set by the KEY_DEBOUNCE_TIME constant.

Note that the relationship between timer values should be Debounce time < Long Press Time < Repeat time. No checking is done in the to enforce this relationship.

Parameters
tthe specified time in milliseconds.

◆ setDoublePressTime()

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.

Parameters
tthe specified time in milliseconds.

◆ setLongPressTime()

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 betweentimer values should be Debounce time < Long Press Time < Repeat time. No checking is done in the to enforce this relationship.

Parameters
tthe specified time in milliseconds.

◆ setRepeatTime()

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 Debounce time < Long Press Time < Repeat time. No checking is done in the to enforce this relationship.

Parameters
tthe specified time in milliseconds.

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