[gcode_macro START_PRINT]
gcode:
  #Get Bed and Extruder temperature from Slicer GCode
  {% set BED_TEMP = params.BED_TEMP|default(60)|float %}
  {% set EXTRUDER_TEMP = params.EXTRUDER_TEMP|default(200)|float %}

  CLEAR_PAUSE
  G90

  ELED_ON

  M117 Heatup Bed {BED_TEMP} degres Nozzle {EXTRUDER_TEMP} degres

  SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={BED_TEMP}  
  TEMPERATURE_WAIT SENSOR=heater_bed MINIMUM={BED_TEMP*0.96} MAXIMUM={BED_TEMP*1.04}
  SET_HEATER_TEMPERATURE HEATER=extruder TARGET={EXTRUDER_TEMP}  
  
  G28

  BED_MESH_PROFILE LOAD=default

  G1 Z10 F3000 ; move z up little to prevent scratching of surface
  G1 X2 Y2 Z10 F5000.0 ; move to start-line position

  TEMPERATURE_WAIT SENSOR=extruder MINIMUM={EXTRUDER_TEMP*0.98} MAXIMUM={EXTRUDER_TEMP*1.02}

  PRIME_LINE

  M117

[gcode_macro END_PRINT]
gcode:
  ELED_OFF

  M117 End print
  TURN_OFF_HEATERS
  
  M117 Present print
  PRESENT_PRINT

  M117
  M84 

[gcode_macro PRIME_LINE]
gcode: 
  SAVE_GCODE_STATE NAME=prime_line_state

  #Precondition extruder
  G92 E0 ; reset extruder
  G1 X2 Y20 Z1.0 F4000.0 ; move to start-line position
  G1 X2 Y20 Z0.3 F300.0 ; move z up little to prevent scratching of surface
  G1 X2 Y200.0 Z0.3 F1500.0 E15 ; draw 1st line
  G1 X2 Y200.0 Z0.4 F5000.0 ; move to side a little
  G1 X2 Y20 Z0.4 F1500.0 E30 ; draw 2nd line
  G92 E0 ; reset extruder
  G1 Z1.0 F300 ; move z up little to prevent scratching of surface

  RESTORE_GCODE_STATE NAME=prime_line_state

[gcode_macro PRESENT_PRINT]
gcode:
  SAVE_GCODE_STATE NAME=present_print_state

  {% set X_MAX = printer.toolhead.axis_maximum.x|float %}
  {% set Y_MAX = printer.toolhead.axis_maximum.y|float %}
  {% set Z_MAX = printer.toolhead.axis_maximum.z|float %}
  {% set Z_PARK_MIN = 100 | float %}
  {% set Z_PARK_LIFT = 10 | float %}

  # Move nozzle away from print while retracting
  G91
  G1 E-3 F300

  G90
  {% if printer.toolhead.position.z + Z_PARK_LIFT > Z_PARK_MIN %}
    G1 X{X_MAX / 2} Y{Y_MAX * 0.8} Z{printer.toolhead.position.z + Z_PARK_LIFT} F4000
  {% else %}
    G1 X{X_MAX / 2} Y{Y_MAX * 0.8} Z{Z_PARK_MIN} F4000
  {% endif %}

  RESTORE_GCODE_STATE NAME=present_print_state

[gcode_macro PAUSE]
description: Pause the actual running print
rename_existing: PAUSE_BASE
gcode:
  PAUSE_BASE

  SET_ELED RED=0.0 GREEN=1.0 BLUE=1.0

  _TOOLHEAD_PARK_PAUSE_CANCEL

[gcode_macro RESUME]
description: Resume the actual running print
rename_existing: RESUME_BASE
gcode:
  ##### read extrude from  _TOOLHEAD_PARK_PAUSE_CANCEL  macro #####
  {% set extrude = printer['gcode_macro _TOOLHEAD_PARK_PAUSE_CANCEL'].extrude %}
  #### get VELOCITY parameter if specified ####
  {% if 'VELOCITY' in params|upper %}
    {% set get_params = ('VELOCITY=' + params.VELOCITY)  %}
  {%else %}
    {% set get_params = "" %}
  {% endif %}
  ##### end of definitions #####
  {% if printer.extruder.can_extrude|lower == 'true' %}
    M83
    G1 E{extrude} F2100
    {% if printer.gcode_move.absolute_extrude |lower == 'true' %} M82 {% endif %}
  {% else %}
    {action_respond_info("Extruder not hot enough")}
  {% endif %}

  ELED_ON

  RESUME_BASE {get_params}

[gcode_macro CANCEL_PRINT]
description: Cancel the actual running print
rename_existing: CANCEL_PRINT_BASE
variable_park: True
gcode:
  ## Move head and retract only if not already in the pause state and park set to true
  {% if printer.pause_resume.is_paused|lower == 'false' and park|lower == 'true'%}
    _TOOLHEAD_PARK_PAUSE_CANCEL
  {% endif %}
  TURN_OFF_HEATERS

  SET_ELED RED=1.0 GREEN=1.0

  CANCEL_PRINT_BASE

[gcode_macro _TOOLHEAD_PARK_PAUSE_CANCEL]
description: Helper: park toolhead used in PAUSE and CANCEL_PRINT
variable_extrude: 1.0
gcode:
  ##### set park positon for x and y #####
  # default is your max posion from your printer.cfg
  {% set x_park = printer.toolhead.axis_maximum.x|float - 5.0 %}
  {% set y_park = printer.toolhead.axis_maximum.y|float - 5.0 %}
  {% set z_park_delta = 2.0 %}
  ##### calculate save lift position #####
  {% set max_z = printer.toolhead.axis_maximum.z|float %}
  {% set act_z = printer.toolhead.position.z|float %}
  {% if act_z < (max_z - z_park_delta) %}
    {% set z_safe = z_park_delta %}
  {% else %}
    {% set z_safe = max_z - act_z %}
  {% endif %}
  ##### end of definitions #####
  {% if printer.extruder.can_extrude|lower == 'true' %}
    M83
    G1 E-{extrude} F2100
    {% if printer.gcode_move.absolute_extrude |lower == 'true' %} M82 {% endif %}
  {% else %}
    {action_respond_info("Extruder not hot enough")}
  {% endif %}
  {% if "xyz" in printer.toolhead.homed_axes %}
    G91
    G1 Z{z_safe} F900
    G90
    G1 X{x_park} Y{y_park} F6000
    {% if printer.gcode_move.absolute_coordinates|lower == 'false' %} G91 {% endif %}
  {% else %}
    {action_respond_info("Printer not homed")}
  {% endif %}

[gcode_macro ELED_ON]
description: Turn extruder LED on and resets led colors
gcode:
  SET_ELED RED=1.0 GREEN=1.0 BLUE=1.0

[gcode_macro ELED_OFF]
description: Turn extruder LED strip off
gcode:
  SET_ELED RED=0.0 GREEN=0.0 BLUE=0.0

[gcode_macro SET_ELED]
description: SET_LED extruder shortcut
#rename_existing: SET_LED_BASE
gcode:
  {% set RED = params.RED|default(0)|float %}
  {% set GREEN = params.GREEN|default(0)|float %}
  {% set BLUE = params.BLUE|default(0)|float %}
  {% set WHITE = params.BLUE|default(0)|float %}

  SET_LED LED=extruder RED={RED} GREEN={GREEN} BLUE={BLUE} WHITE={WHITE}
  