mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-27 02:01:42 +00:00
[hal] Add GetTeamNumber (#5596)
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -484,6 +484,18 @@ Java_edu_wpi_first_hal_HALUtil_getComments
|
||||
return MakeJString(env, std::string_view(comments, len));
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_HALUtil
|
||||
* Method: getTeamNumber
|
||||
* Signature: ()I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_hal_HALUtil_getTeamNumber
|
||||
(JNIEnv* env, jclass)
|
||||
{
|
||||
return HAL_GetTeamNumber();
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_HALUtil
|
||||
* Method: getFPGATime
|
||||
|
||||
@@ -878,6 +878,56 @@ Java_edu_wpi_first_hal_simulation_RoboRioDataJNI_setCPUTemp
|
||||
HALSIM_SetRoboRioCPUTemp(cpuTemp);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_RoboRioDataJNI
|
||||
* Method: registerTeamNumberCallback
|
||||
* Signature: (Ljava/lang/Object;Z)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_RoboRioDataJNI_registerTeamNumberCallback
|
||||
(JNIEnv* env, jclass, jobject callback, jboolean initialNotify)
|
||||
{
|
||||
return sim::AllocateCallbackNoIndex(
|
||||
env, callback, initialNotify, &HALSIM_RegisterRoboRioTeamNumberCallback);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_RoboRioDataJNI
|
||||
* Method: cancelTeamNumberCallback
|
||||
* Signature: (I)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_RoboRioDataJNI_cancelTeamNumberCallback
|
||||
(JNIEnv* env, jclass, jint handle)
|
||||
{
|
||||
return sim::FreeCallbackNoIndex(env, handle,
|
||||
&HALSIM_CancelRoboRioTeamNumberCallback);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_RoboRioDataJNI
|
||||
* Method: getTeamNumber
|
||||
* Signature: ()I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_RoboRioDataJNI_getTeamNumber
|
||||
(JNIEnv*, jclass)
|
||||
{
|
||||
return HALSIM_GetRoboRioTeamNumber();
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_RoboRioDataJNI
|
||||
* Method: setTeamNumber
|
||||
* Signature: (I)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_edu_wpi_first_hal_simulation_RoboRioDataJNI_setTeamNumber
|
||||
(JNIEnv*, jclass, jint value)
|
||||
{
|
||||
HALSIM_SetRoboRioTeamNumber(value);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_first_hal_simulation_RoboRioDataJNI
|
||||
* Method: getSerialNumber
|
||||
|
||||
@@ -92,6 +92,12 @@ size_t HAL_GetSerialNumber(char* buffer, size_t size);
|
||||
*/
|
||||
size_t HAL_GetComments(char* buffer, size_t size);
|
||||
|
||||
/**
|
||||
* Returns the team number configured for the robot controller.
|
||||
* @return team number, or 0 if not found.
|
||||
*/
|
||||
int32_t HAL_GetTeamNumber(void);
|
||||
|
||||
/**
|
||||
* Returns the runtime type of the HAL.
|
||||
*
|
||||
|
||||
@@ -126,6 +126,13 @@ void HALSIM_CancelRoboRioBrownoutVoltageCallback(int32_t uid);
|
||||
double HALSIM_GetRoboRioBrownoutVoltage(void);
|
||||
void HALSIM_SetRoboRioBrownoutVoltage(double brownoutVoltage);
|
||||
|
||||
int32_t HALSIM_RegisterRoboRioTeamNumberCallback(HAL_NotifyCallback callback,
|
||||
void* param,
|
||||
HAL_Bool initialNotify);
|
||||
void HALSIM_CancelRoboRioTeamNumberCallback(int32_t uid);
|
||||
int32_t HALSIM_GetRoboRioTeamNumber(void);
|
||||
void HALSIM_SetRoboRioTeamNumber(int32_t teamNumber);
|
||||
|
||||
int32_t HALSIM_RegisterRoboRioSerialNumberCallback(
|
||||
HAL_RoboRioStringCallback callback, void* param, HAL_Bool initialNotify);
|
||||
void HALSIM_CancelRoboRioSerialNumberCallback(int32_t uid);
|
||||
|
||||
@@ -288,6 +288,10 @@ size_t HAL_GetComments(char* buffer, size_t size) {
|
||||
return HALSIM_GetRoboRioComments(buffer, size);
|
||||
}
|
||||
|
||||
int32_t HAL_GetTeamNumber(void) {
|
||||
return HALSIM_GetRoboRioTeamNumber();
|
||||
}
|
||||
|
||||
uint64_t HAL_GetFPGATime(int32_t* status) {
|
||||
return hal::GetFPGATime();
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ void RoboRioData::ResetData() {
|
||||
userFaults3V3.Reset(0);
|
||||
brownoutVoltage.Reset(6.75);
|
||||
cpuTemp.Reset(45.0);
|
||||
teamNumber.Reset(0);
|
||||
m_serialNumber = "";
|
||||
m_comments = "";
|
||||
}
|
||||
@@ -134,6 +135,7 @@ DEFINE_CAPI(int32_t, UserFaults5V, userFaults5V)
|
||||
DEFINE_CAPI(int32_t, UserFaults3V3, userFaults3V3)
|
||||
DEFINE_CAPI(double, BrownoutVoltage, brownoutVoltage)
|
||||
DEFINE_CAPI(double, CPUTemp, cpuTemp)
|
||||
DEFINE_CAPI(int32_t, TeamNumber, teamNumber)
|
||||
|
||||
int32_t HALSIM_RegisterRoboRioSerialNumberCallback(
|
||||
HAL_RoboRioStringCallback callback, void* param, HAL_Bool initialNotify) {
|
||||
|
||||
@@ -31,6 +31,7 @@ class RoboRioData {
|
||||
HAL_SIMDATAVALUE_DEFINE_NAME(UserFaults3V3)
|
||||
HAL_SIMDATAVALUE_DEFINE_NAME(BrownoutVoltage)
|
||||
HAL_SIMDATAVALUE_DEFINE_NAME(CPUTemp)
|
||||
HAL_SIMDATAVALUE_DEFINE_NAME(TeamNumber)
|
||||
|
||||
HAL_SIMCALLBACKREGISTRY_DEFINE_NAME(SerialNumber)
|
||||
HAL_SIMCALLBACKREGISTRY_DEFINE_NAME(Comments);
|
||||
@@ -59,6 +60,7 @@ class RoboRioData {
|
||||
SimDataValue<double, HAL_MakeDouble, GetBrownoutVoltageName> brownoutVoltage{
|
||||
6.75};
|
||||
SimDataValue<double, HAL_MakeDouble, GetCPUTempName> cpuTemp{45.0};
|
||||
SimDataValue<int32_t, HAL_MakeInt, GetTeamNumberName> teamNumber{0};
|
||||
|
||||
int32_t RegisterSerialNumberCallback(HAL_RoboRioStringCallback callback,
|
||||
void* param, HAL_Bool initialNotify);
|
||||
|
||||
Reference in New Issue
Block a user