Created
September 3, 2016 00:50
-
-
Save mianos/3cfa911aaf4eb19070153c7bd0e9d371 to your computer and use it in GitHub Desktop.
os_task python module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "os_type.h" | |
#include "user_interface.h" | |
#include "ets_alt_task.h" | |
#include "os_task.h" | |
STATIC os_event_t sensor_evt_queue[16]; | |
STATIC ICACHE_FLASH_ATTR void esp_os_task_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { | |
esp_os_task_obj_t *self = self_in; | |
mp_printf(print, "os_task(callback=%x)", (unsigned int)self->callback); | |
} | |
STATIC ICACHE_FLASH_ATTR void task_common_callback(os_event_t *evt) { | |
esp_os_task_obj_t *self = (esp_os_task_obj_t *)evt->par; | |
if (self->callback != mp_const_none) { | |
nlr_buf_t nlr; | |
if (nlr_push(&nlr) == 0) { | |
(void)mp_call_function_1(self->callback, self); | |
} else { | |
printf("exception\n"); | |
mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val); | |
} | |
//gc_collect(); | |
} | |
printf("out\n"); | |
} | |
void ICACHE_FLASH_ATTR esp_os_task_init() { | |
ets_task(task_common_callback, SENSOR_TASK_ID, sensor_evt_queue, sizeof(sensor_evt_queue) / sizeof(*sensor_evt_queue)); | |
} | |
STATIC const mp_arg_t esp_os_task_init_args[] = { | |
{MP_QSTR_callback, MP_ARG_OBJ, {.u_obj = mp_const_none}}, | |
}; | |
#define PYB_TASK_INIT_NUM_ARGS MP_ARRAY_SIZE(esp_os_task_init_args) | |
STATIC ICACHE_FLASH_ATTR mp_obj_t esp_os_task_make_new(const mp_obj_type_t *type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { | |
mp_arg_val_t vals[PYB_TASK_INIT_NUM_ARGS]; | |
mp_arg_parse_all_kw_array(n_args, n_kw, args, PYB_TASK_INIT_NUM_ARGS, esp_os_task_init_args, vals); | |
esp_os_task_obj_t *self = m_new_obj(esp_os_task_obj_t); | |
self->base.type = &esp_os_task_type; | |
self->callback = vals[0].u_obj; | |
return (mp_obj_t)self; | |
} | |
STATIC ICACHE_FLASH_ATTR mp_obj_t esp_os_task_post(mp_obj_t self_in) { | |
esp_os_task_obj_t *self = self_in; | |
ets_post(SENSOR_TASK_ID, 1, (os_param_t)self); | |
return mp_const_none; | |
} | |
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_os_task_post_obj, esp_os_task_post); | |
STATIC mp_obj_t esp_os_task_handler(mp_obj_t self_in, mp_obj_t lambda_in) { | |
esp_os_task_obj_t *self = self_in; | |
self->callback = lambda_in; | |
return mp_const_none; | |
} | |
STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp_os_task_handler_obj, esp_os_task_handler); | |
STATIC const mp_map_elem_t esp_os_task_locals_dict_table[] = { | |
{ MP_OBJ_NEW_QSTR(MP_QSTR_post), (mp_obj_t)&esp_os_task_post_obj }, | |
{ MP_OBJ_NEW_QSTR(MP_QSTR_handler), (mp_obj_t)&esp_os_task_handler_obj }, | |
}; | |
STATIC MP_DEFINE_CONST_DICT(esp_os_task_locals_dict, esp_os_task_locals_dict_table); | |
const mp_obj_type_t esp_os_task_type = { | |
{ &mp_type_type }, | |
.name = MP_QSTR_os_task, | |
.print = esp_os_task_print, | |
.make_new = esp_os_task_make_new, | |
.locals_dict = (mp_obj_t)&esp_os_task_locals_dict, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment