M98 P"0:/sys/led/resetstatus.g"
M98 P"0:/macros/System/Calibration/QC/Disable Probe"


M291 R"Servo Motor Test" P"Servo motor will rotate to 0 degree and then to a set degree.<br><br>Press X or U endstop to stop the test early." S4 K{"Test","Skip","Cancel"}
if input == 0
    while iterations < 30
        if sensors.endstops[0].triggered || sensors.endstops[3].triggered
            break
        M98 P"0:/macros/System/Calibration/Z Probe/Rotate holder to a set degree"
        G4 S2
        M98 P"0:/macros/System/Calibration/Z Probe/Rotate holder to 0 degree"
        G4 S1

    M291 R"Servo Motor Test" P"Was the servo motor able to rotate smoothly?" S4 K{"Servo Rotated Smoothly","Servo Failed"}
    if input == 1
        abort "Error: Servo Failed"
elif input == 1
    ; Skipping
elif input == 2
    abort "Test cancelled by user"

M564 H0
M291 R"Position Build Plate" P"Position the build plate so you can remove and install the Z probe on the tool head, about 5 cm (~2 inches) from the nozzle." Z1 S3
M564 H1

if sensors.probes[0].value[0] != 1000
    M98 P"0:/sys/led/fault.g"
    abort "Error: S0 Dissconnect Z Probe or move the build plate away from the nozzle"
else
    M98 P"0:/sys/led/start_cold.g"



; Enable probe
M98 P"0:/macros/System/Calibration/QC/Enable Probe"
G4 S1

var timeout = 120
var press_count = 10
var start_time = 0
var max_iter = 1200 ; 120 seconds
var iter = 0



; 1 - T0 -> Build Plate ============================================================================
; check if z-probe value is 1000
if sensors.probes[0].value[0] != 1000
    M98 P"0:/sys/led/fault.g"
    abort "Error: S1 Z-Probe value is not 1000 before test"

M291 R"Testing Right Nozzle to Bed" P"Touch Right Nozzle to the Bed 10x and wait for light signal" S1 T0
set var.press_count = 10
set var.start_time = state.upTime

while var.press_count > 0
    M106 P6 S0 B0
    M42 P2 S0
    M42 P1 S0
    M42 P3 S1
    if sensors.probes[0].value[0] == 0
        set var.press_count = var.press_count - 1
        M42 P3 S0
        M42 P2 S1
        G4 S0.03
        while sensors.probes[0].value[0] == 0
            G4 S0.01
    if (state.upTime - var.start_time) > var.timeout
        M98 P"0:/sys/led/fault.g"
        M291 R"Error" P"Time for touching Left Nozzle to Bed has expired" S1 T0
        abort "Error: Time for touching Left Nozzle to Bed has expired"

M98 P"0:/sys/led/end.g"
M98 P"0:/sys/led/resetstatus.g"
M292



M98 P"0:/sys/led/start_cold.g"
G4 S1



; 2 - T1 -> Build Plate ============================================================================
; check if z-probe value is 1000
if sensors.probes[0].value[0] != 1000
    M98 P"0:/sys/led/fault.g"
    abort "Error: S2 Z-Probe value is not 1000 before test"

M291 R"Testing Left Nozzle to Bed" P"Touch Left Nozzle to the Bed 10x and wait for light signal" S1 T0
set var.press_count = 10
set var.start_time = state.upTime

while var.press_count > 0
    M106 P6 S0 B0
    M42 P2 S0
    M42 P1 S0
    M42 P3 S1
    if sensors.probes[0].value[0] == 0
        set var.press_count = var.press_count - 1
        M42 P3 S0
        M42 P2 S1
        G4 S0.03
        while sensors.probes[0].value[0] == 0
            G4 S0.01
    if (state.upTime - var.start_time) > var.timeout
        M98 P"0:/sys/led/fault.g"
        M291 R"Error" P"Time for touching Right Nozzle to Bed has expired" S1 T0
        abort "Error: Time for touching Right Nozzle to Bed has expired"

M98 P"0:/sys/led/end.g"
M98 P"0:/sys/led/resetstatus.g"
M292



M98 P"0:/sys/led/start_cold.g"
G4 S1



; 3 - Attach the Z-Probe to the holder ============================================================================
; check if z-probe value is 1000
if sensors.probes[0].value[0] == 1000
    M291 R"Attach the Z-Probe to the holder" P"Attach the Z-Probe to the holder, Z - Probe value should change to 0" S1 T0
else
    M98 P"0:/sys/led/fault.g"
    abort "Error: S3 Z-Probe value is not 1000"

; wait for z-probe value to change to 0
while sensors.probes[0].value[0] != 0
    G4 P100
    set var.iter = var.iter + 1

if var.iter <= var.max_iter
    M292
    M98 P"0:/sys/led/end.g"
else
    M98 P"0:/sys/led/fault.g"
    abort "Error: S3 Probe was not recognized"


M292
M98 P"0:/sys/led/start_cold.g"
G4 S1



; 4 - Click the Z-Probe ============================================================================
; check if z-probe value is 0
if sensors.probes[0].value[0] != 0
    M98 P"0:/sys/led/fault.g"
    abort "Error: S4 Probe was not attached"

M291 R"Testing Z-Probe Click" P"Click the Z-Probe 10x and wait for light signal" S1 T0
set var.press_count = 10
set var.start_time = state.upTime

while var.press_count > 0
    M106 P6 S0 B0
    M42 P2 S0
    M42 P1 S0
    M42 P3 S1
    if sensors.probes[0].value[0] == 1000
        set var.press_count = var.press_count - 1
        M42 P3 S0
        M42 P2 S1
        G4 S0.03
        while sensors.probes[0].value[0] == 1000
            G4 S0.01
    if (state.upTime - var.start_time) > var.timeout
        M98 P"0:/sys/led/fault.g"
        M291 R"Error" P"Time for clicking Z-Probe has expired" S1 T0
        abort "Error: Time for clicking Z-Probe has expired"

M98 P"0:/sys/led/end.g"
M98 P"0:/sys/led/resetstatus.g"


M98 P"0:/macros/System/Calibration/QC/Disable Probe" ; Disable probe
M98 P"0:/sys/led/resetstatus.g"
M292