# Copyright (C) 2022 Justin Schuh <code@justinschuh.com>
#
# This file may be distributed under the terms of the GNU GPLv3 license.

################################################################################
# Existing menu refinements.
################################################################################

# Hide unused menus

[menu __main __octoprint]
type: list
name: OctoPrint
enable: {printer["gcode_macro _km_globals"].menu_show_octoprint}


# Get the file list out of the SD card menu while printing.
[menu __main __sdcard]
type: list
enable: {'virtual_sdcard' in printer and
         printer["gcode_macro _km_globals"].menu_show_sdcard}
name: SD Card

[menu __main __sdcard __start]
type: command
enable: {printer.virtual_sdcard.file_path and
         not printer.virtual_sdcard.is_active and
         printer.print_stats.state != "printing" and
         printer.print_stats.state != "paused"}
name: Print: {printer.virtual_sdcard.file_path.split('/')|last}
gcode:
  M24
  {menu.back()}

[menu __main __sdcard __select]
type: vsdlist
enable: {not printer.virtual_sdcard.is_active and
         printer.print_stats.state != "printing" and
         printer.print_stats.state != "paused"}
name: Select file
index: 1

[menu __main __sdcard __select __start]
type: command
enable: {printer.virtual_sdcard.file_path and
         not printer.virtual_sdcard.is_active}
name: Print: {printer.virtual_sdcard.file_path.split('/')|last}
gcode:
  M24
  {menu.back()}

[menu __main __setup __calib __delta_calib_auto]
type: command
enable: {'delta_calibrate' in printer.configfile.settings and
         not printer.idle_timeout.state == "Printing"}
name: Delta cal. auto
gcode:
    G28
    DELTA_CALIBRATE

[menu __main __setup __calib __delta_calib_man]
type: list
enable: {'delta_calibrate' in printer.configfile.settings and
         not printer.idle_timeout.state == "Printing"}
name: Delta cal. man

# Require confirmation for anything that would abort an in-progress print.

# Steppers off
[menu __main __control __disable]
type: command
name: Steppers off
enable: {printer.idle_timeout.state != "Printing"}
gcode:
  M84
  M18

[menu __main __control __disable_printing]
type: list
index: 3
enable: {printer.idle_timeout.state == "Printing"}
name: Steppers off

[menu __main __control __disable_printing __confirm]
type: command
name: Confirm steppers off
gcode:
  M84
  M18
  {menu.back()}

# Add a park menu item.
[menu __main __control __park]
type: command
name: Park toolhead
enable: {printer.idle_timeout.state != "Printing" or
         printer.pause_resume.is_paused}
index: 1
gcode:
  PARK LAZY=1

# Octoprint abort
[menu __main __octoprint __abort]
type: list
enable: {printer.idle_timeout.state == "Printing"}
name: Abort printing

[menu __main __octoprint __abort __confirm]
type: command
name: Confirm abort printing
gcode:
  {action_respond_info('action:cancel')}
  {menu.back()}

# SD card cancel
[menu __main __sdcard __cancel]
type: list
enable: {('virtual_sdcard' in printer) and
         (printer.print_stats.state == "printing" or
          printer.print_stats.state == "paused")}
name: Cancel printing

[menu __main __sdcard __cancel __confirm]
type: command
name: Confirm cancel printing
gcode:
  {% if 'pause_resume' in printer %}
    CANCEL_PRINT
  {% else %}
    M25
    M27
    M26 S0
    TURN_OFF_HEATERS
    G91
    G0 Z{(printer.toolhead.position.z, printer.toolhead.axis_maximum.z - 5)|min
        } F1000
    G90
  {% endif %}
  {menu.back()}

# Extra setup menu options

# Shutdown and reboot
[menu __main __setup __restart]
type: list
name: Host Control

[menu __main __setup __restart __reboot]
type: command
name: Reboot host
gcode:
  M117 Rebooting host
  M118 Rebooting host
  {menu.exit()}
  G4 S2 ; Let the user see the console message.
  {action_call_remote_method("reboot_machine")} ; Try Moonraker.
  G4 S1 ; give Moonraker a chance to finish.
  {action_respond_info('action:poweroff')} ; Try Octoprint.

[menu __main __setup __restart __shutdown]
type: command
name: Shutdown host
gcode:
  M117 Shutting down
  M118 Shutting down
  {menu.exit()}
  G4 S2 ; Let the user see the console message.
  {action_call_remote_method("shutdown_machine")} ; Try Moonraker.
  G4 S1 ; give Moonraker a chance to finish.
  {action_respond_info('action:poweroff')} ; Try Octoprint.

# Speed and flow
[menu __main __setup __speed]
type: input
name: Speed: {'%3d' % (menu.input*100)}%
input: {printer.gcode_move.speed_factor}
input_min: 0.01
input_max: 5
input_step: 0.01
realtime: True
index: 4
gcode:
    M220 S{'%d' % (menu.input*100)}

[menu __main __setup __flow]
type: input
name: Flow: {'%3d' % (menu.input*100)}%
input: {printer.gcode_move.extrude_factor}
input_min: 0.01
input_max: 2
input_step: 0.01
realtime: True
index: 5
gcode:
    M221 S{'%d' % (menu.input*100)}

################################################################################
# Replace filament loading with our own macros.
################################################################################

[menu __main __filament]
type: list
name: Filament
enable: {printer.idle_timeout.state != "Printing" or
         printer.pause_resume.is_paused}

# Hide the old load/unload commands.
[menu __main __filament __loadf]
type: command
name: Load Fil. fast
enable: False

[menu __main __filament __loads]
type: command
name: Load Fil. slow
enable: False

[menu __main __filament __unloadf]
type: command
name: Unload Fil.fast
enable: False

[menu __main __filament __unloads]
type: command
name: Unload Fil.slow
enable: False

# Add new load/unload using our macros.
[menu __main __filament __load]
type: command
index: 1
name: Load Filament
gcode:
  LOAD_FILAMENT

[menu __main __filament __unload]
type: command
index: 2
name: Unload Filament
gcode:
  UNLOAD_FILAMENT

[menu __main __filament __feed]
type: input
name: Feed: {'%.1f' % menu.input}
input: 0.0
input_min: -50.0
input_max: 50.0
input_step: 1.0
gcode:
  SAVE_GCODE_STATE NAME=_KM_FILAMENT_LOAD
  M83
  G1 E{'%.1f' % menu.input
   } F{printer["gcode_macro _km_globals"].load_priming_speed}
  RESTORE_GCODE_STATE NAME=_KM_FILAMENT_LOAD

################################################################################
# Layer triggers.
################################################################################

[menu __main __sdcard __pause_layer]
type: input
index: 4
enable: {('virtual_sdcard' in printer) and
         printer.print_stats.state == "printing"}
name: Pause {% set layer = printer.print_stats.info.current_layer %}{%
               if layer >= menu.input|int %}layer{%
               elif layer + 1 == menu.input|int %}at: next{%
               else %}at: {menu.input|int}{% endif %}
input: {printer.print_stats.info.current_layer}
input_min: {printer.print_stats.info.current_layer}
input_max: {printer.print_stats.info.total_layer}
input_step: 1
gcode:
  {% if menu.input|int > printer.print_stats.info.current_layer %}
    PAUSE_AT_LAYER LAYER={menu.input}
  {% endif %}

[menu __main __octoprint __pause_layer]
type: input
index: 1
enable: {printer.idle_timeout.state == "Printing"}
name: Pause {% set layer = printer.print_stats.info.current_layer %}{%
               if layer >= menu.input|int %}layer{%
               elif layer + 1 == menu.input|int + 1 %}at: next{%
               else %}at: {menu.input|int}{% endif %}
input: {printer.print_stats.info.current_layer}
input_min: {printer.print_stats.info.current_layer}
input_max: {printer.print_stats.info.total_layer}
input_step: 1
gcode:
  {% if menu.input|int > printer.print_stats.info.current_layer %}
    GCODE_AT_LAYER LAYER={menu.input|int
                 } COMMAND="RESPOND TYPE=command MSG=action:pause"
  {% endif %}

################################################################################
# Bed surface.
################################################################################

[menu __main __setup __bed_surface]
type: input
name: Bed: {(printer.save_variables.variables.bed_surfaces.available
             | list | sort)[menu.input|int]}
input: {% set surfaces = printer.save_variables.variables.bed_surfaces
         %}{(surfaces.available | list | sort).index(surfaces.active) | int}
input_min: 0
input_max: {((printer.save_variables.variables.bed_surfaces.available
              | length) - 1)}
input_step: 1
index: 4
gcode:
  SET_SURFACE_ACTIVE SURFACE={
      (printer.save_variables.variables.bed_surfaces.available
       | list | sort)[menu.input|int]}

[menu __main __setup __offsetz]
type: input
name: Offset Z:{ '%05.3f' % menu.input }
input: {% set surfaces = printer.save_variables.variables.bed_surfaces
         %}{ surfaces.available[surfaces.active].offset | float }
input_min: -5
input_max: 5
input_step: 0.005
index: 5
realtime: True
gcode:
  SET_SURFACE_OFFSET OFFSET={menu.input}

[menu __main __tune __offsetz]
type: input
name: Offset Z:{ '%05.3f' % menu.input }
input: { printer.gcode_move.homing_origin.z }
input_min: -5
input_max: 5
input_step: 0.005
realtime: True
gcode:
  SET_SURFACE_OFFSET OFFSET={menu.input}

################################################################################
# Heater overrides.
################################################################################

### menu temperature ###

# Hide the original menu
[menu __main __temp]
type: list
name: Temperature
enable: False

[menu __main __temp_km]
type: list
name: Temperature
index: 4

[menu __main __temp_km __hotend0_target]
type: input
enable: {'extruder' in printer}
name: {"Ex0:%3.0f (%4.0f)" % (menu.input, printer.extruder.temperature)}
input: {printer.extruder.target}
input_min: 0
input_max: {printer.configfile.config.extruder.max_temp}
input_step: 1
gcode: SET_HEATER_TEMPERATURE HEATER=extruder TARGET={'%.0f' % menu.input}

[menu __main __temp_km __hotend1_target]
type: input
enable: {'extruder1' in printer}
name: {"Ex1:%3.0f (%4.0f)" % (menu.input, printer.extruder1.temperature)}
input: {printer.extruder1.target}
input_min: 0
input_max: {printer.configfile.config.extruder1.max_temp}
input_step: 1
gcode: SET_HEATER_TEMPERATURE HEATER=extruder1 TARGET={'%.0f' % menu.input}

[menu __main __temp_km __hotbed_target]
type: input
enable: {'heater_bed' in printer}
name: {"Bed:%3.0f (%4.0f)" % (menu.input, printer.heater_bed.temperature)}
input: {printer.heater_bed.target}
input_min: 0
input_max: {printer.configfile.config.heater_bed.max_temp}
input_step: 1
gcode: SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={'%.0f' % menu.input}

# We just create 10 preheat placeholders below, and dynamically enable and fill
# them in from the dictionary in variable_menu_temperature.

## Pre-heat [0] ##
[menu __main __temp_km __preheat_0]
type: list
name: Preheat {printer["gcode_macro _km_globals"].menu_temperature[0].name}
enable: {printer.idle_timeout.state != "Printing" and
         printer["gcode_macro _km_globals"].menu_temperature|length > 0}

[menu __main __temp_km __preheat_0 __all]
type: command
enable: {printer["gcode_macro _km_globals"].menu_temperature[0] | list |
           select('in', ['extruder', 'bed', 'chamber']) | list | length > 1 and
         (('extruder' in printer) + ('heater_bed' in printer) +
          ('heater_generic chamber' in printer.heaters.available_heaters)) > 1}
name: Preheat all
gcode:
  {% set targets = printer["gcode_macro _km_globals"].menu_temperature[0] %}
  {% if 'chamber' in targets and
        'heater_generic chamber' in printer.heaters.available_heaters %}
    SET_HEATER_TEMPERATURE HEATER=chamber TARGET={targets.chamber}
  {% endif %}
  {% if 'bed' in targets and 'heater_bed' in printer %}
    SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={targets.bed}
  {% endif %}
  {% if 'extruder' in targets and 'extruder' in printer %}
    SET_HEATER_TEMPERATURE HEATER={printer.toolhead.extruder} TARGET={
      targets.extruder}
  {% endif %}

[menu __main __temp_km __preheat_0 __hotend]
type: command
enable: {'extruder' in printer and
         'extruder' in printer["gcode_macro _km_globals"].menu_temperature[0]}
name: Preheat hotend
gcode:
  SET_HEATER_TEMPERATURE HEATER={printer.toolhead.extruder} TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[0].extruder}

[menu __main __temp_km __preheat_0 __hotbed]
type: command
enable: {'heater_bed' in printer and
         'bed' in printer["gcode_macro _km_globals"].menu_temperature[0]}
name: Preheat bed
gcode:
  SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[0].bed}

[menu __main __temp_km __preheat_0 __chamber]
type: command
enable: {'heater_generic chamber' in printer.heaters.available_heaters and
         'chamber' in printer["gcode_macro _km_globals"].menu_temperature[0]}
name: Preheat chamber
gcode:
  SET_HEATER_TEMPERATURE HEATER=chamber TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[0].chamber}

## Pre-heat [1] ##
[menu __main __temp_km __preheat_1]
type: list
name: Preheat {printer["gcode_macro _km_globals"].menu_temperature[1].name}
enable: {printer.idle_timeout.state != "Printing" and
         printer["gcode_macro _km_globals"].menu_temperature|length > 1}

[menu __main __temp_km __preheat_1 __all]
type: command
enable: {printer["gcode_macro _km_globals"].menu_temperature[1] | list |
           select('in', ['extruder', 'bed', 'chamber']) | list | length > 1 and
         (('extruder' in printer) + ('heater_bed' in printer) +
          ('heater_generic chamber' in printer.heaters.available_heaters)) > 1}
name: Preheat all
gcode:
  {% set targets = printer["gcode_macro _km_globals"].menu_temperature[1] %}
  {% if 'chamber' in targets and
        'heater_generic chamber' in printer.heaters.available_heaters %}
    SET_HEATER_TEMPERATURE HEATER=chamber TARGET={targets.chamber}
  {% endif %}
  {% if 'bed' in targets and 'heater_bed' in printer %}
    SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={targets.bed}
  {% endif %}
  {% if 'extruder' in targets and 'extruder' in printer %}
    SET_HEATER_TEMPERATURE HEATER={printer.toolhead.extruder} TARGET={
      targets.extruder}
  {% endif %}

[menu __main __temp_km __preheat_1 __hotend]
type: command
enable: {'extruder' in printer and
         'extruder' in printer["gcode_macro _km_globals"].menu_temperature[1]}
name: Preheat hotend
gcode:
  SET_HEATER_TEMPERATURE HEATER={printer.toolhead.extruder} TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[1].extruder}

[menu __main __temp_km __preheat_1 __hotbed]
type: command
enable: {'heater_bed' in printer and
         'bed' in printer["gcode_macro _km_globals"].menu_temperature[1]}
name: Preheat bed
gcode:
  SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[1].bed}

[menu __main __temp_km __preheat_1 __chamber]
type: command
enable: {'heater_generic chamber' in printer.heaters.available_heaters and
         'chamber' in printer["gcode_macro _km_globals"].menu_temperature[1]}
name: Preheat chamber
gcode:
  SET_HEATER_TEMPERATURE HEATER=chamber TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[1].chamber}

## Pre-heat [2] ##
[menu __main __temp_km __preheat_2]
type: list
name: Preheat {printer["gcode_macro _km_globals"].menu_temperature[2].name}
enable: {printer.idle_timeout.state != "Printing" and
         printer["gcode_macro _km_globals"].menu_temperature|length > 2}

[menu __main __temp_km __preheat_2 __all]
type: command
enable: {printer["gcode_macro _km_globals"].menu_temperature[2] | list |
           select('in', ['extruder', 'bed', 'chamber']) | list | length > 1 and
         (('extruder' in printer) + ('heater_bed' in printer) +
          ('heater_generic chamber' in printer.heaters.available_heaters)) > 1}
name: Preheat all
gcode:
  {% set targets = printer["gcode_macro _km_globals"].menu_temperature[2] %}
  {% if 'chamber' in targets and
        'heater_generic chamber' in printer.heaters.available_heaters %}
    SET_HEATER_TEMPERATURE HEATER=chamber TARGET={targets.chamber}
  {% endif %}
  {% if 'bed' in targets and 'heater_bed' in printer %}
    SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={targets.bed}
  {% endif %}
  {% if 'extruder' in targets and 'extruder' in printer %}
    SET_HEATER_TEMPERATURE HEATER={printer.toolhead.extruder} TARGET={
      targets.extruder}
  {% endif %}

[menu __main __temp_km __preheat_2 __hotend]
type: command
enable: {'extruder' in printer and
         'extruder' in printer["gcode_macro _km_globals"].menu_temperature[2]}
name: Preheat hotend
gcode:
  SET_HEATER_TEMPERATURE HEATER={printer.toolhead.extruder} TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[2].extruder}

[menu __main __temp_km __preheat_2 __hotbed]
type: command
enable: {'heater_bed' in printer and
         'bed' in printer["gcode_macro _km_globals"].menu_temperature[2]}
name: Preheat bed
gcode:
  SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[2].bed}

[menu __main __temp_km __preheat_2 __chamber]
type: command
enable: {'heater_generic chamber' in printer.heaters.available_heaters and
         'chamber' in printer["gcode_macro _km_globals"].menu_temperature[2]}
name: Preheat chamber
gcode:
  SET_HEATER_TEMPERATURE HEATER=chamber TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[2].chamber}

## Pre-heat [3] ##
[menu __main __temp_km __preheat_3]
type: list
name: Preheat {printer["gcode_macro _km_globals"].menu_temperature[3].name}
enable: {printer.idle_timeout.state != "Printing" and
         printer["gcode_macro _km_globals"].menu_temperature|length > 3}

[menu __main __temp_km __preheat_3 __all]
type: command
enable: {printer["gcode_macro _km_globals"].menu_temperature[3] | list |
           select('in', ['extruder', 'bed', 'chamber']) | list | length > 1 and
         (('extruder' in printer) + ('heater_bed' in printer) +
          ('heater_generic chamber' in printer.heaters.available_heaters)) > 1}
name: Preheat all
gcode:
  {% set targets = printer["gcode_macro _km_globals"].menu_temperature[3] %}
  {% if 'chamber' in targets and
        'heater_generic chamber' in printer.heaters.available_heaters %}
    SET_HEATER_TEMPERATURE HEATER=chamber TARGET={targets.chamber}
  {% endif %}
  {% if 'bed' in targets and 'heater_bed' in printer %}
    SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={targets.bed}
  {% endif %}
  {% if 'extruder' in targets and 'extruder' in printer %}
    SET_HEATER_TEMPERATURE HEATER={printer.toolhead.extruder} TARGET={
      targets.extruder}
  {% endif %}

[menu __main __temp_km __preheat_3 __hotend]
type: command
enable: {'extruder' in printer and
         'extruder' in printer["gcode_macro _km_globals"].menu_temperature[3]}
name: Preheat hotend
gcode:
  SET_HEATER_TEMPERATURE HEATER={printer.toolhead.extruder} TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[3].extruder}

[menu __main __temp_km __preheat_3 __hotbed]
type: command
enable: {'heater_bed' in printer and
         'bed' in printer["gcode_macro _km_globals"].menu_temperature[3]}
name: Preheat bed
gcode:
  SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[3].bed}

[menu __main __temp_km __preheat_3 __chamber]
type: command
enable: {'heater_generic chamber' in printer.heaters.available_heaters and
         'chamber' in printer["gcode_macro _km_globals"].menu_temperature[3]}
name: Preheat chamber
gcode:
  SET_HEATER_TEMPERATURE HEATER=chamber TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[3].chamber}

## Pre-heat [4] ##
[menu __main __temp_km __preheat_4]
type: list
name: Preheat {printer["gcode_macro _km_globals"].menu_temperature[4].name}
enable: {printer.idle_timeout.state != "Printing" and
         printer["gcode_macro _km_globals"].menu_temperature|length > 4}

[menu __main __temp_km __preheat_4 __all]
type: command
enable: {printer["gcode_macro _km_globals"].menu_temperature[4] | list |
           select('in', ['extruder', 'bed', 'chamber']) | list | length > 1 and
         (('extruder' in printer) + ('heater_bed' in printer) +
          ('heater_generic chamber' in printer.heaters.available_heaters)) > 1}
name: Preheat all
gcode:
  {% set targets = printer["gcode_macro _km_globals"].menu_temperature[4] %}
  {% if 'chamber' in targets and
        'heater_generic chamber' in printer.heaters.available_heaters %}
    SET_HEATER_TEMPERATURE HEATER=chamber TARGET={targets.chamber}
  {% endif %}
  {% if 'bed' in targets and 'heater_bed' in printer %}
    SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={targets.bed}
  {% endif %}
  {% if 'extruder' in targets and 'extruder' in printer %}
    SET_HEATER_TEMPERATURE HEATER={printer.toolhead.extruder} TARGET={
      targets.extruder}
  {% endif %}

[menu __main __temp_km __preheat_4 __hotend]
type: command
enable: {'extruder' in printer and
         'extruder' in printer["gcode_macro _km_globals"].menu_temperature[4]}
name: Preheat hotend
gcode:
  SET_HEATER_TEMPERATURE HEATER={printer.toolhead.extruder} TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[4].extruder}

[menu __main __temp_km __preheat_4 __hotbed]
type: command
enable: {'heater_bed' in printer and
         'bed' in printer["gcode_macro _km_globals"].menu_temperature[4]}
name: Preheat bed
gcode:
  SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[4].bed}

[menu __main __temp_km __preheat_4 __chamber]
type: command
enable: {'heater_generic chamber' in printer.heaters.available_heaters and
         'chamber' in printer["gcode_macro _km_globals"].menu_temperature[4]}
name: Preheat chamber
gcode:
  SET_HEATER_TEMPERATURE HEATER=chamber TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[4].chamber}

## Pre-heat [5] ##
[menu __main __temp_km __preheat_5]
type: list
name: Preheat {printer["gcode_macro _km_globals"].menu_temperature[5].name}
enable: {printer.idle_timeout.state != "Printing" and
         printer["gcode_macro _km_globals"].menu_temperature|length > 5}

[menu __main __temp_km __preheat_5 __all]
type: command
enable: {printer["gcode_macro _km_globals"].menu_temperature[5] | list |
           select('in', ['extruder', 'bed', 'chamber']) | list | length > 1 and
         (('extruder' in printer) + ('heater_bed' in printer) +
          ('heater_generic chamber' in printer.heaters.available_heaters)) > 1}
name: Preheat all
gcode:
  {% set targets = printer["gcode_macro _km_globals"].menu_temperature[5] %}
  {% if 'chamber' in targets and
        'heater_generic chamber' in printer.heaters.available_heaters %}
    SET_HEATER_TEMPERATURE HEATER=chamber TARGET={targets.chamber}
  {% endif %}
  {% if 'bed' in targets and 'heater_bed' in printer %}
    SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={targets.bed}
  {% endif %}
  {% if 'extruder' in targets and 'extruder' in printer %}
    SET_HEATER_TEMPERATURE HEATER={printer.toolhead.extruder} TARGET={
      targets.extruder}
  {% endif %}

[menu __main __temp_km __preheat_5 __hotend]
type: command
enable: {'extruder' in printer and
         'extruder' in printer["gcode_macro _km_globals"].menu_temperature[5]}
name: Preheat hotend
gcode:
  SET_HEATER_TEMPERATURE HEATER={printer.toolhead.extruder} TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[5].extruder}

[menu __main __temp_km __preheat_5 __hotbed]
type: command
enable: {'heater_bed' in printer and
         'bed' in printer["gcode_macro _km_globals"].menu_temperature[5]}
name: Preheat bed
gcode:
  SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[5].bed}

[menu __main __temp_km __preheat_5 __chamber]
type: command
enable: {'heater_generic chamber' in printer.heaters.available_heaters and
         'chamber' in printer["gcode_macro _km_globals"].menu_temperature[5]}
name: Preheat chamber
gcode:
  SET_HEATER_TEMPERATURE HEATER=chamber TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[5].chamber}

## Pre-heat [6] ##
[menu __main __temp_km __preheat_6]
type: list
name: Preheat {printer["gcode_macro _km_globals"].menu_temperature[6].name}
enable: {printer.idle_timeout.state != "Printing" and
         printer["gcode_macro _km_globals"].menu_temperature|length > 6}

[menu __main __temp_km __preheat_6 __all]
type: command
enable: {printer["gcode_macro _km_globals"].menu_temperature[6] | list |
           select('in', ['extruder', 'bed', 'chamber']) | list | length > 1 and
         (('extruder' in printer) + ('heater_bed' in printer) +
          ('heater_generic chamber' in printer.heaters.available_heaters)) > 1}
name: Preheat all
gcode:
  {% set targets = printer["gcode_macro _km_globals"].menu_temperature[6] %}
  {% if 'chamber' in targets and
        'heater_generic chamber' in printer.heaters.available_heaters %}
    SET_HEATER_TEMPERATURE HEATER=chamber TARGET={targets.chamber}
  {% endif %}
  {% if 'bed' in targets and 'heater_bed' in printer %}
    SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={targets.bed}
  {% endif %}
  {% if 'extruder' in targets and 'extruder' in printer %}
    SET_HEATER_TEMPERATURE HEATER={printer.toolhead.extruder} TARGET={
      targets.extruder}
  {% endif %}

[menu __main __temp_km __preheat_6 __hotend]
type: command
enable: {'extruder' in printer and
         'extruder' in printer["gcode_macro _km_globals"].menu_temperature[6]}
name: Preheat hotend
gcode:
  SET_HEATER_TEMPERATURE HEATER={printer.toolhead.extruder} TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[6].extruder}

[menu __main __temp_km __preheat_6 __hotbed]
type: command
enable: {'heater_bed' in printer and
         'bed' in printer["gcode_macro _km_globals"].menu_temperature[6]}
name: Preheat bed
gcode:
  SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[6].bed}

[menu __main __temp_km __preheat_6 __chamber]
type: command
enable: {'heater_generic chamber' in printer.heaters.available_heaters and
         'chamber' in printer["gcode_macro _km_globals"].menu_temperature[6]}
name: Preheat chamber
gcode:
  SET_HEATER_TEMPERATURE HEATER=chamber TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[6].chamber}

## Pre-heat [7] ##
[menu __main __temp_km __preheat_7]
type: list
name: Preheat {printer["gcode_macro _km_globals"].menu_temperature[7].name}
enable: {printer.idle_timeout.state != "Printing" and
         printer["gcode_macro _km_globals"].menu_temperature|length > 7}

[menu __main __temp_km __preheat_7 __all]
type: command
enable: {printer["gcode_macro _km_globals"].menu_temperature[7] | list |
           select('in', ['extruder', 'bed', 'chamber']) | list | length > 1 and
         (('extruder' in printer) + ('heater_bed' in printer) +
          ('heater_generic chamber' in printer.heaters.available_heaters)) > 1}
name: Preheat all
gcode:
  {% set targets = printer["gcode_macro _km_globals"].menu_temperature[7] %}
  {% if 'chamber' in targets and
        'heater_generic chamber' in printer.heaters.available_heaters %}
    SET_HEATER_TEMPERATURE HEATER=chamber TARGET={targets.chamber}
  {% endif %}
  {% if 'bed' in targets and 'heater_bed' in printer %}
    SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={targets.bed}
  {% endif %}
  {% if 'extruder' in targets and 'extruder' in printer %}
    SET_HEATER_TEMPERATURE HEATER={printer.toolhead.extruder} TARGET={
      targets.extruder}
  {% endif %}

[menu __main __temp_km __preheat_7 __hotend]
type: command
enable: {'extruder' in printer and
         'extruder' in printer["gcode_macro _km_globals"].menu_temperature[7]}
name: Preheat hotend
gcode:
  SET_HEATER_TEMPERATURE HEATER={printer.toolhead.extruder} TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[7].extruder}

[menu __main __temp_km __preheat_7 __hotbed]
type: command
enable: {'heater_bed' in printer and
         'bed' in printer["gcode_macro _km_globals"].menu_temperature[7]}
name: Preheat bed
gcode:
  SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[7].bed}

[menu __main __temp_km __preheat_7 __chamber]
type: command
enable: {'heater_generic chamber' in printer.heaters.available_heaters and
         'chamber' in printer["gcode_macro _km_globals"].menu_temperature[7]}
name: Preheat chamber
gcode:
  SET_HEATER_TEMPERATURE HEATER=chamber TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[7].chamber}

## Pre-heat [8] ##
[menu __main __temp_km __preheat_8]
type: list
name: Preheat {printer["gcode_macro _km_globals"].menu_temperature[8].name}
enable: {printer.idle_timeout.state != "Printing" and
         printer["gcode_macro _km_globals"].menu_temperature|length > 8}

[menu __main __temp_km __preheat_8 __all]
type: command
enable: {printer["gcode_macro _km_globals"].menu_temperature[8] | list |
           select('in', ['extruder', 'bed', 'chamber']) | list | length > 1 and
         (('extruder' in printer) + ('heater_bed' in printer) +
          ('heater_generic chamber' in printer.heaters.available_heaters)) > 1}
name: Preheat all
gcode:
  {% set targets = printer["gcode_macro _km_globals"].menu_temperature[8] %}
  {% if 'chamber' in targets and
        'heater_generic chamber' in printer.heaters.available_heaters %}
    SET_HEATER_TEMPERATURE HEATER=chamber TARGET={targets.chamber}
  {% endif %}
  {% if 'bed' in targets and 'heater_bed' in printer %}
    SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={targets.bed}
  {% endif %}
  {% if 'extruder' in targets and 'extruder' in printer %}
    SET_HEATER_TEMPERATURE HEATER={printer.toolhead.extruder} TARGET={
      targets.extruder}
  {% endif %}

[menu __main __temp_km __preheat_8 __hotend]
type: command
enable: {'extruder' in printer and
         'extruder' in printer["gcode_macro _km_globals"].menu_temperature[8]}
name: Preheat hotend
gcode:
  SET_HEATER_TEMPERATURE HEATER={printer.toolhead.extruder} TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[8].extruder}

[menu __main __temp_km __preheat_8 __hotbed]
type: command
enable: {'heater_bed' in printer and
         'bed' in printer["gcode_macro _km_globals"].menu_temperature[8]}
name: Preheat bed
gcode:
  SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[8].bed}

[menu __main __temp_km __preheat_8 __chamber]
type: command
enable: {'heater_generic chamber' in printer.heaters.available_heaters and
         'chamber' in printer["gcode_macro _km_globals"].menu_temperature[8]}
name: Preheat chamber
gcode:
  SET_HEATER_TEMPERATURE HEATER=chamber TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[8].chamber}

## Pre-heat [9] ##
[menu __main __temp_km __preheat_9]
type: list
name: Preheat {printer["gcode_macro _km_globals"].menu_temperature[9].name}
enable: {printer.idle_timeout.state != "Printing" and
         printer["gcode_macro _km_globals"].menu_temperature|length > 9}

[menu __main __temp_km __preheat_9 __all]
type: command
enable: {printer["gcode_macro _km_globals"].menu_temperature[9] | list |
           select('in', ['extruder', 'bed', 'chamber']) | list | length > 1 and
         (('extruder' in printer) + ('heater_bed' in printer) +
          ('heater_generic chamber' in printer.heaters.available_heaters)) > 1}
name: Preheat all
gcode:
  {% set targets = printer["gcode_macro _km_globals"].menu_temperature[9] %}
  {% if 'chamber' in targets and
        'heater_generic chamber' in printer.heaters.available_heaters %}
    SET_HEATER_TEMPERATURE HEATER=chamber TARGET={targets.chamber}
  {% endif %}
  {% if 'bed' in targets and 'heater_bed' in printer %}
    SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={targets.bed}
  {% endif %}
  {% if 'extruder' in targets and 'extruder' in printer %}
    SET_HEATER_TEMPERATURE HEATER={printer.toolhead.extruder} TARGET={
      targets.extruder}
  {% endif %}

[menu __main __temp_km __preheat_9 __hotend]
type: command
enable: {'extruder' in printer and
         'extruder' in printer["gcode_macro _km_globals"].menu_temperature[9]}
name: Preheat hotend
gcode:
  SET_HEATER_TEMPERATURE HEATER={printer.toolhead.extruder} TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[9].extruder}

[menu __main __temp_km __preheat_9 __hotbed]
type: command
enable: {'heater_bed' in printer and
         'bed' in printer["gcode_macro _km_globals"].menu_temperature[9]}
name: Preheat bed
gcode:
  SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[9].bed}

[menu __main __temp_km __preheat_9 __chamber]
type: command
enable: {'heater_generic chamber' in printer.heaters.available_heaters and
         'chamber' in printer["gcode_macro _km_globals"].menu_temperature[9]}
name: Preheat chamber
gcode:
  SET_HEATER_TEMPERATURE HEATER=chamber TARGET={
    printer["gcode_macro _km_globals"].menu_temperature[9].chamber}

## Cooldown ##
[menu __main __temp_km __cooldown_all]
type: command
enable: {printer.idle_timeout.state != "Printing" and
         (('extruder' in printer) + ('heater_bed' in printer) +
          ('heater_generic chamber' in printer.heaters.available_heaters)) > 1}
name: Cooldown all
gcode:
  {% if 'heater_generic chamber' in printer.heaters.available_heaters %}
    SET_HEATER_TEMPERATURE HEATER=chamber TARGET=0
  {% endif %}
  {% if 'heater_bed' in printer %}
    SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET=0
  {% endif %}
  {% if 'extruder' in printer%}
    SET_HEATER_TEMPERATURE HEATER={printer.toolhead.extruder} TARGET=0
  {% endif %}

[menu __main __temp_km __cooldown_hotend]
type: command
enable: {printer.idle_timeout.state != "Printing" and 'extruder' in printer}
name: Cooldown hotend
gcode: M104 S0

[menu __main __temp_km __cooldown_hotbed]
type: command
enable: {printer.idle_timeout.state != "Printing" and 'heater_bed' in printer}
name: Cooldown bed
gcode: M140 S0

[menu __main __temp_km __cooldown_chamber]
type: command
enable: {printer.idle_timeout.state != "Printing" and
         'heater_generic chamber' in printer.heaters.available_heaters}
name: Cooldown chamber
gcode: M141 S0
