2019-10-23 06:56:38 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/mphal.h"
|
|
|
|
|
2022-10-27 04:32:43 +01:00
|
|
|
#if MICROPY_PY_MACHINE_TIMER
|
|
|
|
|
2022-11-11 04:43:55 +00:00
|
|
|
#include "shared/runtime/softtimer.h"
|
|
|
|
|
2019-10-23 06:56:38 +01:00
|
|
|
typedef soft_timer_entry_t machine_timer_obj_t;
|
|
|
|
|
|
|
|
const mp_obj_type_t machine_timer_type;
|
|
|
|
|
|
|
|
STATIC void machine_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2019-12-27 11:46:43 +00:00
|
|
|
machine_timer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-10-23 06:56:38 +01:00
|
|
|
qstr mode = self->mode == SOFT_TIMER_MODE_ONE_SHOT ? MP_QSTR_ONE_SHOT : MP_QSTR_PERIODIC;
|
|
|
|
mp_printf(print, "Timer(mode=%q, period=%u)", mode, self->delta_ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
enum { ARG_mode, ARG_callback, ARG_period, ARG_tick_hz, ARG_freq, };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = SOFT_TIMER_MODE_PERIODIC} },
|
2021-04-21 05:06:14 +01:00
|
|
|
{ MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
2019-10-23 06:56:38 +01:00
|
|
|
{ MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} },
|
|
|
|
{ MP_QSTR_tick_hz, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000} },
|
2019-12-27 11:46:43 +00:00
|
|
|
{ MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
|
2019-10-23 06:56:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Parse args
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
|
|
|
self->mode = args[ARG_mode].u_int;
|
2021-04-21 05:06:14 +01:00
|
|
|
|
|
|
|
uint64_t delta_ms = self->delta_ms;
|
2019-10-23 06:56:38 +01:00
|
|
|
if (args[ARG_freq].u_obj != mp_const_none) {
|
|
|
|
// Frequency specified in Hz
|
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2021-04-21 05:06:14 +01:00
|
|
|
delta_ms = (uint32_t)(MICROPY_FLOAT_CONST(1000.0) / mp_obj_get_float(args[ARG_freq].u_obj));
|
2019-10-23 06:56:38 +01:00
|
|
|
#else
|
2021-04-21 05:06:14 +01:00
|
|
|
delta_ms = 1000 / mp_obj_get_int(args[ARG_freq].u_obj);
|
2019-10-23 06:56:38 +01:00
|
|
|
#endif
|
2021-04-21 05:06:14 +01:00
|
|
|
} else if (args[ARG_period].u_int != 0xffffffff) {
|
2019-10-23 06:56:38 +01:00
|
|
|
// Period specified
|
2021-04-21 05:06:14 +01:00
|
|
|
delta_ms = (uint64_t)args[ARG_period].u_int * 1000 / args[ARG_tick_hz].u_int;
|
2019-10-23 06:56:38 +01:00
|
|
|
}
|
2021-04-21 05:06:14 +01:00
|
|
|
|
|
|
|
if (delta_ms < 1) {
|
|
|
|
delta_ms = 1;
|
|
|
|
} else if (delta_ms >= 0x40000000) {
|
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("period too large"));
|
2019-10-23 06:56:38 +01:00
|
|
|
}
|
2021-04-21 05:06:14 +01:00
|
|
|
self->delta_ms = (uint32_t)delta_ms;
|
2019-10-23 06:56:38 +01:00
|
|
|
|
2021-04-21 05:06:14 +01:00
|
|
|
if (args[ARG_callback].u_obj != MP_OBJ_NULL) {
|
2021-04-27 07:30:58 +01:00
|
|
|
self->py_callback = args[ARG_callback].u_obj;
|
2021-04-21 05:06:14 +01:00
|
|
|
}
|
|
|
|
|
2021-04-27 07:30:58 +01:00
|
|
|
if (self->py_callback != mp_const_none) {
|
2021-04-29 06:21:22 +01:00
|
|
|
soft_timer_insert(self, self->delta_ms);
|
2021-04-21 05:06:14 +01:00
|
|
|
}
|
2019-10-23 06:56:38 +01:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
machine_timer_obj_t *self = m_new_obj(machine_timer_obj_t);
|
2020-01-13 06:12:50 +00:00
|
|
|
self->pairheap.base.type = &machine_timer_type;
|
2021-04-29 06:21:22 +01:00
|
|
|
self->flags = SOFT_TIMER_FLAG_PY_CALLBACK | SOFT_TIMER_FLAG_GC_ALLOCATED;
|
2021-04-21 05:06:14 +01:00
|
|
|
self->delta_ms = 1000;
|
2021-04-27 07:30:58 +01:00
|
|
|
self->py_callback = mp_const_none;
|
2019-10-23 06:56:38 +01:00
|
|
|
|
|
|
|
// Get timer id (only soft timer (-1) supported at the moment)
|
|
|
|
mp_int_t id = -1;
|
|
|
|
if (n_args > 0) {
|
|
|
|
id = mp_obj_get_int(args[0]);
|
|
|
|
--n_args;
|
|
|
|
++args;
|
|
|
|
}
|
|
|
|
if (id != -1) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("Timer doesn't exist"));
|
2019-10-23 06:56:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (n_args > 0 || n_kw > 0) {
|
|
|
|
// Start the timer
|
|
|
|
mp_map_t kw_args;
|
|
|
|
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
|
|
|
|
machine_timer_init_helper(self, n_args, args, &kw_args);
|
|
|
|
}
|
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t machine_timer_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
|
|
|
machine_timer_obj_t *self = MP_OBJ_TO_PTR(args[0]);
|
|
|
|
soft_timer_remove(self);
|
|
|
|
return machine_timer_init_helper(self, n_args - 1, args + 1, kw_args);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_timer_init_obj, 1, machine_timer_init);
|
|
|
|
|
|
|
|
STATIC mp_obj_t machine_timer_deinit(mp_obj_t self_in) {
|
|
|
|
machine_timer_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
soft_timer_remove(self);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_timer_deinit_obj, machine_timer_deinit);
|
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t machine_timer_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_timer_init_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_timer_deinit_obj) },
|
|
|
|
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ONE_SHOT), MP_ROM_INT(SOFT_TIMER_MODE_ONE_SHOT) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PERIODIC), MP_ROM_INT(SOFT_TIMER_MODE_PERIODIC) },
|
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(machine_timer_locals_dict, machine_timer_locals_dict_table);
|
|
|
|
|
2021-07-14 05:38:38 +01:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
machine_timer_type,
|
|
|
|
MP_QSTR_Timer,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
2022-09-16 15:31:23 +01:00
|
|
|
make_new, machine_timer_make_new,
|
2021-07-14 05:38:38 +01:00
|
|
|
print, machine_timer_print,
|
2022-06-24 07:27:46 +01:00
|
|
|
locals_dict, &machine_timer_locals_dict
|
2021-07-14 05:38:38 +01:00
|
|
|
);
|
2022-10-27 04:32:43 +01:00
|
|
|
|
|
|
|
#endif // MICROPY_PY_MACHINE_TIMER
|