|
| 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(µbit_button_a_obj)) { |
| 78 | + microbit_hal_power_wake_on_button(0); |
| 79 | + } else if (items[i] == MP_OBJ_FROM_PTR(µbit_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 | +}; |
0 commit comments