[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:
Thad House
2026-06-11 09:42:39 -07:00
committed by GitHub
parent fe499ede4c
commit c647e67de0
105 changed files with 4210 additions and 1336 deletions

View File

@@ -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