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