[hal,wpilib] Add Touchpad support (#8401)

This commit is contained in:
Thad House
2025-11-21 13:57:11 -08:00
committed by GitHub
parent 8546d301e3
commit 32fc543dc8
34 changed files with 1319 additions and 437 deletions

View File

@@ -350,6 +350,71 @@ double DriverStation::GetStickAxis(int stick, int axis) {
return axes.axes[axis];
}
DriverStation::TouchpadFinger DriverStation::GetStickTouchpadFinger(
int stick, int touchpad, int finger) {
if (stick < 0 || stick >= kJoystickPorts) {
WPILIB_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick);
return TouchpadFinger{false, 0.0f, 0.0f};
}
if (touchpad < 0 || touchpad >= HAL_kMaxJoystickTouchpads) {
WPILIB_ReportError(warn::BadJoystickAxis, "touchpad {} out of range",
touchpad);
return TouchpadFinger{false, 0.0f, 0.0f};
}
if (finger < 0 || finger >= HAL_kMaxJoystickTouchpadFingers) {
WPILIB_ReportError(warn::BadJoystickAxis, "finger {} out of range", finger);
return TouchpadFinger{false, 0.0f, 0.0f};
}
HAL_JoystickTouchpads touchpads;
HAL_GetJoystickTouchpads(stick, &touchpads);
auto touchpadCount = touchpads.count;
if (touchpad < touchpadCount) {
if (finger < touchpads.touchpads[touchpad].count) {
return TouchpadFinger{
touchpads.touchpads[touchpad].fingers[finger].down != 0,
touchpads.touchpads[touchpad].fingers[finger].x,
touchpads.touchpads[touchpad].fingers[finger].y};
}
}
ReportJoystickUnpluggedWarning(
"Joystick Touchpad Finger {} missing, check if all controllers are "
"plugged in",
touchpad);
return TouchpadFinger{false, 0.0f, 0.0f};
}
bool DriverStation::GetStickTouchpadFingerAvailable(int stick, int touchpad,
int finger) {
if (stick < 0 || stick >= kJoystickPorts) {
WPILIB_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick);
return false;
}
if (touchpad < 0 || touchpad >= HAL_kMaxJoystickTouchpads) {
WPILIB_ReportError(warn::BadJoystickAxis, "touchpad {} out of range",
touchpad);
return false;
}
if (finger < 0 || finger >= HAL_kMaxJoystickTouchpadFingers) {
WPILIB_ReportError(warn::BadJoystickAxis, "finger {} out of range", finger);
return false;
}
HAL_JoystickTouchpads touchpads;
HAL_GetJoystickTouchpads(stick, &touchpads);
auto touchpadCount = touchpads.count;
if (touchpad < touchpadCount) {
if (finger < touchpads.touchpads[touchpad].count) {
return true;
}
}
return false;
}
std::optional<double> DriverStation::GetStickAxisIfAvailable(int stick,
int axis) {
if (stick < 0 || stick >= kJoystickPorts) {

View File

@@ -176,3 +176,13 @@ void GenericHID::SetRumble(RumbleType type, double value) {
HAL_SetJoystickRumble(m_port, m_leftRumble, m_rightRumble,
m_leftTriggerRumble, m_rightTriggerRumble);
}
bool GenericHID::GetTouchpadFingerAvailable(int touchpad, int finger) const {
return DriverStation::GetStickTouchpadFingerAvailable(m_port, touchpad,
finger);
}
DriverStation::TouchpadFinger GenericHID::GetTouchpadFinger(int touchpad,
int finger) const {
return DriverStation::GetStickTouchpadFinger(m_port, touchpad, finger);
}

View File

@@ -72,6 +72,12 @@ class DriverStation final {
kUpLeft = HAL_JoystickPOV_kLeftUp,
};
struct TouchpadFinger final {
bool down = false;
float x = 0.0f;
float y = 0.0f;
};
/**
* Gets the angle of a POVDirection.
*
@@ -159,6 +165,28 @@ class DriverStation final {
*/
static double GetStickAxis(int stick, int axis);
/**
* Get the finger data of a touchpad on a joystick, if available.
*
* @param stick The joystick to read.
* @param touchpad The touchpad index to read from the joystick.
* @param finger The finger index to read from the touchpad.
* @return The finger data of the touchpad on the joystick.
*/
static TouchpadFinger GetStickTouchpadFinger(int stick, int touchpad,
int finger);
/**
* Whether a finger on a touchpad is available.
*
* @param stick The joystick to read.
* @param touchpad The touchpad index to read from the joystick.
* @param finger The finger index to read from the touchpad.
* @return True if the finger data is available.
*/
static bool GetStickTouchpadFingerAvailable(int stick, int touchpad,
int finger);
/**
* Get the value of the axis on a joystick, if available.
*

View File

@@ -370,6 +370,23 @@ class GenericHID {
*/
void SetRumble(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.
*/
DriverStation::TouchpadFinger GetTouchpadFinger(int touchpad,
int finger) const;
private:
int m_port;
uint16_t m_leftRumble = 0;