#####################################################################
#   print_start macro
#####################################################################

[include klicky-probe.cfg]

## *** THINGS TO UNCOMMENT: ***
## Bed mesh (2 lines at 2 locations)
## Z_TILT_ADJUST if your printer is a Trident
## Quad gantry level if your printer is a V2
## Nevermore - if you have one

[gcode_macro PRINT_START]
gcode:
  # This part fetches data from your slicer. Such as bed temp, extruder temp, chamber temp and size of your printer.
  {% set target_bed = params.BED|int %}
  {% set target_extruder = params.EXTRUDER|int %}

##Uncomment bellow when you install chamber thermistor ----------------------------------------------------------------------------------------------------------------------
  {% set target_chamber = params.CHAMBER|default("40")|int %}

  {% set x_wait = printer.toolhead.axis_maximum.x|float / 2 %}
  {% set y_wait = printer.toolhead.axis_maximum.y|float / 2 %}

  # Homes the printer, sets absolute positioning and updates the Stealthburner leds.
  STATUS_HOMING         # Sets SB-leds to homing-mode
  G28                   # Full home (XYZ)
  G90                   # Absolut position

  ##  Uncomment for bed mesh (1 of 2)
  #BED_MESH_CLEAR       # Clears old saved bed mesh (if any)

  # Checks if the bed temp is higher than 90c - if so then trigger a heatsoak.
  {% if params.BED|int > 90 %}
    SET_DISPLAY_TEXT MSG="Bed: {target_bed}c"           # Displays info
    STATUS_HEATING                                      # Sets SB-leds to heating-mode
    M106 S255                                           # Turns on the PT-fan

    #  Uncomment if you have a Nevermore.
    SET_FAN_SPEED FAN=Nevermore SPEED=1                   # Turns on the nevermore

    G1 X{x_wait} Y{y_wait} Z15 F9000                    # Goes to center of the bed
    M190 S{target_bed}                                  # Sets the target temp for the bed
    SET_DISPLAY_TEXT MSG="Heatsoak: {target_chamber}c"  # Displays info

##Uncomment Bellow when chaimber thermistor is installed----------------------------------------------------------------------------------------------------------    
#TEMPERATURE_WAIT SENSOR="temperature_sensor chamber" MINIMUM={target_chamber}   # Waits for chamber to reach desired temp

  # If the bed temp is not over 90c, then it skips the heatsoak and just heats up to set temp with a 5min soak
  {% else %}
    SET_DISPLAY_TEXT MSG="Bed: {target_bed}c"           # Displays info
    STATUS_HEATING                                      # Sets SB-leds to heating-mode
    G1 X{x_wait} Y{y_wait} Z15 F9000                    # Goes to center of the bed
    M190 S{target_bed}                                  # Sets the target temp for the bed
    SET_DISPLAY_TEXT MSG="Soak for 5min"                # Displays info
    G4 P300000                                          # Waits 5 min for the bedtemp to stabilize
  {% endif %}

  # Heating nozzle to 150 degrees. This helps with getting a correct Z-home
  SET_DISPLAY_TEXT MSG="Hotend: 150c"          # Displays info
  #M109 S150                                    # Heats the nozzle to 150c
  CLEAN_NOZZLE
  SET_DISPLAY_TEXT MSG="Clean Boi"

  #  Uncomment for Trident (Z_TILT_ADJUST)
  SET_DISPLAY_TEXT MSG="Z-tilt adjust"     # Displays info
  STATUS_LEVELING                          # Sets SB-leds to leveling-mode
  Z_TILT_ADJUST                            # Levels the buildplate via z_tilt_adjust
  G28 Z                                    # Homes Z again after z_tilt_adjust

  ##  Uncomment for V2 (Quad gantry level AKA QGL)
  #SET_DISPLAY_TEXT MSG="QGL"      # Displays info
  #STATUS_LEVELING                 # Sets SB-leds to leveling-mode
  #quad_gantry_level               # Levels the buildplate via QGL
  #G28 Z                           # Homes Z again after QGL

  ##  Uncomment for Klicky auto-z
  #CALIBRATE_Z                                 # Calibrates Z-offset with klicky
  #SET_DISPLAY_TEXT MSG="Z-offset"             # Displays info

  #  Uncomment for bed mesh (2 of 2)
  SET_DISPLAY_TEXT MSG="Bed mesh"    # Displays info
  STATUS_MESHING                     # Sets SB-leds to bed mesh-mode
  bed_mesh_calibrate                 # Starts bed mesh

  # Heats up the nozzle up to target via data from slicer
  SET_DISPLAY_TEXT MSG="Hotend: {target_extruder}c"             # Displays info
  STATUS_HEATING                                                # Sets SB-leds to heating-mode
  G1 X{x_wait} Y{y_wait} Z15 F9000                              # Goes to center of the bed
  M107                                                          # Turns off partcooling fan
  M109 S{target_extruder}                                       # Heats the nozzle to printing temp


  # Gets ready to print by doing a purge line and updating the SB-leds
  SET_DISPLAY_TEXT MSG="Printer goes brr"          # Displays info
  STATUS_PRINTING                                  # Sets SB-leds to printing-mode
  LINE_PURGE
  #G0 X{x_wait - 50} Y4 F10000                      # Moves to starting point
  #G0 Z0.4                                          # Raises Z to 0.4
  #G91                                              # Incremental positioning 
  #G1 X100 E20 F1000                                # Purge line
  #G90                                              # Absolut position

[gcode_macro PRINT_END]
#   Use PRINT_END for the slicer ending script - please customise for your slicer of choice
gcode:
    # safe anti-stringing move coords
    {% set th = printer.toolhead %}
    {% set x_safe = th.position.x + 20 * (1 if th.axis_maximum.x - th.position.x > 20 else -1) %}
    {% set y_safe = th.position.y + 20 * (1 if th.axis_maximum.y - th.position.y > 20 else -1) %}
    {% set z_safe = [th.position.z + 2, th.axis_maximum.z]|min %}
    
    SAVE_GCODE_STATE NAME=STATE_PRINT_END
    
    M400                           ; wait for buffer to clear
    G92 E0                         ; zero the extruder
    G1 E-2.0 F3600                 ; retract filament
    
    TURN_OFF_HEATERS
    
    G90                                      ; absolute positioning
    G0 X{x_safe} Y{y_safe} Z{z_safe} F20000  ; move nozzle to remove stringing
    G0 X{th.axis_maximum.x//2} Y{th.axis_maximum.y - 2} F3600  ; park nozzle at rear
    M107                                     ; turn off fan

    SET_FAN_SPEED FAN=Nevermore SPEED=0
    BED_MESH_CLEAR
    RESTORE_GCODE_STATE NAME=STATE_PRINT_END