2014-06-12 18:07:45 -04:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) FIRST 2014. All Rights Reserved. */
|
|
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "AnalogOutput.h"
|
|
|
|
|
#include "Resource.h"
|
|
|
|
|
#include "WPIErrors.h"
|
|
|
|
|
#include "LiveWindow/LiveWindow.h"
|
|
|
|
|
|
2015-06-23 04:49:51 -07:00
|
|
|
static Resource *outputs = nullptr;
|
2014-06-12 18:07:45 -04:00
|
|
|
|
|
|
|
|
void AnalogOutput::InitAnalogOutput(uint32_t channel) {
|
2015-06-25 15:07:55 -04:00
|
|
|
Resource::CreateResourceObject(&outputs, kAnalogOutputs);
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
char buf[64];
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
if (!checkAnalogOutputChannel(channel)) {
|
|
|
|
|
snprintf(buf, 64, "analog input %d", channel);
|
|
|
|
|
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
if (outputs->Allocate(channel, buf) == ~0ul) {
|
|
|
|
|
CloneError(outputs);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
m_channel = channel;
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void *port = getPort(m_channel);
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
m_port = initializeAnalogOutputPort(port, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
LiveWindow::GetInstance()->AddActuator("AnalogOutput", m_channel, this);
|
|
|
|
|
HALReport(HALUsageReporting::kResourceType_AnalogOutput, m_channel);
|
2014-06-12 18:07:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-12-29 14:09:37 -05:00
|
|
|
* Construct an analog output on the given channel.
|
|
|
|
|
* All analog outputs are located on the MXP port.
|
|
|
|
|
* @param The channel number on the roboRIO to represent.
|
2014-06-12 18:07:45 -04:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
AnalogOutput::AnalogOutput(uint32_t channel) { InitAnalogOutput(channel); }
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2014-12-29 14:09:37 -05:00
|
|
|
/**
|
|
|
|
|
* Destructor. Frees analog output resource
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
AnalogOutput::~AnalogOutput() { outputs->Free(m_channel); }
|
2014-06-12 18:07:45 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the value of the analog output
|
|
|
|
|
*
|
|
|
|
|
* @param voltage The output value in Volts, from 0.0 to +5.0
|
|
|
|
|
*/
|
|
|
|
|
void AnalogOutput::SetVoltage(float voltage) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
|
|
|
|
setAnalogOutput(m_port, voltage, &status);
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2014-06-12 18:07:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the voltage of the analog output
|
|
|
|
|
*
|
|
|
|
|
* @return The value in Volts, from 0.0 to +5.0
|
|
|
|
|
*/
|
2015-06-19 17:23:54 -07:00
|
|
|
float AnalogOutput::GetVoltage() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
|
|
|
|
float voltage = getAnalogOutput(m_port, &status);
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
return voltage;
|
2014-06-12 18:07:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AnalogOutput::UpdateTable() {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_table != nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table->PutNumber("Value", GetVoltage());
|
|
|
|
|
}
|
2014-06-12 18:07:45 -04:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void AnalogOutput::StartLiveWindowMode() {}
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void AnalogOutput::StopLiveWindowMode() {}
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2015-06-19 17:23:54 -07:00
|
|
|
std::string AnalogOutput::GetSmartDashboardType() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
return "Analog Output";
|
2014-06-12 18:07:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AnalogOutput::InitTable(ITable *subTable) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table = subTable;
|
|
|
|
|
UpdateTable();
|
2014-06-12 18:07:45 -04:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
ITable *AnalogOutput::GetTable() const { return m_table; }
|