[gcode_macro START_PRINT]
description: start printing
gcode:
  {% set BED_TEMP = params.BED_TEMP|default(60)|float %}
  {% set EXTRUDER_TEMP = params.EXTRUDER_TEMP|default(190)|float %}
  M117 start heating
  M140 S{BED_TEMP}            # start bed heating
  M104 S{EXTRUDER_TEMP}       # start extruder heating
  NEOPIXEL_ON COLOR=green     # set neopixel green
  G92 E0                      # reset extruder
  G21                         # set units to millimeters
  G90                         # use absolute coordinates
  M83                         # use relative distances for extrusion
  SET_GCODE_OFFSET Z=0.0      # reset the G-Code Z offset
  M117 home
  G28                         # home the printer
  G1 Z5 F3000                 # move the nozzle near the bed
  G1 Z0.25 F300               # move the nozzle very close to the bed
  M117 wait for heating
  NEOPIXEL_ON COLOR=red       # set neopixel red
  G92 E0                      # reset extruder
  M190 S{BED_TEMP}            # set and wait for bed temperature
  M109 S{EXTRUDER_TEMP}       # set and wait for nozzle temperature
  # clean nozzle
  NEOPIXEL_ON COLOR=blue     # set neopixel blue
  M117 clean nozzle
  G0 Z5 F300                  # move Z to travel height
  G0 X40 Y0 F5000             # move to start position
  G0 Z0.2 F1500               # lower Z
  G0 X150 Y0 Z0.2 E10         # draw line
  G0 X150 Y0.8 Z0.2           # move to the side little
  G0 X30 Y0.8 Z0.2 E5         # draw fine line
  # G0 Z5 F300                  # move Z to travel height
  NEOPIXEL_ON COLOR=white     # set neopixel white
  M117 starting

[gcode_macro END_PRINT]
description: end printing
gcode:
  M140 S0                     # Turn off bed
  M104 S0                     # Turn off extruder
  M106 S0                     # Turn off fan
  G91                         # Use relativ coordinates
  G1 X-2 Y-2 E-3 F300         # Move nozzle away from print while retracting
  G1 Z10 F3000                # Raise nozzle by 10mm
  G90                         # Use absolute coordinates
  M84                         # Disable steppers
  NEOPIXEL_OFF                # Turn off NeoPixel

[gcode_macro PREHEAT]
description: set bed and extruder temperature
gcode:
  {% set BED_TEMP = params.BED_TEMP|default(0)|float %}
  {% set EXTRUDER_TEMP = params.EXTRUDER_TEMP|default(190)|float %}
  M117 start heating
  M140 S{BED_TEMP}            # start bed heating
  M104 S{EXTRUDER_TEMP}       # start extruder heating
  M109 S{EXTRUDER_TEMP}       # set and wait for nozzle temperature
  M117 done

[gcode_macro PAUSE]
description: Pause the actual running print
rename_existing: PAUSE_BASE
gcode:
  {% set x = params.X|default(170) %}
  {% set y = params.Y|default(170) %}
  {% set z = params.Z|default(10)|float %}
  {% set e = params.E|default(1) %}
  ## calculate save lift position
  {% set max_z = printer.toolhead.axis_maximum.z|float %}
  {% set act_z = printer.toolhead.position.z|float %}
  {% set lift_z = z|abs %}
  {% if act_z < (max_z - lift_z) %}
    {% set z_safe = lift_z %}
  {% else %}
    {% set z_safe = max_z - act_z %}
  {% endif %}
  PAUSE_BASE
  G91
  {% if printer.extruder.can_extrude|lower == 'true' %}
    G1 E-{e} F2100
  {% else %}
    {action_respond_info("Extruder not hot enough")}
  {% endif %}
  {% if "xyz" in printer.toolhead.homed_axes %}
    G1 Z{z_safe}
    G90
    G1 X{x} Y{y} F6000
  {% else %}
    {action_respond_info("Printer not homed")}
  {% endif %}

[gcode_macro RESUME]
description: Resume the actual running print
rename_existing: RESUME_BASE
gcode:
  {% set e = params.E|default(1) %}
  {% if 'VELOCITY' in params|upper %}
    {% set get_params = ('VELOCITY=' + params.VELOCITY)  %}
  {%else %}
    {% set get_params = "" %}
  {% endif %}
  G91
  {% if printer.extruder.can_extrude|lower == 'true' %}
    G1 E{e} F2100
  {% else %}
    {action_respond_info("Extruder not hot enough")}
  {% endif %}
  RESUME_BASE {get_params}

[gcode_macro CANCEL_PRINT]
description: Cancel the actual running print
rename_existing: CANCEL_PRINT_BASE
gcode:
  TURN_OFF_HEATERS
  CANCEL_PRINT_BASE

[gcode_macro FILAMENT_UNLOAD]
description: unload filament
gcode:
  {% if printer.extruder.can_extrude|lower == 'true' %}
    M83                       # e relative coordinates
    G0 E-370 F3000            # unload
    M82                       # e absolute coordinates
  {% else %}
    {action_respond_info("extruder not hot enough")}
  {% endif %}

[gcode_macro FILAMENT_LOAD]
description: load filament
gcode:
  {% if printer.extruder.can_extrude|lower == 'true' %}
    M83                       # e relative coordinates
    G0 E350 F3000
    G0 E30 F300
    M82                       # e absolute coordinates
  {% else %}
    {action_respond_info("extruder not hot enough")}
  {% endif %}

[gcode_macro PURGE]
description: purge filament
gcode:
  {% set e = params.e|default(10)|float %}
  {% if printer.extruder.can_extrude|lower == 'true' %}
    M83                       # e relative coordinates
    G1 E-{e} F300
    M82                       # e absolute coordinates
  {% else %}
    {action_respond_info("extruder not hot enough")}
  {% endif %}


# [gcode_macro POWER_ON]
# description: power on the printer
# gcode:
#   {action_call_remote_method("set_device_power", device="prusa", state="on")}

# [gcode_macro POWER_OFF]
# description: power off the printer
# gcode:
#   {action_call_remote_method("set_device_power", device="prusa", state="off")}

[gcode_macro M600]
description: Change filament
gcode:
  SAVE_GCODE_STATE NAME=M600_state
  PAUSE
  UNLOAD
  M117 change filament
  RESTORE_GCODE_STATE NAME=M600_state

[gcode_macro G29]
description: Bed Leveling
gcode:
  BED_MESH_CALIBRATE

[gcode_macro BED_MESH_CALIBRATE]
description: Bed Leveling
rename_existing: BED_MESH_CALIBRATE_BASE
gcode:
  {% if "xyz" not in printer.toolhead.homed_axes %}
    M117 home
    G28
  {% endif %}
  M117 calibrate
  BED_MESH_CALIBRATE_BASE
  M84                         # Disable steppers



## Remove unused gcodes
[gcode_macro M201]
description: Set Print Max Acceleration
gcode:
  G4

[gcode_macro M203]
description: Set Max Feedrate
gcode:
  G4

[gcode_macro M205]
description: Set Advanced Settings
gcode:
  G4

[gcode_macro M900]
description: Linear Advance Factor
gcode:
  G4