001/*----------------------------------------------------------------------------*/
002/* Copyright (c) 2014-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.hal.AnalogJNI;
011import edu.wpi.first.wpilibj.hal.FRCNetComm.tResourceType;
012import edu.wpi.first.wpilibj.hal.HAL;
013import edu.wpi.first.wpilibj.smartdashboard.SendableBuilder;
014
015/**
016 * Analog output class.
017 */
018public class AnalogOutput extends SendableBase implements Sendable {
019  private int m_port;
020  private int m_channel;
021
022  /**
023   * Construct an analog output on a specified MXP channel.
024   *
025   * @param channel The channel number to represent.
026   */
027  public AnalogOutput(final int channel) {
028    SensorBase.checkAnalogOutputChannel(channel);
029    m_channel = channel;
030
031    final int portHandle = AnalogJNI.getPort((byte) channel);
032    m_port = AnalogJNI.initializeAnalogOutputPort(portHandle);
033
034    HAL.report(tResourceType.kResourceType_AnalogOutput, channel);
035    setName("AnalogOutput", channel);
036  }
037
038  /**
039   * Channel destructor.
040   */
041  @Override
042  public void free() {
043    super.free();
044    AnalogJNI.freeAnalogOutputPort(m_port);
045    m_port = 0;
046    m_channel = 0;
047  }
048
049  /**
050   * Get the channel of this AnalogOutput.
051   */
052  public int getChannel() {
053    return m_channel;
054  }
055
056  public void setVoltage(double voltage) {
057    AnalogJNI.setAnalogOutput(m_port, voltage);
058  }
059
060  public double getVoltage() {
061    return AnalogJNI.getAnalogOutput(m_port);
062  }
063
064  @Override
065  public void initSendable(SendableBuilder builder) {
066    builder.setSmartDashboardType("Analog Output");
067    builder.addDoubleProperty("Value", this::getVoltage, this::setVoltage);
068  }
069}