From a3e1af72ab73af36b0aef5c131d3dc61f818fc0f Mon Sep 17 00:00:00 2001 From: Jake Date: Sun, 20 Sep 2020 16:12:46 +0200 Subject: [PATCH] Add ESP32 touch sensors as button alternative (#1190) * Add touch option to button handler * Check if touch is pressed in setup * Add TOUCHPIN build env and override example Co-authored-by: Aircoookie --- platformio.ini | 8 ++++++++ platformio_override.ini.example | 1 + wled00/button.cpp | 19 +++++++++++++++---- wled00/const.h | 2 ++ wled00/fcn_declare.h | 1 + wled00/wled.cpp | 4 ++-- 6 files changed, 29 insertions(+), 6 deletions(-) diff --git a/platformio.ini b/platformio.ini index 700ec9d0..06ec2e5f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -334,6 +334,14 @@ lib_ignore = ESPAsyncTCP ESPAsyncUDP +[env:custom32_TOUCHPIN_T0] +board = esp32dev +platform = espressif32@1.12.4 +build_flags = ${common.build_flags_esp32} -D TOUCHPIN=T0 +lib_ignore = + ESPAsyncTCP + ESPAsyncUDP + [env:wemos_shield_esp32] board = esp32dev platform = espressif32@1.12.4 diff --git a/platformio_override.ini.example b/platformio_override.ini.example index da024970..240c486a 100644 --- a/platformio_override.ini.example +++ b/platformio_override.ini.example @@ -21,6 +21,7 @@ build_flags = ${common.build_flags_esp8266} ; PIN defines - uncomment and change, if needed: ; -D LEDPIN=2 ; -D BTNPIN=0 +; -D TOUCHPIN=T0 ; -D IR_PIN=4 ; -D RLYPIN=12 ; -D RLYMDE=1 diff --git a/wled00/button.cpp b/wled00/button.cpp index e71cce96..3c7b7f14 100644 --- a/wled00/button.cpp +++ b/wled00/button.cpp @@ -15,13 +15,24 @@ void shortPressAction() } } +bool isButtonPressed() +{ + #ifdef BTNPIN + if (digitalRead(BTNPIN) == LOW) return true; + #endif + #ifdef TOUCHPIN + if (touchRead(TOUCHPIN) <= TOUCH_THRESHOLD) return true; + #endif + return false; +} + void handleButton() { -#ifdef BTNPIN +#if defined(BTNPIN) || defined(TOUCHPIN) if (!buttonEnabled) return; - - if (digitalRead(BTNPIN) == LOW) //pressed + + if (isButtonPressed()) //pressed { if (!buttonPressedBefore) buttonPressedTime = millis(); buttonPressedBefore = true; @@ -37,7 +48,7 @@ void handleButton() } } } - else if (digitalRead(BTNPIN) == HIGH && buttonPressedBefore) //released + else if (!isButtonPressed() && buttonPressedBefore) //released { long dur = millis() - buttonPressedTime; if (dur < 50) {buttonPressedBefore = false; return;} //too short "press", debounce diff --git a/wled00/const.h b/wled00/const.h index 97bcb3fd..cc65791e 100644 --- a/wled00/const.h +++ b/wled00/const.h @@ -126,6 +126,8 @@ #define ABL_MILLIAMPS_DEFAULT 850; // auto lower brightness to stay close to milliampere limit +#define TOUCH_THRESHOLD 32 // limit to recognize a touch, higher value means more sensitive + // Size of buffer for API JSON object (increase for more segments) #ifdef ESP8266 #define JSON_BUFFER_SIZE 9216 diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index 9cc285cb..86d20934 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -21,6 +21,7 @@ void updateBlynk(); //button.cpp void shortPressAction(); +bool isButtonPressed(); void handleButton(); void handleIO(); diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 7534d25d..befc02c5 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -250,8 +250,8 @@ void WLED::beginStrip() #endif // disable button if it is "pressed" unintentionally -#ifdef BTNPIN - if (digitalRead(BTNPIN) == LOW) +#if defined(BTNPIN) || defined(TOUCHPIN) + if (isButtonPressed()) buttonEnabled = false; #else buttonEnabled = false;