MD_UISwitch Library 2.2
Library for different types of User Interface switches
MD_UISwitch Class Referenceabstract

#include <MD_UISwitch.h>

Inheritance diagram for MD_UISwitch:

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
 

Static Protected Attributes

static const uint16_t KEY_PRESS_TIME = 150
 Default key press time in milliseconds.
 
static const uint16_t KEY_DPRESS_TIME = 250
 Default double press time between presses in milliseconds.
 
static const uint16_t KEY_LONGPRESS_TIME = 600
 Default long press detection time in milliseconds.
 
static const uint16_t KEY_REPEAT_TIME = 300
 Default time between repeats in in milliseconds.
 
static const uint8_t KEY_ACTIVE_STATE = LOW
 Default key is active low - transition high to low detection.
 
static const uint8_t REPEAT_RESULT_ENABLE = 3
 Internal status bit to return KS_REPEAT instead of KS_PRESS.
 
static const uint8_t DPRESS_ENABLE = 2
 Internal status bit to enable double press.
 
static const uint8_t LONGPRESS_ENABLE = 1
 Internal status bit to enable long press.
 
static const uint8_t REPEAT_ENABLE = 0
 Internal status bit to enable repeat key.
 

Detailed Description

Core object for the MD_UISwitch 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_DOWN 

Switch is detected as active (down)

KEY_UP 

Switch is detected as inactive (up)

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_db

enum MD_UISwitch::state_db
protected

Debouncing state values

States for the internal Debouncing algorithm

Enumerator
S_WAIT_START 

Waiting for the debouncing to start.

S_DEBOUNCE 

Currently debouncing state.

S_WAIT_RELEASE 

Waiting for the next transition to be detected.

◆ state_fsm

enum MD_UISwitch::state_fsm
protected

FSM state values

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

Enumerator
S_IDLE 

Idle state - waiting for key transition.

S_PRESS 

Detecting possible simple press.

S_PRESS2A 

Detecting possible double press part A.

S_PRESS2B 

Detecting possible double press part B.

S_PRESSL 

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 initialize 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  )
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.

◆ debounce()

bool MD_UISwitch::debounce ( bool  curStatus,
bool  reset = false 
)
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.

Parameters
curStatuscurrent active status for the switch.
resetan optional identifier to reset the debounce detection.
Returns
true if the switch is 'debounced' active, false otherwise.

◆ 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, depending 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 identifying index for the key item, depending on implementation

◆ processFSM()

MD_UISwitch::keyResult_t MD_UISwitch::processFSM ( bool  swState,
bool  reset = false 
)
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).

Parameters
swStatetrue 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  )
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.

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

Implemented in MD_UISwitch_Digital, MD_UISwitch_User, MD_UISwitch_Analog, MD_UISwitch_Matrix, and MD_UISwitch_4017KM.

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

Parameters
tthe specified time in milliseconds.

◆ setPressTime()

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.

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 Press 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: