Files
arduino-esp32/libraries/ESP32/examples/Touch/TouchButton/TouchButton.ino
Rodrigo Garcia 0ea485e518 Touch Sensor IDF Refactoring (#6194)
Summary

Touch Sensor refactoring to be based on IDF 4.4.
Adds support to ESP32S2 and future ESP32S3.

Adds some new APIs:

For all chips:

void touchAttachInterruptArg(uint8_t pin, void (*userFunc)(void*), void *arg, uint32_t threshold);
This function allows the user to add and pass a void* parameter to the ISR user callback.

void touchDetachInterrupt(uint8_t pin);
This function detaches ISR call back for the touch pad pin.

Only ESP32 chip

void touchInterruptSetThresholdDirection(bool mustbeLower);
This function allows the user to set if the ISR callback will be activated when the touch sensor readings are lower or higher than the threshold defined. See example TouchButton.ino.

Only ESP32-S2 and ESP32-S3 chips

bool touchInterruptGetLastStatus(uint8_t pin);
This function reports if the touch pad pin is touched or untouched. It can be used with ISR to identify when it is touched and untouched (released). See example TouchButtonV2.ino.

Impact

None. ll original APIs and examples now run on ESP32 and ESP32-S2.

Related links

Fix #6095
Fix #6034
Fix #5799
Fix #5745
Fix #5527
Fix #5493
Fix #4321
Fix #4044
Fix #2722
Fix #2625
2022-02-03 19:06:12 +02:00

47 lines
1.2 KiB
C++

/*
This is an example how to use Touch Intrrerupts
The sketh will tell when it is touched and then relesased as like a push-button
This method based on touchInterruptSetThresholdDirection() is only available for ESP32
*/
#include "Arduino.h"
int threshold = 40;
bool touchActive = false;
bool lastTouchActive = false;
bool testingLower = true;
void gotTouchEvent(){
if (lastTouchActive != testingLower) {
touchActive = !touchActive;
testingLower = !testingLower;
// Touch ISR will be inverted: Lower <--> Higher than the Threshold after ISR event is noticed
touchInterruptSetThresholdDirection(testingLower);
}
}
void setup() {
Serial.begin(115200);
delay(1000); // give me time to bring up serial monitor
Serial.println("ESP32 Touch Interrupt Test");
touchAttachInterrupt(T2, gotTouchEvent, threshold);
// Touch ISR will be activated when touchRead is lower than the Threshold
touchInterruptSetThresholdDirection(testingLower);
}
void loop(){
if(lastTouchActive != touchActive){
lastTouchActive = touchActive;
if (touchActive) {
Serial.println(" ---- Touch was Pressed");
} else {
Serial.println(" ---- Touch was Released");
}
}
Serial.printf("T2 pin2 = %d \n", touchRead(T2));
delay(125);
}