blob: 19538a4fe09f3c8e1492313aa9f64e131a9f8e6f [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
Brian Silverman1a675112016-02-20 20:42:49 -05002/* Copyright (c) FIRST 2008-2016. All Rights Reserved. */
Brian Silverman26e4e522015-12-17 01:56:40 -05003/* Open Source Software - may be modified and shared by FRC teams. The code */
Brian Silverman1a675112016-02-20 20:42:49 -05004/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
Brian Silverman26e4e522015-12-17 01:56:40 -05006/*----------------------------------------------------------------------------*/
7
8#include "DigitalInput.h"
9#include "Resource.h"
10#include "WPIErrors.h"
11#include "LiveWindow/LiveWindow.h"
12
13#include <limits>
14#include <sstream>
15
16/**
17 * Create an instance of a Digital Input class.
18 * Creates a digital input given a channel.
19 *
20 * @param channel The DIO channel 0-9 are on-board, 10-25 are on the MXP port
21 */
22DigitalInput::DigitalInput(uint32_t channel) {
23 std::stringstream buf;
24
25 if (!CheckDigitalChannel(channel)) {
26 buf << "Digital Channel " << channel;
27 wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str());
28 m_channel = std::numeric_limits<uint32_t>::max();
29 return;
30 }
31 m_channel = channel;
32
33 int32_t status = 0;
34 allocateDIO(m_digital_ports[channel], true, &status);
35 wpi_setErrorWithContext(status, getHALErrorMessage(status));
36
37 LiveWindow::GetInstance()->AddSensor("DigitalInput", channel, this);
38 HALReport(HALUsageReporting::kResourceType_DigitalInput, channel);
39}
40
41/**
42 * Free resources associated with the Digital Input class.
43 */
44DigitalInput::~DigitalInput() {
45 if (StatusIsFatal()) return;
46 if (m_interrupt != nullptr) {
47 int32_t status = 0;
48 cleanInterrupts(m_interrupt, &status);
49 wpi_setErrorWithContext(status, getHALErrorMessage(status));
50 m_interrupt = nullptr;
51 m_interrupts->Free(m_interruptIndex);
52 }
53
54 int32_t status = 0;
55 freeDIO(m_digital_ports[m_channel], &status);
56 wpi_setErrorWithContext(status, getHALErrorMessage(status));
57}
58
59/**
60 * Get the value from a digital input channel.
61 * Retrieve the value of a single digital input channel from the FPGA.
62 */
63bool DigitalInput::Get() const {
64 if (StatusIsFatal()) return false;
65 int32_t status = 0;
66 bool value = getDIO(m_digital_ports[m_channel], &status);
67 wpi_setErrorWithContext(status, getHALErrorMessage(status));
68 return value;
69}
70
71/**
72 * @return The GPIO channel number that this object represents.
73 */
74uint32_t DigitalInput::GetChannel() const { return m_channel; }
75
76/**
77 * @return The value to be written to the channel field of a routing mux.
78 */
79uint32_t DigitalInput::GetChannelForRouting() const { return GetChannel(); }
80
81/**
82 * @return The value to be written to the module field of a routing mux.
83 */
84uint32_t DigitalInput::GetModuleForRouting() const { return 0; }
85
86/**
87 * @return The value to be written to the analog trigger field of a routing mux.
88 */
89bool DigitalInput::GetAnalogTriggerForRouting() const { return false; }
90
91void DigitalInput::UpdateTable() {
92 if (m_table != nullptr) {
93 m_table->PutBoolean("Value", Get());
94 }
95}
96
97void DigitalInput::StartLiveWindowMode() {}
98
99void DigitalInput::StopLiveWindowMode() {}
100
101std::string DigitalInput::GetSmartDashboardType() const {
102 return "DigitalInput";
103}
104
105void DigitalInput::InitTable(std::shared_ptr<ITable> subTable) {
106 m_table = subTable;
107 UpdateTable();
108}
109
110std::shared_ptr<ITable> DigitalInput::GetTable() const { return m_table; }