Skip to content

Touch change to init only selected GPIO. #6609

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 4 commits into from
Apr 26, 2022
Merged
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
51 changes: 34 additions & 17 deletions cores/esp32/esp32-hal-touch.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,6 @@ static void __touchInit()
if (err != ESP_OK) {
goto err;
}
// Initial no Threshold and setup
for (int i = 0; i < SOC_TOUCH_SENSOR_NUM; i++) {
__touchInterruptHandlers[i].fn = NULL;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe keep this portion to init the handlers?

touch_pad_config(i, SOC_TOUCH_PAD_THRESHOLD_MAX); // returns ESP_OK
}
// keep ISR activated - it can run all together (ISR + touchRead())
err = touch_pad_isr_register(__touchISR, NULL);
if (err != ESP_OK) {
Expand All @@ -148,18 +143,7 @@ static void __touchInit()
// Touch Sensor Timer initiated
touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER); // returns ESP_OK
touch_pad_fsm_start(); // returns ESP_OK

// Initial no Threshold and setup - TOUCH0 is internal denoise channel
for (int i = 1; i < SOC_TOUCH_SENSOR_NUM; i++) {
__touchInterruptHandlers[i].fn = NULL;
touch_pad_config(i); // returns ESP_OK
}
// keep ISR activated - it can run all together (ISR + touchRead())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any downside to init ISR here? Seems that you only needed to delete touch_pad_config(i); to resolve the issue.

Copy link
Member Author

@P-R-O-C-H-Y P-R-O-C-H-Y Apr 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For touch v2, the ISR needs to be inited after touch_pad_config(i), else it's not working. Thats why it got moved to the __touchChannelInit for SOC_TOUCH_VERSION_2 only.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok.

err = touch_pad_isr_register(__touchISR, NULL, TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE);
if (err != ESP_OK) {
goto err;
}
touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE); // returns ESP_OK
//ISR setup moved to __touchChannelInit
#endif

initialized = true;
Expand All @@ -170,13 +154,43 @@ static void __touchInit()
return;
}

static void __touchChannelInit(int pad)
{
static bool channels_initialized[SOC_TOUCH_SENSOR_NUM] = { false };
if(channels_initialized[pad]){
return;
}

#if SOC_TOUCH_VERSION_1 // ESP32
// Initial no Threshold and setup
__touchInterruptHandlers[pad].fn = NULL;
touch_pad_config(pad, SOC_TOUCH_PAD_THRESHOLD_MAX); // returns ESP_OK
#elif SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3
// Initial no Threshold and setup
__touchInterruptHandlers[pad].fn = NULL;
touch_pad_config(pad); // returns ESP_OK
// keep ISR activated - it can run all together (ISR + touchRead())
esp_err_t err = touch_pad_isr_register(__touchISR, NULL, TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE);
if (err != ESP_OK) {
log_e(" Touch sensor initialization error.");
return;
}
touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE); // returns ESP_OK
#endif

channels_initialized[pad] = true;
delay(20); //delay needed before reading from touch channel after config
}

static touch_value_t __touchRead(uint8_t pin)
{
int8_t pad = digitalPinToTouchChannel(pin);
if(pad < 0){
return 0;
}

__touchInit();
__touchChannelInit(pad);

touch_value_t touch_value;
touch_pad_read_raw_data(pad, &touch_value);
Expand All @@ -198,6 +212,9 @@ static void __touchConfigInterrupt(uint8_t pin, void (*userFunc)(void), void *Ar
} else {
// attach ISR User Call
__touchInit();
#if SOC_TOUCH_VERSION_2 // ESP32S2, ESP32S3
__touchChannelInit(pad);
#endif
__touchInterruptHandlers[pad].fn = userFunc;
__touchInterruptHandlers[pad].callWithArgs = callWithArgs;
__touchInterruptHandlers[pad].arg = Args;
Expand Down