|
| 1 | +#include <stdio.h> |
| 2 | + |
| 3 | +#include "py/obj.h" |
| 4 | +#include "py/objstr.h" |
| 5 | +#include "py/runtime.h" |
| 6 | +#include "py/mphal.h" |
| 7 | +#include "syscall.h" |
| 8 | +#include "msgpack.h" |
| 9 | +#include "machine.h" |
| 10 | +#include "lib/mpack/mpack.h" |
| 11 | + |
| 12 | +#include <stdio.h> |
| 13 | +#include <string.h> |
| 14 | + |
| 15 | + |
| 16 | +mp_obj_t wrap_result(int retcode); |
| 17 | + |
| 18 | + |
| 19 | +STATIC mp_obj_t ucomponents_components(size_t n_args, const mp_obj_t *args) { |
| 20 | + if (n_args == 0) { |
| 21 | + return wrap_result(__syscall2(SYS_COMPONENTS_LIST, 0, 0)); |
| 22 | + } else { |
| 23 | + size_t len = 0; |
| 24 | + const char *buf = mp_obj_str_get_data(args[0], &len); |
| 25 | + return wrap_result(__syscall2(SYS_COMPONENTS_LIST, (int) buf, (int) len)); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ucomponents_components_obj, 0, 1, ucomponents_components); |
| 30 | + |
| 31 | + |
| 32 | +STATIC mp_obj_t ucomponents_component_count() { |
| 33 | + return wrap_result(__syscall0(SYS_COMPONENTS_COUNT)); |
| 34 | +} |
| 35 | + |
| 36 | +MP_DEFINE_CONST_FUN_OBJ_0(ucomponents_component_count_obj, ucomponents_component_count); |
| 37 | + |
| 38 | + |
| 39 | +STATIC mp_obj_t ucomponents_max_components() { |
| 40 | + return wrap_result(__syscall0(SYS_COMPONENTS_MAX)); |
| 41 | +} |
| 42 | + |
| 43 | +MP_DEFINE_CONST_FUN_OBJ_0(ucomponents_max_components_obj, ucomponents_max_components); |
| 44 | + |
| 45 | + |
| 46 | +STATIC mp_obj_t ucomponents_methods(mp_obj_t address_obj) { |
| 47 | + byte *data = NULL; |
| 48 | + size_t size = 0; |
| 49 | + |
| 50 | + mpack_writer_t *writer = msgpack_dump_new(&data, &size); |
| 51 | + mpack_start_array(writer, 1); |
| 52 | + msgpack_dump(writer, address_obj); |
| 53 | + mpack_finish_array(writer); |
| 54 | + msgpack_dump_close(writer); |
| 55 | + |
| 56 | + return wrap_result(__syscall2(SYS_COMPONENTS_METHODS, (int) data, (int) size)); |
| 57 | +} |
| 58 | + |
| 59 | +MP_DEFINE_CONST_FUN_OBJ_1(ucomponents_methods_obj, ucomponents_methods); |
| 60 | + |
| 61 | + |
| 62 | +STATIC mp_obj_t ucomponents_annotations(mp_obj_t address_obj, mp_obj_t method_obj) { |
| 63 | + byte *data = NULL; |
| 64 | + size_t size = 0; |
| 65 | + |
| 66 | + mpack_writer_t *writer = msgpack_dump_new(&data, &size); |
| 67 | + mpack_start_array(writer, 2); |
| 68 | + msgpack_dump(writer, address_obj); |
| 69 | + msgpack_dump(writer, method_obj); |
| 70 | + mpack_finish_array(writer); |
| 71 | + msgpack_dump_close(writer); |
| 72 | + |
| 73 | + return wrap_result(__syscall2(SYS_COMPONENTS_ANNOTATIONS, (int) data, (int) size)); |
| 74 | +} |
| 75 | + |
| 76 | +MP_DEFINE_CONST_FUN_OBJ_2(ucomponents_annotations_obj, ucomponents_annotations); |
| 77 | + |
| 78 | + |
| 79 | +STATIC mp_obj_t ucomponents_invoke(size_t n_args, const mp_obj_t *args) { |
| 80 | + byte *data = NULL; |
| 81 | + size_t size = 0; |
| 82 | + |
| 83 | + mpack_writer_t *writer = msgpack_dump_new(&data, &size); |
| 84 | + mpack_start_array(writer, n_args); |
| 85 | + for (int i = 0; i < n_args; i++) |
| 86 | + msgpack_dump(writer, args[i]); |
| 87 | + mpack_finish_array(writer); |
| 88 | + msgpack_dump_close(writer); |
| 89 | + |
| 90 | + mp_obj_t values = wrap_result(__syscall2(SYS_COMPONENTS_INVOKE, (int) data, (int) size)); |
| 91 | + if (values == mp_const_none) |
| 92 | + return mp_const_none; |
| 93 | + |
| 94 | + size_t count = 0; |
| 95 | + mp_obj_t *items = NULL; |
| 96 | + mp_obj_get_array(values, &count, &items); |
| 97 | + if (count == 0) |
| 98 | + return mp_const_none; |
| 99 | + else if (count == 1) |
| 100 | + return items[0]; |
| 101 | + else |
| 102 | + return values; |
| 103 | +} |
| 104 | + |
| 105 | +MP_DEFINE_CONST_FUN_OBJ_VAR(ucomponents_invoke_obj, 2, ucomponents_invoke); |
| 106 | + |
| 107 | + |
| 108 | +STATIC const mp_rom_map_elem_t ucomponents_module_globals_table[] = { |
| 109 | + {MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ucomponents)}, |
| 110 | + |
| 111 | + // components |
| 112 | + {MP_ROM_QSTR(MP_QSTR_components), MP_ROM_PTR(&ucomponents_components_obj)}, |
| 113 | + {MP_ROM_QSTR(MP_QSTR_component_count), MP_ROM_PTR(&ucomponents_component_count_obj)}, |
| 114 | + {MP_ROM_QSTR(MP_QSTR_max_components), MP_ROM_PTR(&ucomponents_max_components_obj)}, |
| 115 | + {MP_ROM_QSTR(MP_QSTR_methods), MP_ROM_PTR(&ucomponents_methods_obj)}, |
| 116 | + {MP_ROM_QSTR(MP_QSTR_annotations), MP_ROM_PTR(&ucomponents_annotations_obj)}, |
| 117 | + |
| 118 | + // invoke |
| 119 | + {MP_ROM_QSTR(MP_QSTR_invoke), MP_ROM_PTR(&ucomponents_invoke_obj)}, |
| 120 | +}; |
| 121 | + |
| 122 | +STATIC MP_DEFINE_CONST_DICT(ucomponents_module_globals, ucomponents_module_globals_table); |
| 123 | + |
| 124 | +const mp_obj_module_t mp_module_ucomponents = { |
| 125 | + .base = {&mp_type_module}, |
| 126 | + .globals = (mp_obj_dict_t *) &ucomponents_module_globals, |
| 127 | +}; |
0 commit comments