@@ -34,7 +34,9 @@ typedef struct {
3434 bool lastStatusIsPressed ;
3535} TouchInterruptHandle_t ;
3636
37- static TouchInterruptHandle_t __touchInterruptHandlers [SOC_TOUCH_SENSOR_NUM ] = { 0 ,};
37+ static TouchInterruptHandle_t __touchInterruptHandlers [SOC_TOUCH_SENSOR_NUM ] = {
38+ 0 ,
39+ };
3840
3941static uint8_t _sample_num = 1 ;
4042static uint32_t _div_num = 1 ;
@@ -52,7 +54,7 @@ static bool enabled = false;
5254static bool running = false;
5355static bool channels_initialized [SOC_TOUCH_SENSOR_NUM ] = {false};
5456
55- static touch_sensor_handle_t touch_sensor_handle = NULL ;
57+ static touch_sensor_handle_t touch_sensor_handle = NULL ;
5658static touch_channel_handle_t touch_channel_handle [SOC_TOUCH_SENSOR_NUM ] = {};
5759
5860// Active threshold to benchmark ratio. (i.e., touch will be activated when data >= benchmark * (1 + ratio))
@@ -61,7 +63,7 @@ static float s_thresh2bm_ratio = 0.015f; // 1.5% for all channels
6163static bool ARDUINO_ISR_ATTR __touchOnActiveISR (touch_sensor_handle_t sens_handle , const touch_active_event_data_t * event , void * user_ctx ) {
6264 uint8_t pad_num = (uint8_t )event -> chan_id ;
6365 __touchInterruptHandlers [pad_num ].lastStatusIsPressed = true;
64- if (__touchInterruptHandlers [pad_num ].fn ) {
66+ if (__touchInterruptHandlers [pad_num ].fn ) {
6567 // keeping backward compatibility with "void cb(void)" and with new "void cb(void *)"
6668 if (__touchInterruptHandlers [pad_num ].callWithArgs ) {
6769 ((voidArgFuncPtr )__touchInterruptHandlers [pad_num ].fn )(__touchInterruptHandlers [pad_num ].arg );
@@ -75,7 +77,7 @@ static bool ARDUINO_ISR_ATTR __touchOnActiveISR(touch_sensor_handle_t sens_handl
7577static bool ARDUINO_ISR_ATTR __touchOnInactiveISR (touch_sensor_handle_t sens_handle , const touch_inactive_event_data_t * event , void * user_ctx ) {
7678 uint8_t pad_num = (uint8_t )event -> chan_id ;
7779 __touchInterruptHandlers [pad_num ].lastStatusIsPressed = false;
78- if (__touchInterruptHandlers [pad_num ].fn ) {
80+ if (__touchInterruptHandlers [pad_num ].fn ) {
7981 // keeping backward compatibility with "void cb(void)" and with new "void cb(void *)"
8082 if (__touchInterruptHandlers [pad_num ].callWithArgs ) {
8183 ((voidArgFuncPtr )__touchInterruptHandlers [pad_num ].fn )(__touchInterruptHandlers [pad_num ].arg );
@@ -87,10 +89,10 @@ static bool ARDUINO_ISR_ATTR __touchOnInactiveISR(touch_sensor_handle_t sens_han
8789}
8890
8991bool touchStop () {
90- if (!running ) { // Already stopped
92+ if (!running ) { // Already stopped
9193 return true;
9294 }
93- if (touch_sensor_stop_continuous_scanning (touch_sensor_handle ) != ESP_OK ) {
95+ if (touch_sensor_stop_continuous_scanning (touch_sensor_handle ) != ESP_OK ) {
9496 log_e ("Touch sensor stop scanning failed!" );
9597 return false;
9698 }
@@ -99,10 +101,10 @@ bool touchStop() {
99101}
100102
101103bool touchDisable () {
102- if (!enabled ) { // Already disabled
104+ if (!enabled ) { // Already disabled
103105 return true;
104106 }
105- if (!running && (touch_sensor_disable (touch_sensor_handle ) != ESP_OK )) {
107+ if (!running && (touch_sensor_disable (touch_sensor_handle ) != ESP_OK )) {
106108 log_e ("Touch sensor still running or disable failed!" );
107109 return false;
108110 }
@@ -111,10 +113,10 @@ bool touchDisable() {
111113}
112114
113115bool touchStart () {
114- if (running ) { // Already running
116+ if (running ) { // Already running
115117 return true;
116118 }
117- if (enabled && (touch_sensor_start_continuous_scanning (touch_sensor_handle ) != ESP_OK )) {
119+ if (enabled && (touch_sensor_start_continuous_scanning (touch_sensor_handle ) != ESP_OK )) {
118120 log_e ("Touch sensor not enabled or failed to start continuous scanning failed!" );
119121 return false;
120122 }
@@ -123,10 +125,10 @@ bool touchStart() {
123125}
124126
125127bool touchEnable () {
126- if (enabled ) { // Already enabled
128+ if (enabled ) { // Already enabled
127129 return true;
128130 }
129- if (touch_sensor_enable (touch_sensor_handle ) != ESP_OK ) {
131+ if (touch_sensor_enable (touch_sensor_handle ) != ESP_OK ) {
130132 log_e ("Touch sensor enable failed!" );
131133 return false;
132134 }
@@ -135,33 +137,37 @@ bool touchEnable() {
135137}
136138
137139bool touchBenchmarkThreshold (uint8_t pad ) {
138- if (!touchEnable ()) {return false;}
140+ if (!touchEnable ()) {
141+ return false;
142+ }
139143
140144 /* Scan the enabled touch channels for several times, to make sure the initial channel data is stable */
141145 for (int i = 0 ; i < 3 ; i ++ ) {
142- if (touch_sensor_trigger_oneshot_scanning (touch_sensor_handle , 2000 ) != ESP_OK ) {
146+ if (touch_sensor_trigger_oneshot_scanning (touch_sensor_handle , 2000 ) != ESP_OK ) {
143147 log_e ("Touch sensor trigger oneshot scanning failed!" );
144148 return false;
145149 }
146150 }
147151
148152 /* Disable the touch channel to rollback the state */
149- if (!touchDisable ()) {return false;}
153+ if (!touchDisable ()) {
154+ return false;
155+ }
150156
151157 // Reconfigure passed pad with new threshold
152158 uint32_t benchmark [_sample_num ] = {};
153- if (touch_channel_read_data (touch_channel_handle [pad ], TOUCH_CHAN_DATA_TYPE_BENCHMARK , benchmark ) != ESP_OK ) {
159+ if (touch_channel_read_data (touch_channel_handle [pad ], TOUCH_CHAN_DATA_TYPE_BENCHMARK , benchmark ) != ESP_OK ) {
154160 log_e ("Touch channel read data failed!" );
155161 return false;
156162 }
157163 /* Calculate the proper active thresholds regarding the initial benchmark */
158164 touch_channel_config_t chan_cfg = {};
159165 for (int i = 0 ; i < _sample_num ; i ++ ) {
160166 chan_cfg .active_thresh [i ] = (uint32_t )(benchmark [i ] * s_thresh2bm_ratio );
161- log_v ("Configured [CH %d] sample %d: benchmark = %" PRIu32 ", threshold = %" PRIu32 "\t" , pad , i , benchmark [i ], chan_cfg .active_thresh [i ]);
167+ log_v ("Configured [CH %d] sample %d: benchmark = %" PRIu32 ", threshold = %" PRIu32 "\t" , pad , i , benchmark [i ], chan_cfg .active_thresh [i ]);
162168 }
163169 /* Update the channel configuration */
164- if (touch_sensor_reconfig_channel (touch_channel_handle [pad ], & chan_cfg ) != ESP_OK ) {
170+ if (touch_sensor_reconfig_channel (touch_channel_handle [pad ], & chan_cfg ) != ESP_OK ) {
165171 log_e ("Touch sensor threshold reconfig channel failed!" );
166172 return false;
167173 }
@@ -187,8 +193,6 @@ static bool touchDetachBus(void *pin) {
187193 return true;
188194}
189195
190-
191-
192196static void __touchInit () {
193197 if (initialized ) {
194198 return ;
@@ -199,9 +203,9 @@ static void __touchInit() {
199203 sample_cfg [0 ] = single_sample_cfg ;
200204
201205 /* Allocate new touch controller handle */
202- touch_sensor_config_t sens_cfg = {
206+ touch_sensor_config_t sens_cfg = {
203207 .power_on_wait_us = __touchSleepTime ,
204- .meas_interval_us = __touchMeasureTime ,
208+ .meas_interval_us = __touchMeasureTime ,
205209 .max_meas_time_us = 0 ,
206210 .output_mode = TOUCH_PAD_OUT_AS_CLOCK ,
207211 .sample_cfg_num = _sample_num ,
@@ -250,10 +254,10 @@ static void __touchChannelInit(int pad) {
250254 __touchInterruptHandlers [pad ].fn = NULL ;
251255
252256 touch_channel_config_t chan_cfg = {
253- .active_thresh = {1000 } // default threshold, will be updated after benchmark
257+ .active_thresh = {1000 } // default threshold, will be updated after benchmark
254258 };
255-
256- if (!touchStop () || !touchDisable ()) {
259+
260+ if (!touchStop () || !touchDisable ()) {
257261 log_e ("Touch sensor stop and disable failed!" );
258262 return ;
259263 }
@@ -272,7 +276,7 @@ static void __touchChannelInit(int pad) {
272276 channels_initialized [pad ] = true;
273277 used_pads ++ ;
274278
275- if (!touchEnable () || !touchStart ()) {
279+ if (!touchEnable () || !touchStart ()) {
276280 log_e ("Touch sensor enable and start failed!" );
277281 }
278282}
@@ -300,9 +304,9 @@ static touch_value_t __touchRead(uint8_t pin) {
300304
301305 uint32_t touch_read [_sample_num ] = {};
302306 touch_channel_read_data (touch_channel_handle [pad ], TOUCH_CHAN_DATA_TYPE_SMOOTH , touch_read );
303- touch_value_t touch_value = touch_read [0 ]; // only one sample configuration for now
307+ touch_value_t touch_value = touch_read [0 ]; // only one sample configuration for now
304308
305- return touch_value ;
309+ return touch_value ;
306310}
307311
308312static void __touchConfigInterrupt (uint8_t pin , void (* userFunc )(void ), void * Args , bool callWithArgs , touch_value_t threshold ) {
@@ -326,25 +330,25 @@ static void __touchConfigInterrupt(uint8_t pin, void (*userFunc)(void), void *Ar
326330 __touchInterruptHandlers [pad ].arg = Args ;
327331 }
328332
329- if (threshold != 0 ) {
330- if (!touchStop () || !touchDisable ()) {
331- log_e ("Touch sensor stop and disable failed!" );
332- return ;
333- }
333+ if (threshold != 0 ) {
334+ if (!touchStop () || !touchDisable ()) {
335+ log_e ("Touch sensor stop and disable failed!" );
336+ return ;
337+ }
334338
335- touch_channel_config_t chan_cfg = {};
336- for (int i = 0 ; i < _sample_num ; i ++ ) {
337- chan_cfg .active_thresh [i ] = threshold ;
338- }
339+ touch_channel_config_t chan_cfg = {};
340+ for (int i = 0 ; i < _sample_num ; i ++ ) {
341+ chan_cfg .active_thresh [i ] = threshold ;
342+ }
339343
340- if (touch_sensor_reconfig_channel (touch_channel_handle [pad ], & chan_cfg ) != ESP_OK ) {
341- log_e ("Touch sensor threshold reconfig channel failed!" );
342- }
344+ if (touch_sensor_reconfig_channel (touch_channel_handle [pad ], & chan_cfg ) != ESP_OK ) {
345+ log_e ("Touch sensor threshold reconfig channel failed!" );
346+ }
343347
344- if (!touchEnable () || !touchStart ()) {
345- log_e ("Touch sensor enable and start failed!" );
348+ if (!touchEnable () || !touchStart ()) {
349+ log_e ("Touch sensor enable and start failed!" );
350+ }
346351 }
347- }
348352}
349353
350354// it keeps backwards compatibility
@@ -394,36 +398,35 @@ void touchSleepWakeUpEnable(uint8_t pin, touch_value_t threshold) {
394398 }
395399
396400 log_v ("Touch sensor deep sleep wake-up configuration for pad %d with threshold %d" , pad , threshold );
397- if (!touchStop () || !touchDisable ()) {
401+ if (!touchStop () || !touchDisable ()) {
398402 log_e ("Touch sensor stop and disable failed!" );
399403 return ;
400404 }
401405
402406 touch_sleep_config_t deep_slp_cfg = {
403- .slp_wakeup_lvl = TOUCH_DEEP_SLEEP_WAKEUP ,
404- .deep_slp_chan = touch_channel_handle [pad ],
405- .deep_slp_thresh = {threshold },
406- .deep_slp_sens_cfg = NULL , // Use the original touch sensor configuration
407+ .slp_wakeup_lvl = TOUCH_DEEP_SLEEP_WAKEUP ,
408+ .deep_slp_chan = touch_channel_handle [pad ],
409+ .deep_slp_thresh = {threshold },
410+ .deep_slp_sens_cfg = NULL , // Use the original touch sensor configuration
407411 };
408412
409413 // Register the deep sleep wake-up
410- if (touch_sensor_config_sleep_wakeup (touch_sensor_handle , & deep_slp_cfg ) != ESP_OK ) {
411- log_e ("Touch sensor deep sleep wake-up failed!" );
412- return ;
414+ if (touch_sensor_config_sleep_wakeup (touch_sensor_handle , & deep_slp_cfg ) != ESP_OK ) {
415+ log_e ("Touch sensor deep sleep wake-up failed!" );
416+ return ;
413417 }
414418
415- if (!touchEnable () || !touchStart ()) {
419+ if (!touchEnable () || !touchStart ()) {
416420 log_e ("Touch sensor enable and start failed!" );
417421 }
418-
419422}
420423
421424void touchSetDefaultThreshold (float percentage ) {
422425 s_thresh2bm_ratio = (float )percentage / 100.0f ;
423426}
424427
425428void touchSetTiming (float measure , uint32_t sleep ) {
426- if (initialized ) {
429+ if (initialized ) {
427430 log_e ("Touch sensor already initialized. Cannot set cycles." );
428431 return ;
429432 }
@@ -432,7 +435,7 @@ void touchSetTiming(float measure, uint32_t sleep) {
432435}
433436
434437void touchSetConfig (uint32_t div_num , uint8_t coarse_freq_tune , uint8_t fine_freq_tune ) {
435- if (initialized ) {
438+ if (initialized ) {
436439 log_e ("Touch sensor already initialized. Cannot set configuration." );
437440 return ;
438441 }
0 commit comments