Skip to content

Fix ESP32 touch; rework common code a bit #6772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 deletions ports/espressif/common-hal/touchio/TouchIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,22 @@
#include "peripherals/touch.h"
#include "shared-bindings/microcontroller/Pin.h"

static uint16_t get_raw_reading(touchio_touchin_obj_t *self) {
#if defined(CONFIG_IDF_TARGET_ESP32)
uint16_t touch_value;
#else
uint32_t touch_value;
#endif;
touch_pad_read_raw_data(self->pin->touch_channel, &touch_value);
if (touch_value > UINT16_MAX) {
return UINT16_MAX;
}
return (uint16_t)touch_value;
}

void common_hal_touchio_touchin_construct(touchio_touchin_obj_t *self,
const mcu_pin_obj_t *pin) {
if (pin->touch_channel == TOUCH_PAD_MAX) {
if (pin->touch_channel == NO_TOUCH_CHANNEL) {
raise_ValueError_invalid_pin();
}
claim_pin(pin);

// initialize touchpad
peripherals_touch_init(pin->touch_channel);

// wait for touch data to reset
mp_hal_delay_ms(10);

// Set a "touched" threshold not too far above the initial value.
// For simple finger touch, the values may vary as much as a factor of two,
// but for touches using fruit or other objects, the difference is much less.

self->pin = pin;
self->threshold = get_raw_reading(self) + 100;
self->threshold = common_hal_touchio_touchin_get_raw_value(self) + 100;
}

bool common_hal_touchio_touchin_deinited(touchio_touchin_obj_t *self) {
Expand All @@ -77,11 +61,11 @@ void common_hal_touchio_touchin_deinit(touchio_touchin_obj_t *self) {
}

bool common_hal_touchio_touchin_get_value(touchio_touchin_obj_t *self) {
return get_raw_reading(self) > self->threshold;
return common_hal_touchio_touchin_get_raw_value(self) > self->threshold;
}

uint16_t common_hal_touchio_touchin_get_raw_value(touchio_touchin_obj_t *self) {
return get_raw_reading(self);
return peripherals_touch_read(self->pin->touch_channel);
}

uint16_t common_hal_touchio_touchin_get_threshold(touchio_touchin_obj_t *self) {
Expand Down
30 changes: 22 additions & 8 deletions ports/espressif/peripherals/touch.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void peripherals_touch_never_reset(const bool enable) {
void peripherals_touch_init(const touch_pad_t touchpad) {
if (!touch_inited) {
touch_pad_init();
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_SW);
}
// touch_pad_config() must be done before touch_pad_fsm_start() the first time.
// Otherwise the calibration is wrong and we get maximum raw values if there is
Expand All @@ -52,13 +52,27 @@ void peripherals_touch_init(const touch_pad_t touchpad) {
touch_pad_config(touchpad, 0);
#else
touch_pad_config(touchpad);
touch_pad_fsm_start();
#endif
if (!touch_inited) {
#if defined(CONFIG_IDF_TARGET_ESP32)
touch_pad_sw_start();
#else
touch_pad_fsm_start();
#endif
touch_inited = true;
touch_inited = true;
}

uint16_t peripherals_touch_read(touch_pad_t touchpad) {
#if defined(CONFIG_IDF_TARGET_ESP32)
uint16_t touch_value;
touch_pad_read(touchpad, &touch_value);
// ESP32 touch_pad_read() returns a lower value when a pin is touched instead of a higher value.
// Flip the values around to be consistent with TouchIn assumptions.
return UINT16_MAX - touch_value;
#else
uint32_t touch_value;
touch_pad_sw_start();
while (!touch_pad_meas_is_done()) {
}
touch_pad_read_raw_data(touchpad, &touch_value);
if (touch_value > UINT16_MAX) {
return UINT16_MAX;
}
return (uint16_t)touch_value;
#endif
}
1 change: 1 addition & 0 deletions ports/espressif/peripherals/touch.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include "driver/touch_pad.h"

extern uint16_t peripherals_touch_read(touch_pad_t touchpad);
extern void peripherals_touch_reset(void);
extern void peripherals_touch_never_reset(const bool enable);
extern void peripherals_touch_init(const touch_pad_t touchpad);
Expand Down