mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-04 03:11:43 +00:00
[wpilib,cmd] Cache HID wrappers (#8970)
Store DriverStation-owned GenericHID and Gamepad instances in Java and C++, and expose the cached objects to Python bindings. Move hand-written command gamepad and joystick wrappers to compose cached CommandGenericHID instances plus typed HID wrappers, including a Python CommandGamepad. This will let us remove UserControls, while helping ensure that we don't have state smashing between GenericHID objects. Another bonus is without inheritance, intellisense will no longer show a bunch of annoying methods, and instead just what actually exists. --------- Co-authored-by: Peter Johnson <johnson.peter@gmail.com>
This commit is contained in:
69
wpilibc/src/main/native/cpp/driverstation/DriverStation.cpp
Normal file
69
wpilibc/src/main/native/cpp/driverstation/DriverStation.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
// 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 "wpi/driverstation/DriverStation.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
#include "wpi/driverstation/Gamepad.hpp"
|
||||
#include "wpi/driverstation/GenericHID.hpp"
|
||||
#include "wpi/system/Errors.hpp"
|
||||
|
||||
using namespace wpi;
|
||||
|
||||
namespace {
|
||||
std::mutex dsMutex;
|
||||
std::array<std::unique_ptr<GenericHID>,
|
||||
wpi::internal::DriverStationBackend::JOYSTICK_PORTS>
|
||||
hids;
|
||||
std::array<std::unique_ptr<Gamepad>,
|
||||
wpi::internal::DriverStationBackend::JOYSTICK_PORTS>
|
||||
gamepads;
|
||||
|
||||
void ValidatePort(int port) {
|
||||
if (port < 0 || port >= wpi::internal::DriverStationBackend::JOYSTICK_PORTS) {
|
||||
throw WPILIB_MakeError(warn::BadJoystickIndex, "port {} out of range",
|
||||
port);
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
GenericHID& DriverStation::GetGenericHIDUnderLock(int port) {
|
||||
ValidatePort(port);
|
||||
|
||||
if (!hids[port]) {
|
||||
hids[port].reset(new GenericHID(port));
|
||||
}
|
||||
|
||||
return *hids[port];
|
||||
}
|
||||
|
||||
GenericHID& DriverStation::GetGenericHID(int port) {
|
||||
std::scoped_lock lock{dsMutex};
|
||||
return GetGenericHIDUnderLock(port);
|
||||
}
|
||||
|
||||
Gamepad& DriverStation::GetGamepad(int port) {
|
||||
std::scoped_lock lock{dsMutex};
|
||||
ValidatePort(port);
|
||||
|
||||
if (!gamepads[port]) {
|
||||
gamepads[port] = std::make_unique<Gamepad>(GetGenericHIDUnderLock(port));
|
||||
}
|
||||
|
||||
return *gamepads[port];
|
||||
}
|
||||
|
||||
void wpi::internal::DriverStationBackend::ResetCachedHIDData() {
|
||||
std::scoped_lock lock{dsMutex};
|
||||
|
||||
for (auto& gamepad : gamepads) {
|
||||
gamepad.reset();
|
||||
}
|
||||
for (auto& hid : hids) {
|
||||
hid.reset();
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,9 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <string>
|
||||
|
||||
#include "wpi/driverstation/DriverStation.hpp"
|
||||
#include "wpi/event/BooleanEvent.hpp"
|
||||
#include "wpi/hal/UsageReporting.hpp"
|
||||
#include "wpi/math/util/MathUtil.hpp"
|
||||
@@ -21,8 +23,18 @@ static double ClampDeadband(double deadband) {
|
||||
return std::clamp(deadband, 0.0, std::nextafter(1.0, 0.0));
|
||||
}
|
||||
|
||||
Gamepad::Gamepad(int port) : GenericHID(port) {
|
||||
HAL_ReportUsage("HID", port, "Gamepad");
|
||||
Gamepad::Gamepad(int port) : Gamepad{DriverStation::GetGenericHID(port)} {}
|
||||
|
||||
Gamepad::Gamepad(GenericHID& hid) : m_hid{&hid} {
|
||||
HAL_ReportUsage("HID", hid.GetPort(), "Gamepad");
|
||||
}
|
||||
|
||||
GenericHID& Gamepad::GetHID() {
|
||||
return *m_hid;
|
||||
}
|
||||
|
||||
const GenericHID& Gamepad::GetHID() const {
|
||||
return *m_hid;
|
||||
}
|
||||
|
||||
double Gamepad::GetLeftX() const {
|
||||
@@ -512,44 +524,88 @@ BooleanEvent Gamepad::Misc6(EventLoop* loop) const {
|
||||
}
|
||||
|
||||
bool Gamepad::GetButton(Button button) const {
|
||||
return GetRawButton(static_cast<int>(button));
|
||||
return m_hid->GetRawButton(static_cast<int>(button));
|
||||
}
|
||||
|
||||
bool Gamepad::GetButtonPressed(Button button) {
|
||||
return GetRawButtonPressed(static_cast<int>(button));
|
||||
return m_hid->GetRawButtonPressed(static_cast<int>(button));
|
||||
}
|
||||
|
||||
bool Gamepad::GetButtonReleased(Button button) {
|
||||
return GetRawButtonReleased(static_cast<int>(button));
|
||||
return m_hid->GetRawButtonReleased(static_cast<int>(button));
|
||||
}
|
||||
|
||||
BooleanEvent Gamepad::ButtonEvent(Button button, EventLoop* loop) const {
|
||||
return GenericHID::Button(static_cast<int>(button), loop);
|
||||
return m_hid->Button(static_cast<int>(button), loop);
|
||||
}
|
||||
|
||||
double Gamepad::GetAxis(Axis axis) const {
|
||||
return GetRawAxis(static_cast<int>(axis));
|
||||
return m_hid->GetRawAxis(static_cast<int>(axis));
|
||||
}
|
||||
|
||||
BooleanEvent Gamepad::AxisLessThan(Axis axis, double threshold,
|
||||
EventLoop* loop) const {
|
||||
return GenericHID::AxisLessThan(static_cast<int>(axis), threshold, loop);
|
||||
return m_hid->AxisLessThan(static_cast<int>(axis), threshold, loop);
|
||||
}
|
||||
|
||||
BooleanEvent Gamepad::AxisGreaterThan(Axis axis, double threshold,
|
||||
EventLoop* loop) const {
|
||||
return GenericHID::AxisGreaterThan(static_cast<int>(axis), threshold, loop);
|
||||
return m_hid->AxisGreaterThan(static_cast<int>(axis), threshold, loop);
|
||||
}
|
||||
|
||||
int Gamepad::GetAxesAvailable() const {
|
||||
return m_hid->GetAxesAvailable();
|
||||
}
|
||||
|
||||
uint64_t Gamepad::GetButtonsAvailable() const {
|
||||
return m_hid->GetButtonsAvailable();
|
||||
}
|
||||
|
||||
bool Gamepad::IsConnected() const {
|
||||
return m_hid->IsConnected();
|
||||
}
|
||||
|
||||
GenericHID::HIDType Gamepad::GetGamepadType() const {
|
||||
return m_hid->GetGamepadType();
|
||||
}
|
||||
|
||||
GenericHID::SupportedOutputs Gamepad::GetSupportedOutputs() const {
|
||||
return m_hid->GetSupportedOutputs();
|
||||
}
|
||||
|
||||
std::string Gamepad::GetName() const {
|
||||
return m_hid->GetName();
|
||||
}
|
||||
|
||||
int Gamepad::GetPort() const {
|
||||
return m_hid->GetPort();
|
||||
}
|
||||
|
||||
void Gamepad::SetLeds(int r, int g, int b) {
|
||||
m_hid->SetLeds(r, g, b);
|
||||
}
|
||||
|
||||
void Gamepad::SetRumble(GenericHID::RumbleType type, double value) {
|
||||
m_hid->SetRumble(type, value);
|
||||
}
|
||||
|
||||
bool Gamepad::GetTouchpadFingerAvailable(int touchpad, int finger) const {
|
||||
return m_hid->GetTouchpadFingerAvailable(touchpad, finger);
|
||||
}
|
||||
|
||||
TouchpadFinger Gamepad::GetTouchpadFinger(int touchpad, int finger) const {
|
||||
return m_hid->GetTouchpadFinger(touchpad, finger);
|
||||
}
|
||||
|
||||
double Gamepad::GetAxisForSendable(Axis axis) const {
|
||||
return wpi::internal::DriverStationBackend::GetStickAxisIfAvailable(
|
||||
GetPort(), static_cast<int>(axis))
|
||||
m_hid->GetPort(), static_cast<int>(axis))
|
||||
.value_or(0.0);
|
||||
}
|
||||
|
||||
bool Gamepad::GetButtonForSendable(Button button) const {
|
||||
return wpi::internal::DriverStationBackend::GetStickButtonIfAvailable(
|
||||
GetPort(), static_cast<int>(button))
|
||||
m_hid->GetPort(), static_cast<int>(button))
|
||||
.value_or(false);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,19 +6,30 @@
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include "wpi/driverstation/DriverStation.hpp"
|
||||
#include "wpi/event/BooleanEvent.hpp"
|
||||
#include "wpi/hal/UsageReporting.hpp"
|
||||
|
||||
using namespace wpi;
|
||||
|
||||
Joystick::Joystick(int port) : GenericHID(port) {
|
||||
Joystick::Joystick(int port) : Joystick{DriverStation::GetGenericHID(port)} {}
|
||||
|
||||
Joystick::Joystick(GenericHID& hid) : m_hid{&hid} {
|
||||
m_axes[Axis::kX] = kDefaultXChannel;
|
||||
m_axes[Axis::kY] = kDefaultYChannel;
|
||||
m_axes[Axis::kZ] = kDefaultZChannel;
|
||||
m_axes[Axis::kTwist] = kDefaultTwistChannel;
|
||||
m_axes[Axis::kThrottle] = kDefaultThrottleChannel;
|
||||
|
||||
HAL_ReportUsage("HID", port, "Joystick");
|
||||
HAL_ReportUsage("HID", hid.GetPort(), "Joystick");
|
||||
}
|
||||
|
||||
GenericHID& Joystick::GetHID() {
|
||||
return *m_hid;
|
||||
}
|
||||
|
||||
const GenericHID& Joystick::GetHID() const {
|
||||
return *m_hid;
|
||||
}
|
||||
|
||||
void Joystick::SetXChannel(int channel) {
|
||||
@@ -61,36 +72,56 @@ int Joystick::GetThrottleChannel() const {
|
||||
return m_axes[Axis::kThrottle];
|
||||
}
|
||||
|
||||
bool Joystick::GetRawButton(int button) const {
|
||||
return m_hid->GetRawButton(button);
|
||||
}
|
||||
|
||||
bool Joystick::GetRawButtonPressed(int button) {
|
||||
return m_hid->GetRawButtonPressed(button);
|
||||
}
|
||||
|
||||
bool Joystick::GetRawButtonReleased(int button) {
|
||||
return m_hid->GetRawButtonReleased(button);
|
||||
}
|
||||
|
||||
double Joystick::GetRawAxis(int axis) const {
|
||||
return m_hid->GetRawAxis(axis);
|
||||
}
|
||||
|
||||
POVDirection Joystick::GetPOV(int pov) const {
|
||||
return m_hid->GetPOV(pov);
|
||||
}
|
||||
|
||||
double Joystick::GetX() const {
|
||||
return GetRawAxis(m_axes[Axis::kX]);
|
||||
return m_hid->GetRawAxis(m_axes[Axis::kX]);
|
||||
}
|
||||
|
||||
double Joystick::GetY() const {
|
||||
return GetRawAxis(m_axes[Axis::kY]);
|
||||
return m_hid->GetRawAxis(m_axes[Axis::kY]);
|
||||
}
|
||||
|
||||
double Joystick::GetZ() const {
|
||||
return GetRawAxis(m_axes[Axis::kZ]);
|
||||
return m_hid->GetRawAxis(m_axes[Axis::kZ]);
|
||||
}
|
||||
|
||||
double Joystick::GetTwist() const {
|
||||
return GetRawAxis(m_axes[Axis::kTwist]);
|
||||
return m_hid->GetRawAxis(m_axes[Axis::kTwist]);
|
||||
}
|
||||
|
||||
double Joystick::GetThrottle() const {
|
||||
return GetRawAxis(m_axes[Axis::kThrottle]);
|
||||
return m_hid->GetRawAxis(m_axes[Axis::kThrottle]);
|
||||
}
|
||||
|
||||
bool Joystick::GetTrigger() const {
|
||||
return GetRawButton(Button::kTrigger);
|
||||
return m_hid->GetRawButton(Button::kTrigger);
|
||||
}
|
||||
|
||||
bool Joystick::GetTriggerPressed() {
|
||||
return GetRawButtonPressed(Button::kTrigger);
|
||||
return m_hid->GetRawButtonPressed(Button::kTrigger);
|
||||
}
|
||||
|
||||
bool Joystick::GetTriggerReleased() {
|
||||
return GetRawButtonReleased(Button::kTrigger);
|
||||
return m_hid->GetRawButtonReleased(Button::kTrigger);
|
||||
}
|
||||
|
||||
BooleanEvent Joystick::Trigger(EventLoop* loop) const {
|
||||
@@ -98,15 +129,15 @@ BooleanEvent Joystick::Trigger(EventLoop* loop) const {
|
||||
}
|
||||
|
||||
bool Joystick::GetTop() const {
|
||||
return GetRawButton(Button::kTop);
|
||||
return m_hid->GetRawButton(Button::kTop);
|
||||
}
|
||||
|
||||
bool Joystick::GetTopPressed() {
|
||||
return GetRawButtonPressed(Button::kTop);
|
||||
return m_hid->GetRawButtonPressed(Button::kTop);
|
||||
}
|
||||
|
||||
bool Joystick::GetTopReleased() {
|
||||
return GetRawButtonReleased(Button::kTop);
|
||||
return m_hid->GetRawButtonReleased(Button::kTop);
|
||||
}
|
||||
|
||||
BooleanEvent Joystick::Top(EventLoop* loop) const {
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "wpi/datalog/DataLog.hpp"
|
||||
#include "wpi/driverstation/GenericHID.hpp"
|
||||
#include "wpi/hal/DriverStation.h"
|
||||
#include "wpi/hal/DriverStationTypes.h"
|
||||
#include "wpi/hal/HAL.h"
|
||||
@@ -47,6 +48,10 @@ static constexpr int availableToCount(uint64_t available) {
|
||||
return 64 - std::countl_zero(available);
|
||||
}
|
||||
|
||||
GenericHID DriverStationBackend::ConstructGenericHID(int port) {
|
||||
return GenericHID{port};
|
||||
}
|
||||
|
||||
namespace {
|
||||
// A simple class which caches the previous value written to an NT entry
|
||||
// Used to prevent redundant, repeated writes of the same value
|
||||
|
||||
@@ -9,7 +9,8 @@
|
||||
using namespace wpi;
|
||||
using namespace wpi::sim;
|
||||
|
||||
GamepadSim::GamepadSim(const wpi::Gamepad& joystick) : GenericHIDSim{joystick} {
|
||||
GamepadSim::GamepadSim(const wpi::Gamepad& joystick)
|
||||
: GenericHIDSim{joystick.GetHID()} {
|
||||
SetAxesMaximumIndex(6);
|
||||
SetButtonsMaximumIndex(26);
|
||||
SetPOVsMaximumIndex(1);
|
||||
|
||||
@@ -11,7 +11,7 @@ using namespace wpi;
|
||||
using namespace wpi::sim;
|
||||
|
||||
JoystickSim::JoystickSim(const Joystick& joystick)
|
||||
: GenericHIDSim{joystick}, m_joystick{&joystick} {
|
||||
: GenericHIDSim{joystick.GetHID()}, m_joystick{&joystick} {
|
||||
// default to a reasonable joystick configuration
|
||||
SetAxesMaximumIndex(5);
|
||||
SetButtonsMaximumIndex(12);
|
||||
|
||||
@@ -12,6 +12,9 @@ class DataLog;
|
||||
|
||||
namespace wpi {
|
||||
|
||||
class Gamepad;
|
||||
class GenericHID;
|
||||
|
||||
/**
|
||||
* Provides access to Driver Station functionality.
|
||||
*/
|
||||
@@ -19,6 +22,26 @@ class DriverStation final {
|
||||
public:
|
||||
DriverStation() = delete;
|
||||
|
||||
/**
|
||||
* Gets the GenericHID object for the given port. GenericHID objects are
|
||||
* cached, so this will always return the same object for the same port.
|
||||
*
|
||||
* @param port The port index on the Driver Station that the controller is
|
||||
* plugged into (0-5).
|
||||
* @return The GenericHID object for the given port.
|
||||
*/
|
||||
static GenericHID& GetGenericHID(int port);
|
||||
|
||||
/**
|
||||
* Gets the Gamepad object for the given port. Gamepad objects are cached, so
|
||||
* this will always return the same object for the same port.
|
||||
*
|
||||
* @param port The port index on the Driver Station that the controller is
|
||||
* plugged into (0-5).
|
||||
* @return The Gamepad object for the given port.
|
||||
*/
|
||||
static Gamepad& GetGamepad(int port);
|
||||
|
||||
/**
|
||||
* Starts logging DriverStation data to data log, including joystick data.
|
||||
* Repeated calls are ignored.
|
||||
@@ -57,6 +80,9 @@ class DriverStation final {
|
||||
static void RemoveRefreshedDataEventHandle(WPI_EventHandle handle) {
|
||||
wpi::internal::DriverStationBackend::RemoveRefreshedDataEventHandle(handle);
|
||||
}
|
||||
|
||||
private:
|
||||
static GenericHID& GetGenericHIDUnderLock(int port);
|
||||
};
|
||||
|
||||
} // namespace wpi
|
||||
|
||||
@@ -4,7 +4,10 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "wpi/driverstation/GenericHID.hpp"
|
||||
#include "wpi/driverstation/HIDDevice.hpp"
|
||||
#include "wpi/util/sendable/Sendable.hpp"
|
||||
#include "wpi/util/sendable/SendableHelper.hpp"
|
||||
|
||||
@@ -22,7 +25,7 @@ namespace wpi {
|
||||
* correct mapping, and only through the official NI DS. Sim is not guaranteed
|
||||
* to have the same mapping, as well as any 3rd party controllers.
|
||||
*/
|
||||
class Gamepad : public GenericHID,
|
||||
class Gamepad : public HIDDevice,
|
||||
public wpi::util::Sendable,
|
||||
public wpi::util::SendableHelper<Gamepad> {
|
||||
public:
|
||||
@@ -108,11 +111,32 @@ class Gamepad : public GenericHID,
|
||||
*/
|
||||
explicit Gamepad(int port);
|
||||
|
||||
/**
|
||||
* Construct an instance of a gamepad with a GenericHID object.
|
||||
*
|
||||
* @param hid The GenericHID object to use for this gamepad.
|
||||
*/
|
||||
explicit Gamepad(GenericHID& hid);
|
||||
|
||||
~Gamepad() override = default;
|
||||
|
||||
Gamepad(Gamepad&&) = default;
|
||||
Gamepad& operator=(Gamepad&&) = default;
|
||||
|
||||
/**
|
||||
* Get the underlying GenericHID object.
|
||||
*
|
||||
* @return the wrapped GenericHID object
|
||||
*/
|
||||
GenericHID& GetHID() override;
|
||||
|
||||
/**
|
||||
* Get the underlying GenericHID object.
|
||||
*
|
||||
* @return the wrapped GenericHID object
|
||||
*/
|
||||
const GenericHID& GetHID() const override;
|
||||
|
||||
/**
|
||||
* Get the X axis value of left side of the controller. Right is positive.
|
||||
*
|
||||
@@ -1157,6 +1181,92 @@ class Gamepad : public GenericHID,
|
||||
BooleanEvent AxisGreaterThan(Axis axis, double threshold,
|
||||
EventLoop* loop) const;
|
||||
|
||||
/**
|
||||
* Get the bitmask of axes for the gamepad.
|
||||
*
|
||||
* @return the number of axis for the current gamepad
|
||||
*/
|
||||
int GetAxesAvailable() const;
|
||||
|
||||
/**
|
||||
* For the current gamepad, return the bitmask of available buttons.
|
||||
*
|
||||
* @return the bitmask of buttons for the current gamepad
|
||||
*/
|
||||
uint64_t GetButtonsAvailable() const;
|
||||
|
||||
/**
|
||||
* Get if the gamepad is connected.
|
||||
*
|
||||
* @return true if the gamepad is connected
|
||||
*/
|
||||
bool IsConnected() const;
|
||||
|
||||
/**
|
||||
* Get the type of the gamepad.
|
||||
*
|
||||
* @return the type of the gamepad.
|
||||
*/
|
||||
GenericHID::HIDType GetGamepadType() const;
|
||||
|
||||
/**
|
||||
* Get the supported outputs for the gamepad.
|
||||
*
|
||||
* @return the supported outputs for the gamepad.
|
||||
*/
|
||||
GenericHID::SupportedOutputs GetSupportedOutputs() const;
|
||||
|
||||
/**
|
||||
* Get the name of the gamepad.
|
||||
*
|
||||
* @return the name of the gamepad.
|
||||
*/
|
||||
std::string GetName() const;
|
||||
|
||||
/**
|
||||
* Get the port number of the gamepad.
|
||||
*
|
||||
* @return The port number of the gamepad.
|
||||
*/
|
||||
int GetPort() const;
|
||||
|
||||
/**
|
||||
* Set leds on the gamepad. If only mono is supported, the system will use
|
||||
* the highest value passed in.
|
||||
*
|
||||
* @param r Red value from 0-255
|
||||
* @param g Green value from 0-255
|
||||
* @param b Blue value from 0-255
|
||||
*/
|
||||
void SetLeds(int r, int g, int b);
|
||||
|
||||
/**
|
||||
* Set the rumble output for the HID.
|
||||
*
|
||||
* The DS currently supports 4 rumble values: left rumble, right rumble, left
|
||||
* trigger rumble, and right trigger rumble.
|
||||
*
|
||||
* @param type Which rumble value to set
|
||||
* @param value The normalized value (0 to 1) to set the rumble to
|
||||
*/
|
||||
void SetRumble(GenericHID::RumbleType type, double value);
|
||||
|
||||
/**
|
||||
* Check if a touchpad finger is available.
|
||||
* @param touchpad The touchpad to check.
|
||||
* @param finger The finger to check.
|
||||
* @return true if the touchpad finger is available.
|
||||
*/
|
||||
bool GetTouchpadFingerAvailable(int touchpad, int finger) const;
|
||||
|
||||
/**
|
||||
* Get the touchpad finger data.
|
||||
* @param touchpad The touchpad to read.
|
||||
* @param finger The finger to read.
|
||||
* @return The touchpad finger data.
|
||||
*/
|
||||
TouchpadFinger GetTouchpadFinger(int touchpad, int finger) const;
|
||||
|
||||
void InitSendable(wpi::util::SendableBuilder& builder) override;
|
||||
|
||||
private:
|
||||
@@ -1169,6 +1279,7 @@ class Gamepad : public GenericHID,
|
||||
double m_rightYDeadband = 0.1;
|
||||
double m_leftTriggerDeadband = 0.01;
|
||||
double m_rightTriggerDeadband = 0.01;
|
||||
GenericHID* m_hid;
|
||||
};
|
||||
|
||||
} // namespace wpi
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "wpi/driverstation/HIDDevice.hpp"
|
||||
#include "wpi/driverstation/POVDirection.hpp"
|
||||
#include "wpi/driverstation/TouchpadFinger.hpp"
|
||||
#include "wpi/driverstation/internal/DriverStationBackend.hpp"
|
||||
@@ -16,7 +17,7 @@ namespace wpi {
|
||||
|
||||
class BooleanEvent;
|
||||
class EventLoop;
|
||||
|
||||
class DriverStation;
|
||||
/**
|
||||
* Handle input from standard HID devices connected to the Driver Station.
|
||||
*
|
||||
@@ -25,8 +26,11 @@ class EventLoop;
|
||||
* single class instance for each device and the mapping of ports to hardware
|
||||
* buttons depends on the code in the Driver Station.
|
||||
*/
|
||||
class GenericHID {
|
||||
class GenericHID final : public HIDDevice {
|
||||
public:
|
||||
friend class DriverStation;
|
||||
friend class internal::DriverStationBackend;
|
||||
|
||||
/**
|
||||
* Represents a rumble output on the Joystick.
|
||||
*/
|
||||
@@ -87,9 +91,24 @@ class GenericHID {
|
||||
SWITCH_JOYCON_PAIR
|
||||
};
|
||||
|
||||
explicit GenericHID(int port);
|
||||
virtual ~GenericHID() = default;
|
||||
~GenericHID() override = default;
|
||||
|
||||
/**
|
||||
* Get this GenericHID object.
|
||||
*
|
||||
* @return this GenericHID object
|
||||
*/
|
||||
GenericHID& GetHID() override { return *this; }
|
||||
|
||||
/**
|
||||
* Get this GenericHID object.
|
||||
*
|
||||
* @return this GenericHID object
|
||||
*/
|
||||
const GenericHID& GetHID() const override { return *this; }
|
||||
|
||||
GenericHID(const GenericHID&) = delete;
|
||||
GenericHID& operator=(const GenericHID&) = delete;
|
||||
GenericHID(GenericHID&&) = default;
|
||||
GenericHID& operator=(GenericHID&&) = default;
|
||||
|
||||
@@ -389,6 +408,7 @@ class GenericHID {
|
||||
TouchpadFinger GetTouchpadFinger(int touchpad, int finger) const;
|
||||
|
||||
private:
|
||||
explicit GenericHID(int port);
|
||||
int m_port;
|
||||
uint16_t m_leftRumble = 0;
|
||||
uint16_t m_rightRumble = 0;
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace wpi {
|
||||
|
||||
class GenericHID;
|
||||
|
||||
/** Interface for device wrappers backed by a GenericHID. */
|
||||
class HIDDevice {
|
||||
public:
|
||||
virtual ~HIDDevice() = default;
|
||||
|
||||
/**
|
||||
* Get the underlying GenericHID object.
|
||||
*
|
||||
* @return the wrapped GenericHID object
|
||||
*/
|
||||
virtual GenericHID& GetHID() = 0;
|
||||
|
||||
/**
|
||||
* Get the underlying GenericHID object.
|
||||
*
|
||||
* @return the wrapped GenericHID object
|
||||
*/
|
||||
virtual const GenericHID& GetHID() const = 0;
|
||||
};
|
||||
|
||||
} // namespace wpi
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <array>
|
||||
|
||||
#include "wpi/driverstation/GenericHID.hpp"
|
||||
#include "wpi/driverstation/HIDDevice.hpp"
|
||||
#include "wpi/units/angle.hpp"
|
||||
|
||||
namespace wpi {
|
||||
@@ -19,7 +20,7 @@ namespace wpi {
|
||||
* single class instance for each joystick and the mapping of ports to hardware
|
||||
* buttons depends on the code in the Driver Station.
|
||||
*/
|
||||
class Joystick : public GenericHID {
|
||||
class Joystick : public HIDDevice {
|
||||
public:
|
||||
/// Default X axis channel.
|
||||
static constexpr int kDefaultXChannel = 0;
|
||||
@@ -68,11 +69,32 @@ class Joystick : public GenericHID {
|
||||
*/
|
||||
explicit Joystick(int port);
|
||||
|
||||
/**
|
||||
* Construct an instance of a joystick with a GenericHID object.
|
||||
*
|
||||
* @param hid The GenericHID object to use for this joystick.
|
||||
*/
|
||||
explicit Joystick(GenericHID& hid);
|
||||
|
||||
~Joystick() override = default;
|
||||
|
||||
Joystick(Joystick&&) = default;
|
||||
Joystick& operator=(Joystick&&) = default;
|
||||
|
||||
/**
|
||||
* Get the underlying GenericHID object.
|
||||
*
|
||||
* @return the wrapped GenericHID object
|
||||
*/
|
||||
GenericHID& GetHID() override;
|
||||
|
||||
/**
|
||||
* Get the underlying GenericHID object.
|
||||
*
|
||||
* @return the wrapped GenericHID object
|
||||
*/
|
||||
const GenericHID& GetHID() const override;
|
||||
|
||||
/**
|
||||
* Set the channel associated with the X axis.
|
||||
*
|
||||
@@ -143,6 +165,55 @@ class Joystick : public GenericHID {
|
||||
*/
|
||||
int GetThrottleChannel() const;
|
||||
|
||||
/**
|
||||
* Get the button value (starting at button 1).
|
||||
*
|
||||
* The buttons are returned in a single 16 bit value with one bit representing
|
||||
* the state of each button. The appropriate button is returned as a boolean
|
||||
* value.
|
||||
*
|
||||
* @param button The button number to be read (starting at 1)
|
||||
* @return The state of the button
|
||||
*/
|
||||
bool GetRawButton(int button) const;
|
||||
|
||||
/**
|
||||
* Whether the button was pressed since the last check. Button indexes begin
|
||||
* at 1.
|
||||
*
|
||||
* @param button The button index, beginning at 1.
|
||||
* @return Whether the button was pressed since the last check.
|
||||
*/
|
||||
bool GetRawButtonPressed(int button);
|
||||
|
||||
/**
|
||||
* Whether the button was released since the last check. Button indexes begin
|
||||
* at 1.
|
||||
*
|
||||
* @param button The button index, beginning at 1.
|
||||
* @return Whether the button was released since the last check.
|
||||
*/
|
||||
bool GetRawButtonReleased(int button);
|
||||
|
||||
/**
|
||||
* Get the value of the axis.
|
||||
*
|
||||
* @param axis The axis to read, starting at 0.
|
||||
* @return The value of the axis.
|
||||
*/
|
||||
double GetRawAxis(int axis) const;
|
||||
|
||||
/**
|
||||
* Get the angle in degrees of a POV on the HID.
|
||||
*
|
||||
* The POV angles start at 0 in the up direction, and increase clockwise (e.g.
|
||||
* right is 90, upper-left is 315).
|
||||
*
|
||||
* @param pov The index of the POV to read, starting at 0.
|
||||
* @return the angle of the POV
|
||||
*/
|
||||
POVDirection GetPOV(int pov = 0) const;
|
||||
|
||||
/**
|
||||
* Get the X value of the current joystick.
|
||||
*
|
||||
@@ -266,6 +337,7 @@ class Joystick : public GenericHID {
|
||||
enum Button { kTrigger = 1, kTop = 2 };
|
||||
|
||||
std::array<int, Axis::kNumAxes> m_axes;
|
||||
GenericHID* m_hid;
|
||||
};
|
||||
|
||||
} // namespace wpi
|
||||
|
||||
@@ -24,6 +24,10 @@ namespace wpi::log {
|
||||
class DataLog;
|
||||
} // namespace wpi::log
|
||||
|
||||
namespace wpi {
|
||||
class GenericHID;
|
||||
} // namespace wpi
|
||||
|
||||
namespace wpi::util {
|
||||
class Color;
|
||||
} // namespace wpi::util
|
||||
@@ -41,6 +45,22 @@ class DriverStationBackend final {
|
||||
/// Number of Joystick ports.
|
||||
static constexpr int JOYSTICK_PORTS = 6;
|
||||
|
||||
/**
|
||||
* Constructs a GenericHID for the given port.
|
||||
*
|
||||
* @param port The port index on the Driver Station.
|
||||
* @return The GenericHID object for the given port.
|
||||
*/
|
||||
static GenericHID ConstructGenericHID(int port);
|
||||
|
||||
/**
|
||||
* Resets cached DriverStation HID wrapper objects.
|
||||
*
|
||||
* This is intended for test cleanup only. Any existing references to cached
|
||||
* GenericHID or Gamepad objects become invalid after this call.
|
||||
*/
|
||||
static void ResetCachedHIDData();
|
||||
|
||||
/**
|
||||
* The state of one joystick button. Button indexes begin at 0.
|
||||
*
|
||||
|
||||
@@ -119,6 +119,7 @@ RobotState = "wpi/driverstation/RobotState.hpp"
|
||||
TouchpadFinger = "wpi/driverstation/TouchpadFinger.hpp"
|
||||
Gamepad = "wpi/driverstation/Gamepad.hpp"
|
||||
GenericHID = "wpi/driverstation/GenericHID.hpp"
|
||||
HIDDevice = "wpi/driverstation/HIDDevice.hpp"
|
||||
Joystick = "wpi/driverstation/Joystick.hpp"
|
||||
NiDsPS4Controller = "wpi/driverstation/NiDsPS4Controller.hpp"
|
||||
NiDsPS5Controller = "wpi/driverstation/NiDsPS5Controller.hpp"
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
extra_includes:
|
||||
- wpi/datalog/DataLog.hpp
|
||||
- wpi/driverstation/Gamepad.hpp
|
||||
- wpi/driverstation/GenericHID.hpp
|
||||
|
||||
classes:
|
||||
wpi::DriverStation:
|
||||
methods:
|
||||
GetGenericHID:
|
||||
return_value_policy: reference_internal
|
||||
GetGamepad:
|
||||
return_value_policy: reference_internal
|
||||
ProvideRefreshedDataEventHandle:
|
||||
RemoveRefreshedDataEventHandle:
|
||||
StartDataLog:
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
extra_includes:
|
||||
- wpi/datalog/DataLog.hpp
|
||||
- wpi/driverstation/GenericHID.hpp
|
||||
- wpi/util/Color.hpp
|
||||
|
||||
classes:
|
||||
@@ -7,6 +8,8 @@ classes:
|
||||
attributes:
|
||||
JOYSTICK_PORTS:
|
||||
methods:
|
||||
ConstructGenericHID:
|
||||
ResetCachedHIDData:
|
||||
GetStickButton:
|
||||
GetStickButtonIfAvailable:
|
||||
GetStickButtonPressed:
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
extra_includes:
|
||||
- wpi/util/sendable/SendableBuilder.hpp
|
||||
- wpi/event/BooleanEvent.hpp
|
||||
|
||||
classes:
|
||||
wpi::Gamepad:
|
||||
force_no_trampoline: true
|
||||
ignored_bases:
|
||||
- wpi::util::SendableHelper<Gamepad>
|
||||
enums:
|
||||
@@ -7,6 +12,15 @@ classes:
|
||||
Axis:
|
||||
methods:
|
||||
Gamepad:
|
||||
overloads:
|
||||
int:
|
||||
GenericHID&:
|
||||
GetHID:
|
||||
overloads:
|
||||
"":
|
||||
return_value_policy: reference_internal
|
||||
'[const]':
|
||||
ignore: true
|
||||
GetLeftX:
|
||||
SetLeftXDeadband:
|
||||
GetLeftY:
|
||||
@@ -139,4 +153,15 @@ classes:
|
||||
GetAxis:
|
||||
AxisLessThan:
|
||||
AxisGreaterThan:
|
||||
GetAxesAvailable:
|
||||
GetButtonsAvailable:
|
||||
IsConnected:
|
||||
GetGamepadType:
|
||||
GetSupportedOutputs:
|
||||
GetName:
|
||||
GetPort:
|
||||
SetLeds:
|
||||
SetRumble:
|
||||
GetTouchpadFingerAvailable:
|
||||
GetTouchpadFinger:
|
||||
InitSendable:
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
extra_includes:
|
||||
- wpi/driverstation/internal/DriverStationBackend.hpp
|
||||
- wpi/event/BooleanEvent.hpp
|
||||
|
||||
classes:
|
||||
wpi::GenericHID:
|
||||
force_no_trampoline: true
|
||||
enums:
|
||||
RumbleType:
|
||||
SupportedOutputs:
|
||||
arithmetic: true
|
||||
HIDType:
|
||||
methods:
|
||||
GenericHID:
|
||||
GetRawButton:
|
||||
GetRawButtonPressed:
|
||||
GetRawButtonReleased:
|
||||
@@ -47,6 +46,12 @@ classes:
|
||||
GetButtonsAvailable:
|
||||
GetTouchpadFingerAvailable:
|
||||
GetTouchpadFinger:
|
||||
GetHID:
|
||||
overloads:
|
||||
"":
|
||||
return_value_policy: reference_internal
|
||||
'[const]':
|
||||
ignore: true
|
||||
inline_code: |
|
||||
cls_GenericHID
|
||||
.def("__repr__", [](py::handle self) {
|
||||
|
||||
14
wpilibc/src/main/python/semiwrap/HIDDevice.yml
Normal file
14
wpilibc/src/main/python/semiwrap/HIDDevice.yml
Normal file
@@ -0,0 +1,14 @@
|
||||
extra_includes:
|
||||
- wpi/driverstation/GenericHID.hpp
|
||||
|
||||
classes:
|
||||
wpi::HIDDevice:
|
||||
force_no_trampoline: true
|
||||
force_no_default_constructor: true
|
||||
methods:
|
||||
GetHID:
|
||||
overloads:
|
||||
"":
|
||||
return_value_policy: reference_internal
|
||||
'[const]':
|
||||
ignore: true
|
||||
@@ -1,8 +1,9 @@
|
||||
extra_includes:
|
||||
- wpi/driverstation/internal/DriverStationBackend.hpp
|
||||
- wpi/event/BooleanEvent.hpp
|
||||
|
||||
classes:
|
||||
wpi::Joystick:
|
||||
force_no_trampoline: true
|
||||
attributes:
|
||||
kDefaultXChannel:
|
||||
kDefaultYChannel:
|
||||
@@ -14,6 +15,15 @@ classes:
|
||||
ButtonType:
|
||||
methods:
|
||||
Joystick:
|
||||
overloads:
|
||||
int:
|
||||
GenericHID&:
|
||||
GetHID:
|
||||
overloads:
|
||||
"":
|
||||
return_value_policy: reference_internal
|
||||
'[const]':
|
||||
ignore: true
|
||||
SetXChannel:
|
||||
SetYChannel:
|
||||
SetZChannel:
|
||||
@@ -40,6 +50,11 @@ classes:
|
||||
GetMagnitude:
|
||||
GetDirection:
|
||||
rename: getDirectionRadians
|
||||
GetRawButton:
|
||||
GetRawButtonPressed:
|
||||
GetRawButtonReleased:
|
||||
GetRawAxis:
|
||||
GetPOV:
|
||||
inline_code: |
|
||||
.def("getDirectionDegrees", [](const Joystick &self) -> wpi::units::degree_t {
|
||||
return self.GetDirection();
|
||||
|
||||
@@ -4,10 +4,20 @@ extra_includes:
|
||||
|
||||
classes:
|
||||
wpi::NiDsPS4Controller:
|
||||
force_no_trampoline: true
|
||||
ignored_bases:
|
||||
- wpi::util::SendableHelper<NiDsPS4Controller>
|
||||
methods:
|
||||
NiDsPS4Controller:
|
||||
overloads:
|
||||
int:
|
||||
GenericHID&:
|
||||
GetHID:
|
||||
overloads:
|
||||
"":
|
||||
return_value_policy: reference_internal
|
||||
'[const]':
|
||||
ignore: true
|
||||
GetLeftX:
|
||||
GetRightX:
|
||||
GetLeftY:
|
||||
@@ -70,6 +80,12 @@ classes:
|
||||
GetTouchpadButton:
|
||||
GetTouchpadButtonPressed:
|
||||
GetTouchpadButtonReleased:
|
||||
IsConnected:
|
||||
GetGamepadType:
|
||||
GetSupportedOutputs:
|
||||
GetName:
|
||||
GetPort:
|
||||
SetRumble:
|
||||
InitSendable:
|
||||
wpi::NiDsPS4Controller::Button:
|
||||
attributes:
|
||||
|
||||
@@ -1,12 +1,23 @@
|
||||
extra_includes:
|
||||
- wpi/util/sendable/SendableBuilder.hpp
|
||||
- wpi/event/BooleanEvent.hpp
|
||||
|
||||
classes:
|
||||
wpi::NiDsPS5Controller:
|
||||
force_no_trampoline: true
|
||||
ignored_bases:
|
||||
- wpi::util::SendableHelper<NiDsPS5Controller>
|
||||
methods:
|
||||
NiDsPS5Controller:
|
||||
overloads:
|
||||
int:
|
||||
GenericHID&:
|
||||
GetHID:
|
||||
overloads:
|
||||
"":
|
||||
return_value_policy: reference_internal
|
||||
'[const]':
|
||||
ignore: true
|
||||
GetLeftX:
|
||||
GetRightX:
|
||||
GetLeftY:
|
||||
@@ -69,6 +80,12 @@ classes:
|
||||
GetTouchpadButton:
|
||||
GetTouchpadButtonPressed:
|
||||
GetTouchpadButtonReleased:
|
||||
IsConnected:
|
||||
GetGamepadType:
|
||||
GetSupportedOutputs:
|
||||
GetName:
|
||||
GetPort:
|
||||
SetRumble:
|
||||
InitSendable:
|
||||
wpi::NiDsPS5Controller::Button:
|
||||
attributes:
|
||||
|
||||
@@ -1,12 +1,23 @@
|
||||
extra_includes:
|
||||
- wpi/util/sendable/SendableBuilder.hpp
|
||||
- wpi/event/BooleanEvent.hpp
|
||||
|
||||
classes:
|
||||
wpi::NiDsStadiaController:
|
||||
force_no_trampoline: true
|
||||
ignored_bases:
|
||||
- wpi::util::SendableHelper<NiDsStadiaController>
|
||||
methods:
|
||||
NiDsStadiaController:
|
||||
overloads:
|
||||
int:
|
||||
GenericHID&:
|
||||
GetHID:
|
||||
overloads:
|
||||
"":
|
||||
return_value_policy: reference_internal
|
||||
'[const]':
|
||||
ignore: true
|
||||
GetLeftX:
|
||||
GetRightX:
|
||||
GetLeftY:
|
||||
@@ -71,6 +82,12 @@ classes:
|
||||
GetRightBumperButton:
|
||||
GetRightBumperButtonPressed:
|
||||
GetRightBumperButtonReleased:
|
||||
IsConnected:
|
||||
GetGamepadType:
|
||||
GetSupportedOutputs:
|
||||
GetName:
|
||||
GetPort:
|
||||
SetRumble:
|
||||
InitSendable:
|
||||
wpi::NiDsStadiaController::Button:
|
||||
attributes:
|
||||
|
||||
@@ -5,10 +5,20 @@ extra_includes:
|
||||
|
||||
classes:
|
||||
wpi::NiDsXboxController:
|
||||
force_no_trampoline: true
|
||||
ignored_bases:
|
||||
- wpi::util::SendableHelper<NiDsXboxController>
|
||||
methods:
|
||||
NiDsXboxController:
|
||||
overloads:
|
||||
int:
|
||||
GenericHID&:
|
||||
GetHID:
|
||||
overloads:
|
||||
"":
|
||||
return_value_policy: reference_internal
|
||||
'[const]':
|
||||
ignore: true
|
||||
GetLeftX:
|
||||
GetRightX:
|
||||
GetLeftY:
|
||||
@@ -63,6 +73,12 @@ classes:
|
||||
GetRightBumperButton:
|
||||
GetRightBumperButtonPressed:
|
||||
GetRightBumperButtonReleased:
|
||||
IsConnected:
|
||||
GetGamepadType:
|
||||
GetSupportedOutputs:
|
||||
GetName:
|
||||
GetPort:
|
||||
SetRumble:
|
||||
InitSendable:
|
||||
wpi::NiDsXboxController::Button:
|
||||
attributes:
|
||||
|
||||
@@ -16,8 +16,10 @@ import wpilib.simulation
|
||||
# TODO: get rid of special-casing.. maybe should register a HAL shutdown hook or something
|
||||
try:
|
||||
import commands2
|
||||
from commands2.button.commandgenerichid import _resetCommandGenericHIDData
|
||||
except ImportError:
|
||||
commands2 = None
|
||||
_resetCommandGenericHIDData = None
|
||||
|
||||
from .controller import RobotTestController
|
||||
|
||||
@@ -98,10 +100,15 @@ class RobotTestingPlugin:
|
||||
|
||||
if commands2 is not None:
|
||||
commands2.CommandScheduler.resetInstance()
|
||||
_resetCommandGenericHIDData()
|
||||
|
||||
# Double-check all objects are destroyed so that HAL handles are released
|
||||
gc.collect()
|
||||
|
||||
# Reset DriverStation cached HID wrapper objects after user and command
|
||||
# references have had a chance to be released.
|
||||
wpilib.DriverStationBackend.resetCachedHIDData()
|
||||
|
||||
# shutdown networktables before other kinds of global cleanup
|
||||
# -> some reset functions will re-register listeners, so it's important
|
||||
# to do this before so that the listeners are active on the current
|
||||
|
||||
Reference in New Issue
Block a user