mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-01 02:41:48 +00:00
[wpilib] Add IsJoystickConnected method (#2847)
This commit is contained in:
@@ -331,6 +331,11 @@ int DriverStation::GetJoystickAxisType(int stick, int axis) const {
|
||||
return static_cast<bool>(descriptor.axisTypes);
|
||||
}
|
||||
|
||||
bool DriverStation::IsJoystickConnected(int stick) const {
|
||||
return GetStickAxisCount(stick) > 0 || GetStickButtonCount(stick) > 0 ||
|
||||
GetStickPOVCount(stick) > 0;
|
||||
}
|
||||
|
||||
bool DriverStation::IsEnabled() const {
|
||||
HAL_ControlWord controlWord;
|
||||
HAL_GetControlWord(&controlWord);
|
||||
|
||||
@@ -47,6 +47,10 @@ int GenericHID::GetButtonCount() const {
|
||||
return m_ds->GetStickButtonCount(m_port);
|
||||
}
|
||||
|
||||
bool GenericHID::IsConnected() const {
|
||||
return m_ds->IsJoystickConnected(m_port);
|
||||
}
|
||||
|
||||
GenericHID::HIDType GenericHID::GetType() const {
|
||||
return static_cast<HIDType>(m_ds->GetJoystickType(m_port));
|
||||
}
|
||||
|
||||
@@ -181,6 +181,17 @@ class DriverStation : public ErrorBase {
|
||||
*/
|
||||
int GetJoystickAxisType(int stick, int axis) const;
|
||||
|
||||
/**
|
||||
* Returns if a joystick is connected to the Driver Station.
|
||||
*
|
||||
* This makes a best effort guess by looking at the reported number of axis,
|
||||
* buttons, and POVs attached.
|
||||
*
|
||||
* @param stick The joystick port number
|
||||
* @return true if a joystick is connected
|
||||
*/
|
||||
bool IsJoystickConnected(int stick) const;
|
||||
|
||||
/**
|
||||
* Check if the DS has enabled the robot.
|
||||
*
|
||||
|
||||
@@ -136,6 +136,13 @@ class GenericHID : public ErrorBase {
|
||||
*/
|
||||
int GetButtonCount() const;
|
||||
|
||||
/**
|
||||
* Get if the HID is connected.
|
||||
*
|
||||
* @return true if the HID is connected
|
||||
*/
|
||||
bool IsConnected() const;
|
||||
|
||||
/**
|
||||
* Get the type of the HID.
|
||||
*
|
||||
|
||||
34
wpilibc/src/test/native/cpp/DriverStationTest.cpp
Normal file
34
wpilibc/src/test/native/cpp/DriverStationTest.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include <tuple>
|
||||
|
||||
#include "frc/DriverStation.h"
|
||||
#include "frc/simulation/DriverStationSim.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
class IsJoystickConnectedParametersTests
|
||||
: public ::testing::TestWithParam<std::tuple<int, int, int, bool>> {};
|
||||
|
||||
TEST_P(IsJoystickConnectedParametersTests, IsJoystickConnected) {
|
||||
frc::sim::DriverStationSim::SetJoystickAxisCount(1, std::get<0>(GetParam()));
|
||||
frc::sim::DriverStationSim::SetJoystickButtonCount(1,
|
||||
std::get<1>(GetParam()));
|
||||
frc::sim::DriverStationSim::SetJoystickPOVCount(1, std::get<2>(GetParam()));
|
||||
frc::sim::DriverStationSim::NotifyNewData();
|
||||
|
||||
ASSERT_EQ(std::get<3>(GetParam()),
|
||||
frc::DriverStation::GetInstance().IsJoystickConnected(1));
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(IsConnectedTests, IsJoystickConnectedParametersTests,
|
||||
::testing::Values(std::make_tuple(0, 0, 0, false),
|
||||
std::make_tuple(1, 0, 0, true),
|
||||
std::make_tuple(0, 1, 0, true),
|
||||
std::make_tuple(0, 0, 1, true),
|
||||
std::make_tuple(1, 1, 1, true),
|
||||
std::make_tuple(4, 10, 1, true)));
|
||||
Reference in New Issue
Block a user