0
点赞
收藏
分享

微信扫一扫

RT_Thread添加自己的模块到MicroPython中

倪雅各 2022-03-30 阅读 33

如何在rt_thread的micropython中添加自己的模块?

现在想在micropython中添加我的ecat模块,具体步骤如下:

以自己编写的modecat.c为例:

编写一个.c代码,存放在modules目录下

代码可参考:

#include "py/mpconfig.h"
#if MICROPY_PY_ECAT

#include <stdio.h>
#include <stdint.h>
#include <string.h>

#include "py/objlist.h"
#include "py/runtime.h"
#include "py/mphal.h"
#include "lib/netutils/netutils.h"


//利用官网的C语言代码生成器生成代码
STATIC mp_obj_t cheng(
        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 +1;

    /* Your code end! */

    return mp_obj_new_int(ret_val);
}
MP_DEFINE_CONST_FUN_OBJ_2(cheng_obj, cheng);

//模块注册表
STATIC const mp_rom_map_elem_t mp_module_ecat_globals_table[] = {
    { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ecat) },
    { MP_ROM_QSTR(MP_QSTR_cheng), MP_ROM_PTR(&cheng_obj) },
};
STATIC MP_DEFINE_CONST_DICT(mp_module_ecat_globals, mp_module_ecat_globals_table);

//定义模块
const mp_obj_module_t mp_module_ecat = {
    .base = { &mp_type_module },
    .globals = (mp_obj_dict_t*)&mp_module_ecat_globals,
};


#endif // MICROPY_PY_ECAT

将函数和模块名添加到qstrdefs.generated.h文件后面

在mpconfigport.h相应位置定义MICROPY_PY_ECAT(如果你的.c代码中有使用MICROPY_PY_ECAT)

在mpconfigport.h中添加ECAT模块

 

 修改完这些文件编译烧录后,在串口输入python指令:

可以看到模块和函数都调用成功。 

 

举报

相关推荐

0 条评论