From 59df1a11ade4a39d079c9f418740b45b4d6121ce Mon Sep 17 00:00:00 2001 From: microDev <70126934+microDev1@users.noreply.github.com> Date: Fri, 25 Sep 2020 00:28:50 +0530 Subject: [PATCH] Add alarm_touch module --- ports/esp32s2/common-hal/alarm/__init__.c | 10 ++++---- ports/esp32s2/common-hal/alarm_io/__init__.c | 12 +++++----- .../esp32s2/common-hal/alarm_time/__init__.c | 4 ++-- .../esp32s2/common-hal/alarm_touch/__init__.c | 7 ++++++ .../common-hal/microcontroller/__init__.c | 2 +- ports/esp32s2/mpconfigport.mk | 1 + ports/esp32s2/supervisor/port.c | 2 +- py/circuitpy_defns.mk | 4 ++++ py/circuitpy_mpconfig.h | 8 +++++++ py/circuitpy_mpconfig.mk | 3 +++ shared-bindings/alarm_io/__init__.c | 10 ++++---- shared-bindings/alarm_time/__init__.c | 4 ++-- shared-bindings/alarm_touch/__init__.c | 24 +++++++++++++++++++ shared-bindings/alarm_touch/__init__.h | 14 +++++++++++ shared-bindings/microcontroller/__init__.c | 9 ------- 15 files changed, 83 insertions(+), 31 deletions(-) create mode 100644 ports/esp32s2/common-hal/alarm_touch/__init__.c create mode 100644 shared-bindings/alarm_touch/__init__.c create mode 100644 shared-bindings/alarm_touch/__init__.h diff --git a/ports/esp32s2/common-hal/alarm/__init__.c b/ports/esp32s2/common-hal/alarm/__init__.c index 89ff6865de..46715d9bf6 100644 --- a/ports/esp32s2/common-hal/alarm/__init__.c +++ b/ports/esp32s2/common-hal/alarm/__init__.c @@ -36,17 +36,17 @@ mp_obj_t common_hal_alarm_get_wake_alarm(void) { case ESP_SLEEP_WAKEUP_TIMER: ; //Wake up from timer. alarm_time_obj_t *timer = m_new_obj(alarm_time_obj_t); - timer->base.type = &alarm_time_type; + timer->base.type = &alarm_time_type; return timer; case ESP_SLEEP_WAKEUP_EXT0: ; //Wake up from GPIO alarm_io_obj_t *ext0 = m_new_obj(alarm_io_obj_t); - ext0->base.type = &alarm_io_type; - return ext0; - case ESP_SLEEP_WAKEUP_TOUCHPAD: + ext0->base.type = &alarm_io_type; + return ext0; + case ESP_SLEEP_WAKEUP_TOUCHPAD: //TODO: implement TouchIO //Wake up from touch on pad, esp_sleep_get_touchpad_wakeup_status() - break; + break; case ESP_SLEEP_WAKEUP_UNDEFINED: default: //Not a deep sleep reset diff --git a/ports/esp32s2/common-hal/alarm_io/__init__.c b/ports/esp32s2/common-hal/alarm_io/__init__.c index 9aa28f4156..a98b933246 100644 --- a/ports/esp32s2/common-hal/alarm_io/__init__.c +++ b/ports/esp32s2/common-hal/alarm_io/__init__.c @@ -5,22 +5,22 @@ mp_obj_t common_hal_alarm_io_pin_state (alarm_io_obj_t *self_in) { if (!rtc_gpio_is_valid_gpio(self_in->gpio)) { - mp_raise_ValueError(translate("io must be rtc io")); - } + mp_raise_ValueError(translate("io must be rtc io")); + } switch(esp_sleep_enable_ext0_wakeup(self_in->gpio, self_in->level)) { case ESP_ERR_INVALID_ARG: mp_raise_ValueError(translate("trigger level must be 0 or 1")); case ESP_ERR_INVALID_STATE: mp_raise_RuntimeError(translate("wakeup conflict")); - default: - break; + default: + break; } if (self_in->pull) { (self_in->level) ? rtc_gpio_pulldown_en(self_in->gpio) : rtc_gpio_pullup_en(self_in->gpio); } - return self_in; -} + return self_in; +} void common_hal_alarm_io_disable (void) { esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_EXT0 | ESP_SLEEP_WAKEUP_EXT1); diff --git a/ports/esp32s2/common-hal/alarm_time/__init__.c b/ports/esp32s2/common-hal/alarm_time/__init__.c index fb601e6be0..252b6e107c 100644 --- a/ports/esp32s2/common-hal/alarm_time/__init__.c +++ b/ports/esp32s2/common-hal/alarm_time/__init__.c @@ -5,8 +5,8 @@ void common_hal_alarm_time_duration (uint32_t ms) { if (esp_sleep_enable_timer_wakeup((ms) * 1000) == ESP_ERR_INVALID_ARG) { mp_raise_ValueError(translate("time out of range")); - } -} + } +} void common_hal_alarm_time_disable (void) { esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER); diff --git a/ports/esp32s2/common-hal/alarm_touch/__init__.c b/ports/esp32s2/common-hal/alarm_touch/__init__.c new file mode 100644 index 0000000000..df32b26930 --- /dev/null +++ b/ports/esp32s2/common-hal/alarm_touch/__init__.c @@ -0,0 +1,7 @@ +#include "esp_sleep.h" + +#include "shared-bindings/alarm_touch/__init__.h" + +void common_hal_alarm_touch_disable (void) { + esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TOUCHPAD); +} diff --git a/ports/esp32s2/common-hal/microcontroller/__init__.c b/ports/esp32s2/common-hal/microcontroller/__init__.c index ba24e1c48d..f1dc24bb89 100644 --- a/ports/esp32s2/common-hal/microcontroller/__init__.c +++ b/ports/esp32s2/common-hal/microcontroller/__init__.c @@ -80,7 +80,7 @@ void common_hal_mcu_reset(void) { } void common_hal_mcu_sleep(void) { - esp_deep_sleep_start(); + esp_deep_sleep_start(); } // The singleton microcontroller.Processor object, bound to microcontroller.cpu diff --git a/ports/esp32s2/mpconfigport.mk b/ports/esp32s2/mpconfigport.mk index 1a78821d32..7024ceb630 100644 --- a/ports/esp32s2/mpconfigport.mk +++ b/ports/esp32s2/mpconfigport.mk @@ -17,6 +17,7 @@ CIRCUITPY_FULL_BUILD = 1 CIRCUITPY_ALARM = 1 CIRCUITPY_ALARM_IO = 1 CIRCUITPY_ALARM_TIME = 1 +CIRCUITPY_ALARM_TOUCH = 1 CIRCUITPY_AUDIOBUSIO = 0 CIRCUITPY_AUDIOIO = 0 CIRCUITPY_CANIO = 1 diff --git a/ports/esp32s2/supervisor/port.c b/ports/esp32s2/supervisor/port.c index 840b979632..df6755783d 100644 --- a/ports/esp32s2/supervisor/port.c +++ b/ports/esp32s2/supervisor/port.c @@ -86,7 +86,7 @@ safe_mode_t port_init(void) { if (heap == NULL) { return NO_HEAP; } - + return NO_SAFE_MODE; } diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 473536d530..72091decbf 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -108,6 +108,9 @@ endif ifeq ($(CIRCUITPY_ALARM_TIME),1) SRC_PATTERNS += alarm_time/% endif +ifeq ($(CIRCUITPY_ALARM_TIME),1) +SRC_PATTERNS += alarm_touch/% +endif ifeq ($(CIRCUITPY_ANALOGIO),1) SRC_PATTERNS += analogio/% endif @@ -310,6 +313,7 @@ SRC_COMMON_HAL_ALL = \ alarm/__init__.c \ alarm_io/__init__.c \ alarm_time/__init__.c \ + alarm_touch/__init__.c \ analogio/AnalogIn.c \ analogio/AnalogOut.c \ analogio/__init__.c \ diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index 778c3131b6..c8141a99a6 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -261,6 +261,13 @@ extern const struct _mp_obj_module_t alarm_time_module; #define ALARM_TIME_MODULE #endif +#if CIRCUITPY_ALARM_TOUCH +extern const struct _mp_obj_module_t alarm_touch_module; +#define ALARM_TOUCH_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_alarm_touch), (mp_obj_t)&alarm_touch_module }, +#else +#define ALARM_TOUCH_MODULE +#endif + #if CIRCUITPY_ANALOGIO #define ANALOGIO_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_analogio), (mp_obj_t)&analogio_module }, extern const struct _mp_obj_module_t analogio_module; @@ -787,6 +794,7 @@ extern const struct _mp_obj_module_t wifi_module; ALARM_MODULE \ ALARM_IO_MODULE \ ALARM_TIME_MODULE \ + ALARM_TOUCH_MODULE \ ANALOGIO_MODULE \ AUDIOBUSIO_MODULE \ AUDIOCORE_MODULE \ diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index c1790e5e35..59d23e4667 100644 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -48,6 +48,9 @@ CFLAGS += -DCIRCUITPY_ALARM_IO=$(CIRCUITPY_ALARM_IO) CIRCUITPY_ALARM_TIME ?= 0 CFLAGS += -DCIRCUITPY_ALARM_TIME=$(CIRCUITPY_ALARM_TIME) +CIRCUITPY_ALARM_TOUCH ?= 0 +CFLAGS += -DCIRCUITPY_ALARM_TOUCH=$(CIRCUITPY_ALARM_TOUCH) + CIRCUITPY_ANALOGIO ?= 1 CFLAGS += -DCIRCUITPY_ANALOGIO=$(CIRCUITPY_ANALOGIO) diff --git a/shared-bindings/alarm_io/__init__.c b/shared-bindings/alarm_io/__init__.c index d772ccb796..dbe4671763 100644 --- a/shared-bindings/alarm_io/__init__.c +++ b/shared-bindings/alarm_io/__init__.c @@ -3,15 +3,15 @@ #include "shared-bindings/alarm_io/__init__.h" #include "shared-bindings/microcontroller/Pin.h" -STATIC mp_obj_t alarm_io_pin_state(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +STATIC mp_obj_t alarm_io_pin_state(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_level, ARG_pull }; static const mp_arg_t allowed_args[] = { { MP_QSTR_level, MP_ARG_INT | MP_ARG_KW_ONLY | MP_ARG_REQUIRED }, { MP_QSTR_pull, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_bool = false} }, - }; + }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mcu_pin_obj_t *pin = validate_obj_is_pin(pos_args[0]); alarm_io_obj_t *self = m_new_obj(alarm_io_obj_t); @@ -20,12 +20,12 @@ STATIC mp_obj_t alarm_io_pin_state(size_t n_args, const mp_obj_t *pos_args, mp_m self->gpio = pin->number; self->level = args[ARG_level].u_int; self->pull = args[ARG_pull].u_bool; - + return common_hal_alarm_io_pin_state(self); } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(alarm_io_pin_state_obj, 1, alarm_io_pin_state); -STATIC mp_obj_t alarm_io_disable(void) { +STATIC mp_obj_t alarm_io_disable(void) { common_hal_alarm_io_disable(); return mp_const_none; } diff --git a/shared-bindings/alarm_time/__init__.c b/shared-bindings/alarm_time/__init__.c index d4474ea2de..218ac68575 100644 --- a/shared-bindings/alarm_time/__init__.c +++ b/shared-bindings/alarm_time/__init__.c @@ -1,7 +1,7 @@ #include "py/obj.h" #include "shared-bindings/alarm_time/__init__.h" -STATIC mp_obj_t alarm_time_duration(mp_obj_t seconds_o) { +STATIC mp_obj_t alarm_time_duration(mp_obj_t seconds_o) { #if MICROPY_PY_BUILTINS_FLOAT mp_float_t seconds = mp_obj_get_float(seconds_o); mp_float_t msecs = 1000.0f * seconds + 0.5f; @@ -22,7 +22,7 @@ STATIC mp_obj_t alarm_time_duration(mp_obj_t seconds_o) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(alarm_time_duration_obj, alarm_time_duration); -STATIC mp_obj_t alarm_time_disable(void) { +STATIC mp_obj_t alarm_time_disable(void) { common_hal_alarm_time_disable(); return mp_const_none; } diff --git a/shared-bindings/alarm_touch/__init__.c b/shared-bindings/alarm_touch/__init__.c new file mode 100644 index 0000000000..e36fa73cb8 --- /dev/null +++ b/shared-bindings/alarm_touch/__init__.c @@ -0,0 +1,24 @@ +#include "py/obj.h" +#include "shared-bindings/alarm_touch/__init__.h" + +STATIC mp_obj_t alarm_touch_disable(void) { + common_hal_alarm_touch_disable(); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(alarm_touch_disable_obj, alarm_touch_disable); + +STATIC const mp_rom_map_elem_t alarm_touch_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_alarm_touch) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_Disable), MP_ROM_PTR(&alarm_touch_disable_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(alarm_touch_module_globals, alarm_touch_module_globals_table); + +const mp_obj_module_t alarm_touch_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&alarm_touch_module_globals, +}; + +const mp_obj_type_t alarm_touch_type = { + { &mp_type_type }, + .name = MP_QSTR_touchAlarm, +}; diff --git a/shared-bindings/alarm_touch/__init__.h b/shared-bindings/alarm_touch/__init__.h new file mode 100644 index 0000000000..600587d247 --- /dev/null +++ b/shared-bindings/alarm_touch/__init__.h @@ -0,0 +1,14 @@ +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TOUCH___INIT___H +#define MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TOUCH___INIT___H + +#include "py/runtime.h" + +typedef struct { + mp_obj_base_t base; +} alarm_touch_obj_t; + +extern const mp_obj_type_t alarm_touch_type; + +extern void common_hal_alarm_touch_disable (void); + +#endif //MICROPY_INCLUDED_SHARED_BINDINGS_ALARM_TOUCH___INIT___H diff --git a/shared-bindings/microcontroller/__init__.c b/shared-bindings/microcontroller/__init__.c index eb58a40982..88dc4179d6 100644 --- a/shared-bindings/microcontroller/__init__.c +++ b/shared-bindings/microcontroller/__init__.c @@ -135,15 +135,6 @@ STATIC mp_obj_t mcu_reset(void) { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(mcu_reset_obj, mcu_reset); -//| def sleep() -> None: -//| """Microcontroller will go into deep sleep. -//| cpy will restart when wakeup func. is triggered`. -//| -//| .. warning:: This may result in file system corruption when connected to a -//| host computer. Be very careful when calling this! Make sure the device -//| "Safely removed" on Windows or "ejected" on Mac OSX and Linux.""" -//| ... -//| STATIC mp_obj_t mcu_sleep(void) { common_hal_mcu_sleep(); // We won't actually get here because mcu is going into sleep.