chelper/hello.c
#include <stdio.h>
#include "compiler.h"
int __visible hello_fun(int value)
{
printf("hello world %d\n", value);
return 0;
}
chelper/init.py
添加hello.c到c_helper.so
如果c_helper.so不存在,则会生成。所以要删掉c_helper.so生成新的
SOURCE_FILES = [
'hello.c',
]
defs_hello = """
int hello_fun(int value);
"""
defs_all = [
defs_hello
]
extras/hello.py
import chelper
class hello_class(object):
def __init__(self, config):
self.printer = config.get_printer()
self.gcode = self.printer.lookup_object("gcode")
# 注册HELLO命令
self.gcode.register_command("HELLO", self.cmd_HELLO, desc=self.cmd_HELLO_help)
# 从printer.cfg配置的hello中获取参数
self.a = config.getfloat("a", 0, minval=0.0, maxval=100)
def hello_fun(self, value):
print("hello_fun")
# 通过ffi获取c方法
ffi_main, ffi_lib = chelper.get_ffi()
ffi_lib.hello_fun(value)
cmd_HELLO_help = "Send a command to the hello for debugging"
def cmd_HELLO(self, gcmd):
# 获取HELLO命令的参数
value = gcmd.get("VALUE", None)
if value is None:
value = 0
else:
value = int(value)
gcmd.respond_info("cmd_HELLO_help: value=%s" % value)
self.hello_fun(value)
# 如果printer.cfg中配置有hello模块,则加载hello模块
def load_config(config):
return hello_class(config)
so
删除
/home/lhgcs/klipper/klippy/chelper/c_helper.so
运行klipper重新生成so