001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.wpilibj;
006
007import edu.wpi.first.hal.AnalogJNI;
008import edu.wpi.first.hal.ConstantsJNI;
009import edu.wpi.first.hal.DIOJNI;
010import edu.wpi.first.hal.PWMJNI;
011import edu.wpi.first.hal.PortsJNI;
012import edu.wpi.first.hal.RelayJNI;
013
014/**
015 * Stores most recent status information as well as containing utility functions for checking
016 * channels and error processing.
017 */
018public final class SensorUtil {
019  /** Ticks per microsecond. */
020  public static final int kSystemClockTicksPerMicrosecond =
021      ConstantsJNI.getSystemClockTicksPerMicrosecond();
022
023  /** Number of digital channels per roboRIO. */
024  public static final int kDigitalChannels = PortsJNI.getNumDigitalChannels();
025
026  /** Number of analog input channels per roboRIO. */
027  public static final int kAnalogInputChannels = PortsJNI.getNumAnalogInputs();
028
029  /** Number of analog output channels per roboRIO. */
030  public static final int kAnalogOutputChannels = PortsJNI.getNumAnalogOutputs();
031
032  /** Number of solenoid channels per module. */
033  public static final int kCTRESolenoidChannels = PortsJNI.getNumCTRESolenoidChannels();
034
035  /** Number of PWM channels per roboRIO. */
036  public static final int kPwmChannels = PortsJNI.getNumPWMChannels();
037
038  /** Number of relay channels per roboRIO. */
039  public static final int kRelayChannels = PortsJNI.getNumRelayHeaders();
040
041  /** Number of power distribution channels per PDP. */
042  public static final int kCTREPDPChannels = PortsJNI.getNumCTREPDPChannels();
043
044  /** Number of power distribution modules per PDP. */
045  public static final int kCTREPDPModules = PortsJNI.getNumCTREPDPModules();
046
047  /** Number of PCM Modules. */
048  public static final int kCTREPCMModules = PortsJNI.getNumCTREPCMModules();
049
050  public static final int kREVPHChannels = PortsJNI.getNumREVPHChannels();
051
052  public static final int kREVPHModules = PortsJNI.getNumREVPHModules();
053
054  /**
055   * Check that the digital channel number is valid. Verify that the channel number is one of the
056   * legal channel numbers. Channel numbers are 0-based.
057   *
058   * @param channel The channel number to check.
059   */
060  public static void checkDigitalChannel(final int channel) {
061    if (!DIOJNI.checkDIOChannel(channel)) {
062      String buf =
063          "Requested DIO channel is out of range. Minimum: 0, Maximum: "
064              + kDigitalChannels
065              + ", Requested: "
066              + channel;
067      throw new IllegalArgumentException(buf);
068    }
069  }
070
071  /**
072   * Check that the digital channel number is valid. Verify that the channel number is one of the
073   * legal channel numbers. Channel numbers are 0-based.
074   *
075   * @param channel The channel number to check.
076   */
077  public static void checkRelayChannel(final int channel) {
078    if (!RelayJNI.checkRelayChannel(channel)) {
079      String buf =
080          "Requested relay channel is out of range. Minimum: 0, Maximum: "
081              + kRelayChannels
082              + ", Requested: "
083              + channel;
084      throw new IllegalArgumentException(buf);
085    }
086  }
087
088  /**
089   * Check that the digital channel number is valid. Verify that the channel number is one of the
090   * legal channel numbers. Channel numbers are 0-based.
091   *
092   * @param channel The channel number to check.
093   */
094  public static void checkPWMChannel(final int channel) {
095    if (!PWMJNI.checkPWMChannel(channel)) {
096      String buf =
097          "Requested PWM channel is out of range. Minimum: 0, Maximum: "
098              + kPwmChannels
099              + ", Requested: "
100              + channel;
101      throw new IllegalArgumentException(buf);
102    }
103  }
104
105  /**
106   * Check that the analog input number is value. Verify that the analog input number is one of the
107   * legal channel numbers. Channel numbers are 0-based.
108   *
109   * @param channel The channel number to check.
110   */
111  public static void checkAnalogInputChannel(final int channel) {
112    if (!AnalogJNI.checkAnalogInputChannel(channel)) {
113      String buf =
114          "Requested analog input channel is out of range. Minimum: 0, Maximum: "
115              + kAnalogInputChannels
116              + ", Requested: "
117              + channel;
118      throw new IllegalArgumentException(buf);
119    }
120  }
121
122  /**
123   * Check that the analog input number is value. Verify that the analog input number is one of the
124   * legal channel numbers. Channel numbers are 0-based.
125   *
126   * @param channel The channel number to check.
127   */
128  public static void checkAnalogOutputChannel(final int channel) {
129    if (!AnalogJNI.checkAnalogOutputChannel(channel)) {
130      String buf =
131          "Requested analog output channel is out of range. Minimum: 0, Maximum: "
132              + kAnalogOutputChannels
133              + ", Requested: "
134              + channel;
135      throw new IllegalArgumentException(buf);
136    }
137  }
138
139  /**
140   * Get the number of the default solenoid module.
141   *
142   * @return The number of the default solenoid module.
143   */
144  public static int getDefaultCTREPCMModule() {
145    return 0;
146  }
147
148  /**
149   * Get the number of the default solenoid module.
150   *
151   * @return The number of the default solenoid module.
152   */
153  public static int getDefaultREVPHModule() {
154    return 1;
155  }
156
157  private SensorUtil() {}
158}