@@ -34,7 +34,9 @@ typedef struct {
34
34
bool lastStatusIsPressed ;
35
35
} TouchInterruptHandle_t ;
36
36
37
- static TouchInterruptHandle_t __touchInterruptHandlers [SOC_TOUCH_SENSOR_NUM ] = { 0 ,};
37
+ static TouchInterruptHandle_t __touchInterruptHandlers [SOC_TOUCH_SENSOR_NUM ] = {
38
+ 0 ,
39
+ };
38
40
39
41
static uint8_t _sample_num = 1 ;
40
42
static uint32_t _div_num = 1 ;
@@ -52,7 +54,7 @@ static bool enabled = false;
52
54
static bool running = false;
53
55
static bool channels_initialized [SOC_TOUCH_SENSOR_NUM ] = {false};
54
56
55
- static touch_sensor_handle_t touch_sensor_handle = NULL ;
57
+ static touch_sensor_handle_t touch_sensor_handle = NULL ;
56
58
static touch_channel_handle_t touch_channel_handle [SOC_TOUCH_SENSOR_NUM ] = {};
57
59
58
60
// 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
61
63
static bool ARDUINO_ISR_ATTR __touchOnActiveISR (touch_sensor_handle_t sens_handle , const touch_active_event_data_t * event , void * user_ctx ) {
62
64
uint8_t pad_num = (uint8_t )event -> chan_id ;
63
65
__touchInterruptHandlers [pad_num ].lastStatusIsPressed = true;
64
- if (__touchInterruptHandlers [pad_num ].fn ) {
66
+ if (__touchInterruptHandlers [pad_num ].fn ) {
65
67
// keeping backward compatibility with "void cb(void)" and with new "void cb(void *)"
66
68
if (__touchInterruptHandlers [pad_num ].callWithArgs ) {
67
69
((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
75
77
static bool ARDUINO_ISR_ATTR __touchOnInactiveISR (touch_sensor_handle_t sens_handle , const touch_inactive_event_data_t * event , void * user_ctx ) {
76
78
uint8_t pad_num = (uint8_t )event -> chan_id ;
77
79
__touchInterruptHandlers [pad_num ].lastStatusIsPressed = false;
78
- if (__touchInterruptHandlers [pad_num ].fn ) {
80
+ if (__touchInterruptHandlers [pad_num ].fn ) {
79
81
// keeping backward compatibility with "void cb(void)" and with new "void cb(void *)"
80
82
if (__touchInterruptHandlers [pad_num ].callWithArgs ) {
81
83
((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
87
89
}
88
90
89
91
bool touchStop () {
90
- if (!running ) { // Already stopped
92
+ if (!running ) { // Already stopped
91
93
return true;
92
94
}
93
- if (touch_sensor_stop_continuous_scanning (touch_sensor_handle ) != ESP_OK ) {
95
+ if (touch_sensor_stop_continuous_scanning (touch_sensor_handle ) != ESP_OK ) {
94
96
log_e ("Touch sensor stop scanning failed!" );
95
97
return false;
96
98
}
@@ -99,10 +101,10 @@ bool touchStop() {
99
101
}
100
102
101
103
bool touchDisable () {
102
- if (!enabled ) { // Already disabled
104
+ if (!enabled ) { // Already disabled
103
105
return true;
104
106
}
105
- if (!running && (touch_sensor_disable (touch_sensor_handle ) != ESP_OK )) {
107
+ if (!running && (touch_sensor_disable (touch_sensor_handle ) != ESP_OK )) {
106
108
log_e ("Touch sensor still running or disable failed!" );
107
109
return false;
108
110
}
@@ -111,10 +113,10 @@ bool touchDisable() {
111
113
}
112
114
113
115
bool touchStart () {
114
- if (running ) { // Already running
116
+ if (running ) { // Already running
115
117
return true;
116
118
}
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 )) {
118
120
log_e ("Touch sensor not enabled or failed to start continuous scanning failed!" );
119
121
return false;
120
122
}
@@ -123,10 +125,10 @@ bool touchStart() {
123
125
}
124
126
125
127
bool touchEnable () {
126
- if (enabled ) { // Already enabled
128
+ if (enabled ) { // Already enabled
127
129
return true;
128
130
}
129
- if (touch_sensor_enable (touch_sensor_handle ) != ESP_OK ) {
131
+ if (touch_sensor_enable (touch_sensor_handle ) != ESP_OK ) {
130
132
log_e ("Touch sensor enable failed!" );
131
133
return false;
132
134
}
@@ -135,33 +137,37 @@ bool touchEnable() {
135
137
}
136
138
137
139
bool touchBenchmarkThreshold (uint8_t pad ) {
138
- if (!touchEnable ()) {return false;}
140
+ if (!touchEnable ()) {
141
+ return false;
142
+ }
139
143
140
144
/* Scan the enabled touch channels for several times, to make sure the initial channel data is stable */
141
145
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 ) {
143
147
log_e ("Touch sensor trigger oneshot scanning failed!" );
144
148
return false;
145
149
}
146
150
}
147
151
148
152
/* Disable the touch channel to rollback the state */
149
- if (!touchDisable ()) {return false;}
153
+ if (!touchDisable ()) {
154
+ return false;
155
+ }
150
156
151
157
// Reconfigure passed pad with new threshold
152
158
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 ) {
154
160
log_e ("Touch channel read data failed!" );
155
161
return false;
156
162
}
157
163
/* Calculate the proper active thresholds regarding the initial benchmark */
158
164
touch_channel_config_t chan_cfg = {};
159
165
for (int i = 0 ; i < _sample_num ; i ++ ) {
160
166
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 ]);
162
168
}
163
169
/* 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 ) {
165
171
log_e ("Touch sensor threshold reconfig channel failed!" );
166
172
return false;
167
173
}
@@ -187,8 +193,6 @@ static bool touchDetachBus(void *pin) {
187
193
return true;
188
194
}
189
195
190
-
191
-
192
196
static void __touchInit () {
193
197
if (initialized ) {
194
198
return ;
@@ -199,9 +203,9 @@ static void __touchInit() {
199
203
sample_cfg [0 ] = single_sample_cfg ;
200
204
201
205
/* Allocate new touch controller handle */
202
- touch_sensor_config_t sens_cfg = {
206
+ touch_sensor_config_t sens_cfg = {
203
207
.power_on_wait_us = __touchSleepTime ,
204
- .meas_interval_us = __touchMeasureTime ,
208
+ .meas_interval_us = __touchMeasureTime ,
205
209
.max_meas_time_us = 0 ,
206
210
.output_mode = TOUCH_PAD_OUT_AS_CLOCK ,
207
211
.sample_cfg_num = _sample_num ,
@@ -250,10 +254,10 @@ static void __touchChannelInit(int pad) {
250
254
__touchInterruptHandlers [pad ].fn = NULL ;
251
255
252
256
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
254
258
};
255
-
256
- if (!touchStop () || !touchDisable ()) {
259
+
260
+ if (!touchStop () || !touchDisable ()) {
257
261
log_e ("Touch sensor stop and disable failed!" );
258
262
return ;
259
263
}
@@ -272,7 +276,7 @@ static void __touchChannelInit(int pad) {
272
276
channels_initialized [pad ] = true;
273
277
used_pads ++ ;
274
278
275
- if (!touchEnable () || !touchStart ()) {
279
+ if (!touchEnable () || !touchStart ()) {
276
280
log_e ("Touch sensor enable and start failed!" );
277
281
}
278
282
}
@@ -300,9 +304,9 @@ static touch_value_t __touchRead(uint8_t pin) {
300
304
301
305
uint32_t touch_read [_sample_num ] = {};
302
306
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
304
308
305
- return touch_value ;
309
+ return touch_value ;
306
310
}
307
311
308
312
static 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
326
330
__touchInterruptHandlers [pad ].arg = Args ;
327
331
}
328
332
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
+ }
334
338
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
+ }
339
343
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
+ }
343
347
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
+ }
346
351
}
347
- }
348
352
}
349
353
350
354
// it keeps backwards compatibility
@@ -394,36 +398,35 @@ void touchSleepWakeUpEnable(uint8_t pin, touch_value_t threshold) {
394
398
}
395
399
396
400
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 ()) {
398
402
log_e ("Touch sensor stop and disable failed!" );
399
403
return ;
400
404
}
401
405
402
406
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
407
411
};
408
412
409
413
// 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 ;
413
417
}
414
418
415
- if (!touchEnable () || !touchStart ()) {
419
+ if (!touchEnable () || !touchStart ()) {
416
420
log_e ("Touch sensor enable and start failed!" );
417
421
}
418
-
419
422
}
420
423
421
424
void touchSetDefaultThreshold (float percentage ) {
422
425
s_thresh2bm_ratio = (float )percentage / 100.0f ;
423
426
}
424
427
425
428
void touchSetTiming (float measure , uint32_t sleep ) {
426
- if (initialized ) {
429
+ if (initialized ) {
427
430
log_e ("Touch sensor already initialized. Cannot set cycles." );
428
431
return ;
429
432
}
@@ -432,7 +435,7 @@ void touchSetTiming(float measure, uint32_t sleep) {
432
435
}
433
436
434
437
void touchSetConfig (uint32_t div_num , uint8_t coarse_freq_tune , uint8_t fine_freq_tune ) {
435
- if (initialized ) {
438
+ if (initialized ) {
436
439
log_e ("Touch sensor already initialized. Cannot set configuration." );
437
440
return ;
438
441
}
0 commit comments