stmhal: Add time module with sleep function.

This commit is contained in:
Damien George 2014-03-22 15:06:29 +00:00
parent ad7b84a7b9
commit d311655655
5 changed files with 53 additions and 1 deletions

View File

@ -74,6 +74,7 @@ SRC_C = \
pyexec.c \ pyexec.c \
pybmodule.c \ pybmodule.c \
osmodule.c \ osmodule.c \
timemodule.c \
import.c \ import.c \
lexerfatfs.c \ lexerfatfs.c \
gpio.c \ gpio.c \

View File

@ -23,6 +23,7 @@
#include "pyexec.h" #include "pyexec.h"
#include "pybmodule.h" #include "pybmodule.h"
#include "osmodule.h" #include "osmodule.h"
#include "timemodule.h"
#include "usart.h" #include "usart.h"
#include "led.h" #include "led.h"
#include "exti.h" #include "exti.h"
@ -276,9 +277,10 @@ soft_reset:
// probably shouldn't do this, so we are compatible with CPython // probably shouldn't do this, so we are compatible with CPython
rt_store_name(MP_QSTR_pyb, (mp_obj_t)&pyb_module); rt_store_name(MP_QSTR_pyb, (mp_obj_t)&pyb_module);
// pre-import the os module // pre-import the os and time modules
// TODO don't do this! (need a way of registering builtin modules...) // TODO don't do this! (need a way of registering builtin modules...)
rt_store_name(MP_QSTR_os, (mp_obj_t)&os_module); rt_store_name(MP_QSTR_os, (mp_obj_t)&os_module);
rt_store_name(MP_QSTR_time, (mp_obj_t)&time_module);
// check if user switch held (initiates reset of filesystem) // check if user switch held (initiates reset of filesystem)
bool reset_filesystem = false; bool reset_filesystem = false;

View File

@ -66,3 +66,7 @@ Q(rmdir)
Q(unlink) Q(unlink)
Q(sep) Q(sep)
Q(urandom) Q(urandom)
// for time module
Q(time)
Q(sleep)

44
stmhal/timemodule.c Normal file
View File

@ -0,0 +1,44 @@
#include <stm32f4xx_hal.h>
#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
#include "map.h"
#include "timemodule.h"
STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) {
#if MICROPY_ENABLE_FLOAT
if (MP_OBJ_IS_INT(seconds_o)) {
#endif
HAL_Delay(1000 * mp_obj_get_int(seconds_o));
#if MICROPY_ENABLE_FLOAT
} else {
HAL_Delay((uint32_t)(1000 * mp_obj_get_float(seconds_o)));
}
#endif
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep);
STATIC const mp_map_elem_t time_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_time) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep), (mp_obj_t)&time_sleep_obj },
};
STATIC const mp_map_t time_module_globals = {
.all_keys_are_qstrs = 1,
.table_is_fixed_array = 1,
.used = sizeof(time_module_globals_table) / sizeof(mp_map_elem_t),
.alloc = sizeof(time_module_globals_table) / sizeof(mp_map_elem_t),
.table = (mp_map_elem_t*)time_module_globals_table,
};
const mp_obj_module_t time_module = {
.base = { &mp_type_module },
.name = MP_QSTR_time,
.globals = (mp_map_t*)&time_module_globals,
};

1
stmhal/timemodule.h Normal file
View File

@ -0,0 +1 @@
extern const mp_obj_module_t time_module;