001/* Copyright (C) Cross The Road Electronics 2024 */
002package com.ctre.phoenix;
003
004import java.util.HashMap;
005
006/**
007 * All the codes available to CTRE products
008 */
009public enum ErrorCode {
010        OK(0),                                          //!< No Error - Function executed as expected
011
012        //CAN-Related
013        CAN_MSG_STALE(1),
014        CAN_TX_FULL(-1),
015        TxFailed(-1),                           //!< Could not transmit the CAN frame.
016        InvalidParamValue(-2),  //!< Caller passed an invalid param
017        CAN_INVALID_PARAM(-2),
018        RxTimeout(-3),                          //!< CAN frame has not been received within specified period of time.
019        CAN_MSG_NOT_FOUND(-3),
020        TxTimeout(-4),                          //!< Not used.
021        CAN_NO_MORE_TX_JOBS(-4),
022        UnexpectedArbId(-5),            //!< Specified Device ID is invalid.
023        CAN_NO_SESSIONS_AVAIL(-5),
024        BufferFull(+6),                 //!< Caller attempted to insert data into a buffer that is full.
025        CAN_OVERFLOW(-6),
026        SensorNotPresent(-7),           //!< Sensor is not present
027        FirmwareTooOld (-8),
028
029
030        //General
031        GeneralError(-100),             //!< User Specified General Error
032        GENERAL_ERROR(-100),
033
034        //Signal
035        SIG_NOT_UPDATED(-200),
036        SigNotUpdated(-200),                    //!< Have not received an value response for signal.
037        NotAllPIDValuesUpdated(-201),
038
039        //Gadgeteer Port Error Codes
040        //These include errors between ports and modules
041        GEN_PORT_ERROR(-300),
042        PORT_MODULE_TYPE_MISMATCH(-301),
043        //Gadgeteer Module Error Codes
044        //These apply only to the module units themselves
045        GEN_MODULE_ERROR(-400),
046        MODULE_NOT_INIT_SET_ERROR(-401),
047        MODULE_NOT_INIT_GET_ERROR(-402),
048
049
050        //API
051        WheelRadiusTooSmall(-500),
052        TicksPerRevZero(-501),
053        DistanceBetweenWheelsTooSmall(-502),
054        GainsAreNotSet(-503),
055        DoubleVoltageCompensatingWPI(-505),
056
057        //Higher Level
058        IncompatibleMode(-600),
059        InvalidHandle(-601),            //!< Handle does not match stored map of handles
060
061
062        //CAN Related
063        PulseWidthSensorNotPresent (10),        //!< Special Code for "isSensorPresent"
064
065        //General
066        GeneralWarning(100),
067        FeatureNotSupported(101),
068        NotImplemented(102),
069        FirmVersionCouldNotBeRetrieved (103),
070        FeaturesNotAvailableYet(104),
071        ControlModeNotValid(105),
072
073        ControlModeNotSupportedYet(106),
074        CascadedPIDNotSupportedYet(107),
075        AuxiliaryPIDNotSupportedYet(107),
076        RemoteSensorsNotSupportedYet(108),
077        MotProfFirmThreshold(109),
078        MotProfFirmThreshold2(110),
079        
080        //Simulation
081        SimDeviceNotFound(200),
082        SimPhysicsTypeNotSupported(201),
083        SimDeviceAlreadyExists(202);
084
085        //---------------------- Integral To Enum operators -----------//
086    public final int value; //!< Hold the integral value of an enum instance.
087    /** private c'tor for above declarations */
088        private ErrorCode(int initValue) {this.value = initValue;       }
089    /** Keep singleton map to quickly lookup enum via int */
090    private static HashMap<Integer, ErrorCode> _map = null;
091        /** static c'tor, prepare the map */
092    static {
093        _map = new HashMap<Integer, ErrorCode>();
094                for (ErrorCode type : ErrorCode.values()) {
095                        _map.put(type.value, type);
096                }
097    }
098    /** public lookup to convert int to enum */
099        public static ErrorCode valueOf(int value) {
100                ErrorCode retval = _map.get(value);
101                if (retval != null)
102                        return retval;
103                return GeneralError;
104        }
105
106        /** @return the first nonzero error code */
107        public static ErrorCode worstOne(ErrorCode errorCode1, ErrorCode errorCode2) {
108                if (errorCode1.value != 0)
109                        return errorCode1;
110                return errorCode2;
111        }
112
113        /** @return the first nonzero error code */
114        public static ErrorCode worstOne(ErrorCode errorCode1, ErrorCode errorCode2, ErrorCode errorCode3) {
115                if (errorCode1.value != 0)
116                        return errorCode1;
117                if (errorCode2.value != 0)
118                        return errorCode2;
119                return errorCode3;
120        }
121
122        /** @return the first nonzero error code */
123        public static ErrorCode worstOne(ErrorCode errorCode1, ErrorCode errorCode2, ErrorCode errorCode3,
124                        ErrorCode errorCode4) {
125                if (errorCode1.value != 0)
126                        return errorCode1;
127                if (errorCode2.value != 0)
128                        return errorCode2;
129                if (errorCode3.value != 0)
130                        return errorCode3;
131                return errorCode4;
132        }
133};