mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
This makes code easier to read and more consistent between C++ and Java. Also update clang-format settings to always add a line break (even if no braces are used).
77 lines
2.0 KiB
C++
77 lines
2.0 KiB
C++
// Copyright (c) FIRST and other WPILib contributors.
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
#include "frc/AnalogOutput.h"
|
|
|
|
#include <limits>
|
|
#include <utility>
|
|
|
|
#include <hal/AnalogOutput.h>
|
|
#include <hal/FRCUsageReporting.h>
|
|
#include <hal/HALBase.h>
|
|
#include <hal/Ports.h>
|
|
|
|
#include "frc/SensorUtil.h"
|
|
#include "frc/WPIErrors.h"
|
|
#include "frc/smartdashboard/SendableBuilder.h"
|
|
#include "frc/smartdashboard/SendableRegistry.h"
|
|
|
|
using namespace frc;
|
|
|
|
AnalogOutput::AnalogOutput(int channel) {
|
|
if (!SensorUtil::CheckAnalogOutputChannel(channel)) {
|
|
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange,
|
|
"analog output " + wpi::Twine(channel));
|
|
m_channel = std::numeric_limits<int>::max();
|
|
m_port = HAL_kInvalidHandle;
|
|
return;
|
|
}
|
|
|
|
m_channel = channel;
|
|
|
|
HAL_PortHandle port = HAL_GetPort(m_channel);
|
|
int32_t status = 0;
|
|
m_port = HAL_InitializeAnalogOutputPort(port, &status);
|
|
if (status != 0) {
|
|
wpi_setHALErrorWithRange(status, 0, HAL_GetNumAnalogOutputs(), channel);
|
|
m_channel = std::numeric_limits<int>::max();
|
|
m_port = HAL_kInvalidHandle;
|
|
return;
|
|
}
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_AnalogOutput, m_channel + 1);
|
|
SendableRegistry::GetInstance().AddLW(this, "AnalogOutput", m_channel);
|
|
}
|
|
|
|
AnalogOutput::~AnalogOutput() {
|
|
HAL_FreeAnalogOutputPort(m_port);
|
|
}
|
|
|
|
void AnalogOutput::SetVoltage(double voltage) {
|
|
int32_t status = 0;
|
|
HAL_SetAnalogOutput(m_port, voltage, &status);
|
|
|
|
wpi_setHALError(status);
|
|
}
|
|
|
|
double AnalogOutput::GetVoltage() const {
|
|
int32_t status = 0;
|
|
double voltage = HAL_GetAnalogOutput(m_port, &status);
|
|
|
|
wpi_setHALError(status);
|
|
|
|
return voltage;
|
|
}
|
|
|
|
int AnalogOutput::GetChannel() const {
|
|
return m_channel;
|
|
}
|
|
|
|
void AnalogOutput::InitSendable(SendableBuilder& builder) {
|
|
builder.SetSmartDashboardType("Analog Output");
|
|
builder.AddDoubleProperty(
|
|
"Value", [=]() { return GetVoltage(); },
|
|
[=](double value) { SetVoltage(value); });
|
|
}
|