Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2014. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "AnalogOutput.h" |
| 9 | #include "Resource.h" |
| 10 | #include "WPIErrors.h" |
| 11 | #include "LiveWindow/LiveWindow.h" |
| 12 | #include "HAL/Port.h" |
| 13 | |
| 14 | #include <limits> |
| 15 | #include <sstream> |
| 16 | |
| 17 | static std::unique_ptr<Resource> outputs; |
| 18 | |
| 19 | /** |
| 20 | * Construct an analog output on the given channel. |
| 21 | * All analog outputs are located on the MXP port. |
| 22 | * @param The channel number on the roboRIO to represent. |
| 23 | */ |
| 24 | AnalogOutput::AnalogOutput(uint32_t channel) { |
| 25 | Resource::CreateResourceObject(outputs, kAnalogOutputs); |
| 26 | |
| 27 | std::stringstream buf; |
| 28 | buf << "analog input " << channel; |
| 29 | |
| 30 | if (!checkAnalogOutputChannel(channel)) { |
| 31 | wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str()); |
| 32 | m_channel = std::numeric_limits<uint32_t>::max(); |
| 33 | m_port = nullptr; |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | if (outputs->Allocate(channel, buf.str()) == |
| 38 | std::numeric_limits<uint32_t>::max()) { |
| 39 | CloneError(*outputs); |
| 40 | m_channel = std::numeric_limits<uint32_t>::max(); |
| 41 | m_port = nullptr; |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | m_channel = channel; |
| 46 | |
| 47 | void* port = getPort(m_channel); |
| 48 | int32_t status = 0; |
| 49 | m_port = initializeAnalogOutputPort(port, &status); |
| 50 | wpi_setErrorWithContext(status, getHALErrorMessage(status)); |
| 51 | freePort(port); |
| 52 | |
| 53 | LiveWindow::GetInstance()->AddActuator("AnalogOutput", m_channel, this); |
| 54 | HALReport(HALUsageReporting::kResourceType_AnalogOutput, m_channel); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Destructor. Frees analog output resource |
| 59 | */ |
| 60 | AnalogOutput::~AnalogOutput() { |
| 61 | freeAnalogOutputPort(m_port); |
| 62 | outputs->Free(m_channel); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Set the value of the analog output |
| 67 | * |
| 68 | * @param voltage The output value in Volts, from 0.0 to +5.0 |
| 69 | */ |
| 70 | void AnalogOutput::SetVoltage(float voltage) { |
| 71 | int32_t status = 0; |
| 72 | setAnalogOutput(m_port, voltage, &status); |
| 73 | |
| 74 | wpi_setErrorWithContext(status, getHALErrorMessage(status)); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get the voltage of the analog output |
| 79 | * |
| 80 | * @return The value in Volts, from 0.0 to +5.0 |
| 81 | */ |
| 82 | float AnalogOutput::GetVoltage() const { |
| 83 | int32_t status = 0; |
| 84 | float voltage = getAnalogOutput(m_port, &status); |
| 85 | |
| 86 | wpi_setErrorWithContext(status, getHALErrorMessage(status)); |
| 87 | |
| 88 | return voltage; |
| 89 | } |
| 90 | |
| 91 | void AnalogOutput::UpdateTable() { |
| 92 | if (m_table != nullptr) { |
| 93 | m_table->PutNumber("Value", GetVoltage()); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void AnalogOutput::StartLiveWindowMode() {} |
| 98 | |
| 99 | void AnalogOutput::StopLiveWindowMode() {} |
| 100 | |
| 101 | std::string AnalogOutput::GetSmartDashboardType() const { |
| 102 | return "Analog Output"; |
| 103 | } |
| 104 | |
| 105 | void AnalogOutput::InitTable(std::shared_ptr<ITable> subTable) { |
| 106 | m_table = subTable; |
| 107 | UpdateTable(); |
| 108 | } |
| 109 | |
| 110 | std::shared_ptr<ITable> AnalogOutput::GetTable() const { return m_table; } |