# Copyright (C) 2022 Justin Schuh <code@justinschuh.com>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
[gcode_macro m300]
description: Emits and audible beep.
  Usage: M300 [P<duration>] [S<frequency>]
gcode:
  {% set settings = printer.configfile.settings %}
  {% if "output_pin beeper" in printer or "pwm_cycle_time beeper" in printer %}
    {% set P = (params.P|default(100)|int, 0)|max %}
    {% set S = (params.S|default(1000)|int, 1)|max %}
    SET_PIN PIN=beeper VALUE={% if "output_pin beeper" in settings %}1{%else%}{
        settings["pwm_cycle_time beeper"].scale|default(1.0) * 0.5
      } CYCLE_TIME={ 1.0 / S }{% endif %}
    G4 P{P}
    SET_PIN PIN=beeper VALUE=0
  {% else %}
    {action_respond_info(
       "M300 is disabled. To enable create an [pwm_cycle_time beeper] config.")}
  {% endif %}

[gcode_macro _km_beep_if_available]
description: Wraps beeper to avoid console spam
gcode:
  {% if "output_pin beeper" in printer or "pwm_cycle_time beeper" in printer %}
    {% for i in range(params.BEEPS|int) %}
      M300 P100
      G4 P200
    {% endfor %}
  {% endif %}
