17
17
18
18
#include "driver/touch_sensor.h"
19
19
#include "esp32-hal-touch.h"
20
+ #include "esp32-hal-periman.h"
20
21
21
22
/*
22
23
Internal Private Touch Data Structure and Functions
@@ -44,6 +45,10 @@ typedef struct {
44
45
45
46
static TouchInterruptHandle_t __touchInterruptHandlers [SOC_TOUCH_SENSOR_NUM ] = {0 ,};
46
47
48
+ static uint8_t used_pads = 0 ;
49
+ static bool initialized = false;
50
+ static bool channels_initialized [SOC_TOUCH_SENSOR_NUM ] = { false };
51
+
47
52
static void ARDUINO_ISR_ATTR __touchISR (void * arg )
48
53
{
49
54
#if SOC_TOUCH_VERSION_1 // ESP32
@@ -99,11 +104,23 @@ static void __touchSetCycles(uint16_t measure, uint16_t sleep)
99
104
touch_pad_set_measurement_interval (sleep );
100
105
}
101
106
102
-
107
+ static bool touchDetachBus (void * pin ){
108
+ int8_t pad = digitalPinToTouchChannel ((int )(pin - 1 ));
109
+ channels_initialized [pad ] = false;
110
+ used_pads -- ;
111
+ if (used_pads == 0 ) {
112
+ if (touch_pad_deinit () != ESP_OK ) //deinit touch module, as no pads are used
113
+ {
114
+ log_e ("Touch module deinit failed!" );
115
+ return false;
116
+ }
117
+ initialized = false;
118
+ }
119
+ return true;
120
+ }
103
121
104
122
static void __touchInit ()
105
123
{
106
- static bool initialized = false;
107
124
if (initialized ){
108
125
return ;
109
126
}
@@ -163,8 +180,7 @@ static void __touchInit()
163
180
164
181
static void __touchChannelInit (int pad )
165
182
{
166
- static bool channels_initialized [SOC_TOUCH_SENSOR_NUM ] = { false };
167
- if (channels_initialized [pad ]){
183
+ if (channels_initialized [pad ]){
168
184
return ;
169
185
}
170
186
@@ -186,6 +202,7 @@ static void __touchChannelInit(int pad)
186
202
#endif
187
203
188
204
channels_initialized [pad ] = true;
205
+ used_pads ++ ;
189
206
delay (20 ); //delay needed before reading from touch channel after config
190
207
}
191
208
@@ -197,8 +214,19 @@ static touch_value_t __touchRead(uint8_t pin)
197
214
return 0 ;
198
215
}
199
216
200
- __touchInit ();
201
- __touchChannelInit (pad );
217
+ if (perimanGetPinBus (pin , ESP32_BUS_TYPE_TOUCH ) == NULL ){
218
+ perimanSetBusDeinit (ESP32_BUS_TYPE_TOUCH , touchDetachBus );
219
+ if (!perimanSetPinBus (pin , ESP32_BUS_TYPE_INIT , NULL )){
220
+ return 0 ;
221
+ }
222
+ __touchInit ();
223
+ __touchChannelInit (pad );
224
+
225
+ if (!perimanSetPinBus (pin , ESP32_BUS_TYPE_TOUCH , (void * )(pin + 1 ))){
226
+ touchDetachBus ((void * )(pin + 1 ));
227
+ return 0 ;
228
+ }
229
+ }
202
230
203
231
touch_value_t touch_value ;
204
232
touch_pad_read_raw_data (pad , & touch_value );
@@ -280,9 +308,17 @@ void touchSleepWakeUpEnable(uint8_t pin, touch_value_t threshold)
280
308
log_e (" No touch pad on selected pin!" );
281
309
return ;
282
310
}
283
- __touchInit ();
284
- __touchChannelInit (pad );
285
311
312
+ if (perimanGetPinBus (pin , ESP32_BUS_TYPE_TOUCH ) == NULL ){
313
+ perimanSetBusDeinit (ESP32_BUS_TYPE_TOUCH , touchDetachBus );
314
+ __touchInit ();
315
+ __touchChannelInit (pad );
316
+ if (!perimanSetPinBus (pin , ESP32_BUS_TYPE_TOUCH , (void * )(pin + 1 ))){
317
+ log_e ("Failed to set bus to Peripheral manager" );
318
+ touchDetachBus ((void * )(pin + 1 ));
319
+ return ;
320
+ }
321
+ }
286
322
#if SOC_TOUCH_VERSION_1 // Only for ESP32 SoC
287
323
touch_pad_set_thresh (pad , threshold );
288
324
0 commit comments