Skip to content

Commit 6cab5b7

Browse files
committed
code review
1 parent c7ec9c5 commit 6cab5b7

File tree

2 files changed

+52
-56
lines changed

2 files changed

+52
-56
lines changed

cores/esp32/esp32-hal-touch.c

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
#include "soc/soc_caps.h"
1616

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+
1721
#if SOC_TOUCH_SENSOR_NUM > 0
1822
#include "driver/touch_sensor.h"
1923
#include "esp32-hal-touch.h"
@@ -22,8 +26,13 @@
2226
Internal Private Touch Data Structure and Functions
2327
*/
2428

29+
#if SOC_TOUCH_VERSION_1 // ESP32
2530
static uint16_t __touchSleepCycles = 0x1000;
2631
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
2736

2837
typedef void (*voidFuncPtr)(void);
2938
typedef void (*voidArgFuncPtr)(void *);
@@ -58,27 +67,25 @@ static void ARDUINO_ISR_ATTR __touchISR(void * arg)
5867
}
5968
}
6069
}
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+
}
8289
#endif
8390
}
8491

@@ -88,9 +95,7 @@ static void __touchSetCycles(uint16_t measure, uint16_t sleep)
8895
{
8996
__touchSleepCycles = sleep;
9097
__touchMeasureCycles = measure;
91-
#if SOC_TOUCH_VERSION_1 || SOC_TOUCH_VERSION_2 // ESP32 || ESP32S2, ESP32S3
9298
touch_pad_set_meas_time(sleep, measure);
93-
#endif
9499
}
95100

96101

@@ -129,15 +134,13 @@ static void __touchInit()
129134
goto err;
130135
}
131136
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
135138
err = touch_pad_init();
136139
if (err != ESP_OK) {
137140
goto err;
138141
}
139142
// 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);
141144
touch_pad_set_voltage(TOUCH_PAD_HIGH_VOLTAGE_THRESHOLD, TOUCH_PAD_LOW_VOLTAGE_THRESHOLD, TOUCH_PAD_ATTEN_VOLTAGE_THRESHOLD);
142145
touch_pad_set_idle_channel_connect(TOUCH_PAD_IDLE_CH_CONNECT_DEFAULT);
143146
touch_pad_denoise_t denoise = {
@@ -171,30 +174,21 @@ static void __touchInit()
171174
return;
172175
}
173176

174-
static uint32_t __touchRead(uint8_t pin)
177+
static touch_value_t __touchRead(uint8_t pin)
175178
{
176179
int8_t pad = digitalPinToTouchChannel(pin);
177180
if(pad < 0){
178181
return 0;
179182
}
180183
__touchInit();
181184

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;
191186
touch_pad_read_raw_data(pad, &touch_value);
192-
#endif
193187

194-
return (uint32_t) touch_value;
188+
return touch_value;
195189
}
196190

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)
198192
{
199193
int8_t pad = digitalPinToTouchChannel(pin);
200194
if(pad < 0){
@@ -214,22 +208,20 @@ static void __touchConfigInterrupt(uint8_t pin, void (*userFunc)(void), void *Ar
214208
}
215209

216210
#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
221213
touch_pad_set_thresh(pad, threshold);
222214
#endif
223215
}
224216

225217
// 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)
227219
{
228220
__touchConfigInterrupt(pin, userFunc, NULL, threshold, false);
229221
}
230222

231223
// 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)
233225
{
234226
__touchConfigInterrupt(pin, userFunc, args, threshold, true);
235227
}
@@ -245,17 +237,15 @@ static void __touchDettachInterrupt(uint8_t pin)
245237
External Public Touch API Functions
246238
*/
247239

248-
#if SOC_TOUCH_VERSION_1 // Only for ESP32 SoC
240+
#if SOC_TOUCH_VERSION_1 // Only for ESP32 SoC
249241
void touchInterruptSetThresholdDirection(bool mustbeLower) {
250242
if (mustbeLower) {
251243
touch_pad_set_trigger_mode(TOUCH_TRIGGER_BELOW);
252244
} else {
253245
touch_pad_set_trigger_mode(TOUCH_TRIGGER_ABOVE);
254246
}
255247
}
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
259249
// returns true if touch pad has been and continues pressed and false otherwise
260250
bool touchInterruptGetLastStatus(uint8_t pin) {
261251
int8_t pad = digitalPinToTouchChannel(pin);
@@ -267,9 +257,9 @@ bool touchInterruptGetLastStatus(uint8_t pin) {
267257
}
268258
#endif
269259

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")));
273263
extern void touchDetachInterrupt(uint8_t) __attribute__ ((weak, alias("__touchDettachInterrupt")));
274264
extern void touchSetCycles(uint16_t, uint16_t) __attribute__ ((weak, alias("__touchSetCycles")));
275265

cores/esp32/esp32-hal-touch.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ extern "C" {
2929
#if SOC_TOUCH_SENSOR_NUM > 0
3030
#include "esp32-hal.h"
3131

32+
#if SOC_TOUCH_VERSION_1 // ESP32
33+
typedef uint16_t touch_value_t;
34+
#elif SOC_TOUCH_VERSION_2 // ESP32S2 ESP32S3
35+
typedef uint32_t touch_value_t;
36+
#endif
37+
3238
/*
3339
* Set cycles that measurement operation takes
3440
* The result from touchRead, threshold and detection
@@ -43,15 +49,15 @@ void touchSetCycles(uint16_t measure, uint16_t sleep);
4349
* You can use this method to chose a good threshold value
4450
* to use as value for touchAttachInterrupt
4551
* */
46-
uint32_t touchRead(uint8_t pin);
52+
touch_value_t touchRead(uint8_t pin);
4753

4854
/*
4955
* Set function to be called if touch pad value falls (ESP32)
5056
* below the given threshold / rises (ESP32-S2/S3) by given increment (threshold).
5157
* Use touchRead to determine a proper threshold between touched and untouched state
5258
* */
53-
void touchAttachInterrupt(uint8_t pin, void (*userFunc)(void), uint32_t threshold);
54-
void touchAttachInterruptArg(uint8_t pin, void (*userFunc)(void*), void *arg, uint32_t threshold);
59+
void touchAttachInterrupt(uint8_t pin, void (*userFunc)(void), touch_value_t threshold);
60+
void touchAttachInterruptArg(uint8_t pin, void (*userFunc)(void*), void *arg, touch_value_t threshold);
5561
void touchDetachInterrupt(uint8_t pin);
5662

5763
/*

0 commit comments

Comments
 (0)