Skip to content

Touch Sensor IDF Refactoring #6194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
342 changes: 187 additions & 155 deletions cores/esp32/esp32-hal-touch.c

Large diffs are not rendered by default.

52 changes: 46 additions & 6 deletions cores/esp32/esp32-hal-touch.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,21 @@
extern "C" {
#endif

#include "soc/soc_caps.h"
#include "esp32-hal.h"

#if SOC_TOUCH_SENSOR_NUM > 0

#if !defined(SOC_TOUCH_VERSION_1) && !defined(SOC_TOUCH_VERSION_2)
#error Touch IDF driver Not supported!
#endif

#if SOC_TOUCH_VERSION_1 // ESP32
typedef uint16_t touch_value_t;
#elif SOC_TOUCH_VERSION_2 // ESP32S2 ESP32S3
typedef uint32_t touch_value_t;
#endif

/*
* Set cycles that measurement operation takes
* The result from touchRead, threshold and detection
Expand All @@ -40,17 +53,44 @@ void touchSetCycles(uint16_t measure, uint16_t sleep);
* You can use this method to chose a good threshold value
* to use as value for touchAttachInterrupt
* */
uint16_t touchRead(uint8_t pin);
touch_value_t touchRead(uint8_t pin);

/*
* Set function to be called if touch pad value falls
* below the given threshold. Use touchRead to determine
* a proper threshold between touched and untouched state
* Set function to be called if touch pad value falls (ESP32)
* below the given threshold / rises (ESP32-S2/S3) by given increment (threshold).
* Use touchRead to determine a proper threshold between touched and untouched state
* */
void touchAttachInterrupt(uint8_t pin, void (*userFunc)(void), uint16_t threshold);
void touchAttachInterrupt(uint8_t pin, void (*userFunc)(void), touch_value_t threshold);
void touchAttachInterruptArg(uint8_t pin, void (*userFunc)(void*), void *arg, touch_value_t threshold);
void touchDetachInterrupt(uint8_t pin);

/*
* Specific functions to ESP32
* Tells the driver if it shall activate the ISR if the sensor is Lower or Higher than the Threshold
* Default if Lower.
**/

#if SOC_TOUCH_VERSION_1 // Only for ESP32 SoC
void touchInterruptSetThresholdDirection(bool mustbeLower);
#endif


/*
* Specific functions to ESP32-S2 and ESP32-S3
* Returns true when the latest ISR status for the Touchpad is that it is touched (Active)
* and false when the Touchpad is untoouched (Inactive)
* This function can be used in conjunction with ISR User callback in order to take action
* as soon as the touchpad is touched and/or released
**/

#if SOC_TOUCH_VERSION_2 // Only for ESP32S2 and ESP32S3
// returns true if touch pad has been and continues pressed and false otherwise
bool touchInterruptGetLastStatus(uint8_t pin);
#endif

#endif // SOC_TOUCH_SENSOR_NUM > 0

#ifdef __cplusplus
}
#endif

#endif /* MAIN_ESP32_HAL_TOUCH_H_ */
Empty file.
Empty file.
Empty file.
46 changes: 46 additions & 0 deletions libraries/ESP32/examples/Touch/TouchButton/TouchButton.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*

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);
}
Empty file.
Empty file.
53 changes: 53 additions & 0 deletions libraries/ESP32/examples/Touch/TouchButtonV2/TouchButtonV2.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*

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 touchInterruptGetLastStatus() is only available for ESP32 S2 and S3
*/

#include "Arduino.h"

int threshold = 1500; // ESP32S2
bool touch1detected = false;
bool touch2detected = false;

void gotTouch1() {
touch1detected = true;
}

void gotTouch2() {
touch2detected = true;
}

void setup() {
Serial.begin(115200);
delay(1000); // give me time to bring up serial monitor

Serial.println("\n ESP32 Touch Interrupt Test\n");
touchAttachInterrupt(T1, gotTouch1, threshold);
touchAttachInterrupt(T2, gotTouch2, threshold);
}

void loop() {
static uint32_t count = 0;

if (touch1detected) {
touch1detected = false;
if (touchInterruptGetLastStatus(T1)) {
Serial.println(" --- T1 Touched");
} else {
Serial.println(" --- T1 Released");
}
}
if (touch2detected) {
touch2detected = false;
if (touchInterruptGetLastStatus(T2)) {
Serial.println(" --- T2 Touched");
} else {
Serial.println(" --- T2 Released");
}
}

delay(80);
}