blob: a39127135b62d98d7f1116de88903217d48a2f2e [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
Brian Silverman8fce7482020-01-05 13:18:21 -08004
5#include "frc/AnalogOutput.h"
6
7#include <limits>
8#include <utility>
9
10#include <hal/AnalogOutput.h>
11#include <hal/FRCUsageReporting.h>
12#include <hal/HALBase.h>
13#include <hal/Ports.h>
Austin Schuh812d0d12021-11-04 20:16:48 -070014#include <wpi/StackTrace.h>
15#include <wpi/sendable/SendableBuilder.h>
16#include <wpi/sendable/SendableRegistry.h>
Brian Silverman8fce7482020-01-05 13:18:21 -080017
Austin Schuh812d0d12021-11-04 20:16:48 -070018#include "frc/Errors.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080019#include "frc/SensorUtil.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080020
21using namespace frc;
22
23AnalogOutput::AnalogOutput(int channel) {
24 if (!SensorUtil::CheckAnalogOutputChannel(channel)) {
Austin Schuh812d0d12021-11-04 20:16:48 -070025 throw FRC_MakeError(err::ChannelIndexOutOfRange, "Channel {}", channel);
Brian Silverman8fce7482020-01-05 13:18:21 -080026 }
27
28 m_channel = channel;
29
30 HAL_PortHandle port = HAL_GetPort(m_channel);
31 int32_t status = 0;
Austin Schuh812d0d12021-11-04 20:16:48 -070032 std::string stackTrace = wpi::GetStackTrace(1);
33 m_port = HAL_InitializeAnalogOutputPort(port, stackTrace.c_str(), &status);
34 FRC_CheckErrorStatus(status, "Channel {}", channel);
Brian Silverman8fce7482020-01-05 13:18:21 -080035
36 HAL_Report(HALUsageReporting::kResourceType_AnalogOutput, m_channel + 1);
Austin Schuh812d0d12021-11-04 20:16:48 -070037 wpi::SendableRegistry::AddLW(this, "AnalogOutput", m_channel);
Brian Silverman8fce7482020-01-05 13:18:21 -080038}
39
Austin Schuh812d0d12021-11-04 20:16:48 -070040AnalogOutput::~AnalogOutput() {
41 HAL_FreeAnalogOutputPort(m_port);
42}
Brian Silverman8fce7482020-01-05 13:18:21 -080043
44void AnalogOutput::SetVoltage(double voltage) {
45 int32_t status = 0;
46 HAL_SetAnalogOutput(m_port, voltage, &status);
Austin Schuh812d0d12021-11-04 20:16:48 -070047 FRC_CheckErrorStatus(status, "Channel {}", m_channel);
Brian Silverman8fce7482020-01-05 13:18:21 -080048}
49
50double AnalogOutput::GetVoltage() const {
51 int32_t status = 0;
52 double voltage = HAL_GetAnalogOutput(m_port, &status);
Austin Schuh812d0d12021-11-04 20:16:48 -070053 FRC_CheckErrorStatus(status, "Channel {}", m_channel);
Brian Silverman8fce7482020-01-05 13:18:21 -080054 return voltage;
55}
56
Austin Schuh812d0d12021-11-04 20:16:48 -070057int AnalogOutput::GetChannel() const {
58 return m_channel;
59}
Brian Silverman8fce7482020-01-05 13:18:21 -080060
Austin Schuh812d0d12021-11-04 20:16:48 -070061void AnalogOutput::InitSendable(wpi::SendableBuilder& builder) {
Brian Silverman8fce7482020-01-05 13:18:21 -080062 builder.SetSmartDashboardType("Analog Output");
Austin Schuh1e69f942020-11-14 15:06:14 -080063 builder.AddDoubleProperty(
Austin Schuh812d0d12021-11-04 20:16:48 -070064 "Value", [=] { return GetVoltage(); },
Austin Schuh1e69f942020-11-14 15:06:14 -080065 [=](double value) { SetVoltage(value); });
Brian Silverman8fce7482020-01-05 13:18:21 -080066}