001/* Copyright (C) Cross The Road Electronics 2024 */
002package com.ctre.phoenix.led;
003
004/**
005 * Sticky Faults the CANdle can have
006 */
007public class CANdleStickyFaults {
008    /**
009     * Device detects hardware failure
010     */
011    boolean HardwareFault;
012    /**
013     * API error detected.  Make sure API and firmware versions are compatible.
014     */
015    boolean APIError;
016    /**
017     * Boot while receiving an enable frame 
018     */
019    boolean BootDuringEnable;
020    /**
021     * VBat is under 5V
022     */
023    boolean VBatTooLow;
024    /**
025     * VBat is over 30V
026     */
027    boolean VBatTooHigh;
028    /**
029     * 5V Line is under 4 V
030     */
031    boolean V5TooLow;
032    /**
033     * 5V Line is over 6V
034     */
035    boolean V5TooHigh;
036    /**
037     * Exceeded output current of 6 amps
038     */
039    boolean SoftwareFuse;
040    /**
041     * Device is over temperature
042     */
043    boolean ThermalFault;
044    /**
045     * Output pin is shorted to something
046     */
047    boolean ShortCircuit;
048    /**
049     * @return true if any faults are tripped
050     */
051    boolean hasAnyFault() {
052        return  HardwareFault |
053                APIError |
054                BootDuringEnable |
055                VBatTooLow |
056                VBatTooHigh |
057                V5TooLow |
058                V5TooHigh |
059                SoftwareFuse |
060                ThermalFault |
061                ShortCircuit;
062    }
063    /**
064     * @return Current fault list as a bit field
065     */
066    long toBitfield() {
067        long commonFaults = 0;
068        commonFaults |= ShortCircuit ? 1 : 0; commonFaults <<= 1;
069        commonFaults |= ThermalFault ? 1 : 0; commonFaults <<= 1;
070        commonFaults |= SoftwareFuse ? 1 : 0; commonFaults <<= 1;
071        commonFaults |= V5TooLow ? 1 : 0; commonFaults <<= 1;
072        commonFaults |= V5TooHigh ? 1 : 0; commonFaults <<= 1;
073        commonFaults |= VBatTooLow ? 1 : 0; commonFaults <<= 1;
074        commonFaults |= VBatTooHigh ? 1 : 0; commonFaults <<= 1;
075        commonFaults |= BootDuringEnable ? 1 : 0; commonFaults <<= 1;
076        commonFaults |= APIError ? 1 : 0; commonFaults <<= 1;
077        commonFaults |= HardwareFault ? 1 : 0;
078
079        return commonFaults;
080    }
081    void update(long bits) {
082        long mask = 1;
083        HardwareFault  =    (bits & mask) != 0; mask <<= 1;
084        APIError =          (bits & mask) != 0; mask <<= 1;
085        BootDuringEnable =  (bits & mask) != 0; mask <<= 1;
086        VBatTooHigh =       (bits & mask) != 0; mask <<= 1;
087        VBatTooLow =        (bits & mask) != 0; mask <<= 1;
088        V5TooHigh =         (bits & mask) != 0; mask <<= 1;
089        V5TooLow =          (bits & mask) != 0; mask <<= 1;
090        SoftwareFuse =      (bits & mask) != 0; mask <<= 1;
091        ThermalFault =      (bits & mask) != 0; mask <<= 1;
092        ShortCircuit =      (bits & mask) != 0; mask <<= 1;
093    }
094    /**
095     * Updates current fault list with specified bit field of faults
096     * 
097     * @param bits bit field of faults to update with
098     */
099    CANdleStickyFaults(int bits) {
100        update(bits);
101    }
102    CANdleStickyFaults() {
103        update(0);
104    }
105    
106}