小黑屋|Klipper玩客 ( 桂ICP备13004039号-7 )

GMT+8, 2024-6-24 13:44

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

Trinus探索篇:Klipper中LED的配置

605
0
0
0
文一 LV2
正文
发布时间:2023-04-17
本帖最后由 文一 于 2023-4-17 14:42 编辑

之所以没直接放在基础篇里说这个话题,主要是这个也是在摸索,不想给各位新手添加负担。
首先我们来温故一下,klipper官方文档中怎样对LED进行定义的,因为只涉及到单色LED,所以只需要看这一段即可:
  1. [led]¶
  2. 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.


  3. [led my_led]
  4. #red_pin:
  5. #green_pin:
  6. #blue_pin:
  7. #white_pin:
  8. #   控制给定LED的引脚。
  9. #   至少提供一个上述引脚
  10. #cycle_time: 0.010
  11. #   每个PWM周期的时间(以秒为单位)
  12. #   使用软件PWM时建议大于10毫秒
  13. #   默认值为0.010秒
  14. #hardware_pwm: False
  15. #   启用硬件PWM代替软件PWM
  16. #   当使用硬件PWM时,真正的PWM周期时间受执行的约束,
  17. #   可能显著地与请求的周期时间不同。
  18. #   默认为False
  19. #initial_RED: 0.0
  20. #initial_GREEN: 0.0
  21. #initial_BLUE: 0.0
  22. #initial_WHITE: 0.0
  23. #   设置初始化时LED颜色
  24. #   每个值应当在0.0与1.0之间
  25. #   每个颜色的默认值都为0
复制代码


这段表面上很有道理,实际上,我们因为是单色LED,可能只能如此定义:

  1. [led my_led]
  2. red_pin:!PC4
  3. cycle_time: 0.010
  4. initial_RED: 1.0
复制代码

一个简单的led配置好了。可以在网页端多了一个my_led的选项,不过这个也太简陋了吧,不急,咱们再看看怎么操作指令。
  1. [led]¶
  2. The following command is available when any of the led config sections are enabled.

  3. SET_LED¶
  4. 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.

  5. SET_LED_TEMPLATE¶
  6. 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).

  7. [output_pin]¶
  8. The following command is available when an output_pin config section is enabled.

  9. SET_PIN¶
  10. 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.

  11. 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、明暗配置
  1. [led my_led]
  2. red_pin:!PC4
  3. initial_RED: 1.0

  4. [gcode_macro LEDOFF]
  5. gcode:  SET_LED LED=my_led RED=0  

  6. [gcode_macro LEDMIN]
  7. gcode:  SET_LED LED=my_led RED=0.1

  8. [gcode_macro LEDMAX]
  9. gcode:  SET_LED LED=my_led RED=1
复制代码




2、闪烁配置
  1. [output_pin LED_pin]
  2. pin:!PC4
  3. pwm: True
  4. value: 0

  5. [gcode_macro LEDOFF]
  6. gcode:  SET_PIN PIN=LED_pin VALUE=0

  7. [gcode_macro LEDMIN]
  8. gcode:  SET_PIN PIN=LED_pin VALUE=0.5 CYCLE_TIME=2

  9. [gcode_macro LEDMAX]
  10. gcode:  SET_PIN PIN=LED_pin VALUE=1 CYCLE_TIME=5
复制代码

完成宏的定义后,保存为T_led.cfg,
然后在macro文件里调用定义的宏,分别在归零、调平等状态显示不同的LED效果。

以上为一个简单的配置示例,可能代码比较简单,供各位自己探索。

回复

 
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表