# Copyright (C) 2022 Justin Schuh <code@justinschuh.com>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
#
# Credit to original source:
# https://gist.github.com/ChipCE/95fdbd3c2f3a064397f9610f915f7d02

[gcode_macro bed_mesh_calibrate]
rename_existing: _KM_BED_MESH_CALIBRATE_BASE
description: Wraps BED_MESH_CALIBRATE, scaling probe count to specified area.
  Usage: See Klipper documentation.
gcode:
  {% set km = printer["gcode_macro _km_globals"] %}
  {% set probe_mesh_padding = km.probe_mesh_padding %}
  {% set probe_min_count = km.probe_min_count %}
  {% set probe_count_scale = km.probe_count_scale %}
  {% set bed_mesh = printer.configfile.config.bed_mesh %}

  # don't have the math functions available to work on a delta bed.
  {%if "mesh_radius" not in bed_mesh %}
    {% set safe_min_x = bed_mesh.mesh_min.split(",")[0]|float %}
    {% set safe_min_y = bed_mesh.mesh_min.split(",")[1]|float %}
    {% set safe_max_x = bed_mesh.mesh_max.split(",")[0]|float %}
    {% set safe_max_y = bed_mesh.mesh_max.split(",")[1]|float %}

    {% if "MESH_MIN" in params %}
      {% set mesh_min_x = (params.MESH_MIN.split(",")[0]|float -
                           probe_mesh_padding, safe_min_x)|max %}
      {% set mesh_min_y = (params.MESH_MIN.split(",")[1]|float -
                           probe_mesh_padding, safe_min_y)|max %}
    {% else %}
      {% set mesh_min_x = safe_min_x %}
      {% set mesh_min_y = safe_min_y %}
    {% endif %}
    {% if "MESH_MAX" in params %}
      {% set mesh_max_x = (params.MESH_MAX.split(",")[0]|float +
                           probe_mesh_padding, safe_max_x)|min %}
      {% set mesh_max_y = (params.MESH_MAX.split(",")[1]|float +
                           probe_mesh_padding, safe_max_y)|min %}
    {% else %}
      {% set mesh_max_x = safe_max_x %}
      {% set mesh_max_y = safe_max_y %}
    {% endif %}

    {% set max_x_probes = (params.PROBE_COUNT |
                           default(bed_mesh.probe_count)).split(",")[0]|int %}
    {% set max_y_probes = (params.PROBE_COUNT |
                           default(bed_mesh.probe_count)).split(",")[1]|int %}

    {% set x_probes = (max_x_probes * (mesh_max_x - mesh_min_x) /
                      (safe_max_x - safe_min_x) * probe_count_scale)
                      | round(0) | int %}
    {% set x_probes = ((x_probes, probe_min_count)|max, max_x_probes)|min %}

    {% set y_probes = (max_y_probes * (mesh_max_y - mesh_min_y ) /
                      (safe_max_y - safe_min_y) * probe_count_scale )
                      | round(0) | int %}
    {% set y_probes = ((y_probes, probe_min_count)|max, max_y_probes)|min %}

    {% set dummy = params.__setitem__("MESH_MIN", mesh_min_x~","~mesh_min_y) %}
    {% set dummy = params.__setitem__("MESH_MAX", mesh_max_x~","~mesh_max_y) %}
    {% set dummy = params.__setitem__("PROBE_COUNT", x_probes~","~y_probes) %}
  {% endif %}
  _km_bed_mesh_calibrate_base{%for k in params%}{
      ' '~k~'="'~params[k]~'"'}{%endfor%}

[gcode_macro g29]
gcode:
  BED_MESH_CALIBRATE