2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2016-05-25 20:23:37 -07:00
|
|
|
|
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>
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <string>
|
|
|
|
|
#include <string_view>
|
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>
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <fmt/format.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>
|
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;
|
|
|
|
|
|
|
|
|
|
// 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 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;
|
2019-05-31 13:43:32 -07:00
|
|
|
desc->type = (std::numeric_limits<uint8_t>::max)();
|
2018-07-18 22:22:41 -07:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-23 20:25:07 -07:00
|
|
|
static wpi::mutex* newDSDataAvailableMutex;
|
|
|
|
|
static wpi::condition_variable* newDSDataAvailableCond;
|
2020-07-13 21:57:54 -07:00
|
|
|
static int newDSDataAvailableCounter{0};
|
2018-07-18 22:22:41 -07:00
|
|
|
|
2020-12-28 01:19:59 -08:00
|
|
|
namespace hal::init {
|
2019-10-23 20:25:07 -07:00
|
|
|
void InitializeFRCDriverStation() {
|
|
|
|
|
static wpi::mutex newMutex;
|
|
|
|
|
newDSDataAvailableMutex = &newMutex;
|
|
|
|
|
static wpi::condition_variable newCond;
|
|
|
|
|
newDSDataAvailableCond = &newCond;
|
|
|
|
|
}
|
2020-12-28 01:19:59 -08:00
|
|
|
} // namespace hal::init
|
2017-12-10 18:02:07 -08:00
|
|
|
|
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;
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock 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) {
|
2020-12-28 12:58:06 -08: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)) {
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view detailsRef{details};
|
|
|
|
|
std::string_view locationRef{location};
|
|
|
|
|
std::string_view callStackRef{callStack};
|
2019-03-03 15:43:04 -08:00
|
|
|
|
|
|
|
|
// 1 tag, 4 timestamp, 2 seqnum
|
|
|
|
|
// 2 numOccur, 4 error code, 1 flags, 6 strlen
|
|
|
|
|
// 1 extra needed for padding on Netcomm end.
|
|
|
|
|
size_t baseLength = 21;
|
|
|
|
|
|
|
|
|
|
if (baseLength + detailsRef.size() + locationRef.size() +
|
|
|
|
|
callStackRef.size() <=
|
2020-02-18 20:44:40 -08:00
|
|
|
65535) {
|
2019-03-03 15:43:04 -08:00
|
|
|
// Pass through
|
|
|
|
|
retval = FRC_NetworkCommunication_sendError(isError, errorCode, isLVCode,
|
|
|
|
|
details, location, callStack);
|
2020-02-18 20:44:40 -08:00
|
|
|
} else if (baseLength + detailsRef.size() > 65535) {
|
2019-03-03 15:43:04 -08:00
|
|
|
// Details too long, cut both location and stack
|
2020-02-18 20:44:40 -08:00
|
|
|
auto newLen = 65535 - baseLength;
|
2019-03-03 15:43:04 -08:00
|
|
|
std::string newDetails{details, newLen};
|
|
|
|
|
char empty = '\0';
|
|
|
|
|
retval = FRC_NetworkCommunication_sendError(
|
|
|
|
|
isError, errorCode, isLVCode, newDetails.c_str(), &empty, &empty);
|
2020-02-18 20:44:40 -08:00
|
|
|
} else if (baseLength + detailsRef.size() + locationRef.size() > 65535) {
|
2019-03-03 15:43:04 -08:00
|
|
|
// Location too long, cut stack
|
2020-02-18 20:44:40 -08:00
|
|
|
auto newLen = 65535 - baseLength - detailsRef.size();
|
2019-03-03 15:43:04 -08:00
|
|
|
std::string newLocation{location, newLen};
|
|
|
|
|
char empty = '\0';
|
|
|
|
|
retval = FRC_NetworkCommunication_sendError(
|
|
|
|
|
isError, errorCode, isLVCode, details, newLocation.c_str(), &empty);
|
|
|
|
|
} else {
|
|
|
|
|
// Stack too long
|
2020-02-18 20:44:40 -08:00
|
|
|
auto newLen = 65535 - baseLength - detailsRef.size() - locationRef.size();
|
2019-03-03 15:43:04 -08:00
|
|
|
std::string newCallStack{callStack, newLen};
|
|
|
|
|
retval = FRC_NetworkCommunication_sendError(isError, errorCode, isLVCode,
|
|
|
|
|
details, location,
|
|
|
|
|
newCallStack.c_str());
|
|
|
|
|
}
|
2016-07-17 19:42:33 -07:00
|
|
|
if (printMsg) {
|
2021-06-06 16:13:58 -07:00
|
|
|
fmt::memory_buffer buf;
|
2016-07-17 19:42:33 -07:00
|
|
|
if (location && location[0] != '\0') {
|
2021-06-06 16:13:58 -07:00
|
|
|
fmt::format_to(buf, "{} at {}: ", isError ? "Error" : "Warning",
|
|
|
|
|
location);
|
2016-07-17 19:42:33 -07:00
|
|
|
}
|
2021-06-06 16:13:58 -07:00
|
|
|
fmt::format_to(buf, "{}\n", details);
|
2016-07-17 19:42:33 -07:00
|
|
|
if (callStack && callStack[0] != '\0') {
|
2021-06-06 16:13:58 -07:00
|
|
|
fmt::format_to(buf, "{}\n", callStack);
|
2016-07-17 19:42:33 -07:00
|
|
|
}
|
2021-06-06 16:13:58 -07:00
|
|
|
std::fwrite(buf.data(), buf.size(), 1, stderr);
|
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
|
|
|
|
2020-02-18 20:44:40 -08:00
|
|
|
int32_t HAL_SendConsoleLine(const char* line) {
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view lineRef{line};
|
2020-02-18 20:44:40 -08:00
|
|
|
if (lineRef.size() <= 65535) {
|
|
|
|
|
// Send directly
|
|
|
|
|
return FRC_NetworkCommunication_sendConsoleLine(line);
|
|
|
|
|
} else {
|
|
|
|
|
// Need to truncate
|
|
|
|
|
std::string newLine{line, 65535};
|
|
|
|
|
return FRC_NetworkCommunication_sendConsoleLine(newLine.c_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t HAL_GetControlWord(HAL_ControlWord* controlWord) {
|
2019-10-23 20:25:07 -07:00
|
|
|
return HAL_GetControlWordInternal(controlWord);
|
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) {
|
2019-10-23 20:25:07 -07:00
|
|
|
return HAL_GetJoystickAxesInternal(joystickNum, axes);
|
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) {
|
2019-10-23 20:25:07 -07:00
|
|
|
return HAL_GetJoystickPOVsInternal(joystickNum, povs);
|
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) {
|
2019-10-23 20:25:07 -07:00
|
|
|
return HAL_GetJoystickButtonsInternal(joystickNum, buttons);
|
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) {
|
2019-10-23 20:25:07 -07:00
|
|
|
return HAL_GetJoystickDescriptorInternal(joystickNum, desc);
|
2018-07-18 22:22:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t HAL_GetMatchInfo(HAL_MatchInfo* info) {
|
2019-10-23 20:25:07 -07:00
|
|
|
return HAL_GetMatchInfoInternal(info);
|
2018-07-18 22:22:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void HAL_FreeJoystickName(char* name) {
|
|
|
|
|
std::free(name);
|
|
|
|
|
}
|
2017-06-30 16:11:16 -07:00
|
|
|
|
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
|
|
|
|
2019-11-01 17:09:28 -07:00
|
|
|
static int& GetThreadLocalLastCount() {
|
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.
|
2020-07-13 21:57:54 -07:00
|
|
|
thread_local int lastCount{0};
|
2019-11-01 17:09:28 -07:00
|
|
|
return lastCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HAL_Bool HAL_IsNewControlData(void) {
|
2020-07-13 21:57:54 -07:00
|
|
|
std::scoped_lock lock{*newDSDataAvailableMutex};
|
2019-11-01 17:09:28 -07:00
|
|
|
int& lastCount = GetThreadLocalLastCount();
|
2020-07-13 21:57:54 -07:00
|
|
|
int currentCount = newDSDataAvailableCounter;
|
2020-12-28 12:58:06 -08:00
|
|
|
if (lastCount == currentCount) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-05-08 20:21:47 -07:00
|
|
|
lastCount = currentCount;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void HAL_WaitForDSData(void) {
|
|
|
|
|
HAL_WaitForDSDataTimeout(0);
|
|
|
|
|
}
|
2017-05-08 20:21:47 -07:00
|
|
|
|
|
|
|
|
HAL_Bool HAL_WaitForDSDataTimeout(double timeout) {
|
2020-07-13 21:57:54 -07:00
|
|
|
std::unique_lock lock{*newDSDataAvailableMutex};
|
|
|
|
|
int& lastCount = GetThreadLocalLastCount();
|
|
|
|
|
int currentCount = newDSDataAvailableCounter;
|
|
|
|
|
if (lastCount != currentCount) {
|
|
|
|
|
lastCount = currentCount;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2017-05-08 20:21:47 -07:00
|
|
|
auto timeoutTime =
|
|
|
|
|
std::chrono::steady_clock::now() + std::chrono::duration<double>(timeout);
|
|
|
|
|
|
2020-07-13 21:57:54 -07:00
|
|
|
while (newDSDataAvailableCounter == currentCount) {
|
2017-05-08 20:21:47 -07:00
|
|
|
if (timeout > 0) {
|
2019-10-23 20:25:07 -07:00
|
|
|
auto timedOut = newDSDataAvailableCond->wait_until(lock, timeoutTime);
|
2017-05-08 20:21:47 -07:00
|
|
|
if (timedOut == std::cv_status::timeout) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-10-23 20:25:07 -07:00
|
|
|
newDSDataAvailableCond->wait(lock);
|
2017-05-08 20:21:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
2020-07-13 21:57:54 -07:00
|
|
|
lastCount = newDSDataAvailableCounter;
|
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
|
2020-12-28 12:58:06 -08:00
|
|
|
if (refNum != refNumber) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-07-13 21:57:54 -07:00
|
|
|
std::scoped_lock lock{*newDSDataAvailableMutex};
|
2019-10-23 20:25:07 -07:00
|
|
|
// Notify all threads
|
2020-07-13 21:57:54 -07:00
|
|
|
++newDSDataAvailableCounter;
|
2019-10-23 20:25:07 -07:00
|
|
|
newDSDataAvailableCond->notify_all();
|
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
|
2020-12-28 12:58:06 -08:00
|
|
|
if (initialized) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-05-08 20:21:47 -07:00
|
|
|
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(initializeMutex);
|
2017-05-08 20:21:47 -07:00
|
|
|
// Second check in case another thread was waiting
|
2020-12-28 12:58:06 -08:00
|
|
|
if (initialized) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
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.
|
|
|
|
|
*/
|
2020-12-28 12:58:06 -08:00
|
|
|
void HAL_ReleaseDSMutex(void) {
|
|
|
|
|
newDataOccur(refNumber);
|
|
|
|
|
}
|
2017-05-08 20:21:47 -07:00
|
|
|
|
2015-11-26 00:08:32 -08:00
|
|
|
} // extern "C"
|