Skip to content

Commit

Permalink
Add ESP32 touch sensors as button alternative (Aircoookie#1190)
Browse files Browse the repository at this point in the history
* Add touch option to button handler

* Check if touch is pressed in setup

* Add TOUCHPIN build env and override example

Co-authored-by: Aircoookie <[email protected]>
  • Loading branch information
Jakeler and Aircoookie authored Sep 20, 2020
1 parent 1313a44 commit a3e1af7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 6 deletions.
8 changes: 8 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,14 @@ lib_ignore =
ESPAsyncTCP
ESPAsyncUDP

[env:custom32_TOUCHPIN_T0]
board = esp32dev
platform = [email protected]
build_flags = ${common.build_flags_esp32} -D TOUCHPIN=T0
lib_ignore =
ESPAsyncTCP
ESPAsyncUDP

[env:wemos_shield_esp32]
board = esp32dev
platform = [email protected]
Expand Down
1 change: 1 addition & 0 deletions platformio_override.ini.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 15 additions & 4 deletions wled00/button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions wled00/const.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions wled00/fcn_declare.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ void updateBlynk();

//button.cpp
void shortPressAction();
bool isButtonPressed();
void handleButton();
void handleIO();

Expand Down
4 changes: 2 additions & 2 deletions wled00/wled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit a3e1af7

Please sign in to comment.