Motion lights outside the G-code queue (extras/motion_lights.py)

The AM312 case light and nozzle LEDs waited for long commands (heat waits,
AFC loads, QGL) because [gcode_button] runs through the G-code queue. The
new module reads the pin itself, keeps the timeout on a reactor timer and
sets the case light with output_pin's send_async_request and the nozzle
LEDs directly on the led_effect object. CASELIGHT and _LIGHTS_ON keep
working; _STATUS_SHOW reads printer.motion_lights.near.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mangavalkandClaude Opus 5.5 committed 2026-11-02 00:35:26 +01:00
1 parent 55b04a7cef
commit f25f348d0b
4 files changed
+269 -100

No files matched your search

+21 -97
View File
@@ -1,8 +1,12 @@
# PIR motion sensor (AM312): case light and nozzle LEDs while someone is near
#
# The AM312 keeps its output on as long as it sees movement: press = motion,
# release = no more motion. The timeout starts at the release, so the lights
# stay on while someone works at the printer.
# Handled by extras/motion_lights.py, outside the G-code queue: the lights
# react at once, also during heat waits, AFC loads and other long commands
# (with [gcode_button] they waited for those to finish).
#
# The AM312 keeps its output on as long as it sees movement. The timeout
# starts when the motion stops, so the lights stay on while someone works at
# the printer.
#
# someone near: case light near_value, nozzle LEDs white
# nobody near, print/pause: case light print_value, nozzle LEDs off
@@ -10,99 +14,19 @@
# logo and Box Turtle logo dim (_STATUS ready)
#
# CASELIGHT MODE=ON [VALUE=0.5] | OFF | AUTO overrides the sensor until the
# next Klipper restart. Setting the case light in Mainsail only lasts until
# the next motion.
# next Klipper restart; CASELIGHT alone shows the state. _LIGHTS_ON turns the
# lights on as if someone walked by (print start and end, resume).
# MOTION_LIGHTS_DEBUG ENABLE=1 reports every change in the console. Setting
# the case light in Mainsail lasts until the next change.
[gcode_button am312]
[motion_lights]
pin: !PC0
press_gcode:
_MOTION_START
release_gcode:
_MOTION_END
[gcode_macro _LIGHTS]
description: Set the case light and nozzle LEDs from motion, print state and CASELIGHT mode
variable_timeout: 60 # s without motion before the lights dim or turn off
variable_near_value: 1.0 # case light while someone is near
variable_print_value: 0.1 # case light while printing or paused, nobody near
variable_idle_value: 0.0 # case light while idle, nobody near
variable_on_value: 1.0 # case light for CASELIGHT MODE=ON
variable_mode: 'auto' # auto (motion sensor), on, off: set by CASELIGHT
variable_present: False # someone near: set by the motion sensor
variable_near: False # lights on (present or MODE=ON): read by _STATUS
variable_debug: False # True: report every change in the console
gcode:
{% set busy = printer.print_stats.state in ['printing', 'paused'] %}
{% if mode == 'on' %}
{% set lit = True %}
{% set value = on_value %}
{% elif mode == 'off' %}
{% set lit = False %}
{% set value = 0.0 %}
{% else %}
{% set lit = present %}
{% set value = near_value if present else (print_value if busy else idle_value) %}
{% endif %}
{% if debug %}
RESPOND MSG="Lights: mode={mode} present={present} print={printer.print_stats.state}: case light {value}, nozzle {'on' if lit else 'off'}"
{% endif %}
{% if printer['output_pin caselight'].value != value %}
SET_PIN PIN=caselight VALUE={value}
{% endif %}
{% if printer['led_effect sb_nozzle_light'].enabled != lit %}
SET_LED_EFFECT EFFECT=sb_nozzle_light {'' if lit else 'STOP=1'} FADETIME={printer['gcode_macro _STATUS'].fade_time}
{% endif %}
{% if near != lit %}
SET_GCODE_VARIABLE MACRO=_LIGHTS VARIABLE=near VALUE={lit}
# Night mode of the logo when idle
_STATUS
{% endif %}
[gcode_macro _MOTION_START]
description: Motion sensor: someone is near
gcode:
UPDATE_DELAYED_GCODE ID=_motion_timeout DURATION=0
{% if not printer['gcode_macro _LIGHTS'].present %}
SET_GCODE_VARIABLE MACRO=_LIGHTS VARIABLE=present VALUE=True
_LIGHTS
{% endif %}
[gcode_macro _MOTION_END]
description: Motion sensor: no more motion, lights off after the timeout
gcode:
UPDATE_DELAYED_GCODE ID=_motion_timeout DURATION={printer['gcode_macro _LIGHTS'].timeout}
[delayed_gcode _motion_timeout]
gcode:
SET_GCODE_VARIABLE MACRO=_LIGHTS VARIABLE=present VALUE=False
_LIGHTS
[gcode_macro _LIGHTS_ON]
description: Lights on as if someone walked by (print start and end, startup)
gcode:
_MOTION_START
{% if printer['gcode_button am312'].state == 'RELEASED' %}
_MOTION_END
{% endif %}
[delayed_gcode _lights_startup]
initial_duration: 2
gcode:
_LIGHTS_ON
[gcode_macro CASELIGHT]
description: Case light and nozzle LEDs: MODE=AUTO (motion sensor), ON [VALUE=0-1] or OFF; no MODE: show the mode
gcode:
{% set l = printer['gcode_macro _LIGHTS'] %}
{% set mode = params.MODE|default('')|lower %}
{% if mode == '' %}
RESPOND MSG="Case light: mode {l.mode}, {'someone near' if l.present else 'nobody near'}, value {printer['output_pin caselight'].value}"
{% elif mode not in ['auto', 'on', 'off'] %}
_ERROR MSG="CASELIGHT: MODE must be AUTO, ON or OFF"
{% else %}
{% if 'VALUE' in params %}
SET_GCODE_VARIABLE MACRO=_LIGHTS VARIABLE=on_value VALUE={[[params.VALUE|float, 0.0]|max, 1.0]|min}
{% endif %}
SET_GCODE_VARIABLE MACRO=_LIGHTS VARIABLE=mode VALUE="'{mode}'"
_LIGHTS
{% endif %}
light: caselight
nozzle_effect: sb_nozzle_light
timeout: 60 # s without motion before the lights dim or turn off
near_value: 1.0 # case light while someone is near
print_value: 0.1 # case light while printing or paused, nobody near
idle_value: 0.0 # case light while idle, nobody near
on_value: 1.0 # case light for CASELIGHT MODE=ON
fade_time: 0.5 # nozzle LED fade (s), same as _STATUS fade_time
status_gcode: _STATUS # night mode of the logo when nobody is near