Files
allwpilib/wpilibc/src/main/native/cpp/DigitalInput.cpp
Tyler Veness fbdc810887 Upgrade to C++20 (#4239)
* Use explicit this capture required by C++20
* Use C++20 span
* Replace wpi::numbers with std::numbers
* Fix C++20 clang-tidy warning false positive in fmt
* Remove ciso646 include since C++20 removed that header
* Fix global-buffer-overflow asan warnings in ntcore tests
* Add DIOSetProxy constructor to HAL

* Upgrade MSVC compiler to 2022
* Bump native-utils to 2023.2.7 (changes to std=c++20)

Co-authored-by: Peter Johnson <johnson.peter@gmail.com>
2022-10-15 16:33:14 -07:00

74 lines
2.0 KiB
C++

// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "frc/DigitalInput.h"
#include <limits>
#include <hal/DIO.h>
#include <hal/FRCUsageReporting.h>
#include <hal/HALBase.h>
#include <hal/Ports.h>
#include <wpi/StackTrace.h>
#include <wpi/sendable/SendableBuilder.h>
#include <wpi/sendable/SendableRegistry.h>
#include "frc/Errors.h"
#include "frc/SensorUtil.h"
using namespace frc;
DigitalInput::DigitalInput(int channel) {
if (!SensorUtil::CheckDigitalChannel(channel)) {
throw FRC_MakeError(err::ChannelIndexOutOfRange, "Channel {}", channel);
}
m_channel = channel;
int32_t status = 0;
std::string stackTrace = wpi::GetStackTrace(1);
m_handle = HAL_InitializeDIOPort(HAL_GetPort(channel), true,
stackTrace.c_str(), &status);
FRC_CheckErrorStatus(status, "Channel {}", channel);
HAL_Report(HALUsageReporting::kResourceType_DigitalInput, channel + 1);
wpi::SendableRegistry::AddLW(this, "DigitalInput", channel);
}
DigitalInput::~DigitalInput() {
HAL_FreeDIOPort(m_handle);
}
bool DigitalInput::Get() const {
int32_t status = 0;
bool value = HAL_GetDIO(m_handle, &status);
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
return value;
}
HAL_Handle DigitalInput::GetPortHandleForRouting() const {
return m_handle;
}
AnalogTriggerType DigitalInput::GetAnalogTriggerTypeForRouting() const {
return static_cast<AnalogTriggerType>(0);
}
bool DigitalInput::IsAnalogTrigger() const {
return false;
}
void DigitalInput::SetSimDevice(HAL_SimDeviceHandle device) {
HAL_SetDIOSimDevice(m_handle, device);
}
int DigitalInput::GetChannel() const {
return m_channel;
}
void DigitalInput::InitSendable(wpi::SendableBuilder& builder) {
builder.SetSmartDashboardType("Digital Input");
builder.AddBooleanProperty(
"Value", [=, this] { return Get(); }, nullptr);
}