Paul Sokolovsky | 5d7c408 | 2015-12-31 00:51:08 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013-2015 Damien P. George |
Paul Sokolovsky | 7193086 | 2016-01-05 21:23:23 +0200 | [diff] [blame] | 7 | * Copyright (c) 2016 Paul Sokolovsky |
Paul Sokolovsky | 5d7c408 | 2015-12-31 00:51:08 +0200 | [diff] [blame] | 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | * of this software and associated documentation files (the "Software"), to deal |
| 11 | * in the Software without restriction, including without limitation the rights |
| 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | * copies of the Software, and to permit persons to whom the Software is |
| 14 | * furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included in |
| 17 | * all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | * THE SOFTWARE. |
| 26 | */ |
| 27 | |
| 28 | #include <stdint.h> |
| 29 | |
| 30 | #include "py/obj.h" |
Paul Sokolovsky | 7193086 | 2016-01-05 21:23:23 +0200 | [diff] [blame] | 31 | #include "py/runtime.h" |
Paul Sokolovsky | 5d7c408 | 2015-12-31 00:51:08 +0200 | [diff] [blame] | 32 | #include "extmod/machine_mem.h" |
Paul Sokolovsky | 7193086 | 2016-01-05 21:23:23 +0200 | [diff] [blame] | 33 | #include "utils.h" |
| 34 | |
| 35 | #include "os_type.h" |
| 36 | #include "osapi.h" |
Paul Sokolovsky | 5d7c408 | 2015-12-31 00:51:08 +0200 | [diff] [blame] | 37 | |
| 38 | #if MICROPY_PY_MACHINE |
| 39 | |
Paul Sokolovsky | 7193086 | 2016-01-05 21:23:23 +0200 | [diff] [blame] | 40 | typedef struct _esp_timer_obj_t { |
| 41 | mp_obj_base_t base; |
| 42 | os_timer_t timer; |
| 43 | mp_obj_t callback; |
| 44 | } esp_timer_obj_t; |
| 45 | |
| 46 | const mp_obj_type_t esp_timer_type; |
| 47 | |
| 48 | STATIC void esp_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 49 | esp_timer_obj_t *self = self_in; |
| 50 | mp_printf(print, "Timer(%p)", &self->timer); |
| 51 | } |
| 52 | |
| 53 | STATIC mp_obj_t esp_timer_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { |
| 54 | mp_arg_check_num(n_args, n_kw, 1, 1, false); |
| 55 | esp_timer_obj_t *tim = m_new_obj(esp_timer_obj_t); |
| 56 | tim->base.type = &esp_timer_type; |
| 57 | return tim; |
| 58 | } |
| 59 | |
| 60 | STATIC void esp_timer_cb(void *arg) { |
| 61 | esp_timer_obj_t *self = arg; |
| 62 | call_function_1_protected(self->callback, self); |
| 63 | } |
| 64 | |
| 65 | STATIC mp_obj_t esp_timer_init_helper(esp_timer_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 66 | static const mp_arg_t allowed_args[] = { |
| 67 | // { MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
| 68 | { MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0xffffffff} }, |
| 69 | { MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} }, |
| 70 | { MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
| 71 | }; |
| 72 | |
| 73 | // parse args |
| 74 | mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 75 | mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 76 | |
| 77 | self->callback = args[2].u_obj; |
Paul Sokolovsky | f39bcb3 | 2016-01-07 18:58:52 +0200 | [diff] [blame^] | 78 | // Be sure to disarm timer before making any changes |
| 79 | os_timer_disarm(&self->timer); |
Paul Sokolovsky | 7193086 | 2016-01-05 21:23:23 +0200 | [diff] [blame] | 80 | os_timer_setfn(&self->timer, esp_timer_cb, self); |
| 81 | os_timer_arm(&self->timer, args[0].u_int, args[1].u_int); |
| 82 | |
| 83 | return mp_const_none; |
| 84 | } |
| 85 | |
| 86 | STATIC mp_obj_t esp_timer_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { |
| 87 | return esp_timer_init_helper(args[0], n_args - 1, args + 1, kw_args); |
| 88 | } |
| 89 | STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp_timer_init_obj, 1, esp_timer_init); |
| 90 | |
| 91 | STATIC mp_obj_t esp_timer_deinit(mp_obj_t self_in) { |
| 92 | esp_timer_obj_t *self = self_in; |
| 93 | os_timer_disarm(&self->timer); |
| 94 | return mp_const_none; |
| 95 | } |
| 96 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_timer_deinit_obj, esp_timer_deinit); |
| 97 | |
| 98 | STATIC const mp_map_elem_t esp_timer_locals_dict_table[] = { |
| 99 | { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), (mp_obj_t)&esp_timer_deinit_obj }, |
| 100 | { MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&esp_timer_init_obj }, |
| 101 | // { MP_OBJ_NEW_QSTR(MP_QSTR_callback), (mp_obj_t)&esp_timer_callback_obj }, |
| 102 | }; |
| 103 | STATIC MP_DEFINE_CONST_DICT(esp_timer_locals_dict, esp_timer_locals_dict_table); |
| 104 | |
| 105 | const mp_obj_type_t esp_timer_type = { |
| 106 | { &mp_type_type }, |
| 107 | .name = MP_QSTR_Timer, |
| 108 | .print = esp_timer_print, |
| 109 | .make_new = esp_timer_make_new, |
| 110 | .locals_dict = (mp_obj_t)&esp_timer_locals_dict, |
| 111 | }; |
| 112 | |
Paul Sokolovsky | 5d7c408 | 2015-12-31 00:51:08 +0200 | [diff] [blame] | 113 | STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { |
| 114 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) }, |
| 115 | { MP_ROM_QSTR(MP_QSTR_mem8), MP_ROM_PTR(&machine_mem8_obj) }, |
| 116 | { MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) }, |
| 117 | { MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) }, |
Paul Sokolovsky | 7193086 | 2016-01-05 21:23:23 +0200 | [diff] [blame] | 118 | |
| 119 | { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&esp_timer_type) }, |
Paul Sokolovsky | 5d7c408 | 2015-12-31 00:51:08 +0200 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table); |
| 123 | |
| 124 | const mp_obj_module_t mp_module_machine = { |
| 125 | .base = { &mp_type_module }, |
| 126 | .name = MP_QSTR_umachine, |
| 127 | .globals = (mp_obj_dict_t*)&machine_module_globals, |
| 128 | }; |
| 129 | |
| 130 | #endif // MICROPY_PY_MACHINE |