From 5837c2ebd239edcb615dd3dd4400a66617a8054b Mon Sep 17 00:00:00 2001 From: SummerGift Date: Fri, 13 Dec 2019 15:15:45 +0800 Subject: [PATCH] [add] expose userfunc module to micropython --- port/genhdr/qstrdefs.generated.h | 2 +- port/modules/moduserfunc.c | 57 ++++++++++++++++++++++++++++++++ port/mpconfigport.h | 4 +++ 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 port/modules/moduserfunc.c diff --git a/port/genhdr/qstrdefs.generated.h b/port/genhdr/qstrdefs.generated.h index 6b1a56e..426a2ce 100644 --- a/port/genhdr/qstrdefs.generated.h +++ b/port/genhdr/qstrdefs.generated.h @@ -769,4 +769,4 @@ QDEF(MP_QSTR_IRQ, (const byte*)"\xaf\x03" "IRQ") QDEF(MP_QSTR_IRQ_HIGH_LEVEL, (const byte*)"\x57\x0e" "IRQ_HIGH_LEVEL") QDEF(MP_QSTR_IRQ_LOW_LEVEL, (const byte*)"\x8d\x0d" "IRQ_LOW_LEVEL") QDEF(MP_QSTR_show_bmp, (const byte*)"\xe6\x08" "show_bmp") -// This file was automatically generated by makeqstrdata.py +QDEF(MP_QSTR_userfunc, (const byte*)"\x4a\x08" "userfunc") diff --git a/port/modules/moduserfunc.c b/port/modules/moduserfunc.c new file mode 100644 index 0000000..5405592 --- /dev/null +++ b/port/modules/moduserfunc.c @@ -0,0 +1,57 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 SummerGift + * + * 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 "py/runtime.h" + +STATIC mp_obj_t add( + mp_obj_t arg_1_obj, + mp_obj_t arg_2_obj) { + mp_int_t arg_1 = mp_obj_get_int(arg_1_obj); + mp_int_t arg_2 = mp_obj_get_int(arg_2_obj); + mp_int_t ret_val; + + /* Your code start! */ + + ret_val = arg_1 + arg_2; + + /* Your code end! */ + + return mp_obj_new_int(ret_val); +} +MP_DEFINE_CONST_FUN_OBJ_2(add_obj, add); + +STATIC const mp_rom_map_elem_t mp_module_userfunc_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_userfunc) }, + { MP_ROM_QSTR(MP_QSTR_add), MP_ROM_PTR(&add_obj) }, +}; + +STATIC MP_DEFINE_CONST_DICT(mp_module_userfunc_globals, mp_module_userfunc_globals_table); + +const mp_obj_module_t mp_module_userfunc = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&mp_module_userfunc_globals, +}; + diff --git a/port/mpconfigport.h b/port/mpconfigport.h index 3610549..d1369c7 100644 --- a/port/mpconfigport.h +++ b/port/mpconfigport.h @@ -354,6 +354,7 @@ extern const struct _mp_obj_module_t mp_module_io; extern const struct _mp_obj_fun_builtin_fixed_t machine_soft_reset_obj; extern const struct _mp_obj_module_t mp_module_ffi; extern const struct _mp_obj_module_t mp_module_network; +extern const struct _mp_obj_module_t mp_module_userfunc; #if MICROPY_PY_RTTHREAD #define RTTHREAD_PORT_BUILTIN_MODULES { MP_ROM_QSTR(MP_QSTR_rtthread), MP_ROM_PTR(&mp_module_rtthread) }, @@ -478,6 +479,8 @@ extern const struct _mp_obj_module_t mp_module_network; #define MODNETWORK_PORT_BUILTIN_MODULES #endif +#define USERFUNC_PORT_BUILTIN_MODULES { MP_ROM_QSTR(MP_QSTR_userfunc), MP_ROM_PTR(&mp_module_userfunc) }, + // extra built in names to add to the global namespace #define MICROPY_PORT_BUILTINS \ { MP_ROM_QSTR(MP_QSTR_exit), MP_ROM_PTR(&machine_soft_reset_obj) }, \ @@ -493,6 +496,7 @@ extern const struct _mp_obj_module_t mp_module_network; MODUTIME_PORT_BUILTIN_MODULES \ MODFFI_PORT_BUILTIN_MODULES \ MODNETWORK_PORT_BUILTIN_MODULES \ + USERFUNC_PORT_BUILTIN_MODULES \ #define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \ MODUTIME_PORT_BUILTIN_MODULE_WEAK_LINKS \