[hal] Add GetTeamNumber (#5596)

This commit is contained in:
Ryan Blue
2023-09-02 02:34:18 -04:00
committed by GitHub
parent a750bee54d
commit ac23f92451
19 changed files with 259 additions and 0 deletions

View File

@@ -47,6 +47,8 @@ static char roboRioCommentsString[64];
static size_t roboRioCommentsStringSize;
static bool roboRioCommentsStringInitialized;
static int32_t teamNumber = -1;
static const volatile HAL_HMBData* hmbBuffer;
#define HAL_HMB_TIMESTAMP_OFFSET 5
@@ -353,6 +355,36 @@ size_t HAL_GetComments(char* buffer, size_t size) {
return toCopy;
}
void InitializeTeamNumber(void) {
char hostnameBuf[25];
auto status = gethostname(hostnameBuf, sizeof(hostnameBuf));
if (status != 0) {
teamNumber = 0;
return;
}
std::string_view hostname{hostnameBuf, sizeof(hostnameBuf)};
// hostname is frc-{TEAM}-roborio
// Split string around '-' (max of 2 splits), take the second element of the
// resulting array.
wpi::SmallVector<std::string_view> elements;
wpi::split(hostname, elements, "-", 2);
if (elements.size() < 3) {
teamNumber = 0;
return;
}
teamNumber = wpi::parse_integer<int32_t>(elements[1], 10).value_or(0);
}
int32_t HAL_GetTeamNumber(void) {
if (teamNumber == -1) {
InitializeTeamNumber();
}
return teamNumber;
}
uint64_t HAL_GetFPGATime(int32_t* status) {
hal::init::CheckInit();
if (!hmbBuffer) {

View File

@@ -29,6 +29,7 @@ DEFINE_CAPI(int32_t, UserFaults5V, 0)
DEFINE_CAPI(int32_t, UserFaults3V3, 0)
DEFINE_CAPI(double, BrownoutVoltage, 6.75)
DEFINE_CAPI(double, CPUTemp, 45.0)
DEFINE_CAPI(int32_t, TeamNumber, 0);
int32_t HALSIM_RegisterRoboRioSerialNumberCallback(
HAL_RoboRioStringCallback callback, void* param, HAL_Bool initialNotify) {