|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2013, 2014 Damien P. George |
| 7 | + * Copyright (c) 2015 Josef Gajdusek |
| 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 <stdio.h> |
| 29 | +#include <string.h> |
| 30 | + |
| 31 | +#include "py/nlr.h" |
| 32 | +#include "py/obj.h" |
| 33 | +#include "py/gc.h" |
| 34 | +#include "py/runtime.h" |
| 35 | +#include "py/mphal.h" |
| 36 | +#include "py/smallint.h" |
| 37 | + |
| 38 | +/// \module time - time related functions |
| 39 | +/// |
| 40 | +/// The `time` module provides functions for getting the current time and date, |
| 41 | +/// and for sleeping. |
| 42 | + |
| 43 | +/// \function sleep(seconds) |
| 44 | +/// Sleep for the given number of seconds. |
| 45 | +STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) { |
| 46 | + #if MICROPY_PY_BUILTINS_FLOAT |
| 47 | + mp_hal_delay_ms(1000 * mp_obj_get_float(seconds_o)); |
| 48 | + #else |
| 49 | + mp_hal_delay_ms(1000 * mp_obj_get_int(seconds_o)); |
| 50 | + #endif |
| 51 | + return mp_const_none; |
| 52 | +} |
| 53 | +MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep); |
| 54 | + |
| 55 | +STATIC mp_obj_t time_sleep_ms(mp_obj_t arg) { |
| 56 | + mp_hal_delay_ms(mp_obj_get_int(arg)); |
| 57 | + return mp_const_none; |
| 58 | +} |
| 59 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_ms_obj, time_sleep_ms); |
| 60 | + |
| 61 | +STATIC mp_obj_t time_sleep_us(mp_obj_t arg) { |
| 62 | + mp_hal_delay_us(mp_obj_get_int(arg)); |
| 63 | + return mp_const_none; |
| 64 | +} |
| 65 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_us_obj, time_sleep_us); |
| 66 | + |
| 67 | +STATIC const mp_map_elem_t time_module_globals_table[] = { |
| 68 | + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_utime) }, |
| 69 | + |
| 70 | + { MP_OBJ_NEW_QSTR(MP_QSTR_sleep), (mp_obj_t)&time_sleep_obj }, |
| 71 | + { MP_OBJ_NEW_QSTR(MP_QSTR_sleep_ms), (mp_obj_t)&time_sleep_ms_obj }, |
| 72 | + { MP_OBJ_NEW_QSTR(MP_QSTR_sleep_us), (mp_obj_t)&time_sleep_us_obj }, |
| 73 | +}; |
| 74 | + |
| 75 | +STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table); |
| 76 | + |
| 77 | +const mp_obj_module_t utime_module = { |
| 78 | + .base = { &mp_type_module }, |
| 79 | + .name = MP_QSTR_utime, |
| 80 | + .globals = (mp_obj_dict_t*)&time_module_globals, |
| 81 | +}; |
0 commit comments