Add function to disable alarm

This commit is contained in:
microDev 2020-09-22 23:47:10 +05:30 committed by Scott Shawcroft
parent 05a3f203db
commit 21ba61afbb
No known key found for this signature in database
GPG Key ID: 0DFD512649C052DA
6 changed files with 73 additions and 15 deletions

View File

@ -3,25 +3,25 @@
#include "esp_sleep.h"
#include "driver/rtc_io.h"
void common_hal_io_alarm_pin_state (uint8_t gpio, uint8_t level, bool pull) {
if (!rtc_gpio_is_valid_gpio(gpio)) {
mp_raise_ValueError(translate("io must be rtc io"));
return;
mp_obj_t common_hal_io_alarm_pin_state (io_alarm_obj_t *self_in) {
if (!rtc_gpio_is_valid_gpio(self_in->gpio)) {
mp_raise_ValueError(translate("io must be rtc io"));
}
switch(esp_sleep_enable_ext0_wakeup(gpio, level)) {
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"));
return;
case ESP_ERR_INVALID_STATE:
mp_raise_RuntimeError(translate("wakeup conflict"));
return;
default:
break;
}
if (pull) {
(level) ? rtc_gpio_pulldown_en(gpio) : rtc_gpio_pullup_en(gpio);
}
if (self_in->pull) { (self_in->level) ? rtc_gpio_pulldown_en(self_in->gpio) : rtc_gpio_pullup_en(self_in->gpio); }
return self_in;
}
void common_hal_io_alarm_disable (void) {
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_EXT0 | ESP_SLEEP_WAKEUP_EXT1);
}

View File

@ -8,3 +8,6 @@ void common_hal_time_alarm_duration (uint32_t ms) {
}
}
void common_hal_time_alarm_disable (void) {
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER);
}

View File

@ -3,7 +3,7 @@
#include "shared-bindings/io_alarm/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
//| Set Timer Wakeup
//| Set Pin Wakeup
//|
STATIC mp_obj_t io_alarm_pin_state(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_level, ARG_pull };
@ -16,15 +16,30 @@ STATIC mp_obj_t io_alarm_pin_state(size_t n_args, const mp_obj_t *pos_args, mp_m
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]);
common_hal_io_alarm_pin_state(pin->number, args[ARG_level].u_int, args[ARG_pull].u_bool);
io_alarm_obj_t *self = m_new_obj(io_alarm_obj_t);
return mp_const_none;
self->base.type = &io_alarm_type;
self->gpio = pin->number;
self->level = args[ARG_level].u_int;
self->pull = args[ARG_pull].u_bool;
return common_hal_io_alarm_pin_state(self);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(io_alarm_pin_state_obj, 1, io_alarm_pin_state);
//| Disable Pin Wakeup
//|
STATIC mp_obj_t io_alarm_disable(void) {
common_hal_io_alarm_disable();
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(io_alarm_disable_obj, io_alarm_disable);
STATIC const mp_rom_map_elem_t io_alarm_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_io_alarm) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_PinState), MP_ROM_PTR(&io_alarm_pin_state_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_Disable), MP_ROM_PTR(&io_alarm_disable_obj) },
};
STATIC MP_DEFINE_CONST_DICT(io_alarm_module_globals, io_alarm_module_globals_table);
@ -32,3 +47,8 @@ const mp_obj_module_t io_alarm_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&io_alarm_module_globals,
};
const mp_obj_type_t io_alarm_type = {
{ &mp_type_type },
.name = MP_QSTR_ioAlarm,
};

View File

@ -3,6 +3,15 @@
#include "py/runtime.h"
extern void common_hal_io_alarm_pin_state(uint8_t gpio, uint8_t level, bool pull);
typedef struct {
mp_obj_base_t base;
uint8_t gpio, level;
bool pull;
} io_alarm_obj_t;
extern const mp_obj_type_t io_alarm_type;
extern mp_obj_t common_hal_io_alarm_pin_state (io_alarm_obj_t *self_in);
extern void common_hal_io_alarm_disable (void);
#endif //MICROPY_INCLUDED_SHARED_BINDINGS_IO_ALARM___INIT___H

View File

@ -11,17 +11,31 @@ STATIC mp_obj_t time_alarm_duration(mp_obj_t seconds_o) {
mp_int_t seconds = mp_obj_get_int(seconds_o);
mp_int_t msecs = 1000 * seconds;
#endif
if (seconds < 0) {
mp_raise_ValueError(translate("sleep length must be non-negative"));
}
common_hal_time_alarm_duration(msecs);
return mp_const_none;
time_alarm_obj_t *self = m_new_obj(time_alarm_obj_t);
self->base.type = &time_alarm_type;
return self;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_alarm_duration_obj, time_alarm_duration);
//| Disable Timer Wakeup
//|
STATIC mp_obj_t time_alarm_disable(void) {
common_hal_time_alarm_disable();
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_alarm_disable_obj, time_alarm_disable);
STATIC const mp_rom_map_elem_t time_alarm_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_time_alarm) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_Duration), MP_ROM_PTR(&time_alarm_duration_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_Disable), MP_ROM_PTR(&time_alarm_disable_obj) },
};
STATIC MP_DEFINE_CONST_DICT(time_alarm_module_globals, time_alarm_module_globals_table);
@ -29,3 +43,8 @@ const mp_obj_module_t time_alarm_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&time_alarm_module_globals,
};
const mp_obj_type_t time_alarm_type = {
{ &mp_type_type },
.name = MP_QSTR_timeAlarm,
};

View File

@ -3,6 +3,13 @@
#include "py/runtime.h"
typedef struct {
mp_obj_base_t base;
} time_alarm_obj_t;
extern const mp_obj_type_t time_alarm_type;
extern void common_hal_time_alarm_duration(uint32_t);
extern void common_hal_time_alarm_disable (void);
#endif //MICROPY_INCLUDED_SHARED_BINDINGS_TIME_ALARM___INIT___H