Skip to content

Commit 009fbd2

Browse files
committed
codal_port/modpower: Add initial power module.
This initial implementation supports: - power.off() - power.deep_sleep() with ms, pins and buttons arguments See issue #60. Signed-off-by: Damien George <[email protected]>
1 parent 2ca649a commit 009fbd2

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

src/codal_port/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ SRC_C += \
8787
modmusic.c \
8888
modmusictunes.c \
8989
modos.c \
90+
modpower.c \
9091
modradio.c \
9192
modspeech.c \
9293
modthis.c \

src/codal_port/modpower.c

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/runtime.h"
28+
#include "py/mphal.h"
29+
30+
STATIC size_t get_array(mp_obj_t *src, mp_obj_t **items) {
31+
if (*src == mp_const_none) {
32+
// None, so an array of length 0.
33+
*items = NULL;
34+
return 0;
35+
} else if (mp_obj_is_type(*src, &mp_type_tuple) || mp_obj_is_type(*src, &mp_type_list)) {
36+
// A tuple/list passed in, get items and length.
37+
size_t len;
38+
mp_obj_get_array(*src, &len, items);
39+
return len;
40+
} else {
41+
// A single object passed in, so an array of length 1.
42+
*items = src;
43+
return 1;
44+
}
45+
}
46+
47+
STATIC mp_obj_t power_off(void) {
48+
microbit_hal_power_off();
49+
return mp_const_none;
50+
}
51+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(power_off_obj, power_off);
52+
53+
STATIC mp_obj_t power_deep_sleep(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
54+
enum { ARG_ms, ARG_pins, ARG_buttons, ARG_run_every };
55+
static const mp_arg_t allowed_args[] = {
56+
{ MP_QSTR_ms, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
57+
{ MP_QSTR_pins, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
58+
{ MP_QSTR_buttons, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
59+
{ MP_QSTR_run_every, MP_ARG_BOOL, {.u_bool = false} },
60+
};
61+
62+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
63+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
64+
65+
if (args[ARG_pins].u_obj != mp_const_none) {
66+
mp_obj_t *items;
67+
size_t len = get_array(&args[ARG_pins].u_obj, &items);
68+
for (size_t i = 0; i < len; ++i) {
69+
microbit_hal_power_wake_on_pin(microbit_obj_get_pin_name(items[i]));
70+
}
71+
}
72+
73+
if (args[ARG_buttons].u_obj != mp_const_none) {
74+
mp_obj_t *items;
75+
size_t len = get_array(&args[ARG_buttons].u_obj, &items);
76+
for (size_t i = 0; i < len; ++i) {
77+
if (items[i] == MP_OBJ_FROM_PTR(&microbit_button_a_obj)) {
78+
microbit_hal_power_wake_on_button(0);
79+
} else if (items[i] == MP_OBJ_FROM_PTR(&microbit_button_b_obj)) {
80+
microbit_hal_power_wake_on_button(1);
81+
} else {
82+
mp_raise_ValueError(MP_ERROR_TEXT("expecting a button"));
83+
}
84+
}
85+
}
86+
87+
if (args[ARG_run_every].u_bool) {
88+
mp_raise_ValueError(MP_ERROR_TEXT("run_every not implemented"));
89+
}
90+
91+
if (args[ARG_ms].u_obj == mp_const_none) {
92+
microbit_hal_power_deep_sleep(false, 0);
93+
} else {
94+
microbit_hal_power_deep_sleep(true, mp_obj_get_int(args[ARG_ms].u_obj));
95+
}
96+
97+
return mp_const_none;
98+
}
99+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(power_deep_sleep_obj, 0, power_deep_sleep);
100+
101+
STATIC const mp_rom_map_elem_t power_module_globals_table[] = {
102+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_power) },
103+
104+
{ MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&power_off_obj) },
105+
{ MP_ROM_QSTR(MP_QSTR_deep_sleep), MP_ROM_PTR(&power_deep_sleep_obj) },
106+
};
107+
STATIC MP_DEFINE_CONST_DICT(power_module_globals, power_module_globals_table);
108+
109+
const mp_obj_module_t power_module = {
110+
.base = { &mp_type_module },
111+
.globals = (mp_obj_dict_t *)&power_module_globals,
112+
};

src/codal_port/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ extern const struct _mp_obj_module_t machine_module;
131131
extern const struct _mp_obj_module_t microbit_module;
132132
extern const struct _mp_obj_module_t music_module;
133133
extern const struct _mp_obj_module_t os_module;
134+
extern const struct _mp_obj_module_t power_module;
134135
extern const struct _mp_obj_module_t radio_module;
135136
extern const struct _mp_obj_module_t speech_module;
136137
extern const struct _mp_obj_module_t this_module;
@@ -145,6 +146,7 @@ extern const struct _mp_obj_module_t utime_module;
145146
{ MP_ROM_QSTR(MP_QSTR_microbit), MP_ROM_PTR(&microbit_module) }, \
146147
{ MP_ROM_QSTR(MP_QSTR_music), MP_ROM_PTR(&music_module) }, \
147148
{ MP_ROM_QSTR(MP_QSTR_os), MP_ROM_PTR(&os_module) }, \
149+
{ MP_ROM_QSTR(MP_QSTR_power), MP_ROM_PTR(&power_module) }, \
148150
{ MP_ROM_QSTR(MP_QSTR_radio), MP_ROM_PTR(&radio_module) }, \
149151
{ MP_ROM_QSTR(MP_QSTR_speech), MP_ROM_PTR(&speech_module) }, \
150152
{ MP_ROM_QSTR(MP_QSTR_this), MP_ROM_PTR(&this_module) }, \

0 commit comments

Comments
 (0)