![]() |
MD_KeySwitch Library
1.4
Library for user keyswitch on digital input
|
#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 | |
Core object for the MD_KeySwitch library
|
protected |
FSM state values
States for the internal Finite State Machine to recognised the key press
| 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.
| pin | the digital pin to which the switch is connected. |
| onState | the state for the switch to be active |
| MD_KeySwitch::~MD_KeySwitch | ( | ) |
Class Destructor.
Release allocated memory and does the necessary to clean up once the queue is no longer required.
| 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.
| 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.
| f | true to enable, false to disable. |
| 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.
| f | true to enable, false to disable. |
| 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.
| f | true to enable, false to disable. |
| 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.
| f | true to enable, false to disable (default). |
| 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.
| 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.
| t | the specified time in milliseconds. |
| 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.
| t | the specified time in milliseconds. |
| 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.
| t | the specified time in milliseconds. |
| 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.
| t | the specified time in milliseconds. |