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

M291 R"Voltage Test" P"Do you want to start the voltage test?" S4 K{"Test","Skip","Cancel"}
if input == 1
    M99
elif input == 2
    abort "Test cancelled by user"
    

var minVoltage = 24
var maxVoltage = 24.3
var requiredStableTime = 20  ; Seconds voltage must remain in range to pass
var consecutiveGoodReads = 0 ; Counter for consecutive good readings
var checkInterval = 100      ; Check every 100ms
var checksPerSecond = 1000 / var.checkInterval
var requiredGoodReads = var.requiredStableTime * var.checksPerSecond  ; Total checks needed for stable pass

echo "Voltage Test: Required range " ^ var.minVoltage ^ "V - " ^ var.maxVoltage ^ "V, stable for " ^ var.requiredStableTime ^ " seconds"

M98 P"0:/sys/led/dimmwhite.g"
G1 S2

var testComplete = false
var lastEchoTime = 0
var lastState = ""  ; Track LED state to avoid unnecessary changes

while !var.testComplete
    G4 P{var.checkInterval}
    
    var currentVoltage = boards[0].vIn.current
    
    if var.currentVoltage > var.maxVoltage
        ; RED - Voltage too high
        if var.lastState != "red"
            M42 P1 S0  ; Red off
            M42 P2 S0  ; Green off
            M42 P3 S0  ; Blue off
            M42 P1 S1  ; Red on
            set var.lastState = "red"
        set var.consecutiveGoodReads = 0  ; Reset counter
        ; Echo only every 10 seconds to avoid overflow
        if (state.upTime - var.lastEchoTime) >= 10
            echo "Voltage HIGH: " ^ var.currentVoltage ^ "V (adjust down)"
            set var.lastEchoTime = state.upTime
        
    elif var.currentVoltage < var.minVoltage
        ; BLUE - Voltage too low
        if var.lastState != "blue"
            M42 P1 S0  ; Red off
            M42 P2 S0  ; Green off
            M42 P3 S0  ; Blue off
            M42 P3 S1  ; Blue on
            set var.lastState = "blue"
        set var.consecutiveGoodReads = 0  ; Reset counter
        ; Echo only every 10 seconds to avoid overflow
        if (state.upTime - var.lastEchoTime) >= 10
            echo "Voltage LOW: " ^ var.currentVoltage ^ "V (adjust up)"
            set var.lastEchoTime = state.upTime
        
    else
        ; GREEN - Voltage in range (LED animation provides feedback, no echo needed)
        if var.lastState != "green"
            M42 P1 S0  ; Red off
            M42 P2 S0  ; Green off
            M42 P3 S0  ; Blue off
            M42 P2 S1  ; Green on
            set var.lastState = "green"
        set var.consecutiveGoodReads = var.consecutiveGoodReads + 1
        
        ; Check if we've been stable long enough
        if var.consecutiveGoodReads >= var.requiredGoodReads
            set var.testComplete = true
            echo "TEST PASSED: Voltage stable at " ^ var.currentVoltage ^ "V for " ^ var.requiredStableTime ^ " seconds"
    
    ; Allow manual stop if endstops triggered (emergency exit during RED/BLUE)
    if var.currentVoltage < var.minVoltage || var.currentVoltage > var.maxVoltage
        if sensors.endstops[0].triggered || sensors.endstops[3].triggered
            echo "Manual stop detected via endstop - Test aborted"
            M98 P"0:/sys/led/fault.g"
            G4 S1
            M98 P"0:/sys/led/resetstatus.g"
            abort "Voltage test manually stopped - voltage out of range"

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