[sim] Add joystick simulation support (#2595)

This adds joystick functions to DriverStationSim, and new GenericHIDSim,
JoystickSim, and XboxControllerSim classes.
This commit is contained in:
Peter Johnson
2020-07-15 00:33:57 -07:00
committed by GitHub
parent b06ddcdd86
commit b9feb81226
19 changed files with 1979 additions and 19 deletions

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
/* Copyright (c) 2017-2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
@@ -12,6 +12,8 @@
#include "DriverStationDataInternal.h"
#include "hal/DriverStation.h"
static constexpr int kNumJoysticks = 6;
namespace hal {
struct JoystickOutputStore {
int64_t outputs = 0;
@@ -47,13 +49,14 @@ void DriverStationData::ResetData() {
{
std::scoped_lock lock(m_joystickDataMutex);
m_joystickAxes = std::make_unique<HAL_JoystickAxes[]>(6);
m_joystickPOVs = std::make_unique<HAL_JoystickPOVs[]>(6);
m_joystickButtons = std::make_unique<HAL_JoystickButtons[]>(6);
m_joystickOutputs = std::make_unique<JoystickOutputStore[]>(6);
m_joystickDescriptor = std::make_unique<HAL_JoystickDescriptor[]>(6);
m_joystickAxes = std::make_unique<HAL_JoystickAxes[]>(kNumJoysticks);
m_joystickPOVs = std::make_unique<HAL_JoystickPOVs[]>(kNumJoysticks);
m_joystickButtons = std::make_unique<HAL_JoystickButtons[]>(kNumJoysticks);
m_joystickOutputs = std::make_unique<JoystickOutputStore[]>(kNumJoysticks);
m_joystickDescriptor =
std::make_unique<HAL_JoystickDescriptor[]>(kNumJoysticks);
for (int i = 0; i < 6; i++) {
for (int i = 0; i < kNumJoysticks; i++) {
m_joystickAxes[i].count = 0;
m_joystickPOVs[i].count = 0;
m_joystickButtons[i].count = 0;
@@ -71,21 +74,25 @@ void DriverStationData::ResetData() {
void DriverStationData::GetJoystickAxes(int32_t joystickNum,
HAL_JoystickAxes* axes) {
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return;
std::scoped_lock lock(m_joystickDataMutex);
*axes = m_joystickAxes[joystickNum];
}
void DriverStationData::GetJoystickPOVs(int32_t joystickNum,
HAL_JoystickPOVs* povs) {
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return;
std::scoped_lock lock(m_joystickDataMutex);
*povs = m_joystickPOVs[joystickNum];
}
void DriverStationData::GetJoystickButtons(int32_t joystickNum,
HAL_JoystickButtons* buttons) {
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return;
std::scoped_lock lock(m_joystickDataMutex);
*buttons = m_joystickButtons[joystickNum];
}
void DriverStationData::GetJoystickDescriptor(
int32_t joystickNum, HAL_JoystickDescriptor* descriptor) {
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return;
std::scoped_lock lock(m_joystickDataMutex);
*descriptor = m_joystickDescriptor[joystickNum];
// Always ensure name is null terminated
@@ -95,6 +102,7 @@ void DriverStationData::GetJoystickOutputs(int32_t joystickNum,
int64_t* outputs,
int32_t* leftRumble,
int32_t* rightRumble) {
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return;
std::scoped_lock lock(m_joystickDataMutex);
*leftRumble = m_joystickOutputs[joystickNum].leftRumble;
*outputs = m_joystickOutputs[joystickNum].outputs;
@@ -107,22 +115,26 @@ void DriverStationData::GetMatchInfo(HAL_MatchInfo* info) {
void DriverStationData::SetJoystickAxes(int32_t joystickNum,
const HAL_JoystickAxes* axes) {
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return;
std::scoped_lock lock(m_joystickDataMutex);
m_joystickAxes[joystickNum] = *axes;
}
void DriverStationData::SetJoystickPOVs(int32_t joystickNum,
const HAL_JoystickPOVs* povs) {
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return;
std::scoped_lock lock(m_joystickDataMutex);
m_joystickPOVs[joystickNum] = *povs;
}
void DriverStationData::SetJoystickButtons(int32_t joystickNum,
const HAL_JoystickButtons* buttons) {
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return;
std::scoped_lock lock(m_joystickDataMutex);
m_joystickButtons[joystickNum] = *buttons;
}
void DriverStationData::SetJoystickDescriptor(
int32_t joystickNum, const HAL_JoystickDescriptor* descriptor) {
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return;
std::scoped_lock lock(m_joystickDataMutex);
m_joystickDescriptor[joystickNum] = *descriptor;
}
@@ -130,6 +142,7 @@ void DriverStationData::SetJoystickDescriptor(
void DriverStationData::SetJoystickOutputs(int32_t joystickNum, int64_t outputs,
int32_t leftRumble,
int32_t rightRumble) {
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return;
std::scoped_lock lock(m_joystickDataMutex);
m_joystickOutputs[joystickNum].leftRumble = leftRumble;
m_joystickOutputs[joystickNum].outputs = outputs;
@@ -144,6 +157,116 @@ void DriverStationData::SetMatchInfo(const HAL_MatchInfo* info) {
void DriverStationData::NotifyNewData() { HAL_ReleaseDSMutex(); }
void DriverStationData::SetJoystickButton(int32_t stick, int32_t button,
HAL_Bool state) {
if (stick < 0 || stick >= kNumJoysticks) return;
std::scoped_lock lock(m_joystickDataMutex);
if (state)
m_joystickButtons[stick].buttons |= 1 << (button - 1);
else
m_joystickButtons[stick].buttons &= ~(1 << (button - 1));
}
void DriverStationData::SetJoystickAxis(int32_t stick, int32_t axis,
double value) {
if (stick < 0 || stick >= kNumJoysticks) return;
if (axis < 0 || axis >= HAL_kMaxJoystickAxes) return;
std::scoped_lock lock(m_joystickDataMutex);
m_joystickAxes[stick].axes[axis] = value;
}
void DriverStationData::SetJoystickPOV(int32_t stick, int32_t pov,
int32_t value) {
if (stick < 0 || stick >= kNumJoysticks) return;
if (pov < 0 || pov >= HAL_kMaxJoystickPOVs) return;
std::scoped_lock lock(m_joystickDataMutex);
m_joystickPOVs[stick].povs[pov] = value;
}
void DriverStationData::SetJoystickButtons(int32_t stick, uint32_t buttons) {
if (stick < 0 || stick >= kNumJoysticks) return;
std::scoped_lock lock(m_joystickDataMutex);
m_joystickButtons[stick].buttons = buttons;
}
void DriverStationData::SetJoystickAxisCount(int32_t stick, int32_t count) {
if (stick < 0 || stick >= kNumJoysticks) return;
std::scoped_lock lock(m_joystickDataMutex);
m_joystickAxes[stick].count = count;
m_joystickDescriptor[stick].axisCount = count;
}
void DriverStationData::SetJoystickPOVCount(int32_t stick, int32_t count) {
if (stick < 0 || stick >= kNumJoysticks) return;
std::scoped_lock lock(m_joystickDataMutex);
m_joystickPOVs[stick].count = count;
m_joystickDescriptor[stick].povCount = count;
}
void DriverStationData::SetJoystickButtonCount(int32_t stick, int32_t count) {
if (stick < 0 || stick >= kNumJoysticks) return;
std::scoped_lock lock(m_joystickDataMutex);
m_joystickButtons[stick].count = count;
m_joystickDescriptor[stick].buttonCount = count;
}
void DriverStationData::SetJoystickIsXbox(int32_t stick, HAL_Bool isXbox) {
if (stick < 0 || stick >= kNumJoysticks) return;
std::scoped_lock lock(m_joystickDataMutex);
m_joystickDescriptor[stick].isXbox = isXbox;
}
void DriverStationData::SetJoystickType(int32_t stick, int32_t type) {
if (stick < 0 || stick >= kNumJoysticks) return;
std::scoped_lock lock(m_joystickDataMutex);
m_joystickDescriptor[stick].type = type;
}
void DriverStationData::SetJoystickName(int32_t stick, const char* name) {
if (stick < 0 || stick >= kNumJoysticks) return;
std::scoped_lock lock(m_joystickDataMutex);
std::strncpy(m_joystickDescriptor[stick].name, name,
sizeof(m_joystickDescriptor[stick].name) - 1);
}
void DriverStationData::SetJoystickAxisType(int32_t stick, int32_t axis,
int32_t type) {
if (stick < 0 || stick >= kNumJoysticks) return;
if (axis < 0 || axis >= HAL_kMaxJoystickAxes) return;
std::scoped_lock lock(m_joystickDataMutex);
m_joystickDescriptor[stick].axisTypes[axis] = type;
}
void DriverStationData::SetGameSpecificMessage(const char* message) {
std::scoped_lock lock(m_matchInfoMutex);
std::strncpy(reinterpret_cast<char*>(m_matchInfo->gameSpecificMessage),
message, sizeof(m_matchInfo->gameSpecificMessage) - 1);
*(std::end(m_matchInfo->gameSpecificMessage) - 1) = '\0';
m_matchInfo->gameSpecificMessageSize = std::strlen(message);
}
void DriverStationData::SetEventName(const char* name) {
std::scoped_lock lock(m_matchInfoMutex);
std::strncpy(m_matchInfo->eventName, name,
sizeof(m_matchInfo->eventName) - 1);
*(std::end(m_matchInfo->eventName) - 1) = '\0';
}
void DriverStationData::SetMatchType(HAL_MatchType type) {
std::scoped_lock lock(m_matchInfoMutex);
m_matchInfo->matchType = type;
}
void DriverStationData::SetMatchNumber(int32_t matchNumber) {
std::scoped_lock lock(m_matchInfoMutex);
m_matchInfo->matchNumber = matchNumber;
}
void DriverStationData::SetReplayNumber(int32_t replayNumber) {
std::scoped_lock lock(m_matchInfoMutex);
m_matchInfo->replayNumber = replayNumber;
}
extern "C" {
void HALSIM_ResetDriverStationData(void) { SimDriverStationData->ResetData(); }
@@ -191,6 +314,70 @@ void HALSIM_NotifyDriverStationNewData(void) {
SimDriverStationData->NotifyNewData();
}
void HALSIM_SetJoystickButton(int32_t stick, int32_t button, HAL_Bool state) {
SimDriverStationData->SetJoystickButton(stick, button, state);
}
void HALSIM_SetJoystickAxis(int32_t stick, int32_t axis, double value) {
SimDriverStationData->SetJoystickAxis(stick, axis, value);
}
void HALSIM_SetJoystickPOV(int32_t stick, int32_t pov, int32_t value) {
SimDriverStationData->SetJoystickPOV(stick, pov, value);
}
void HALSIM_SetJoystickButtonsValue(int32_t stick, uint32_t buttons) {
SimDriverStationData->SetJoystickButtons(stick, buttons);
}
void HALSIM_SetJoystickAxisCount(int32_t stick, int32_t count) {
SimDriverStationData->SetJoystickAxisCount(stick, count);
}
void HALSIM_SetJoystickPOVCount(int32_t stick, int32_t count) {
SimDriverStationData->SetJoystickPOVCount(stick, count);
}
void HALSIM_SetJoystickButtonCount(int32_t stick, int32_t count) {
SimDriverStationData->SetJoystickButtonCount(stick, count);
}
void HALSIM_SetJoystickIsXbox(int32_t stick, HAL_Bool isXbox) {
SimDriverStationData->SetJoystickIsXbox(stick, isXbox);
}
void HALSIM_SetJoystickType(int32_t stick, int32_t type) {
SimDriverStationData->SetJoystickType(stick, type);
}
void HALSIM_SetJoystickName(int32_t stick, const char* name) {
SimDriverStationData->SetJoystickName(stick, name);
}
void HALSIM_SetJoystickAxisType(int32_t stick, int32_t axis, int32_t type) {
SimDriverStationData->SetJoystickAxisType(stick, axis, type);
}
void HALSIM_SetGameSpecificMessage(const char* message) {
SimDriverStationData->SetGameSpecificMessage(message);
}
void HALSIM_SetEventName(const char* name) {
SimDriverStationData->SetEventName(name);
}
void HALSIM_SetMatchType(HAL_MatchType type) {
SimDriverStationData->SetMatchType(type);
}
void HALSIM_SetMatchNumber(int32_t matchNumber) {
SimDriverStationData->SetMatchNumber(matchNumber);
}
void HALSIM_SetReplayNumber(int32_t replayNumber) {
SimDriverStationData->SetReplayNumber(replayNumber);
}
#define REGISTER(NAME) \
SimDriverStationData->NAME.RegisterCallback(callback, param, initialNotify)

View File

@@ -58,6 +58,25 @@ class DriverStationData {
void NotifyNewData();
void SetJoystickButton(int32_t stick, int32_t button, HAL_Bool state);
void SetJoystickAxis(int32_t stick, int32_t axis, double value);
void SetJoystickPOV(int32_t stick, int32_t pov, int32_t value);
void SetJoystickButtons(int32_t stick, uint32_t buttons);
void SetJoystickAxisCount(int32_t stick, int32_t count);
void SetJoystickPOVCount(int32_t stick, int32_t count);
void SetJoystickButtonCount(int32_t stick, int32_t count);
void SetJoystickIsXbox(int32_t stick, HAL_Bool isXbox);
void SetJoystickType(int32_t stick, int32_t type);
void SetJoystickName(int32_t stick, const char* name);
void SetJoystickAxisType(int32_t stick, int32_t axis, int32_t type);
void SetGameSpecificMessage(const char* message);
void SetEventName(const char* name);
void SetMatchType(HAL_MatchType type);
void SetMatchNumber(int32_t matchNumber);
void SetReplayNumber(int32_t replayNumber);
SimDataValue<HAL_Bool, HAL_MakeBoolean, GetEnabledName> enabled{false};
SimDataValue<HAL_Bool, HAL_MakeBoolean, GetAutonomousName> autonomous{false};
SimDataValue<HAL_Bool, HAL_MakeBoolean, GetTestName> test{false};