From 80fa2d9d60887e01a2e927a1a4011ca50d53074b Mon Sep 17 00:00:00 2001 From: Robert Marshall Date: Sat, 20 Aug 2022 22:14:41 +0100 Subject: [PATCH] Add touch input for lights --- src/TouchInput.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 9 +++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/TouchInput.cpp diff --git a/src/TouchInput.cpp b/src/TouchInput.cpp new file mode 100644 index 0000000..1df4c85 --- /dev/null +++ b/src/TouchInput.cpp @@ -0,0 +1,40 @@ +#ifndef TouchInput_cpp +#define TouchInput_cpp + +#include +#include + +#define TOUCH_THRESHOLD 20 + +class TouchInput { + const unsigned int _debounceDelay = 50; + + int _pin; + std::function _callback; + unsigned long _touchStart; + bool _currentTouchValue, _prevTouchValue; + + public: + TouchInput(int pin, std::function 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 diff --git a/src/main.cpp b/src/main.cpp index fbeb66e..42701e7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,7 @@ #include "EspNowControl.cpp" #include "LedManager.cpp" #include "TimerManager.cpp" +#include "TouchInput.cpp" #include LedManager *EspNowControl::_leds = 0; // WTF, C++? @@ -30,6 +31,13 @@ LedManager _leds(&_timers); BluetoothLightControl _bluetoothLightControl(&_leds); BluetoothLEDCallback _btCallback(&_bluetoothLightControl); BluetoothService _btService("Van Lights", &_btCallback); +TouchInput _touchInput(32, []() { + _seatingLeds.toggle(); + _kitchenLeds.toggle(); + _bathroomLeds.toggle(); + _awningLeds.toggle(); + _cabLeds.toggle(); +}); void setup() { Serial.begin(115200); @@ -61,4 +69,5 @@ void setup() { void loop() { _timers.loop(); _leds.loop(); + _touchInput.loop(); }