本帖最后由 文一 于 2023-4-17 14:42 编辑
之所以没直接放在基础篇里说这个话题,主要是这个也是在摸索,不想给各位新手添加负担。
首先我们来温故一下,klipper官方文档中怎样对LED进行定义的,因为只涉及到单色LED,所以只需要看这一段即可:
- [led]¶
- Support for LEDs (and LED strips) controlled via micro-controller PWM pins (one may define any number of sections with an "led" prefix). See the command reference for more information.
- [led my_led]
- #red_pin:
- #green_pin:
- #blue_pin:
- #white_pin:
- # 控制给定LED的引脚。
- # 至少提供一个上述引脚
- #cycle_time: 0.010
- # 每个PWM周期的时间(以秒为单位)
- # 使用软件PWM时建议大于10毫秒
- # 默认值为0.010秒
- #hardware_pwm: False
- # 启用硬件PWM代替软件PWM
- # 当使用硬件PWM时,真正的PWM周期时间受执行的约束,
- # 可能显著地与请求的周期时间不同。
- # 默认为False
- #initial_RED: 0.0
- #initial_GREEN: 0.0
- #initial_BLUE: 0.0
- #initial_WHITE: 0.0
- # 设置初始化时LED颜色
- # 每个值应当在0.0与1.0之间
- # 每个颜色的默认值都为0
复制代码
这段表面上很有道理,实际上,我们因为是单色LED,可能只能如此定义:
- [led my_led]
- red_pin:!PC4
- cycle_time: 0.010
- initial_RED: 1.0
复制代码
一个简单的led配置好了。可以在网页端多了一个my_led的选项,不过这个也太简陋了吧,不急,咱们再看看怎么操作指令。
- [led]¶
- The following command is available when any of the led config sections are enabled.
- SET_LED¶
- SET_LED LED=<config_name> RED=<value> GREEN=<value> BLUE=<value> WHITE=<value> [INDEX=<index>] [TRANSMIT=0] [SYNC=1]: This sets the LED output. Each color <value> must be between 0.0 and 1.0. The WHITE option is only valid on RGBW LEDs. If the LED supports multiple chips in a daisy-chain then one may specify INDEX to alter the color of just the given chip (1 for the first chip, 2 for the second, etc.). If INDEX is not provided then all LEDs in the daisy-chain will be set to the provided color. If TRANSMIT=0 is specified then the color change will only be made on the next SET_LED command that does not specify TRANSMIT=0; this may be useful in combination with the INDEX parameter to batch multiple updates in a daisy-chain. By default, the SET_LED command will sync it's changes with other ongoing gcode commands. This can lead to undesirable behavior if LEDs are being set while the printer is not printing as it will reset the idle timeout. If careful timing is not needed, the optional SYNC=0 parameter can be specified to apply the changes without resetting the idle timeout.
- SET_LED_TEMPLATE¶
- SET_LED_TEMPLATE LED=<led_name> TEMPLATE=<template_name> [<param_x>=<literal>] [INDEX=<index>]: Assign a display_template to a given LED. For example, if one defined a [display_template my_led_template] config section then one could assign TEMPLATE=my_led_template here. The display_template should produce a comma separated string containing four floating point numbers corresponding to red, green, blue, and white color settings. The template will be continuously evaluated and the LED will be automatically set to the resulting colors. One may set display_template parameters to use during template evaluation (parameters will be parsed as Python literals). If INDEX is not specified then all chips in the LED's daisy-chain will be set to the template, otherwise only the chip with the given index will be updated. If TEMPLATE is an empty string then this command will clear any previous template assigned to the LED (one can then use SET_LED commands to manage the LED's color settings).
- [output_pin]¶
- The following command is available when an output_pin config section is enabled.
- SET_PIN¶
- SET_PIN PIN=config_name VALUE=<value> [CYCLE_TIME=<cycle_time>]: Set the pin to the given output VALUE. VALUE should be 0 or 1 for "digital" output pins. For PWM pins, set to a value between 0.0 and 1.0, or between 0.0 and scale if a scale is configured in the output_pin config section.
- Some pins (currently only "soft PWM" pins) support setting an explicit cycle time using the CYCLE_TIME parameter (specified in seconds). Note that the CYCLE_TIME parameter is not stored between SET_PIN commands (any SET_PIN command without an explicit CYCLE_TIME parameter will use the cycle_time specified in the output_pin config section).
复制代码
那么实际上我们就有了两种配置方式::分别是明暗配置和闪烁配置。
1、明暗配置
- [led my_led]
- red_pin:!PC4
- initial_RED: 1.0
- [gcode_macro LEDOFF]
- gcode: SET_LED LED=my_led RED=0
- [gcode_macro LEDMIN]
- gcode: SET_LED LED=my_led RED=0.1
- [gcode_macro LEDMAX]
- gcode: SET_LED LED=my_led RED=1
复制代码
2、闪烁配置
- [output_pin LED_pin]
- pin:!PC4
- pwm: True
- value: 0
- [gcode_macro LEDOFF]
- gcode: SET_PIN PIN=LED_pin VALUE=0
- [gcode_macro LEDMIN]
- gcode: SET_PIN PIN=LED_pin VALUE=0.5 CYCLE_TIME=2
- [gcode_macro LEDMAX]
- gcode: SET_PIN PIN=LED_pin VALUE=1 CYCLE_TIME=5
复制代码
完成宏的定义后,保存为T_led.cfg,
然后在macro文件里调用定义的宏,分别在归零、调平等状态显示不同的LED效果。
以上为一个简单的配置示例,可能代码比较简单,供各位自己探索。
|
|