/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008-2012. All Rights Reserved.                        */
/* Open Source Software - may be modified and shared by FRC teams. The code   */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project.                                                               */
/*----------------------------------------------------------------------------*/

package edu.wpi.first.wpilibj;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;

import edu.wpi.first.wpilibj.hal.HALLibrary;
import edu.wpi.first.wpilibj.hal.HALUtil;
import edu.wpi.first.wpilibj.parsing.IUtility;

/**
 * Contains global utility functions
 */
public class Utility implements IUtility {

	private Utility() {
	}

	/**
	 * Return the FPGA Version number. For now, expect this to be 2009.
	 * 
	 * @return FPGA Version number.
	 */
	int getFPGAVersion() {
		ByteBuffer status = ByteBuffer.allocateDirect(4);
		// set the byte order
		status.order(ByteOrder.LITTLE_ENDIAN);
		int value = HALUtil.getFPGAVersion(status.asIntBuffer());
		HALUtil.checkStatus(status.asIntBuffer());
		return value;
	}

	/**
	 * Return the FPGA Revision number. The format of the revision is 3 numbers.
	 * The 12 most significant bits are the Major Revision. the next 8 bits are
	 * the Minor Revision. The 12 least significant bits are the Build Number.
	 * 
	 * @return FPGA Revision number.
	 */
	long getFPGARevision() {
		ByteBuffer status = ByteBuffer.allocateDirect(4);
		// set the byte order
		status.order(ByteOrder.LITTLE_ENDIAN);
		int value = HALUtil.getFPGARevision(status.asIntBuffer());
		HALUtil.checkStatus(status.asIntBuffer());
		return (long) value;
	}

	/**
	 * Read the microsecond timer from the FPGA.
	 * 
	 * @return The current time in microseconds according to the FPGA.
	 */
	public static long getFPGATime() {
		ByteBuffer status = ByteBuffer.allocateDirect(4);
		// set the byte order
		status.order(ByteOrder.LITTLE_ENDIAN);
		
		long value = HALUtil.getFPGATime(status.asIntBuffer());
		HALUtil.checkStatus(status.asIntBuffer());
		return value;
	}

	/**
	 * Control whether to send System.err output to the driver station's error
	 * pane.
	 * 
	 * @param enabled
	 *            if true, send error stream to driver station, otherwise
	 *            disable sending error stream
	 */
	public static void sendErrorStreamToDriverStation(boolean enabled) {
		final String url = "dserror:edu.wpi.first.wpilibj.Utility"; // the path
																	// is just a
																	// comment.
		/* XXX: Code Review!
		Isolate isolate = VM.getCurrentIsolate();
		if (enabled) {
			isolate.addErr(url);
		} else {
			isolate.removeErr(url);
		} */
	}

}
