From ad3e2d7d3b6953248d68234642185cde21b798e6 Mon Sep 17 00:00:00 2001 From: David Vo Date: Sun, 8 Jul 2018 15:18:03 +1000 Subject: [PATCH] Make HAL headers C-compatible (#1177) Also fix the return type of HAL_IsNewControlData() and HAL_MatchType's type. Since UsageReporting is intended to be namespaced, it is hidden when this is being used in C. Fixes: #476 Closes: #535 Ref: #1122 --- hal/src/main/native/athena/FRCDriverStation.cpp | 2 +- hal/src/main/native/include/HAL/Accelerometer.h | 4 +++- hal/src/main/native/include/HAL/AnalogTrigger.h | 4 +++- hal/src/main/native/include/HAL/CANAPI.h | 6 ++++-- hal/src/main/native/include/HAL/Counter.h | 4 +++- hal/src/main/native/include/HAL/DriverStation.h | 16 +++++++++++----- hal/src/main/native/include/HAL/Encoder.h | 6 ++++-- hal/src/main/native/include/HAL/HAL.h | 7 ++++++- hal/src/main/native/include/HAL/I2C.h | 6 +++++- hal/src/main/native/include/HAL/SPI.h | 4 +++- hal/src/main/native/include/HAL/SerialPort.h | 6 +++++- hal/src/main/native/include/HAL/Types.h | 8 ++++++++ hal/src/main/native/sim/DriverStation.cpp | 2 +- 13 files changed, 57 insertions(+), 18 deletions(-) diff --git a/hal/src/main/native/athena/FRCDriverStation.cpp b/hal/src/main/native/athena/FRCDriverStation.cpp index 520fd25c0d..03c9935d32 100644 --- a/hal/src/main/native/athena/FRCDriverStation.cpp +++ b/hal/src/main/native/athena/FRCDriverStation.cpp @@ -307,7 +307,7 @@ void HAL_ObserveUserProgramTest(void) { FRC_NetworkCommunication_observeUserProgramTest(); } -bool HAL_IsNewControlData(void) { +HAL_Bool HAL_IsNewControlData(void) { // 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 diff --git a/hal/src/main/native/include/HAL/Accelerometer.h b/hal/src/main/native/include/HAL/Accelerometer.h index 0b2c6db03f..880d47e277 100644 --- a/hal/src/main/native/include/HAL/Accelerometer.h +++ b/hal/src/main/native/include/HAL/Accelerometer.h @@ -9,14 +9,16 @@ #include "HAL/Types.h" +// clang-format off /** * The acceptable accelerometer ranges. */ -enum HAL_AccelerometerRange : int32_t { +HAL_ENUM(HAL_AccelerometerRange) { HAL_AccelerometerRange_k2G = 0, HAL_AccelerometerRange_k4G = 1, HAL_AccelerometerRange_k8G = 2, }; +// clang-format on #ifdef __cplusplus extern "C" { diff --git a/hal/src/main/native/include/HAL/AnalogTrigger.h b/hal/src/main/native/include/HAL/AnalogTrigger.h index 59face9bd3..f642ba9386 100644 --- a/hal/src/main/native/include/HAL/AnalogTrigger.h +++ b/hal/src/main/native/include/HAL/AnalogTrigger.h @@ -11,15 +11,17 @@ #include "HAL/Types.h" +// clang-format off /** * The type of analog trigger to trigger on. */ -enum HAL_AnalogTriggerType : int32_t { +HAL_ENUM(HAL_AnalogTriggerType) { HAL_Trigger_kInWindow = 0, HAL_Trigger_kState = 1, HAL_Trigger_kRisingPulse = 2, HAL_Trigger_kFallingPulse = 3 }; +// clang-format on #ifdef __cplusplus extern "C" { diff --git a/hal/src/main/native/include/HAL/CANAPI.h b/hal/src/main/native/include/HAL/CANAPI.h index 46a255520e..f4fff3cc25 100644 --- a/hal/src/main/native/include/HAL/CANAPI.h +++ b/hal/src/main/native/include/HAL/CANAPI.h @@ -11,12 +11,13 @@ #include "HAL/Types.h" +// clang-format off /** * The CAN device type. * * Teams should use HAL_CAN_Dev_kMiscellaneous */ -enum HAL_CANDeviceType : int32_t { +HAL_ENUM(HAL_CANDeviceType) { HAL_CAN_Dev_kBroadcast = 0, HAL_CAN_Dev_kRobotController = 1, HAL_CAN_Dev_kMotorController = 2, @@ -36,7 +37,7 @@ enum HAL_CANDeviceType : int32_t { * * Teams should use HAL_CAN_Man_kTeamUse. */ -enum HAL_CANManufacturer : int32_t { +HAL_ENUM(HAL_CANManufacturer) { HAL_CAN_Man_kBroadcast = 0, HAL_CAN_Man_kNI = 1, HAL_CAN_Man_kLM = 2, @@ -45,6 +46,7 @@ enum HAL_CANManufacturer : int32_t { HAL_CAN_Man_kMS = 7, HAL_CAN_Man_kTeamUse = 8, }; +// clang-format on #ifdef __cplusplus extern "C" { diff --git a/hal/src/main/native/include/HAL/Counter.h b/hal/src/main/native/include/HAL/Counter.h index 85bdc5c732..89ab8d4117 100644 --- a/hal/src/main/native/include/HAL/Counter.h +++ b/hal/src/main/native/include/HAL/Counter.h @@ -12,15 +12,17 @@ #include "HAL/AnalogTrigger.h" #include "HAL/Types.h" +// clang-format off /** * The counter mode. */ -enum HAL_Counter_Mode : int32_t { +HAL_ENUM(HAL_Counter_Mode) { HAL_Counter_kTwoPulse = 0, HAL_Counter_kSemiperiod = 1, HAL_Counter_kPulseLength = 2, HAL_Counter_kExternalDirection = 3 }; +// clang-format on #ifdef __cplusplus extern "C" { diff --git a/hal/src/main/native/include/HAL/DriverStation.h b/hal/src/main/native/include/HAL/DriverStation.h index 3a09a0c620..5ce038527a 100644 --- a/hal/src/main/native/include/HAL/DriverStation.h +++ b/hal/src/main/native/include/HAL/DriverStation.h @@ -9,8 +9,6 @@ #include -#include - #include "HAL/Types.h" #define HAL_IO_CONFIG_DATA_SIZE 32 @@ -37,8 +35,10 @@ struct HAL_ControlWord { uint32_t dsAttached : 1; uint32_t control_reserved : 26; }; +typedef struct HAL_ControlWord HAL_ControlWord; -enum HAL_AllianceStationID : int32_t { +// clang-format off +HAL_ENUM(HAL_AllianceStationID) { HAL_AllianceStationID_kRed1, HAL_AllianceStationID_kRed2, HAL_AllianceStationID_kRed3, @@ -47,12 +47,13 @@ enum HAL_AllianceStationID : int32_t { HAL_AllianceStationID_kBlue3, }; -enum HAL_MatchType { +HAL_ENUM(HAL_MatchType) { HAL_kMatchType_none, HAL_kMatchType_practice, HAL_kMatchType_qualification, HAL_kMatchType_elimination, }; +// clang-format on /* The maximum number of axes that will be stored in a single HALJoystickAxes * struct. This is used for allocating buffers, not bounds checking, since @@ -65,16 +66,19 @@ struct HAL_JoystickAxes { int16_t count; float axes[HAL_kMaxJoystickAxes]; }; +typedef struct HAL_JoystickAxes HAL_JoystickAxes; struct HAL_JoystickPOVs { int16_t count; int16_t povs[HAL_kMaxJoystickPOVs]; }; +typedef struct HAL_JoystickPOVs HAL_JoystickPOVs; struct HAL_JoystickButtons { uint32_t buttons; uint8_t count; }; +typedef struct HAL_JoystickButtons HAL_JoystickButtons; struct HAL_JoystickDescriptor { uint8_t isXbox; @@ -85,6 +89,7 @@ struct HAL_JoystickDescriptor { uint8_t buttonCount; uint8_t povCount; }; +typedef struct HAL_JoystickDescriptor HAL_JoystickDescriptor; struct HAL_MatchInfo { char* eventName; @@ -93,6 +98,7 @@ struct HAL_MatchInfo { uint8_t replayNumber; char* gameSpecificMessage; }; +typedef struct HAL_MatchInfo HAL_MatchInfo; #ifdef __cplusplus extern "C" { @@ -286,7 +292,7 @@ void HAL_ReleaseDSMutex(void); * * @return true if the control data has been updated since the last call */ -bool HAL_IsNewControlData(void); +HAL_Bool HAL_IsNewControlData(void); /** * Waits for the newest DS packet to arrive. Note that this is a blocking call. diff --git a/hal/src/main/native/include/HAL/Encoder.h b/hal/src/main/native/include/HAL/Encoder.h index d9fc1412d8..ca6c16bd16 100644 --- a/hal/src/main/native/include/HAL/Encoder.h +++ b/hal/src/main/native/include/HAL/Encoder.h @@ -12,10 +12,11 @@ #include "HAL/AnalogTrigger.h" #include "HAL/Types.h" +// clang-format off /** * The type of index pulse for the encoder. */ -enum HAL_EncoderIndexingType : int32_t { +HAL_ENUM(HAL_EncoderIndexingType) { HAL_kResetWhileHigh, HAL_kResetWhileLow, HAL_kResetOnFallingEdge, @@ -25,11 +26,12 @@ enum HAL_EncoderIndexingType : int32_t { /** * The encoding scaling of the encoder. */ -enum HAL_EncoderEncodingType : int32_t { +HAL_ENUM(HAL_EncoderEncodingType) { HAL_Encoder_k1X, HAL_Encoder_k2X, HAL_Encoder_k4X }; +// clang-format on #ifdef __cplusplus extern "C" { diff --git a/hal/src/main/native/include/HAL/HAL.h b/hal/src/main/native/include/HAL/HAL.h index 4de10c7cc8..22e6bcd201 100644 --- a/hal/src/main/native/include/HAL/HAL.h +++ b/hal/src/main/native/include/HAL/HAL.h @@ -39,11 +39,16 @@ #endif // HAL_USE_LABVIEW #include "HAL/Types.h" + +#ifdef __cplusplus #include "UsageReporting.h" namespace HALUsageReporting = nUsageReporting; +#endif -enum HAL_RuntimeType : int32_t { HAL_Athena, HAL_Mock }; +// clang-format off +HAL_ENUM(HAL_RuntimeType) { HAL_Athena, HAL_Mock }; +// clang-format on #ifdef __cplusplus extern "C" { diff --git a/hal/src/main/native/include/HAL/I2C.h b/hal/src/main/native/include/HAL/I2C.h index 90941efeff..a71f92da75 100644 --- a/hal/src/main/native/include/HAL/I2C.h +++ b/hal/src/main/native/include/HAL/I2C.h @@ -9,7 +9,11 @@ #include -enum HAL_I2CPort : int32_t { HAL_I2C_kOnboard = 0, HAL_I2C_kMXP }; +#include "HAL/Types.h" + +// clang-format off +HAL_ENUM(HAL_I2CPort) { HAL_I2C_kOnboard = 0, HAL_I2C_kMXP }; +// clang-format on #ifdef __cplusplus extern "C" { diff --git a/hal/src/main/native/include/HAL/SPI.h b/hal/src/main/native/include/HAL/SPI.h index 25b756da94..a197fccda4 100644 --- a/hal/src/main/native/include/HAL/SPI.h +++ b/hal/src/main/native/include/HAL/SPI.h @@ -12,13 +12,15 @@ #include "HAL/AnalogTrigger.h" #include "HAL/Types.h" -enum HAL_SPIPort : int32_t { +// clang-format off +HAL_ENUM(HAL_SPIPort) { HAL_SPI_kOnboardCS0 = 0, HAL_SPI_kOnboardCS1, HAL_SPI_kOnboardCS2, HAL_SPI_kOnboardCS3, HAL_SPI_kMXP }; +// clang-format on #ifdef __cplusplus extern "C" { diff --git a/hal/src/main/native/include/HAL/SerialPort.h b/hal/src/main/native/include/HAL/SerialPort.h index e9e7021008..3adb4003cc 100644 --- a/hal/src/main/native/include/HAL/SerialPort.h +++ b/hal/src/main/native/include/HAL/SerialPort.h @@ -9,12 +9,16 @@ #include -enum HAL_SerialPort : int32_t { +#include "HAL/Types.h" + +// clang-format off +HAL_ENUM(HAL_SerialPort) { HAL_SerialPort_Onboard = 0, HAL_SerialPort_MXP = 1, HAL_SerialPort_USB1 = 2, HAL_SerialPort_USB2 = 3 }; +// clang-format on #ifdef __cplusplus extern "C" { diff --git a/hal/src/main/native/include/HAL/Types.h b/hal/src/main/native/include/HAL/Types.h index 4de98adba7..f9f8abd971 100644 --- a/hal/src/main/native/include/HAL/Types.h +++ b/hal/src/main/native/include/HAL/Types.h @@ -48,3 +48,11 @@ typedef HAL_Handle HAL_CANHandle; typedef HAL_CANHandle HAL_PDPHandle; typedef int32_t HAL_Bool; + +#ifdef __cplusplus +#define HAL_ENUM(name) enum name : int32_t +#else +#define HAL_ENUM(name) \ + typedef int32_t name; \ + enum name +#endif diff --git a/hal/src/main/native/sim/DriverStation.cpp b/hal/src/main/native/sim/DriverStation.cpp index ebdf0636ca..e8f56967f9 100644 --- a/hal/src/main/native/sim/DriverStation.cpp +++ b/hal/src/main/native/sim/DriverStation.cpp @@ -204,7 +204,7 @@ static void InitLastCountKey(void) { } #endif -bool HAL_IsNewControlData(void) { +HAL_Bool HAL_IsNewControlData(void) { #ifdef __APPLE__ pthread_once(&lastCountKeyOnce, InitLastCountKey); int* lastCountPtr = static_cast(pthread_getspecific(lastCountKey));