Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2008. All Rights Reserved. |
| 3 | */ |
| 4 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 5 | /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 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 | */ |
| 22 | DigitalInput::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 | */ |
| 44 | DigitalInput::~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 | */ |
| 63 | bool 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 | */ |
| 74 | uint32_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 | */ |
| 79 | uint32_t DigitalInput::GetChannelForRouting() const { return GetChannel(); } |
| 80 | |
| 81 | /** |
| 82 | * @return The value to be written to the module field of a routing mux. |
| 83 | */ |
| 84 | uint32_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 | */ |
| 89 | bool DigitalInput::GetAnalogTriggerForRouting() const { return false; } |
| 90 | |
| 91 | void DigitalInput::UpdateTable() { |
| 92 | if (m_table != nullptr) { |
| 93 | m_table->PutBoolean("Value", Get()); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void DigitalInput::StartLiveWindowMode() {} |
| 98 | |
| 99 | void DigitalInput::StopLiveWindowMode() {} |
| 100 | |
| 101 | std::string DigitalInput::GetSmartDashboardType() const { |
| 102 | return "DigitalInput"; |
| 103 | } |
| 104 | |
| 105 | void DigitalInput::InitTable(std::shared_ptr<ITable> subTable) { |
| 106 | m_table = subTable; |
| 107 | UpdateTable(); |
| 108 | } |
| 109 | |
| 110 | std::shared_ptr<ITable> DigitalInput::GetTable() const { return m_table; } |