###########################################
# Additional Macros for Creality Ender-3 V3
###########################################

[respond]

[virtual_pins]

[output_pin ADAPTIVE_BED_MESH]
pin: virtual_pin:ADAPTIVE_BED_MESH_pin
value: 1

[output_pin FULL_BED_MESH]
pin: virtual_pin:FULL_BED_MESH_pin
value: 0

[output_pin ADAPTIVE_PURGE_LINE]
pin: virtual_pin:ADAPTIVE_PURGE_LINE_pin
value: 1


[gcode_macro _ADAPTIVE_BED_MESH_ON]
description: Enable Adaptive Bed Mesh and Disable Full Bed Mesh
gcode:
  SET_PIN PIN=ADAPTIVE_BED_MESH VALUE=1
  SET_PIN PIN=FULL_BED_MESH VALUE=0
  RESPOND TYPE=command MSG="action:prompt_end"


[gcode_macro _FULL_BED_MESH_ON]
description: Disable Adaptive Bed Mesh and Enable Full Bed Mesh
gcode:
  SET_PIN PIN=ADAPTIVE_BED_MESH VALUE=0
  SET_PIN PIN=FULL_BED_MESH VALUE=1
  RESPOND TYPE=command MSG="action:prompt_end"


[gcode_macro _BED_MESH_OFF]
description: Disable Adaptive Bed Mesh and Enable Full Bed Mesh
gcode:
  SET_PIN PIN=ADAPTIVE_BED_MESH VALUE=0
  SET_PIN PIN=FULL_BED_MESH VALUE=0
  RESPOND TYPE=command MSG="action:prompt_end"


[gcode_macro _ADAPTIVE_PURGE_LINE_ON]
description: Enable Adaptive Purge Line
gcode:
  SET_PIN PIN=ADAPTIVE_PURGE_LINE VALUE=1
  RESPOND TYPE=command MSG="action:prompt_end"


[gcode_macro _ADAPTIVE_PURGE_LINE_OFF]
description: Disable Adaptive Purge Line
gcode:
  SET_PIN PIN=ADAPTIVE_PURGE_LINE VALUE=0
  RESPOND TYPE=command MSG="action:prompt_end"


[gcode_macro KAMP_BED_MESH_SETTINGS]
description: Configure Bed Mesh type
gcode:
  RESPOND TYPE=command MSG="action:prompt_begin Bed Mesh Settings"
  RESPOND TYPE=command MSG="action:prompt_text What type of bed mesh do you want to use when you start printing?"
  RESPOND TYPE=command MSG="action:prompt_button ADAPTIVE BED MESH|_ADAPTIVE_BED_MESH_ON|primary"
  RESPOND TYPE=command MSG="action:prompt_button FULL BED MESH|_FULL_BED_MESH_ON|primary"
  RESPOND TYPE=command MSG="action:prompt_button NONE|_BED_MESH_OFF|primary"
  RESPOND TYPE=command MSG="action:prompt_show"


[gcode_macro KAMP_PURGE_LINE_SETTINGS]
description: Configure Purge Line type
gcode:
  RESPOND TYPE=command MSG="action:prompt_begin Purge Line Settings"
  RESPOND TYPE=command MSG="action:prompt_text What type of purge line do you want to use when you start printing?"
  RESPOND TYPE=command MSG="action:prompt_button ADAPTIVE PURGE LINE|_ADAPTIVE_PURGE_LINE_ON|primary"
  RESPOND TYPE=command MSG="action:prompt_button CLASSIC PURGE LINE|_ADAPTIVE_PURGE_LINE_OFF|primary"
  RESPOND TYPE=command MSG="action:prompt_show"


[gcode_macro START_PRINT]
variable_prepare: 0
variable_z_sensorless: 0
gcode:
  SET_GCODE_VARIABLE MACRO=M106 VARIABLE=user_flag VALUE=0
  WAIT_TEMP_END
  CLEAR_PAUSE
  SET_TEMPERATURE_FAN_TARGET temperature_fan=soc_fan target=5
  {% set g28_extruder_temp = printer.custom_macro.g28_ext_temp %}
  {% set bed_temp = printer.custom_macro.default_bed_temp %}
  {% set extruder_temp = printer.custom_macro.default_extruder_temp %}
  {% if 'BED_TEMP' in params|upper and (params.BED_TEMP|float) %}
    {% set bed_temp = params.BED_TEMP %}
  {% endif %}
  {% if 'EXTRUDER_TEMP' in params|upper and (params.EXTRUDER_TEMP|float) %}
    {% set extruder_temp = params.EXTRUDER_TEMP %}
  {% endif %}
  {% if printer['gcode_macro START_PRINT'].prepare|int == 0 %}
    {action_respond_info("not prepare.\n")}
    PRINT_PREPARE_CLEAR
    CX_ROUGH_G28 EXTRUDER_TEMP={extruder_temp} BED_TEMP={bed_temp}
    CX_NOZZLE_CLEAR
    Z_SENSORLESS_SET
    ACCURATE_G28
    {% if printer['output_pin ADAPTIVE_BED_MESH'].value == 1 %}
      RESPOND TYPE=command MSG="Starting Adaptative Bed Mesh..."
      BED_MESH_CLEAR
      BED_MESH_CALIBRATE
      BED_MESH_PROFILE SAVE=adaptive
      BED_MESH_PROFILE LOAD=adaptive
    {% else %}
      {% if printer['output_pin FULL_BED_MESH'].value == 0 and (not printer['bed_mesh'].profile_name) %}
        RESPOND TYPE=command MSG="No bed mesh found. Starting Full Bed Mesh..."
        CX_PRINT_LEVELING_CALIBRATION
      {% endif %}
      {% if printer['output_pin FULL_BED_MESH'].value == 1 %}
        RESPOND TYPE=command MSG="Starting Full Bed Mesh..."
        CX_PRINT_LEVELING_CALIBRATION
      {% endif %}
      BED_MESH_PROFILE LOAD=default
    {% endif %}
  {% else %}
    PRINT_PREPARE_CLEAR
  {% endif %}
  {% if printer['output_pin ADAPTIVE_PURGE_LINE'].value == 1 %}
    _SMART_PARK
    M109 S{extruder_temp}
    M190 S{bed_temp}
    RESPOND TYPE=command MSG="Starting Adaptive Purge Line..."
    _LINE_PURGE
  {% else %}  
    RESPOND TYPE=command MSG="Starting Classic Purge Line..."
    CX_PRINT_DRAW_ONE_LINE
  {% endif %}
  SET_VELOCITY_LIMIT ACCEL={printer.configfile.settings.printer.max_accel}
