2015-03-28 17:45:40 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2015-03-28 17:45:40 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 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 <stdio.h>
|
|
|
|
|
|
|
|
#include "py/obj.h"
|
2015-10-30 19:03:58 -04:00
|
|
|
#include "py/mphal.h"
|
2015-03-28 17:45:40 -04:00
|
|
|
#include "modpyb.h"
|
|
|
|
|
|
|
|
STATIC mp_obj_t pyb_millis(void) {
|
2015-10-30 19:03:58 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms());
|
2015-03-28 17:45:40 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis);
|
|
|
|
|
|
|
|
STATIC mp_obj_t pyb_elapsed_millis(mp_obj_t start) {
|
|
|
|
uint32_t startMillis = mp_obj_get_int(start);
|
2015-10-30 19:03:58 -04:00
|
|
|
uint32_t currMillis = mp_hal_ticks_ms();
|
2015-03-28 17:45:40 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT((currMillis - startMillis) & 0x1fff);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_millis_obj, pyb_elapsed_millis);
|
|
|
|
|
|
|
|
STATIC mp_obj_t pyb_delay(mp_obj_t ms_in) {
|
|
|
|
mp_int_t ms = mp_obj_get_int(ms_in);
|
|
|
|
if (ms >= 0) {
|
2015-10-30 19:03:58 -04:00
|
|
|
mp_hal_delay_ms(ms);
|
2015-03-28 17:45:40 -04:00
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_delay_obj, pyb_delay);
|
|
|
|
|
2017-08-21 07:34:23 -04:00
|
|
|
STATIC const mp_rom_map_elem_t pyb_module_globals_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_pyb) },
|
2015-03-28 17:45:40 -04:00
|
|
|
|
2017-08-21 07:34:23 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_millis), MP_ROM_PTR(&pyb_millis_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_elapsed_millis), MP_ROM_PTR(&pyb_elapsed_millis_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_delay), MP_ROM_PTR(&pyb_delay_obj) },
|
2015-03-28 17:45:40 -04:00
|
|
|
|
2017-08-21 07:34:23 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&pyb_led_type) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_Switch), MP_ROM_PTR(&pyb_switch_type) },
|
2015-03-28 17:45:40 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(pyb_module_globals, pyb_module_globals_table);
|
|
|
|
|
|
|
|
const mp_obj_module_t pyb_module = {
|
|
|
|
.base = { &mp_type_module },
|
2020-02-26 23:36:53 -05:00
|
|
|
.globals = (mp_obj_dict_t *)&pyb_module_globals,
|
2015-03-28 17:45:40 -04:00
|
|
|
};
|
2022-04-20 07:14:47 -04:00
|
|
|
|
2022-05-31 08:56:11 -04:00
|
|
|
MP_REGISTER_MODULE(MP_QSTR_pyb, pyb_module);
|