########################################
# Useful Macros
########################################

[gcode_shell_command Klipper_Backup]
command: sh /usr/data/helper-script/files/scripts/useful_macros.sh -backup_klipper
timeout: 600.0
verbose: true

[gcode_shell_command Klipper_Restore]
command: sh /usr/data/helper-script/files/scripts/useful_macros.sh -restore_klipper
timeout: 600.0
verbose: true

[gcode_shell_command Moonraker_Backup]
command: sh /usr/data/helper-script/files/scripts/useful_macros.sh -backup_moonraker
timeout: 600.0
verbose: true

[gcode_shell_command Moonraker_Restore]
command: sh /usr/data/helper-script/files/scripts/useful_macros.sh -restore_moonraker
timeout: 600.0
verbose: true

[gcode_shell_command Reload_Camera]
command: sh /usr/data/helper-script/files/scripts/useful_macros.sh -reload_camera
timeout: 600.0
verbose: true


[gcode_macro _KLIPPER_BACKUP]
gcode:
  RUN_SHELL_COMMAND CMD=Klipper_Backup
  RESPOND TYPE=command MSG="action:prompt_end"


[gcode_macro _KLIPPER_RESTORE]
gcode:
  RUN_SHELL_COMMAND CMD=Klipper_Restore
  RESPOND TYPE=command MSG="action:prompt_end"


[gcode_macro _MOONRAKER_BACKUP]
gcode:
  RUN_SHELL_COMMAND CMD=Moonraker_Backup
  RESPOND TYPE=command MSG="action:prompt_end"


[gcode_macro _MOONRAKER_RESTORE]
gcode:
  RUN_SHELL_COMMAND CMD=Moonraker_Restore
  RESPOND TYPE=command MSG="action:prompt_end"


[gcode_macro KLIPPER_BACKUP_CONFIG]
description: Backup Klipper configuration files
gcode:
  RESPOND TYPE=command MSG="action:prompt_begin Backup Klipper configuration files"
  RESPOND TYPE=command MSG="action:prompt_text Do you want to backup Klipper configuration files?"
  RESPOND TYPE=command MSG="action:prompt_footer_button CANCEL|RESPOND TYPE=command MSG="action:prompt_end"|error"
  RESPOND TYPE=command MSG="action:prompt_footer_button BACKUP|_KLIPPER_BACKUP|primary"
  RESPOND TYPE=command MSG="action:prompt_show"


[gcode_macro KLIPPER_RESTORE_CONFIG]
description: Restore Klipper configuration files
gcode:
  RESPOND TYPE=command MSG="action:prompt_begin Restore Klipper configuration files"
  RESPOND TYPE=command MSG="action:prompt_text Do you want to restore Klipper configuration files?"
  RESPOND TYPE=command MSG="action:prompt_footer_button CANCEL|RESPOND TYPE=command MSG="action:prompt_end"|error"
  RESPOND TYPE=command MSG="action:prompt_footer_button RESTORE|_KLIPPER_RESTORE|primary"
  RESPOND TYPE=command MSG="action:prompt_show"


[gcode_macro MOONRAKER_BACKUP_DATABASE]
description: Backup Moonraker database
gcode:
  RESPOND TYPE=command MSG="action:prompt_begin Backup Moonraker database"
  RESPOND TYPE=command MSG="action:prompt_text Do you want to backup Moonraker database?"
  RESPOND TYPE=command MSG="action:prompt_footer_button CANCEL|RESPOND TYPE=command MSG="action:prompt_end"|error"
  RESPOND TYPE=command MSG="action:prompt_footer_button BACKUP|_MOONRAKER_BACKUP|primary"
  RESPOND TYPE=command MSG="action:prompt_show"


[gcode_macro MOONRAKER_RESTORE_DATABASE]
description: Restore Moonraker database
gcode:
  RESPOND TYPE=command MSG="action:prompt_begin Restore Moonraker database"
  RESPOND TYPE=command MSG="action:prompt_text Do you want to restore Moonraker database?"
  RESPOND TYPE=command MSG="action:prompt_footer_button CANCEL|RESPOND TYPE=command MSG="action:prompt_end"|error"
  RESPOND TYPE=command MSG="action:prompt_footer_button RESTORE|_MOONRAKER_RESTORE|primary"
  RESPOND TYPE=command MSG="action:prompt_show"


[gcode_macro RELOAD_CAMERA]
description: Reload camera service
gcode:
  RUN_SHELL_COMMAND CMD=Reload_Camera


[gcode_macro BED_LEVELING]
description: Start Bed Leveling
gcode:
  {% if 'PROBE_COUNT' in params|upper %}
    {% set get_count = ('PROBE_COUNT=' + params.PROBE_COUNT) %}
  {%else %}
    {% set get_count = "" %}
  {% endif %}
  {% set bed_temp = printer.custom_macro.default_bed_temp %}
  {% set extruder_temp = printer.custom_macro.g28_ext_temp %}
  {% set nozzle_clear_temp = printer.custom_macro.default_extruder_temp %}
  {% if 'BED_TEMP' in params|upper %}
    {% set bed_temp = params.BED_TEMP %}
  {% endif %}
  {% if 'EXTRUDER_TEMP' in params|upper %}
    {% set nozzle_clear_temp = params.EXTRUDER_TEMP %}
  {% endif %}
  SET_FILAMENT_SENSOR SENSOR=filament_sensor ENABLE=0
  SET_TEMPERATURE_FAN_TARGET TEMPERATURE_FAN=soc_fan TARGET=30
  M109 S140
  {% if printer.toolhead.homed_axes != "xyz" %}
    G28
  {% endif %}
  BED_MESH_CLEAR
  NOZZLE_CLEAR HOT_MIN_TEMP={hotend_temp} HOT_MAX_TEMP={nozzle_clear_temp} BED_MAX_TEMP={bed_temp}
  M204 S5000
  SET_VELOCITY_LIMIT ACCEL_TO_DECEL=5000
  BED_MESH_CALIBRATE {get_count}
  BED_MESH_OUTPUT
  {% set y_park = printer.toolhead.axis_maximum.y/2 %}
  {% set x_park = printer.toolhead.axis_maximum.x|float - 10.0 %}
  G1 Z50 F3600
  TURN_OFF_HEATERS
  SET_TEMPERATURE_FAN_TARGET TEMPERATURE_FAN=soc_fan TARGET=45
  SET_FILAMENT_SENSOR SENSOR=filament_sensor ENABLE=1


[gcode_macro PID_BED]
description: Start Bed PID
gcode:
  G90
  {% if printer.toolhead.homed_axes != "xyz" %}
    G28
  {% endif %}
  G1 Z10 F600
  M106
  PID_CALIBRATE HEATER=heater_bed TARGET={params.BED_TEMP|default(70)}
  M107
  {% set y_park = printer.toolhead.axis_maximum.y/2 %}
  {% set x_park = printer.toolhead.axis_maximum.x|float - 10.0 %}
  G1 X{x_park} Y{y_park} F6000


[gcode_macro PID_HOTEND]
description: Start Hotend PID
gcode:
  G90
  {% if printer.toolhead.homed_axes != "xyz" %}
    G28
  {% endif %}
  G1 Z10 F600
  M106
  PID_CALIBRATE HEATER=extruder TARGET={params.HOTEND_TEMP|default(250)}
  M107
  {% set y_park = printer.toolhead.axis_maximum.y/2 %}
  {% set x_park = printer.toolhead.axis_maximum.x|float - 10.0 %}
  G1 X{x_park} Y{y_park} F6000
  WAIT_TEMP_START


[gcode_macro WARMUP]
description: Stress Test
variable_maxd: 14142.14 ; = SQRT(2*maxy)
gcode:
  {% set min_loops = 2 %}
  {% set max_loops = params.LOOPS|default(3)|int %}
  {% if 'LOOPS' in params|upper %}
    {% if max_loops < min_loops %}
      {% set max_loops = min_loops %}
    {% endif %}
  {% endif %}
  {% set loop_cnt = max_loops %}
  {% set maxx = params.X_ACCEL_MAX|default(10000)|int %}
  {% set maxy = params.Y_ACCEL_MAX|default(10000)|int %}
  {% set max_x = (printer.toolhead.axis_maximum.x|int-5) %}
  {% set max_y = (printer.toolhead.axis_maximum.y|int-5) %}
  {% set loop_step_y = max_y//(loop_cnt-1) %}
  {% set loop_step_x = max_x//(loop_cnt-1) %}
  {% set y_park = printer.toolhead.axis_maximum.y/2 %}
  {% set x_park = printer.toolhead.axis_maximum.x|float - 10.0 %}
  {% if printer.toolhead.homed_axes != "xyz" %}
    G28
  {% endif %}
  SET_VELOCITY_LIMIT ACCEL={maxx} ACCEL_TO_DECEL={maxx/2}
  {% for number in range(10,max_y+11,loop_step_y) %}
    {% if number >= max_y %}
    {% set number = max_y %}
    {% endif %}
    G1 F{maxy} X10 Y{number}
    G1 F{maxx} X{max_x} Y{number}
  {% endfor %}
  SET_VELOCITY_LIMIT ACCEL={maxy} ACCEL_TO_DECEL={maxy/2}
  {% for number in range(10,max_x+11,loop_step_y) %}
    {% if number >= max_x %}
    {% set number = max_x %}
    {% endif %}
    G1 F{maxy} X{number} Y{max_y}
    G1 F{maxy} X{number} Y10
  {% endfor %}
  SET_VELOCITY_LIMIT ACCEL={maxd} ACCEL_TO_DECEL={maxd/2}
  {% for times in range(loop_cnt) %}
    G1 F{maxx} X10 Y10
    G1 F{maxd} X{max_x} Y{max_y}
    G1 F{maxx} X10 Y{max_y}
    G1 F{maxd} X{max_x} Y10
    G1 F{maxy} X{max_x} Y{max_y}
    G1 F{maxd} X10 Y10
    G1 F{maxy} X10 Y{max_y}
    G1 F{maxd} X{max_x} Y10
  {% endfor %}
  SET_VELOCITY_LIMIT ACCEL={maxx} ACCEL_TO_DECEL={maxx/2}
  {% for times in range(loop_cnt) %}
    G1 F{maxy} X10 Y10
    G1 F{maxy} X10 Y{max_y}
    G1 F{maxx} X{max_x} Y{max_y}
    G1 F{maxy} X{max_x} Y10
    G1 F{maxx} X10 Y10
    G1 F{maxx} X{max_x} Y10
    G1 F{maxy} X{max_x} Y{max_y}
    G1 F{maxx} X10 Y{max_y}
  {% endfor %}
  G1 X{x_park} Y{y_park} F15000
