STATIC void mp_reset(void) { mp_stack_set_top((void*)0x40000000); mp_stack_set_limit(8192); mp_hal_init(); gc_init(heap, heap + sizeof(heap)); mp_init(); mp_obj_list_init(mp_sys_path, 0); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script) mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib)); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_)); mp_obj_list_init(mp_sys_argv, 0); #if MICROPY_VFS_FAT memset(MP_STATE_PORT(fs_user_mount), 0, sizeof(MP_STATE_PORT(fs_user_mount))); #endif MP_STATE_PORT(mp_kbd_exception) = mp_obj_new_exception(&mp_type_KeyboardInterrupt); MP_STATE_PORT(term_obj) = MP_OBJ_NULL; MP_STATE_PORT(dupterm_arr_obj) = MP_OBJ_NULL; pin_init0(); readline_init0(); dupterm_task_init(); #if MICROPY_MODULE_FROZEN pyexec_frozen_module("_boot.py"); pyexec_file("boot.py"); pyexec_file("main.py"); #endif }
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, mp_uint_t line, qstr block) { GET_NATIVE_EXCEPTION(self, self_in); #if MICROPY_ENABLE_GC if (gc_is_locked()) { if (self->traceback == MP_OBJ_NULL) { // We can't allocate any memory, and no memory has been // pre-allocated, so there is nothing else we can do. return; } mp_obj_list_t *list = self->traceback; if (list->alloc <= (list->len + 3)) { // There is some preallocated memory, but not enough to store an // entire record. return; } } #endif // for traceback, we are just using the list object for convenience, it's not really a list of Python objects if (self->traceback == MP_OBJ_NULL) { self->traceback = mp_obj_new_list(0, NULL); } mp_obj_list_append(self->traceback, (mp_obj_t)(mp_uint_t)file); mp_obj_list_append(self->traceback, (mp_obj_t)(mp_uint_t)line); mp_obj_list_append(self->traceback, (mp_obj_t)(mp_uint_t)block); }
STATIC void mp_reset(void) { mp_stack_set_top((void*)0x40000000); mp_stack_set_limit(8192); mp_hal_init(); gc_init(heap, heap + sizeof(heap)); mp_init(); mp_obj_list_init(mp_sys_path, 0); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script) mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash_slash_lib)); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash)); mp_obj_list_init(mp_sys_argv, 0); MP_STATE_PORT(term_obj) = MP_OBJ_NULL; MP_STATE_PORT(dupterm_arr_obj) = MP_OBJ_NULL; #if MICROPY_EMIT_XTENSA || MICROPY_EMIT_INLINE_XTENSA extern void esp_native_code_init(void); esp_native_code_init(); #endif pin_init0(); readline_init0(); dupterm_task_init(); #if MICROPY_MODULE_FROZEN pyexec_frozen_module("_boot.py"); pyexec_file("boot.py"); if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) { pyexec_file("main.py"); } #endif }
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block) { GET_NATIVE_EXCEPTION(self, self_in); // for traceback, we are just using the list object for convenience, it's not really a list of Python objects if (self->traceback == MP_OBJ_NULL) { self->traceback = mp_obj_new_list(0, NULL); } mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)file); mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)line); mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)block); }
STATIC void setup_sys(int argc, char **argv) { mp_obj_list_init(mp_sys_path, 0); for (int i = 0; i < sys_path_count; i++) { mp_obj_list_append(mp_sys_path, mp_obj_new_str(sys_paths[i], (mp_uint_t) strlen(sys_paths[i]), true)); } mp_obj_list_init(mp_sys_argv, 0); for (int i = 0; i < argc; i++) { mp_obj_list_append(mp_sys_argv, mp_obj_new_str(argv[i], strlen(argv[i]), true)); } }
void mp_task(void *pvParameter) { volatile uint32_t sp = (uint32_t)get_sp(); #if MICROPY_PY_THREAD mp_thread_init(&mp_task_stack[0], MP_TASK_STACK_LEN); #endif uart_init(); soft_reset: // initialise the stack pointer for the main thread mp_stack_set_top((void *)sp); mp_stack_set_limit(MP_TASK_STACK_SIZE - 1024); gc_init(mp_task_heap, mp_task_heap + sizeof(mp_task_heap)); mp_init(); mp_obj_list_init(mp_sys_path, 0); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib)); mp_obj_list_init(mp_sys_argv, 0); readline_init0(); // initialise peripherals machine_pins_init(); // run boot-up scripts pyexec_frozen_module("_boot.py"); pyexec_file("boot.py"); if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) { pyexec_file("main.py"); } for (;;) { if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) { if (pyexec_raw_repl() != 0) { break; } } else { if (pyexec_friendly_repl() != 0) { break; } } } #if MICROPY_PY_THREAD mp_thread_deinit(); #endif mp_hal_stdout_tx_str("PYB: soft reboot\r\n"); // deinitialise peripherals machine_pins_deinit(); mp_deinit(); fflush(stdout); goto soft_reset; }
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block) { // make sure self_in is an exception instance // TODO add traceback information to user-defined exceptions (need proper builtin subclassing for that) if (mp_obj_get_type(self_in)->make_new == mp_obj_exception_make_new && self_in != &mp_emergency_exception_obj) { mp_obj_exception_t *self = self_in; // for traceback, we are just using the list object for convenience, it's not really a list of Python objects if (self->traceback == MP_OBJ_NULL) { self->traceback = mp_obj_new_list(0, NULL); } mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)file); mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)line); mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)block); } }
/// \method names() /// Returns the cpu and board names for this pin. STATIC mp_obj_t pin_names(mp_obj_t self_in) { pin_obj_t *self = self_in; mp_obj_t result = mp_obj_new_list(0, NULL); mp_obj_list_append(result, MP_OBJ_NEW_QSTR(self->name)); mp_map_t *map = mp_obj_dict_get_map((mp_obj_t)&pin_board_pins_locals_dict); mp_map_elem_t *elem = map->table; for (mp_uint_t i = 0; i < map->used; i++, elem++) { if (elem->value == self) { mp_obj_list_append(result, elem->key); } } return result; }
static mp_obj_t list_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { // TODO check n_kw == 0 switch (n_args) { case 0: // return a new, empty list return mp_obj_new_list(0, NULL); case 1: { // make list from iterable mp_obj_t iterable = rt_getiter(args[0]); mp_obj_t list = mp_obj_new_list(0, NULL); mp_obj_t item; while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) { mp_obj_list_append(list, item); } return list; } default: nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "list takes at most 1 argument, %d given", (void*)(machine_int_t)n_args)); } return NULL; }
STATIC mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) { assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list)); mp_obj_list_t *self = self_in; // insert has its own strange index logic mp_int_t index = MP_OBJ_SMALL_INT_VALUE(idx); mp_int_t i; if (index < 0) { index += self->len; } if (index < 0) { index = 0; } if (index > self->len) { index = self->len; } mp_obj_list_append(self_in, mp_const_none); for (i = self->len-1; i > index; i--) { self->items[i] = self->items[i-1]; } self->items[index] = obj; return mp_const_none; }
STATIC mp_obj_t mp_builtin_dir(uint n_args, const mp_obj_t *args) { // TODO make this function more general and less of a hack mp_obj_dict_t *dict = NULL; if (n_args == 0) { // make a list of names in the local name space dict = mp_locals_get(); } else { // n_args == 1 // make a list of names in the given object if (MP_OBJ_IS_TYPE(args[0], &mp_type_module)) { dict = mp_obj_module_get_globals(args[0]); } else { mp_obj_type_t *type; if (MP_OBJ_IS_TYPE(args[0], &mp_type_type)) { type = args[0]; } else { type = mp_obj_get_type(args[0]); } if (type->locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)) { dict = type->locals_dict; } } } mp_obj_t dir = mp_obj_new_list(0, NULL); if (dict != NULL) { for (uint i = 0; i < dict->map.alloc; i++) { if (MP_MAP_SLOT_IS_FILLED(&dict->map, i)) { mp_obj_list_append(dir, dict->map.table[i].key); } } } return dir; }
/** * Return a list of all files and folders in given path. * * def listdir(path) */ static mp_obj_t os_listdir(mp_uint_t n_args, const mp_obj_t *args_p) { struct fs_dir_t dir; struct fs_dir_entry_t entry; mp_obj_t entries; mp_obj_t name; const char *path_p; if (n_args == 0) { path_p = ""; } else { path_p = mp_obj_str_get_str(args_p[0]); } if (fs_dir_open(&dir, path_p, O_READ) != 0) { nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "No such file or directory: '%s'", path_p)); } entries = mp_obj_new_list(0, NULL); while (fs_dir_read(&dir, &entry) == 1) { name = mp_obj_new_str(&entry.name[0], strlen(entry.name), false); mp_obj_list_append(entries, name); } fs_dir_close(&dir); return (entries); }
STATIC void mp_help_add_from_map(mp_obj_t list, const mp_map_t *map) { for (size_t i = 0; i < map->alloc; i++) { if (MP_MAP_SLOT_IS_FILLED(map, i)) { mp_obj_list_append(list, map->table[i].key); } } }
STATIC mp_obj_t list_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { // TODO check n_kw == 0 switch (n_args) { case 0: // return a new, empty list return mp_obj_new_list(0, NULL); case 1: { // make list from iterable mp_obj_t iterable = mp_getiter(args[0]); mp_obj_t list = mp_obj_new_list(0, NULL); mp_obj_t item; while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) { mp_obj_list_append(list, item); } return list; } default: nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "list takes at most 1 argument, %d given", n_args)); } return NULL; }
static mp_obj_t py_image_draw_keypoints(mp_obj_t image_obj, mp_obj_t kpts_obj) { image_t *image = NULL; py_kp_obj_t *kpts=NULL; /* get pointer */ image = py_image_cobj(image_obj); kpts = (py_kp_obj_t*)kpts_obj; /* sanity checks */ PY_ASSERT_TRUE_MSG(image->bpp == 1, "This function is only supported on GRAYSCALE images"); PY_ASSERT_TYPE(kpts_obj, &py_kp_type); color_t cl = {.r=0xFF, .g=0xFF, .b=0xFF}; for (int i=0; i<kpts->size; i++) { kp_t *kp = &kpts->kpts[i]; float co = arm_cos_f32(kp->angle); float si = arm_sin_f32(kp->angle); imlib_draw_line(image, kp->x, kp->y, kp->x+(co*10), kp->y+(si*10)); imlib_draw_circle(image, kp->x, kp->y, 4, &cl); } return mp_const_none; } static mp_obj_t py_image_find_blobs(mp_obj_t image_obj) { /* C stuff */ array_t *blobs; struct image *image; mp_obj_t blob_obj[6]; /* MP List */ mp_obj_t objects_list = mp_const_none; /* get image pointer */ image = py_image_cobj(image_obj); /* run color dector */ blobs = imlib_count_blobs(image); /* Create empty Python list */ objects_list = mp_obj_new_list(0, NULL); if (array_length(blobs)) { for (int j=0; j<array_length(blobs); j++) { blob_t *r = array_at(blobs, j); blob_obj[0] = mp_obj_new_int(r->x); blob_obj[1] = mp_obj_new_int(r->y); blob_obj[2] = mp_obj_new_int(r->w); blob_obj[3] = mp_obj_new_int(r->h); blob_obj[4] = mp_obj_new_int(r->c); blob_obj[5] = mp_obj_new_int(r->id); mp_obj_list_append(objects_list, mp_obj_new_tuple(6, blob_obj)); } } array_free(blobs); return objects_list; }
STATIC void adv_event_handler(mp_obj_t self_in, uint16_t event_id, ble_drv_adv_data_t * data) { ubluepy_scanner_obj_t *self = MP_OBJ_TO_PTR(self_in); ubluepy_scan_entry_obj_t * item = m_new_obj(ubluepy_scan_entry_obj_t); item->base.type = &ubluepy_scan_entry_type; vstr_t vstr; vstr_init(&vstr, 17); vstr_printf(&vstr, ""HEX2_FMT":"HEX2_FMT":"HEX2_FMT":" \ HEX2_FMT":"HEX2_FMT":"HEX2_FMT"", data->p_peer_addr[5], data->p_peer_addr[4], data->p_peer_addr[3], data->p_peer_addr[2], data->p_peer_addr[1], data->p_peer_addr[0]); item->addr = mp_obj_new_str(vstr.buf, vstr.len); vstr_clear(&vstr); item->addr_type = data->addr_type; item->rssi = data->rssi; item->data = mp_obj_new_bytearray(data->data_len, data->p_data); mp_obj_list_append(self->adv_reports, item); // Continue scanning ble_drv_scan_start(true); }
STATIC mp_obj_t list_extend_from_iter(mp_obj_t list, mp_obj_t iterable) { mp_obj_t iter = mp_getiter(iterable); mp_obj_t item; while ((item = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { mp_obj_list_append(list, item); } return list; }
STATIC void mp_help_add_from_names(mp_obj_t list, const char *name) { while (*name) { size_t l = strlen(name); // name should end in '.py' and we strip it off mp_obj_list_append(list, mp_obj_new_str(name, l - 3, false)); name += l + 1; } }
mp_obj_t py_cpufreq_get_supported_frequencies() { mp_obj_t freq_list = mp_obj_new_list(0, NULL); for (int i=0; i<ARRAY_LENGTH(cpufreq_freqs); i++) { mp_obj_list_append(freq_list, mp_obj_new_int(cpufreq_freqs[i])); } return freq_list; }
static mp_obj_t py_image_find_features(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { struct image *image = NULL; struct cascade *cascade = NULL; array_t *objects_array=NULL; mp_obj_t objects_list = mp_const_none; /* sanity checks */ PY_ASSERT_TRUE_MSG(sensor.pixformat == PIXFORMAT_GRAYSCALE, "This function is only supported on GRAYSCALE images"); PY_ASSERT_TRUE_MSG(sensor.framesize <= OMV_MAX_INT_FRAME, "This function is only supported on "OMV_MAX_INT_FRAME_STR" and smaller frames"); /* read arguments */ image = py_image_cobj(args[0]); cascade = py_cascade_cobj(args[1]); /* set some defaults */ cascade->threshold = 0.65f; cascade->scale_factor = 1.65f; /* read kw args */ mp_map_elem_t *kw_thresh = mp_map_lookup(kw_args, MP_OBJ_NEW_QSTR(qstr_from_str("threshold")), MP_MAP_LOOKUP); if (kw_thresh != NULL) { cascade->threshold = mp_obj_get_float(kw_thresh->value); } mp_map_elem_t *kw_scalef = mp_map_lookup(kw_args, MP_OBJ_NEW_QSTR(qstr_from_str("scale")), MP_MAP_LOOKUP); if (kw_scalef != NULL) { cascade->scale_factor = mp_obj_get_float(kw_scalef->value); } /* Detect objects */ objects_array = imlib_detect_objects(image, cascade); /* Create empty Python list */ objects_list = mp_obj_new_list(0, NULL); /* Add detected objects to the list */ for (int i=0; i<array_length(objects_array); i++) { struct rectangle *r = array_at(objects_array, i); mp_obj_t rec_obj[4] = { mp_obj_new_int(r->x), mp_obj_new_int(r->y), mp_obj_new_int(r->w), mp_obj_new_int(r->h), }; mp_obj_list_append(objects_list, mp_obj_new_tuple(4, rec_obj)); } /* Free the objects array */ array_free(objects_array); return objects_list; }
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block) { #if MICROPY_ENABLE_GC if (gc_is_locked()) { // We can't allocate memory, so don't bother to try return; } #endif GET_NATIVE_EXCEPTION(self, self_in); // for traceback, we are just using the list object for convenience, it's not really a list of Python objects if (self->traceback == MP_OBJ_NULL) { self->traceback = mp_obj_new_list(0, NULL); } mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)file); mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)line); mp_obj_list_append(self->traceback, (mp_obj_t)(machine_uint_t)block); }
void pyb_sleep_add (const mp_obj_t obj, WakeUpCB_t wakeup) { pyb_sleep_obj_t *sleep_obj = m_new_obj(pyb_sleep_obj_t); sleep_obj->base.type = &pyb_sleep_type; sleep_obj->obj = obj; sleep_obj->wakeup = wakeup; // remove it in case it was already registered pyb_sleep_remove (obj); mp_obj_list_append(&MP_STATE_PORT(pyb_sleep_obj_list), sleep_obj); }
/// \classmethod af_list() /// Returns an array of alternate functions available for this pin. STATIC mp_obj_t pin_af_list(mp_obj_t self_in) { pin_obj_t *self = MP_OBJ_TO_PTR(self_in); mp_obj_t result = mp_obj_new_list(0, NULL); const pin_af_obj_t *af = self->af; for (mp_uint_t i = 0; i < self->num_af; i++, af++) { mp_obj_list_append(result, MP_OBJ_FROM_PTR(af)); } return result; }
void mod_network_register_nic(mp_obj_t nic) { for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) { if (MP_STATE_PORT(mod_network_nic_list).items[i] == nic) { // nic already registered return; } } // nic not registered so add to list mp_obj_list_append(MP_OBJ_FROM_PTR(&MP_STATE_PORT(mod_network_nic_list)), nic); }
/// \classmethod af_list() /// Returns an array of alternate functions available for this pin. STATIC mp_obj_t pin_af_list(mp_obj_t self_in) { pin_obj_t *self = self_in; mp_obj_t result = mp_obj_new_list(0, NULL); const pin_af_obj_t *af = self->af; for (mp_uint_t i = 0; i < self->num_af; i++, af++) { mp_obj_list_append(result, (mp_obj_t)af); } return result; }
mp_obj_t mp_seq_extract_slice(uint len, const mp_obj_t *seq, mp_bound_slice_t *indexes) { machine_int_t start = indexes->start, stop = indexes->stop; machine_int_t step = indexes->step; mp_obj_t res = mp_obj_new_list(0, NULL); if (step < 0) { stop--; while (start <= stop) { mp_obj_list_append(res, seq[stop]); stop += step; } } else { while (start < stop) { mp_obj_list_append(res, seq[start]); start += step; } } return res; }
// TODO take an optional extra argument (what does it do exactly?) STATIC mp_obj_t stream_unbuffered_readlines(mp_obj_t self) { mp_obj_t lines = mp_obj_new_list(0, NULL); for (;;) { mp_obj_t line = stream_unbuffered_readline(1, &self); if (!mp_obj_is_true(line)) { break; } mp_obj_list_append(lines, line); } return lines; }
/****************************************************************************** DEFINE PRIVATE FUNCTIONS ******************************************************************************/ STATIC pyb_uart_obj_t* pyb_uart_add (pyb_uart_id_t uart_id) { // create a new uart object pyb_uart_obj_t *self = m_new_obj(pyb_uart_obj_t); self->base.type = &pyb_uart_type; self->uart_id = uart_id; self->read_buf = NULL; self->enabled = false; // add it to the list mp_obj_list_append(&MP_STATE_PORT(pyb_uart_list), self); return self; }
mp_obj_t mp_seq_extract_slice(size_t len, const mp_obj_t *seq, mp_bound_slice_t *indexes) { (void)len; // TODO can we remove len from the arg list? mp_int_t start = indexes->start, stop = indexes->stop; mp_int_t step = indexes->step; mp_obj_t res = mp_obj_new_list(0, NULL); if (step < 0) { while (start >= stop) { mp_obj_list_append(res, seq[start]); start += step; } } else { while (start < stop) { mp_obj_list_append(res, seq[start]); start += step; } } return res; }
STATIC mp_obj_t pin_alt_list(mp_obj_t self_in) { pin_obj_t *self = self_in; mp_obj_t af[2]; mp_obj_t afs = mp_obj_new_list(0, NULL); for (int i = 0; i < self->num_afs; i++) { af[0] = MP_OBJ_NEW_QSTR(self->af_list[i].name); af[1] = mp_obj_new_int(self->af_list[i].idx); mp_obj_list_append(afs, mp_obj_new_tuple(MP_ARRAY_SIZE(af), af)); } return afs; }