# QIDI Plus4 V1.0.0

[gcode_macro _CG28]
gcode:
    {% if "xyz" not in printer.toolhead.homed_axes %}
        G28
    {% endif %}

[gcode_macro zoffset]
gcode:
    SET_GCODE_OFFSET Z={printer.probe.last_z_result} MOVE=1

# Development test
[gcode_macro test_zoffset]
gcode:
    G28
    get_zoffset
    G1 X{150 - printer.probe["x_offset"]} Y{150 - printer.probe["y_offset"]} F9000
    G1 Z10 F600
    probe
    M400
    G4 P5000
    G1 Z10 F600

[gcode_macro move_subzoffset]
gcode:
    G1 Z{printer.probe.last_z_result} F600

[gcode_macro get_zoffset]
gcode:
    TOGGLE_CHAMBER_FAN
    G1 Z10 F600
    Z_VIBRATE
    QIDI_PROBE_PIN_1
    m204 S50
    G4 P500
    probe probe_speed=5 lift_speed=5 samples=5 sample_retract_dist=5
    move_subzoffset
    M114
    {% set p=(-0.11 + printer.gcode_move.homing_origin.z)|float %}
    SET_KINEMATIC_POSITION Z={p}
    G1 Z30 F600
    QIDI_PROBE_PIN_2
    m204 S10000
    TOGGLE_CHAMBER_FAN

[gcode_macro save_zoffset]
gcode:
    {% if printer.gcode_move.homing_origin.z < 0.5 %}
       SAVE_VARIABLE VARIABLE=z_offset VALUE={printer.gcode_move.homing_origin.z}
    {% endif %}

[gcode_macro set_zoffset]        
gcode:
    {% set z = printer.save_variables.variables.z_offset %}
    SET_GCODE_OFFSET Z={z} MOVE=0

# Specialized for power lose recovery
[gcode_macro CLEAR_NOZZLE_PLR]
gcode:
    {% set hotendtemp = params.HOTEND|default(250)|int %}
    {% if (printer.gcode_move.position.z ) < 35 %}
        G1 Z35 F900
    {% else %}
        G91
        G1 Z{5} F900 
        G90
    {% endif %}
    G1 X56 F12000
    G1 Y310 F12000
    G1 Y324 F600
    G1 X95 F600
    M400

    M106 S0
    M109 S{hotendtemp}

    G92 E0
    G1 E5 F50
    G92 E0
    G1 E80 F200
    G92 E0
    G1 E-2 F200
    G4 P300

    M106 S255
    G1 Y316 F30000
    G1 Y320 F3000
    G1 Y316 F30000
    G1 Y320 F3000
    G1 Y316 F30000
    G1 Y320 F3000
    G1 Y316 F12000
    G1 Y312 F600

[gcode_macro CLEAR_NOZZLE]
gcode:
    {% set hotendtemp = params.HOTEND|default(250)|int %}
    {% if (printer.gcode_move.position.z ) < 35 %}
        G1 Z35 F900
    {% else %}
        G91
        G1 Z{5} F900 
        G90
    {% endif %}
    G1 X56 F12000
    G1 Y310 F12000
    G1 Y324 F600
    G1 X95 F600
    M400
    M109 S{hotendtemp}
    G92 E0
    G1 E5 F50
    G92 E0
    G1 E80 F200
    G92 E0
    G1 E-2 F200

    M106 S255
    G4 P5000
    M104 S140
    G1 Y318 F9000
    G1 Y322 F600
    G1 Y318 F9000
    G1 Y322 F600
    G1 Y308 F30000
    G1 Y324 F600

    G1 X124 E-1 F1000 

    TEMPERATURE_WAIT SENSOR=extruder MAXIMUM={170}

    G1 X133 F200
    G1 Y321 F200
    G2 I0.5 J0.5 F600
    G2 I0.5 J0.5 F600
    G2 I0.5 J0.5 F600

    G1 Y319 F150
    G1 X132 
    G1 Y324
    G1 X131 
    G1 Y319
    G1 X130
    G1 Y324
    G1 X129
    G1 Y319

    G1 X113 F200
    G1 Y320
    G1 X125
    G1 X113
    G1 X125
    G2 I0.5 J0.5 F200
    G2 I0.5 J0.5 F200
    G2 I0.5 J0.5 F200    

    G1 Y300 F600
    G1 X95 F12000
    G1 Y314 F9000
    G1 Y324 F600

    G1 X58 F12000
    G1 X78 F12000
    G1 Y324
    G1 X58 F12000
    G1 X78 F12000
    G1 Y323.5
    G1 X58 F12000
    G1 X78 F12000
    G1 Y323
    G1 X58 F12000
    G1 X78 F12000
    G1 Y322.5
    G1 X58 F12000
    G1 X78 F12000
    G1 Y322
    G1 X58 F12000
    G1 X75 F12000
    G1 Y321.5
    G2 I0.8 J0.8 F600
    G2 I0.8 J0.8 F600
    G2 I0.8 J0.8 F600

    G1 Y324 F600

    M106 S0

    G1 X95 F12000
    G1 Y316 F9000
    G1 Y312 F600

    M400
    M118 Nozzle cleared
    G1 Y10 F12000
    G1 X10 F12000
    TEMPERATURE_WAIT SENSOR=extruder MAXIMUM={150}
    M107
    M106 P2 S0
    M400
    M118 Nozzle cooled

# Excute when start printing, prior to gcode file
[gcode_macro PRINT_START]
gcode:
    AUTOTUNE_SHAPERS

    {% set bedtemp = params.BED|int %}
    {% set hotendtemp = params.HOTEND|int %}
    {% set chambertemp = params.CHAMBER|default(0)|int %}
    set_zoffset
    M104 S0

    M106 P2 S0
    M106 P3 S0
    M106 S255
    G28      
    M141 S{chambertemp}    
    M140 S{bedtemp}    
    M106 S0
    CLEAR_NOZZLE HOTEND={hotendtemp}
    M191 S{chambertemp}    
    M190 S{bedtemp}   
    M104 S140
    G29
    G0 Z50 F600
    G0 X5 Y5  F6000
    
    {% if chambertemp == 0 %}
        M106 P3 S255
    {% endif %}
    M109 S{hotendtemp}
    M141 S{chambertemp}    
    M204 S10000
    SET_PRINT_STATS_INFO CURRENT_LAYER=1
    ENABLE_ALL_SENSOR
    save_last_file

[gcode_macro ENABLE_ALL_SENSOR]
gcode:
    ENABLE_FILAMENT_WIDTH_SENSOR
    RESET_FILAMENT_WIDTH_SENSOR
    query_filament_width
    SET_FILAMENT_SENSOR SENSOR=fila ENABLE=1

[gcode_macro DISABLE_ALL_SENSOR]
gcode:
    SET_FILAMENT_SENSOR SENSOR=fila ENABLE=0
    DISABLE_FILAMENT_WIDTH_SENSOR


[gcode_macro AUTOTUNE_SHAPERS]
variable_autotune_shapers: 'ei'
gcode:

[gcode_macro M84]
rename_existing:M84.1
gcode:
    M84.1
	SET_STEPPER_ENABLE STEPPER=stepper_x enable=0
	SET_STEPPER_ENABLE STEPPER=stepper_y enable=0
	SET_STEPPER_ENABLE STEPPER=stepper_z enable=1
    SET_STEPPER_ENABLE STEPPER=stepper_z1 enable=1
	SET_STEPPER_ENABLE STEPPER=extruder enable=0

# Used for power lose recovery detection
[gcode_macro DETECT_INTERRUPTION]
gcode:
    {% set was_interrupted = printer.save_variables.variables.was_interrupted %}
    {% if was_interrupted %}
    M118 Detected unexpected interruption during the last print. Do you want to resume printing? (Do not move the extruder before resuming.)
    M118 Yes: RESUME_INTERRUPTED
    M118 No: CLEAR_LAST_FILE
    {% endif %}

# Excute when klipper init
[delayed_gcode PRINTER_INIT]
initial_duration:0.2
gcode:
	SET_STEPPER_ENABLE STEPPER=stepper_z enable=1
    SET_STEPPER_ENABLE STEPPER=stepper_z1 enable=1
    BED_MESH_CLEAR      
    SET_FILAMENT_SENSOR SENSOR=fila ENABLE=0

[homing_override]
axes:xyz
gcode: 
    {% set HOME_CUR = 1 %}
    {% set driver_config = printer.configfile.settings['tmc2240 stepper_x'] %}
    {% set RUN_CUR = driver_config.run_current %}
    {% set HOLD_CUR = driver_config.hold_current %}
    m204 S10000
    M220 S100
	{% if params.X is defined %}
	    SET_TMC_CURRENT STEPPER=stepper_x CURRENT={HOME_CUR * 0.7} 
        G28 X
		SET_TMC_CURRENT STEPPER=stepper_x CURRENT={HOME_CUR}     
        BEEP I=1 DUR=100       
        G1 X10 F1200
    {% endif %}

    {% if params.Y is defined %}
		SET_TMC_CURRENT STEPPER=stepper_y CURRENT={HOME_CUR * 0.9} 
		G28 Y
		SET_TMC_CURRENT STEPPER=stepper_y CURRENT={HOME_CUR}  
        BEEP I=1 DUR=100          
       G1 Y10 F1200
    {% endif %}

    {% if params.Z is defined %}
        G28 x
        G28 Y
        G28 X
        G1 X150 Y150 F7800

        SET_KINEMATIC_POSITION Z={printer.toolhead.axis_maximum.z-30}
        QIDI_PROBE_PIN_2
        probe samples=2
        SET_KINEMATIC_POSITION Z=1.9
        G1 Z10 F600
        Z_VIBRATE
        QIDI_PROBE_PIN_1
        probe probe_speed=10
        SET_KINEMATIC_POSITION Z=-0.1
        G1 Z30 F480
    {% endif %}

    {% if params.X is undefined %}
    {% if params.Y is undefined %}
    {% if params.Z is undefined %}
        SET_KINEMATIC_POSITION X=0
        SET_KINEMATIC_POSITION Y=0
        SET_KINEMATIC_POSITION Z={printer.toolhead.axis_maximum.z-30}
        G91
        G1 Z7 F600	
        G1 X5 F2400
        G1 Y5 F2400
        G4 P2000
    
       SET_TMC_CURRENT STEPPER=stepper_x CURRENT={HOME_CUR * 0.8} 
        G28 X
    	SET_TMC_CURRENT STEPPER=stepper_x CURRENT={HOME_CUR} 
        BEEP I=1 DUR=100  
        G1 X45 F1200
    
    	SET_TMC_CURRENT STEPPER=stepper_y CURRENT={HOME_CUR * 0.9} 
    	G28 Y
    	SET_TMC_CURRENT STEPPER=stepper_y CURRENT={HOME_CUR} 
        BEEP I=1 DUR=100        
        G1 Y10 F1200

        SET_TMC_CURRENT STEPPER=stepper_x CURRENT={HOME_CUR * 0.8} 
        G28 X
    	SET_TMC_CURRENT STEPPER=stepper_x CURRENT={HOME_CUR} 
        BEEP I=1 DUR=100  
        G1 X10 F1200

        SET_KINEMATIC_POSITION Z={printer.toolhead.axis_maximum.z-10}

        G90
        G1 X150 Y150 F7800
        G91
        QIDI_PROBE_PIN_2
        G28 Z
        G1 Z30  F600
    {% endif %}
    {% endif %}
    {% endif %}
    SET_TMC_CURRENT STEPPER=stepper_x CURRENT={RUN_CUR} 
    SET_TMC_CURRENT STEPPER=stepper_y CURRENT={RUN_CUR} 
    M204 S10000
    G90
    QIDI_PROBE_PIN_2

[gcode_macro SHAPER_CALIBRATE]
rename_existing: RESHAPER_CALIBRATE
gcode:
    RESHAPER_CALIBRATE FREQ_START=20 FREQ_END=150

# Excute when print end, after gcode file
[gcode_macro PRINT_END]
gcode:
    {% if printer.gcode_move.homing_origin.z < 0.5 %}
       SAVE_VARIABLE VARIABLE=z_offset VALUE={printer.gcode_move.homing_origin.z}
    {% endif %}
    SET_IDLE_TIMEOUT TIMEOUT={printer.configfile.settings.idle_timeout.timeout}
    CLEAR_PAUSE
    M400
    G4 P5000
    M106 P2 S0
    M106 P0 S0
    M106 P3 S0
    
    M104 S0
    M140 S0
    M141 S0

    M220 S100
    M221 S100
    SET_STEPPER_ENABLE STEPPER=stepper_x enable=0
	SET_STEPPER_ENABLE STEPPER=stepper_y enable=0
	SET_STEPPER_ENABLE STEPPER=stepper_z enable=1
    SET_STEPPER_ENABLE STEPPER=stepper_z1 enable=1
	SET_STEPPER_ENABLE STEPPER=extruder enable=0
	
    DISABLE_ALL_SENSOR
    BED_MESH_CLEAR
    G31
    CLEAR_LAST_FILE
    M84
    BEEP I=2 DUR=500

[gcode_macro CANCEL_PRINT]
rename_existing: BASE_CANCEL_PRINT
gcode:
    {% if (printer.gcode_move.position.z) < 200 %}
        G1 Z200 F600                                      
    {% endif %}

    G1  X0 Y0 F7800
    save_zoffset
    SET_IDLE_TIMEOUT TIMEOUT={printer.configfile.settings.idle_timeout.timeout}
    CLEAR_PAUSE

    M106 P2 S0
    M106 P0 S0
    M106 P3 S0
    
    M104 S0
    M140 S0
    M141 S0

    M220 S100
    M221 S100
    SET_STEPPER_ENABLE STEPPER=stepper_x enable=0
	SET_STEPPER_ENABLE STEPPER=stepper_y enable=0
	SET_STEPPER_ENABLE STEPPER=stepper_z enable=1
    SET_STEPPER_ENABLE STEPPER=stepper_z1 enable=1
	SET_STEPPER_ENABLE STEPPER=extruder enable=0
	
    DISABLE_ALL_SENSOR
    BED_MESH_CLEAR
    G31
    BEEP I=2 DUR=500

    M84
    SDCARD_RESET_FILE
    BASE_CANCEL_PRINT
    CLEAR_LAST_FILE

[gcode_macro PAUSE]
rename_existing: BASE_PAUSE
gcode:
    {% set z = params.Z|default(35)|int %}
    
    {% if printer['pause_resume'].is_paused|int == 0 %}     
        SET_GCODE_VARIABLE MACRO=RESUME VARIABLE=zhop VALUE={z}
        SET_GCODE_VARIABLE MACRO=RESUME VARIABLE=etemp VALUE={printer['extruder'].target}
        SET_GCODE_VARIABLE MACRO=RESUME VARIABLE=efan VALUE={printer["fan_generic cooling_fan"].speed *255}

        DISABLE_FILAMENT_WIDTH_SENSOR
        SET_FILAMENT_SENSOR SENSOR=fila ENABLE=0    
        SAVE_GCODE_STATE NAME=PAUSE              
        BASE_PAUSE
        G92 E0
        G1 E-5 F1800
        {% if (printer.gcode_move.position.z ) < z %}
            G91
            G1 Z{z} F900
        {% else %}
            G91
            G1 Z{5} F900 
            SET_GCODE_VARIABLE MACRO=RESUME VARIABLE=zhop VALUE=0
        {% endif %}
        SAVE_GCODE_STATE NAME=PAUSEPARK2
        G90
        G1 X95 F12000
        G1 Y312 F12000
        G1 Y316 F600
        G1 Y320 F9000
        G1 Y324 F600
        SAVE_GCODE_STATE NAME=PAUSEPARK
        M104 S0
        SET_IDLE_TIMEOUT TIMEOUT=86400
        SET_STEPPER_ENABLE STEPPER=extruder enable=0
    {% endif %}

[gcode_macro RESUME]
rename_existing: BASE_RESUME
variable_zhop: 0
variable_etemp: 0
variable_efan: 0
gcode:
    {% set e = params.E|default(5)|int %}
    
    {% if printer['pause_resume'].is_paused|int == 1 %}
        SET_IDLE_TIMEOUT TIMEOUT={printer.configfile.settings.idle_timeout.timeout}
        {% if etemp > 0 %}
            M109 S{etemp|int}
        {% endif %}
        M83              
        M106 S0
        G1 X95 F12000
        G1 Y324 F12000                
        G92 E0
        G1 E5 F50
        G92 E0
        G1 E50 F200
        G92 E0
        G1 E-0.8 F200
        G4 P300

        M106 S{efan}

        G1 Y318 F30000
        G1 Y322 F3000
        G1 Y318 F30000
        G1 Y322 F3000
        G1 Y318 F30000
        G1 Y322 F3000
        G1 Y324 F600

        G1 X58 F12000
        G1 X78 F12000
        G1 X58 F12000
        G1 X78 F12000
        G1 X58 F12000
        G1 X78 F12000
        G1 X58 F12000
        G1 X78 F12000
        G1 X58 F12000
        G1 X78 F12000
        G1 X58 F12000
        G1 X78 F12000
        G1 X95 F12000

        G1 Y316 F9000
        G1 Y312 F600
        G1 Y260 F12000 
        RESTORE_GCODE_STATE NAME=PAUSEPARK2 MOVE=1 MOVE_SPEED=200                            
        RESTORE_GCODE_STATE NAME=PAUSE MOVE=1 MOVE_SPEED=15
        BASE_RESUME       
        ENABLE_FILAMENT_WIDTH_SENSOR
        RESET_FILAMENT_WIDTH_SENSOR
        query_filament_width
        SET_FILAMENT_SENSOR SENSOR=fila ENABLE=1
    {% endif %}

[gcode_macro BEEP]
gcode:
    {% set i = params.I|default(1)|int %}
    {% set dur = params.DUR|default(100)|int %}

    {% if printer["output_pin sound"].value|int == 1 %}
        {% for iteration in range(i|int) %}
            SET_PIN PIN=beeper VALUE=1
            G4 P{dur}
            SET_PIN PIN=beeper VALUE=0
    		G4 P{dur}
        {% endfor %}
    {% endif %}

[gcode_macro M141]
gcode:
    {% if printer["heater_generic chamber"] is defined %}
        {% set s = params.S|float %}
        SET_HEATER_TEMPERATURE HEATER=chamber TARGET={([s, 65]|min)}
    {% endif %}

[gcode_macro M191]
gcode:
    {% if printer["heater_generic chamber"] is defined %}
        {% set s = params.S|float %}
    
        M141 {% for p in params %}{'%s%s' % (p, params[p])}{% endfor %}  
        {% if s != 0 %}
            TEMPERATURE_WAIT SENSOR="heater_generic chamber" MINIMUM={([s, 65]|min)-2}
        {% endif %}
    {% endif %}
     
[gcode_macro M106]
gcode:
    {% set p = params.P|default(0)|int %}

    {% if p == 2 %}
    {% if params.S is defined %}
        SET_FAN_SPEED FAN=auxiliary_cooling_fan SPEED={(params.S|float / 255.0)}
    {% else %}
        SET_FAN_SPEED FAN=auxiliary_cooling_fan SPEED=1
    {% endif %}
    {% endif %} 

    {% if p == 0 %}
    {% if params.S is defined %}
        SET_FAN_SPEED FAN=cooling_fan SPEED={(params.S|float / 255.0)}
        
    {% else %}
        SET_FAN_SPEED FAN=cooling_fan SPEED=1
    {% endif %}
    {% endif %} 

    {% if p == 3 %}
    {% if params.S is defined %}
        SET_FAN_SPEED FAN=chamber_circulation_fan SPEED={(params.S|float / 255.0)}
    {% else %}
        SET_FAN_SPEED FAN=chamber_circulation_fan SPEED=1
    {% endif %}
    {% endif %} 

[gcode_macro M107]
gcode:  
    SET_FAN_SPEED FAN=cooling_fan SPEED=0

[gcode_macro M303]
gcode:
    {% if params.E is defined %}
     {% if params.S is defined %}
        {% if (params.E|int)==-1 %} 
         PID_CALIBRATE HEATER=heater_bed TARGET={params.S|int}
        {% endif %}
        {% if (params.E|int)==0 %}
         PID_CALIBRATE HEATER=extruder TARGET={params.S|int}
        {% endif %}
     {% endif %}
  {% endif %}

[gcode_macro M8029]
gcode:
     {% if params.D is defined %}
       {% if (params.D|int)==1 %} 
        ENABLE_FILAMENT_WIDTH_SENSOR
       {% endif %}
       {% if (params.D|int)==0 %} 
        DISABLE_FILAMENT_WIDTH_SENSOR
       {% endif %}
     {% endif %}

[gcode_macro M900]
gcode:
    {% if params.K is defined %} 
          SET_PRESSURE_ADVANCE ADVANCE={params.K}
    {% endif %}  
    {% if params.T is defined %}    
       SET_PRESSURE_ADVANCE SMOOTH_TIME={params.T}
    {% endif %} 

[gcode_macro M290]
gcode:
   SET_GCODE_OFFSET Z_ADJUST={params.Z}

[gcode_macro M901]
gcode:
    G28
    SHAPER_CALIBRATE
    M118 Input shaping complete
    SAVE_CONFIG
       
[gcode_macro M0]
gcode:
    PAUSE

[gcode_macro M25]
rename_existing: M9925
gcode:
    PAUSE

[gcode_macro RESPOND_INFO]
variable_S:0
gcode:
    {% if params.S is defined %}
        {% set s = params.S|int %}
        {% if s == 0 %}
            { action_respond_info("Nozzle cooled")  }
        {% endif %}
    {% endif %}

[gcode_macro M4027]
gcode:
    G1 X{150 - printer.probe["x_offset"]} Y{150 - printer.probe["y_offset"]} F9000
    probe
    SAVE_Z_OFFSET_TO_BED_MESH
    G1 z10 F600
    SET_GCODE_OFFSET Z=0 MOVE=1
    BED_MESH_CALIBRATE
    G0 Z50 F600
    G0 X0 Y0 F9000
    M400
    M118 Bed mesh calibrate complete
    SAVE_CONFIG

[gcode_macro M4028]
gcode:
    G28 
    get_zoffset
    M400
    M118 Position init complete
    
[gcode_macro M4029]
gcode:
    M104 S140
    M141 S0
    SAVE_VARIABLE VARIABLE=z_offset VALUE=0
    G28 
    M400
    M118 Position init complete
    
    CLEAR_NOZZLE HOTEND=280
    G28
    get_zoffset

[gcode_macro M4030]
gcode:    
    BED_MESH_CLEAR      
    SET_GCODE_OFFSET Z=0 MOVE=0
    G28
    get_zoffset
    G1 X150 Y150 F9000
    G1 Z0 F600

[gcode_macro M4031]
gcode:
    SET_KINEMATIC_POSITION Z=20
    G1 Z30 F300
    SET_TMC_CURRENT STEPPER=stepper_z CURRENT={printer.configfile.settings['tmc2209 stepper_z'].run_current * 0.7 }
    SET_TMC_CURRENT STEPPER=stepper_z1 CURRENT={printer.configfile.settings['tmc2209 stepper_z1'].run_current * 0.7 }
    REVERSE_HOMING
    SET_TMC_CURRENT STEPPER=stepper_z CURRENT={printer.configfile.settings['tmc2209 stepper_z'].run_current}
    SET_TMC_CURRENT STEPPER=stepper_z1 CURRENT={printer.configfile.settings['tmc2209 stepper_z1'].run_current}
    G91
    G1 Z-30 F600
    G90

    SET_TMC_CURRENT STEPPER=stepper_z CURRENT={printer.configfile.settings['tmc2209 stepper_z'].run_current * 0.7 }
    SET_TMC_CURRENT STEPPER=stepper_z1 CURRENT={printer.configfile.settings['tmc2209 stepper_z1'].run_current * 0.7 }
    REVERSE_HOMING
    SET_TMC_CURRENT STEPPER=stepper_z CURRENT={printer.configfile.settings['tmc2209 stepper_z'].run_current}
    SET_TMC_CURRENT STEPPER=stepper_z1 CURRENT={printer.configfile.settings['tmc2209 stepper_z1'].run_current}
    G91
    G1 Z-30 F600
    G90

[gcode_macro M603]
description: Unload filament
gcode:
    {% set hotendtemp = params.S|default(250)|int %}
    {% set accel = printer.toolhead.max_accel|int %}
    _CG28
    M204 S10000
    G1 Y150 F9000
    G1 X305 F9000
    G1 Y20 F9000
    G1 Y5 F3000
    G4 P500
    G1 Y20 F9000
    G1 Y5 F3000
    G4 P500
    G1 Y20 F9000
    
    G1 X56  F12000
    G1 Y310 F12000
    G1 Y324 F600
    G1 X95  F600

    M106 S0
    M109 S{hotendtemp}
    M118 Heat up complete

    G92 E0
    G1 E5 F50
    G1 E50 F200
    G92 E0
    G1 E-0.8 F200
    G4 P300

    M106 S255
    M104 S{hotendtemp-100}   
    G4 P5000


    G1 Y318 F9000
    G1 Y322 F600
    G1 Y318 F9000
    G1 Y322 F600
    G1 Y308 F30000
    G1 Y324 F600

    M106 S0
    M400
    M204 S{accel}
    M118 Unload finish

[gcode_macro M604]
description: Load filament
gcode:
    {% set hotendtemp = params.S|default(250)|int %}
    {% set current_state = params.F|default(1)|int %}
    {% set accel = printer.toolhead.max_accel|int %}
    M204 S10000
    M104 S{hotendtemp}
    _CG28
    {% if current_state == 1 %}
        {% if (printer.gcode_move.position.y) > 300 %}
            G91
            G1 Y-20 F9000
            G90
        {% endif %}
        G1 X95 F12000
        G1 Y312 F12000
        G1 Y316 F600
        G1 Y320 F9000
        G1 Y324 F600
    {% endif %}
    M109 S{hotendtemp}
    M118 Heat up complete

    G1 X95 F600
    G1 Y324 F600
    G92 E0
    G1 E5 F50
    G92 E0
    G1 E120 F300
    G92 E0
    G1 E-0.8 F200
    G4 P300

    M106 S255
    M104 S{hotendtemp-100}    
    G4 P5000

    G1 Y318 F9000
    G1 Y322 F600
    G1 Y318 F9000
    G1 Y322 F600
    G1 Y308 F30000
    G1 Y324 F600

    M106 S0

    M400
    M204 S{accel}
    M118 Load finish

[output_pin sound]
pin:PA1
value:0

[gcode_macro beep_on]
gcode:
    SET_PIN PIN=sound VALUE=1
    
[gcode_macro beep_off]
gcode:
    SET_PIN PIN=sound VALUE=0

[gcode_arcs]
resolution: 0.5

[gcode_macro M109]
rename_existing: M99109
gcode:
    {% set s = params.S|float %}
    
    M104 {% for p in params %}{'%s%s' % (p, params[p])}{% endfor %}
    {% if s != 0 %}
        TEMPERATURE_WAIT SENSOR=extruder MINIMUM={s} MAXIMUM={s+1}
    {% endif %}

[exclude_object]

[gcode_macro G31]
gcode:
    SET_GCODE_VARIABLE MACRO=G29 VARIABLE=k VALUE=1

[gcode_macro G32]
gcode:
    SET_GCODE_VARIABLE MACRO=G29 VARIABLE=k VALUE=0

[gcode_macro G29]
variable_k:1
gcode:
    {% set temp = printer["heater_generic chamber"].target %}
    M141 S0
    BED_MESH_CLEAR
    {% if k|int==1 %}
        G28   
        get_zoffset
        M141 S{temp}
        G1 X{150 - printer.probe["x_offset"]} Y{150 - printer.probe["y_offset"]} F9000
        G1 Z10 F600
        probe
        SAVE_Z_OFFSET_TO_BED_MESH
        G1 z10 F600
        BED_MESH_CALIBRATE PROFILE=kamp
        SAVE_VARIABLE VARIABLE=profile_name VALUE='"kamp"'
        G4 P5000
        SAVE_CONFIG_QD
    {% else %}
        g28
        get_zoffset
        BED_MESH_PROFILE LOAD=default
        SAVE_VARIABLE VARIABLE=profile_name VALUE='"default"'
    {% endif %}

[gcode_macro M204]
rename_existing: M99204
gcode:
    {% if params.S is defined %}
        {% set s = params.S|float %}
    {% endif %}
    {% if params.P is defined %}
    {% if params.T is defined %}
        {% set s = [params.P|float ,params.T|float] | min %}
    {% endif %}
    {% endif %}

    SET_VELOCITY_LIMIT ACCEL={s}
    SET_VELOCITY_LIMIT ACCEL_TO_DECEL={s/2}

[gcode_macro Z_VIBRATE]
gcode:
    m204 S400
    G90
    G0 Z4
    G91
   #G4 P3000
     SET_PIN PIN=ctlyd VALUE=1 
    {% for z in range(1,50) %}
        G1 Z1 F1200
#        G4 P50
        G1 Z-1 F1200
#       G4 P50
    {% endfor %}
    M204 S100
    SET_PIN PIN=ctlyd VALUE=0
    G4 P1000
    G1 Z4
    G90
 