Files
klipper-macros/macros/sensors/motion_sensor.cfg
T

108 lines
4.2 KiB
INI
Raw Normal View History

# 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.
#
# someone near: case light near_value, nozzle LEDs white
# nobody near, print/pause: case light print_value, nozzle LEDs off
# nobody near, idle: case light idle_value, nozzle LEDs off,
# 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.
[gcode_button am312]
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 %}