################################################################################
# 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.
################################################################################

# Filament Change ##############################################################

[gcode_macro FILAMENT_CHANGE]
# Unloads filament, waites 5 minutes for manual filament switch and loads again
gcode:
    SAVE_GCODE_STATE NAME=FILAMENT_CHANGE_STATE
    {% set timer = params.T|default(300)|float %}
    {% set unload = params.U|default(100)|float %}
    {% set load = params.L|default(100)|float %}
    {% if printer.pause_resume.is_paused %}
        M118 Already paused
    {% else %}
        {% if printer.toolhead.homed_axes != "xyz" %}
            M118 Homing
            G28                                                                 # home if not homed
        {% else %}
            M118 Pausing print
            PAUSE
        {% endif %}
    {% endif %}
    M118 Changing filament...
    SET_IDLE_TIMEOUT TIMEOUT=7200
    FILAMENT_UNLOAD U={unload}
    COUNTDOWN TIME={timer} MSG="Change filament! Time left: "
    FILAMENT_LOAD L={load}
    RESTORE_GCODE_STATE NAME=FILAMENT_CHANGE_STATE

[gcode_macro FILAMENT_LOAD]
gcode:
    {% set load = params.L|default(100)|float * 0.5 %}
    {% set extruder_temp = params.T|default(220)|float %}
    SAVE_GCODE_STATE NAME=FILAMENT_LOAD_STATE
    LOW_TEMP_CHECK T={extruder_temp}
    M118 Loading filament...
    M83                                                                         # relative extrusion
    G0 E{load} F600                                                             # load the filament into the hotend area
    G4 P1000                                                                    # wait 1 second
    G1 E50 F100                                                                 # purge
    M400                                                                        # wait until all moves are completed
    M118 Filament loaded!
    BEEP
    RESTORE_GCODE_STATE NAME=FILAMENT_LOAD_STATE

[gcode_macro FILAMENT_UNLOAD]
gcode:
    {% set unload = params.U|default(100)|float %}
    {% set extruder_temp = params.T|default(220)|float %}
    SAVE_GCODE_STATE NAME=FILAMENT_UNLOAD_STATE
    LOW_TEMP_CHECK T={extruder_temp}
    M118 Unloading filament
    M83                                                                         # relative extrusion
    G0 E-5 F3600                                                                # extract filament to the coldend zone 
    G4 P3000                                                                    # wait for 3 seconds
    G0 E5 F3600                                                                 # push back the filament to smash any stringing 
    G0 E-15 F3600                                                               # extract back fast to the coldend zone 
    G0 E-{unload} F300                                                          # extract a lot
    M400                                                                        # wait until all moves are completed
    M118 Filament unloaded!
    BEEP
    RESTORE_GCODE_STATE NAME=FILAMENT_UNLOAD_STATE

[gcode_macro LOW_TEMP_CHECK]
gcode:
    {% set extruder_temp = params.T|default(220)|float %}
    {% if printer.extruder.target > extruder_temp %}                            # check for a setpoint for extruder
        {% set extruder_temp = printer.extruder.target %}
    {% endif %}
    {% if printer.extruder.temperature < extruder_temp %}                       # heat to the target temp
        M118 Heating to {extruder_temp}
        SET_HEATER_TEMPERATURE HEATER=extruder TARGET={extruder_temp}
        TEMPERATURE_WAIT SENSOR=extruder MINIMUM={extruder_temp}
    {% endif %}

[gcode_macro COUNTDOWN]
gcode: 
    {% set timer = params.TIME|default(10)|int %}
    {% set message = params.MSG|default("Time: ") %}
    # countdown
    {% if timer > 60 %}
        {% for s in range(timer, 60, -10) %}
            M118 {message} {s}s
            G4 P10000                                                           # wait 10 seconds
        {% endfor %}
        {% set timer = 60 %}
    {% endif %}
    {% if timer > 10 %}
        {% for s in range(timer, 10, -5) %}
            M118 {message} {s}s
            G4 P5000                                                            # wait 5 seconds
        {% endfor %}
        {% set timer = 10 %}
    {% endif %}
    {% if timer > 0 %}
        {% for s in range(timer, 0, -1) %}
            M118 {message} {s}s
            G4 P1000                                                            # wait 1 second
        {% endfor %}
    {% endif %}
    BEEP

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