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 edu.wpi.first.wpilibj.hal.DIOJNI;
011import edu.wpi.first.wpilibj.hal.AnalogJNI;
012
013/**
014 * Base class for all sensors.
015 * Stores most recent status information as well as containing utility functions for checking
016 * channels and error processing.
017 */
018public abstract class SensorBase { // TODO: Refactor
019
020        // TODO: Move this to the HAL
021
022    /**
023     * Ticks per microsecond
024     */
025    public static final int kSystemClockTicksPerMicrosecond = 40;
026    /**
027     * Number of digital channels per roboRIO
028     */
029    public static final int kDigitalChannels = 26;
030    /**
031     * Number of analog input channels
032     */
033    public static final int kAnalogInputChannels = 8;
034    /**
035     * Number of analog output channels
036     */
037    public static final int kAnalogOutputChannels = 2;
038    /**
039     * Number of solenoid channels per module
040     */
041    public static final int kSolenoidChannels = 8;
042    /**
043     * Number of solenoid modules
044     */
045    public static final int kSolenoidModules = 2;
046    /**
047     * Number of PWM channels per roboRIO
048     */
049    public static final int kPwmChannels = 20;
050    /**
051     * Number of relay channels per roboRIO
052     */
053    public static final int kRelayChannels = 4;
054    /**
055     * Number of power distribution channels
056     */
057    public static final int kPDPChannels = 16;
058
059    private static int m_defaultSolenoidModule = 0;
060
061    /**
062     * Creates an instance of the sensor base and gets an FPGA handle
063     */
064    public SensorBase() {
065    }
066
067    /**
068     * Set the default location for the Solenoid module.
069     *
070     * @param moduleNumber The number of the solenoid module to use.
071     */
072    public static void setDefaultSolenoidModule(final int moduleNumber) {
073        checkSolenoidModule(moduleNumber);
074        SensorBase.m_defaultSolenoidModule = moduleNumber;
075    }
076
077    /**
078     * Verify that the solenoid module is correct.
079     *
080     * @param moduleNumber The solenoid module module number to check.
081     */
082    protected static void checkSolenoidModule(final int moduleNumber) {
083//        if(HALLibrary.checkSolenoidModule((byte) (moduleNumber - 1)) != 0) {
084//            System.err.println("Solenoid module " + moduleNumber + " is not present.");
085//        }
086    }
087
088    /**
089     * Check that the digital channel number is valid.
090     * Verify that the channel number is one of the legal channel numbers. Channel numbers are
091     * 1-based.
092     *
093     * @param channel The channel number to check.
094     */
095    protected static void checkDigitalChannel(final int channel) {
096        if (channel < 0 || channel >= kDigitalChannels) {
097            throw new IndexOutOfBoundsException("Requested digital channel number is out of range.");
098        }
099    }
100
101    /**
102     * Check that the digital channel number is valid.
103     * Verify that the channel number is one of the legal channel numbers. Channel numbers are
104     * 1-based.
105     *
106     * @param channel The channel number to check.
107     */
108    protected static void checkRelayChannel(final int channel) {
109        if (channel < 0 || channel >= kRelayChannels) {
110            throw new IndexOutOfBoundsException("Requested relay channel number is out of range.");
111        }
112    }
113
114    /**
115     * Check that the digital channel number is valid.
116     * Verify that the channel number is one of the legal channel numbers. Channel numbers are
117     * 1-based.
118     *
119     * @param channel The channel number to check.
120     */
121    protected static void checkPWMChannel(final int channel) {
122        if (channel < 0 || channel >= kPwmChannels) {
123            throw new IndexOutOfBoundsException("Requested PWM channel number is out of range.");
124        }
125    }
126
127    /**
128     * Check that the analog input number is value.
129     * Verify that the analog input number is one of the legal channel numbers. Channel numbers
130     * are 0-based.
131     *
132     * @param channel The channel number to check.
133     */
134    protected static void checkAnalogInputChannel(final int channel) {
135        if (channel < 0 || channel >= kAnalogInputChannels) {
136            throw new IndexOutOfBoundsException("Requested analog input channel number is out of range.");
137        }
138    }
139
140    /**
141     * Check that the analog input number is value.
142     * Verify that the analog input number is one of the legal channel numbers. Channel numbers
143     * are 0-based.
144     *
145     * @param channel The channel number to check.
146     */
147    protected static void checkAnalogOutputChannel(final int channel) {
148        if (channel < 0 || channel >= kAnalogOutputChannels) {
149            throw new IndexOutOfBoundsException("Requested analog output channel number is out of range.");
150        }
151    }
152
153    /**
154     * Verify that the solenoid channel number is within limits.  Channel numbers
155     * are 1-based.
156     *
157     * @param channel The channel number to check.
158     */
159    protected static void checkSolenoidChannel(final int channel) {
160        if (channel < 0 || channel >= kSolenoidChannels) {
161            throw new IndexOutOfBoundsException("Requested solenoid channel number is out of range.");
162        }
163    }
164
165    /**
166     * Verify that the power distribution channel number is within limits.
167     * Channel numbers are 1-based.
168     *
169     * @param channel The channel number to check.
170     */
171    protected static void checkPDPChannel(final int channel) {
172        if (channel < 0 || channel >= kPDPChannels) {
173            throw new IndexOutOfBoundsException("Requested PDP channel number is out of range.");
174        }
175    }
176
177    /**
178     * Get the number of the default solenoid module.
179     *
180     * @return The number of the default solenoid module.
181     */
182    public static int getDefaultSolenoidModule() {
183        return SensorBase.m_defaultSolenoidModule;
184    }
185
186    /**
187     * Free the resources used by this object
188     */
189    public void free() {}
190}