blob: 3e0720851a54d4e72f8b1c455cdd3294785ee2e7 [file] [log] [blame]
Parker Schuhd3b7a8872018-02-19 16:42:27 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008-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 "frc971/wpilib/ahal/DigitalInput.h"
9
10#include <limits>
11#include <sstream>
12
Austin Schuhf6b94632019-02-02 22:11:27 -080013#include "hal/DIO.h"
14#include "hal/HAL.h"
15#include "hal/Ports.h"
Parker Schuhd3b7a8872018-02-19 16:42:27 -080016#include "frc971/wpilib/ahal/WPIErrors.h"
17
18using namespace frc;
19
20/**
21 * Create an instance of a Digital Input class.
22 *
23 * Creates a digital input given a channel.
24 *
25 * @param channel The DIO channel 0-9 are on-board, 10-25 are on the MXP port
26 */
27DigitalInput::DigitalInput(int channel) {
28 std::stringstream buf;
29
30 if (!CheckDigitalChannel(channel)) {
31 buf << "Digital Channel " << channel;
32 wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str());
33 m_channel = std::numeric_limits<int>::max();
34 return;
35 }
36 m_channel = channel;
37
38 int32_t status = 0;
Austin Schuh9950f682021-11-06 15:27:58 -070039 m_handle =
40 HAL_InitializeDIOPort(HAL_GetPort(channel), true, nullptr, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080041 if (status != 0) {
42 wpi_setErrorWithContextRange(status, 0, HAL_GetNumDigitalChannels(),
43 channel, HAL_GetErrorMessage(status));
44 m_handle = HAL_kInvalidHandle;
45 m_channel = std::numeric_limits<int>::max();
46 return;
47 }
48
49 HAL_Report(HALUsageReporting::kResourceType_DigitalInput, channel);
50}
51
52/**
53 * Free resources associated with the Digital Input class.
54 */
55DigitalInput::~DigitalInput() {
56 if (StatusIsFatal()) return;
57 if (m_interrupt != HAL_kInvalidHandle) {
Austin Schuh9950f682021-11-06 15:27:58 -070058 HAL_CleanInterrupts(m_interrupt);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080059 m_interrupt = HAL_kInvalidHandle;
60 }
61
62 HAL_FreeDIOPort(m_handle);
63}
64
65/**
66 * Get the value from a digital input channel.
67 *
68 * Retrieve the value of a single digital input channel from the FPGA.
69 */
70bool DigitalInput::Get() const {
71 if (StatusIsFatal()) return false;
72 int32_t status = 0;
73 bool value = HAL_GetDIO(m_handle, &status);
74 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
75 return value;
76}
77
78/**
79 * @return The GPIO channel number that this object represents.
80 */
81int DigitalInput::GetChannel() const { return m_channel; }
82
83/**
84 * @return The HAL Handle to the specified source.
85 */
86HAL_Handle DigitalInput::GetPortHandleForRouting() const { return m_handle; }
87
88/**
89 * Is source an AnalogTrigger
90 */
91bool DigitalInput::IsAnalogTrigger() const { return false; }
92
93/**
94 * @return The type of analog trigger output to be used. 0 for Digitals
95 */
96AnalogTriggerType DigitalInput::GetAnalogTriggerTypeForRouting() const {
97 return (AnalogTriggerType)0;
98}