001/*----------------------------------------------------------------------------*/
002/* Copyright (c) FIRST 2008-2012. All Rights Reserved.                        */
003/* Open Source Software - may be modified and shared by FRC teams. The code   */
004/* must be accompanied by the FIRST BSD license file in the root directory of */
005/* the project.                                                               */
006/*----------------------------------------------------------------------------*/
007
008package edu.wpi.first.wpilibj;
009
010import java.nio.ByteBuffer;
011import java.nio.ByteOrder;
012import java.nio.IntBuffer;
013import java.lang.StackTraceElement;
014
015import edu.wpi.first.wpilibj.hal.HALLibrary;
016import edu.wpi.first.wpilibj.hal.HALUtil;
017import edu.wpi.first.wpilibj.communication.FRCNetworkCommunicationsLibrary;
018
019/**
020 * Contains global utility functions
021 */
022public class Utility {
023
024        private Utility() {
025        }
026
027        /**
028         * Return the FPGA Version number. For now, expect this to be 2009.
029         *
030         * @return FPGA Version number.
031         */
032        int getFPGAVersion() {
033                ByteBuffer status = ByteBuffer.allocateDirect(4);
034                // set the byte order
035                status.order(ByteOrder.LITTLE_ENDIAN);
036                int value = HALUtil.getFPGAVersion(status.asIntBuffer());
037                HALUtil.checkStatus(status.asIntBuffer());
038                return value;
039        }
040
041        /**
042         * Return the FPGA Revision number. The format of the revision is 3 numbers.
043         * The 12 most significant bits are the Major Revision. the next 8 bits are
044         * the Minor Revision. The 12 least significant bits are the Build Number.
045         *
046         * @return FPGA Revision number.
047         */
048        long getFPGARevision() {
049                ByteBuffer status = ByteBuffer.allocateDirect(4);
050                // set the byte order
051                status.order(ByteOrder.LITTLE_ENDIAN);
052                int value = HALUtil.getFPGARevision(status.asIntBuffer());
053                HALUtil.checkStatus(status.asIntBuffer());
054                return (long) value;
055        }
056
057        /**
058         * Read the microsecond timer from the FPGA.
059         *
060         * @return The current time in microseconds according to the FPGA.
061         */
062        public static long getFPGATime() {
063                ByteBuffer status = ByteBuffer.allocateDirect(4);
064                // set the byte order
065                status.order(ByteOrder.LITTLE_ENDIAN);
066
067                long value = HALUtil.getFPGATime(status.asIntBuffer());
068                HALUtil.checkStatus(status.asIntBuffer());
069                return value;
070        }
071
072        /**
073         * Get the state of the "USER" button on the RoboRIO
074         * @return true if the button is currently pressed down
075         */
076        public static boolean getUserButton() {
077                ByteBuffer status = ByteBuffer.allocateDirect(4);
078                // set the byte order
079                status.order(ByteOrder.LITTLE_ENDIAN);
080
081                boolean value = HALUtil.getFPGAButton(status.asIntBuffer());
082                HALUtil.checkStatus(status.asIntBuffer());
083                return value;
084        }
085}