[gcode_macro bed_mesh_calibrate_fast]
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, and
  # not sure how to make relative reference index work.
  {%if "mesh_radius" not in bed_mesh and
       "MESH_RADIUS" not in params and
       "relative_reference_index" not in bed_mesh and
       "RELATIVE_REFERENCE_INDEX" not in params %}
    {% 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 %}

    # Always bound MESH_MIN and MESH_MAX.
    {% 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 probe_count = (params.PROBE_COUNT |
                          default(bed_mesh.probe_count)).split(",") %}
    # Don't scale the probe count if one was explicitly provided.
    {% if "PROBE_COUNT" not in params %}
        {% set max_x_probes = probe_count[0]|int %}
        {% set max_y_probes = probe_count[1]|default(max_x_probes)|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 %}
        # Add probes for bicubic if one axis has too many probes for lagrange.
        {% if x_probes > 6 and y_probes < 4 %}
          {% set y_probes = 4 %}
        {% elif y_probes > 6 and x_probes < 4 %}
          {% set x_probes = 4 %}
        {% endif %}
        {% set probe_count = [x_probes,y_probes] %}
    {% endif %}
    {% 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", probe_count|join(',')) %}
    # Force bicubic if we've exceeded the max for lagrange.
    {% if probe_count[0]|int > 6 or probe_count[1]|default(0)|int > 6 %}
      {% set dummy = params.__setitem__("ALGORITHM", "bicubic") %}
    {% endif %}
  {% endif %}
  {% if printer["gcode_macro bed_mesh_calibrate"].km_override|default(False) %}
    {% set calibrate_cmd = "_km_bed_mesh_calibrate_base" %}
  {% else %}
    {% set calibrate_cmd = "BED_MESH_CALIBRATE" %}
  {% endif %}
  BED_MESH_CHECK
  {calibrate_cmd}{%for k in params%}{' '~k~'="'~params[k]~'"'}{%endfor%}

[gcode_macro bed_mesh_check]
description: Warns if bed_mesh config may generate an invalid mesh.
  Usage: See Klipper documentation.
gcode:
  {% if printer.bed_mesh is defined %}
    {% set x_min = printer.configfile.settings.stepper_x.position_min %}
    {% set y_min = printer.configfile.settings.stepper_y.position_min %}
    {% set x_max = printer.configfile.settings.stepper_x.position_max %}
    {% set y_max = printer.configfile.settings.stepper_y.position_max %}

    {% set mesh_min_x = printer.configfile.settings.bed_mesh.mesh_min[0] %}
    {% set mesh_min_y = printer.configfile.settings.bed_mesh.mesh_min[1] %}
    {% set mesh_max_x = printer.configfile.settings.bed_mesh.mesh_max[0] %}
    {% set mesh_max_y = printer.configfile.settings.bed_mesh.mesh_max[1] %}

    {% if "bltouch" in printer.configfile.settings %}
        {% set x_offset = printer.configfile.settings.bltouch.x_offset %}
        {% set y_offset = printer.configfile.settings.bltouch.y_offset %}
        {% set probe = "bltouch" %}
    {% elif "probe" in printer.configfile.settings %}
        {% set x_offset = printer.configfile.settings.probe.x_offset %}
        {% set y_offset = printer.configfile.settings.probe.y_offset %}
        {% set probe = "probe" %}
    {% else %}
        {% set x_offset = 0.0 %}
        {% set y_offset = 0.0 %}
    {% endif %}

    {% set output = [] %}
    {% set warn =
         "* bed_mesh.mesh_%s (%f, %f) does not account for " ~ probe ~
         ".%s_offset (%f) and can move out of range for "
         "stepper_%s.position_%s (%f)." %}
    {% if x_offset > 0 and (mesh_min_x - x_offset) < x_min %}
      {% set dummy = output.append(warn % ('min', mesh_min_x, mesh_min_y,
                                           'x', x_offset, 'x', 'min', x_min)) %}
    {% elif x_offset < 0 and (mesh_max_x - x_offset) > x_max %}
      {% set dummy = output.append(warn % ('max', mesh_max_x, mesh_max_y,
                                           'x', x_offset, 'x', 'max', x_max)) %}
    {% endif %}
    {% if y_offset > 0 and (mesh_min_y - y_offset) < y_min %}
      {% set dummy = output.append(warn % ('min', mesh_min_x, mesh_min_y,
                                           'y', y_offset, 'y', 'min', y_min)) %}
    {% elif y_offset < 0 and (mesh_max_y - y_offset) > y_max %}
      {% set dummy = output.append(warn % ('max', mesh_max_x, mesh_max_y,
                                           'y', y_offset, 'y', 'max', y_max)) %}
    {% endif %}

    {% if output %}
      { action_respond_info(
        "Warning: The following issue(s) were detected in your [bed_mesh] "
        " config:\n" ~ output|join("\n")) }
    {% endif %}
  {% endif %}
