14
14
15
15
#include "soc/soc_caps.h"
16
16
17
+ #if !defined(SOC_TOUCH_VERSION_1 ) && !defined(SOC_TOUCH_VERSION_2 )
18
+ #error CONFIG_IDF_TARGET is not supported for Touch IDF driver!
19
+ #endif
20
+
17
21
#if SOC_TOUCH_SENSOR_NUM > 0
18
22
#include "driver/touch_sensor.h"
19
23
#include "esp32-hal-touch.h"
22
26
Internal Private Touch Data Structure and Functions
23
27
*/
24
28
29
+ #if SOC_TOUCH_VERSION_1 // ESP32
25
30
static uint16_t __touchSleepCycles = 0x1000 ;
26
31
static uint16_t __touchMeasureCycles = 0x1000 ;
32
+ #elif SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3
33
+ static uint16_t __touchSleepCycles = TOUCH_PAD_SLEEP_CYCLE_DEFAULT ;
34
+ static uint16_t __touchMeasureCycles = TOUCH_PAD_MEASURE_CYCLE_DEFAULT ;
35
+ #endif
27
36
28
37
typedef void (* voidFuncPtr )(void );
29
38
typedef void (* voidArgFuncPtr )(void * );
@@ -58,27 +67,25 @@ static void ARDUINO_ISR_ATTR __touchISR(void * arg)
58
67
}
59
68
}
60
69
}
61
- #endif
62
-
63
- #if SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3
64
- touch_pad_intr_mask_t evt = touch_pad_read_intr_status_mask ();
65
- uint8_t pad_num = touch_pad_get_current_meas_channel ();
66
- if (evt & TOUCH_PAD_INTR_MASK_ACTIVE ) {
67
- // touch has been pressed / touched
68
- __touchInterruptHandlers [pad_num ].lastStatusIsPressed = true;
69
- }
70
- if (evt & TOUCH_PAD_INTR_MASK_INACTIVE ) {
71
- // touch has been released / untouched
72
- __touchInterruptHandlers [pad_num ].lastStatusIsPressed = false;
73
- }
74
- if (__touchInterruptHandlers [pad_num ].fn ){
75
- // keeping backward compatibility with "void cb(void)" and with new "void cb(vooid *)"
76
- if (__touchInterruptHandlers [pad_num ].callWithArgs ) {
77
- ((voidArgFuncPtr )__touchInterruptHandlers [pad_num ].fn )(__touchInterruptHandlers [pad_num ].arg );
78
- } else {
79
- __touchInterruptHandlers [pad_num ].fn ();
80
- }
81
- }
70
+ #elif SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3
71
+ touch_pad_intr_mask_t evt = touch_pad_read_intr_status_mask ();
72
+ uint8_t pad_num = touch_pad_get_current_meas_channel ();
73
+ if (evt & TOUCH_PAD_INTR_MASK_ACTIVE ) {
74
+ // touch has been pressed / touched
75
+ __touchInterruptHandlers [pad_num ].lastStatusIsPressed = true;
76
+ }
77
+ if (evt & TOUCH_PAD_INTR_MASK_INACTIVE ) {
78
+ // touch has been released / untouched
79
+ __touchInterruptHandlers [pad_num ].lastStatusIsPressed = false;
80
+ }
81
+ if (__touchInterruptHandlers [pad_num ].fn ){
82
+ // keeping backward compatibility with "void cb(void)" and with new "void cb(vooid *)"
83
+ if (__touchInterruptHandlers [pad_num ].callWithArgs ) {
84
+ ((voidArgFuncPtr )__touchInterruptHandlers [pad_num ].fn )(__touchInterruptHandlers [pad_num ].arg );
85
+ } else {
86
+ __touchInterruptHandlers [pad_num ].fn ();
87
+ }
88
+ }
82
89
#endif
83
90
}
84
91
@@ -88,9 +95,7 @@ static void __touchSetCycles(uint16_t measure, uint16_t sleep)
88
95
{
89
96
__touchSleepCycles = sleep ;
90
97
__touchMeasureCycles = measure ;
91
- #if SOC_TOUCH_VERSION_1 || SOC_TOUCH_VERSION_2 // ESP32 || ESP32S2, ESP32S3
92
98
touch_pad_set_meas_time (sleep , measure );
93
- #endif
94
99
}
95
100
96
101
@@ -129,15 +134,13 @@ static void __touchInit()
129
134
goto err ;
130
135
}
131
136
touch_pad_intr_enable (); // returns ESP_OK
132
- #endif
133
-
134
- #if SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3
137
+ #elif SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3
135
138
err = touch_pad_init ();
136
139
if (err != ESP_OK ) {
137
140
goto err ;
138
141
}
139
142
// the next lines will drive the touch reading values -- all os them return ESP_OK
140
- touch_pad_set_meas_time (TOUCH_PAD_SLEEP_CYCLE_DEFAULT , TOUCH_PAD_MEASURE_CYCLE_DEFAULT );
143
+ touch_pad_set_meas_time (__touchSleepCycles , __touchMeasureCycles );
141
144
touch_pad_set_voltage (TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD , TOUCH_PAD_LOW_VOLTAGE_THRESHOLD , TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD );
142
145
touch_pad_set_idle_channel_connect (TOUCH_PAD_IDLE_CH_CONNECT_DEFAULT );
143
146
touch_pad_denoise_t denoise = {
@@ -171,30 +174,21 @@ static void __touchInit()
171
174
return ;
172
175
}
173
176
174
- static uint32_t __touchRead (uint8_t pin )
177
+ static touch_value_t __touchRead (uint8_t pin )
175
178
{
176
179
int8_t pad = digitalPinToTouchChannel (pin );
177
180
if (pad < 0 ){
178
181
return 0 ;
179
182
}
180
183
__touchInit ();
181
184
182
- #if SOC_TOUCH_VERSION_1 // ESP32
183
- uint16_t touch_value ;
184
- #endif
185
-
186
- #if SOC_TOUCH_VERSION_2 // ESP32S2 ESP32S3
187
- uint32_t touch_value ;
188
- #endif
189
-
190
- #if SOC_TOUCH_VERSION_1 || SOC_TOUCH_VERSION_2 // ESP32 || ESP32S2, ESP32S3
185
+ touch_value_t touch_value ;
191
186
touch_pad_read_raw_data (pad , & touch_value );
192
- #endif
193
187
194
- return ( uint32_t ) touch_value ;
188
+ return touch_value ;
195
189
}
196
190
197
- static void __touchConfigInterrupt (uint8_t pin , void (* userFunc )(void ), void * Args , uint32_t threshold , bool callWithArgs )
191
+ static void __touchConfigInterrupt (uint8_t pin , void (* userFunc )(void ), void * Args , touch_value_t threshold , bool callWithArgs )
198
192
{
199
193
int8_t pad = digitalPinToTouchChannel (pin );
200
194
if (pad < 0 ){
@@ -214,22 +208,20 @@ static void __touchConfigInterrupt(uint8_t pin, void (*userFunc)(void), void *Ar
214
208
}
215
209
216
210
#if SOC_TOUCH_VERSION_1 // ESP32
217
- touch_pad_config (pad , (uint16_t ) threshold );
218
- #endif
219
-
220
- #if SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3
211
+ touch_pad_config (pad , threshold );
212
+ #elif SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3
221
213
touch_pad_set_thresh (pad , threshold );
222
214
#endif
223
215
}
224
216
225
217
// it keeps backwards compatibility
226
- static void __touchAttachInterrupt (uint8_t pin , void (* userFunc )(void ), uint32_t threshold )
218
+ static void __touchAttachInterrupt (uint8_t pin , void (* userFunc )(void ), touch_value_t threshold )
227
219
{
228
220
__touchConfigInterrupt (pin , userFunc , NULL , threshold , false);
229
221
}
230
222
231
223
// new additional version of the API with User Args
232
- static void __touchAttachArgsInterrupt (uint8_t pin , void (* userFunc )(void ), void * args , uint32_t threshold )
224
+ static void __touchAttachArgsInterrupt (uint8_t pin , void (* userFunc )(void ), void * args , touch_value_t threshold )
233
225
{
234
226
__touchConfigInterrupt (pin , userFunc , args , threshold , true);
235
227
}
@@ -245,17 +237,15 @@ static void __touchDettachInterrupt(uint8_t pin)
245
237
External Public Touch API Functions
246
238
*/
247
239
248
- #if SOC_TOUCH_VERSION_1 // Only for ESP32 SoC
240
+ #if SOC_TOUCH_VERSION_1 // Only for ESP32 SoC
249
241
void touchInterruptSetThresholdDirection (bool mustbeLower ) {
250
242
if (mustbeLower ) {
251
243
touch_pad_set_trigger_mode (TOUCH_TRIGGER_BELOW );
252
244
} else {
253
245
touch_pad_set_trigger_mode (TOUCH_TRIGGER_ABOVE );
254
246
}
255
247
}
256
- #endif
257
-
258
- #if SOC_TOUCH_VERSION_2 // Only for ESP32S2 and ESP32S3
248
+ #elif SOC_TOUCH_VERSION_2 // Only for ESP32S2 and ESP32S3
259
249
// returns true if touch pad has been and continues pressed and false otherwise
260
250
bool touchInterruptGetLastStatus (uint8_t pin ) {
261
251
int8_t pad = digitalPinToTouchChannel (pin );
@@ -267,9 +257,9 @@ bool touchInterruptGetLastStatus(uint8_t pin) {
267
257
}
268
258
#endif
269
259
270
- extern uint32_t touchRead (uint8_t ) __attribute__ ((weak , alias ("__touchRead" )));
271
- extern void touchAttachInterrupt (uint8_t , voidFuncPtr , uint32_t ) __attribute__ ((weak , alias ("__touchAttachInterrupt" )));
272
- extern void touchAttachInterruptArg (uint8_t , voidArgFuncPtr , void * , uint32_t ) __attribute__ ((weak , alias ("__touchAttachArgsInterrupt" )));
260
+ extern touch_value_t touchRead (uint8_t ) __attribute__ ((weak , alias ("__touchRead" )));
261
+ extern void touchAttachInterrupt (uint8_t , voidFuncPtr , touch_value_t ) __attribute__ ((weak , alias ("__touchAttachInterrupt" )));
262
+ extern void touchAttachInterruptArg (uint8_t , voidArgFuncPtr , void * , touch_value_t ) __attribute__ ((weak , alias ("__touchAttachArgsInterrupt" )));
273
263
extern void touchDetachInterrupt (uint8_t ) __attribute__ ((weak , alias ("__touchDettachInterrupt" )));
274
264
extern void touchSetCycles (uint16_t , uint16_t ) __attribute__ ((weak , alias ("__touchSetCycles" )));
275
265
0 commit comments