# Copyright (C) 2022 Justin Schuh <code@justinschuh.com>
#
# This file may be distributed under the terms of the GNU GPLv3 license.

[gcode_macro m201]
description: Sets maximum accelleration.
  Usage: M201 [X<accel>] [Y<accel>]
variable_max_accel: 1.7976931348623157e+308
gcode:
  {% set km = printer["gcode_macro _km_globals"] %}
  {% if 'X' in params or 'Y' in params %}
    {% set accel = (params.X|default(params.Y)|float,
                    params.Y|default(params.X)|float)|min %}
    SET_GCODE_VARIABLE MACRO=m201 VARIABLE=max_accel VALUE="{accel}"
  {% else %}
    SET_VELOCITY_LIMIT
  {% endif %}

[gcode_macro m203]
description: Sets maximum velocity.
  Usage: M203 [X<velocity>] [Y<velocity>]
gcode:
  {% if 'X' in params or 'Y' in params %}
    {% set velocity = (params.X|default(params.Y)|float,
                       params.Y|default(params.X)|float)|min %}
    SET_VELOCITY_LIMIT VELOCITY="{velocity}"
  {% else %}
    SET_VELOCITY_LIMIT
  {% endif %}

[gcode_macro m205]
description: Sets square corner velocity.
  Usage: M203 [X<velocity>] [Y<velocity>]
gcode:
  {% if 'X' in params or 'Y' in params %}
    SET_VELOCITY_LIMIT SQUARE_CORNER_VELOCITY="{
      (params.X|default(0)|float, params.Y|default(0)|float)|min}"
  {% else %}
    SET_VELOCITY_LIMIT
  {% endif %}

[gcode_macro m900]
description: Sets pressure advance.
  Usage: M900 [K<advance>] [T<extruder_index>]
gcode:
  {% set km = printer["gcode_macro _km_globals"] %}
  {% if km.pressure_advance_scale > 0.0 %}
    {% set extruder = "extruder" ~ params.T|replace('0', '')
       if "T" in params else printer.toolhead.extruder %}
    {% if 'K' in params %}
      SET_PRESSURE_ADVANCE EXTRUDER="{extruder}" ADVANCE="{
        params.K|float * km.pressure_advance_scale}"
    {% endif %}
  {% endif %}


[gcode_macro _reset_velocity_limits]
description: Sets maximum accelleration.
  Usage: M204 [S<accel>] [P<accel> T<accel>]
gcode:
  SET_GCODE_VARIABLE MACRO=m201 VARIABLE=max_accel VALUE="{1.7976931348623157e+308}"
