Skip to content

Commit 2bc8d73

Browse files
committed
Fix ESP32 touch; rework common code a bit
1 parent 3fbddfd commit 2bc8d73

File tree

3 files changed

+27
-28
lines changed

3 files changed

+27
-28
lines changed

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

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,38 +30,22 @@
3030
#include "peripherals/touch.h"
3131
#include "shared-bindings/microcontroller/Pin.h"
3232

33-
static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
34-
#if defined(CONFIG_IDF_TARGET_ESP32)
35-
uint16_t touch_value;
36-
#else
37-
uint32_t touch_value;
38-
#endif;
39-
touch_pad_read_raw_data(self->pin->touch_channel, &touch_value);
40-
if (touch_value > UINT16_MAX) {
41-
return UINT16_MAX;
42-
}
43-
return (uint16_t)touch_value;
44-
}
45-
4633
void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self,
4734
const mcu_pin_obj_t *pin) {
48-
if (pin->touch_channel == TOUCH_PAD_MAX) {
35+
if (pin->touch_channel == NO_TOUCH_CHANNEL) {
4936
raise_ValueError_invalid_pin();
5037
}
5138
claim_pin(pin);
5239

5340
// initialize touchpad
5441
peripherals_touch_init(pin->touch_channel);
5542

56-
// wait for touch data to reset
57-
mp_hal_delay_ms(10);
58-
5943
// Set a "touched" threshold not too far above the initial value.
6044
// For simple finger touch, the values may vary as much as a factor of two,
6145
// but for touches using fruit or other objects, the difference is much less.
6246

6347
self->pin = pin;
64-
self->threshold = get_raw_reading(self) + 100;
48+
self->threshold = common_hal_touchio_touchin_get_raw_value(self) + 100;
6549
}
6650

6751
bool common_hal_touchio_touchin_deinited(touchio_touchin_obj_t *self) {
@@ -77,11 +61,11 @@ void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t *self) {
7761
}
7862

7963
bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self) {
80-
return get_raw_reading(self) > self->threshold;
64+
return common_hal_touchio_touchin_get_raw_value(self) > self->threshold;
8165
}
8266

8367
uint16_t common_hal_touchio_touchin_get_raw_value(touchio_touchin_obj_t *self) {
84-
return get_raw_reading(self);
68+
return peripherals_touch_read(self->pin->touch_channel);
8569
}
8670

8771
uint16_t common_hal_touchio_touchin_get_threshold(touchio_touchin_obj_t *self) {

ports/espressif/peripherals/touch.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void peripherals_touch_never_reset(const bool enable) {
4343
void peripherals_touch_init(const touch_pad_t touchpad) {
4444
if (!touch_inited) {
4545
touch_pad_init();
46-
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
46+
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_SW);
4747
}
4848
// touch_pad_config() must be done before touch_pad_fsm_start() the first time.
4949
// Otherwise the calibration is wrong and we get maximum raw values if there is
@@ -52,13 +52,27 @@ void peripherals_touch_init(const touch_pad_t touchpad) {
5252
touch_pad_config(touchpad, 0);
5353
#else
5454
touch_pad_config(touchpad);
55+
touch_pad_fsm_start();
5556
#endif
56-
if (!touch_inited) {
57-
#if defined(CONFIG_IDF_TARGET_ESP32)
58-
touch_pad_sw_start();
59-
#else
60-
touch_pad_fsm_start();
61-
#endif
62-
touch_inited = true;
57+
touch_inited = true;
58+
}
59+
60+
uint16_t peripherals_touch_read(touch_pad_t touchpad) {
61+
#if defined(CONFIG_IDF_TARGET_ESP32)
62+
uint16_t touch_value;
63+
touch_pad_read(touchpad, &touch_value);
64+
// ESP32 touch_pad_read() returns a lower value when a pin is touched instead of a higher value.
65+
// Flip the values around to be consistent with TouchIn assumptions.
66+
return UINT16_MAX - touch_value;
67+
#else
68+
uint32_t touch_value;
69+
touch_pad_sw_start();
70+
while (!touch_pad_meas_is_done()) {
71+
}
72+
touch_pad_read_raw_data(touchpad, &touch_value);
73+
if (touch_value > UINT16_MAX) {
74+
return UINT16_MAX;
6375
}
76+
return (uint16_t)touch_value;
77+
#endif
6478
}

ports/espressif/peripherals/touch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "driver/touch_pad.h"
3131

32+
extern uint16_t peripherals_touch_read(touch_pad_t touchpad);
3233
extern void peripherals_touch_reset(void);
3334
extern void peripherals_touch_never_reset(const bool enable);
3435
extern void peripherals_touch_init(const touch_pad_t touchpad);

0 commit comments

Comments
 (0)