MD_KeySwitch Library  1.4
Library for user keyswitch on digital input
MD_KeySwitch Class Reference

#include <MD_KeySwitch.h>

Public Types

Enumerated values and Typedefs.

/** Return values for switch status

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

enum  keyResult_t {
  KS_NULL, KS_PRESS, KS_DPRESS, KS_LONGPRESS,
  KS_RPTPRESS
}
 

Public Member Functions

Class constructor and destructor.
 MD_KeySwitch (uint8_t pin, uint8_t onState=KEY_ACTIVE_STATE)
 
 ~MD_KeySwitch ()
 
Methods for core object control.
void begin (void)
 
keyResult_t read (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 Attributes

uint8_t _pin
 pin number
 
uint8_t _onState
 digital state for ON
 
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
 
uint16_t _timeRepeat
 repeat time delay in milliseconds
 

Detailed Description

Core object for the MD_KeySwitch library

Member Enumeration Documentation

◆ keyResult_t

Enumerator
KS_NULL 

No key press.

KS_PRESS 

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

KS_DPRESS 

Double press.

KS_LONGPRESS 

Long press.

KS_RPTPRESS 

Repeated key press (only if enableRepeatResult(true))

◆ state_t

enum MD_KeySwitch::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_KeySwitch()

MD_KeySwitch::MD_KeySwitch ( uint8_t  pin,
uint8_t  onState = KEY_ACTIVE_STATE 
)

Class Constructor.

Instantiate a new instance of the class. The parameters passed are used to the hardware interface to the switch.

The option parameter onState telles the library which level (LOW or HIGH) should be considered the switch 'on' state. If the default LOW state is selected then the library will initialise the pin with INPUT_PULLUP and no external pullup resistors are necessary. If specified HIGH, external pull down resistors will be required.

Parameters
pinthe digital pin to which the switch is connected.
onStatethe state for the switch to be active

◆ ~MD_KeySwitch()

MD_KeySwitch::~MD_KeySwitch ( )

Class Destructor.

Release allocated memory and does the necessary to clean up once the queue is no longer required.

Member Function Documentation

◆ begin()

void MD_KeySwitch::begin ( void  )

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.

◆ enableDoublePress()

void MD_KeySwitch::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_KeySwitch::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_KeySwitch::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_KeySwitch::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).

◆ read()

MD_KeySwitch::keyResult_t MD_KeySwitch::read ( void  )

Return the state of the switch

Return one of the keypress types depending on what has been detected. 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 whose operation is directed by the timer and option values specified.

Returns
one of the keyResult_t enumerated values

◆ setDebounceTime()

void MD_KeySwitch::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_KeySwitch::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_KeySwitch::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_KeySwitch::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: