001/*
002 * Copyright (C) Cross The Road Electronics.  All rights reserved.
003 * License information can be found in CTRE_LICENSE.txt
004 * For support and suggestions contact support@ctr-electronics.com or file
005 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
006 */
007package com.ctre.phoenixpro;
008
009import java.util.HashMap;
010
011/**
012 * Information about the timestamp of a signal.
013 *
014 * @deprecated Classes in the phoenixpro package will be removed in 2024.
015 *             Users should instead use classes from the phoenix6 package.
016 */
017@Deprecated(forRemoval = true)
018public class Timestamp {
019
020    /**
021     * Source of the timestamp.
022     *
023     * @deprecated Classes in the phoenixpro package will be removed in 2024.
024     *             Users should instead use classes from the phoenix6 package.
025     */
026    @Deprecated(forRemoval = true)
027    public enum TimestampSource {
028        /**
029         * Timestamp as reported by the system.
030         * This timestamp is captured when the system receives the signal value.
031         * <p>
032         * This timestamp is present on all systems and is guaranteed to be monotonic.
033         * However, this timestamp is the least accurate due to processing delays within the system.
034         */
035        System(0),
036        /**
037         * Timestamp as reported by the CANivore.
038         * This timestamp is captured when the CANivore receives the signal value.
039         * <p>
040         * The CANivore is synchronized to the system monotonic clock and benefits
041         * from reduced latency over the {@link TimestampSource#System System} timestamp.
042         * <p>
043         * On the native roboRIO CAN bus, this timestamp is equivalent to the {@link TimestampSource#System System} timestamp.
044         * <p>
045         * When used with CANivore, the only inaccuracy in this measurement is latency
046         * from CAN bus arbitration.
047         */
048        CANivore(1),
049        /**
050         * This timestamp source is not currently implemented in this version of Phoenix Pro.
051         * <p>
052         * Timestamp as reported by the device.
053         * This timestamp is captured when the device transmits the signal value.
054         * Because it is timestamped in the device, it is the most accurate timestamp source.
055         * <p>
056         * This timestamp is synchronized to the CANivore clock, which is itself synchronized
057         * to the system monotonic clock. As a result, this timestamp source requires a CANivore.
058         * <p>
059         * It can be assumed there is no latency between this timestamp and when the data was taken.
060         */
061        Device(2);
062
063        public final int value;
064
065        TimestampSource(int initValue) {
066            this.value = initValue;
067        }
068
069        private static HashMap<Integer, TimestampSource> _map = null;
070        static {
071            _map = new HashMap<Integer, TimestampSource>();
072            for (TimestampSource type : TimestampSource.values()) {
073                _map.put(type.value, type);
074            }
075        }
076
077        /**
078         * Gets TimestampSource from specified value
079         *
080         * @param value Value of TimestampSouce
081         * @return TimestampSource of specified value
082         */
083        public static TimestampSource valueOf(int value) {
084            TimestampSource retval = _map.get(value);
085            if (retval != null)
086                return retval;
087            return TimestampSource.values()[0];
088        }
089    }
090
091    private double time;
092    private TimestampSource source;
093    private boolean valid;
094
095    /**
096     * Construct a new Timestamp for the given source.
097     *
098     * @param time The time in seconds
099     * @param source The timestamp source
100     */
101    Timestamp(double time, TimestampSource source) {
102        update(time, source, true);
103    }
104    /**
105     * Construct a new invalid Timestamp.
106     */
107    Timestamp() {
108        this.valid = false;
109    }
110
111    void update(double time, TimestampSource source, boolean valid) {
112        this.time = time;
113        this.source = source;
114        this.valid = valid;
115    }
116
117    /**
118     * Get the time in seconds as reported from this timestamp
119     *
120     * @return Time in seconds
121     */
122    public double getTime() {
123        return time;
124    }
125
126    /**
127     * Get the source of this timestamp
128     *
129     * @return Source of this timestamp
130     */
131    public TimestampSource getSource() {
132        return source;
133    }
134
135    /**
136     * Get the latency of this timestamp compared to now
137     *
138     * @return Difference between now and this timestamp
139     */
140    public double getLatency() {
141        return Utils.getCurrentTimeSeconds() - time;
142    }
143
144    /**
145     * Returns if this Timestamp is valid or not.
146     *
147     * @return true when this is valid
148     */
149    public boolean isValid() {
150        return valid;
151    }
152}