* @file OneButton.cpp
*
* @brief Library for detecting button clicks, doubleclicks and long press
* pattern on a single button.
*
* @author Matthias Hertel, https://www.mathertel.de
* @Copyright Copyright (c) by Matthias Hertel, https://www.mathertel.de.
*
* This work is licensed under a BSD style license. See
* http://www.mathertel.de/License.aspx
*
* More information on: https://www.mathertel.de/Arduino/OneButtonLibrary.aspx
*
* Changelog: see OneButton.h
*/
#include "OneButton.h"
* @brief Construct a new OneButton object but not (yet) initialize the IO pin.
*/
OneButton::OneButton()
{
_pin = -1;
}
* Initialize the OneButton library.
* @param pin The pin to be used for input from a momentary button.
* @param activeLow Set to true when the input level is LOW when the button is pressed, Default is true.
* @param pullupActive Activate the internal pullup when available. Default is true.
*/
OneButton::OneButton(const int pin, const boolean activeLow, const bool pullupActive)
{
_pin = pin;
if (activeLow) {
_buttonPressed = LOW;
} else {
_buttonPressed = HIGH;
}
if (pullupActive) {
pinMode(pin, INPUT_PULLUP);
} else {
pinMode(pin, INPUT);
}
}
void OneButton::setDebounceTicks(const int ticks)
{
_debounceTicks = ticks;
}
void OneButton::setClickTicks(const int ticks)
{
_clickTicks = ticks;
}
void OneButton::setPressTicks(const int ticks)
{
_pressTicks = ticks;
}
void OneButton::attachClick(callbackFunction newFunction)
{
_clickFunc = newFunction;
}
void OneButton::attachClick(parameterizedCallbackFunction newFunction, void *parameter)
{
_paramClickFunc = newFunction;
_clickFuncParam = parameter;
}
void OneButton::attachDoubleClick(callbackFunction newFunction)
{
_doubleClickFunc = newFunction;
_maxClicks = max(_maxClicks, 2);
}
void OneButton::attachDoubleClick(parameterizedCallbackFunction newFunction, void *parameter)
{
_paramDoubleClickFunc = newFunction;
_doubleClickFuncParam = parameter;
_maxClicks = max(_maxClicks, 2);
}
void OneButton::attachMultiClick(callbackFunction newFunction)
{
_multiClickFunc = newFunction;
_maxClicks = max(_maxClicks, 100);
}
void OneButton::attachMultiClick(parameterizedCallbackFunction newFunction, void *parameter)
{
_paramMultiClickFunc = newFunction;
_multiClickFuncParam = parameter;
_maxClicks = max(_maxClicks, 100);
}
void OneButton::attachLongPressStart(callbackFunction newFunction)
{
_longPressStartFunc = newFunction;
}
void OneButton::attachLongPressStart(parameterizedCallbackFunction newFunction, void *parameter)
{
_paramLongPressStartFunc = newFunction;
_longPressStartFuncParam = parameter;
}
void OneButton::attachLongPressStop(callbackFunction newFunction)
{
_longPressStopFunc = newFunction;
}
void OneButton::attachLongPressStop(parameterizedCallbackFunction newFunction, void *parameter)
{
_paramLongPressStopFunc = newFunction;
_longPressStopFuncParam = parameter;
}
void OneButton::attachDuringLongPress(callbackFunction newFunction)
{
_duringLongPressFunc = newFunction;
}
void OneButton::attachDuringLongPress(parameterizedCallbackFunction newFunction, void *parameter)
{
_paramDuringLongPressFunc = newFunction;
_duringLongPressFuncParam = parameter;
}
void OneButton::reset(void)
{
_state = OneButton::OCS_INIT;
_lastState = OneButton::OCS_INIT;
_nClicks = 0;
_startTime = 0;
}
int OneButton::getNumberClicks(void)
{
return _nClicks;
}
* @brief Check input of the configured pin and then advance the finite state
* machine (FSM).
*/
void OneButton::tick(void)
{
if (_pin >= 0 && _pin < 255) {
tick(digitalRead(_pin) == _buttonPressed);
}
}
* @brief Advance to a new state and save the last one to come back in cas of bouncing detection.
*/
void OneButton::_newState(stateMachine_t nextState)
{
_lastState = _state;
_state = nextState;
}
* @brief Run the finite state machine (FSM) using the given level.
*/
void OneButton::tick(bool activeLevel)
{
unsigned long now = millis();
unsigned long waitTime = (now - _startTime);
switch (_state) {
case OneButton::OCS_INIT:
if (activeLevel) {
_newState(OneButton::OCS_DOWN);
_startTime = now;
_nClicks = 0;
}
break;
case OneButton::OCS_DOWN:
if ((!activeLevel) && (waitTime < _debounceTicks)) {
_newState(_lastState);
} else if (!activeLevel) {
_newState(OneButton::OCS_UP);
_startTime = now;
} else if ((activeLevel) && (waitTime > _pressTicks)) {
if (_longPressStartFunc) _longPressStartFunc();
if (_paramLongPressStartFunc) _paramLongPressStartFunc(_longPressStartFuncParam);
_newState(OneButton::OCS_PRESS);
}
break;
case OneButton::OCS_UP:
if ((activeLevel) && (waitTime < _debounceTicks)) {
_newState(_lastState);
} else if (waitTime >= _debounceTicks) {
_nClicks++;
_newState(OneButton::OCS_COUNT);
}
break;
case OneButton::OCS_COUNT:
if (activeLevel) {
_newState(OneButton::OCS_DOWN);
_startTime = now;
} else if ((waitTime > _clickTicks) || (_nClicks == _maxClicks)) {
if (_nClicks == 1) {
if (_clickFunc) _clickFunc();
if (_paramClickFunc) _paramClickFunc(_clickFuncParam);
} else if (_nClicks == 2) {
if (_doubleClickFunc) _doubleClickFunc();
if (_paramDoubleClickFunc) _paramDoubleClickFunc(_doubleClickFuncParam);
} else {
if (_multiClickFunc) _multiClickFunc();
if (_paramMultiClickFunc) _paramMultiClickFunc(_multiClickFuncParam);
}
reset();
}
break;
case OneButton::OCS_PRESS:
if (!activeLevel) {
_newState(OneButton::OCS_PRESSEND);
_startTime = now;
} else {
if (_duringLongPressFunc) _duringLongPressFunc();
if (_paramDuringLongPressFunc) _paramDuringLongPressFunc(_duringLongPressFuncParam);
}
break;
case OneButton::OCS_PRESSEND:
if ((activeLevel) && (waitTime < _debounceTicks)) {
_newState(_lastState);
} else if (waitTime >= _debounceTicks) {
if (_longPressStopFunc) _longPressStopFunc();
if (_paramLongPressStopFunc) _paramLongPressStopFunc(_longPressStopFuncParam);
reset();
}
break;
default:
_newState(OneButton::OCS_INIT);
break;
}
}