001/* Copyright (C) Cross The Road Electronics 2024 */
002package com.ctre.phoenix.motorcontrol;
003
004import java.util.HashMap;
005
006/**
007 * Choose the feedback device for a selected sensor
008 */
009public enum TalonSRXFeedbackDevice {    
010        /**
011         * Quadrature encoder
012         */
013        QuadEncoder(0),
014        /**
015         * Analog potentiometer/encoder
016         */
017        Analog(2),
018        /**
019         * Tachometer
020         */
021        Tachometer(4),
022        /**
023         * CTRE Mag Encoder in Relative mode or
024         * any other device that uses PWM to encode its output
025         */
026        PulseWidthEncodedPosition(8),
027
028        /**
029         * Sum0 + Sum1
030         */
031        SensorSum(9),
032        /**
033         * Diff0 - Diff1
034         */
035        SensorDifference(10),
036        /**
037         * Sensor configured in RemoteFilter0
038         */
039        RemoteSensor0(11),
040        /**
041         * Sensor configured in RemoteFilter1
042         */
043        RemoteSensor1(12),
044        /**
045         * Position and velocity will read 0.
046         */
047        None(14),
048        /**
049         * Motor Controller will fake a sensor based on applied motor output.
050         */
051        SoftwareEmulatedSensor(15),
052
053        /**
054         * CTR mag encoder configured in absolute, is the same 
055         * as a PWM sensor.
056         */
057        CTRE_MagEncoder_Absolute(8),
058        /**
059         * CTR mag encoder configured in relative, is the same 
060         * as an quadrature encoder sensor.
061         */
062        CTRE_MagEncoder_Relative(0);
063        
064        /** Value of Feedback Device */
065        public final int value;
066
067        /**
068         * Create Feedback device of initValue
069         * @param initValue Value of TalonSRXFeedbackDevice
070         */
071        TalonSRXFeedbackDevice(int initValue)
072        {
073                this.value = initValue;
074        }
075    /** Keep singleton map to quickly lookup enum via int */
076    private static HashMap<Integer, TalonSRXFeedbackDevice> _map = null;
077        /** static c'tor, prepare the map */
078    static {
079        _map = new HashMap<Integer, TalonSRXFeedbackDevice>();
080                for (TalonSRXFeedbackDevice type : TalonSRXFeedbackDevice.values()) {
081                        _map.put(type.value, type);
082                }
083    }
084        /**
085         * Get TalonSRXFeedbackDevice from specified value
086         * @param value Value of TalonSRXFeedbackDevice
087         * @return TalonSRXFeedbackDevice of specified value
088         */
089        public static TalonSRXFeedbackDevice valueOf(int value) {
090                TalonSRXFeedbackDevice retval = _map.get(value);
091                if (retval != null)
092                        return retval;
093                return QuadEncoder;
094        }
095        /**
096         * Get TalonSRXFeedbackDevice from specified value
097         * @param value Value of TalonSRXFeedbackDevice
098         * @return TalonSRXFeedbackDevice of specified value
099         */
100    public static TalonSRXFeedbackDevice valueOf(double value) {
101        return valueOf((int) value); 
102        }
103        
104        /**
105         * @return string representation of specified TalonSRXFeedbackDevice
106         */
107    public String toString() {
108        switch(value) {
109            case 0 : return "TalonSRXFeedbackDevice.QuadEncoder";
110            case 2 : return "TalonSRXFeedbackDevice.Analog";
111            case 4 : return "TalonSRXFeedbackDevice.Tachometer";
112            case 8 : return "TalonSRXFeedbackDevice.PulseWidthEncodedPosition";
113            case 9 : return "TalonSRXFeedbackDevice.SensorSum";
114            case 10: return "TalonSRXFeedbackDevice.SensorDifference";
115            case 11: return "TalonSRXFeedbackDevice.RemoteSensor0";
116            case 12: return "TalonSRXFeedbackDevice.RemoteSensor1";
117                        case 14: return "TalonSRXFeedbackDevice.None";
118            case 15: return "TalonSRXFeedbackDevice.SoftwareEmulatedSensor";
119            default: return "InvalidValue";
120
121        }
122
123    }
124        
125        /**
126         * Helper method to convert to generic FeedbackDevice enum.
127         * @return value cast as FeedbackDevice
128         */
129        public FeedbackDevice toFeedbackDevice(){
130                switch(value) {
131                        case 0 : return FeedbackDevice.QuadEncoder;
132                        case 2 : return FeedbackDevice.Analog;
133                        case 4 : return FeedbackDevice.Tachometer;
134                        case 8 : return FeedbackDevice.PulseWidthEncodedPosition;
135                        case 9 : return FeedbackDevice.SensorSum;
136                        case 10: return FeedbackDevice.SensorDifference;
137                        case 11: return FeedbackDevice.RemoteSensor0;
138                        case 12: return FeedbackDevice.RemoteSensor1;
139                        case 14: return FeedbackDevice.None;
140                        case 15: return FeedbackDevice.SoftwareEmulatedSensor;
141                        default: return FeedbackDevice.QuadEncoder;
142                }
143        }
144
145};