########################################
# M600 Support
########################################

[respond]

[idle_timeout]
gcode:
  {% if printer["gcode_macro M600"].m600_state == 1 %}
    RESPOND TYPE=command MSG="Stopping hotend heating..."
    M104 S0
  {% else %}
    TURN_OFF_HEATERS
    M84
  {% endif %}
timeout: 1800


[filament_switch_sensor filament_sensor]
pause_on_runout: false
switch_pin: !PC15
runout_gcode:
  M600


[gcode_macro _UNLOAD_FILAMENT]
gcode:
  M109 S{printer['gcode_macro PRINTER_PARAM'].hotend_temp|int}
  RESPOND TYPE=command MSG="Unloading filament..."
  G91
  G1 E20 F180
  G1 E-30 F180
  G1 E-50 F2000


[gcode_macro _LOAD_FILAMENT]
gcode:
  M109 S{printer['gcode_macro PRINTER_PARAM'].hotend_temp|int}
  RESPOND TYPE=command MSG="Loading filament..."
  G91
  G1 E150 F180
  G90
  M400


[gcode_macro _PURGE_MORE]
gcode:
  M109 S{printer['gcode_macro PRINTER_PARAM'].hotend_temp|int}
  RESPOND TYPE=command MSG="Purging filament..."
  G91
  G1 E10 F180
  G90
  M400


[gcode_macro M600]
description: Filament Change
variable_m600_state: 0
variable_fan0_speed: 0
gcode:
  {% set E = printer["gcode_macro PAUSE"].extrude|float %}
  {% set y_park = printer.toolhead.axis_minimum.y|float + 10.0 %}
  {% set x_park = printer.toolhead.axis_maximum.x|float - 10.0 %}
  {% set max_z = printer["gcode_macro PRINTER_PARAM"].max_z_position|float %}
  {% set act_z = printer.toolhead.position.z|float %}
  {% set z_safe = 0.0 %}
  {% if act_z < 48.0 %}
    {% set z_safe = 50.0 - act_z %}
  {% elif act_z < (max_z - 2.0) %}
    {% set z_safe = 2.0 %}
  {% elif act_z < max_z %}
    {% set z_safe = max_z - act_z %}
  {% endif %}
  SET_GCODE_VARIABLE MACRO=M600 VARIABLE=m600_state VALUE=1
  SET_GCODE_VARIABLE MACRO=PRINTER_PARAM VARIABLE=hotend_temp VALUE={printer.extruder.target}
  SET_GCODE_VARIABLE MACRO=PRINTER_PARAM VARIABLE=z_safe_pause VALUE={z_safe|float}
  RESPOND TYPE=command MSG="Print paused for filament change!"
  PAUSE_BASE
  G91
  {% if "xyz" in printer.toolhead.homed_axes %}
    {% if printer.extruder.can_extrude|lower == 'true' %}
      G1 E-{E} F2100
    {% else %}
      RESPOND TYPE=command MSG="Extruder not hot enough!"
    {% endif %}
    G1 Z{z_safe} F600
    M400
    G90
    G1 X{x_park} Y{y_park} F6000
  {% endif %}
  _UNLOAD_FILAMENT
  SET_GCODE_VARIABLE MACRO=M600 VARIABLE=fan0_speed VALUE={printer['output_pin fan0'].value}
  M106 P0 S0
  SET_IDLE_TIMEOUT TIMEOUT=900
  RESPOND TYPE=command MSG="action:prompt_begin Filament change detected!"
  RESPOND TYPE=command MSG="action:prompt_text A necessary filament change has been detected. Please replace filament, LOAD it and click RESUME button."
  RESPOND TYPE=command MSG="action:prompt_button UNLOAD FILAMENT|_UNLOAD_FILAMENT|secondary"
  RESPOND TYPE=command MSG="action:prompt_button LOAD FILAMENT|_LOAD_FILAMENT|secondary"
  RESPOND TYPE=command MSG="action:prompt_button PURGE MORE FILAMENT|_PURGE_MORE|secondary"
  RESPOND TYPE=command MSG="action:prompt_footer_button CANCEL PRINT|CANCEL_PRINT|error"
  RESPOND TYPE=command MSG="action:prompt_footer_button IGNORE|RESPOND TYPE=command MSG=action:prompt_end|warning"
  RESPOND TYPE=command MSG="action:prompt_footer_button RESUME|RESUME|primary"
  RESPOND TYPE=command MSG="action:prompt_show"


[gcode_macro RESUME]
description: Resume the current print
rename_existing: RESUME_BASE
gcode:
  {% if printer['gcode_macro PRINTER_PARAM'].hotend_temp|int != 0 %}
    {% if printer['gcode_macro PRINTER_PARAM'].hotend_temp|int > printer.extruder.temperature %}
      RESPOND TYPE=command MSG="Starting hotend heating..."
      M109 S{printer['gcode_macro PRINTER_PARAM'].hotend_temp|int}
    {% else %}
      RESPOND TYPE=command MSG="Starting hotend heating..."
      M104 S{printer['gcode_macro PRINTER_PARAM'].hotend_temp|int}
    {% endif %}
    SET_GCODE_VARIABLE MACRO=PRINTER_PARAM VARIABLE=hotend_temp VALUE=0
  {% endif %}
  {% if printer['gcode_macro M600'].m600_state != 1 %}
    {% set z_resume_move = printer['gcode_macro PRINTER_PARAM'].z_safe_pause|int %}
    {% if z_resume_move > 2 %}
      {% set z_resume_move = z_resume_move - 2 %}
      G91
      G1 Z-{z_resume_move} F600
      M400
    {% endif %}
  {% endif %}
  {% set E = printer["gcode_macro PAUSE"].extrude|float %}
  {% if 'VELOCITY' in params|upper %}
    {% set get_params = ('VELOCITY=' + params.VELOCITY) %}
  {%else %}
    {% set get_params = "" %}
  {% endif %}
  {% if printer["gcode_macro M600"].m600_state == 1 %}
    {% if printer['gcode_macro M600'].fan0_speed > 0 %}
      {% set s_value = (printer['gcode_macro M600'].fan0_speed * 255 - printer['gcode_macro PRINTER_PARAM'].fan0_min) * 255 / (255 - printer['gcode_macro PRINTER_PARAM'].fan0_min)|float %}
      M106 P0 S{s_value}
      SET_GCODE_VARIABLE MACRO=M600 VARIABLE=fan0_speed VALUE=0
    {% endif %}
    SET_GCODE_VARIABLE MACRO=M600 VARIABLE=m600_state VALUE=0
    SET_IDLE_TIMEOUT TIMEOUT=1800
    RESPOND TYPE=command MSG="action:prompt_end"
  {% else %}
    {% if printer.extruder.can_extrude|lower == 'true' %}
      G91
      G1 E{E} F2100
      G90
      M400
    {% else %}
      RESPOND TYPE=command MSG="Extruder not hot enough!"
    {% endif %}
  {% endif %}
  RESPOND TYPE=command MSG="Resuming printing..."
  RESUME_BASE {get_params}
