001/*----------------------------------------------------------------------------*/
002/* Copyright (c) 2017-2018 FIRST. 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.livewindow.LiveWindow;
011
012/**
013 * Base class for all sensors. Stores most recent status information as well as containing utility
014 * functions for checking channels and error processing.
015 */
016public abstract class SendableBase implements Sendable {
017  private String m_name = "";
018  private String m_subsystem = "Ungrouped";
019
020  /**
021   * Creates an instance of the sensor base.
022   */
023  public SendableBase() {
024    this(true);
025  }
026
027  /**
028   * Creates an instance of the sensor base.
029   *
030   * @param addLiveWindow if true, add this Sendable to LiveWindow
031   */
032  public SendableBase(boolean addLiveWindow) {
033    if (addLiveWindow) {
034      LiveWindow.add(this);
035    }
036  }
037
038  /**
039   * Free the resources used by this object.
040   */
041  public void free() {
042    LiveWindow.remove(this);
043  }
044
045  @Override
046  public final synchronized String getName() {
047    return m_name;
048  }
049
050  @Override
051  public final synchronized void setName(String name) {
052    m_name = name;
053  }
054
055  /**
056   * Sets the name of the sensor with a channel number.
057   *
058   * @param moduleType A string that defines the module name in the label for the value
059   * @param channel    The channel number the device is plugged into
060   */
061  protected final void setName(String moduleType, int channel) {
062    setName(moduleType + "[" + channel + "]");
063  }
064
065  /**
066   * Sets the name of the sensor with a module and channel number.
067   *
068   * @param moduleType   A string that defines the module name in the label for the value
069   * @param moduleNumber The number of the particular module type
070   * @param channel      The channel number the device is plugged into (usually PWM)
071   */
072  protected final void setName(String moduleType, int moduleNumber, int channel) {
073    setName(moduleType + "[" + moduleNumber + "," + channel + "]");
074  }
075
076  @Override
077  public final synchronized String getSubsystem() {
078    return m_subsystem;
079  }
080
081  @Override
082  public final synchronized void setSubsystem(String subsystem) {
083    m_subsystem = subsystem;
084  }
085
086  /**
087   * Add a child component.
088   *
089   * @param child child component
090   */
091  protected final void addChild(Object child) {
092    LiveWindow.addChild(this, child);
093  }
094}