Skip to content

Commit 21ba61a

Browse files
microdev1tannewt
authored andcommitted
Add function to disable alarm
1 parent 05a3f20 commit 21ba61a

File tree

6 files changed

+73
-15
lines changed

6 files changed

+73
-15
lines changed

ports/esp32s2/common-hal/io_alarm/__init__.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@
33
#include "esp_sleep.h"
44
#include "driver/rtc_io.h"
55

6-
void common_hal_io_alarm_pin_state (uint8_t gpio, uint8_t level, bool pull) {
7-
if (!rtc_gpio_is_valid_gpio(gpio)) {
8-
mp_raise_ValueError(translate("io must be rtc io"));
9-
return;
6+
mp_obj_t common_hal_io_alarm_pin_state (io_alarm_obj_t *self_in) {
7+
if (!rtc_gpio_is_valid_gpio(self_in->gpio)) {
8+
mp_raise_ValueError(translate("io must be rtc io"));
109
}
1110

12-
switch(esp_sleep_enable_ext0_wakeup(gpio, level)) {
11+
switch(esp_sleep_enable_ext0_wakeup(self_in->gpio, self_in->level)) {
1312
case ESP_ERR_INVALID_ARG:
1413
mp_raise_ValueError(translate("trigger level must be 0 or 1"));
15-
return;
1614
case ESP_ERR_INVALID_STATE:
1715
mp_raise_RuntimeError(translate("wakeup conflict"));
18-
return;
1916
default:
2017
break;
2118
}
2219

23-
if (pull) {
24-
(level) ? rtc_gpio_pulldown_en(gpio) : rtc_gpio_pullup_en(gpio);
25-
}
20+
if (self_in->pull) { (self_in->level) ? rtc_gpio_pulldown_en(self_in->gpio) : rtc_gpio_pullup_en(self_in->gpio); }
21+
22+
return self_in;
2623
}
2724

25+
void common_hal_io_alarm_disable (void) {
26+
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_EXT0 | ESP_SLEEP_WAKEUP_EXT1);
27+
}

ports/esp32s2/common-hal/time_alarm/__init__.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ void common_hal_time_alarm_duration (uint32_t ms) {
88
}
99
}
1010

11+
void common_hal_time_alarm_disable (void) {
12+
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER);
13+
}

shared-bindings/io_alarm/__init__.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "shared-bindings/io_alarm/__init__.h"
44
#include "shared-bindings/microcontroller/Pin.h"
55

6-
//| Set Timer Wakeup
6+
//| Set Pin Wakeup
77
//|
88
STATIC mp_obj_t io_alarm_pin_state(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
99
enum { ARG_level, ARG_pull };
@@ -16,19 +16,39 @@ STATIC mp_obj_t io_alarm_pin_state(size_t n_args, const mp_obj_t *pos_args, mp_m
1616
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
1717

1818
mcu_pin_obj_t *pin = validate_obj_is_pin(pos_args[0]);
19-
common_hal_io_alarm_pin_state(pin->number, args[ARG_level].u_int, args[ARG_pull].u_bool);
19+
io_alarm_obj_t *self = m_new_obj(io_alarm_obj_t);
2020

21-
return mp_const_none;
21+
self->base.type = &io_alarm_type;
22+
self->gpio = pin->number;
23+
self->level = args[ARG_level].u_int;
24+
self->pull = args[ARG_pull].u_bool;
25+
26+
return common_hal_io_alarm_pin_state(self);
2227
}
2328
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(io_alarm_pin_state_obj, 1, io_alarm_pin_state);
2429

30+
31+
//| Disable Pin Wakeup
32+
//|
33+
STATIC mp_obj_t io_alarm_disable(void) {
34+
common_hal_io_alarm_disable();
35+
return mp_const_none;
36+
}
37+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(io_alarm_disable_obj, io_alarm_disable);
38+
2539
STATIC const mp_rom_map_elem_t io_alarm_module_globals_table[] = {
2640
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_io_alarm) },
2741
{ MP_OBJ_NEW_QSTR(MP_QSTR_PinState), MP_ROM_PTR(&io_alarm_pin_state_obj) },
42+
{ MP_OBJ_NEW_QSTR(MP_QSTR_Disable), MP_ROM_PTR(&io_alarm_disable_obj) },
2843
};
2944
STATIC MP_DEFINE_CONST_DICT(io_alarm_module_globals, io_alarm_module_globals_table);
3045

3146
const mp_obj_module_t io_alarm_module = {
3247
.base = { &mp_type_module },
3348
.globals = (mp_obj_dict_t*)&io_alarm_module_globals,
3449
};
50+
51+
const mp_obj_type_t io_alarm_type = {
52+
{ &mp_type_type },
53+
.name = MP_QSTR_ioAlarm,
54+
};

shared-bindings/io_alarm/__init__.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33

44
#include "py/runtime.h"
55

6-
extern void common_hal_io_alarm_pin_state(uint8_t gpio, uint8_t level, bool pull);
6+
typedef struct {
7+
mp_obj_base_t base;
8+
uint8_t gpio, level;
9+
bool pull;
10+
} io_alarm_obj_t;
11+
12+
extern const mp_obj_type_t io_alarm_type;
13+
14+
extern mp_obj_t common_hal_io_alarm_pin_state (io_alarm_obj_t *self_in);
15+
extern void common_hal_io_alarm_disable (void);
716

817
#endif //MICROPY_INCLUDED_SHARED_BINDINGS_IO_ALARM___INIT___H

shared-bindings/time_alarm/__init__.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,40 @@ STATIC mp_obj_t time_alarm_duration(mp_obj_t seconds_o) {
1111
mp_int_t seconds = mp_obj_get_int(seconds_o);
1212
mp_int_t msecs = 1000 * seconds;
1313
#endif
14+
1415
if (seconds < 0) {
1516
mp_raise_ValueError(translate("sleep length must be non-negative"));
1617
}
1718
common_hal_time_alarm_duration(msecs);
18-
return mp_const_none;
19+
20+
time_alarm_obj_t *self = m_new_obj(time_alarm_obj_t);
21+
self->base.type = &time_alarm_type;
22+
23+
return self;
1924
}
2025
STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_alarm_duration_obj, time_alarm_duration);
2126

27+
//| Disable Timer Wakeup
28+
//|
29+
STATIC mp_obj_t time_alarm_disable(void) {
30+
common_hal_time_alarm_disable();
31+
return mp_const_none;
32+
}
33+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_alarm_disable_obj, time_alarm_disable);
34+
2235
STATIC const mp_rom_map_elem_t time_alarm_module_globals_table[] = {
2336
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_time_alarm) },
2437
{ MP_OBJ_NEW_QSTR(MP_QSTR_Duration), MP_ROM_PTR(&time_alarm_duration_obj) },
38+
{ MP_OBJ_NEW_QSTR(MP_QSTR_Disable), MP_ROM_PTR(&time_alarm_disable_obj) },
2539
};
2640
STATIC MP_DEFINE_CONST_DICT(time_alarm_module_globals, time_alarm_module_globals_table);
2741

2842
const mp_obj_module_t time_alarm_module = {
2943
.base = { &mp_type_module },
3044
.globals = (mp_obj_dict_t*)&time_alarm_module_globals,
3145
};
46+
47+
const mp_obj_type_t time_alarm_type = {
48+
{ &mp_type_type },
49+
.name = MP_QSTR_timeAlarm,
50+
};

shared-bindings/time_alarm/__init__.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33

44
#include "py/runtime.h"
55

6+
typedef struct {
7+
mp_obj_base_t base;
8+
} time_alarm_obj_t;
9+
10+
extern const mp_obj_type_t time_alarm_type;
11+
612
extern void common_hal_time_alarm_duration(uint32_t);
13+
extern void common_hal_time_alarm_disable (void);
714

815
#endif //MICROPY_INCLUDED_SHARED_BINDINGS_TIME_ALARM___INIT___H

0 commit comments

Comments
 (0)