初始化提交
This commit is contained in:
1
arduino-cli/libraries/ESP32touchButton/.gitignore
vendored
Normal file
1
arduino-cli/libraries/ESP32touchButton/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
.DS_Store
|
||||
284
arduino-cli/libraries/ESP32touchButton/ESP32touchButton.cpp
Normal file
284
arduino-cli/libraries/ESP32touchButton/ESP32touchButton.cpp
Normal file
@@ -0,0 +1,284 @@
|
||||
/**
|
||||
* @file ESP32touchButton.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/ESP32touchButtonLibrary.aspx
|
||||
*
|
||||
* Changelog: see ESP32touchButton.h
|
||||
*/
|
||||
|
||||
#include "ESP32touchButton.h"
|
||||
|
||||
// ----- Initialization and Default Values -----
|
||||
|
||||
/**
|
||||
* @brief Construct a new ESP32touchButton object but not (yet) initialize the IO pin.
|
||||
*/
|
||||
ESP32touchButton::ESP32touchButton()
|
||||
{
|
||||
_pin = -1;
|
||||
// further initialization has moved to ESP32touchButton.h
|
||||
}
|
||||
|
||||
ESP32touchButton::ESP32touchButton(int pin, int activeLow, bool pullupActive)
|
||||
{
|
||||
// ESP32touchButton();
|
||||
_pin = pin;
|
||||
|
||||
if (activeLow) {
|
||||
// the button connects the input pin to GND when pressed.
|
||||
_buttonPressed = LOW;
|
||||
|
||||
} else {
|
||||
// the button connects the input pin to VCC when pressed.
|
||||
_buttonPressed = HIGH;
|
||||
} // if
|
||||
|
||||
if (pullupActive) {
|
||||
// use the given pin as input and activate internal PULLUP resistor.
|
||||
//pinMode(pin, INPUT_PULLUP);
|
||||
} else {
|
||||
// use the given pin as input
|
||||
//pinMode(pin, INPUT);
|
||||
} // if
|
||||
} // ESP32touchButton
|
||||
|
||||
|
||||
// explicitly set the number of millisec that have to pass by before a click is
|
||||
// assumed as safe.
|
||||
void ESP32touchButton::setDebounceTicks(int ticks)
|
||||
{
|
||||
_debounceTicks = ticks;
|
||||
} // setDebounceTicks
|
||||
|
||||
// explicitly set the number of millisec that have to pass by before a click is
|
||||
// detected.
|
||||
void ESP32touchButton::setClickTicks(int ticks)
|
||||
{
|
||||
_clickTicks = ticks;
|
||||
} // setClickTicks
|
||||
|
||||
|
||||
// explicitly set the number of millisec that have to pass by before a long
|
||||
// button press is detected.
|
||||
void ESP32touchButton::setPressTicks(int ticks)
|
||||
{
|
||||
_pressTicks = ticks;
|
||||
} // setPressTicks
|
||||
|
||||
|
||||
// save function for click event
|
||||
void ESP32touchButton::attachClick(callbackFunction newFunction)
|
||||
{
|
||||
_clickFunc = newFunction;
|
||||
} // attachClick
|
||||
|
||||
|
||||
// save function for parameterized click event
|
||||
void ESP32touchButton::attachClick(parameterizedCallbackFunction newFunction, void* parameter)
|
||||
{
|
||||
_paramClickFunc = newFunction;
|
||||
_clickFuncParam = parameter;
|
||||
} // attachClick
|
||||
|
||||
|
||||
// save function for doubleClick event
|
||||
void ESP32touchButton::attachDoubleClick(callbackFunction newFunction)
|
||||
{
|
||||
_doubleClickFunc = newFunction;
|
||||
} // attachDoubleClick
|
||||
|
||||
|
||||
// save function for parameterized doubleClick event
|
||||
void ESP32touchButton::attachDoubleClick(parameterizedCallbackFunction newFunction, void* parameter)
|
||||
{
|
||||
_paramDoubleClickFunc = newFunction;
|
||||
_doubleClickFuncParam = parameter;
|
||||
} // attachDoubleClick
|
||||
|
||||
|
||||
// save function for press event
|
||||
// DEPRECATED, is replaced by attachLongPressStart, attachLongPressStop,
|
||||
// attachDuringLongPress,
|
||||
void ESP32touchButton::attachPress(callbackFunction newFunction)
|
||||
{
|
||||
_pressFunc = newFunction;
|
||||
} // attachPress
|
||||
|
||||
// save function for longPressStart event
|
||||
void ESP32touchButton::attachLongPressStart(callbackFunction newFunction)
|
||||
{
|
||||
_longPressStartFunc = newFunction;
|
||||
} // attachLongPressStart
|
||||
|
||||
// save function for parameterized longPressStart event
|
||||
void ESP32touchButton::attachLongPressStart(parameterizedCallbackFunction newFunction, void* parameter)
|
||||
{
|
||||
_paramLongPressStartFunc = newFunction;
|
||||
_longPressStartFuncParam = parameter;
|
||||
} // attachLongPressStart
|
||||
|
||||
// save function for longPressStop event
|
||||
void ESP32touchButton::attachLongPressStop(callbackFunction newFunction)
|
||||
{
|
||||
_longPressStopFunc = newFunction;
|
||||
} // attachLongPressStop
|
||||
|
||||
// save function for parameterized longPressStop event
|
||||
void ESP32touchButton::attachLongPressStop(parameterizedCallbackFunction newFunction, void* parameter)
|
||||
{
|
||||
_paramLongPressStopFunc = newFunction;
|
||||
_longPressStopFuncParam = parameter;
|
||||
} // attachLongPressStop
|
||||
|
||||
// save function for during longPress event
|
||||
void ESP32touchButton::attachDuringLongPress(callbackFunction newFunction)
|
||||
{
|
||||
_duringLongPressFunc = newFunction;
|
||||
} // attachDuringLongPress
|
||||
|
||||
// save function for parameterized during longPress event
|
||||
void ESP32touchButton::attachDuringLongPress(parameterizedCallbackFunction newFunction, void* parameter)
|
||||
{
|
||||
_paramDuringLongPressFunc = newFunction;
|
||||
_duringLongPressFuncParam = parameter;
|
||||
} // attachDuringLongPress
|
||||
|
||||
// function to get the current long pressed state
|
||||
bool ESP32touchButton::isLongPressed(){
|
||||
return _isLongPressed;
|
||||
}
|
||||
|
||||
int ESP32touchButton::getPressedTicks(){
|
||||
return _stopTime - _startTime;
|
||||
}
|
||||
|
||||
void ESP32touchButton::reset(void){
|
||||
_state = 0; // restart.
|
||||
_startTime = 0;
|
||||
_stopTime = 0;
|
||||
_isLongPressed = false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Check input of the configured pin and then advance the finite state
|
||||
* machine (FSM).
|
||||
*/
|
||||
void ESP32touchButton::tick(void)
|
||||
{
|
||||
if (_pin >= 0) {
|
||||
tick((touchRead(_pin) >= 40) == _buttonPressed);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Advance the finite state machine (FSM) using the given level.
|
||||
*/
|
||||
void ESP32touchButton::tick(bool activeLevel)
|
||||
{
|
||||
unsigned long now = millis(); // current (relative) time in msecs.
|
||||
|
||||
// Implementation of the state machine
|
||||
|
||||
if (_state == 0) { // waiting for menu pin being pressed.
|
||||
if (activeLevel) {
|
||||
_state = 1; // step to state 1
|
||||
_startTime = now; // remember starting time
|
||||
} // if
|
||||
|
||||
} else if (_state == 1) { // waiting for menu pin being released.
|
||||
|
||||
if ((!activeLevel) &&
|
||||
((unsigned long)(now - _startTime) < _debounceTicks)) {
|
||||
// button was released to quickly so I assume some debouncing.
|
||||
// go back to state 0 without calling a function.
|
||||
_state = 0;
|
||||
|
||||
} else if (!activeLevel) {
|
||||
_state = 2; // step to state 2
|
||||
_stopTime = now; // remember stopping time
|
||||
|
||||
} else if ((activeLevel) &&
|
||||
((unsigned long)(now - _startTime) > _pressTicks)) {
|
||||
_isLongPressed = true; // Keep track of long press state
|
||||
if (_pressFunc)
|
||||
_pressFunc();
|
||||
if (_longPressStartFunc)
|
||||
_longPressStartFunc();
|
||||
if (_paramLongPressStartFunc)
|
||||
_paramLongPressStartFunc(_longPressStartFuncParam);
|
||||
if (_duringLongPressFunc)
|
||||
_duringLongPressFunc();
|
||||
if (_paramDuringLongPressFunc)
|
||||
_paramDuringLongPressFunc(_duringLongPressFuncParam);
|
||||
_state = 6; // step to state 6
|
||||
_stopTime = now; // remember stopping time
|
||||
} else {
|
||||
// wait. Stay in this state.
|
||||
} // if
|
||||
|
||||
} else if (_state == 2) {
|
||||
// waiting for menu pin being pressed the second time or timeout.
|
||||
if ((_doubleClickFunc == NULL && _paramDoubleClickFunc == NULL) ||
|
||||
(unsigned long)(now - _startTime) > _clickTicks) {
|
||||
// this was only a single short click
|
||||
if (_clickFunc)
|
||||
_clickFunc();
|
||||
if (_paramClickFunc)
|
||||
_paramClickFunc(_clickFuncParam);
|
||||
_state = 0; // restart.
|
||||
|
||||
} else if ((activeLevel) &&
|
||||
((unsigned long)(now - _stopTime) > _debounceTicks)) {
|
||||
_state = 3; // step to state 3
|
||||
_startTime = now; // remember starting time
|
||||
} // if
|
||||
|
||||
} else if (_state == 3) { // waiting for menu pin being released finally.
|
||||
// Stay here for at least _debounceTicks because else we might end up in
|
||||
// state 1 if the button bounces for too long.
|
||||
if ((!activeLevel) &&
|
||||
((unsigned long)(now - _startTime) > _debounceTicks)) {
|
||||
// this was a 2 click sequence.
|
||||
if (_doubleClickFunc)
|
||||
_doubleClickFunc();
|
||||
if (_paramDoubleClickFunc)
|
||||
_paramDoubleClickFunc(_doubleClickFuncParam);
|
||||
_state = 0; // restart.
|
||||
_stopTime = now; // remember stopping time
|
||||
} // if
|
||||
|
||||
} else if (_state == 6) {
|
||||
// waiting for menu pin being release after long press.
|
||||
if (!activeLevel) {
|
||||
_isLongPressed = false; // Keep track of long press state
|
||||
if (_longPressStopFunc)
|
||||
_longPressStopFunc();
|
||||
if (_paramLongPressStopFunc)
|
||||
_paramLongPressStopFunc(_longPressStopFuncParam);
|
||||
_state = 0; // restart.
|
||||
_stopTime = now; // remember stopping time
|
||||
} else {
|
||||
// button is being long pressed
|
||||
_isLongPressed = true; // Keep track of long press state
|
||||
if (_duringLongPressFunc)
|
||||
_duringLongPressFunc();
|
||||
if (_paramDuringLongPressFunc)
|
||||
_paramDuringLongPressFunc(_duringLongPressFuncParam);
|
||||
} // if
|
||||
|
||||
} // if
|
||||
} // ESP32touchButton.tick()
|
||||
|
||||
|
||||
// end.
|
||||
133
arduino-cli/libraries/ESP32touchButton/ESP32touchButton.h
Normal file
133
arduino-cli/libraries/ESP32touchButton/ESP32touchButton.h
Normal file
@@ -0,0 +1,133 @@
|
||||
// -----
|
||||
// ESP32touchButton.h - Library for detecting button clicks, doubleclicks and long
|
||||
// press pattern on a single button. This class is implemented for use with the
|
||||
// Arduino environment. Copyright (c) by Matthias Hertel,
|
||||
// http://www.mathertel.de This work is licensed under a BSD style license. See
|
||||
// http://www.mathertel.de/License.aspx More information on:
|
||||
// http://www.mathertel.de/Arduino
|
||||
// -----
|
||||
// 02.10.2010 created by Matthias Hertel
|
||||
// 21.04.2011 transformed into a library
|
||||
// 01.12.2011 include file changed to work with the Arduino 1.0 environment
|
||||
// 23.03.2014 Enhanced long press functionalities by adding longPressStart and
|
||||
// longPressStop callbacks
|
||||
// 21.09.2015 A simple way for debounce detection added.
|
||||
// 14.05.2017 Debouncing improvements.
|
||||
// 25.06.2018 Optional third parameter for deactivating pullup.
|
||||
// 26.09.2018 Anatoli Arkhipenko: Included solution to use library with other
|
||||
// sources of input.
|
||||
// 26.09.2018 Initialization moved into class declaration.
|
||||
// 26.09.2018 Jay M Ericsson: compiler warnings removed.
|
||||
// -----
|
||||
|
||||
#ifndef ESP32touchButton_h
|
||||
#define ESP32touchButton_h
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
// ----- Callback function types -----
|
||||
|
||||
extern "C" {
|
||||
typedef void (*callbackFunction)(void);
|
||||
typedef void (*parameterizedCallbackFunction)(void*);
|
||||
}
|
||||
|
||||
|
||||
class ESP32touchButton
|
||||
{
|
||||
public:
|
||||
// ----- Constructor -----
|
||||
ESP32touchButton();
|
||||
|
||||
ESP32touchButton(int pin, int active, bool pullupActive = true);
|
||||
|
||||
// ----- Set runtime parameters -----
|
||||
|
||||
// set # millisec after safe click is assumed.
|
||||
void setDebounceTicks(int ticks);
|
||||
|
||||
// set # millisec after single click is assumed.
|
||||
void setClickTicks(int ticks);
|
||||
|
||||
// set # millisec after press is assumed.
|
||||
void setPressTicks(int ticks);
|
||||
|
||||
// attach functions that will be called when button was pressed in the
|
||||
// specified way.
|
||||
void attachClick(callbackFunction newFunction);
|
||||
void attachClick(parameterizedCallbackFunction newFunction, void* parameter);
|
||||
void attachDoubleClick(callbackFunction newFunction);
|
||||
void attachDoubleClick(parameterizedCallbackFunction newFunction, void* parameter);
|
||||
void attachPress(
|
||||
callbackFunction newFunction); // DEPRECATED, replaced by longPressStart,
|
||||
// longPressStop and duringLongPress
|
||||
void attachLongPressStart(callbackFunction newFunction);
|
||||
void attachLongPressStart(parameterizedCallbackFunction newFunction, void* parameter);
|
||||
void attachLongPressStop(callbackFunction newFunction);
|
||||
void attachLongPressStop(parameterizedCallbackFunction newFunction, void* parameter);
|
||||
void attachDuringLongPress(callbackFunction newFunction);
|
||||
void attachDuringLongPress(parameterizedCallbackFunction newFunction, void* parameter);
|
||||
|
||||
// ----- State machine functions -----
|
||||
|
||||
/**
|
||||
* @brief Call this function every some milliseconds for checking the input
|
||||
* level at the initialized digital pin.
|
||||
*/
|
||||
void tick(void);
|
||||
|
||||
/**
|
||||
* @brief Call this function every time the input level has changed.
|
||||
* Using this function no digital input pin is checked because the current
|
||||
* level is given by the parameter.
|
||||
*/
|
||||
void tick(bool level);
|
||||
|
||||
bool isLongPressed();
|
||||
int getPressedTicks();
|
||||
void reset(void);
|
||||
|
||||
private:
|
||||
int _pin; // hardware pin number.
|
||||
unsigned int _debounceTicks = 50; // number of ticks for debounce times.
|
||||
unsigned int _clickTicks = 600; // number of ticks that have to pass by
|
||||
// before a click is detected.
|
||||
unsigned int _pressTicks = 1000; // number of ticks that have to pass by
|
||||
// before a long button press is detected
|
||||
|
||||
int _buttonPressed;
|
||||
|
||||
bool _isLongPressed = false;
|
||||
|
||||
// These variables will hold functions acting as event source.
|
||||
callbackFunction _clickFunc = NULL;
|
||||
parameterizedCallbackFunction _paramClickFunc = NULL;
|
||||
void* _clickFuncParam = NULL;
|
||||
|
||||
callbackFunction _doubleClickFunc = NULL;
|
||||
parameterizedCallbackFunction _paramDoubleClickFunc = NULL;
|
||||
void* _doubleClickFuncParam = NULL;
|
||||
|
||||
callbackFunction _pressFunc = NULL;
|
||||
|
||||
callbackFunction _longPressStartFunc = NULL;
|
||||
parameterizedCallbackFunction _paramLongPressStartFunc = NULL;
|
||||
void* _longPressStartFuncParam = NULL;
|
||||
|
||||
callbackFunction _longPressStopFunc = NULL;
|
||||
parameterizedCallbackFunction _paramLongPressStopFunc = NULL;
|
||||
void* _longPressStopFuncParam;
|
||||
|
||||
callbackFunction _duringLongPressFunc = NULL;
|
||||
parameterizedCallbackFunction _paramDuringLongPressFunc = NULL;
|
||||
void* _duringLongPressFuncParam = NULL;
|
||||
|
||||
// These variables that hold information across the upcoming tick calls.
|
||||
// They are initialized once on program start and are updated every time the
|
||||
// tick function is called.
|
||||
int _state = 0;
|
||||
unsigned long _startTime; // will be set in state 1
|
||||
unsigned long _stopTime; // will be set in state 2
|
||||
};
|
||||
|
||||
#endif
|
||||
15
arduino-cli/libraries/ESP32touchButton/LICENSE
Normal file
15
arduino-cli/libraries/ESP32touchButton/LICENSE
Normal file
@@ -0,0 +1,15 @@
|
||||
See http://www.mathertel.de/License.aspx
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2005-2014 by Matthias Hertel, http://www.mathertel.de/
|
||||
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
•Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
•Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
•Neither the name of the copyright owners nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
12
arduino-cli/libraries/ESP32touchButton/README.md
Normal file
12
arduino-cli/libraries/ESP32touchButton/README.md
Normal file
@@ -0,0 +1,12 @@
|
||||
Arduino OneButton Library
|
||||
===
|
||||
|
||||
This Arduino libary is improving the usage of a singe button for input.
|
||||
It shows how to use an digital input pin with a single pushbutton attached
|
||||
for detecting some of the typical button press events like single clicks, double clicks and long-time pressing.
|
||||
This enables you to reuse the same button for multiple functions and lowers the hardware invests.
|
||||
|
||||
This is also a sample for implementing simple finite-state machines by using the simple pattern above.
|
||||
|
||||
You can find more detail on this library at
|
||||
http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
|
||||
37
arduino-cli/libraries/ESP32touchButton/keywords.txt
Normal file
37
arduino-cli/libraries/ESP32touchButton/keywords.txt
Normal file
@@ -0,0 +1,37 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map for OneButton
|
||||
#######################################
|
||||
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
callbackFunction KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
setClickTicks KEYWORD2
|
||||
setPressTicks KEYWORD2
|
||||
setDebounceTicks KEYWORD2
|
||||
attachClick KEYWORD2
|
||||
attachDoubleClick KEYWORD2
|
||||
attachLongPressStart KEYWORD2
|
||||
attachLongPressStop KEYWORD2
|
||||
attachDuringLongPress KEYWORD2
|
||||
isLongPressed KEYWORD2
|
||||
tick KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Instances (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
OneButton KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
|
||||
|
||||
11
arduino-cli/libraries/ESP32touchButton/library.json
Normal file
11
arduino-cli/libraries/ESP32touchButton/library.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"name": "OneButton",
|
||||
"keywords": "arduino, button, pushbutton",
|
||||
"description": "This Arduino library is improving the usage of a singe button for input. It shows how to use an digital input pin with a single pushbutton attached for detecting some of the typical button press events like single clicks, double clicks and long-time pressing. This enables you to reuse the same button for multiple functions and lowers the hardware invests.",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/mathertel/OneButton"
|
||||
},
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*"
|
||||
}
|
||||
Reference in New Issue
Block a user