[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

@@ -8,28 +8,42 @@
{%- endmacro %}
#include "wpi/driverstation/{{ ConsoleName }}Controller.hpp"
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/event/BooleanEvent.hpp"
#include "wpi/hal/UsageReporting.hpp"
#include "wpi/util/sendable/SendableBuilder.hpp"
#include "wpi/event/BooleanEvent.hpp"
using namespace wpi;
{{ ConsoleName }}Controller::{{ ConsoleName }}Controller(int port) : GenericHID(port) {
HAL_ReportUsage("HID", port, "{{ ConsoleName }}Controller");
{{ ConsoleName }}Controller::{{ ConsoleName }}Controller(int port)
: {{ ConsoleName }}Controller{DriverStation::GetGenericHID(port)} {}
{{ ConsoleName }}Controller::{{ ConsoleName }}Controller(GenericHID& hid)
: m_hid{&hid} {
HAL_ReportUsage("HID", hid.GetPort(), "{{ ConsoleName }}Controller");
}
GenericHID& {{ ConsoleName }}Controller::GetHID() {
return *m_hid;
}
const GenericHID& {{ ConsoleName }}Controller::GetHID() const {
return *m_hid;
}
{% for stick in sticks %}
double {{ ConsoleName }}Controller::Get{{ stick.NameParts|map("capitalize")|join }}() const {
return GetRawAxis(Axis::k{{ stick.NameParts|map("capitalize")|join }});
return m_hid->GetRawAxis(Axis::k{{ stick.NameParts|map("capitalize")|join }});
}
{% endfor -%}
{% for trigger in triggers %}
double {{ ConsoleName }}Controller::Get{{ capitalize_first(trigger.name) }}Axis() const {
return GetRawAxis(Axis::k{{ capitalize_first(trigger.name) }});
return m_hid->GetRawAxis(Axis::k{{ capitalize_first(trigger.name) }});
}
{% if trigger.UseThresholdMethods %}
BooleanEvent {{ ConsoleName }}Controller::{{ capitalize_first(trigger.name) }}(double threshold, EventLoop* loop) const {
return BooleanEvent(loop, [this, threshold] { return this->Get{{ capitalize_first(trigger.name) }}Axis() > threshold; });
BooleanEvent {{ ConsoleName }}Controller::{{ capitalize_first(trigger.name) }}(double threshold,
EventLoop* loop) const {
return BooleanEvent(
loop, [this, threshold] { return this->Get{{ capitalize_first(trigger.name) }}Axis() > threshold; });
}
BooleanEvent {{ ConsoleName }}Controller::{{ capitalize_first(trigger.name) }}(EventLoop* loop) const {
@@ -39,31 +53,59 @@ BooleanEvent {{ ConsoleName }}Controller::{{ capitalize_first(trigger.name) }}(E
{% endfor -%}
{% for button in buttons %}
bool {{ ConsoleName }}Controller::Get{{ capitalize_first(button.name) }}Button() const {
return GetRawButton(Button::k{{ capitalize_first(button.name) }});
return m_hid->GetRawButton(Button::k{{ capitalize_first(button.name) }});
}
bool {{ ConsoleName }}Controller::Get{{ capitalize_first(button.name) }}ButtonPressed() {
return GetRawButtonPressed(Button::k{{ capitalize_first(button.name) }});
return m_hid->GetRawButtonPressed(Button::k{{ capitalize_first(button.name) }});
}
bool {{ ConsoleName }}Controller::Get{{ capitalize_first(button.name) }}ButtonReleased() {
return GetRawButtonReleased(Button::k{{ capitalize_first(button.name) }});
return m_hid->GetRawButtonReleased(Button::k{{ capitalize_first(button.name) }});
}
BooleanEvent {{ ConsoleName }}Controller::{{ capitalize_first(button.name) }}(EventLoop* loop) const {
return BooleanEvent(loop, [this]() { return this->Get{{ capitalize_first(button.name) }}Button(); });
}
{% endfor %}
bool {{ ConsoleName }}Controller::IsConnected() const {
return m_hid->IsConnected();
}
GenericHID::HIDType {{ ConsoleName }}Controller::GetGamepadType() const {
return m_hid->GetGamepadType();
}
GenericHID::SupportedOutputs {{ ConsoleName }}Controller::GetSupportedOutputs() const {
return m_hid->GetSupportedOutputs();
}
std::string {{ ConsoleName }}Controller::GetName() const {
return m_hid->GetName();
}
int {{ ConsoleName }}Controller::GetPort() const {
return m_hid->GetPort();
}
void {{ ConsoleName }}Controller::SetRumble(GenericHID::RumbleType type,
double value) {
m_hid->SetRumble(type, value);
}
void {{ ConsoleName }}Controller::InitSendable(wpi::util::SendableBuilder& builder) {
builder.SetSmartDashboardType("HID");
builder.PublishConstString("ControllerType", "{{ ConsoleName }}");
{%- for trigger in triggers %}
builder.AddDoubleProperty("{{ capitalize_first(trigger.name) }} Axis", [this] { return Get{{ capitalize_first(trigger.name) }}Axis(); }, nullptr);
builder.AddDoubleProperty("{{ capitalize_first(trigger.name) }} Axis",
[this] { return Get{{ capitalize_first(trigger.name) }}Axis(); }, nullptr);
{%- endfor -%}
{% for stick in sticks %}
builder.AddDoubleProperty("{{ stick.NameParts|map("capitalize")|join }}", [this] { return Get{{ stick.NameParts|map("capitalize")|join }}(); }, nullptr);
builder.AddDoubleProperty("{{ stick.NameParts|map("capitalize")|join }}",
[this] { return Get{{ stick.NameParts|map("capitalize")|join }}(); }, nullptr);
{%- endfor -%}
{% for button in buttons %}
builder.AddBooleanProperty("{{ capitalize_first(button.name) }}", [this] { return Get{{ capitalize_first(button.name) }}Button(); }, nullptr);
builder.AddBooleanProperty("{{ capitalize_first(button.name) }}",
[this] { return Get{{ capitalize_first(button.name) }}Button(); }, nullptr);
{%- endfor %}
}

View File

@@ -14,7 +14,7 @@ using namespace wpi;
using namespace wpi::sim;
{{ ConsoleName }}ControllerSim::{{ ConsoleName }}ControllerSim(const {{ ConsoleName }}Controller& joystick)
: GenericHIDSim{joystick} {
: GenericHIDSim{joystick.GetHID()} {
SetAxesMaximumIndex({{ sticks|length + triggers|length }});
SetButtonsMaximumIndex({{ buttons|length }});
SetPOVsMaximumIndex(1);

View File

@@ -8,10 +8,12 @@
{%- endmacro %}
#pragma once
#include "wpi/util/sendable/Sendable.hpp"
#include "wpi/util/sendable/SendableHelper.hpp"
#include <string>
#include "wpi/driverstation/GenericHID.hpp"
#include "wpi/driverstation/HIDDevice.hpp"
#include "wpi/util/sendable/Sendable.hpp"
#include "wpi/util/sendable/SendableHelper.hpp"
namespace wpi {
@@ -27,9 +29,10 @@ 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 {{ ConsoleName }}Controller : public GenericHID,
public wpi::util::Sendable,
public wpi::util::SendableHelper<{{ ConsoleName }}Controller> {
class {{ ConsoleName }}Controller
: public HIDDevice,
public wpi::util::Sendable,
public wpi::util::SendableHelper<{{ ConsoleName }}Controller> {
public:
/**
* Construct an instance of a controller.
@@ -41,10 +44,31 @@ class {{ ConsoleName }}Controller : public GenericHID,
*/
explicit {{ ConsoleName }}Controller(int port);
/**
* Construct an instance of a controller with a GenericHID object.
*
* @param hid The GenericHID object to use for this controller.
*/
explicit {{ ConsoleName }}Controller(GenericHID& hid);
~{{ ConsoleName }}Controller() override = default;
{{ ConsoleName }}Controller({{ ConsoleName }}Controller&&) = default;
{{ ConsoleName }}Controller& operator=({{ ConsoleName }}Controller&&) = 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;
{% for stick in sticks %}
/**
* Get the {{ stick.NameParts[1] }} axis value of {{ stick.NameParts[0] }} side of the controller. {{ stick.PositiveDirection }} is positive.
@@ -137,7 +161,56 @@ class {{ ConsoleName }}Controller : public GenericHID,
{%- endfor %}
};
/**
* Get if the controller is connected.
*
* @return true if the controller is connected
*/
bool IsConnected() const;
/**
* Get the type of the controller.
*
* @return the type of the controller.
*/
GenericHID::HIDType GetGamepadType() const;
/**
* Get the supported outputs of the controller.
*
* @return the supported outputs of the controller.
*/
GenericHID::SupportedOutputs GetSupportedOutputs() const;
/**
* Get the name of the controller.
*
* @return the name of the controller.
*/
std::string GetName() const;
/**
* Get the port number of the controller.
*
* @return The port number of the controller.
*/
int GetPort() const;
/**
* 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);
void InitSendable(wpi::util::SendableBuilder& builder) override;
private:
GenericHID* m_hid;
};
} // namespace wpi

View File

@@ -6,51 +6,63 @@
#include "wpi/driverstation/NiDsPS4Controller.hpp"
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/event/BooleanEvent.hpp"
#include "wpi/hal/UsageReporting.hpp"
#include "wpi/util/sendable/SendableBuilder.hpp"
#include "wpi/event/BooleanEvent.hpp"
using namespace wpi;
NiDsPS4Controller::NiDsPS4Controller(int port) : GenericHID(port) {
HAL_ReportUsage("HID", port, "NiDsPS4Controller");
NiDsPS4Controller::NiDsPS4Controller(int port)
: NiDsPS4Controller{DriverStation::GetGenericHID(port)} {}
NiDsPS4Controller::NiDsPS4Controller(GenericHID& hid)
: m_hid{&hid} {
HAL_ReportUsage("HID", hid.GetPort(), "NiDsPS4Controller");
}
GenericHID& NiDsPS4Controller::GetHID() {
return *m_hid;
}
const GenericHID& NiDsPS4Controller::GetHID() const {
return *m_hid;
}
double NiDsPS4Controller::GetLeftX() const {
return GetRawAxis(Axis::kLeftX);
return m_hid->GetRawAxis(Axis::kLeftX);
}
double NiDsPS4Controller::GetLeftY() const {
return GetRawAxis(Axis::kLeftY);
return m_hid->GetRawAxis(Axis::kLeftY);
}
double NiDsPS4Controller::GetRightX() const {
return GetRawAxis(Axis::kRightX);
return m_hid->GetRawAxis(Axis::kRightX);
}
double NiDsPS4Controller::GetRightY() const {
return GetRawAxis(Axis::kRightY);
return m_hid->GetRawAxis(Axis::kRightY);
}
double NiDsPS4Controller::GetL2Axis() const {
return GetRawAxis(Axis::kL2);
return m_hid->GetRawAxis(Axis::kL2);
}
double NiDsPS4Controller::GetR2Axis() const {
return GetRawAxis(Axis::kR2);
return m_hid->GetRawAxis(Axis::kR2);
}
bool NiDsPS4Controller::GetSquareButton() const {
return GetRawButton(Button::kSquare);
return m_hid->GetRawButton(Button::kSquare);
}
bool NiDsPS4Controller::GetSquareButtonPressed() {
return GetRawButtonPressed(Button::kSquare);
return m_hid->GetRawButtonPressed(Button::kSquare);
}
bool NiDsPS4Controller::GetSquareButtonReleased() {
return GetRawButtonReleased(Button::kSquare);
return m_hid->GetRawButtonReleased(Button::kSquare);
}
BooleanEvent NiDsPS4Controller::Square(EventLoop* loop) const {
@@ -58,15 +70,15 @@ BooleanEvent NiDsPS4Controller::Square(EventLoop* loop) const {
}
bool NiDsPS4Controller::GetCrossButton() const {
return GetRawButton(Button::kCross);
return m_hid->GetRawButton(Button::kCross);
}
bool NiDsPS4Controller::GetCrossButtonPressed() {
return GetRawButtonPressed(Button::kCross);
return m_hid->GetRawButtonPressed(Button::kCross);
}
bool NiDsPS4Controller::GetCrossButtonReleased() {
return GetRawButtonReleased(Button::kCross);
return m_hid->GetRawButtonReleased(Button::kCross);
}
BooleanEvent NiDsPS4Controller::Cross(EventLoop* loop) const {
@@ -74,15 +86,15 @@ BooleanEvent NiDsPS4Controller::Cross(EventLoop* loop) const {
}
bool NiDsPS4Controller::GetCircleButton() const {
return GetRawButton(Button::kCircle);
return m_hid->GetRawButton(Button::kCircle);
}
bool NiDsPS4Controller::GetCircleButtonPressed() {
return GetRawButtonPressed(Button::kCircle);
return m_hid->GetRawButtonPressed(Button::kCircle);
}
bool NiDsPS4Controller::GetCircleButtonReleased() {
return GetRawButtonReleased(Button::kCircle);
return m_hid->GetRawButtonReleased(Button::kCircle);
}
BooleanEvent NiDsPS4Controller::Circle(EventLoop* loop) const {
@@ -90,15 +102,15 @@ BooleanEvent NiDsPS4Controller::Circle(EventLoop* loop) const {
}
bool NiDsPS4Controller::GetTriangleButton() const {
return GetRawButton(Button::kTriangle);
return m_hid->GetRawButton(Button::kTriangle);
}
bool NiDsPS4Controller::GetTriangleButtonPressed() {
return GetRawButtonPressed(Button::kTriangle);
return m_hid->GetRawButtonPressed(Button::kTriangle);
}
bool NiDsPS4Controller::GetTriangleButtonReleased() {
return GetRawButtonReleased(Button::kTriangle);
return m_hid->GetRawButtonReleased(Button::kTriangle);
}
BooleanEvent NiDsPS4Controller::Triangle(EventLoop* loop) const {
@@ -106,15 +118,15 @@ BooleanEvent NiDsPS4Controller::Triangle(EventLoop* loop) const {
}
bool NiDsPS4Controller::GetL1Button() const {
return GetRawButton(Button::kL1);
return m_hid->GetRawButton(Button::kL1);
}
bool NiDsPS4Controller::GetL1ButtonPressed() {
return GetRawButtonPressed(Button::kL1);
return m_hid->GetRawButtonPressed(Button::kL1);
}
bool NiDsPS4Controller::GetL1ButtonReleased() {
return GetRawButtonReleased(Button::kL1);
return m_hid->GetRawButtonReleased(Button::kL1);
}
BooleanEvent NiDsPS4Controller::L1(EventLoop* loop) const {
@@ -122,15 +134,15 @@ BooleanEvent NiDsPS4Controller::L1(EventLoop* loop) const {
}
bool NiDsPS4Controller::GetR1Button() const {
return GetRawButton(Button::kR1);
return m_hid->GetRawButton(Button::kR1);
}
bool NiDsPS4Controller::GetR1ButtonPressed() {
return GetRawButtonPressed(Button::kR1);
return m_hid->GetRawButtonPressed(Button::kR1);
}
bool NiDsPS4Controller::GetR1ButtonReleased() {
return GetRawButtonReleased(Button::kR1);
return m_hid->GetRawButtonReleased(Button::kR1);
}
BooleanEvent NiDsPS4Controller::R1(EventLoop* loop) const {
@@ -138,15 +150,15 @@ BooleanEvent NiDsPS4Controller::R1(EventLoop* loop) const {
}
bool NiDsPS4Controller::GetL2Button() const {
return GetRawButton(Button::kL2);
return m_hid->GetRawButton(Button::kL2);
}
bool NiDsPS4Controller::GetL2ButtonPressed() {
return GetRawButtonPressed(Button::kL2);
return m_hid->GetRawButtonPressed(Button::kL2);
}
bool NiDsPS4Controller::GetL2ButtonReleased() {
return GetRawButtonReleased(Button::kL2);
return m_hid->GetRawButtonReleased(Button::kL2);
}
BooleanEvent NiDsPS4Controller::L2(EventLoop* loop) const {
@@ -154,15 +166,15 @@ BooleanEvent NiDsPS4Controller::L2(EventLoop* loop) const {
}
bool NiDsPS4Controller::GetR2Button() const {
return GetRawButton(Button::kR2);
return m_hid->GetRawButton(Button::kR2);
}
bool NiDsPS4Controller::GetR2ButtonPressed() {
return GetRawButtonPressed(Button::kR2);
return m_hid->GetRawButtonPressed(Button::kR2);
}
bool NiDsPS4Controller::GetR2ButtonReleased() {
return GetRawButtonReleased(Button::kR2);
return m_hid->GetRawButtonReleased(Button::kR2);
}
BooleanEvent NiDsPS4Controller::R2(EventLoop* loop) const {
@@ -170,15 +182,15 @@ BooleanEvent NiDsPS4Controller::R2(EventLoop* loop) const {
}
bool NiDsPS4Controller::GetShareButton() const {
return GetRawButton(Button::kShare);
return m_hid->GetRawButton(Button::kShare);
}
bool NiDsPS4Controller::GetShareButtonPressed() {
return GetRawButtonPressed(Button::kShare);
return m_hid->GetRawButtonPressed(Button::kShare);
}
bool NiDsPS4Controller::GetShareButtonReleased() {
return GetRawButtonReleased(Button::kShare);
return m_hid->GetRawButtonReleased(Button::kShare);
}
BooleanEvent NiDsPS4Controller::Share(EventLoop* loop) const {
@@ -186,15 +198,15 @@ BooleanEvent NiDsPS4Controller::Share(EventLoop* loop) const {
}
bool NiDsPS4Controller::GetOptionsButton() const {
return GetRawButton(Button::kOptions);
return m_hid->GetRawButton(Button::kOptions);
}
bool NiDsPS4Controller::GetOptionsButtonPressed() {
return GetRawButtonPressed(Button::kOptions);
return m_hid->GetRawButtonPressed(Button::kOptions);
}
bool NiDsPS4Controller::GetOptionsButtonReleased() {
return GetRawButtonReleased(Button::kOptions);
return m_hid->GetRawButtonReleased(Button::kOptions);
}
BooleanEvent NiDsPS4Controller::Options(EventLoop* loop) const {
@@ -202,15 +214,15 @@ BooleanEvent NiDsPS4Controller::Options(EventLoop* loop) const {
}
bool NiDsPS4Controller::GetL3Button() const {
return GetRawButton(Button::kL3);
return m_hid->GetRawButton(Button::kL3);
}
bool NiDsPS4Controller::GetL3ButtonPressed() {
return GetRawButtonPressed(Button::kL3);
return m_hid->GetRawButtonPressed(Button::kL3);
}
bool NiDsPS4Controller::GetL3ButtonReleased() {
return GetRawButtonReleased(Button::kL3);
return m_hid->GetRawButtonReleased(Button::kL3);
}
BooleanEvent NiDsPS4Controller::L3(EventLoop* loop) const {
@@ -218,15 +230,15 @@ BooleanEvent NiDsPS4Controller::L3(EventLoop* loop) const {
}
bool NiDsPS4Controller::GetR3Button() const {
return GetRawButton(Button::kR3);
return m_hid->GetRawButton(Button::kR3);
}
bool NiDsPS4Controller::GetR3ButtonPressed() {
return GetRawButtonPressed(Button::kR3);
return m_hid->GetRawButtonPressed(Button::kR3);
}
bool NiDsPS4Controller::GetR3ButtonReleased() {
return GetRawButtonReleased(Button::kR3);
return m_hid->GetRawButtonReleased(Button::kR3);
}
BooleanEvent NiDsPS4Controller::R3(EventLoop* loop) const {
@@ -234,15 +246,15 @@ BooleanEvent NiDsPS4Controller::R3(EventLoop* loop) const {
}
bool NiDsPS4Controller::GetPSButton() const {
return GetRawButton(Button::kPS);
return m_hid->GetRawButton(Button::kPS);
}
bool NiDsPS4Controller::GetPSButtonPressed() {
return GetRawButtonPressed(Button::kPS);
return m_hid->GetRawButtonPressed(Button::kPS);
}
bool NiDsPS4Controller::GetPSButtonReleased() {
return GetRawButtonReleased(Button::kPS);
return m_hid->GetRawButtonReleased(Button::kPS);
}
BooleanEvent NiDsPS4Controller::PS(EventLoop* loop) const {
@@ -250,42 +262,87 @@ BooleanEvent NiDsPS4Controller::PS(EventLoop* loop) const {
}
bool NiDsPS4Controller::GetTouchpadButton() const {
return GetRawButton(Button::kTouchpad);
return m_hid->GetRawButton(Button::kTouchpad);
}
bool NiDsPS4Controller::GetTouchpadButtonPressed() {
return GetRawButtonPressed(Button::kTouchpad);
return m_hid->GetRawButtonPressed(Button::kTouchpad);
}
bool NiDsPS4Controller::GetTouchpadButtonReleased() {
return GetRawButtonReleased(Button::kTouchpad);
return m_hid->GetRawButtonReleased(Button::kTouchpad);
}
BooleanEvent NiDsPS4Controller::Touchpad(EventLoop* loop) const {
return BooleanEvent(loop, [this]() { return this->GetTouchpadButton(); });
}
bool NiDsPS4Controller::IsConnected() const {
return m_hid->IsConnected();
}
GenericHID::HIDType NiDsPS4Controller::GetGamepadType() const {
return m_hid->GetGamepadType();
}
GenericHID::SupportedOutputs NiDsPS4Controller::GetSupportedOutputs() const {
return m_hid->GetSupportedOutputs();
}
std::string NiDsPS4Controller::GetName() const {
return m_hid->GetName();
}
int NiDsPS4Controller::GetPort() const {
return m_hid->GetPort();
}
void NiDsPS4Controller::SetRumble(GenericHID::RumbleType type,
double value) {
m_hid->SetRumble(type, value);
}
void NiDsPS4Controller::InitSendable(wpi::util::SendableBuilder& builder) {
builder.SetSmartDashboardType("HID");
builder.PublishConstString("ControllerType", "NiDsPS4");
builder.AddDoubleProperty("L2 Axis", [this] { return GetL2Axis(); }, nullptr);
builder.AddDoubleProperty("R2 Axis", [this] { return GetR2Axis(); }, nullptr);
builder.AddDoubleProperty("LeftX", [this] { return GetLeftX(); }, nullptr);
builder.AddDoubleProperty("LeftY", [this] { return GetLeftY(); }, nullptr);
builder.AddDoubleProperty("RightX", [this] { return GetRightX(); }, nullptr);
builder.AddDoubleProperty("RightY", [this] { return GetRightY(); }, nullptr);
builder.AddBooleanProperty("Square", [this] { return GetSquareButton(); }, nullptr);
builder.AddBooleanProperty("Cross", [this] { return GetCrossButton(); }, nullptr);
builder.AddBooleanProperty("Circle", [this] { return GetCircleButton(); }, nullptr);
builder.AddBooleanProperty("Triangle", [this] { return GetTriangleButton(); }, nullptr);
builder.AddBooleanProperty("L1", [this] { return GetL1Button(); }, nullptr);
builder.AddBooleanProperty("R1", [this] { return GetR1Button(); }, nullptr);
builder.AddBooleanProperty("L2", [this] { return GetL2Button(); }, nullptr);
builder.AddBooleanProperty("R2", [this] { return GetR2Button(); }, nullptr);
builder.AddBooleanProperty("Share", [this] { return GetShareButton(); }, nullptr);
builder.AddBooleanProperty("Options", [this] { return GetOptionsButton(); }, nullptr);
builder.AddBooleanProperty("L3", [this] { return GetL3Button(); }, nullptr);
builder.AddBooleanProperty("R3", [this] { return GetR3Button(); }, nullptr);
builder.AddBooleanProperty("PS", [this] { return GetPSButton(); }, nullptr);
builder.AddBooleanProperty("Touchpad", [this] { return GetTouchpadButton(); }, nullptr);
builder.AddDoubleProperty("L2 Axis",
[this] { return GetL2Axis(); }, nullptr);
builder.AddDoubleProperty("R2 Axis",
[this] { return GetR2Axis(); }, nullptr);
builder.AddDoubleProperty("LeftX",
[this] { return GetLeftX(); }, nullptr);
builder.AddDoubleProperty("LeftY",
[this] { return GetLeftY(); }, nullptr);
builder.AddDoubleProperty("RightX",
[this] { return GetRightX(); }, nullptr);
builder.AddDoubleProperty("RightY",
[this] { return GetRightY(); }, nullptr);
builder.AddBooleanProperty("Square",
[this] { return GetSquareButton(); }, nullptr);
builder.AddBooleanProperty("Cross",
[this] { return GetCrossButton(); }, nullptr);
builder.AddBooleanProperty("Circle",
[this] { return GetCircleButton(); }, nullptr);
builder.AddBooleanProperty("Triangle",
[this] { return GetTriangleButton(); }, nullptr);
builder.AddBooleanProperty("L1",
[this] { return GetL1Button(); }, nullptr);
builder.AddBooleanProperty("R1",
[this] { return GetR1Button(); }, nullptr);
builder.AddBooleanProperty("L2",
[this] { return GetL2Button(); }, nullptr);
builder.AddBooleanProperty("R2",
[this] { return GetR2Button(); }, nullptr);
builder.AddBooleanProperty("Share",
[this] { return GetShareButton(); }, nullptr);
builder.AddBooleanProperty("Options",
[this] { return GetOptionsButton(); }, nullptr);
builder.AddBooleanProperty("L3",
[this] { return GetL3Button(); }, nullptr);
builder.AddBooleanProperty("R3",
[this] { return GetR3Button(); }, nullptr);
builder.AddBooleanProperty("PS",
[this] { return GetPSButton(); }, nullptr);
builder.AddBooleanProperty("Touchpad",
[this] { return GetTouchpadButton(); }, nullptr);
}

View File

@@ -6,51 +6,63 @@
#include "wpi/driverstation/NiDsPS5Controller.hpp"
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/event/BooleanEvent.hpp"
#include "wpi/hal/UsageReporting.hpp"
#include "wpi/util/sendable/SendableBuilder.hpp"
#include "wpi/event/BooleanEvent.hpp"
using namespace wpi;
NiDsPS5Controller::NiDsPS5Controller(int port) : GenericHID(port) {
HAL_ReportUsage("HID", port, "NiDsPS5Controller");
NiDsPS5Controller::NiDsPS5Controller(int port)
: NiDsPS5Controller{DriverStation::GetGenericHID(port)} {}
NiDsPS5Controller::NiDsPS5Controller(GenericHID& hid)
: m_hid{&hid} {
HAL_ReportUsage("HID", hid.GetPort(), "NiDsPS5Controller");
}
GenericHID& NiDsPS5Controller::GetHID() {
return *m_hid;
}
const GenericHID& NiDsPS5Controller::GetHID() const {
return *m_hid;
}
double NiDsPS5Controller::GetLeftX() const {
return GetRawAxis(Axis::kLeftX);
return m_hid->GetRawAxis(Axis::kLeftX);
}
double NiDsPS5Controller::GetLeftY() const {
return GetRawAxis(Axis::kLeftY);
return m_hid->GetRawAxis(Axis::kLeftY);
}
double NiDsPS5Controller::GetRightX() const {
return GetRawAxis(Axis::kRightX);
return m_hid->GetRawAxis(Axis::kRightX);
}
double NiDsPS5Controller::GetRightY() const {
return GetRawAxis(Axis::kRightY);
return m_hid->GetRawAxis(Axis::kRightY);
}
double NiDsPS5Controller::GetL2Axis() const {
return GetRawAxis(Axis::kL2);
return m_hid->GetRawAxis(Axis::kL2);
}
double NiDsPS5Controller::GetR2Axis() const {
return GetRawAxis(Axis::kR2);
return m_hid->GetRawAxis(Axis::kR2);
}
bool NiDsPS5Controller::GetSquareButton() const {
return GetRawButton(Button::kSquare);
return m_hid->GetRawButton(Button::kSquare);
}
bool NiDsPS5Controller::GetSquareButtonPressed() {
return GetRawButtonPressed(Button::kSquare);
return m_hid->GetRawButtonPressed(Button::kSquare);
}
bool NiDsPS5Controller::GetSquareButtonReleased() {
return GetRawButtonReleased(Button::kSquare);
return m_hid->GetRawButtonReleased(Button::kSquare);
}
BooleanEvent NiDsPS5Controller::Square(EventLoop* loop) const {
@@ -58,15 +70,15 @@ BooleanEvent NiDsPS5Controller::Square(EventLoop* loop) const {
}
bool NiDsPS5Controller::GetCrossButton() const {
return GetRawButton(Button::kCross);
return m_hid->GetRawButton(Button::kCross);
}
bool NiDsPS5Controller::GetCrossButtonPressed() {
return GetRawButtonPressed(Button::kCross);
return m_hid->GetRawButtonPressed(Button::kCross);
}
bool NiDsPS5Controller::GetCrossButtonReleased() {
return GetRawButtonReleased(Button::kCross);
return m_hid->GetRawButtonReleased(Button::kCross);
}
BooleanEvent NiDsPS5Controller::Cross(EventLoop* loop) const {
@@ -74,15 +86,15 @@ BooleanEvent NiDsPS5Controller::Cross(EventLoop* loop) const {
}
bool NiDsPS5Controller::GetCircleButton() const {
return GetRawButton(Button::kCircle);
return m_hid->GetRawButton(Button::kCircle);
}
bool NiDsPS5Controller::GetCircleButtonPressed() {
return GetRawButtonPressed(Button::kCircle);
return m_hid->GetRawButtonPressed(Button::kCircle);
}
bool NiDsPS5Controller::GetCircleButtonReleased() {
return GetRawButtonReleased(Button::kCircle);
return m_hid->GetRawButtonReleased(Button::kCircle);
}
BooleanEvent NiDsPS5Controller::Circle(EventLoop* loop) const {
@@ -90,15 +102,15 @@ BooleanEvent NiDsPS5Controller::Circle(EventLoop* loop) const {
}
bool NiDsPS5Controller::GetTriangleButton() const {
return GetRawButton(Button::kTriangle);
return m_hid->GetRawButton(Button::kTriangle);
}
bool NiDsPS5Controller::GetTriangleButtonPressed() {
return GetRawButtonPressed(Button::kTriangle);
return m_hid->GetRawButtonPressed(Button::kTriangle);
}
bool NiDsPS5Controller::GetTriangleButtonReleased() {
return GetRawButtonReleased(Button::kTriangle);
return m_hid->GetRawButtonReleased(Button::kTriangle);
}
BooleanEvent NiDsPS5Controller::Triangle(EventLoop* loop) const {
@@ -106,15 +118,15 @@ BooleanEvent NiDsPS5Controller::Triangle(EventLoop* loop) const {
}
bool NiDsPS5Controller::GetL1Button() const {
return GetRawButton(Button::kL1);
return m_hid->GetRawButton(Button::kL1);
}
bool NiDsPS5Controller::GetL1ButtonPressed() {
return GetRawButtonPressed(Button::kL1);
return m_hid->GetRawButtonPressed(Button::kL1);
}
bool NiDsPS5Controller::GetL1ButtonReleased() {
return GetRawButtonReleased(Button::kL1);
return m_hid->GetRawButtonReleased(Button::kL1);
}
BooleanEvent NiDsPS5Controller::L1(EventLoop* loop) const {
@@ -122,15 +134,15 @@ BooleanEvent NiDsPS5Controller::L1(EventLoop* loop) const {
}
bool NiDsPS5Controller::GetR1Button() const {
return GetRawButton(Button::kR1);
return m_hid->GetRawButton(Button::kR1);
}
bool NiDsPS5Controller::GetR1ButtonPressed() {
return GetRawButtonPressed(Button::kR1);
return m_hid->GetRawButtonPressed(Button::kR1);
}
bool NiDsPS5Controller::GetR1ButtonReleased() {
return GetRawButtonReleased(Button::kR1);
return m_hid->GetRawButtonReleased(Button::kR1);
}
BooleanEvent NiDsPS5Controller::R1(EventLoop* loop) const {
@@ -138,15 +150,15 @@ BooleanEvent NiDsPS5Controller::R1(EventLoop* loop) const {
}
bool NiDsPS5Controller::GetL2Button() const {
return GetRawButton(Button::kL2);
return m_hid->GetRawButton(Button::kL2);
}
bool NiDsPS5Controller::GetL2ButtonPressed() {
return GetRawButtonPressed(Button::kL2);
return m_hid->GetRawButtonPressed(Button::kL2);
}
bool NiDsPS5Controller::GetL2ButtonReleased() {
return GetRawButtonReleased(Button::kL2);
return m_hid->GetRawButtonReleased(Button::kL2);
}
BooleanEvent NiDsPS5Controller::L2(EventLoop* loop) const {
@@ -154,15 +166,15 @@ BooleanEvent NiDsPS5Controller::L2(EventLoop* loop) const {
}
bool NiDsPS5Controller::GetR2Button() const {
return GetRawButton(Button::kR2);
return m_hid->GetRawButton(Button::kR2);
}
bool NiDsPS5Controller::GetR2ButtonPressed() {
return GetRawButtonPressed(Button::kR2);
return m_hid->GetRawButtonPressed(Button::kR2);
}
bool NiDsPS5Controller::GetR2ButtonReleased() {
return GetRawButtonReleased(Button::kR2);
return m_hid->GetRawButtonReleased(Button::kR2);
}
BooleanEvent NiDsPS5Controller::R2(EventLoop* loop) const {
@@ -170,15 +182,15 @@ BooleanEvent NiDsPS5Controller::R2(EventLoop* loop) const {
}
bool NiDsPS5Controller::GetCreateButton() const {
return GetRawButton(Button::kCreate);
return m_hid->GetRawButton(Button::kCreate);
}
bool NiDsPS5Controller::GetCreateButtonPressed() {
return GetRawButtonPressed(Button::kCreate);
return m_hid->GetRawButtonPressed(Button::kCreate);
}
bool NiDsPS5Controller::GetCreateButtonReleased() {
return GetRawButtonReleased(Button::kCreate);
return m_hid->GetRawButtonReleased(Button::kCreate);
}
BooleanEvent NiDsPS5Controller::Create(EventLoop* loop) const {
@@ -186,15 +198,15 @@ BooleanEvent NiDsPS5Controller::Create(EventLoop* loop) const {
}
bool NiDsPS5Controller::GetOptionsButton() const {
return GetRawButton(Button::kOptions);
return m_hid->GetRawButton(Button::kOptions);
}
bool NiDsPS5Controller::GetOptionsButtonPressed() {
return GetRawButtonPressed(Button::kOptions);
return m_hid->GetRawButtonPressed(Button::kOptions);
}
bool NiDsPS5Controller::GetOptionsButtonReleased() {
return GetRawButtonReleased(Button::kOptions);
return m_hid->GetRawButtonReleased(Button::kOptions);
}
BooleanEvent NiDsPS5Controller::Options(EventLoop* loop) const {
@@ -202,15 +214,15 @@ BooleanEvent NiDsPS5Controller::Options(EventLoop* loop) const {
}
bool NiDsPS5Controller::GetL3Button() const {
return GetRawButton(Button::kL3);
return m_hid->GetRawButton(Button::kL3);
}
bool NiDsPS5Controller::GetL3ButtonPressed() {
return GetRawButtonPressed(Button::kL3);
return m_hid->GetRawButtonPressed(Button::kL3);
}
bool NiDsPS5Controller::GetL3ButtonReleased() {
return GetRawButtonReleased(Button::kL3);
return m_hid->GetRawButtonReleased(Button::kL3);
}
BooleanEvent NiDsPS5Controller::L3(EventLoop* loop) const {
@@ -218,15 +230,15 @@ BooleanEvent NiDsPS5Controller::L3(EventLoop* loop) const {
}
bool NiDsPS5Controller::GetR3Button() const {
return GetRawButton(Button::kR3);
return m_hid->GetRawButton(Button::kR3);
}
bool NiDsPS5Controller::GetR3ButtonPressed() {
return GetRawButtonPressed(Button::kR3);
return m_hid->GetRawButtonPressed(Button::kR3);
}
bool NiDsPS5Controller::GetR3ButtonReleased() {
return GetRawButtonReleased(Button::kR3);
return m_hid->GetRawButtonReleased(Button::kR3);
}
BooleanEvent NiDsPS5Controller::R3(EventLoop* loop) const {
@@ -234,15 +246,15 @@ BooleanEvent NiDsPS5Controller::R3(EventLoop* loop) const {
}
bool NiDsPS5Controller::GetPSButton() const {
return GetRawButton(Button::kPS);
return m_hid->GetRawButton(Button::kPS);
}
bool NiDsPS5Controller::GetPSButtonPressed() {
return GetRawButtonPressed(Button::kPS);
return m_hid->GetRawButtonPressed(Button::kPS);
}
bool NiDsPS5Controller::GetPSButtonReleased() {
return GetRawButtonReleased(Button::kPS);
return m_hid->GetRawButtonReleased(Button::kPS);
}
BooleanEvent NiDsPS5Controller::PS(EventLoop* loop) const {
@@ -250,42 +262,87 @@ BooleanEvent NiDsPS5Controller::PS(EventLoop* loop) const {
}
bool NiDsPS5Controller::GetTouchpadButton() const {
return GetRawButton(Button::kTouchpad);
return m_hid->GetRawButton(Button::kTouchpad);
}
bool NiDsPS5Controller::GetTouchpadButtonPressed() {
return GetRawButtonPressed(Button::kTouchpad);
return m_hid->GetRawButtonPressed(Button::kTouchpad);
}
bool NiDsPS5Controller::GetTouchpadButtonReleased() {
return GetRawButtonReleased(Button::kTouchpad);
return m_hid->GetRawButtonReleased(Button::kTouchpad);
}
BooleanEvent NiDsPS5Controller::Touchpad(EventLoop* loop) const {
return BooleanEvent(loop, [this]() { return this->GetTouchpadButton(); });
}
bool NiDsPS5Controller::IsConnected() const {
return m_hid->IsConnected();
}
GenericHID::HIDType NiDsPS5Controller::GetGamepadType() const {
return m_hid->GetGamepadType();
}
GenericHID::SupportedOutputs NiDsPS5Controller::GetSupportedOutputs() const {
return m_hid->GetSupportedOutputs();
}
std::string NiDsPS5Controller::GetName() const {
return m_hid->GetName();
}
int NiDsPS5Controller::GetPort() const {
return m_hid->GetPort();
}
void NiDsPS5Controller::SetRumble(GenericHID::RumbleType type,
double value) {
m_hid->SetRumble(type, value);
}
void NiDsPS5Controller::InitSendable(wpi::util::SendableBuilder& builder) {
builder.SetSmartDashboardType("HID");
builder.PublishConstString("ControllerType", "NiDsPS5");
builder.AddDoubleProperty("L2 Axis", [this] { return GetL2Axis(); }, nullptr);
builder.AddDoubleProperty("R2 Axis", [this] { return GetR2Axis(); }, nullptr);
builder.AddDoubleProperty("LeftX", [this] { return GetLeftX(); }, nullptr);
builder.AddDoubleProperty("LeftY", [this] { return GetLeftY(); }, nullptr);
builder.AddDoubleProperty("RightX", [this] { return GetRightX(); }, nullptr);
builder.AddDoubleProperty("RightY", [this] { return GetRightY(); }, nullptr);
builder.AddBooleanProperty("Square", [this] { return GetSquareButton(); }, nullptr);
builder.AddBooleanProperty("Cross", [this] { return GetCrossButton(); }, nullptr);
builder.AddBooleanProperty("Circle", [this] { return GetCircleButton(); }, nullptr);
builder.AddBooleanProperty("Triangle", [this] { return GetTriangleButton(); }, nullptr);
builder.AddBooleanProperty("L1", [this] { return GetL1Button(); }, nullptr);
builder.AddBooleanProperty("R1", [this] { return GetR1Button(); }, nullptr);
builder.AddBooleanProperty("L2", [this] { return GetL2Button(); }, nullptr);
builder.AddBooleanProperty("R2", [this] { return GetR2Button(); }, nullptr);
builder.AddBooleanProperty("Create", [this] { return GetCreateButton(); }, nullptr);
builder.AddBooleanProperty("Options", [this] { return GetOptionsButton(); }, nullptr);
builder.AddBooleanProperty("L3", [this] { return GetL3Button(); }, nullptr);
builder.AddBooleanProperty("R3", [this] { return GetR3Button(); }, nullptr);
builder.AddBooleanProperty("PS", [this] { return GetPSButton(); }, nullptr);
builder.AddBooleanProperty("Touchpad", [this] { return GetTouchpadButton(); }, nullptr);
builder.AddDoubleProperty("L2 Axis",
[this] { return GetL2Axis(); }, nullptr);
builder.AddDoubleProperty("R2 Axis",
[this] { return GetR2Axis(); }, nullptr);
builder.AddDoubleProperty("LeftX",
[this] { return GetLeftX(); }, nullptr);
builder.AddDoubleProperty("LeftY",
[this] { return GetLeftY(); }, nullptr);
builder.AddDoubleProperty("RightX",
[this] { return GetRightX(); }, nullptr);
builder.AddDoubleProperty("RightY",
[this] { return GetRightY(); }, nullptr);
builder.AddBooleanProperty("Square",
[this] { return GetSquareButton(); }, nullptr);
builder.AddBooleanProperty("Cross",
[this] { return GetCrossButton(); }, nullptr);
builder.AddBooleanProperty("Circle",
[this] { return GetCircleButton(); }, nullptr);
builder.AddBooleanProperty("Triangle",
[this] { return GetTriangleButton(); }, nullptr);
builder.AddBooleanProperty("L1",
[this] { return GetL1Button(); }, nullptr);
builder.AddBooleanProperty("R1",
[this] { return GetR1Button(); }, nullptr);
builder.AddBooleanProperty("L2",
[this] { return GetL2Button(); }, nullptr);
builder.AddBooleanProperty("R2",
[this] { return GetR2Button(); }, nullptr);
builder.AddBooleanProperty("Create",
[this] { return GetCreateButton(); }, nullptr);
builder.AddBooleanProperty("Options",
[this] { return GetOptionsButton(); }, nullptr);
builder.AddBooleanProperty("L3",
[this] { return GetL3Button(); }, nullptr);
builder.AddBooleanProperty("R3",
[this] { return GetR3Button(); }, nullptr);
builder.AddBooleanProperty("PS",
[this] { return GetPSButton(); }, nullptr);
builder.AddBooleanProperty("Touchpad",
[this] { return GetTouchpadButton(); }, nullptr);
}

View File

@@ -6,43 +6,55 @@
#include "wpi/driverstation/NiDsStadiaController.hpp"
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/event/BooleanEvent.hpp"
#include "wpi/hal/UsageReporting.hpp"
#include "wpi/util/sendable/SendableBuilder.hpp"
#include "wpi/event/BooleanEvent.hpp"
using namespace wpi;
NiDsStadiaController::NiDsStadiaController(int port) : GenericHID(port) {
HAL_ReportUsage("HID", port, "NiDsStadiaController");
NiDsStadiaController::NiDsStadiaController(int port)
: NiDsStadiaController{DriverStation::GetGenericHID(port)} {}
NiDsStadiaController::NiDsStadiaController(GenericHID& hid)
: m_hid{&hid} {
HAL_ReportUsage("HID", hid.GetPort(), "NiDsStadiaController");
}
GenericHID& NiDsStadiaController::GetHID() {
return *m_hid;
}
const GenericHID& NiDsStadiaController::GetHID() const {
return *m_hid;
}
double NiDsStadiaController::GetLeftX() const {
return GetRawAxis(Axis::kLeftX);
return m_hid->GetRawAxis(Axis::kLeftX);
}
double NiDsStadiaController::GetRightX() const {
return GetRawAxis(Axis::kRightX);
return m_hid->GetRawAxis(Axis::kRightX);
}
double NiDsStadiaController::GetLeftY() const {
return GetRawAxis(Axis::kLeftY);
return m_hid->GetRawAxis(Axis::kLeftY);
}
double NiDsStadiaController::GetRightY() const {
return GetRawAxis(Axis::kRightY);
return m_hid->GetRawAxis(Axis::kRightY);
}
bool NiDsStadiaController::GetAButton() const {
return GetRawButton(Button::kA);
return m_hid->GetRawButton(Button::kA);
}
bool NiDsStadiaController::GetAButtonPressed() {
return GetRawButtonPressed(Button::kA);
return m_hid->GetRawButtonPressed(Button::kA);
}
bool NiDsStadiaController::GetAButtonReleased() {
return GetRawButtonReleased(Button::kA);
return m_hid->GetRawButtonReleased(Button::kA);
}
BooleanEvent NiDsStadiaController::A(EventLoop* loop) const {
@@ -50,15 +62,15 @@ BooleanEvent NiDsStadiaController::A(EventLoop* loop) const {
}
bool NiDsStadiaController::GetBButton() const {
return GetRawButton(Button::kB);
return m_hid->GetRawButton(Button::kB);
}
bool NiDsStadiaController::GetBButtonPressed() {
return GetRawButtonPressed(Button::kB);
return m_hid->GetRawButtonPressed(Button::kB);
}
bool NiDsStadiaController::GetBButtonReleased() {
return GetRawButtonReleased(Button::kB);
return m_hid->GetRawButtonReleased(Button::kB);
}
BooleanEvent NiDsStadiaController::B(EventLoop* loop) const {
@@ -66,15 +78,15 @@ BooleanEvent NiDsStadiaController::B(EventLoop* loop) const {
}
bool NiDsStadiaController::GetXButton() const {
return GetRawButton(Button::kX);
return m_hid->GetRawButton(Button::kX);
}
bool NiDsStadiaController::GetXButtonPressed() {
return GetRawButtonPressed(Button::kX);
return m_hid->GetRawButtonPressed(Button::kX);
}
bool NiDsStadiaController::GetXButtonReleased() {
return GetRawButtonReleased(Button::kX);
return m_hid->GetRawButtonReleased(Button::kX);
}
BooleanEvent NiDsStadiaController::X(EventLoop* loop) const {
@@ -82,15 +94,15 @@ BooleanEvent NiDsStadiaController::X(EventLoop* loop) const {
}
bool NiDsStadiaController::GetYButton() const {
return GetRawButton(Button::kY);
return m_hid->GetRawButton(Button::kY);
}
bool NiDsStadiaController::GetYButtonPressed() {
return GetRawButtonPressed(Button::kY);
return m_hid->GetRawButtonPressed(Button::kY);
}
bool NiDsStadiaController::GetYButtonReleased() {
return GetRawButtonReleased(Button::kY);
return m_hid->GetRawButtonReleased(Button::kY);
}
BooleanEvent NiDsStadiaController::Y(EventLoop* loop) const {
@@ -98,15 +110,15 @@ BooleanEvent NiDsStadiaController::Y(EventLoop* loop) const {
}
bool NiDsStadiaController::GetLeftBumperButton() const {
return GetRawButton(Button::kLeftBumper);
return m_hid->GetRawButton(Button::kLeftBumper);
}
bool NiDsStadiaController::GetLeftBumperButtonPressed() {
return GetRawButtonPressed(Button::kLeftBumper);
return m_hid->GetRawButtonPressed(Button::kLeftBumper);
}
bool NiDsStadiaController::GetLeftBumperButtonReleased() {
return GetRawButtonReleased(Button::kLeftBumper);
return m_hid->GetRawButtonReleased(Button::kLeftBumper);
}
BooleanEvent NiDsStadiaController::LeftBumper(EventLoop* loop) const {
@@ -114,15 +126,15 @@ BooleanEvent NiDsStadiaController::LeftBumper(EventLoop* loop) const {
}
bool NiDsStadiaController::GetRightBumperButton() const {
return GetRawButton(Button::kRightBumper);
return m_hid->GetRawButton(Button::kRightBumper);
}
bool NiDsStadiaController::GetRightBumperButtonPressed() {
return GetRawButtonPressed(Button::kRightBumper);
return m_hid->GetRawButtonPressed(Button::kRightBumper);
}
bool NiDsStadiaController::GetRightBumperButtonReleased() {
return GetRawButtonReleased(Button::kRightBumper);
return m_hid->GetRawButtonReleased(Button::kRightBumper);
}
BooleanEvent NiDsStadiaController::RightBumper(EventLoop* loop) const {
@@ -130,15 +142,15 @@ BooleanEvent NiDsStadiaController::RightBumper(EventLoop* loop) const {
}
bool NiDsStadiaController::GetLeftStickButton() const {
return GetRawButton(Button::kLeftStick);
return m_hid->GetRawButton(Button::kLeftStick);
}
bool NiDsStadiaController::GetLeftStickButtonPressed() {
return GetRawButtonPressed(Button::kLeftStick);
return m_hid->GetRawButtonPressed(Button::kLeftStick);
}
bool NiDsStadiaController::GetLeftStickButtonReleased() {
return GetRawButtonReleased(Button::kLeftStick);
return m_hid->GetRawButtonReleased(Button::kLeftStick);
}
BooleanEvent NiDsStadiaController::LeftStick(EventLoop* loop) const {
@@ -146,15 +158,15 @@ BooleanEvent NiDsStadiaController::LeftStick(EventLoop* loop) const {
}
bool NiDsStadiaController::GetRightStickButton() const {
return GetRawButton(Button::kRightStick);
return m_hid->GetRawButton(Button::kRightStick);
}
bool NiDsStadiaController::GetRightStickButtonPressed() {
return GetRawButtonPressed(Button::kRightStick);
return m_hid->GetRawButtonPressed(Button::kRightStick);
}
bool NiDsStadiaController::GetRightStickButtonReleased() {
return GetRawButtonReleased(Button::kRightStick);
return m_hid->GetRawButtonReleased(Button::kRightStick);
}
BooleanEvent NiDsStadiaController::RightStick(EventLoop* loop) const {
@@ -162,15 +174,15 @@ BooleanEvent NiDsStadiaController::RightStick(EventLoop* loop) const {
}
bool NiDsStadiaController::GetEllipsesButton() const {
return GetRawButton(Button::kEllipses);
return m_hid->GetRawButton(Button::kEllipses);
}
bool NiDsStadiaController::GetEllipsesButtonPressed() {
return GetRawButtonPressed(Button::kEllipses);
return m_hid->GetRawButtonPressed(Button::kEllipses);
}
bool NiDsStadiaController::GetEllipsesButtonReleased() {
return GetRawButtonReleased(Button::kEllipses);
return m_hid->GetRawButtonReleased(Button::kEllipses);
}
BooleanEvent NiDsStadiaController::Ellipses(EventLoop* loop) const {
@@ -178,15 +190,15 @@ BooleanEvent NiDsStadiaController::Ellipses(EventLoop* loop) const {
}
bool NiDsStadiaController::GetHamburgerButton() const {
return GetRawButton(Button::kHamburger);
return m_hid->GetRawButton(Button::kHamburger);
}
bool NiDsStadiaController::GetHamburgerButtonPressed() {
return GetRawButtonPressed(Button::kHamburger);
return m_hid->GetRawButtonPressed(Button::kHamburger);
}
bool NiDsStadiaController::GetHamburgerButtonReleased() {
return GetRawButtonReleased(Button::kHamburger);
return m_hid->GetRawButtonReleased(Button::kHamburger);
}
BooleanEvent NiDsStadiaController::Hamburger(EventLoop* loop) const {
@@ -194,15 +206,15 @@ BooleanEvent NiDsStadiaController::Hamburger(EventLoop* loop) const {
}
bool NiDsStadiaController::GetStadiaButton() const {
return GetRawButton(Button::kStadia);
return m_hid->GetRawButton(Button::kStadia);
}
bool NiDsStadiaController::GetStadiaButtonPressed() {
return GetRawButtonPressed(Button::kStadia);
return m_hid->GetRawButtonPressed(Button::kStadia);
}
bool NiDsStadiaController::GetStadiaButtonReleased() {
return GetRawButtonReleased(Button::kStadia);
return m_hid->GetRawButtonReleased(Button::kStadia);
}
BooleanEvent NiDsStadiaController::Stadia(EventLoop* loop) const {
@@ -210,15 +222,15 @@ BooleanEvent NiDsStadiaController::Stadia(EventLoop* loop) const {
}
bool NiDsStadiaController::GetRightTriggerButton() const {
return GetRawButton(Button::kRightTrigger);
return m_hid->GetRawButton(Button::kRightTrigger);
}
bool NiDsStadiaController::GetRightTriggerButtonPressed() {
return GetRawButtonPressed(Button::kRightTrigger);
return m_hid->GetRawButtonPressed(Button::kRightTrigger);
}
bool NiDsStadiaController::GetRightTriggerButtonReleased() {
return GetRawButtonReleased(Button::kRightTrigger);
return m_hid->GetRawButtonReleased(Button::kRightTrigger);
}
BooleanEvent NiDsStadiaController::RightTrigger(EventLoop* loop) const {
@@ -226,15 +238,15 @@ BooleanEvent NiDsStadiaController::RightTrigger(EventLoop* loop) const {
}
bool NiDsStadiaController::GetLeftTriggerButton() const {
return GetRawButton(Button::kLeftTrigger);
return m_hid->GetRawButton(Button::kLeftTrigger);
}
bool NiDsStadiaController::GetLeftTriggerButtonPressed() {
return GetRawButtonPressed(Button::kLeftTrigger);
return m_hid->GetRawButtonPressed(Button::kLeftTrigger);
}
bool NiDsStadiaController::GetLeftTriggerButtonReleased() {
return GetRawButtonReleased(Button::kLeftTrigger);
return m_hid->GetRawButtonReleased(Button::kLeftTrigger);
}
BooleanEvent NiDsStadiaController::LeftTrigger(EventLoop* loop) const {
@@ -242,15 +254,15 @@ BooleanEvent NiDsStadiaController::LeftTrigger(EventLoop* loop) const {
}
bool NiDsStadiaController::GetGoogleButton() const {
return GetRawButton(Button::kGoogle);
return m_hid->GetRawButton(Button::kGoogle);
}
bool NiDsStadiaController::GetGoogleButtonPressed() {
return GetRawButtonPressed(Button::kGoogle);
return m_hid->GetRawButtonPressed(Button::kGoogle);
}
bool NiDsStadiaController::GetGoogleButtonReleased() {
return GetRawButtonReleased(Button::kGoogle);
return m_hid->GetRawButtonReleased(Button::kGoogle);
}
BooleanEvent NiDsStadiaController::Google(EventLoop* loop) const {
@@ -258,41 +270,85 @@ BooleanEvent NiDsStadiaController::Google(EventLoop* loop) const {
}
bool NiDsStadiaController::GetFrameButton() const {
return GetRawButton(Button::kFrame);
return m_hid->GetRawButton(Button::kFrame);
}
bool NiDsStadiaController::GetFrameButtonPressed() {
return GetRawButtonPressed(Button::kFrame);
return m_hid->GetRawButtonPressed(Button::kFrame);
}
bool NiDsStadiaController::GetFrameButtonReleased() {
return GetRawButtonReleased(Button::kFrame);
return m_hid->GetRawButtonReleased(Button::kFrame);
}
BooleanEvent NiDsStadiaController::Frame(EventLoop* loop) const {
return BooleanEvent(loop, [this]() { return this->GetFrameButton(); });
}
bool NiDsStadiaController::IsConnected() const {
return m_hid->IsConnected();
}
GenericHID::HIDType NiDsStadiaController::GetGamepadType() const {
return m_hid->GetGamepadType();
}
GenericHID::SupportedOutputs NiDsStadiaController::GetSupportedOutputs() const {
return m_hid->GetSupportedOutputs();
}
std::string NiDsStadiaController::GetName() const {
return m_hid->GetName();
}
int NiDsStadiaController::GetPort() const {
return m_hid->GetPort();
}
void NiDsStadiaController::SetRumble(GenericHID::RumbleType type,
double value) {
m_hid->SetRumble(type, value);
}
void NiDsStadiaController::InitSendable(wpi::util::SendableBuilder& builder) {
builder.SetSmartDashboardType("HID");
builder.PublishConstString("ControllerType", "NiDsStadia");
builder.AddDoubleProperty("LeftX", [this] { return GetLeftX(); }, nullptr);
builder.AddDoubleProperty("RightX", [this] { return GetRightX(); }, nullptr);
builder.AddDoubleProperty("LeftY", [this] { return GetLeftY(); }, nullptr);
builder.AddDoubleProperty("RightY", [this] { return GetRightY(); }, nullptr);
builder.AddBooleanProperty("A", [this] { return GetAButton(); }, nullptr);
builder.AddBooleanProperty("B", [this] { return GetBButton(); }, nullptr);
builder.AddBooleanProperty("X", [this] { return GetXButton(); }, nullptr);
builder.AddBooleanProperty("Y", [this] { return GetYButton(); }, nullptr);
builder.AddBooleanProperty("LeftBumper", [this] { return GetLeftBumperButton(); }, nullptr);
builder.AddBooleanProperty("RightBumper", [this] { return GetRightBumperButton(); }, nullptr);
builder.AddBooleanProperty("LeftStick", [this] { return GetLeftStickButton(); }, nullptr);
builder.AddBooleanProperty("RightStick", [this] { return GetRightStickButton(); }, nullptr);
builder.AddBooleanProperty("Ellipses", [this] { return GetEllipsesButton(); }, nullptr);
builder.AddBooleanProperty("Hamburger", [this] { return GetHamburgerButton(); }, nullptr);
builder.AddBooleanProperty("Stadia", [this] { return GetStadiaButton(); }, nullptr);
builder.AddBooleanProperty("RightTrigger", [this] { return GetRightTriggerButton(); }, nullptr);
builder.AddBooleanProperty("LeftTrigger", [this] { return GetLeftTriggerButton(); }, nullptr);
builder.AddBooleanProperty("Google", [this] { return GetGoogleButton(); }, nullptr);
builder.AddBooleanProperty("Frame", [this] { return GetFrameButton(); }, nullptr);
builder.AddDoubleProperty("LeftX",
[this] { return GetLeftX(); }, nullptr);
builder.AddDoubleProperty("RightX",
[this] { return GetRightX(); }, nullptr);
builder.AddDoubleProperty("LeftY",
[this] { return GetLeftY(); }, nullptr);
builder.AddDoubleProperty("RightY",
[this] { return GetRightY(); }, nullptr);
builder.AddBooleanProperty("A",
[this] { return GetAButton(); }, nullptr);
builder.AddBooleanProperty("B",
[this] { return GetBButton(); }, nullptr);
builder.AddBooleanProperty("X",
[this] { return GetXButton(); }, nullptr);
builder.AddBooleanProperty("Y",
[this] { return GetYButton(); }, nullptr);
builder.AddBooleanProperty("LeftBumper",
[this] { return GetLeftBumperButton(); }, nullptr);
builder.AddBooleanProperty("RightBumper",
[this] { return GetRightBumperButton(); }, nullptr);
builder.AddBooleanProperty("LeftStick",
[this] { return GetLeftStickButton(); }, nullptr);
builder.AddBooleanProperty("RightStick",
[this] { return GetRightStickButton(); }, nullptr);
builder.AddBooleanProperty("Ellipses",
[this] { return GetEllipsesButton(); }, nullptr);
builder.AddBooleanProperty("Hamburger",
[this] { return GetHamburgerButton(); }, nullptr);
builder.AddBooleanProperty("Stadia",
[this] { return GetStadiaButton(); }, nullptr);
builder.AddBooleanProperty("RightTrigger",
[this] { return GetRightTriggerButton(); }, nullptr);
builder.AddBooleanProperty("LeftTrigger",
[this] { return GetLeftTriggerButton(); }, nullptr);
builder.AddBooleanProperty("Google",
[this] { return GetGoogleButton(); }, nullptr);
builder.AddBooleanProperty("Frame",
[this] { return GetFrameButton(); }, nullptr);
}

View File

@@ -6,39 +6,53 @@
#include "wpi/driverstation/NiDsXboxController.hpp"
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/event/BooleanEvent.hpp"
#include "wpi/hal/UsageReporting.hpp"
#include "wpi/util/sendable/SendableBuilder.hpp"
#include "wpi/event/BooleanEvent.hpp"
using namespace wpi;
NiDsXboxController::NiDsXboxController(int port) : GenericHID(port) {
HAL_ReportUsage("HID", port, "NiDsXboxController");
NiDsXboxController::NiDsXboxController(int port)
: NiDsXboxController{DriverStation::GetGenericHID(port)} {}
NiDsXboxController::NiDsXboxController(GenericHID& hid)
: m_hid{&hid} {
HAL_ReportUsage("HID", hid.GetPort(), "NiDsXboxController");
}
GenericHID& NiDsXboxController::GetHID() {
return *m_hid;
}
const GenericHID& NiDsXboxController::GetHID() const {
return *m_hid;
}
double NiDsXboxController::GetLeftX() const {
return GetRawAxis(Axis::kLeftX);
return m_hid->GetRawAxis(Axis::kLeftX);
}
double NiDsXboxController::GetRightX() const {
return GetRawAxis(Axis::kRightX);
return m_hid->GetRawAxis(Axis::kRightX);
}
double NiDsXboxController::GetLeftY() const {
return GetRawAxis(Axis::kLeftY);
return m_hid->GetRawAxis(Axis::kLeftY);
}
double NiDsXboxController::GetRightY() const {
return GetRawAxis(Axis::kRightY);
return m_hid->GetRawAxis(Axis::kRightY);
}
double NiDsXboxController::GetLeftTriggerAxis() const {
return GetRawAxis(Axis::kLeftTrigger);
return m_hid->GetRawAxis(Axis::kLeftTrigger);
}
BooleanEvent NiDsXboxController::LeftTrigger(double threshold, EventLoop* loop) const {
return BooleanEvent(loop, [this, threshold] { return this->GetLeftTriggerAxis() > threshold; });
BooleanEvent NiDsXboxController::LeftTrigger(double threshold,
EventLoop* loop) const {
return BooleanEvent(
loop, [this, threshold] { return this->GetLeftTriggerAxis() > threshold; });
}
BooleanEvent NiDsXboxController::LeftTrigger(EventLoop* loop) const {
@@ -46,11 +60,13 @@ BooleanEvent NiDsXboxController::LeftTrigger(EventLoop* loop) const {
}
double NiDsXboxController::GetRightTriggerAxis() const {
return GetRawAxis(Axis::kRightTrigger);
return m_hid->GetRawAxis(Axis::kRightTrigger);
}
BooleanEvent NiDsXboxController::RightTrigger(double threshold, EventLoop* loop) const {
return BooleanEvent(loop, [this, threshold] { return this->GetRightTriggerAxis() > threshold; });
BooleanEvent NiDsXboxController::RightTrigger(double threshold,
EventLoop* loop) const {
return BooleanEvent(
loop, [this, threshold] { return this->GetRightTriggerAxis() > threshold; });
}
BooleanEvent NiDsXboxController::RightTrigger(EventLoop* loop) const {
@@ -58,15 +74,15 @@ BooleanEvent NiDsXboxController::RightTrigger(EventLoop* loop) const {
}
bool NiDsXboxController::GetAButton() const {
return GetRawButton(Button::kA);
return m_hid->GetRawButton(Button::kA);
}
bool NiDsXboxController::GetAButtonPressed() {
return GetRawButtonPressed(Button::kA);
return m_hid->GetRawButtonPressed(Button::kA);
}
bool NiDsXboxController::GetAButtonReleased() {
return GetRawButtonReleased(Button::kA);
return m_hid->GetRawButtonReleased(Button::kA);
}
BooleanEvent NiDsXboxController::A(EventLoop* loop) const {
@@ -74,15 +90,15 @@ BooleanEvent NiDsXboxController::A(EventLoop* loop) const {
}
bool NiDsXboxController::GetBButton() const {
return GetRawButton(Button::kB);
return m_hid->GetRawButton(Button::kB);
}
bool NiDsXboxController::GetBButtonPressed() {
return GetRawButtonPressed(Button::kB);
return m_hid->GetRawButtonPressed(Button::kB);
}
bool NiDsXboxController::GetBButtonReleased() {
return GetRawButtonReleased(Button::kB);
return m_hid->GetRawButtonReleased(Button::kB);
}
BooleanEvent NiDsXboxController::B(EventLoop* loop) const {
@@ -90,15 +106,15 @@ BooleanEvent NiDsXboxController::B(EventLoop* loop) const {
}
bool NiDsXboxController::GetXButton() const {
return GetRawButton(Button::kX);
return m_hid->GetRawButton(Button::kX);
}
bool NiDsXboxController::GetXButtonPressed() {
return GetRawButtonPressed(Button::kX);
return m_hid->GetRawButtonPressed(Button::kX);
}
bool NiDsXboxController::GetXButtonReleased() {
return GetRawButtonReleased(Button::kX);
return m_hid->GetRawButtonReleased(Button::kX);
}
BooleanEvent NiDsXboxController::X(EventLoop* loop) const {
@@ -106,15 +122,15 @@ BooleanEvent NiDsXboxController::X(EventLoop* loop) const {
}
bool NiDsXboxController::GetYButton() const {
return GetRawButton(Button::kY);
return m_hid->GetRawButton(Button::kY);
}
bool NiDsXboxController::GetYButtonPressed() {
return GetRawButtonPressed(Button::kY);
return m_hid->GetRawButtonPressed(Button::kY);
}
bool NiDsXboxController::GetYButtonReleased() {
return GetRawButtonReleased(Button::kY);
return m_hid->GetRawButtonReleased(Button::kY);
}
BooleanEvent NiDsXboxController::Y(EventLoop* loop) const {
@@ -122,15 +138,15 @@ BooleanEvent NiDsXboxController::Y(EventLoop* loop) const {
}
bool NiDsXboxController::GetLeftBumperButton() const {
return GetRawButton(Button::kLeftBumper);
return m_hid->GetRawButton(Button::kLeftBumper);
}
bool NiDsXboxController::GetLeftBumperButtonPressed() {
return GetRawButtonPressed(Button::kLeftBumper);
return m_hid->GetRawButtonPressed(Button::kLeftBumper);
}
bool NiDsXboxController::GetLeftBumperButtonReleased() {
return GetRawButtonReleased(Button::kLeftBumper);
return m_hid->GetRawButtonReleased(Button::kLeftBumper);
}
BooleanEvent NiDsXboxController::LeftBumper(EventLoop* loop) const {
@@ -138,15 +154,15 @@ BooleanEvent NiDsXboxController::LeftBumper(EventLoop* loop) const {
}
bool NiDsXboxController::GetRightBumperButton() const {
return GetRawButton(Button::kRightBumper);
return m_hid->GetRawButton(Button::kRightBumper);
}
bool NiDsXboxController::GetRightBumperButtonPressed() {
return GetRawButtonPressed(Button::kRightBumper);
return m_hid->GetRawButtonPressed(Button::kRightBumper);
}
bool NiDsXboxController::GetRightBumperButtonReleased() {
return GetRawButtonReleased(Button::kRightBumper);
return m_hid->GetRawButtonReleased(Button::kRightBumper);
}
BooleanEvent NiDsXboxController::RightBumper(EventLoop* loop) const {
@@ -154,15 +170,15 @@ BooleanEvent NiDsXboxController::RightBumper(EventLoop* loop) const {
}
bool NiDsXboxController::GetBackButton() const {
return GetRawButton(Button::kBack);
return m_hid->GetRawButton(Button::kBack);
}
bool NiDsXboxController::GetBackButtonPressed() {
return GetRawButtonPressed(Button::kBack);
return m_hid->GetRawButtonPressed(Button::kBack);
}
bool NiDsXboxController::GetBackButtonReleased() {
return GetRawButtonReleased(Button::kBack);
return m_hid->GetRawButtonReleased(Button::kBack);
}
BooleanEvent NiDsXboxController::Back(EventLoop* loop) const {
@@ -170,15 +186,15 @@ BooleanEvent NiDsXboxController::Back(EventLoop* loop) const {
}
bool NiDsXboxController::GetStartButton() const {
return GetRawButton(Button::kStart);
return m_hid->GetRawButton(Button::kStart);
}
bool NiDsXboxController::GetStartButtonPressed() {
return GetRawButtonPressed(Button::kStart);
return m_hid->GetRawButtonPressed(Button::kStart);
}
bool NiDsXboxController::GetStartButtonReleased() {
return GetRawButtonReleased(Button::kStart);
return m_hid->GetRawButtonReleased(Button::kStart);
}
BooleanEvent NiDsXboxController::Start(EventLoop* loop) const {
@@ -186,15 +202,15 @@ BooleanEvent NiDsXboxController::Start(EventLoop* loop) const {
}
bool NiDsXboxController::GetLeftStickButton() const {
return GetRawButton(Button::kLeftStick);
return m_hid->GetRawButton(Button::kLeftStick);
}
bool NiDsXboxController::GetLeftStickButtonPressed() {
return GetRawButtonPressed(Button::kLeftStick);
return m_hid->GetRawButtonPressed(Button::kLeftStick);
}
bool NiDsXboxController::GetLeftStickButtonReleased() {
return GetRawButtonReleased(Button::kLeftStick);
return m_hid->GetRawButtonReleased(Button::kLeftStick);
}
BooleanEvent NiDsXboxController::LeftStick(EventLoop* loop) const {
@@ -202,38 +218,79 @@ BooleanEvent NiDsXboxController::LeftStick(EventLoop* loop) const {
}
bool NiDsXboxController::GetRightStickButton() const {
return GetRawButton(Button::kRightStick);
return m_hid->GetRawButton(Button::kRightStick);
}
bool NiDsXboxController::GetRightStickButtonPressed() {
return GetRawButtonPressed(Button::kRightStick);
return m_hid->GetRawButtonPressed(Button::kRightStick);
}
bool NiDsXboxController::GetRightStickButtonReleased() {
return GetRawButtonReleased(Button::kRightStick);
return m_hid->GetRawButtonReleased(Button::kRightStick);
}
BooleanEvent NiDsXboxController::RightStick(EventLoop* loop) const {
return BooleanEvent(loop, [this]() { return this->GetRightStickButton(); });
}
bool NiDsXboxController::IsConnected() const {
return m_hid->IsConnected();
}
GenericHID::HIDType NiDsXboxController::GetGamepadType() const {
return m_hid->GetGamepadType();
}
GenericHID::SupportedOutputs NiDsXboxController::GetSupportedOutputs() const {
return m_hid->GetSupportedOutputs();
}
std::string NiDsXboxController::GetName() const {
return m_hid->GetName();
}
int NiDsXboxController::GetPort() const {
return m_hid->GetPort();
}
void NiDsXboxController::SetRumble(GenericHID::RumbleType type,
double value) {
m_hid->SetRumble(type, value);
}
void NiDsXboxController::InitSendable(wpi::util::SendableBuilder& builder) {
builder.SetSmartDashboardType("HID");
builder.PublishConstString("ControllerType", "NiDsXbox");
builder.AddDoubleProperty("LeftTrigger Axis", [this] { return GetLeftTriggerAxis(); }, nullptr);
builder.AddDoubleProperty("RightTrigger Axis", [this] { return GetRightTriggerAxis(); }, nullptr);
builder.AddDoubleProperty("LeftX", [this] { return GetLeftX(); }, nullptr);
builder.AddDoubleProperty("RightX", [this] { return GetRightX(); }, nullptr);
builder.AddDoubleProperty("LeftY", [this] { return GetLeftY(); }, nullptr);
builder.AddDoubleProperty("RightY", [this] { return GetRightY(); }, nullptr);
builder.AddBooleanProperty("A", [this] { return GetAButton(); }, nullptr);
builder.AddBooleanProperty("B", [this] { return GetBButton(); }, nullptr);
builder.AddBooleanProperty("X", [this] { return GetXButton(); }, nullptr);
builder.AddBooleanProperty("Y", [this] { return GetYButton(); }, nullptr);
builder.AddBooleanProperty("LeftBumper", [this] { return GetLeftBumperButton(); }, nullptr);
builder.AddBooleanProperty("RightBumper", [this] { return GetRightBumperButton(); }, nullptr);
builder.AddBooleanProperty("Back", [this] { return GetBackButton(); }, nullptr);
builder.AddBooleanProperty("Start", [this] { return GetStartButton(); }, nullptr);
builder.AddBooleanProperty("LeftStick", [this] { return GetLeftStickButton(); }, nullptr);
builder.AddBooleanProperty("RightStick", [this] { return GetRightStickButton(); }, nullptr);
builder.AddDoubleProperty("LeftTrigger Axis",
[this] { return GetLeftTriggerAxis(); }, nullptr);
builder.AddDoubleProperty("RightTrigger Axis",
[this] { return GetRightTriggerAxis(); }, nullptr);
builder.AddDoubleProperty("LeftX",
[this] { return GetLeftX(); }, nullptr);
builder.AddDoubleProperty("RightX",
[this] { return GetRightX(); }, nullptr);
builder.AddDoubleProperty("LeftY",
[this] { return GetLeftY(); }, nullptr);
builder.AddDoubleProperty("RightY",
[this] { return GetRightY(); }, nullptr);
builder.AddBooleanProperty("A",
[this] { return GetAButton(); }, nullptr);
builder.AddBooleanProperty("B",
[this] { return GetBButton(); }, nullptr);
builder.AddBooleanProperty("X",
[this] { return GetXButton(); }, nullptr);
builder.AddBooleanProperty("Y",
[this] { return GetYButton(); }, nullptr);
builder.AddBooleanProperty("LeftBumper",
[this] { return GetLeftBumperButton(); }, nullptr);
builder.AddBooleanProperty("RightBumper",
[this] { return GetRightBumperButton(); }, nullptr);
builder.AddBooleanProperty("Back",
[this] { return GetBackButton(); }, nullptr);
builder.AddBooleanProperty("Start",
[this] { return GetStartButton(); }, nullptr);
builder.AddBooleanProperty("LeftStick",
[this] { return GetLeftStickButton(); }, nullptr);
builder.AddBooleanProperty("RightStick",
[this] { return GetRightStickButton(); }, nullptr);
}

View File

@@ -12,7 +12,7 @@ using namespace wpi;
using namespace wpi::sim;
NiDsPS4ControllerSim::NiDsPS4ControllerSim(const NiDsPS4Controller& joystick)
: GenericHIDSim{joystick} {
: GenericHIDSim{joystick.GetHID()} {
SetAxesMaximumIndex(6);
SetButtonsMaximumIndex(14);
SetPOVsMaximumIndex(1);

View File

@@ -12,7 +12,7 @@ using namespace wpi;
using namespace wpi::sim;
NiDsPS5ControllerSim::NiDsPS5ControllerSim(const NiDsPS5Controller& joystick)
: GenericHIDSim{joystick} {
: GenericHIDSim{joystick.GetHID()} {
SetAxesMaximumIndex(6);
SetButtonsMaximumIndex(14);
SetPOVsMaximumIndex(1);

View File

@@ -12,7 +12,7 @@ using namespace wpi;
using namespace wpi::sim;
NiDsStadiaControllerSim::NiDsStadiaControllerSim(const NiDsStadiaController& joystick)
: GenericHIDSim{joystick} {
: GenericHIDSim{joystick.GetHID()} {
SetAxesMaximumIndex(4);
SetButtonsMaximumIndex(15);
SetPOVsMaximumIndex(1);

View File

@@ -12,7 +12,7 @@ using namespace wpi;
using namespace wpi::sim;
NiDsXboxControllerSim::NiDsXboxControllerSim(const NiDsXboxController& joystick)
: GenericHIDSim{joystick} {
: GenericHIDSim{joystick.GetHID()} {
SetAxesMaximumIndex(6);
SetButtonsMaximumIndex(10);
SetPOVsMaximumIndex(1);

View File

@@ -6,10 +6,12 @@
#pragma once
#include "wpi/util/sendable/Sendable.hpp"
#include "wpi/util/sendable/SendableHelper.hpp"
#include <string>
#include "wpi/driverstation/GenericHID.hpp"
#include "wpi/driverstation/HIDDevice.hpp"
#include "wpi/util/sendable/Sendable.hpp"
#include "wpi/util/sendable/SendableHelper.hpp"
namespace wpi {
@@ -25,9 +27,10 @@ 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 NiDsPS4Controller : public GenericHID,
public wpi::util::Sendable,
public wpi::util::SendableHelper<NiDsPS4Controller> {
class NiDsPS4Controller
: public HIDDevice,
public wpi::util::Sendable,
public wpi::util::SendableHelper<NiDsPS4Controller> {
public:
/**
* Construct an instance of a controller.
@@ -39,11 +42,32 @@ class NiDsPS4Controller : public GenericHID,
*/
explicit NiDsPS4Controller(int port);
/**
* Construct an instance of a controller with a GenericHID object.
*
* @param hid The GenericHID object to use for this controller.
*/
explicit NiDsPS4Controller(GenericHID& hid);
~NiDsPS4Controller() override = default;
NiDsPS4Controller(NiDsPS4Controller&&) = default;
NiDsPS4Controller& operator=(NiDsPS4Controller&&) = 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.
*
@@ -570,7 +594,56 @@ class NiDsPS4Controller : public GenericHID,
static constexpr int kR2 = 4;
};
/**
* Get if the controller is connected.
*
* @return true if the controller is connected
*/
bool IsConnected() const;
/**
* Get the type of the controller.
*
* @return the type of the controller.
*/
GenericHID::HIDType GetGamepadType() const;
/**
* Get the supported outputs of the controller.
*
* @return the supported outputs of the controller.
*/
GenericHID::SupportedOutputs GetSupportedOutputs() const;
/**
* Get the name of the controller.
*
* @return the name of the controller.
*/
std::string GetName() const;
/**
* Get the port number of the controller.
*
* @return The port number of the controller.
*/
int GetPort() const;
/**
* 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);
void InitSendable(wpi::util::SendableBuilder& builder) override;
private:
GenericHID* m_hid;
};
} // namespace wpi

View File

@@ -6,10 +6,12 @@
#pragma once
#include "wpi/util/sendable/Sendable.hpp"
#include "wpi/util/sendable/SendableHelper.hpp"
#include <string>
#include "wpi/driverstation/GenericHID.hpp"
#include "wpi/driverstation/HIDDevice.hpp"
#include "wpi/util/sendable/Sendable.hpp"
#include "wpi/util/sendable/SendableHelper.hpp"
namespace wpi {
@@ -25,9 +27,10 @@ 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 NiDsPS5Controller : public GenericHID,
public wpi::util::Sendable,
public wpi::util::SendableHelper<NiDsPS5Controller> {
class NiDsPS5Controller
: public HIDDevice,
public wpi::util::Sendable,
public wpi::util::SendableHelper<NiDsPS5Controller> {
public:
/**
* Construct an instance of a controller.
@@ -39,11 +42,32 @@ class NiDsPS5Controller : public GenericHID,
*/
explicit NiDsPS5Controller(int port);
/**
* Construct an instance of a controller with a GenericHID object.
*
* @param hid The GenericHID object to use for this controller.
*/
explicit NiDsPS5Controller(GenericHID& hid);
~NiDsPS5Controller() override = default;
NiDsPS5Controller(NiDsPS5Controller&&) = default;
NiDsPS5Controller& operator=(NiDsPS5Controller&&) = 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.
*
@@ -570,7 +594,56 @@ class NiDsPS5Controller : public GenericHID,
static constexpr int kR2 = 4;
};
/**
* Get if the controller is connected.
*
* @return true if the controller is connected
*/
bool IsConnected() const;
/**
* Get the type of the controller.
*
* @return the type of the controller.
*/
GenericHID::HIDType GetGamepadType() const;
/**
* Get the supported outputs of the controller.
*
* @return the supported outputs of the controller.
*/
GenericHID::SupportedOutputs GetSupportedOutputs() const;
/**
* Get the name of the controller.
*
* @return the name of the controller.
*/
std::string GetName() const;
/**
* Get the port number of the controller.
*
* @return The port number of the controller.
*/
int GetPort() const;
/**
* 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);
void InitSendable(wpi::util::SendableBuilder& builder) override;
private:
GenericHID* m_hid;
};
} // namespace wpi

View File

@@ -6,10 +6,12 @@
#pragma once
#include "wpi/util/sendable/Sendable.hpp"
#include "wpi/util/sendable/SendableHelper.hpp"
#include <string>
#include "wpi/driverstation/GenericHID.hpp"
#include "wpi/driverstation/HIDDevice.hpp"
#include "wpi/util/sendable/Sendable.hpp"
#include "wpi/util/sendable/SendableHelper.hpp"
namespace wpi {
@@ -25,9 +27,10 @@ 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 NiDsStadiaController : public GenericHID,
public wpi::util::Sendable,
public wpi::util::SendableHelper<NiDsStadiaController> {
class NiDsStadiaController
: public HIDDevice,
public wpi::util::Sendable,
public wpi::util::SendableHelper<NiDsStadiaController> {
public:
/**
* Construct an instance of a controller.
@@ -39,11 +42,32 @@ class NiDsStadiaController : public GenericHID,
*/
explicit NiDsStadiaController(int port);
/**
* Construct an instance of a controller with a GenericHID object.
*
* @param hid The GenericHID object to use for this controller.
*/
explicit NiDsStadiaController(GenericHID& hid);
~NiDsStadiaController() override = default;
NiDsStadiaController(NiDsStadiaController&&) = default;
NiDsStadiaController& operator=(NiDsStadiaController&&) = 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.
*
@@ -583,7 +607,56 @@ class NiDsStadiaController : public GenericHID,
static constexpr int kRightY = 4;
};
/**
* Get if the controller is connected.
*
* @return true if the controller is connected
*/
bool IsConnected() const;
/**
* Get the type of the controller.
*
* @return the type of the controller.
*/
GenericHID::HIDType GetGamepadType() const;
/**
* Get the supported outputs of the controller.
*
* @return the supported outputs of the controller.
*/
GenericHID::SupportedOutputs GetSupportedOutputs() const;
/**
* Get the name of the controller.
*
* @return the name of the controller.
*/
std::string GetName() const;
/**
* Get the port number of the controller.
*
* @return The port number of the controller.
*/
int GetPort() const;
/**
* 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);
void InitSendable(wpi::util::SendableBuilder& builder) override;
private:
GenericHID* m_hid;
};
} // namespace wpi

View File

@@ -6,10 +6,12 @@
#pragma once
#include "wpi/util/sendable/Sendable.hpp"
#include "wpi/util/sendable/SendableHelper.hpp"
#include <string>
#include "wpi/driverstation/GenericHID.hpp"
#include "wpi/driverstation/HIDDevice.hpp"
#include "wpi/util/sendable/Sendable.hpp"
#include "wpi/util/sendable/SendableHelper.hpp"
namespace wpi {
@@ -25,9 +27,10 @@ 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 NiDsXboxController : public GenericHID,
public wpi::util::Sendable,
public wpi::util::SendableHelper<NiDsXboxController> {
class NiDsXboxController
: public HIDDevice,
public wpi::util::Sendable,
public wpi::util::SendableHelper<NiDsXboxController> {
public:
/**
* Construct an instance of a controller.
@@ -39,11 +42,32 @@ class NiDsXboxController : public GenericHID,
*/
explicit NiDsXboxController(int port);
/**
* Construct an instance of a controller with a GenericHID object.
*
* @param hid The GenericHID object to use for this controller.
*/
explicit NiDsXboxController(GenericHID& hid);
~NiDsXboxController() override = default;
NiDsXboxController(NiDsXboxController&&) = default;
NiDsXboxController& operator=(NiDsXboxController&&) = 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.
*
@@ -482,7 +506,56 @@ class NiDsXboxController : public GenericHID,
static constexpr int kRightTrigger = 3;
};
/**
* Get if the controller is connected.
*
* @return true if the controller is connected
*/
bool IsConnected() const;
/**
* Get the type of the controller.
*
* @return the type of the controller.
*/
GenericHID::HIDType GetGamepadType() const;
/**
* Get the supported outputs of the controller.
*
* @return the supported outputs of the controller.
*/
GenericHID::SupportedOutputs GetSupportedOutputs() const;
/**
* Get the name of the controller.
*
* @return the name of the controller.
*/
std::string GetName() const;
/**
* Get the port number of the controller.
*
* @return The port number of the controller.
*/
int GetPort() const;
/**
* 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);
void InitSendable(wpi::util::SendableBuilder& builder) override;
private:
GenericHID* m_hid;
};
} // namespace wpi

View 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();
}
}

View File

@@ -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);
}

View File

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

View File

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

View File

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

View File

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

View File

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

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View 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

View File

@@ -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();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -2,7 +2,7 @@
// 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 <string>
#include <cstring>
#include <tuple>
#include <gtest/gtest.h>
@@ -37,7 +37,7 @@ INSTANTIATE_TEST_SUITE_P(IsConnectedTests, IsJoystickConnectedParametersTest,
std::make_tuple(4, 10, 1, true)));
class JoystickConnectionWarningTest
: public ::testing::TestWithParam<
std::tuple<bool, bool, bool, std::string>> {};
std::tuple<bool, bool, bool, const char*>> {};
TEST_P(JoystickConnectionWarningTest, JoystickConnectionWarnings) {
// Capture all output to stderr.
@@ -51,15 +51,17 @@ TEST_P(JoystickConnectionWarningTest, JoystickConnectionWarnings) {
// Create joystick and attempt to retrieve button.
wpi::Joystick joystick(0);
joystick.GetRawButton(1);
joystick.GetHID().GetRawButton(1);
wpi::sim::StepTiming(1_s);
EXPECT_EQ(wpi::internal::DriverStationBackend::
IsJoystickConnectionWarningSilenced(),
std::get<2>(GetParam()));
EXPECT_EQ(::testing::internal::GetCapturedStderr().substr(
0, std::get<3>(GetParam()).size()),
std::get<3>(GetParam()));
auto expected = std::get<3>(GetParam());
EXPECT_STREQ(::testing::internal::GetCapturedStderr()
.substr(0, std::strlen(expected))
.c_str(),
expected);
}
INSTANTIATE_TEST_SUITE_P(

View File

@@ -6,13 +6,14 @@
#include <gtest/gtest.h>
#include "wpi/driverstation/internal/DriverStationBackend.hpp"
#include "wpi/simulation/GenericHIDSim.hpp"
using namespace wpi;
using RumbleType = GenericHID::RumbleType;
static constexpr double kEpsilon = 0.0001;
TEST(GenericHIDTest, RumbleRange) {
GenericHID hid{0};
GenericHID hid = internal::DriverStationBackend::ConstructGenericHID(0);
sim::GenericHIDSim sim{0};
for (int i = 0; i <= 100; i++) {
@@ -34,7 +35,7 @@ TEST(GenericHIDTest, RumbleRange) {
}
TEST(GenericHIDTest, RumbleTypes) {
GenericHID hid{0};
GenericHID hid = internal::DriverStationBackend::ConstructGenericHID(0);
sim::GenericHIDSim sim{0};
// Make sure all are off

View File

@@ -0,0 +1,67 @@
import pytest
from wpilib import DriverStation, DriverStationBackend, GenericHID
from wpilib.simulation import DriverStationSim, GenericHIDSim
RumbleType = GenericHID.RumbleType
RUMBLE_TYPES = (
RumbleType.LEFT_RUMBLE,
RumbleType.RIGHT_RUMBLE,
RumbleType.LEFT_TRIGGER_RUMBLE,
RumbleType.RIGHT_TRIGGER_RUMBLE,
)
@pytest.fixture(autouse=True)
def reset_driver_station_sim():
DriverStationSim.resetData()
yield
DriverStationBackend.resetCachedHIDData()
DriverStationSim.resetData()
def test_rumble_range() -> None:
hid = DriverStationBackend.constructGenericHID(0)
sim = GenericHIDSim(0)
for i in range(101):
rumble_value = i / 100.0
for rumble_type in RUMBLE_TYPES:
hid.setRumble(rumble_type, rumble_value)
assert sim.getRumble(rumble_type) == pytest.approx(
rumble_value, abs=0.0001
)
def test_rumble_types() -> None:
hid = DriverStationBackend.constructGenericHID(0)
sim = GenericHIDSim(0)
for rumble_type in RUMBLE_TYPES:
hid.setRumble(rumble_type, 0)
for rumble_type in RUMBLE_TYPES:
assert sim.getRumble(rumble_type) == pytest.approx(0, abs=0.0001)
for active_rumble_type in RUMBLE_TYPES:
hid.setRumble(active_rumble_type, 1)
for rumble_type in RUMBLE_TYPES:
expected = 1 if rumble_type == active_rumble_type else 0
assert sim.getRumble(rumble_type) == pytest.approx(
expected, abs=0.0001
)
hid.setRumble(active_rumble_type, 0)
def test_cached_hid_data_reset() -> None:
DriverStation.getGenericHID(0)
DriverStation.getGamepad(0)
DriverStationBackend.resetCachedHIDData()
assert DriverStation.getGenericHID(0).getPort() == 0
assert DriverStation.getGamepad(0).getPort() == 0