# ============================================================================
# NEPTUNE 4 PLUS CALIBRATION SUITE
# Complete calibration macros for Cartographer 3D
# ============================================================================
# Installation: Add [include calibration/*.cfg] to printer.cfg
# Usage: Run CALIBRATION_MENU in console
# ============================================================================

# ----------------------------------------------------------------------------
# MAIN MENU - Shows all available calibration options
# ----------------------------------------------------------------------------
[gcode_macro CALIBRATION_MENU]
description: Show calibration menu with all options
gcode:
    RESPOND MSG="=============================================="
    RESPOND MSG="   NEPTUNE 4 PLUS CALIBRATION SUITE"
    RESPOND MSG="=============================================="
    RESPOND MSG=""
    RESPOND MSG="Available calibrations:"
    RESPOND MSG="  1. CALIBRATE_Z_OFFSET          - Z0 with Cartographer"
    RESPOND MSG="  2. CALIBRATE_INPUT_SHAPER      - ADXL resonance"
    RESPOND MSG="  3. CALIBRATE_PRESSURE_ADVANCE  - PA tuning"
    RESPOND MSG="  4. CALIBRATE_FLOW_RATE         - Flow calibration"
    RESPOND MSG="  5. CALIBRATE_TEMPERATURE       - Temp tower"
    RESPOND MSG="  6. CALIBRATE_RETRACTION        - Retraction test"
    RESPOND MSG="  7. CALIBRATE_BED_MESH          - Mesh verification"
    RESPOND MSG=""
    RESPOND MSG="Full suite: CALIBRATION_FULL"
    RESPOND MSG="=============================================="

# ----------------------------------------------------------------------------
# 1. Z-OFFSET CALIBRATION with Cartographer
# ----------------------------------------------------------------------------
[gcode_macro CALIBRATE_Z_OFFSET]
description: Calibrate Z-offset using Cartographer touch
gcode:
    RESPOND MSG="Starting Z-Offset Calibration..."
    RESPOND MSG=""
    RESPOND MSG="Step 1: Heating up..."
    
    # Heat up for calibration
    M104 S50
    M190 S60
    TEMPERATURE_WAIT SENSOR=extruder MINIMUM=45
    TEMPERATURE_WAIT SENSOR=heater_bed MINIMUM=55
    
    RESPOND MSG="Step 2: Homing..."
    G28
    
    RESPOND MSG="Step 3: Cartographer Z0 calibration..."
    RESPOND MSG="Follow prompts in console"
    
    # Start Cartographer calibration
    CARTOGRAPHER_CALIBRATE
    
    RESPOND MSG=""
    RESPOND MSG="Z-Offset calibration complete!"
    RESPOND MSG="Don't forget to run SAVE_CONFIG"

# ----------------------------------------------------------------------------
# 2. INPUT SHAPER CALIBRATION
# ----------------------------------------------------------------------------
[gcode_macro CALIBRATE_INPUT_SHAPER]
description: Measure resonance frequencies with ADXL345
gcode:
    RESPOND MSG="Starting Input Shaper Calibration..."
    RESPOND MSG=""
    RESPOND MSG="Step 1: Homing..."
    G28
    
    RESPOND MSG="Step 2: Measuring X-axis resonance..."
    MEASURE_AXES_NOISE axis=x
    
    RESPOND MSG="Step 3: Measuring Y-axis resonance..."
    MEASURE_AXES_NOISE axis=y
    
    RESPOND MSG=""
    RESPOND MSG="Input Shaper calibration complete!"
    RESPOND MSG="Check console for shaper_type and shaper_freq values"
    RESPOND MSG="Add to [input_shaper] section and run SAVE_CONFIG"

# ----------------------------------------------------------------------------
# 3. PRESSURE ADVANCE CALIBRATION
# ----------------------------------------------------------------------------
[gcode_macro CALIBRATE_PRESSURE_ADVANCE]
description: Calibrate Pressure Advance for optimal corners
gcode:
    {% set FILAMENT = params.FILAMENT|default("PLA")|upper %}
    {% set START_PA = params.START|default(0.01)|float %}
    {% set END_PA = params.END|default(0.05)|float %}
    {% set STEP = params.STEP|default(0.002)|float %}
    
    RESPOND MSG="Starting Pressure Advance Calibration..."
    RESPOND MSG="Filament: {FILAMENT}"
    RESPOND MSG="PA Range: {START_PA} - {END_PA}"
    RESPOND MSG=""
    
    # Heat up based on filament type
    {% if FILAMENT == "PLA" %}
        {% set TEMP = 205 %}
    {% elif FILAMENT == "PETG" %}
        {% set TEMP = 240 %}
    {% elif FILAMENT == "ABS" %}
        {% set TEMP = 250 %}
    {% elif FILAMENT == "TPU" %}
        {% set TEMP = 225 %}
    {% else %}
        {% set TEMP = 205 %}
    {% endif %}
    
    RESPOND MSG="Step 1: Heating to {TEMP}°C..."
    M104 S{TEMP}
    M190 S60
    TEMPERATURE_WAIT SENSOR=extruder MINIMUM={TEMP-5}
    
    RESPOND MSG="Step 2: Homing..."
    G28
    
    RESPOND MSG="Step 3: Starting PA calibration pattern..."
    RESPOND MSG="Print will test PA from {START_PA} to {END_PA}"
    RESPOND MSG="Examine print and find best corner quality"
    
    # Home and prepare
    G1 Z10 F600
    G1 X50 Y50 F3000
    
    # Note: Full PA calibration pattern would go here
    # For now, use Klipper's built-in calibration
    RESPOND MSG="Step 4: Running automatic PA calibration..."
    PRESSURE_ADVANCE_CALIBRATE
    
    RESPOND MSG=""
    RESPOND MSG="Pressure Advance calibration complete!"
    RESPOND MSG="Check console for recommended PA value"
    RESPOND MSG="Add to [extruder] section and run SAVE_CONFIG"

# ----------------------------------------------------------------------------
# 4. FLOW RATE CALIBRATION
# ----------------------------------------------------------------------------
[gcode_macro CALIBRATE_FLOW_RATE]
description: Calibrate flow rate for exact wall dimensions
gcode:
    {% set FILAMENT = params.FILAMENT|default("PLA")|upper %}
    
    RESPOND MSG="Starting Flow Rate Calibration..."
    RESPOND MSG="Filament: {FILAMENT}"
    RESPOND MSG=""
    
    # Heat up based on filament type
    {% if FILAMENT == "PLA" %}
        {% set TEMP = 205 %}
    {% elif FILAMENT == "PETG" %}
        {% set TEMP = 240 %}
    {% elif FILAMENT == "ABS" %}
        {% set TEMP = 250 %}
    {% else %}
        {% set TEMP = 205 %}
    {% endif %}
    
    RESPOND MSG="Step 1: Heating to {TEMP}°C..."
    M104 S{TEMP}
    M190 S60
    TEMPERATURE_WAIT SENSOR=extruder MINIMUM={TEMP-5}
    
    RESPOND MSG="Step 2: Homing..."
    G28
    
    RESPOND MSG="Step 3: Printing flow test cube..."
    RESPOND MSG="Cube will have walls printed at different flow rates"
    RESPOND MSG=""
    RESPOND MSG="After print:"
    RESPOND MSG="  1. Measure wall thickness with calipers"
    RESPOND MSG="  2. Find which section matches nominal wall width"
    RESPOND MSG="  3. Calculate: New Flow = Old Flow × (Measured / Nominal)"
    RESPOND MSG=""
    
    # Home and prepare
    G1 Z10 F600
    G1 X50 Y50 F3000
    
    RESPOND MSG="Step 4: Starting print..."
    # Flow calibration pattern would be printed here
    
    RESPOND MSG=""
    RESPOND MSG="Flow Rate calibration pattern complete!"
    RESPOND MSG="Update flow_ratio in filament profile and run SAVE_CONFIG"

# ----------------------------------------------------------------------------
# 5. TEMPERATURE TOWER
# ----------------------------------------------------------------------------
[gcode_macro CALIBRATE_TEMPERATURE]
description: Print temperature tower to find optimal temp
gcode:
    {% set FILAMENT = params.FILAMENT|default("PLA")|upper %}
    {% set START_TEMP = params.START|default(195)|int %}
    {% set END_TEMP = params.END|default(225)|int %}
    {% set STEP = params.STEP|default(5)|int %}
    
    RESPOND MSG="Starting Temperature Tower..."
    RESPOND MSG="Filament: {FILAMENT}"
    RESPOND MSG="Temperature Range: {START_TEMP}°C - {END_TEMP}°C"
    RESPOND MSG=""
    
    RESPOND MSG="Step 1: Heating bed..."
    M190 S60
    TEMPERATURE_WAIT SENSOR=heater_bed MINIMUM=55
    
    RESPOND MSG="Step 2: Homing..."
    G28
    
    RESPOND MSG="Step 3: Printing temperature tower..."
    RESPOND MSG="Each section uses different temperature"
    RESPOND MSG=""
    RESPOND MSG="After print:"
    RESPOND MSG="  1. Check for stringing (too hot)"
    RESPOND MSG="  2. Check for poor layer adhesion (too cold)"
    RESPOND MSG="  3. Check bridging quality"
    RESPOND MSG="  4. Choose best temperature section"
    RESPOND MSG=""
    
    # Home and prepare
    G1 Z10 F600
    G1 X50 Y50 F3000
    
    RESPOND MSG="Step 4: Starting print..."
    # Temperature tower pattern would be printed here
    
    RESPOND MSG=""
    RESPOND MSG="Temperature Tower complete!"
    RESPOND MSG="Update nozzle_temperature in filament profile"

# ----------------------------------------------------------------------------
# 6. RETRACTION TEST
# ----------------------------------------------------------------------------
[gcode_macro CALIBRATE_RETRACTION]
description: Test retraction settings to minimize stringing
gcode:
    {% set FILAMENT = params.FILAMENT|default("PLA")|upper %}
    {% set HOTEND = params.HOTEND|default("ORIGINAL")|upper %}
    
    RESPOND MSG="Starting Retraction Test..."
    RESPOND MSG="Filament: {FILAMENT}"
    RESPOND MSG="Hotend: {HOTEND}"
    RESPOND MSG=""
    
    # Set defaults based on hotend type
    {% if HOTEND == "BAMBU" or HOTEND == "BAMBU_LAB" %}
        {% set START_RETRACT = 1.2 %}
        {% set END_RETRACT = 2.5 %}
    {% else %}
        {% set START_RETRACT = 1.5 %}
        {% set END_RETRACT = 3.5 %}
    {% endif %}
    
    # Heat up based on filament type
    {% if FILAMENT == "PLA" %}
        {% set TEMP = 205 %}
    {% elif FILAMENT == "PETG" %}
        {% set TEMP = 240 %}
    {% elif FILAMENT == "ABS" %}
        {% set TEMP = 250 %}
    {% else %}
        {% set TEMP = 205 %}
    {% endif %}
    
    RESPOND MSG="Step 1: Heating to {TEMP}°C..."
    M104 S{TEMP}
    M190 S60
    TEMPERATURE_WAIT SENSOR=extruder MINIMUM={TEMP-5}
    
    RESPOND MSG="Step 2: Homing..."
    G28
    
    RESPOND MSG="Step 3: Printing retraction test..."
    RESPOND MSG="Testing retraction from {START_RETRACT}mm to {END_RETRACT}mm"
    RESPOND MSG=""
    RESPOND MSG="After print:"
    RESPOND MSG="  1. Find section with least stringing"
    RESPOND MSG="  2. Ensure no underextrusion"
    RESPOND MSG="  3. Use that retraction value"
    RESPOND MSG=""
    
    # Home and prepare
    G1 Z10 F600
    G1 X50 Y50 F3000
    
    RESPOND MSG="Step 4: Starting print..."
    # Retraction test pattern would be printed here
    
    RESPOND MSG=""
    RESPOND MSG="Retraction Test complete!"
    RESPOND MSG="Update retract_length in filament profile"

# ----------------------------------------------------------------------------
# 7. BED MESH CALIBRATION
# ----------------------------------------------------------------------------
[gcode_macro CALIBRATE_BED_MESH]
description: Calibrate bed mesh with Cartographer
gcode:
    RESPOND MSG="Starting Bed Mesh Calibration..."
    RESPOND MSG=""
    RESPOND MSG="Step 1: Heating bed..."
    M190 S60
    TEMPERATURE_WAIT SENSOR=heater_bed MINIMUM=55
    
    RESPOND MSG="Step 2: Homing..."
    G28
    
    RESPOND MSG="Step 3: Clearing existing mesh..."
    BED_MESH_CLEAR
    
    RESPOND MSG="Step 4: Starting adaptive mesh calibration..."
    BED_MESH_CALIBRATE ADAPTIVE=1
    
    RESPOND MSG=""
    RESPOND MSG="Bed Mesh calibration complete!"
    RESPOND MSG="Mesh saved automatically"
    RESPOND MSG="Run SAVE_CONFIG to make permanent"

# ----------------------------------------------------------------------------
# FULL CALIBRATION SUITE - Runs all calibrations in sequence
# ----------------------------------------------------------------------------
[gcode_macro CALIBRATION_FULL]
description: Run complete calibration suite (takes ~2.5 hours)
gcode:
    RESPOND MSG="=============================================="
    RESPOND MSG="   STARTING FULL CALIBRATION SUITE"
    RESPOND MSG="   Estimated time: ~2.5 hours"
    RESPOND MSG="=============================================="
    RESPOND MSG=""
    RESPOND MSG="This will run ALL calibrations in sequence."
    RESPOND MSG="You can stop at any time with emergency stop."
    RESPOND MSG=""
    RESPOND MSG="Starting in 5 seconds..."
    G4 P5000
    
    # 1. Z-Offset
    RESPOND MSG=">>> Starting Z-Offset Calibration..."
    CALIBRATE_Z_OFFSET
    G4 P2000
    
    # 2. Input Shaper
    RESPOND MSG=">>> Starting Input Shaper Calibration..."
    CALIBRATE_INPUT_SHAPER
    G4 P2000
    
    # 3. Pressure Advance
    RESPOND MSG=">>> Starting Pressure Advance Calibration..."
    CALIBRATE_PRESSURE_ADVANCE FILAMENT=PLA
    G4 P2000
    
    # 4. Flow Rate
    RESPOND MSG=">>> Starting Flow Rate Calibration..."
    CALIBRATE_FLOW_RATE FILAMENT=PLA
    G4 P2000
    
    # 5. Temperature Tower
    RESPOND MSG=">>> Starting Temperature Tower..."
    CALIBRATE_TEMPERATURE FILAMENT=PLA
    G4 P2000
    
    # 6. Retraction Test
    RESPOND MSG=">>> Starting Retraction Test..."
    CALIBRATE_RETRACTION FILAMENT=PLA
    G4 P2000
    
    # 7. Bed Mesh
    RESPOND MSG=">>> Starting Bed Mesh Calibration..."
    CALIBRATE_BED_MESH
    
    RESPOND MSG=""
    RESPOND MSG="=============================================="
    RESPOND MSG="   FULL CALIBRATION SUITE COMPLETE!"
    RESPOND MSG="=============================================="
    RESPOND MSG=""
    RESPOND MSG="IMPORTANT: Run SAVE_CONFIG now to save all settings!"
    RESPOND MSG=""

# ----------------------------------------------------------------------------
# QUICK CALIBRATION - Essential calibrations only (~30 min)
# ----------------------------------------------------------------------------
[gcode_macro CALIBRATION_QUICK]
description: Run quick calibration (Z-offset + PA + Bed Mesh, ~30 min)
gcode:
    RESPOND MSG="=============================================="
    RESPOND MSG="   STARTING QUICK CALIBRATION"
    RESPOND MSG="   Estimated time: ~30 minutes"
    RESPOND MSG="=============================================="
    RESPOND MSG=""
    
    # 1. Z-Offset
    RESPOND MSG=">>> Z-Offset Calibration..."
    CALIBRATE_Z_OFFSET
    G4 P2000
    
    # 2. Pressure Advance
    RESPOND MSG=">>> Pressure Advance Calibration..."
    CALIBRATE_PRESSURE_ADVANCE FILAMENT=PLA
    G4 P2000
    
    # 3. Bed Mesh
    RESPOND MSG=">>> Bed Mesh Calibration..."
    CALIBRATE_BED_MESH
    
    RESPOND MSG=""
    RESPOND MSG="=============================================="
    RESPOND MSG="   QUICK CALIBRATION COMPLETE!"
    RESPOND MSG="=============================================="
    RESPOND MSG=""
    RESPOND MSG="Run SAVE_CONFIG to save settings!"
