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

# Marlin Compatibility #########################################################

[gcode_macro G27]
gcode:
    PARK

[gcode_macro M125]
gcode:
    PARK

[gcode_macro M76]                                                               # Marlin Compatibility
gcode:
    PAUSE

[gcode_macro M601]                                                               # Marlin Compatibility
gcode:
    PAUSE

[gcode_macro M300]                                                              # Marlin Compatibility
gcode:
    BEEP

[gcode_macro M600]                                                              # Marlin Compatibility
gcode:
    FILAMENT_CHANGE

[gcode_macro M701]                                                              # Marlin Compatibility
gcode:
    FILAMENT_LOAD

[gcode_macro M702]                                                              # Marlin Compatibility
gcode:
    FILAMENT_UNLOAD

[gcode_macro M92]
gcode:
    {% set extruder_steps = params.E|default(0.0025)|float %}
    SET_EXTRUDER_STEP_DISTANCE DISTANCE={extruder_steps}

[gcode_macro M204]                                                              # Set Acceleration
# Klipper accepts M204 S<value> OR M204 P<value> T<value> where both P and T must be specified
# PrusaSliser sends M204 P<value> without T and Klipper shows an error
# This macro makes M204 command more versatile
rename_existing: M204.1
gcode:
    {% set factor = params.F|default(0.5)|float %}
    {% if 'S' in params %}
        SET_VELOCITY_LIMIT ACCEL={S} ACCEL_TO_DECEL={ S|float * factor }
    {% else %}
        {% if 'P' in params %}
            {% if 'T' in params %}
                {% if P|int < T|int %}
                    SET_VELOCITY_LIMIT ACCEL={P} ACCEL_TO_DECEL={ P|float * factor }
                {% else %}
                    SET_VELOCITY_LIMIT ACCEL={T} ACCEL_TO_DECEL={ T|float * factor }
                {% endif %}
            {% else %}
                SET_VELOCITY_LIMIT ACCEL={P} ACCEL_TO_DECEL={ P|float * factor }
            {% endif %}
        {% elif 'T' in params %}
            SET_VELOCITY_LIMIT ACCEL={T} ACCEL_TO_DECEL={ T|float * factor }
        {% endif %}
    {% endif %}

[gcode_macro M205]                                                              # Set Junction deviation or Jerk
gcode:
    {% if 'J' in params %}
        SET_VELOCITY_LIMIT SQUARE_CORNER_VELOCITY={J}
    {% elif 'X' in params %}
        SET_VELOCITY_LIMIT SQUARE_CORNER_VELOCITY={X}
    {% elif 'Y' in params %}
        SET_VELOCITY_LIMIT SQUARE_CORNER_VELOCITY={Y}
    {% endif %}

[gcode_macro M207]                                                              # Set Firmware Retraction
gcode:
    {% set length = params.S|default(0.5)|float %}
    {% set speed = params.F|default(25)|float %}
    SET_RETRACTION RETRACT_LENGTH={length} RETRACT_SPEED={speed}

[gcode_macro M303]                                                              # PID Autotune
gcode:
    {% if 'E' in params %}
        {% set heater = params.E|default(0)|int %}
        {% set temp = params.T|default(0)|float %}
        {% if heater == 0 %}                                                    # extruder
            {% if temp >= printer.configfile.settings.extruder.min_extrude_temp|float %}
                {% if temp <= printer.configfile.settings.extruder.max_temp|float %}
                    PID_CALIBRATE HEATER=extruder TARGET={T}
                {% endif %}
            {% endif %}
        {% elif heater == -1 %}                                                 # bed
            {% if temp <= printer.configfile.settings.heater_bed.max_temp|float %}
                PID_CALIBRATE HEATER=heater_bed TARGET={T}
            {% endif %}
        {% endif %}
    {% endif %}

[gcode_macro M486]                                                              # Label Objects
# M486 has no realization in Klipper yet
# This empty macro stops M486-error terminal messages
gcode:
    # Do nothing

[gcode_macro M500]                                                              # Save Settings
gcode:
    SAVE_CONFIG

[gcode_macro M900]                                                              # Set Pressure Advance
gcode:
    {% if 'K' in params %}
        SET_PRESSURE_ADVANCE ADVANCE={ params.K|float }
    {% endif %}
