|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2019 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 | +#include "shared/runtime/softtimer.h" |
| 30 | + |
| 31 | +typedef soft_timer_entry_t machine_timer_obj_t; |
| 32 | + |
| 33 | +const mp_obj_type_t machine_timer_type; |
| 34 | + |
| 35 | +STATIC void machine_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 36 | + machine_timer_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 37 | + qstr mode = self->mode == SOFT_TIMER_MODE_ONE_SHOT ? MP_QSTR_ONE_SHOT : MP_QSTR_PERIODIC; |
| 38 | + mp_printf(print, "Timer(mode=%q, period=%u)", mode, self->delta_ms); |
| 39 | +} |
| 40 | + |
| 41 | +STATIC mp_obj_t machine_timer_init_helper(machine_timer_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 42 | + enum { ARG_mode, ARG_callback, ARG_period, ARG_tick_hz, ARG_freq, }; |
| 43 | + static const mp_arg_t allowed_args[] = { |
| 44 | + { MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = SOFT_TIMER_MODE_PERIODIC} }, |
| 45 | + { MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 46 | + { MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} }, |
| 47 | + { MP_QSTR_tick_hz, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000} }, |
| 48 | + { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, |
| 49 | + }; |
| 50 | + |
| 51 | + // Parse args |
| 52 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 53 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 54 | + |
| 55 | + self->mode = args[ARG_mode].u_int; |
| 56 | + |
| 57 | + uint64_t delta_ms = self->delta_ms; |
| 58 | + if (args[ARG_freq].u_obj != mp_const_none) { |
| 59 | + // Frequency specified in Hz |
| 60 | + #if MICROPY_PY_BUILTINS_FLOAT |
| 61 | + delta_ms = (uint32_t)(MICROPY_FLOAT_CONST(1000.0) / mp_obj_get_float(args[ARG_freq].u_obj)); |
| 62 | + #else |
| 63 | + delta_ms = 1000 / mp_obj_get_int(args[ARG_freq].u_obj); |
| 64 | + #endif |
| 65 | + } else if (args[ARG_period].u_int != 0xffffffff) { |
| 66 | + // Period specified |
| 67 | + delta_ms = (uint64_t)args[ARG_period].u_int * 1000 / args[ARG_tick_hz].u_int; |
| 68 | + } |
| 69 | + |
| 70 | + if (delta_ms < 1) { |
| 71 | + delta_ms = 1; |
| 72 | + } else if (delta_ms >= 0x40000000) { |
| 73 | + mp_raise_ValueError(MP_ERROR_TEXT("period too large")); |
| 74 | + } |
| 75 | + self->delta_ms = (uint32_t)delta_ms; |
| 76 | + |
| 77 | + if (args[ARG_callback].u_obj != MP_OBJ_NULL) { |
| 78 | + self->py_callback = args[ARG_callback].u_obj; |
| 79 | + } |
| 80 | + |
| 81 | + if (self->py_callback != mp_const_none) { |
| 82 | + soft_timer_insert(self, self->delta_ms); |
| 83 | + } |
| 84 | + |
| 85 | + return mp_const_none; |
| 86 | +} |
| 87 | + |
| 88 | +STATIC mp_obj_t machine_timer_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
| 89 | + machine_timer_obj_t *self = m_new_obj(machine_timer_obj_t); |
| 90 | + self->pairheap.base.type = &machine_timer_type; |
| 91 | + self->flags = SOFT_TIMER_FLAG_PY_CALLBACK | SOFT_TIMER_FLAG_GC_ALLOCATED; |
| 92 | + self->delta_ms = 1000; |
| 93 | + self->py_callback = mp_const_none; |
| 94 | + |
| 95 | + // Get timer id (only soft timer (-1) supported at the moment) |
| 96 | + mp_int_t id = -1; |
| 97 | + if (n_args > 0) { |
| 98 | + id = mp_obj_get_int(args[0]); |
| 99 | + --n_args; |
| 100 | + ++args; |
| 101 | + } |
| 102 | + if (id != -1) { |
| 103 | + mp_raise_ValueError(MP_ERROR_TEXT("Timer doesn't exist")); |
| 104 | + } |
| 105 | + |
| 106 | + if (n_args > 0 || n_kw > 0) { |
| 107 | + // Start the timer |
| 108 | + mp_map_t kw_args; |
| 109 | + mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); |
| 110 | + machine_timer_init_helper(self, n_args, args, &kw_args); |
| 111 | + } |
| 112 | + |
| 113 | + return MP_OBJ_FROM_PTR(self); |
| 114 | +} |
| 115 | + |
| 116 | +STATIC mp_obj_t machine_timer_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { |
| 117 | + machine_timer_obj_t *self = MP_OBJ_TO_PTR(args[0]); |
| 118 | + soft_timer_remove(self); |
| 119 | + return machine_timer_init_helper(self, n_args - 1, args + 1, kw_args); |
| 120 | +} |
| 121 | +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_timer_init_obj, 1, machine_timer_init); |
| 122 | + |
| 123 | +STATIC mp_obj_t machine_timer_deinit(mp_obj_t self_in) { |
| 124 | + machine_timer_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 125 | + soft_timer_remove(self); |
| 126 | + return mp_const_none; |
| 127 | +} |
| 128 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_deinit_obj, machine_timer_deinit); |
| 129 | + |
| 130 | +STATIC const mp_rom_map_elem_t machine_timer_locals_dict_table[] = { |
| 131 | + { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_timer_init_obj) }, |
| 132 | + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_timer_deinit_obj) }, |
| 133 | + |
| 134 | + { MP_ROM_QSTR(MP_QSTR_ONE_SHOT), MP_ROM_INT(SOFT_TIMER_MODE_ONE_SHOT) }, |
| 135 | + { MP_ROM_QSTR(MP_QSTR_PERIODIC), MP_ROM_INT(SOFT_TIMER_MODE_PERIODIC) }, |
| 136 | +}; |
| 137 | +STATIC MP_DEFINE_CONST_DICT(machine_timer_locals_dict, machine_timer_locals_dict_table); |
| 138 | + |
| 139 | +MP_DEFINE_CONST_OBJ_TYPE( |
| 140 | + machine_timer_type, |
| 141 | + MP_QSTR_Timer, |
| 142 | + MP_TYPE_FLAG_NONE, |
| 143 | + make_new, machine_timer_make_new, |
| 144 | + print, machine_timer_print, |
| 145 | + locals_dict, &machine_timer_locals_dict |
| 146 | + ); |
0 commit comments