3D打印实战:自定义模块并调用C语言

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值