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      StringBuilder buf = new StringBuilder();
063      buf.append("Requested DIO channel is out of range. Minimum: 0, Maximum: ")
064          .append(kDigitalChannels)
065          .append(", Requested: ")
066          .append(channel);
067      throw new IllegalArgumentException(buf.toString());
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      StringBuilder buf = new StringBuilder();
080      buf.append("Requested relay channel is out of range. Minimum: 0, Maximum: ")
081          .append(kRelayChannels)
082          .append(", Requested: ")
083          .append(channel);
084      throw new IllegalArgumentException(buf.toString());
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      StringBuilder buf = new StringBuilder();
097      buf.append("Requested PWM channel is out of range. Minimum: 0, Maximum: ")
098          .append(kPwmChannels)
099          .append(", Requested: ")
100          .append(channel);
101      throw new IllegalArgumentException(buf.toString());
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      StringBuilder buf = new StringBuilder();
114      buf.append("Requested analog input channel is out of range. Minimum: 0, Maximum: ")
115          .append(kAnalogInputChannels)
116          .append(", Requested: ")
117          .append(channel);
118      throw new IllegalArgumentException(buf.toString());
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      StringBuilder buf = new StringBuilder();
131      buf.append("Requested analog output channel is out of range. Minimum: 0, Maximum: ")
132          .append(kAnalogOutputChannels)
133          .append(", Requested: ")
134          .append(channel);
135      throw new IllegalArgumentException(buf.toString());
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  @SuppressWarnings("AbbreviationAsWordInName")
145  public static int getDefaultCTREPCMModule() {
146    return 0;
147  }
148
149  /**
150   * Get the number of the default solenoid module.
151   *
152   * @return The number of the default solenoid module.
153   */
154  @SuppressWarnings("AbbreviationAsWordInName")
155  public static int getDefaultREVPHModule() {
156    return 1;
157  }
158
159  private SensorUtil() {}
160}