####################################################################
# Macros used for printing
# - BRUSHIE
# - CANCEL_PRINT
# - M600
# - M900
# - PARK
# - PAUSE
# - PRINT_START
# - PRINT_END
# - PURGE_NOZZLE
# - RESUME
####################################################################


# BRUSHIE
[gcode_macro BRUSHIE]
gcode:
  {% set x0=250 %}
  {% set x1=200 %}
  {% set y0=309 %}
  {% set z0=1.5 %}
  {% set z_hop = printer['gcode_macro _USER_VARIABLE'].z_hop|int %}
  {% if "xyz" in printer.toolhead.homed_axes %}
    M118 brushie brushie brushie
    G0 Z{z_hop} F1000           # move Z to travel height
    G0 X{x0} Y{y0} F6000        # move to x0/y0
    G0 Z{z0}                    # lower
    G0 X{x1}                    # 1
    G0 X{x0}                    # 2
    G0 X{x1}                    # 3
    G0 Z{z_hop} F300            # move Z to travel height
  {% else %}
    M118 Printer not homed
  {% endif %}


# # - CANCEL_PRINT
[gcode_macro CANCEL_PRINT]
description: Cancel the actual running print
rename_existing: _CANCEL_PRINT_BASE
gcode:
  SET_FILAMENT_SENSOR SENSOR=encoder_sensor ENABLE=0
  CLEAR_PAUSE
  SDCARD_RESET_FILE
  TURN_OFF_HEATERS
  PARK
  _CANCEL_PRINT_BASE


[gcode_macro M600]
description: Change filament
gcode:
  SAVE_GCODE_STATE NAME=M600_state
  PAUSE Y=15
  M118 M600 change filament
  RESTORE_GCODE_STATE NAME=M600_state

[gcode_macro M900]
description: Set pressure advance
gcode:
  SET_PRESSURE_ADVANCE ADVANCE={params.K|default(0)}


# PARK
[gcode_macro PARK]
gcode:
  {% set Y=params.Y|default(295) %}
  {% if "xyz" in printer.toolhead.homed_axes %}
    # set park positon for x and y
    ## close to max
    {% set x_park = printer.toolhead.axis_maximum.x|float - 5.0 %}
    {% set y_park = printer.toolhead.axis_maximum.y|float - 5.0 %}
    {% if Y != y_park %}
      {% set y_park = Y %}
    {% endif %}
    # calculate save lift position
    {% set z_max = printer.toolhead.axis_maximum.z|float %}
    {% set z_act = printer.toolhead.position.z|float %}
    {% set z_safe = z_act + 10 %}
    {% if z_safe > z_max %}
        {% set z_safe = z_max %}
    {% endif %}
    G91
    G0 Z{z_safe} F1200
    G90
    G0 X{x_park} Y{y_park} F6000
  {% else %}
    M118 Printer not homed
  {% endif %}


# - PAUSE
[gcode_macro PAUSE]
description: Pause the actual running print
rename_existing: _PAUSE_BASE
# change this if you need more or less extrusion
variable_extrude: 1.0
gcode:
  {% set YMIN = printer.toolhead.axis_minimum.y|float + 5.0 %}
  {% set Y=params.Y|default(YMIN) %}
  # read E from pause macro
  {% set E = printer["gcode_macro PAUSE"].extrude|float %}
  # end of definitions
  M118 pause
  _PAUSE_BASE
  {% if printer.extruder.can_extrude|lower == 'true' %}
    G91
    G1 E-{E} F2100
    G90
  {% else %}
    M118 Extruder not hot enough
  {% endif %}


# - PRINT_START
[gcode_macro PRINT_START]
gcode:
  {% set BED=params.BED|default(100)|int %}
  {% set EXTRUDER=params.EXTRUDER|default(250)|int %}
  {% set CHAMBER=params.CHAMBER|default(0)|int %}
  {% set SHEET=params.SHEET|default("default")|string %}
  {% set QGL=params.QGL|default(1)|int %}
  {% set BMC=params.BMC|default(0)|int %}
  {% set PURGE=params.PURGE|default(1)|int %}
  {% set SOAK=params.SOAK|default(0)|int * 1000 * 60 %}
  {% set Z_ADJUST=params.Z_ADJUST|default(0.0)|float %}
  {% set ERCF=params.ERCF|default(0)|int %}
  {% set ERCF_EXTRUDER=params.ERCF_EXTRUDER|default(0)|int %}
  {action_respond_info("starting print BED=%d, EXTRUDER=%d, CHAMBER=%d, SHEET=%s, QGL=%d, BMC=%d, PURGE=%d, SOAK=%d, Z_AJUST=%f, ERCF=%d, ERCF_EXTRUDER=%d" % (BED, EXTRUDER, CHAMBER, SHEET, QGL, BMC, PURGE, SOAK, Z_ADJUST, ERCF, ERCF_EXTRUDER))}
  M118 configuring
  SET_GCODE_OFFSET Z=0.0  # reset z offset
  M140 S{BED}             # start bed heating
  M104 S{EXTRUDER}        # start extruder heating
  G92 E0                  # reset extruder
  G21                     # set units to millimeters
  G90                     # use absolute coordinates
  M83                     # use relative distances for extrusion

  G4 P2000
  {% if BMC %}
    BED_MESH_CLEAR
  {% endif %}
  M118 home
  G28
  BRUSHIE
  {% if QGL %}
    M118 qgl
    QUAD_GANTRY_LEVEL PARK=false
    BRUSHIE
  {% endif %}
  M118 calibrate z
  CALIBRATE_Z
  {% if BMC %}
    M118 bed mesh calibrate
    BED_MESH_CALIBRATE
  {% else %}
    M118 bed mesh load {SHEET}
    BED_MESH_PROFILE LOAD={SHEET}
  {% endif %}

  M118 heating
  G4 P2000
  G92 E0                 # reset extruder
  M190 S{BED}            # set and wait for bed temperature
  M109 S{EXTRUDER}       # set and wait for nozzle temperature
  TEMPERATURE_WAIT SENSOR="temperature_sensor chamber" MINIMUM={CHAMBER}  # wait for chamber temp
  {% if SOAK > 0 %}
    M118 soaking for {SOAK/1000/60|int} min
    G4 P{SOAK}
  {% endif %}

  M118 starting
  SET_GCODE_OFFSET Z_ADJUST={params.Z_ADJUST|default(0.0)|float} MOVE=1
  {% if ERCF %}
    M118 ercf changing to tool {ERCF_EXTRUDER}
    ERCF_CHANGE_TOOL_SLICER TOOL={ERCF_EXTRUDER}
  {% endif %}
  BRUSHIE
  {% if PURGE %}
    PURGE_NOZZLE
  {% endif %}


# - PRINT_END
[gcode_macro PRINT_END]
gcode:
  {% set UNLOAD_AT_END=params.UNLOAD_AT_END|default(0)|int %}
  M118 finished
  PARK
  M400                          ; wait for buffer to clear
  G92 E0                        ; zero the extruder
  G1 E-10.0 F3600               ; retract filament
  {% if UNLOAD_AT_END == 1 %}
    ERCF_EJECT
  {% endif %}
  TURN_OFF_HEATERS
  M107                          ; turn off fan


# - PURGE_NOZZLE
[gcode_macro PURGE_NOZZLE]
gcode:
  {% set x0=params.x0|default(100) %}
  {% set x1=params.x1|default(200) %}
  {% set y0=params.y0|default(2) %}
  {% set y1=params.y1|default(3) %}
  {% set z_hop = printer['gcode_macro _USER_VARIABLE'].z_hop|int %}
  M118 purge nozzle
  G0 Z{z_hop} F300            # move Z to travel height
  G0 X{x0} Y{y0} F5000        # move to x0/y0
  G0 Z0.24 F300               # lower Z
  G0 X{x1} E20 F1500          # draw line
  G0 Y{y1}                    # move to y1
  G0 X{x0} E10                # draw fine line
  G0 X{x0-10}                 # move a little further
  G0 E-3                      # retract filament
  G0 Z{z_hop} F300            # move Z to travel height


# - RESUME
[gcode_macro RESUME]
description: Resume the actual running print
rename_existing: _RESUME_BASE
gcode:
  {% if printer["gcode_macro ERCF_PAUSE"].is_paused|int != 0 %}
    M118 You can't resume the print without unlocking the ERCF first.
    M118 Run ERCF_UNLOCK and solve any issue before hitting Resume again
  {% else %}
    # read E from pause macro
    {% set E = printer["gcode_macro PAUSE"].extrude|float %}
    # get VELOCITY parameter if specified
    {% if 'VELOCITY' in params|upper %}
      {% set get_params = ('VELOCITY=' + params.VELOCITY)  %}
    {%else %}
      {% set get_params = "" %}
    {% endif %}
    # end of definitions
    M118 resume
    {% if printer.extruder.can_extrude|lower == 'true' %}
      G91
      G1 E{E} F6000
      G90
    {% else %}
      M118 Extruder not hot enough
    {% endif %}
    {% if printer["gcode_macro ERCF_VAR"].clog_detection|int == 1 %}
        SET_FILAMENT_SENSOR SENSOR=encoder_sensor ENABLE=1
    {% endif %}
    M118 calling _RESUME_BASE
    G4 P2000
    _RESUME_BASE
  {% endif %}


[gcode_macro PRINT_LAYER_CHANGE]
gcode:
  {% set layer=params.LAYER|default(0)|int %}
  {% set layer_z=params.LAYER_Z|default(0) %}
  {% set total_layer_count=params.TOTAL_LAYER_COUNT|default(0) %}
  M117 > layer {layer+1}/{total_layer_count} {layer_z}mm