2016-05-25 20:23:37 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:20:21 -08:00
|
|
|
/* Copyright (c) 2016-2018 FIRST. All Rights Reserved. */
|
2016-05-25 20:23:37 -07:00
|
|
|
/* 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. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2017-05-08 20:21:47 -07:00
|
|
|
#include <atomic>
|
2016-12-02 00:32:01 -08:00
|
|
|
#include <chrono>
|
2016-07-10 23:10:05 -07:00
|
|
|
#include <cstdlib>
|
2015-11-15 14:49:50 -08:00
|
|
|
#include <cstring>
|
2016-07-12 10:45:14 -07:00
|
|
|
#include <limits>
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2017-08-27 00:11:52 -07:00
|
|
|
#include <FRC_NetworkCommunication/FRCComm.h>
|
2017-11-08 23:41:16 -08:00
|
|
|
#include <FRC_NetworkCommunication/NetCommRPCProxy_Occur.h>
|
2018-07-18 22:22:41 -07:00
|
|
|
#include <wpi/SafeThread.h>
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/condition_variable.h>
|
|
|
|
|
#include <wpi/mutex.h>
|
|
|
|
|
#include <wpi/raw_ostream.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "hal/DriverStation.h"
|
2015-08-19 11:12:54 -04:00
|
|
|
|
2016-09-06 19:39:28 -07:00
|
|
|
static_assert(sizeof(int32_t) >= sizeof(int),
|
|
|
|
|
"FRC_NetworkComm status variable is larger than 32 bits");
|
|
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
struct HAL_JoystickAxesInt {
|
2016-09-06 19:39:28 -07:00
|
|
|
int16_t count;
|
2016-07-09 00:24:26 -07:00
|
|
|
int16_t axes[HAL_kMaxJoystickAxes];
|
2016-05-21 01:42:16 -07:00
|
|
|
};
|
|
|
|
|
|
2018-07-18 22:22:41 -07:00
|
|
|
static constexpr int kJoystickPorts = 6;
|
|
|
|
|
|
|
|
|
|
// Joystick User Data
|
|
|
|
|
static std::unique_ptr<HAL_JoystickAxes[]> m_joystickAxes;
|
|
|
|
|
static std::unique_ptr<HAL_JoystickPOVs[]> m_joystickPOVs;
|
|
|
|
|
static std::unique_ptr<HAL_JoystickButtons[]> m_joystickButtons;
|
|
|
|
|
static std::unique_ptr<HAL_JoystickDescriptor[]> m_joystickDescriptor;
|
|
|
|
|
static std::unique_ptr<HAL_MatchInfo> m_matchInfo;
|
|
|
|
|
|
|
|
|
|
// Joystick Cached Data
|
|
|
|
|
static std::unique_ptr<HAL_JoystickAxes[]> m_joystickAxesCache;
|
|
|
|
|
static std::unique_ptr<HAL_JoystickPOVs[]> m_joystickPOVsCache;
|
|
|
|
|
static std::unique_ptr<HAL_JoystickButtons[]> m_joystickButtonsCache;
|
|
|
|
|
static std::unique_ptr<HAL_JoystickDescriptor[]> m_joystickDescriptorCache;
|
|
|
|
|
static std::unique_ptr<HAL_MatchInfo> m_matchInfoCache;
|
|
|
|
|
|
|
|
|
|
static wpi::mutex m_cacheDataMutex;
|
|
|
|
|
|
|
|
|
|
// Control word variables
|
|
|
|
|
static HAL_ControlWord m_controlWordCache;
|
|
|
|
|
static std::chrono::steady_clock::time_point m_lastControlWordUpdate;
|
|
|
|
|
static wpi::mutex m_controlWordMutex;
|
|
|
|
|
|
|
|
|
|
// Message and Data variables
|
2017-11-13 09:51:48 -08:00
|
|
|
static wpi::mutex msgMutex;
|
2016-07-17 19:42:33 -07:00
|
|
|
|
2018-07-18 22:22:41 -07:00
|
|
|
static void InitializeDriverStationCaches() {
|
|
|
|
|
m_joystickAxes = std::make_unique<HAL_JoystickAxes[]>(kJoystickPorts);
|
|
|
|
|
m_joystickPOVs = std::make_unique<HAL_JoystickPOVs[]>(kJoystickPorts);
|
|
|
|
|
m_joystickButtons = std::make_unique<HAL_JoystickButtons[]>(kJoystickPorts);
|
|
|
|
|
m_joystickDescriptor =
|
|
|
|
|
std::make_unique<HAL_JoystickDescriptor[]>(kJoystickPorts);
|
|
|
|
|
m_matchInfo = std::make_unique<HAL_MatchInfo>();
|
|
|
|
|
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<HAL_MatchInfo>();
|
|
|
|
|
|
|
|
|
|
// All joysticks should default to having zero axes, povs and buttons, so
|
|
|
|
|
// uninitialized memory doesn't get sent to speed controllers.
|
|
|
|
|
for (unsigned int i = 0; i < kJoystickPorts; i++) {
|
|
|
|
|
m_joystickAxes[i].count = 0;
|
|
|
|
|
m_joystickPOVs[i].count = 0;
|
|
|
|
|
m_joystickButtons[i].count = 0;
|
|
|
|
|
m_joystickDescriptor[i].isXbox = 0;
|
|
|
|
|
m_joystickDescriptor[i].type = -1;
|
|
|
|
|
m_joystickDescriptor[i].name[0] = '\0';
|
|
|
|
|
|
|
|
|
|
m_joystickAxesCache[i].count = 0;
|
|
|
|
|
m_joystickPOVsCache[i].count = 0;
|
|
|
|
|
m_joystickButtonsCache[i].count = 0;
|
|
|
|
|
m_joystickDescriptorCache[i].isXbox = 0;
|
|
|
|
|
m_joystickDescriptorCache[i].type = -1;
|
|
|
|
|
m_joystickDescriptorCache[i].name[0] = '\0';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int32_t HAL_GetJoystickAxesInternal(int32_t joystickNum,
|
|
|
|
|
HAL_JoystickAxes* axes) {
|
|
|
|
|
HAL_JoystickAxesInt axesInt;
|
|
|
|
|
|
|
|
|
|
int retVal = FRC_NetworkCommunication_getJoystickAxes(
|
|
|
|
|
joystickNum, reinterpret_cast<JoystickAxes_t*>(&axesInt),
|
|
|
|
|
HAL_kMaxJoystickAxes);
|
|
|
|
|
|
|
|
|
|
// copy integer values to double values
|
|
|
|
|
axes->count = axesInt.count;
|
|
|
|
|
// current scaling is -128 to 127, can easily be patched in the future by
|
|
|
|
|
// changing this function.
|
|
|
|
|
for (int32_t i = 0; i < axesInt.count; i++) {
|
|
|
|
|
int8_t value = axesInt.axes[i];
|
|
|
|
|
if (value < 0) {
|
|
|
|
|
axes->axes[i] = value / 128.0;
|
|
|
|
|
} else {
|
|
|
|
|
axes->axes[i] = value / 127.0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int32_t HAL_GetJoystickPOVsInternal(int32_t joystickNum,
|
|
|
|
|
HAL_JoystickPOVs* povs) {
|
|
|
|
|
return FRC_NetworkCommunication_getJoystickPOVs(
|
|
|
|
|
joystickNum, reinterpret_cast<JoystickPOV_t*>(povs),
|
|
|
|
|
HAL_kMaxJoystickPOVs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int32_t HAL_GetJoystickButtonsInternal(int32_t joystickNum,
|
|
|
|
|
HAL_JoystickButtons* buttons) {
|
|
|
|
|
return FRC_NetworkCommunication_getJoystickButtons(
|
|
|
|
|
joystickNum, &buttons->buttons, &buttons->count);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Retrieve the Joystick Descriptor for particular slot
|
|
|
|
|
* @param desc [out] descriptor (data transfer object) to fill in. desc is
|
|
|
|
|
* filled in regardless of success. In other words, if descriptor is not
|
|
|
|
|
* available, desc is filled in with default values matching the init-values in
|
|
|
|
|
* Java and C++ Driverstation for when caller requests a too-large joystick
|
|
|
|
|
* index.
|
|
|
|
|
*
|
|
|
|
|
* @return error code reported from Network Comm back-end. Zero is good,
|
|
|
|
|
* nonzero is bad.
|
|
|
|
|
*/
|
|
|
|
|
static int32_t HAL_GetJoystickDescriptorInternal(int32_t joystickNum,
|
|
|
|
|
HAL_JoystickDescriptor* desc) {
|
|
|
|
|
desc->isXbox = 0;
|
|
|
|
|
desc->type = std::numeric_limits<uint8_t>::max();
|
|
|
|
|
desc->name[0] = '\0';
|
|
|
|
|
desc->axisCount =
|
|
|
|
|
HAL_kMaxJoystickAxes; /* set to the desc->axisTypes's capacity */
|
|
|
|
|
desc->buttonCount = 0;
|
|
|
|
|
desc->povCount = 0;
|
|
|
|
|
int retval = FRC_NetworkCommunication_getJoystickDesc(
|
|
|
|
|
joystickNum, &desc->isXbox, &desc->type,
|
|
|
|
|
reinterpret_cast<char*>(&desc->name), &desc->axisCount,
|
|
|
|
|
reinterpret_cast<uint8_t*>(&desc->axisTypes), &desc->buttonCount,
|
|
|
|
|
&desc->povCount);
|
|
|
|
|
/* check the return, if there is an error and the RIOimage predates FRC2017,
|
|
|
|
|
* then axisCount needs to be cleared */
|
|
|
|
|
if (retval != 0) {
|
|
|
|
|
/* set count to zero so downstream code doesn't decode invalid axisTypes. */
|
|
|
|
|
desc->axisCount = 0;
|
|
|
|
|
}
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int32_t HAL_GetControlWordInternal(HAL_ControlWord* controlWord) {
|
|
|
|
|
std::memset(controlWord, 0, sizeof(HAL_ControlWord));
|
|
|
|
|
return FRC_NetworkCommunication_getControlWord(
|
|
|
|
|
reinterpret_cast<ControlWord_t*>(controlWord));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int32_t HAL_GetMatchInfoInternal(HAL_MatchInfo* info) {
|
|
|
|
|
MatchType_t matchType = MatchType_t::kMatchType_none;
|
|
|
|
|
int status = FRC_NetworkCommunication_getMatchInfo(
|
|
|
|
|
info->eventName, &matchType, &info->matchNumber, &info->replayNumber,
|
|
|
|
|
info->gameSpecificMessage, &info->gameSpecificMessageSize);
|
|
|
|
|
|
|
|
|
|
info->matchType = static_cast<HAL_MatchType>(matchType);
|
|
|
|
|
|
|
|
|
|
*(std::end(info->eventName) - 1) = '\0';
|
|
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void UpdateDriverStationControlWord(bool force,
|
|
|
|
|
HAL_ControlWord& controlWord) {
|
|
|
|
|
auto now = std::chrono::steady_clock::now();
|
|
|
|
|
std::lock_guard<wpi::mutex> lock(m_controlWordMutex);
|
|
|
|
|
// Update every 50 ms or on force.
|
|
|
|
|
if ((now - m_lastControlWordUpdate > std::chrono::milliseconds(50)) ||
|
|
|
|
|
force) {
|
|
|
|
|
HAL_GetControlWordInternal(&m_controlWordCache);
|
|
|
|
|
m_lastControlWordUpdate = now;
|
|
|
|
|
}
|
|
|
|
|
controlWord = m_controlWordCache;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void UpdateDriverStationDataCaches() {
|
|
|
|
|
// Get the status of all of the joysticks, and save to the cache
|
|
|
|
|
for (uint8_t stick = 0; stick < kJoystickPorts; stick++) {
|
|
|
|
|
HAL_GetJoystickAxesInternal(stick, &m_joystickAxesCache[stick]);
|
|
|
|
|
HAL_GetJoystickPOVsInternal(stick, &m_joystickPOVsCache[stick]);
|
|
|
|
|
HAL_GetJoystickButtonsInternal(stick, &m_joystickButtonsCache[stick]);
|
|
|
|
|
HAL_GetJoystickDescriptorInternal(stick, &m_joystickDescriptorCache[stick]);
|
|
|
|
|
}
|
|
|
|
|
// Grab match specific data
|
|
|
|
|
HAL_GetMatchInfoInternal(m_matchInfoCache.get());
|
|
|
|
|
|
|
|
|
|
// Force a control word update, to make sure the data is the newest.
|
|
|
|
|
HAL_ControlWord controlWord;
|
|
|
|
|
UpdateDriverStationControlWord(true, controlWord);
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
// Obtain a lock on the data, swap the cached data into the main data arrays
|
|
|
|
|
std::lock_guard<wpi::mutex> lock(m_cacheDataMutex);
|
|
|
|
|
|
|
|
|
|
m_joystickAxes.swap(m_joystickAxesCache);
|
|
|
|
|
m_joystickPOVs.swap(m_joystickPOVsCache);
|
|
|
|
|
m_joystickButtons.swap(m_joystickButtonsCache);
|
|
|
|
|
m_joystickDescriptor.swap(m_joystickDescriptorCache);
|
|
|
|
|
m_matchInfo.swap(m_matchInfoCache);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DriverStationThread : public wpi::SafeThread {
|
|
|
|
|
public:
|
|
|
|
|
void Main() {
|
|
|
|
|
std::unique_lock<wpi::mutex> lock(m_mutex);
|
|
|
|
|
while (m_active) {
|
|
|
|
|
m_cond.wait(lock, [&] { return !m_active || m_notify; });
|
|
|
|
|
if (!m_active) break;
|
|
|
|
|
m_notify = false;
|
|
|
|
|
|
2018-10-30 11:51:17 -07:00
|
|
|
lock.unlock();
|
2018-07-18 22:22:41 -07:00
|
|
|
UpdateDriverStationDataCaches();
|
2018-10-30 11:51:17 -07:00
|
|
|
lock.lock();
|
2018-07-18 22:22:41 -07:00
|
|
|
|
2018-10-30 11:51:17 -07:00
|
|
|
// Notify all threads
|
2018-07-18 22:22:41 -07:00
|
|
|
newDSDataAvailableCounter++;
|
2018-10-30 11:51:17 -07:00
|
|
|
newDSDataAvailableCond.notify_all();
|
2018-07-18 22:22:41 -07:00
|
|
|
}
|
2018-10-30 11:51:17 -07:00
|
|
|
|
|
|
|
|
// Notify waiters on thread exit
|
|
|
|
|
newDSDataAvailableCounter++;
|
|
|
|
|
newDSDataAvailableCond.notify_all();
|
2018-07-18 22:22:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool m_notify = false;
|
2018-10-30 11:51:17 -07:00
|
|
|
wpi::condition_variable newDSDataAvailableCond;
|
|
|
|
|
int newDSDataAvailableCounter{0};
|
2018-07-18 22:22:41 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class DriverStationThreadOwner
|
|
|
|
|
: public wpi::SafeThreadOwner<DriverStationThread> {
|
|
|
|
|
public:
|
|
|
|
|
void Notify() {
|
|
|
|
|
auto thr = GetThread();
|
|
|
|
|
if (!thr) return;
|
|
|
|
|
thr->m_notify = true;
|
|
|
|
|
thr->m_cond.notify_one();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static std::unique_ptr<DriverStationThreadOwner> dsThread = nullptr;
|
|
|
|
|
|
2017-12-10 18:02:07 -08:00
|
|
|
namespace hal {
|
|
|
|
|
namespace init {
|
2018-10-30 11:51:17 -07:00
|
|
|
void InitializeFRCDriverStation() {}
|
2017-12-10 18:02:07 -08:00
|
|
|
} // namespace init
|
|
|
|
|
} // namespace hal
|
|
|
|
|
|
2015-11-26 00:08:32 -08:00
|
|
|
extern "C" {
|
2017-08-23 22:07:46 -07:00
|
|
|
|
2016-07-17 19:42:33 -07:00
|
|
|
int32_t HAL_SendError(HAL_Bool isError, int32_t errorCode, HAL_Bool isLVCode,
|
|
|
|
|
const char* details, const char* location,
|
|
|
|
|
const char* callStack, HAL_Bool printMsg) {
|
|
|
|
|
// Avoid flooding console by keeping track of previous 5 error
|
|
|
|
|
// messages and only printing again if they're longer than 1 second old.
|
|
|
|
|
static constexpr int KEEP_MSGS = 5;
|
2017-11-13 09:51:48 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(msgMutex);
|
2016-08-12 13:45:28 -07:00
|
|
|
static std::string prevMsg[KEEP_MSGS];
|
2016-12-02 00:32:01 -08:00
|
|
|
static std::chrono::time_point<std::chrono::steady_clock>
|
|
|
|
|
prevMsgTime[KEEP_MSGS];
|
|
|
|
|
static bool initialized = false;
|
|
|
|
|
if (!initialized) {
|
|
|
|
|
for (int i = 0; i < KEEP_MSGS; i++) {
|
|
|
|
|
prevMsgTime[i] =
|
|
|
|
|
std::chrono::steady_clock::now() - std::chrono::seconds(2);
|
|
|
|
|
}
|
|
|
|
|
initialized = true;
|
|
|
|
|
}
|
2016-07-17 19:42:33 -07:00
|
|
|
|
2016-12-02 00:32:01 -08:00
|
|
|
auto curTime = std::chrono::steady_clock::now();
|
2016-07-17 19:42:33 -07:00
|
|
|
int i;
|
|
|
|
|
for (i = 0; i < KEEP_MSGS; ++i) {
|
2016-08-12 13:45:28 -07:00
|
|
|
if (prevMsg[i] == details) break;
|
2016-07-17 19:42:33 -07:00
|
|
|
}
|
|
|
|
|
int retval = 0;
|
2016-12-02 00:32:01 -08:00
|
|
|
if (i == KEEP_MSGS || (curTime - prevMsgTime[i]) >= std::chrono::seconds(1)) {
|
2016-07-17 19:42:33 -07:00
|
|
|
retval = FRC_NetworkCommunication_sendError(isError, errorCode, isLVCode,
|
|
|
|
|
details, location, callStack);
|
|
|
|
|
if (printMsg) {
|
|
|
|
|
if (location && location[0] != '\0') {
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::errs() << (isError ? "Error" : "Warning") << " at " << location
|
|
|
|
|
<< ": ";
|
2016-07-17 19:42:33 -07:00
|
|
|
}
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::errs() << details << "\n";
|
2016-07-17 19:42:33 -07:00
|
|
|
if (callStack && callStack[0] != '\0') {
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::errs() << callStack << "\n";
|
2016-07-17 19:42:33 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (i == KEEP_MSGS) {
|
|
|
|
|
// replace the oldest one
|
|
|
|
|
i = 0;
|
2016-12-02 00:32:01 -08:00
|
|
|
auto first = prevMsgTime[0];
|
2016-07-17 19:42:33 -07:00
|
|
|
for (int j = 1; j < KEEP_MSGS; ++j) {
|
2016-08-12 13:45:28 -07:00
|
|
|
if (prevMsgTime[j] < first) {
|
|
|
|
|
first = prevMsgTime[j];
|
2016-07-17 19:42:33 -07:00
|
|
|
i = j;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-12 13:45:28 -07:00
|
|
|
prevMsg[i] = details;
|
2016-07-17 19:42:33 -07:00
|
|
|
}
|
2016-08-12 13:45:28 -07:00
|
|
|
prevMsgTime[i] = curTime;
|
2016-07-17 19:42:33 -07:00
|
|
|
}
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
2015-11-26 00:08:32 -08:00
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t HAL_GetControlWord(HAL_ControlWord* controlWord) {
|
2016-07-10 16:24:57 -07:00
|
|
|
std::memset(controlWord, 0, sizeof(HAL_ControlWord));
|
2018-07-18 22:22:41 -07:00
|
|
|
UpdateDriverStationControlWord(false, *controlWord);
|
|
|
|
|
return 0;
|
2015-08-19 11:12:54 -04:00
|
|
|
}
|
|
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t HAL_GetJoystickAxes(int32_t joystickNum, HAL_JoystickAxes* axes) {
|
2018-07-18 22:22:41 -07:00
|
|
|
std::unique_lock<wpi::mutex> lock(m_cacheDataMutex);
|
|
|
|
|
*axes = m_joystickAxes[joystickNum];
|
|
|
|
|
return 0;
|
2015-08-19 11:12:54 -04:00
|
|
|
}
|
|
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t HAL_GetJoystickPOVs(int32_t joystickNum, HAL_JoystickPOVs* povs) {
|
2018-07-18 22:22:41 -07:00
|
|
|
std::unique_lock<wpi::mutex> lock(m_cacheDataMutex);
|
|
|
|
|
*povs = m_joystickPOVs[joystickNum];
|
|
|
|
|
return 0;
|
2015-08-19 11:12:54 -04:00
|
|
|
}
|
|
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t HAL_GetJoystickButtons(int32_t joystickNum,
|
|
|
|
|
HAL_JoystickButtons* buttons) {
|
2018-07-18 22:22:41 -07:00
|
|
|
std::unique_lock<wpi::mutex> lock(m_cacheDataMutex);
|
|
|
|
|
*buttons = m_joystickButtons[joystickNum];
|
|
|
|
|
return 0;
|
2015-08-19 11:12:54 -04:00
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t HAL_GetJoystickDescriptor(int32_t joystickNum,
|
|
|
|
|
HAL_JoystickDescriptor* desc) {
|
2018-07-18 22:22:41 -07:00
|
|
|
std::unique_lock<wpi::mutex> lock(m_cacheDataMutex);
|
|
|
|
|
*desc = m_joystickDescriptor[joystickNum];
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t HAL_GetMatchInfo(HAL_MatchInfo* info) {
|
|
|
|
|
std::unique_lock<wpi::mutex> lock(m_cacheDataMutex);
|
|
|
|
|
*info = *m_matchInfo;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HAL_AllianceStationID HAL_GetAllianceStation(int32_t* status) {
|
|
|
|
|
HAL_AllianceStationID allianceStation;
|
|
|
|
|
*status = FRC_NetworkCommunication_getAllianceStation(
|
|
|
|
|
reinterpret_cast<AllianceStationID_t*>(&allianceStation));
|
|
|
|
|
return allianceStation;
|
2016-05-20 17:30:37 -07:00
|
|
|
}
|
|
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
HAL_Bool HAL_GetJoystickIsXbox(int32_t joystickNum) {
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_JoystickDescriptor joystickDesc;
|
|
|
|
|
if (HAL_GetJoystickDescriptor(joystickNum, &joystickDesc) < 0) {
|
2016-05-20 17:30:37 -07:00
|
|
|
return 0;
|
|
|
|
|
} else {
|
|
|
|
|
return joystickDesc.isXbox;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t HAL_GetJoystickType(int32_t joystickNum) {
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_JoystickDescriptor joystickDesc;
|
|
|
|
|
if (HAL_GetJoystickDescriptor(joystickNum, &joystickDesc) < 0) {
|
2016-05-20 17:30:37 -07:00
|
|
|
return -1;
|
|
|
|
|
} else {
|
|
|
|
|
return joystickDesc.type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
char* HAL_GetJoystickName(int32_t joystickNum) {
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_JoystickDescriptor joystickDesc;
|
|
|
|
|
if (HAL_GetJoystickDescriptor(joystickNum, &joystickDesc) < 0) {
|
2016-07-10 17:47:44 -07:00
|
|
|
char* name = static_cast<char*>(std::malloc(1));
|
2016-05-20 17:30:37 -07:00
|
|
|
name[0] = '\0';
|
|
|
|
|
return name;
|
|
|
|
|
} else {
|
|
|
|
|
size_t len = std::strlen(joystickDesc.name);
|
2016-07-10 17:47:44 -07:00
|
|
|
char* name = static_cast<char*>(std::malloc(len + 1));
|
|
|
|
|
std::strncpy(name, joystickDesc.name, len);
|
|
|
|
|
name[len] = '\0';
|
2016-05-20 17:30:37 -07:00
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-30 16:11:16 -07:00
|
|
|
void HAL_FreeJoystickName(char* name) { std::free(name); }
|
|
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t HAL_GetJoystickAxisType(int32_t joystickNum, int32_t axis) {
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_JoystickDescriptor joystickDesc;
|
|
|
|
|
if (HAL_GetJoystickDescriptor(joystickNum, &joystickDesc) < 0) {
|
2016-05-20 17:30:37 -07:00
|
|
|
return -1;
|
|
|
|
|
} else {
|
|
|
|
|
return joystickDesc.axisTypes[axis];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t HAL_SetJoystickOutputs(int32_t joystickNum, int64_t outputs,
|
|
|
|
|
int32_t leftRumble, int32_t rightRumble) {
|
2016-05-20 17:30:37 -07:00
|
|
|
return FRC_NetworkCommunication_setJoystickOutputs(joystickNum, outputs,
|
|
|
|
|
leftRumble, rightRumble);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-13 19:25:57 -07:00
|
|
|
double HAL_GetMatchTime(int32_t* status) {
|
2016-07-10 16:24:57 -07:00
|
|
|
float matchTime;
|
|
|
|
|
*status = FRC_NetworkCommunication_getMatchTime(&matchTime);
|
|
|
|
|
return matchTime;
|
2016-05-20 17:30:37 -07:00
|
|
|
}
|
|
|
|
|
|
2016-07-10 16:24:57 -07:00
|
|
|
void HAL_ObserveUserProgramStarting(void) {
|
2016-05-20 17:30:37 -07:00
|
|
|
FRC_NetworkCommunication_observeUserProgramStarting();
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-10 16:24:57 -07:00
|
|
|
void HAL_ObserveUserProgramDisabled(void) {
|
2016-05-20 17:30:37 -07:00
|
|
|
FRC_NetworkCommunication_observeUserProgramDisabled();
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-10 16:24:57 -07:00
|
|
|
void HAL_ObserveUserProgramAutonomous(void) {
|
2016-05-20 17:30:37 -07:00
|
|
|
FRC_NetworkCommunication_observeUserProgramAutonomous();
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-10 16:24:57 -07:00
|
|
|
void HAL_ObserveUserProgramTeleop(void) {
|
2016-05-20 17:30:37 -07:00
|
|
|
FRC_NetworkCommunication_observeUserProgramTeleop();
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-10 16:24:57 -07:00
|
|
|
void HAL_ObserveUserProgramTest(void) {
|
2016-05-20 17:30:37 -07:00
|
|
|
FRC_NetworkCommunication_observeUserProgramTest();
|
2015-08-19 11:12:54 -04:00
|
|
|
}
|
2015-11-26 00:08:32 -08:00
|
|
|
|
2018-07-08 15:18:03 +10:00
|
|
|
HAL_Bool HAL_IsNewControlData(void) {
|
2017-05-08 20:21:47 -07:00
|
|
|
// There is a rollover error condition here. At Packet# = n * (uintmax), this
|
|
|
|
|
// will return false when instead it should return true. However, this at a
|
|
|
|
|
// 20ms rate occurs once every 2.7 years of DS connected runtime, so not
|
|
|
|
|
// worth the cycles to check.
|
|
|
|
|
thread_local int lastCount{-1};
|
2018-10-30 11:51:17 -07:00
|
|
|
if (!dsThread) return false;
|
|
|
|
|
auto thr = dsThread->GetThread();
|
|
|
|
|
if (!thr) return false;
|
|
|
|
|
int currentCount = thr->newDSDataAvailableCounter;
|
2017-05-08 20:21:47 -07:00
|
|
|
if (lastCount == currentCount) return false;
|
|
|
|
|
lastCount = currentCount;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-18 22:22:41 -07:00
|
|
|
/**
|
|
|
|
|
* Waits for the newest DS packet to arrive. Note that this is a blocking call.
|
|
|
|
|
*/
|
2017-05-08 20:21:47 -07:00
|
|
|
void HAL_WaitForDSData(void) { HAL_WaitForDSDataTimeout(0); }
|
|
|
|
|
|
2018-07-18 22:22:41 -07:00
|
|
|
/**
|
|
|
|
|
* Waits for the newest DS packet to arrive. If timeout is <= 0, this will wait
|
|
|
|
|
* forever. Otherwise, it will wait until either a new packet, or the timeout
|
|
|
|
|
* time has passed. Returns true on new data, false on timeout.
|
|
|
|
|
*/
|
2017-05-08 20:21:47 -07:00
|
|
|
HAL_Bool HAL_WaitForDSDataTimeout(double timeout) {
|
|
|
|
|
auto timeoutTime =
|
|
|
|
|
std::chrono::steady_clock::now() + std::chrono::duration<double>(timeout);
|
|
|
|
|
|
2018-10-30 11:51:17 -07:00
|
|
|
if (!dsThread) return false;
|
|
|
|
|
auto thr = dsThread->GetThread();
|
|
|
|
|
if (!thr) return false;
|
|
|
|
|
int currentCount = thr->newDSDataAvailableCounter;
|
|
|
|
|
while (thr->newDSDataAvailableCounter == currentCount) {
|
2017-05-08 20:21:47 -07:00
|
|
|
if (timeout > 0) {
|
2018-10-30 11:51:17 -07:00
|
|
|
auto timedOut =
|
|
|
|
|
thr->newDSDataAvailableCond.wait_until(thr.GetLock(), timeoutTime);
|
2017-05-08 20:21:47 -07:00
|
|
|
if (timedOut == std::cv_status::timeout) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2018-10-30 11:51:17 -07:00
|
|
|
thr->newDSDataAvailableCond.wait(thr.GetLock());
|
2017-05-08 20:21:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2016-07-17 19:42:33 -07:00
|
|
|
}
|
|
|
|
|
|
2017-05-08 20:21:47 -07:00
|
|
|
// Constant number to be used for our occur handle
|
|
|
|
|
constexpr int32_t refNumber = 42;
|
|
|
|
|
|
2017-11-08 23:41:16 -08:00
|
|
|
static void newDataOccur(uint32_t refNum) {
|
2017-05-08 20:21:47 -07:00
|
|
|
// Since we could get other values, require our specific handle
|
|
|
|
|
// to signal our threads
|
2017-11-08 23:41:16 -08:00
|
|
|
if (refNum != refNumber) return;
|
2018-07-18 22:22:41 -07:00
|
|
|
dsThread->Notify();
|
2017-05-08 20:21:47 -07:00
|
|
|
}
|
|
|
|
|
|
2018-07-18 22:22:41 -07:00
|
|
|
/*
|
|
|
|
|
* Call this to initialize the driver station communication. This will properly
|
|
|
|
|
* handle multiple calls. However note that this CANNOT be called from a library
|
|
|
|
|
* that interfaces with LabVIEW.
|
|
|
|
|
*/
|
2016-07-17 19:42:33 -07:00
|
|
|
void HAL_InitializeDriverStation(void) {
|
2017-05-08 20:21:47 -07:00
|
|
|
static std::atomic_bool initialized{false};
|
2017-11-13 09:51:48 -08:00
|
|
|
static wpi::mutex initializeMutex;
|
2017-05-08 20:21:47 -07:00
|
|
|
// Initial check, as if it's true initialization has finished
|
|
|
|
|
if (initialized) return;
|
|
|
|
|
|
2017-11-13 09:51:48 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(initializeMutex);
|
2017-05-08 20:21:47 -07:00
|
|
|
// Second check in case another thread was waiting
|
|
|
|
|
if (initialized) return;
|
|
|
|
|
|
2018-07-18 22:22:41 -07:00
|
|
|
InitializeDriverStationCaches();
|
|
|
|
|
|
|
|
|
|
dsThread = std::make_unique<DriverStationThreadOwner>();
|
|
|
|
|
dsThread->Start();
|
|
|
|
|
|
2017-05-08 20:21:47 -07:00
|
|
|
// Set up the occur function internally with NetComm
|
|
|
|
|
NetCommRPCProxy_SetOccurFuncPointer(newDataOccur);
|
|
|
|
|
// Set up our occur reference number
|
|
|
|
|
setNewDataOccurRef(refNumber);
|
|
|
|
|
|
|
|
|
|
initialized = true;
|
2016-07-17 19:42:33 -07:00
|
|
|
}
|
|
|
|
|
|
2018-07-18 22:22:41 -07:00
|
|
|
/*
|
|
|
|
|
* Releases the DS Mutex to allow proper shutdown of any threads that are
|
|
|
|
|
* waiting on it.
|
|
|
|
|
*/
|
2017-05-08 20:21:47 -07:00
|
|
|
void HAL_ReleaseDSMutex(void) { newDataOccur(refNumber); }
|
|
|
|
|
|
2015-11-26 00:08:32 -08:00
|
|
|
} // extern "C"
|