Add touch input for lights
This commit is contained in:
parent
f61b3ffcc6
commit
80fa2d9d60
2 changed files with 49 additions and 0 deletions
40
src/TouchInput.cpp
Normal file
40
src/TouchInput.cpp
Normal file
|
@ -0,0 +1,40 @@
|
|||
#ifndef TouchInput_cpp
|
||||
#define TouchInput_cpp
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <functional>
|
||||
|
||||
#define TOUCH_THRESHOLD 20
|
||||
|
||||
class TouchInput {
|
||||
const unsigned int _debounceDelay = 50;
|
||||
|
||||
int _pin;
|
||||
std::function<void()> _callback;
|
||||
unsigned long _touchStart;
|
||||
bool _currentTouchValue, _prevTouchValue;
|
||||
|
||||
public:
|
||||
TouchInput(int pin, std::function<void()> callback){
|
||||
_pin = pin;
|
||||
_callback = callback;
|
||||
}
|
||||
|
||||
void loop(){
|
||||
bool touching = touchRead(_pin) <= TOUCH_THRESHOLD;
|
||||
|
||||
if (_prevTouchValue != touching) {
|
||||
_touchStart = millis();
|
||||
}
|
||||
|
||||
if (millis() - _touchStart >= _debounceDelay && touching != _currentTouchValue){
|
||||
_currentTouchValue = touching;
|
||||
if (touching)
|
||||
_callback();
|
||||
}
|
||||
|
||||
_prevTouchValue = touching;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue