mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11:42 +00:00
[hal,wpilib] Add support for joystick outputs (#8385)
Support joystick outputs, including Rumble and LEDs. Also requires an update to Joystick descriptors, as that has also changed in mrccomm to support showing what outputs are supported.
This commit is contained in:
@@ -471,7 +471,7 @@ bool DriverStation::GetJoystickIsGamepad(int stick) {
|
||||
return static_cast<bool>(descriptor.isGamepad);
|
||||
}
|
||||
|
||||
int DriverStation::GetJoystickType(int stick) {
|
||||
int DriverStation::GetJoystickGamepadType(int stick) {
|
||||
if (stick < 0 || stick >= kJoystickPorts) {
|
||||
WPILIB_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick);
|
||||
return -1;
|
||||
@@ -480,7 +480,19 @@ int DriverStation::GetJoystickType(int stick) {
|
||||
HAL_JoystickDescriptor descriptor;
|
||||
HAL_GetJoystickDescriptor(stick, &descriptor);
|
||||
|
||||
return static_cast<int>(descriptor.type);
|
||||
return static_cast<int>(descriptor.gamepadType);
|
||||
}
|
||||
|
||||
int DriverStation::GetJoystickSupportedOutputs(int stick) {
|
||||
if (stick < 0 || stick >= kJoystickPorts) {
|
||||
WPILIB_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick);
|
||||
return 0;
|
||||
}
|
||||
|
||||
HAL_JoystickDescriptor descriptor;
|
||||
HAL_GetJoystickDescriptor(stick, &descriptor);
|
||||
|
||||
return static_cast<int>(descriptor.supportedOutputs);
|
||||
}
|
||||
|
||||
std::string DriverStation::GetJoystickName(int stick) {
|
||||
|
||||
@@ -135,8 +135,13 @@ bool GenericHID::IsConnected() const {
|
||||
return DriverStation::IsJoystickConnected(m_port);
|
||||
}
|
||||
|
||||
GenericHID::HIDType GenericHID::GetType() const {
|
||||
return static_cast<HIDType>(DriverStation::GetJoystickType(m_port));
|
||||
GenericHID::HIDType GenericHID::GetGamepadType() const {
|
||||
return static_cast<HIDType>(DriverStation::GetJoystickGamepadType(m_port));
|
||||
}
|
||||
|
||||
GenericHID::SupportedOutputs GenericHID::GetSupportedOutputs() const {
|
||||
return static_cast<SupportedOutputs>(
|
||||
DriverStation::GetJoystickSupportedOutputs(m_port));
|
||||
}
|
||||
|
||||
std::string GenericHID::GetName() const {
|
||||
@@ -147,16 +152,11 @@ int GenericHID::GetPort() const {
|
||||
return m_port;
|
||||
}
|
||||
|
||||
void GenericHID::SetOutput(int outputNumber, bool value) {
|
||||
m_outputs =
|
||||
(m_outputs & ~(1 << (outputNumber - 1))) | (value << (outputNumber - 1));
|
||||
|
||||
HAL_SetJoystickOutputs(m_port, m_outputs, m_leftRumble, m_rightRumble);
|
||||
}
|
||||
|
||||
void GenericHID::SetOutputs(int value) {
|
||||
m_outputs = value;
|
||||
HAL_SetJoystickOutputs(m_port, m_outputs, m_leftRumble, m_rightRumble);
|
||||
void GenericHID::SetLeds(int r, int g, int b) {
|
||||
uint32_t value = (static_cast<uint32_t>(r & 0xFF) << 16) |
|
||||
(static_cast<uint32_t>(g & 0xFF) << 8) |
|
||||
static_cast<uint32_t>(b & 0xFF);
|
||||
HAL_SetJoystickLeds(m_port, value);
|
||||
}
|
||||
|
||||
void GenericHID::SetRumble(RumbleType type, double value) {
|
||||
@@ -167,10 +167,12 @@ void GenericHID::SetRumble(RumbleType type, double value) {
|
||||
m_leftRumble = rumbleValue;
|
||||
} else if (type == kRightRumble) {
|
||||
m_rightRumble = rumbleValue;
|
||||
} else {
|
||||
m_leftRumble = rumbleValue;
|
||||
m_rightRumble = rumbleValue;
|
||||
} else if (type == kLeftTriggerRumble) {
|
||||
m_leftTriggerRumble = rumbleValue;
|
||||
} else if (type == kRightTriggerRumble) {
|
||||
m_rightTriggerRumble = rumbleValue;
|
||||
}
|
||||
|
||||
HAL_SetJoystickOutputs(m_port, m_outputs, m_leftRumble, m_rightRumble);
|
||||
HAL_SetJoystickRumble(m_port, m_leftRumble, m_rightRumble,
|
||||
m_leftTriggerRumble, m_rightTriggerRumble);
|
||||
}
|
||||
|
||||
@@ -180,20 +180,31 @@ void DriverStationSim::SetSendConsoleLine(bool shouldSend) {
|
||||
}
|
||||
}
|
||||
|
||||
int64_t DriverStationSim::GetJoystickOutputs(int stick) {
|
||||
int64_t outputs = 0;
|
||||
int32_t leftRumble;
|
||||
int32_t rightRumble;
|
||||
HALSIM_GetJoystickOutputs(stick, &outputs, &leftRumble, &rightRumble);
|
||||
return outputs;
|
||||
int32_t DriverStationSim::GetJoystickLeds(int stick) {
|
||||
int32_t leds = 0;
|
||||
HALSIM_GetJoystickLeds(stick, &leds);
|
||||
return leds;
|
||||
}
|
||||
|
||||
int DriverStationSim::GetJoystickRumble(int stick, int rumbleNum) {
|
||||
int64_t outputs;
|
||||
int32_t leftRumble = 0;
|
||||
int32_t rightRumble = 0;
|
||||
HALSIM_GetJoystickOutputs(stick, &outputs, &leftRumble, &rightRumble);
|
||||
return rumbleNum == 0 ? leftRumble : rightRumble;
|
||||
int32_t leftTriggerRumble = 0;
|
||||
int32_t rightTriggerRumble = 0;
|
||||
HALSIM_GetJoystickRumbles(stick, &leftRumble, &rightRumble,
|
||||
&leftTriggerRumble, &rightTriggerRumble);
|
||||
switch (rumbleNum) {
|
||||
case 0:
|
||||
return leftRumble;
|
||||
case 1:
|
||||
return rightRumble;
|
||||
case 2:
|
||||
return leftTriggerRumble;
|
||||
case 3:
|
||||
return rightTriggerRumble;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void DriverStationSim::SetJoystickButton(int stick, int button, bool state) {
|
||||
@@ -261,8 +272,13 @@ void DriverStationSim::SetJoystickIsGamepad(int stick, bool isGamepad) {
|
||||
HALSIM_SetJoystickIsGamepad(stick, isGamepad);
|
||||
}
|
||||
|
||||
void DriverStationSim::SetJoystickType(int stick, int type) {
|
||||
HALSIM_SetJoystickType(stick, type);
|
||||
void DriverStationSim::SetJoystickGamepadType(int stick, int type) {
|
||||
HALSIM_SetJoystickGamepadType(stick, type);
|
||||
}
|
||||
|
||||
void DriverStationSim::SetJoystickSupportedOutputs(int stick,
|
||||
int supportedOutputs) {
|
||||
HALSIM_SetJoystickSupportedOutputs(stick, supportedOutputs);
|
||||
}
|
||||
|
||||
void DriverStationSim::SetJoystickName(int stick, std::string_view name) {
|
||||
|
||||
@@ -60,25 +60,41 @@ void GenericHIDSim::SetButtonsAvailable(uint64_t count) {
|
||||
DriverStationSim::SetJoystickButtonsAvailable(m_port, count);
|
||||
}
|
||||
|
||||
void GenericHIDSim::SetType(GenericHID::HIDType type) {
|
||||
DriverStationSim::SetJoystickType(m_port, type);
|
||||
void GenericHIDSim::SetGamepadType(GenericHID::HIDType type) {
|
||||
DriverStationSim::SetJoystickGamepadType(m_port, type);
|
||||
}
|
||||
|
||||
void GenericHIDSim::SetSupportedOutputs(
|
||||
GenericHID::SupportedOutputs supportedOutputs) {
|
||||
DriverStationSim::SetJoystickSupportedOutputs(m_port, supportedOutputs);
|
||||
}
|
||||
|
||||
void GenericHIDSim::SetName(const char* name) {
|
||||
DriverStationSim::SetJoystickName(m_port, name);
|
||||
}
|
||||
|
||||
bool GenericHIDSim::GetOutput(int outputNumber) {
|
||||
int64_t outputs = GetOutputs();
|
||||
return (outputs & (static_cast<int64_t>(1) << (outputNumber - 1))) != 0;
|
||||
}
|
||||
|
||||
int64_t GenericHIDSim::GetOutputs() {
|
||||
return DriverStationSim::GetJoystickOutputs(m_port);
|
||||
int32_t GenericHIDSim::GetLeds() {
|
||||
return DriverStationSim::GetJoystickLeds(m_port);
|
||||
}
|
||||
|
||||
double GenericHIDSim::GetRumble(GenericHID::RumbleType type) {
|
||||
int value = DriverStationSim::GetJoystickRumble(
|
||||
m_port, type == GenericHID::kLeftRumble ? 0 : 1);
|
||||
int intType = 0;
|
||||
switch (type) {
|
||||
case GenericHID::kLeftRumble:
|
||||
intType = 0;
|
||||
break;
|
||||
case GenericHID::kRightRumble:
|
||||
intType = 1;
|
||||
break;
|
||||
case GenericHID::kLeftTriggerRumble:
|
||||
intType = 2;
|
||||
break;
|
||||
case GenericHID::kRightTriggerRumble:
|
||||
intType = 3;
|
||||
break;
|
||||
default:
|
||||
return 0.0;
|
||||
}
|
||||
int value = DriverStationSim::GetJoystickRumble(m_port, intType);
|
||||
return value / 65535.0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user