Add touch input for lights

This commit is contained in:
Robert Marshall 2022-08-20 22:14:41 +01:00
parent f61b3ffcc6
commit 80fa2d9d60
2 changed files with 49 additions and 0 deletions

40
src/TouchInput.cpp Normal file
View 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

View file

@ -5,6 +5,7 @@
#include "EspNowControl.cpp" #include "EspNowControl.cpp"
#include "LedManager.cpp" #include "LedManager.cpp"
#include "TimerManager.cpp" #include "TimerManager.cpp"
#include "TouchInput.cpp"
#include <Arduino.h> #include <Arduino.h>
LedManager *EspNowControl::_leds = 0; // WTF, C++? LedManager *EspNowControl::_leds = 0; // WTF, C++?
@ -30,6 +31,13 @@ LedManager _leds(&_timers);
BluetoothLightControl _bluetoothLightControl(&_leds); BluetoothLightControl _bluetoothLightControl(&_leds);
BluetoothLEDCallback _btCallback(&_bluetoothLightControl); BluetoothLEDCallback _btCallback(&_bluetoothLightControl);
BluetoothService _btService("Van Lights", &_btCallback); BluetoothService _btService("Van Lights", &_btCallback);
TouchInput _touchInput(32, []() {
_seatingLeds.toggle();
_kitchenLeds.toggle();
_bathroomLeds.toggle();
_awningLeds.toggle();
_cabLeds.toggle();
});
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
@ -61,4 +69,5 @@ void setup() {
void loop() { void loop() {
_timers.loop(); _timers.loop();
_leds.loop(); _leds.loop();
_touchInput.loop();
} }