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.phoenix6;
008
009import com.ctre.phoenix6.jni.UtilsJNI;
010
011import edu.wpi.first.wpilibj.Timer;
012
013public class Utils {
014    /**
015     * Get the current timestamp in seconds.
016     * <p>
017     * This is the time source used for status signals.
018     * <p>
019     * This time source is typically continuous and monotonic.
020     * However, it may be overridden in simulation to use a
021     * non-monotonic, non-continuous source.
022     *
023     * @return Current time in seconds
024     */
025    public static double getCurrentTimeSeconds() {
026        return UtilsJNI.getCurrentTimeSeconds();
027    }
028    /**
029     * Get the system timestamp in seconds.
030     * <p>
031     * This is NOT the time source used for status signals.
032     * Use GetCurrentTImeSeconds instead when working with
033     * status signal timing.
034     * <p>
035     * This time source is guaranteed to be continuous and
036     * monotonic, making it useful for measuring time deltas
037     * in a robot program.
038     *
039     * @return System time in seconds
040     */
041    public static double getSystemTimeSeconds() {
042        return UtilsJNI.getSystemTimeSeconds();
043    }
044    /**
045     * Get whether the program is running in simulation.
046     *
047     * @return {@code true} if in simulation
048     */
049    public static boolean isSimulation() {
050        return UtilsJNI.isSimulation();
051    }
052    /**
053     * Get whether the program is running in replay mode.
054     *
055     * @return {@code true} if in replay mode
056     */
057    public static boolean isReplay() {
058        return UtilsJNI.isReplay();
059    }
060
061    /**
062     * Converts an FPGA timestamp to the timebase
063     * reported by {@link #getCurrentTimeSeconds()}.
064     *
065     * @param fpgaTimeSeconds The FPGA timestamp in seconds
066     * @return The equivalent {@link #getCurrentTimeSeconds()} timestamp in seconds
067     */
068    public static double fpgaToCurrentTime(double fpgaTimeSeconds) {
069        return (getCurrentTimeSeconds() - Timer.getFPGATimestamp()) + fpgaTimeSeconds;
070    }
071}