1717
1818#include "driver/touch_sensor.h"
1919#include "esp32-hal-touch.h"
20+ #include "esp32-hal-periman.h"
2021
2122/*
2223 Internal Private Touch Data Structure and Functions
@@ -44,6 +45,10 @@ typedef struct {
4445
4546static TouchInterruptHandle_t __touchInterruptHandlers [SOC_TOUCH_SENSOR_NUM ] = {0 ,};
4647
48+ static uint8_t used_pads = 0 ;
49+ static bool initialized = false;
50+ static bool channels_initialized [SOC_TOUCH_SENSOR_NUM ] = { false };
51+
4752static void ARDUINO_ISR_ATTR __touchISR (void * arg )
4853{
4954#if SOC_TOUCH_VERSION_1 // ESP32
@@ -99,11 +104,23 @@ static void __touchSetCycles(uint16_t measure, uint16_t sleep)
99104 touch_pad_set_measurement_interval (sleep );
100105}
101106
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+ }
103121
104122static void __touchInit ()
105123{
106- static bool initialized = false;
107124 if (initialized ){
108125 return ;
109126 }
@@ -163,8 +180,7 @@ static void __touchInit()
163180
164181static void __touchChannelInit (int pad )
165182{
166- static bool channels_initialized [SOC_TOUCH_SENSOR_NUM ] = { false };
167- if (channels_initialized [pad ]){
183+ if (channels_initialized [pad ]){
168184 return ;
169185 }
170186
@@ -186,6 +202,7 @@ static void __touchChannelInit(int pad)
186202#endif
187203
188204 channels_initialized [pad ] = true;
205+ used_pads ++ ;
189206 delay (20 ); //delay needed before reading from touch channel after config
190207}
191208
@@ -197,8 +214,19 @@ static touch_value_t __touchRead(uint8_t pin)
197214 return 0 ;
198215 }
199216
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+ }
202230
203231 touch_value_t touch_value ;
204232 touch_pad_read_raw_data (pad , & touch_value );
@@ -280,9 +308,17 @@ void touchSleepWakeUpEnable(uint8_t pin, touch_value_t threshold)
280308 log_e (" No touch pad on selected pin!" );
281309 return ;
282310 }
283- __touchInit ();
284- __touchChannelInit (pad );
285311
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+ }
286322 #if SOC_TOUCH_VERSION_1 // Only for ESP32 SoC
287323 touch_pad_set_thresh (pad , threshold );
288324
0 commit comments