From 5f448139e0c5972def8a8f138cd29afd1e9222f8 Mon Sep 17 00:00:00 2001 From: microDev <70126934+microDev1@users.noreply.github.com> Date: Mon, 11 Jan 2021 15:30:45 +0530 Subject: [PATCH] fixes and enhancement for touch alarm - prevent touch alarm initialization if not set - fix wake_alarm is set to touch alarm on autoreload - add ability to have multiple touch alarms in light sleep --- locale/circuitpython.pot | 10 +-- ports/esp32s2/common-hal/alarm/__init__.c | 4 +- .../common-hal/alarm/touch/TouchAlarm.c | 66 +++++++++++++------ 3 files changed, 53 insertions(+), 27 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index f319b82f8b..dc5396b8e7 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2021-01-04 12:55-0600\n" +"POT-Creation-Date: 2021-01-11 14:24+0530\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -1485,12 +1485,12 @@ msgid "" "%d bpp given" msgstr "" -#: ports/esp32s2/common-hal/alarm/time/TimeAlarm.c -msgid "Only one alarm.time alarm can be set." +#: ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c +msgid "Only one TouchAlarm can be set in deep sleep." msgstr "" -#: ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c -msgid "Only one alarm.touch alarm can be set." +#: ports/esp32s2/common-hal/alarm/time/TimeAlarm.c +msgid "Only one alarm.time alarm can be set." msgstr "" #: shared-module/displayio/ColorConverter.c diff --git a/ports/esp32s2/common-hal/alarm/__init__.c b/ports/esp32s2/common-hal/alarm/__init__.c index 47764a03a6..1cec5d4ae8 100644 --- a/ports/esp32s2/common-hal/alarm/__init__.c +++ b/ports/esp32s2/common-hal/alarm/__init__.c @@ -92,9 +92,9 @@ STATIC mp_obj_t _get_wake_alarm(size_t n_alarms, const mp_obj_t *alarms) { return alarm_pin_pinalarm_get_wakeup_alarm(n_alarms, alarms); } - case ESP_SLEEP_WAKEUP_TOUCHPAD: + case ESP_SLEEP_WAKEUP_TOUCHPAD: { return alarm_touch_touchalarm_get_wakeup_alarm(n_alarms, alarms); - break; + } case ESP_SLEEP_WAKEUP_UNDEFINED: default: diff --git a/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c b/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c index 20553877ca..3095bc9e8b 100644 --- a/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c +++ b/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c @@ -31,8 +31,8 @@ #include "peripherals/touch.h" #include "supervisor/esp_port.h" +static uint16_t touch_channel_mask; static volatile bool woke_up = false; -static touch_pad_t touch_channel = TOUCH_PAD_MAX; void common_hal_alarm_touch_touchalarm_construct(alarm_touch_touchalarm_obj_t *self, const mcu_pin_obj_t *pin) { if (pin->touch_channel == TOUCH_PAD_MAX) { @@ -50,14 +50,20 @@ mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(const size_t n_alarms, const mp } } + // Create TouchAlarm object. alarm_touch_touchalarm_obj_t *alarm = m_new_obj(alarm_touch_touchalarm_obj_t); alarm->base.type = &alarm_touch_touchalarm_type; alarm->pin = NULL; + touch_pad_t wake_channel = touch_pad_get_current_meas_channel(); + if (wake_channel == TOUCH_PAD_MAX) { + return alarm; + } + // Map the pin number back to a pin object. for (size_t i = 0; i < mcu_pin_globals.map.used; i++) { const mcu_pin_obj_t* pin_obj = MP_OBJ_TO_PTR(mcu_pin_globals.map.table[i].value); - if (pin_obj->touch_channel == touch_channel) { + if (pin_obj->touch_channel == wake_channel) { alarm->pin = mcu_pin_globals.map.table[i].value; break; } @@ -83,46 +89,66 @@ void alarm_touch_touchalarm_set_alarm(const bool deep_sleep, const size_t n_alar for (size_t i = 0; i < n_alarms; i++) { if (MP_OBJ_IS_TYPE(alarms[i], &alarm_touch_touchalarm_type)) { - if (!touch_alarm_set) { - touch_alarm = MP_OBJ_TO_PTR(alarms[i]); - touch_alarm_set = true; - } else { - mp_raise_ValueError(translate("Only one alarm.touch alarm can be set.")); + if (deep_sleep && touch_alarm_set) { + mp_raise_ValueError(translate("Only one TouchAlarm can be set in deep sleep.")); } + touch_alarm = MP_OBJ_TO_PTR(alarms[i]); + touch_channel_mask |= 1 << touch_alarm->pin->number; + touch_alarm_set = true; } } + if (!touch_alarm_set) { return; } - touch_channel = touch_alarm->pin->touch_channel; - // configure interrupt for pretend to deep sleep // this will be disabled if we actually deep sleep - // intialize touchpad + // reset touch peripheral peripherals_touch_reset(); peripherals_touch_never_reset(true); - peripherals_touch_init(touch_channel); - // wait for touch data to reset - mp_hal_delay_ms(10); + for (uint8_t i = 1; i <= 14; i++) { + if ((touch_channel_mask & 1 << i) != 0) { + touch_pad_t touch_channel = (touch_pad_t)i; + // intialize touchpad + peripherals_touch_init(touch_channel); - // configure trigger threshold - uint32_t touch_value; - touch_pad_read_benchmark(touch_channel, &touch_value); - touch_pad_set_thresh(touch_channel, touch_value * 0.1); //10% + // wait for touch data to reset + mp_hal_delay_ms(10); + + // configure trigger threshold + uint32_t touch_value; + touch_pad_read_benchmark(touch_channel, &touch_value); + touch_pad_set_thresh(touch_channel, touch_value * 0.1); //10% + } + } // configure touch interrupt touch_pad_timeout_set(true, SOC_TOUCH_PAD_THRESHOLD_MAX); touch_pad_isr_register(touch_interrupt, NULL, TOUCH_PAD_INTR_MASK_ALL); - touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE | TOUCH_PAD_INTR_MASK_TIMEOUT); + touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE); } void alarm_touch_touchalarm_prepare_for_deep_sleep(void) { - // intialize touchpad + if (!touch_channel_mask) { + return; + } + + touch_pad_t touch_channel = TOUCH_PAD_MAX; + for (uint8_t i = 1; i <= 14; i++) { + if ((touch_channel_mask & 1 << i) != 0) { + touch_channel = (touch_pad_t)i; + break; + } + } + + // reset touch peripheral peripherals_touch_never_reset(false); peripherals_touch_reset(); + + // intialize touchpad peripherals_touch_init(touch_channel); // configure touchpad for sleep @@ -148,6 +174,6 @@ bool alarm_touch_touchalarm_woke_us_up(void) { void alarm_touch_touchalarm_reset(void) { woke_up = false; - touch_channel = TOUCH_PAD_MAX; + touch_channel_mask = 0; peripherals_touch_never_reset(false); }