From 8eaf2b0c1933fa4d0bef2786647b0eb0a586f605 Mon Sep 17 00:00:00 2001 From: microDev <70126934+microDev1@users.noreply.github.com> Date: Fri, 18 Dec 2020 12:54:36 +0530 Subject: implement touch alarm --- locale/circuitpython.pot | 3 +- ports/esp32s2/common-hal/alarm/__init__.c | 18 ++++--- ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c | 49 +++++++++++++++++ ports/esp32s2/common-hal/alarm/touch/TouchAlarm.h | 45 ++++++++++++++++ py/circuitpy_defns.mk | 1 + shared-bindings/alarm/__init__.c | 17 +++++- shared-bindings/alarm/touch/TouchAlarm.c | 64 +++++++++++++++++++++++ shared-bindings/alarm/touch/TouchAlarm.h | 40 ++++++++++++++ 8 files changed, 229 insertions(+), 8 deletions(-) create mode 100644 ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c create mode 100644 ports/esp32s2/common-hal/alarm/touch/TouchAlarm.h create mode 100644 shared-bindings/alarm/touch/TouchAlarm.c create mode 100644 shared-bindings/alarm/touch/TouchAlarm.h diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index d64cff185..63407f52b 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: 2020-12-14 12:59-0500\n" +"POT-Creation-Date: 2020-12-18 12:42+0530\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -1144,6 +1144,7 @@ msgstr "" #: ports/atmel-samd/common-hal/audioio/AudioOut.c #: ports/atmel-samd/common-hal/touchio/TouchIn.c +#: ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c #: ports/esp32s2/common-hal/touchio/TouchIn.c shared-bindings/pwmio/PWMOut.c #: shared-module/rgbmatrix/RGBMatrix.c msgid "Invalid pin" diff --git a/ports/esp32s2/common-hal/alarm/__init__.c b/ports/esp32s2/common-hal/alarm/__init__.c index fae921db0..6fbad952f 100644 --- a/ports/esp32s2/common-hal/alarm/__init__.c +++ b/ports/esp32s2/common-hal/alarm/__init__.c @@ -29,11 +29,13 @@ #include "py/objtuple.h" #include "py/runtime.h" -#include "shared-bindings/alarm/pin/PinAlarm.h" #include "shared-bindings/alarm/SleepMemory.h" +#include "shared-bindings/alarm/pin/PinAlarm.h" #include "shared-bindings/alarm/time/TimeAlarm.h" -#include "shared-bindings/microcontroller/__init__.h" +#include "shared-bindings/alarm/touch/TouchAlarm.h" + #include "shared-bindings/wifi/__init__.h" +#include "shared-bindings/microcontroller/__init__.h" #include "supervisor/port.h" #include "supervisor/shared/workflow.h" @@ -49,10 +51,10 @@ const alarm_sleep_memory_obj_t alarm_sleep_memory_obj = { }, }; - void alarm_reset(void) { - alarm_time_timealarm_reset(); alarm_sleep_memory_reset(); + alarm_time_timealarm_reset(); + alarm_touch_touchalarm_reset(); esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL); } @@ -60,6 +62,9 @@ STATIC esp_sleep_wakeup_cause_t _get_wakeup_cause(void) { if (alarm_time_timealarm_woke_us_up()) { return ESP_SLEEP_WAKEUP_TIMER; } + if (alarm_touch_touchalarm_woke_us_up()) { + return ESP_SLEEP_WAKEUP_TOUCHPAD; + } return esp_sleep_get_wakeup_cause(); } @@ -80,8 +85,7 @@ STATIC mp_obj_t _get_wake_alarm(size_t n_alarms, const mp_obj_t *alarms) { } case ESP_SLEEP_WAKEUP_TOUCHPAD: - // TODO: implement TouchIO - // Wake up from touch on pad, esp_sleep_get_touchpad_wakeup_status() + return alarm_touch_touchalarm_get_wakeup_alarm(n_alarms, alarms); break; case ESP_SLEEP_WAKEUP_UNDEFINED: @@ -110,6 +114,8 @@ STATIC void _setup_sleep_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t } time_alarm = MP_OBJ_TO_PTR(alarms[i]); time_alarm_set = true; + } else if (MP_OBJ_IS_TYPE(alarms[i], &alarm_touch_touchalarm_type)) { + alarm_touch_touchalarm_set_alarm(MP_OBJ_TO_PTR(alarms[i])); } } diff --git a/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c b/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c new file mode 100644 index 000000000..b660e90c0 --- /dev/null +++ b/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c @@ -0,0 +1,49 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 microDev + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "shared-bindings/alarm/touch/TouchAlarm.h" + +#include "esp_sleep.h" + +void common_hal_alarm_touch_touchalarm_construct(alarm_touch_touchalarm_obj_t *self, const mcu_pin_obj_t *pin) { + +} + +mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t *alarms) { + return mp_const_none; +} + +void alarm_touch_touchalarm_set_alarm(alarm_touch_touchalarm_obj_t *self) { + +} + +bool alarm_touch_touchalarm_woke_us_up(void) { + return false; +} + +void alarm_touch_touchalarm_reset(void) { + +} diff --git a/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.h b/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.h new file mode 100644 index 000000000..11643945d --- /dev/null +++ b/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.h @@ -0,0 +1,45 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 microDev + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_COMMON_HAL_ALARM_TOUCH_TOUCHALARM_H +#define MICROPY_INCLUDED_COMMON_HAL_ALARM_TOUCH_TOUCHALARM_H + +#include "py/obj.h" +#include "common-hal/microcontroller/Pin.h" + +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t *pin; +} alarm_touch_touchalarm_obj_t; + +// Find the alarm object that caused us to wake up or create an equivalent one. +mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t *alarms); +// Check for the wake up alarm from pretend deep sleep. +void alarm_touch_touchalarm_set_alarm(alarm_touch_touchalarm_obj_t *self); +bool alarm_touch_touchalarm_woke_us_up(void); +void alarm_touch_touchalarm_reset(void); + +#endif // MICROPY_INCLUDED_COMMON_HAL_ALARM_TOUCH_TOUCHALARM_H diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 1eafce259..a36600d62 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -308,6 +308,7 @@ SRC_COMMON_HAL_ALL = \ alarm/__init__.c \ alarm/pin/PinAlarm.c \ alarm/time/TimeAlarm.c \ + alarm/touch/TouchAlarm.c \ analogio/AnalogIn.c \ analogio/AnalogOut.c \ analogio/__init__.c \ diff --git a/shared-bindings/alarm/__init__.c b/shared-bindings/alarm/__init__.c index 700fe020e..526f5453d 100644 --- a/shared-bindings/alarm/__init__.c +++ b/shared-bindings/alarm/__init__.c @@ -32,6 +32,7 @@ #include "shared-bindings/alarm/SleepMemory.h" #include "shared-bindings/alarm/pin/PinAlarm.h" #include "shared-bindings/alarm/time/TimeAlarm.h" +#include "shared-bindings/alarm/touch/TouchAlarm.h" #include "shared-bindings/supervisor/Runtime.h" #include "shared-bindings/time/__init__.h" #include "supervisor/shared/autoreload.h" @@ -72,7 +73,8 @@ void validate_objs_are_alarms(size_t n_args, const mp_obj_t *objs) { for (size_t i = 0; i < n_args; i++) { if (MP_OBJ_IS_TYPE(objs[i], &alarm_pin_pin_alarm_type) || - MP_OBJ_IS_TYPE(objs[i], &alarm_time_time_alarm_type)) { + MP_OBJ_IS_TYPE(objs[i], &alarm_time_time_alarm_type) || + MP_OBJ_IS_TYPE(objs[i], &alarm_touch_touchalarm_type)) { continue; } mp_raise_TypeError_varg(translate("Expected an alarm")); @@ -182,6 +184,18 @@ STATIC const mp_obj_module_t alarm_time_module = { .globals = (mp_obj_dict_t*)&alarm_time_globals, }; +STATIC const mp_map_elem_t alarm_touch_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_touch) }, + { MP_ROM_QSTR(MP_QSTR_TouchAlarm), MP_OBJ_FROM_PTR(&alarm_touch_touchalarm_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(alarm_touch_globals, alarm_touch_globals_table); + +STATIC const mp_obj_module_t alarm_touch_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&alarm_touch_globals, +}; + // The module table is mutable because .wake_alarm is a mutable attribute. STATIC mp_map_elem_t alarm_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_alarm) }, @@ -195,6 +209,7 @@ STATIC mp_map_elem_t alarm_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_pin), MP_OBJ_FROM_PTR(&alarm_pin_module) }, { MP_ROM_QSTR(MP_QSTR_time), MP_OBJ_FROM_PTR(&alarm_time_module) }, + { MP_ROM_QSTR(MP_QSTR_touch), MP_OBJ_FROM_PTR(&alarm_touch_module) }, { MP_ROM_QSTR(MP_QSTR_SleepMemory), MP_OBJ_FROM_PTR(&alarm_sleep_memory_type) }, { MP_ROM_QSTR(MP_QSTR_sleep_memory), MP_OBJ_FROM_PTR(&alarm_sleep_memory_obj) }, diff --git a/shared-bindings/alarm/touch/TouchAlarm.c b/shared-bindings/alarm/touch/TouchAlarm.c new file mode 100644 index 000000000..8ee1bc75a --- /dev/null +++ b/shared-bindings/alarm/touch/TouchAlarm.c @@ -0,0 +1,64 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 microDev + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "shared-bindings/alarm/touch/TouchAlarm.h" +#include "shared-bindings/board/__init__.h" + +//| class TouchAlarm: +//| """Trigger an alarm when touch is detected.""" +//| +//| def __init__(self, *pin: microcontroller.Pin) -> None: +//| """Create an alarm that will be triggered when the +//| given pin is touched. +//| +//| """ +//| ... +//| +STATIC mp_obj_t alarm_touch_touchalarm_make_new(const mp_obj_type_t *type, + mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + alarm_touch_touchalarm_obj_t *self = m_new_obj(alarm_touch_touchalarm_obj_t); + self->base.type = &alarm_touch_touchalarm_type; + + enum { ARG_pin }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, + }; + + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[ARG_pin].u_obj); + + common_hal_alarm_touch_touchalarm_construct(self, pin); + + return MP_OBJ_FROM_PTR(self); +} + +const mp_obj_type_t alarm_touch_touchalarm_type = { + { &mp_type_type }, + .name = MP_QSTR_TouchAlarm, + .make_new = alarm_touch_touchalarm_make_new, +}; diff --git a/shared-bindings/alarm/touch/TouchAlarm.h b/shared-bindings/alarm/touch/TouchAlarm.h new file mode 100644 index 000000000..1b70d4dae --- /dev/null +++ b/shared-bindings/alarm/touch/TouchAlarm.h @@ -0,0 +1,40 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 microDev + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TOUCH_TOUCHALARM_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TOUCH_TOUCHALARM_H + +#include "py/obj.h" +#include "py/runtime.h" + +#include "common-hal/microcontroller/Pin.h" +#include "common-hal/alarm/touch/TouchAlarm.h" + +extern const mp_obj_type_t alarm_touch_touchalarm_type; + +extern void common_hal_alarm_touch_touchalarm_construct(alarm_touch_touchalarm_obj_t *self, const mcu_pin_obj_t *pin); + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TOUCH_TOUCHALARM_H -- cgit v1.2.3 From a60fabdffa6b7a06e6a5ad2278fe225f086f704a Mon Sep 17 00:00:00 2001 From: microDev <70126934+microDev1@users.noreply.github.com> Date: Sat, 19 Dec 2020 12:56:34 +0530 Subject: add touch alarm support for esp32s2 --- ports/esp32s2/Makefile | 1 + ports/esp32s2/common-hal/alarm/__init__.c | 1 + ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c | 62 +++++++++++++++++++++-- ports/esp32s2/common-hal/alarm/touch/TouchAlarm.h | 1 + ports/esp32s2/common-hal/touchio/TouchIn.c | 22 ++------ ports/esp32s2/common-hal/touchio/TouchIn.h | 5 +- ports/esp32s2/peripherals/touch.c | 46 +++++++++++++++++ ports/esp32s2/peripherals/touch.h | 35 +++++++++++++ ports/esp32s2/supervisor/port.c | 4 +- 9 files changed, 150 insertions(+), 27 deletions(-) create mode 100644 ports/esp32s2/peripherals/touch.c create mode 100644 ports/esp32s2/peripherals/touch.h diff --git a/ports/esp32s2/Makefile b/ports/esp32s2/Makefile index 01f8f48a7..3291c7fac 100644 --- a/ports/esp32s2/Makefile +++ b/ports/esp32s2/Makefile @@ -192,6 +192,7 @@ SRC_C += \ lib/utils/sys_stdio_mphal.c \ lib/netutils/netutils.c \ peripherals/timer.c \ + peripherals/touch.c \ peripherals/pcnt.c \ peripherals/pins.c \ peripherals/rmt.c \ diff --git a/ports/esp32s2/common-hal/alarm/__init__.c b/ports/esp32s2/common-hal/alarm/__init__.c index 6fbad952f..bdac2edc6 100644 --- a/ports/esp32s2/common-hal/alarm/__init__.c +++ b/ports/esp32s2/common-hal/alarm/__init__.c @@ -162,6 +162,7 @@ void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *ala } void NORETURN alarm_enter_deep_sleep(void) { + alarm_touch_touchalarm_prepare_for_deep_sleep(); // The ESP-IDF caches the deep sleep settings and applies them before sleep. // We don't need to worry about resetting them in the interim. esp_deep_sleep_start(); diff --git a/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c b/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c index b660e90c0..56085c529 100644 --- a/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c +++ b/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c @@ -25,19 +25,75 @@ */ #include "shared-bindings/alarm/touch/TouchAlarm.h" +#include "shared-bindings/microcontroller/__init__.h" + +#include "peripherals/touch.h" #include "esp_sleep.h" 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) { + mp_raise_ValueError(translate("Invalid pin")); + } + self->pin = pin; } mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t *alarms) { - return mp_const_none; + // First, check to see if we match any given alarms. + for (size_t i = 0; i < n_alarms; i++) { + if (MP_OBJ_IS_TYPE(alarms[i], &alarm_touch_touchalarm_type)) { + return alarms[i]; + } + } + + gpio_num_t pin_number = esp_sleep_get_touchpad_wakeup_status(); + + alarm_touch_touchalarm_obj_t *alarm = m_new_obj(alarm_touch_touchalarm_obj_t); + alarm->base.type = &alarm_touch_touchalarm_type; + alarm->pin = NULL; + + // 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->number == pin_number) { + alarm->pin = mcu_pin_globals.map.table[i].value; + break; + } + } + + return alarm; } +static uint16_t sleep_touch_pin; + void alarm_touch_touchalarm_set_alarm(alarm_touch_touchalarm_obj_t *self) { + sleep_touch_pin |= 1 << self->pin->number; + esp_sleep_enable_touchpad_wakeup(); + esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON); +} + +static void configure_sleep_touch_pin(touch_pad_t touch_channel) { + // intialize touchpad + peripherals_touch_init(touch_channel); + + // configure touchpad for sleep + touch_pad_sleep_channel_enable(touch_channel, true); + touch_pad_sleep_channel_enable_proximity(touch_channel, false); + + // wait for touch data to reset + mp_hal_delay_ms(10); + uint32_t touch_value; + touch_pad_sleep_channel_read_smooth(touch_channel, &touch_value); + touch_pad_sleep_set_threshold(touch_channel, touch_value * 0.1); //10% +} + +void alarm_touch_touchalarm_prepare_for_deep_sleep(void) { + for (uint8_t i = 1; i <= 14; i++) { + if ((sleep_touch_pin & 1 << i) != 0) { + configure_sleep_touch_pin((touch_pad_t)i); + } + } } bool alarm_touch_touchalarm_woke_us_up(void) { @@ -45,5 +101,5 @@ bool alarm_touch_touchalarm_woke_us_up(void) { } void alarm_touch_touchalarm_reset(void) { - + sleep_touch_pin = 0; } diff --git a/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.h b/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.h index 11643945d..aa1c27fea 100644 --- a/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.h +++ b/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.h @@ -39,6 +39,7 @@ typedef struct { mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t *alarms); // Check for the wake up alarm from pretend deep sleep. void alarm_touch_touchalarm_set_alarm(alarm_touch_touchalarm_obj_t *self); +void alarm_touch_touchalarm_prepare_for_deep_sleep(void); bool alarm_touch_touchalarm_woke_us_up(void); void alarm_touch_touchalarm_reset(void); diff --git a/ports/esp32s2/common-hal/touchio/TouchIn.c b/ports/esp32s2/common-hal/touchio/TouchIn.c index b27cbcf86..afce8e72d 100644 --- a/ports/esp32s2/common-hal/touchio/TouchIn.c +++ b/ports/esp32s2/common-hal/touchio/TouchIn.c @@ -28,15 +28,7 @@ #include "py/runtime.h" #include "driver/touch_pad.h" - -bool touch_inited = false; - -void touchin_reset(void) { - if (touch_inited) { - touch_pad_deinit(); - touch_inited = false; - } -} +#include "peripherals/touch.h" static uint16_t get_raw_reading(touchio_touchin_obj_t *self) { uint32_t touch_value; @@ -54,16 +46,10 @@ void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self, } claim_pin(pin); - if (!touch_inited) { - touch_pad_init(); - touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER); - touch_pad_fsm_start(); - touch_inited = true; - } - - touch_pad_config((touch_pad_t)pin->touch_channel); + // initialize touchpad + peripherals_touch_init((touch_pad_t)pin->touch_channel); - // wait for "raw data" to reset + // wait for touch data to reset mp_hal_delay_ms(10); // Initial values for pins will vary, depending on what peripherals the pins diff --git a/ports/esp32s2/common-hal/touchio/TouchIn.h b/ports/esp32s2/common-hal/touchio/TouchIn.h index 91de20931..2f7c66d2b 100644 --- a/ports/esp32s2/common-hal/touchio/TouchIn.h +++ b/ports/esp32s2/common-hal/touchio/TouchIn.h @@ -27,9 +27,8 @@ #ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_TOUCHIO_TOUCHIN_H #define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_TOUCHIO_TOUCHIN_H -#include "common-hal/microcontroller/Pin.h" - #include "py/obj.h" +#include "common-hal/microcontroller/Pin.h" typedef struct { mp_obj_base_t base; @@ -37,6 +36,4 @@ typedef struct { uint16_t threshold; } touchio_touchin_obj_t; -void touchin_reset(void); - #endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_TOUCHIO_TOUCHIN_H diff --git a/ports/esp32s2/peripherals/touch.c b/ports/esp32s2/peripherals/touch.c new file mode 100644 index 000000000..3f496addd --- /dev/null +++ b/ports/esp32s2/peripherals/touch.c @@ -0,0 +1,46 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 microDev + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "peripherals/touch.h" + +static bool touch_inited = false; + +void peripherals_touch_reset(void) { + if (touch_inited) { + touch_pad_deinit(); + touch_inited = false; + } +} + +void peripherals_touch_init(touch_pad_t touchpad) { + if (!touch_inited) { + touch_pad_init(); + touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER); + touch_pad_fsm_start(); + touch_inited = true; + } + touch_pad_config(touchpad); +} diff --git a/ports/esp32s2/peripherals/touch.h b/ports/esp32s2/peripherals/touch.h new file mode 100644 index 000000000..d9e0bd5fc --- /dev/null +++ b/ports/esp32s2/peripherals/touch.h @@ -0,0 +1,35 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 microDev + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_ESP32S2_PERIPHERALS_TOUCH_HANDLER_H +#define MICROPY_INCLUDED_ESP32S2_PERIPHERALS_TOUCH_HANDLER_H + +#include "driver/touch_pad.h" + +extern void peripherals_touch_reset(void); +extern void peripherals_touch_init(touch_pad_t touchpad); + +#endif // MICROPY_INCLUDED_ESP32S2_PERIPHERALS_TOUCH_HANDLER_H diff --git a/ports/esp32s2/supervisor/port.c b/ports/esp32s2/supervisor/port.c index 7037b4f05..4dc985cfe 100644 --- a/ports/esp32s2/supervisor/port.c +++ b/ports/esp32s2/supervisor/port.c @@ -44,7 +44,6 @@ #include "common-hal/ps2io/Ps2.h" #include "common-hal/pulseio/PulseIn.h" #include "common-hal/pwmio/PWMOut.h" -#include "common-hal/touchio/TouchIn.h" #include "common-hal/watchdog/WatchDogTimer.h" #include "common-hal/wifi/__init__.h" #include "supervisor/memory.h" @@ -54,6 +53,7 @@ #include "peripherals/rmt.h" #include "peripherals/pcnt.h" #include "peripherals/timer.h" +#include "peripherals/touch.h" #include "components/esp_rom/include/esp_rom_uart.h" #include "components/heap/include/esp_heap_caps.h" #include "components/xtensa/include/esp_debug_helpers.h" @@ -164,7 +164,7 @@ void reset_port(void) { #endif #if CIRCUITPY_TOUCHIO_USE_NATIVE - touchin_reset(); + peripherals_touch_reset(); #endif #if CIRCUITPY_WATCHDOG -- cgit v1.2.3 From ecd7c0878eaf7038317c5c59c6040f588093fa69 Mon Sep 17 00:00:00 2001 From: microDev <70126934+microDev1@users.noreply.github.com> Date: Sat, 19 Dec 2020 20:54:36 +0530 Subject: expose wake pin parameter and more tweaks --- locale/circuitpython.pot | 10 +++++++- ports/esp32s2/common-hal/alarm/__init__.c | 12 ++++++++- ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c | 17 ++++--------- shared-bindings/alarm/touch/TouchAlarm.c | 31 +++++++++++++++++++++-- 4 files changed, 54 insertions(+), 16 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 63407f52b..593e955f2 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: 2020-12-18 12:42+0530\n" +"POT-Creation-Date: 2020-12-18 20:40+0530\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -1463,6 +1463,10 @@ msgstr "" msgid "Only one alarm.time alarm can be set." msgstr "" +#: ports/esp32s2/common-hal/alarm/__init__.c +msgid "Only one alarm.touch alarm can be set." +msgstr "" + #: shared-module/displayio/ColorConverter.c msgid "Only one color can be transparent at a time" msgstr "" @@ -1837,6 +1841,10 @@ msgstr "" msgid "Total data to write is larger than outgoing_packet_length" msgstr "" +#: ports/esp32s2/common-hal/alarm/__init__.c +msgid "TouchAlarm not available in light sleep" +msgstr "" + #: py/obj.c msgid "Traceback (most recent call last):\n" msgstr "" diff --git a/ports/esp32s2/common-hal/alarm/__init__.c b/ports/esp32s2/common-hal/alarm/__init__.c index bdac2edc6..79c245704 100644 --- a/ports/esp32s2/common-hal/alarm/__init__.c +++ b/ports/esp32s2/common-hal/alarm/__init__.c @@ -103,6 +103,7 @@ mp_obj_t common_hal_alarm_get_wake_alarm(void) { // Set up light sleep or deep sleep alarms. STATIC void _setup_sleep_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms) { bool time_alarm_set = false; + bool touch_alarm_set = false; alarm_time_time_alarm_obj_t *time_alarm = MP_OBJ_NULL; for (size_t i = 0; i < n_alarms; i++) { @@ -115,7 +116,16 @@ STATIC void _setup_sleep_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t time_alarm = MP_OBJ_TO_PTR(alarms[i]); time_alarm_set = true; } else if (MP_OBJ_IS_TYPE(alarms[i], &alarm_touch_touchalarm_type)) { - alarm_touch_touchalarm_set_alarm(MP_OBJ_TO_PTR(alarms[i])); + if (!touch_alarm_set) { + if (deep_sleep) { + alarm_touch_touchalarm_set_alarm(MP_OBJ_TO_PTR(alarms[i])); + touch_alarm_set = true; + } else { + mp_raise_NotImplementedError(translate("TouchAlarm not available in light sleep")); + } + } else { + mp_raise_ValueError(translate("Only one alarm.touch alarm can be set.")); + } } } diff --git a/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c b/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c index 56085c529..a2fda8e78 100644 --- a/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c +++ b/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c @@ -35,6 +35,7 @@ void common_hal_alarm_touch_touchalarm_construct(alarm_touch_touchalarm_obj_t *s if (pin->touch_channel == TOUCH_PAD_MAX) { mp_raise_ValueError(translate("Invalid pin")); } + claim_pin(pin); self->pin = pin; } @@ -64,15 +65,15 @@ mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t return alarm; } -static uint16_t sleep_touch_pin; +static touch_pad_t touch_channel = TOUCH_PAD_MAX; void alarm_touch_touchalarm_set_alarm(alarm_touch_touchalarm_obj_t *self) { - sleep_touch_pin |= 1 << self->pin->number; + touch_channel = (touch_pad_t)self->pin->number; esp_sleep_enable_touchpad_wakeup(); esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON); } -static void configure_sleep_touch_pin(touch_pad_t touch_channel) { +void alarm_touch_touchalarm_prepare_for_deep_sleep(void) { // intialize touchpad peripherals_touch_init(touch_channel); @@ -88,18 +89,10 @@ static void configure_sleep_touch_pin(touch_pad_t touch_channel) { touch_pad_sleep_set_threshold(touch_channel, touch_value * 0.1); //10% } -void alarm_touch_touchalarm_prepare_for_deep_sleep(void) { - for (uint8_t i = 1; i <= 14; i++) { - if ((sleep_touch_pin & 1 << i) != 0) { - configure_sleep_touch_pin((touch_pad_t)i); - } - } -} - bool alarm_touch_touchalarm_woke_us_up(void) { return false; } void alarm_touch_touchalarm_reset(void) { - sleep_touch_pin = 0; + touch_channel = TOUCH_PAD_MAX; } diff --git a/shared-bindings/alarm/touch/TouchAlarm.c b/shared-bindings/alarm/touch/TouchAlarm.c index 8ee1bc75a..4259b71bd 100644 --- a/shared-bindings/alarm/touch/TouchAlarm.c +++ b/shared-bindings/alarm/touch/TouchAlarm.c @@ -27,13 +27,18 @@ #include "shared-bindings/alarm/touch/TouchAlarm.h" #include "shared-bindings/board/__init__.h" +#include "py/objproperty.h" + //| class TouchAlarm: //| """Trigger an alarm when touch is detected.""" //| //| def __init__(self, *pin: microcontroller.Pin) -> None: -//| """Create an alarm that will be triggered when the -//| given pin is touched. +//| """Create an alarm that will be triggered when the given pin is touched. +//| The alarm is not active until it is passed to an `alarm`-enabling function, such as +//| `alarm.light_sleep_until_alarms()` or `alarm.exit_and_deep_sleep_until_alarms()`. //| +//| :param microcontroller.Pin pin: The pin to monitor. On some ports, the choice of pin +//| may be limited due to hardware restrictions, particularly for deep-sleep alarms. //| """ //| ... //| @@ -57,8 +62,30 @@ STATIC mp_obj_t alarm_touch_touchalarm_make_new(const mp_obj_type_t *type, return MP_OBJ_FROM_PTR(self); } +//| pin: microcontroller.Pin +//| """The trigger pin.""" +//| +STATIC mp_obj_t alarm_touch_touchalarm_obj_get_pin(mp_obj_t self_in) { + alarm_touch_touchalarm_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_FROM_PTR(self->pin); +} +MP_DEFINE_CONST_FUN_OBJ_1(alarm_touch_touchalarm_get_pin_obj, alarm_touch_touchalarm_obj_get_pin); + +const mp_obj_property_t alarm_touch_touchalarm_pin_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&alarm_touch_touchalarm_get_pin_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + +STATIC const mp_rom_map_elem_t alarm_touch_touchalarm_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_pin), MP_ROM_PTR(&alarm_touch_touchalarm_pin_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(alarm_touch_touchalarm_locals_dict, alarm_touch_touchalarm_locals_dict_table); + const mp_obj_type_t alarm_touch_touchalarm_type = { { &mp_type_type }, .name = MP_QSTR_TouchAlarm, .make_new = alarm_touch_touchalarm_make_new, + .locals_dict = (mp_obj_t)&alarm_touch_touchalarm_locals_dict, }; -- cgit v1.2.3 From c7f68022efa1306b0c7cb2fa47dcc84dbca2c605 Mon Sep 17 00:00:00 2001 From: microDev <70126934+microDev1@users.noreply.github.com> Date: Wed, 30 Dec 2020 22:11:22 +0530 Subject: add pretend-to-sleep functionality --- locale/circuitpython.pot | 6 +- ports/esp32s2/common-hal/alarm/__init__.c | 13 +--- ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c | 81 ++++++++++++++++++++--- ports/esp32s2/common-hal/alarm/touch/TouchAlarm.h | 4 +- ports/esp32s2/common-hal/touchio/TouchIn.c | 8 +-- ports/esp32s2/peripherals/pins.h | 2 +- ports/esp32s2/peripherals/touch.c | 11 ++- ports/esp32s2/peripherals/touch.h | 3 +- 8 files changed, 90 insertions(+), 38 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 593e955f2..b9cf5bdcf 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: 2020-12-18 20:40+0530\n" +"POT-Creation-Date: 2020-12-22 22:54+0530\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -1463,7 +1463,7 @@ msgstr "" msgid "Only one alarm.time alarm can be set." msgstr "" -#: ports/esp32s2/common-hal/alarm/__init__.c +#: ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c msgid "Only one alarm.touch alarm can be set." msgstr "" @@ -1841,7 +1841,7 @@ msgstr "" msgid "Total data to write is larger than outgoing_packet_length" msgstr "" -#: ports/esp32s2/common-hal/alarm/__init__.c +#: ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c msgid "TouchAlarm not available in light sleep" msgstr "" diff --git a/ports/esp32s2/common-hal/alarm/__init__.c b/ports/esp32s2/common-hal/alarm/__init__.c index 79c245704..9e5772eac 100644 --- a/ports/esp32s2/common-hal/alarm/__init__.c +++ b/ports/esp32s2/common-hal/alarm/__init__.c @@ -103,7 +103,6 @@ mp_obj_t common_hal_alarm_get_wake_alarm(void) { // Set up light sleep or deep sleep alarms. STATIC void _setup_sleep_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms) { bool time_alarm_set = false; - bool touch_alarm_set = false; alarm_time_time_alarm_obj_t *time_alarm = MP_OBJ_NULL; for (size_t i = 0; i < n_alarms; i++) { @@ -115,23 +114,13 @@ STATIC void _setup_sleep_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t } time_alarm = MP_OBJ_TO_PTR(alarms[i]); time_alarm_set = true; - } else if (MP_OBJ_IS_TYPE(alarms[i], &alarm_touch_touchalarm_type)) { - if (!touch_alarm_set) { - if (deep_sleep) { - alarm_touch_touchalarm_set_alarm(MP_OBJ_TO_PTR(alarms[i])); - touch_alarm_set = true; - } else { - mp_raise_NotImplementedError(translate("TouchAlarm not available in light sleep")); - } - } else { - mp_raise_ValueError(translate("Only one alarm.touch alarm can be set.")); - } } } if (time_alarm_set) { alarm_time_timealarm_set_alarm(time_alarm); } + alarm_touch_touchalarm_set_alarm(deep_sleep, n_alarms, alarms); } STATIC void _idle_until_alarm(void) { diff --git a/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c b/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c index a2fda8e78..f5497af47 100644 --- a/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c +++ b/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c @@ -27,9 +27,12 @@ #include "shared-bindings/alarm/touch/TouchAlarm.h" #include "shared-bindings/microcontroller/__init__.h" +#include "esp_sleep.h" #include "peripherals/touch.h" +#include "supervisor/esp_port.h" -#include "esp_sleep.h" +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) { @@ -39,7 +42,7 @@ void common_hal_alarm_touch_touchalarm_construct(alarm_touch_touchalarm_obj_t *s self->pin = pin; } -mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t *alarms) { +mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(const size_t n_alarms, const mp_obj_t *alarms) { // First, check to see if we match any given alarms. for (size_t i = 0; i < n_alarms; i++) { if (MP_OBJ_IS_TYPE(alarms[i], &alarm_touch_touchalarm_type)) { @@ -47,8 +50,6 @@ mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t } } - gpio_num_t pin_number = esp_sleep_get_touchpad_wakeup_status(); - alarm_touch_touchalarm_obj_t *alarm = m_new_obj(alarm_touch_touchalarm_obj_t); alarm->base.type = &alarm_touch_touchalarm_type; alarm->pin = NULL; @@ -56,7 +57,7 @@ mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t // 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->number == pin_number) { + if (pin_obj->touch_channel == touch_channel) { alarm->pin = mcu_pin_globals.map.table[i].value; break; } @@ -65,16 +66,67 @@ mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t return alarm; } -static touch_pad_t touch_channel = TOUCH_PAD_MAX; +// This is used to wake the main CircuitPython task. +void touch_interrupt(void *arg) { + (void) arg; + woke_up = true; + BaseType_t task_wakeup; + vTaskNotifyGiveFromISR(circuitpython_task, &task_wakeup); + if (task_wakeup) { + portYIELD_FROM_ISR(); + } +} -void alarm_touch_touchalarm_set_alarm(alarm_touch_touchalarm_obj_t *self) { - touch_channel = (touch_pad_t)self->pin->number; - esp_sleep_enable_touchpad_wakeup(); - esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON); +void alarm_touch_touchalarm_set_alarm(const bool deep_sleep, const size_t n_alarms, const mp_obj_t *alarms) { + bool touch_alarm_set = false; + alarm_touch_touchalarm_obj_t *touch_alarm = MP_OBJ_NULL; + + for (size_t i = 0; i < n_alarms; i++) { + if (MP_OBJ_IS_TYPE(alarms[i], &alarm_touch_touchalarm_type)) { + if (!touch_alarm_set) { + if (deep_sleep) { + touch_alarm = MP_OBJ_TO_PTR(alarms[i]); + touch_alarm_set = true; + } else { + mp_raise_NotImplementedError(translate("TouchAlarm not available in light sleep")); + } + } else { + mp_raise_ValueError(translate("Only one alarm.touch alarm can be set.")); + } + } + } + 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 + peripherals_touch_reset(); + peripherals_touch_never_reset(true); + peripherals_touch_init(touch_channel); + + // 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); } void alarm_touch_touchalarm_prepare_for_deep_sleep(void) { // intialize touchpad + peripherals_touch_never_reset(false); + peripherals_touch_reset(); peripherals_touch_init(touch_channel); // configure touchpad for sleep @@ -84,15 +136,22 @@ void alarm_touch_touchalarm_prepare_for_deep_sleep(void) { // wait for touch data to reset mp_hal_delay_ms(10); + // configure trigger threshold uint32_t touch_value; touch_pad_sleep_channel_read_smooth(touch_channel, &touch_value); touch_pad_sleep_set_threshold(touch_channel, touch_value * 0.1); //10% + + // enable touchpad wakeup + esp_sleep_enable_touchpad_wakeup(); + esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON); } bool alarm_touch_touchalarm_woke_us_up(void) { - return false; + return woke_up; } void alarm_touch_touchalarm_reset(void) { + woke_up = false; touch_channel = TOUCH_PAD_MAX; + peripherals_touch_never_reset(false); } diff --git a/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.h b/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.h index aa1c27fea..755a3977a 100644 --- a/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.h +++ b/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.h @@ -36,9 +36,9 @@ typedef struct { } alarm_touch_touchalarm_obj_t; // Find the alarm object that caused us to wake up or create an equivalent one. -mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t *alarms); +mp_obj_t alarm_touch_touchalarm_get_wakeup_alarm(const size_t n_alarms, const mp_obj_t *alarms); // Check for the wake up alarm from pretend deep sleep. -void alarm_touch_touchalarm_set_alarm(alarm_touch_touchalarm_obj_t *self); +void alarm_touch_touchalarm_set_alarm(const bool deep_sleep, const size_t n_alarms, const mp_obj_t *alarms); void alarm_touch_touchalarm_prepare_for_deep_sleep(void); bool alarm_touch_touchalarm_woke_us_up(void); void alarm_touch_touchalarm_reset(void); diff --git a/ports/esp32s2/common-hal/touchio/TouchIn.c b/ports/esp32s2/common-hal/touchio/TouchIn.c index afce8e72d..401b84d5c 100644 --- a/ports/esp32s2/common-hal/touchio/TouchIn.c +++ b/ports/esp32s2/common-hal/touchio/TouchIn.c @@ -27,12 +27,11 @@ #include "shared-bindings/touchio/TouchIn.h" #include "py/runtime.h" -#include "driver/touch_pad.h" #include "peripherals/touch.h" static uint16_t get_raw_reading(touchio_touchin_obj_t *self) { uint32_t touch_value; - touch_pad_read_raw_data((touch_pad_t)self->pin->touch_channel, &touch_value); + touch_pad_read_raw_data(self->pin->touch_channel, &touch_value); if (touch_value > UINT16_MAX) { return UINT16_MAX; } @@ -47,14 +46,11 @@ void common_hal_touchio_touchin_construct(touchio_touchin_obj_t* self, claim_pin(pin); // initialize touchpad - peripherals_touch_init((touch_pad_t)pin->touch_channel); + peripherals_touch_init(pin->touch_channel); // wait for touch data to reset mp_hal_delay_ms(10); - // Initial values for pins will vary, depending on what peripherals the pins - // share on-chip. - // 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. diff --git a/ports/esp32s2/peripherals/pins.h b/ports/esp32s2/peripherals/pins.h index c78eb8385..dc28385bd 100644 --- a/ports/esp32s2/peripherals/pins.h +++ b/ports/esp32s2/peripherals/pins.h @@ -44,7 +44,7 @@ typedef struct { gpio_num_t number; uint8_t adc_index:2; uint8_t adc_channel:6; - uint8_t touch_channel; + touch_pad_t touch_channel; } mcu_pin_obj_t; extern const mcu_pin_obj_t pin_GPIO0; diff --git a/ports/esp32s2/peripherals/touch.c b/ports/esp32s2/peripherals/touch.c index 3f496addd..6cf33b2bd 100644 --- a/ports/esp32s2/peripherals/touch.c +++ b/ports/esp32s2/peripherals/touch.c @@ -24,18 +24,25 @@ * THE SOFTWARE. */ +#include "components/soc/include/hal/gpio_types.h" +// above include fixes build error in idf@v4.2 #include "peripherals/touch.h" static bool touch_inited = false; +static bool touch_never_reset = false; void peripherals_touch_reset(void) { - if (touch_inited) { + if (touch_inited && !touch_never_reset) { touch_pad_deinit(); touch_inited = false; } } -void peripherals_touch_init(touch_pad_t touchpad) { +void peripherals_touch_never_reset(const bool enable) { + touch_never_reset = 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); diff --git a/ports/esp32s2/peripherals/touch.h b/ports/esp32s2/peripherals/touch.h index d9e0bd5fc..218045f39 100644 --- a/ports/esp32s2/peripherals/touch.h +++ b/ports/esp32s2/peripherals/touch.h @@ -30,6 +30,7 @@ #include "driver/touch_pad.h" extern void peripherals_touch_reset(void); -extern void peripherals_touch_init(touch_pad_t touchpad); +extern void peripherals_touch_never_reset(const bool enable); +extern void peripherals_touch_init(const touch_pad_t touchpad); #endif // MICROPY_INCLUDED_ESP32S2_PERIPHERALS_TOUCH_HANDLER_H -- cgit v1.2.3 From b83bdc540d64c647e9339a8368aef83bab1387f5 Mon Sep 17 00:00:00 2001 From: microDev <70126934+microDev1@users.noreply.github.com> Date: Thu, 31 Dec 2020 02:22:20 +0530 Subject: enable light-sleep functionality --- ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c b/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c index f5497af47..20553877c 100644 --- a/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c +++ b/ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c @@ -84,12 +84,8 @@ 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) { - if (deep_sleep) { - touch_alarm = MP_OBJ_TO_PTR(alarms[i]); - touch_alarm_set = true; - } else { - mp_raise_NotImplementedError(translate("TouchAlarm not available in light sleep")); - } + 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.")); } -- cgit v1.2.3 From 0bad6110d4f2c1b66560dccb2eaf54f563d8f5a8 Mon Sep 17 00:00:00 2001 From: microDev <70126934+microDev1@users.noreply.github.com> Date: Thu, 31 Dec 2020 02:44:20 +0530 Subject: update wake-alarm implementation --- main.c | 2 +- ports/esp32s2/common-hal/alarm/__init__.c | 2 +- shared-bindings/alarm/__init__.c | 10 +++------- shared-bindings/alarm/__init__.h | 2 +- 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/main.c b/main.c index 9c4055a28..91b6a062d 100755 --- a/main.c +++ b/main.c @@ -157,7 +157,7 @@ STATIC void start_mp(supervisor_allocation* heap) { #if CIRCUITPY_ALARM // Record which alarm woke us up, if any. An object may be created so the heap must be functional. - alarm_save_wakeup_alarm(); + alarm_save_wake_alarm(); // Reset alarm module only after we retrieved the wakeup alarm. alarm_reset(); #endif diff --git a/ports/esp32s2/common-hal/alarm/__init__.c b/ports/esp32s2/common-hal/alarm/__init__.c index 0b1a7f049..47764a03a 100644 --- a/ports/esp32s2/common-hal/alarm/__init__.c +++ b/ports/esp32s2/common-hal/alarm/__init__.c @@ -121,9 +121,9 @@ STATIC void _idle_until_alarm(void) { RUN_BACKGROUND_TASKS; // Allow ctrl-C interrupt. if (alarm_woken_from_sleep()) { + alarm_save_wake_alarm(); return; } - port_idle_until_interrupt(); } } diff --git a/shared-bindings/alarm/__init__.c b/shared-bindings/alarm/__init__.c index e8230a4ca..7023c70e5 100644 --- a/shared-bindings/alarm/__init__.c +++ b/shared-bindings/alarm/__init__.c @@ -227,21 +227,17 @@ mp_obj_t alarm_get_wake_alarm(void) { } } -STATIC void alarm_set_wake_alarm(mp_obj_t alarm) { +// Initialize .wake_alarm value. +void alarm_save_wake_alarm(void) { // Equivalent of: // alarm.wake_alarm = alarm mp_map_elem_t *elem = mp_map_lookup(&alarm_module_globals.map, MP_ROM_QSTR(MP_QSTR_wake_alarm), MP_MAP_LOOKUP); if (elem) { - elem->value = alarm; + elem->value = common_hal_alarm_get_wake_alarm(); } } -// Initialize .wake_alarm value. -void alarm_save_wakeup_alarm(void) { - alarm_set_wake_alarm(common_hal_alarm_get_wake_alarm()); -} - const mp_obj_module_t alarm_module = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t*)&alarm_module_globals, diff --git a/shared-bindings/alarm/__init__.h b/shared-bindings/alarm/__init__.h index 4c12e0e70..154f91e26 100644 --- a/shared-bindings/alarm/__init__.h +++ b/shared-bindings/alarm/__init__.h @@ -50,7 +50,7 @@ extern void common_hal_alarm_gc_collect(void); extern mp_obj_t common_hal_alarm_get_wake_alarm(void); // Used by wake-up code. -void alarm_save_wakeup_alarm(void); +void alarm_save_wake_alarm(void); // True if an alarm is alerting. This is most useful for pretend deep sleep. -- cgit v1.2.3