################################################################################
# Board: MKS Robin Nano v1.1 (Flying Bear Reborn 2.0) / MKS Robin Nano v1.3 / MKS Robin Nano-S v1.3
# Printer: Flying Bear Ghost 4S / Ghost 5 / Reborn
# G-code Macros by ODUCCEU
################################################################################
# WARNING! DO NOT EDIT THIS FILE!
# To override settings from this file, you can copy and paste the relevant
# sections into your printer.cfg and change it there.
################################################################################

# Print Job Management #########################################################

[gcode_macro START_PRINT]
## PrusaSliser/SuperSlicer: 
# M190 S0
# M109 S0
# START_PRINT EXTRUDER_TEMP={first_layer_temperature[0]} BED_TEMP={first_layer_bed_temperature[0]}
## Cura: 
# M190 S0
# M109 S0
# START_PRINT EXTRUDER_TEMP={material_print_temperature_layer_0} BED_TEMP={material_bed_temperature_layer_0}
description: Start routine for the print
variable_retract: 10
gcode:
    {% set extruder_temp = params.EXTRUDER_TEMP|default(240)|float %}
    {% set bed_temp = params.BED_TEMP|default(70)|float %}
    {% set E = printer["gcode_macro START_PRINT"].retract|float %}
    {% if bed_temp > 90 %}
        {% set bed_temp_preheat = (bed_temp * 0.90)|int %}
    {% else %}
        {% set bed_temp_preheat = (bed_temp * 0.75)|int %}
    {% endif %}
    CLEAR_PAUSE
    M220 S100                                                                   # reset feedrate
    M221 S100                                                                   # reset flowrate
    G90                                                                         # absolute positioning
    M83                                                                         # relative extrusion mode
    SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={bed_temp}                  # set bed temp
    TEMPERATURE_WAIT SENSOR=heater_bed MINIMUM={bed_temp_preheat}               # wait until bed is partially heated
    SET_HEATER_TEMPERATURE HEATER=extruder TARGET={extruder_temp}               # set extruder temp
    TEMPERATURE_WAIT SENSOR=heater_bed MINIMUM={bed_temp}                       # wait for bed temp
    TEMPERATURE_WAIT SENSOR=extruder MINIMUM={extruder_temp}                    # wait for extruder temp
    G28                                                                         # home
    G0 Z10 F600                                                                 # raise Z
    G1 E{E} F2100                                                               # prime

[gcode_macro END_PRINT]
gcode:
    {% set E = printer["gcode_macro START_PRINT"].retract|float %}
    TURN_OFF_HEATERS
    M107                                                                        # turn off fan
    G91                                                                         # relative positioning
    G1 E-{E} F2100                                                              # retract
    G0 X1 Y1 F6000                                                              # wipe
    G0 Z2 F600                                                                  # raise Z
    G90                                                                         # absolute positioning
    PARK Z=30
    M84                                                                         # turn off all motors
    BEEP P=200 S=250
    SAVE_UPDATES

[gcode_macro PARK]
gcode:
    {% set x_park = params.X|default(printer.toolhead.axis_minimum.x)|float %}
    {% set y_park = params.Y|default(printer.toolhead.axis_minimum.y)|float %}
    {% set z_park = params.Z|default(10)|float + printer.toolhead.position.z|float %}
    {% 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 %}
    {% if x_park > x_max %}
        {% set x_park = x_max %}
    {% endif %}
    {% if y_park > y_max %}
        {% set y_park = y_max %}
    {% endif %}
    {% if z_park > z_max %}
        {% set z_park = z_max %}
    {% endif %}
    SAVE_GCODE_STATE NAME=PARK_STATE
    {% if printer.toolhead.homed_axes != "xyz" %} G28 {% endif %}               # home if not homed
    G90                                                                         # absolute positioning
    G0 Z{z_park} F600 
    G0 X{x_park} Y{y_park} F6000
    RESTORE_GCODE_STATE name=PARK_STATE

[gcode_macro PAUSE]
description: Pause the actual running print
rename_existing: BASE_PAUSE
gcode:
    {% set E = printer["gcode_macro START_PRINT"].retract|float %}
    SAVE_GCODE_STATE NAME=PAUSE_STATE
    BASE_PAUSE
    {% if printer.extruder.can_extrude|lower == 'true' %}
        G91
        G1 E-{E} F2100                                                          # retract
        G90
    {% else %}
        {action_respond_info("Extruder is not hot enough")}
    {% endif %}
    PARK

[gcode_macro RESUME]
description: Resume the actual running print
rename_existing: BASE_RESUME
gcode:
    {% set E = printer["gcode_macro START_PRINT"].retract|float %}
    {% if 'VELOCITY' in params|upper %}
        {% set get_params = ('VELOCITY=' + params.VELOCITY)  %}
    {%else %}
        {% set get_params = "" %}
    {% endif %}
    {% if printer.extruder.can_extrude|lower == 'true' %}
        G91
        G1 E{E} F2100
        G90
    {% else %}
        {action_respond_info("Extruder is not hot enough")}
    {% endif %}  
    RESTORE_GCODE_STATE NAME=PAUSE_STATE MOVE=1
    BASE_RESUME {get_params}

[gcode_macro CANCEL_PRINT]
description: Cancel the actual running print
rename_existing: BASE_CANCEL_PRINT
gcode:
    {% set E = printer["gcode_macro START_PRINT"].retract|float %}
    CLEAR_PAUSE
    TURN_OFF_HEATERS
    M107                                                                        # turn off fan
    {% if printer.extruder.can_extrude|lower == 'true' %}
        G91                                                                     # relative positioning
        G1 E-{E} F2100                                                          # retract
        G0 Z2 F600                                                              # raise Z
        G90                                                                     # absolute positioning
    {% else %}
        {action_respond_info("Extruder is not hot enough")}
    {% endif %}
    SDCARD_RESET_FILE
    BASE_CANCEL_PRINT
    PARK Z=30
    M84                                                                         # turn off all motors

# Save to Config ##########################################################

[gcode_macro SAVE_UPDATES]
description: Run SAVE_CONFIG if there are updates that it may persist to disk
gcode:
    {% if printer.configfile.save_config_pending %}
        M118 Saving and restarting now
        G4 P2000                                                                # wait 2 seconds
        SAVE_CONFIG
    {% endif %}

################################################################################
