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

[gcode_macro _check_kinematic_limits]
gcode:
  {% set toolhead = printer.toolhead %}
  {% if params.X and (params.X|float < toolhead.axis_minimum.x or
                      params.X|float > toolhead.axis_maximum.x) %}
    {action_raise_error("X[%.3f] must be between %.3f and %.3f."
                        | format(params.X|float, toolhead.axis_minimum.x,
                                 toolhead.axis_maximum.x))}
  {% elif params.Y and (params.Y|float < toolhead.axis_minimum.y or
                        params.Y|float > toolhead.axis_maximum.y) %}
    {action_raise_error("Y[%.3f] must be between %.3f and %.3f."
                        | format(params.Y|float, toolhead.axis_minimum.y,
                                 toolhead.axis_maximum.y))}
  {% elif params.Z and (params.Z|float < toolhead.axis_minimum.z or
                        params.Z|float > toolhead.axis_maximum.z) %}
    {action_raise_error("Z[%.3f] must be between %.3f and %.3f."
                        | format(params.Z|float, toolhead.axis_minimum.z,
                                 toolhead.axis_maximum.z))}
  {% elif params.E and (params.E|float|abs > printer.configfile.settings[
                          "extruder"].max_extrude_only_distance) %}
    {action_raise_error("E[%.4f] exceeds max_extrude_only_distance[%.4f]."
       | format(params.E|float|abs, printer.configfile.settings[
                                      "extruder"].max_extrude_only_distance))}
  {% endif %}

[gcode_macro lazy_home]
description: Homes the specified axes. If lazy is true, already homed axes
  are skipped.
  Usage: LAZY_HOME [LAZY=<1|0>] [AXES=<axes_string>]
gcode:
  # This is split apart so we can force the macro rename cache to be ready.
  LIST_MACROS SILENT=1
  _LAZY_HOME_INNER {rawparams}

[gcode_macro _lazy_home_inner]
gcode:
  # Find the real g28 command.
  {% set G28 = (printer["gcode_macro list_macros"].macros.g28|
                  default(["g28"],True))[-1] %}
  {% set axes = 'XYZ'|select('in', params.AXES|default("XYZ")|upper|list) %}
  {% if not axes %} # No axes means home everything.
    {% set axes = 'XYZ' %}
  {% endif %}
  {% if params.LAZY|default(1)|int %} # Prune out the already homed axes.
    {% set axes = axes|reject('in', printer.toolhead.homed_axes|upper)|join() %}
  {% endif %}

  {% if axes %}
    _KM_PRINT_STATUS ACTION=PUSH_STATUS
    _KM_PRINT_STATUS ACTION=CHANGE STATUS=homing
    {G28}{% for k in axes %}{' ' ~ k}{% endfor %}
    _KM_PRINT_STATUS ACTION=CHANGE STATUS=pop_status
  {% endif %}

# Met deze onderstaande codeblok wordt de printkop voor elke homing 2 mm omhoog gezet, of de printer weet wat de Z positie is of niet. https://klipper.discourse.group/t/moving-z-before-homing-or-other/1037/4
[force_move]
# Enable commands that force potentially unsafe movement
enable_force_move: True

[gcode_macro UNSAFE_LOWER_BED]
description: Lift Z 2mm without homing
gcode:
  G90
  SET_KINEMATIC_POSITION Z=0
  G0 Z2 F600
  M84

[gcode_macro g28]
description: Wraps the G28 command to add the Marlin "O" parameter so that
  already homed axes will not be homed again. See the Klipper documentation on
  G28 for the behavior of the other parameters.
  Usage: G28 [O] ...
rename_existing: G28.6245197
gcode:
  UNSAFE_LOWER_BED
  {% set axes = 'XYZ'|select('in', params)|join() %}
  LAZY_HOME LAZY={('O' in params)|int}{%if axes%} AXES={axes}{%endif%}
