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;
012
013/**
014 * Stores most recent status information as well as containing utility functions for checking
015 * channels and error processing.
016 */
017public final class SensorUtil {
018  /** Ticks per microsecond. */
019  public static final int kSystemClockTicksPerMicrosecond =
020      ConstantsJNI.getSystemClockTicksPerMicrosecond();
021
022  /** Number of digital channels per Systemcore. */
023  public static final int kDigitalChannels = PortsJNI.getNumDigitalChannels();
024
025  /** Number of analog input channels per Systemcore. */
026  public static final int kAnalogInputChannels = PortsJNI.getNumAnalogInputs();
027
028  /** Number of solenoid channels per module. */
029  public static final int kCTRESolenoidChannels = PortsJNI.getNumCTRESolenoidChannels();
030
031  /** Number of PWM channels per Systemcore. */
032  public static final int kPwmChannels = PortsJNI.getNumPWMChannels();
033
034  /** Number of power distribution channels per PDP. */
035  public static final int kCTREPDPChannels = PortsJNI.getNumCTREPDPChannels();
036
037  /** Number of power distribution modules per PDP. */
038  public static final int kCTREPDPModules = PortsJNI.getNumCTREPDPModules();
039
040  /** Number of PCM Modules. */
041  public static final int kCTREPCMModules = PortsJNI.getNumCTREPCMModules();
042
043  /** Number of power distribution channels per PH. */
044  public static final int kREVPHChannels = PortsJNI.getNumREVPHChannels();
045
046  /** Number of PH modules. */
047  public static final int kREVPHModules = PortsJNI.getNumREVPHModules();
048
049  /**
050   * Check that the digital channel number is valid. Verify that the channel number is one of the
051   * legal channel numbers. Channel numbers are 0-based.
052   *
053   * @param channel The channel number to check.
054   */
055  public static void checkDigitalChannel(final int channel) {
056    if (!DIOJNI.checkDIOChannel(channel)) {
057      String buf =
058          "Requested DIO channel is out of range. Minimum: 0, Maximum: "
059              + kDigitalChannels
060              + ", Requested: "
061              + channel;
062      throw new IllegalArgumentException(buf);
063    }
064  }
065
066  /**
067   * Check that the PWM channel number is valid. Verify that the channel number is one of the legal
068   * channel numbers. Channel numbers are 0-based.
069   *
070   * @param channel The channel number to check.
071   */
072  public static void checkPWMChannel(final int channel) {
073    if (!PWMJNI.checkPWMChannel(channel)) {
074      String buf =
075          "Requested PWM channel is out of range. Minimum: 0, Maximum: "
076              + kPwmChannels
077              + ", Requested: "
078              + channel;
079      throw new IllegalArgumentException(buf);
080    }
081  }
082
083  /**
084   * Check that the analog input number is value. Verify that the analog input number is one of the
085   * legal channel numbers. Channel numbers are 0-based.
086   *
087   * @param channel The channel number to check.
088   */
089  public static void checkAnalogInputChannel(final int channel) {
090    if (!AnalogJNI.checkAnalogInputChannel(channel)) {
091      String buf =
092          "Requested analog input channel is out of range. Minimum: 0, Maximum: "
093              + kAnalogInputChannels
094              + ", Requested: "
095              + channel;
096      throw new IllegalArgumentException(buf);
097    }
098  }
099
100  /**
101   * Get the number of the default solenoid module.
102   *
103   * @return The number of the default solenoid module.
104   */
105  public static int getDefaultCTREPCMModule() {
106    return 0;
107  }
108
109  /**
110   * Get the number of the default solenoid module.
111   *
112   * @return The number of the default solenoid module.
113   */
114  public static int getDefaultREVPHModule() {
115    return 1;
116  }
117
118  private SensorUtil() {}
119}