001/*----------------------------------------------------------------------------*/ 002/* Copyright (c) FIRST 2014-2017. 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.livewindow.LiveWindow; 014import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable; 015import edu.wpi.first.wpilibj.tables.ITable; 016 017/** 018 * Analog output class. 019 */ 020public class AnalogOutput extends SensorBase implements LiveWindowSendable { 021 private int m_port; 022 private int m_channel; 023 024 /** 025 * Construct an analog output on a specified MXP channel. 026 * 027 * @param channel The channel number to represent. 028 */ 029 public AnalogOutput(final int channel) { 030 m_channel = channel; 031 032 SensorBase.checkAnalogOutputChannel(channel); 033 034 final int portHandle = AnalogJNI.getPort((byte) channel); 035 m_port = AnalogJNI.initializeAnalogOutputPort(portHandle); 036 037 LiveWindow.addSensor("AnalogOutput", channel, this); 038 HAL.report(tResourceType.kResourceType_AnalogOutput, channel); 039 } 040 041 /** 042 * Channel destructor. 043 */ 044 public void free() { 045 AnalogJNI.freeAnalogOutputPort(m_port); 046 m_port = 0; 047 m_channel = 0; 048 } 049 050 /** 051 * Get the channel of this AnalogOutput. 052 */ 053 public int getChannel() { 054 return m_channel; 055 } 056 057 public void setVoltage(double voltage) { 058 AnalogJNI.setAnalogOutput(m_port, voltage); 059 } 060 061 public double getVoltage() { 062 return AnalogJNI.getAnalogOutput(m_port); 063 } 064 065 /* 066 * Live Window code, only does anything if live window is activated. 067 */ 068 @Override 069 public String getSmartDashboardType() { 070 return "Analog Output"; 071 } 072 073 private ITable m_table; 074 075 @Override 076 public void initTable(ITable subtable) { 077 m_table = subtable; 078 updateTable(); 079 } 080 081 @Override 082 public void updateTable() { 083 if (m_table != null) { 084 m_table.putNumber("Value", getVoltage()); 085 } 086 } 087 088 @Override 089 public ITable getTable() { 090 return m_table; 091 } 092 093 /** 094 * Analog Channels don't have to do anything special when entering the LiveWindow. {@inheritDoc} 095 */ 096 @Override 097 public void startLiveWindowMode() { 098 } 099 100 /** 101 * Analog Channels don't have to do anything special when exiting the LiveWindow. {@inheritDoc} 102 */ 103 @Override 104 public void stopLiveWindowMode() { 105 } 106}