blob: dcffea6a719cd6372fcb9234ffc8fd07c653da4e [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2016-2017. 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 "AnalogPotentiometer.h"
9
10using namespace frc;
11
12/**
13 * Common initialization code called by all constructors.
14 */
15void AnalogPotentiometer::initPot(AnalogInput* input, double scale,
16 double offset) {
17 m_scale = scale;
18 m_offset = offset;
19 m_analog_input = input;
20}
21
22AnalogPotentiometer::AnalogPotentiometer(int channel, double scale,
23 double offset) {
24 m_init_analog_input = true;
25 initPot(new AnalogInput(channel), scale, offset);
26}
27
28AnalogPotentiometer::AnalogPotentiometer(AnalogInput* input, double scale,
29 double offset) {
30 m_init_analog_input = false;
31 initPot(input, scale, offset);
32}
33
34AnalogPotentiometer::AnalogPotentiometer(AnalogInput& input, double scale,
35 double offset) {
36 m_init_analog_input = false;
37 initPot(&input, scale, offset);
38}
39
40AnalogPotentiometer::~AnalogPotentiometer() {
41 if (m_init_analog_input) {
42 delete m_analog_input;
43 m_init_analog_input = false;
44 }
45}
46
47/**
48 * Get the current reading of the potentiomere.
49 *
50 * @return The current position of the potentiometer.
51 */
52double AnalogPotentiometer::Get() const {
53 return m_analog_input->GetVoltage() * m_scale + m_offset;
54}
55
56/**
57 * Implement the PIDSource interface.
58 *
59 * @return The current reading.
60 */
61double AnalogPotentiometer::PIDGet() { return Get(); }
62
63/**
64 * @return the Smart Dashboard Type
65 */
66std::string AnalogPotentiometer::GetSmartDashboardType() const {
67 return "Analog Input";
68}
69
70/**
71 * Live Window code, only does anything if live window is activated.
72 */
73void AnalogPotentiometer::InitTable(std::shared_ptr<ITable> subtable) {
74 m_table = subtable;
75 UpdateTable();
76}
77
78void AnalogPotentiometer::UpdateTable() {
79 if (m_table != nullptr) {
80 m_table->PutNumber("Value", Get());
81 }
82}
83
84std::shared_ptr<ITable> AnalogPotentiometer::GetTable() const {
85 return m_table;
86}