Adds match specific calls to Java and C++ (#720)

Uses caching, matching the joystick calls.
This commit is contained in:
Thad House
2017-11-09 19:59:29 -08:00
committed by Peter Johnson
parent 2225c4fee2
commit 7bbd13d914
9 changed files with 310 additions and 51 deletions

View File

@@ -20,6 +20,16 @@
#include "Utility.h"
#include "WPIErrors.h"
namespace frc {
struct MatchInfoData {
std::string eventName;
std::string gameSpecificMessage;
int matchNumber = 0;
int replayNumber = 0;
DriverStation::MatchType matchType = DriverStation::MatchType::kNone;
};
} // namespace frc
using namespace frc;
const double JOYSTICK_UNPLUGGED_MESSAGE_INTERVAL = 1.0;
@@ -93,7 +103,7 @@ bool DriverStation::GetStickButton(int stick, int button) {
"ERROR: Button indexes begin at 1 in WPILib for C++ and Java");
return false;
}
std::unique_lock<std::mutex> lock(m_joystickDataMutex);
std::unique_lock<std::mutex> lock(m_cacheDataMutex);
if (button > m_joystickButtons[stick].count) {
// Unlock early so error printing isn't locked.
lock.unlock();
@@ -124,7 +134,7 @@ bool DriverStation::GetStickButtonPressed(int stick, int button) {
"ERROR: Button indexes begin at 1 in WPILib for C++ and Java");
return false;
}
std::unique_lock<std::mutex> lock(m_joystickDataMutex);
std::unique_lock<std::mutex> lock(m_cacheDataMutex);
if (button > m_joystickButtons[stick].count) {
// Unlock early so error printing isn't locked.
lock.unlock();
@@ -161,7 +171,7 @@ bool DriverStation::GetStickButtonReleased(int stick, int button) {
"ERROR: Button indexes begin at 1 in WPILib for C++ and Java");
return false;
}
std::unique_lock<std::mutex> lock(m_joystickDataMutex);
std::unique_lock<std::mutex> lock(m_cacheDataMutex);
if (button > m_joystickButtons[stick].count) {
// Unlock early so error printing isn't locked.
lock.unlock();
@@ -194,10 +204,10 @@ double DriverStation::GetStickAxis(int stick, int axis) {
wpi_setWPIError(BadJoystickIndex);
return 0;
}
std::unique_lock<std::mutex> lock(m_joystickDataMutex);
std::unique_lock<std::mutex> lock(m_cacheDataMutex);
if (axis >= m_joystickAxes[stick].count) {
// Unlock early so error printing isn't locked.
m_joystickDataMutex.unlock();
m_cacheDataMutex.unlock();
lock.release();
if (axis >= HAL_kMaxJoystickAxes)
wpi_setWPIError(BadJoystickAxis);
@@ -220,7 +230,7 @@ int DriverStation::GetStickPOV(int stick, int pov) {
wpi_setWPIError(BadJoystickIndex);
return -1;
}
std::unique_lock<std::mutex> lock(m_joystickDataMutex);
std::unique_lock<std::mutex> lock(m_cacheDataMutex);
if (pov >= m_joystickPOVs[stick].count) {
// Unlock early so error printing isn't locked.
lock.unlock();
@@ -246,7 +256,7 @@ int DriverStation::GetStickButtons(int stick) const {
wpi_setWPIError(BadJoystickIndex);
return 0;
}
std::lock_guard<std::mutex> lock(m_joystickDataMutex);
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
return m_joystickButtons[stick].buttons;
}
@@ -261,7 +271,7 @@ int DriverStation::GetStickAxisCount(int stick) const {
wpi_setWPIError(BadJoystickIndex);
return 0;
}
std::lock_guard<std::mutex> lock(m_joystickDataMutex);
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
return m_joystickAxes[stick].count;
}
@@ -276,7 +286,7 @@ int DriverStation::GetStickPOVCount(int stick) const {
wpi_setWPIError(BadJoystickIndex);
return 0;
}
std::lock_guard<std::mutex> lock(m_joystickDataMutex);
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
return m_joystickPOVs[stick].count;
}
@@ -291,7 +301,7 @@ int DriverStation::GetStickButtonCount(int stick) const {
wpi_setWPIError(BadJoystickIndex);
return 0;
}
std::lock_guard<std::mutex> lock(m_joystickDataMutex);
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
return m_joystickButtons[stick].count;
}
@@ -306,7 +316,7 @@ bool DriverStation::GetJoystickIsXbox(int stick) const {
wpi_setWPIError(BadJoystickIndex);
return false;
}
std::lock_guard<std::mutex> lock(m_joystickDataMutex);
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
return static_cast<bool>(m_joystickDescriptor[stick].isXbox);
}
@@ -321,7 +331,7 @@ int DriverStation::GetJoystickType(int stick) const {
wpi_setWPIError(BadJoystickIndex);
return -1;
}
std::lock_guard<std::mutex> lock(m_joystickDataMutex);
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
return static_cast<int>(m_joystickDescriptor[stick].type);
}
@@ -335,7 +345,7 @@ std::string DriverStation::GetJoystickName(int stick) const {
if (stick >= kJoystickPorts) {
wpi_setWPIError(BadJoystickIndex);
}
std::lock_guard<std::mutex> lock(m_joystickDataMutex);
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
std::string retVal(m_joystickDescriptor[stick].name);
return retVal;
}
@@ -351,7 +361,7 @@ int DriverStation::GetJoystickAxisType(int stick, int axis) const {
wpi_setWPIError(BadJoystickIndex);
return -1;
}
std::lock_guard<std::mutex> lock(m_joystickDataMutex);
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
return m_joystickDescriptor[stick].axisTypes[axis];
}
@@ -471,6 +481,31 @@ bool DriverStation::IsBrownedOut() const {
return retVal;
}
std::string DriverStation::GetGameSpecificMessage() const {
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
return m_matchInfo->gameSpecificMessage;
}
std::string DriverStation::GetEventName() const {
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
return m_matchInfo->eventName;
}
DriverStation::MatchType DriverStation::GetMatchType() const {
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
return m_matchInfo->matchType;
}
int DriverStation::GetMatchNumber() const {
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
return m_matchInfo->matchNumber;
}
int DriverStation::GetReplayNumber() const {
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
return m_matchInfo->replayNumber;
}
/**
* Return the alliance that the driver station says it is on.
*
@@ -597,12 +632,25 @@ void DriverStation::GetData() {
HAL_GetJoystickButtons(stick, &m_joystickButtonsCache[stick]);
HAL_GetJoystickDescriptor(stick, &m_joystickDescriptorCache[stick]);
}
// Grab match specific data
HAL_MatchInfo matchInfo;
auto status = HAL_GetMatchInfo(&matchInfo);
if (status == 0) {
m_matchInfoCache->eventName = matchInfo.eventName;
m_matchInfoCache->matchNumber = matchInfo.matchNumber;
m_matchInfoCache->replayNumber = matchInfo.replayNumber;
m_matchInfoCache->matchType =
static_cast<DriverStation::MatchType>(matchInfo.matchType);
m_matchInfoCache->gameSpecificMessage = matchInfo.gameSpecificMessage;
}
HAL_FreeMatchInfo(&matchInfo);
// Force a control word update, to make sure the data is the newest.
HAL_ControlWord controlWord;
UpdateControlWord(true, controlWord);
// Obtain a write lock on the data, swap the cached data into the
// main data arrays
std::lock_guard<std::mutex> lock(m_joystickDataMutex);
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
for (int32_t i = 0; i < kJoystickPorts; i++) {
// If buttons weren't pressed and are now, set flags in m_buttonsPressed
@@ -618,6 +666,7 @@ void DriverStation::GetData() {
m_joystickPOVs.swap(m_joystickPOVsCache);
m_joystickButtons.swap(m_joystickButtonsCache);
m_joystickDescriptor.swap(m_joystickDescriptorCache);
m_matchInfo.swap(m_matchInfoCache);
}
/**
@@ -631,12 +680,14 @@ DriverStation::DriverStation() {
m_joystickButtons = std::make_unique<HAL_JoystickButtons[]>(kJoystickPorts);
m_joystickDescriptor =
std::make_unique<HAL_JoystickDescriptor[]>(kJoystickPorts);
m_matchInfo = std::make_unique<MatchInfoData>();
m_joystickAxesCache = std::make_unique<HAL_JoystickAxes[]>(kJoystickPorts);
m_joystickPOVsCache = std::make_unique<HAL_JoystickPOVs[]>(kJoystickPorts);
m_joystickButtonsCache =
std::make_unique<HAL_JoystickButtons[]>(kJoystickPorts);
m_joystickDescriptorCache =
std::make_unique<HAL_JoystickDescriptor[]>(kJoystickPorts);
m_matchInfoCache = std::make_unique<MatchInfoData>();
// All joysticks should default to having zero axes, povs and buttons, so
// uninitialized memory doesn't get sent to speed controllers.