Skip to content

Commit 5b3c930

Browse files
authored
Merge pull request #3738 from microDev1/fix-touch
ESP32S2: Fix multiple touchpad don't work simultaneously
2 parents a975ef4 + e90cb3a commit 5b3c930

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

ports/esp32s2/common-hal/touchio/TouchIn.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,19 @@
2525
*/
2626

2727
#include "shared-bindings/touchio/TouchIn.h"
28-
#include "py/runtime.h"
2928

29+
#include "py/runtime.h"
3030
#include "driver/touch_pad.h"
3131

32+
bool touch_inited = false;
33+
34+
void touchin_reset(void) {
35+
if (touch_inited) {
36+
touch_pad_deinit();
37+
touch_inited = false;
38+
}
39+
}
40+
3241
static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
3342
uint32_t touch_value;
3443
touch_pad_read_raw_data((touch_pad_t)self->pin->touch_channel, &touch_value);
@@ -45,11 +54,14 @@ void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self,
4554
}
4655
claim_pin(pin);
4756

48-
touch_pad_init();
49-
touch_pad_config((touch_pad_t)pin->touch_channel);
57+
if (!touch_inited) {
58+
touch_pad_init();
59+
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
60+
touch_pad_fsm_start();
61+
touch_inited = true;
62+
}
5063

51-
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
52-
touch_pad_fsm_start();
64+
touch_pad_config((touch_pad_t)pin->touch_channel);
5365

5466
// wait for "raw data" to reset
5567
mp_hal_delay_ms(10);
@@ -73,14 +85,12 @@ void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t* self) {
7385
if (common_hal_touchio_touchin_deinited(self)) {
7486
return;
7587
}
76-
touch_pad_deinit();
7788
reset_pin_number(self->pin->touch_channel);
7889
self->pin = NULL;
7990
}
8091

8192
bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self) {
82-
uint16_t reading = get_raw_reading(self);
83-
return reading > self->threshold;
93+
return get_raw_reading(self) > self->threshold;
8494
}
8595

8696
uint16_t common_hal_touchio_touchin_get_raw_value(touchio_touchin_obj_t *self) {

ports/esp32s2/supervisor/port.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "common-hal/ps2io/Ps2.h"
4545
#include "common-hal/pulseio/PulseIn.h"
4646
#include "common-hal/pwmio/PWMOut.h"
47+
#include "common-hal/touchio/TouchIn.h"
4748
#include "common-hal/watchdog/WatchDogTimer.h"
4849
#include "common-hal/wifi/__init__.h"
4950
#include "supervisor/memory.h"
@@ -146,6 +147,10 @@ void reset_port(void) {
146147
rtc_reset();
147148
#endif
148149

150+
#if CIRCUITPY_TOUCHIO_USE_NATIVE
151+
touchin_reset();
152+
#endif
153+
149154
#if CIRCUITPY_WATCHDOG
150155
watchdog_reset();
151156
#endif

0 commit comments

Comments
 (0)