Skip to content

Commit 0ea485e

Browse files
authored
Touch Sensor IDF Refactoring (espressif#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 espressif#6095 Fix espressif#6034 Fix espressif#5799 Fix espressif#5745 Fix espressif#5527 Fix espressif#5493 Fix espressif#4321 Fix espressif#4044 Fix espressif#2722 Fix espressif#2625
1 parent 3a96fc0 commit 0ea485e

File tree

9 files changed

+332
-161
lines changed

9 files changed

+332
-161
lines changed

Diff for: cores/esp32/esp32-hal-touch.c

+187-155
Large diffs are not rendered by default.

Diff for: cores/esp32/esp32-hal-touch.h

+46-6
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,21 @@
2424
extern "C" {
2525
#endif
2626

27+
#include "soc/soc_caps.h"
2728
#include "esp32-hal.h"
2829

30+
#if SOC_TOUCH_SENSOR_NUM > 0
31+
32+
#if !defined(SOC_TOUCH_VERSION_1) && !defined(SOC_TOUCH_VERSION_2)
33+
#error Touch IDF driver Not supported!
34+
#endif
35+
36+
#if SOC_TOUCH_VERSION_1 // ESP32
37+
typedef uint16_t touch_value_t;
38+
#elif SOC_TOUCH_VERSION_2 // ESP32S2 ESP32S3
39+
typedef uint32_t touch_value_t;
40+
#endif
41+
2942
/*
3043
* Set cycles that measurement operation takes
3144
* The result from touchRead, threshold and detection
@@ -40,17 +53,44 @@ void touchSetCycles(uint16_t measure, uint16_t sleep);
4053
* You can use this method to chose a good threshold value
4154
* to use as value for touchAttachInterrupt
4255
* */
43-
uint16_t touchRead(uint8_t pin);
56+
touch_value_t touchRead(uint8_t pin);
4457

4558
/*
46-
* Set function to be called if touch pad value falls
47-
* below the given threshold. Use touchRead to determine
48-
* a proper threshold between touched and untouched state
59+
* Set function to be called if touch pad value falls (ESP32)
60+
* below the given threshold / rises (ESP32-S2/S3) by given increment (threshold).
61+
* Use touchRead to determine a proper threshold between touched and untouched state
4962
* */
50-
void touchAttachInterrupt(uint8_t pin, void (*userFunc)(void), uint16_t threshold);
63+
void touchAttachInterrupt(uint8_t pin, void (*userFunc)(void), touch_value_t threshold);
64+
void touchAttachInterruptArg(uint8_t pin, void (*userFunc)(void*), void *arg, touch_value_t threshold);
65+
void touchDetachInterrupt(uint8_t pin);
66+
67+
/*
68+
* Specific functions to ESP32
69+
* Tells the driver if it shall activate the ISR if the sensor is Lower or Higher than the Threshold
70+
* Default if Lower.
71+
**/
72+
73+
#if SOC_TOUCH_VERSION_1 // Only for ESP32 SoC
74+
void touchInterruptSetThresholdDirection(bool mustbeLower);
75+
#endif
76+
77+
78+
/*
79+
* Specific functions to ESP32-S2 and ESP32-S3
80+
* Returns true when the latest ISR status for the Touchpad is that it is touched (Active)
81+
* and false when the Touchpad is untoouched (Inactive)
82+
* This function can be used in conjunction with ISR User callback in order to take action
83+
* as soon as the touchpad is touched and/or released
84+
**/
85+
86+
#if SOC_TOUCH_VERSION_2 // Only for ESP32S2 and ESP32S3
87+
// returns true if touch pad has been and continues pressed and false otherwise
88+
bool touchInterruptGetLastStatus(uint8_t pin);
89+
#endif
90+
91+
#endif // SOC_TOUCH_SENSOR_NUM > 0
5192

5293
#ifdef __cplusplus
5394
}
5495
#endif
55-
5696
#endif /* MAIN_ESP32_HAL_TOUCH_H_ */

Diff for: libraries/ESP32/examples/Touch/TouchButton/.skip.esp32c3

Whitespace-only changes.

Diff for: libraries/ESP32/examples/Touch/TouchButton/.skip.esp32s2

Whitespace-only changes.

Diff for: libraries/ESP32/examples/Touch/TouchButton/.skip.esp32s3

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
3+
This is an example how to use Touch Intrrerupts
4+
The sketh will tell when it is touched and then relesased as like a push-button
5+
6+
This method based on touchInterruptSetThresholdDirection() is only available for ESP32
7+
*/
8+
9+
#include "Arduino.h"
10+
11+
int threshold = 40;
12+
bool touchActive = false;
13+
bool lastTouchActive = false;
14+
bool testingLower = true;
15+
16+
void gotTouchEvent(){
17+
if (lastTouchActive != testingLower) {
18+
touchActive = !touchActive;
19+
testingLower = !testingLower;
20+
// Touch ISR will be inverted: Lower <--> Higher than the Threshold after ISR event is noticed
21+
touchInterruptSetThresholdDirection(testingLower);
22+
}
23+
}
24+
25+
void setup() {
26+
Serial.begin(115200);
27+
delay(1000); // give me time to bring up serial monitor
28+
Serial.println("ESP32 Touch Interrupt Test");
29+
touchAttachInterrupt(T2, gotTouchEvent, threshold);
30+
31+
// Touch ISR will be activated when touchRead is lower than the Threshold
32+
touchInterruptSetThresholdDirection(testingLower);
33+
}
34+
35+
void loop(){
36+
if(lastTouchActive != touchActive){
37+
lastTouchActive = touchActive;
38+
if (touchActive) {
39+
Serial.println(" ---- Touch was Pressed");
40+
} else {
41+
Serial.println(" ---- Touch was Released");
42+
}
43+
}
44+
Serial.printf("T2 pin2 = %d \n", touchRead(T2));
45+
delay(125);
46+
}

Diff for: libraries/ESP32/examples/Touch/TouchButtonV2/.skip.esp32

Whitespace-only changes.

Diff for: libraries/ESP32/examples/Touch/TouchButtonV2/.skip.esp32c3

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
3+
This is an example how to use Touch Intrrerupts
4+
The sketh will tell when it is touched and then relesased as like a push-button
5+
6+
This method based on touchInterruptGetLastStatus() is only available for ESP32 S2 and S3
7+
*/
8+
9+
#include "Arduino.h"
10+
11+
int threshold = 1500; // ESP32S2
12+
bool touch1detected = false;
13+
bool touch2detected = false;
14+
15+
void gotTouch1() {
16+
touch1detected = true;
17+
}
18+
19+
void gotTouch2() {
20+
touch2detected = true;
21+
}
22+
23+
void setup() {
24+
Serial.begin(115200);
25+
delay(1000); // give me time to bring up serial monitor
26+
27+
Serial.println("\n ESP32 Touch Interrupt Test\n");
28+
touchAttachInterrupt(T1, gotTouch1, threshold);
29+
touchAttachInterrupt(T2, gotTouch2, threshold);
30+
}
31+
32+
void loop() {
33+
static uint32_t count = 0;
34+
35+
if (touch1detected) {
36+
touch1detected = false;
37+
if (touchInterruptGetLastStatus(T1)) {
38+
Serial.println(" --- T1 Touched");
39+
} else {
40+
Serial.println(" --- T1 Released");
41+
}
42+
}
43+
if (touch2detected) {
44+
touch2detected = false;
45+
if (touchInterruptGetLastStatus(T2)) {
46+
Serial.println(" --- T2 Touched");
47+
} else {
48+
Serial.println(" --- T2 Released");
49+
}
50+
}
51+
52+
delay(80);
53+
}

0 commit comments

Comments
 (0)