mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-27 02:01:42 +00:00
Add braces to C++ single-line loops and conditionals (NFC) (#2973)
This makes code easier to read and more consistent between C++ and Java. Also update clang-format settings to always add a line break (even if no braces are used).
This commit is contained in:
@@ -22,7 +22,13 @@ void HAL_SetAccelerometerActive(HAL_Bool active) {
|
||||
void HAL_SetAccelerometerRange(HAL_AccelerometerRange range) {
|
||||
SimAccelerometerData[0].range = range;
|
||||
}
|
||||
double HAL_GetAccelerometerX(void) { return SimAccelerometerData[0].x; }
|
||||
double HAL_GetAccelerometerY(void) { return SimAccelerometerData[0].y; }
|
||||
double HAL_GetAccelerometerZ(void) { return SimAccelerometerData[0].z; }
|
||||
double HAL_GetAccelerometerX(void) {
|
||||
return SimAccelerometerData[0].x;
|
||||
}
|
||||
double HAL_GetAccelerometerY(void) {
|
||||
return SimAccelerometerData[0].y;
|
||||
}
|
||||
double HAL_GetAccelerometerZ(void) {
|
||||
return SimAccelerometerData[0].z;
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -83,7 +83,9 @@ HAL_AddressableLEDHandle HAL_InitializeAddressableLED(
|
||||
void HAL_FreeAddressableLED(HAL_AddressableLEDHandle handle) {
|
||||
auto led = ledHandles->Get(handle);
|
||||
ledHandles->Free(handle);
|
||||
if (!led) return;
|
||||
if (!led) {
|
||||
return;
|
||||
}
|
||||
SimAddressableLEDData[led->index].running = false;
|
||||
SimAddressableLEDData[led->index].initialized = false;
|
||||
}
|
||||
|
||||
@@ -24,7 +24,9 @@ HAL_Bool HAL_IsAccumulatorChannel(HAL_AnalogInputHandle analogPortHandle,
|
||||
return false;
|
||||
}
|
||||
for (int32_t i = 0; i < kNumAccumulators; i++) {
|
||||
if (port->channel == kAccumulatorChannels[i]) return true;
|
||||
if (port->channel == kAccumulatorChannels[i]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -50,8 +50,9 @@ HAL_GyroHandle HAL_InitializeAnalogGyro(HAL_AnalogInputHandle analogHandle,
|
||||
|
||||
auto handle = analogGyroHandles->Allocate(channel, status);
|
||||
|
||||
if (*status != 0)
|
||||
if (*status != 0) {
|
||||
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
|
||||
}
|
||||
|
||||
// Initialize port structure
|
||||
auto gyro = analogGyroHandles->Get(handle);
|
||||
@@ -75,7 +76,9 @@ void HAL_SetupAnalogGyro(HAL_GyroHandle handle, int32_t* status) {
|
||||
void HAL_FreeAnalogGyro(HAL_GyroHandle handle) {
|
||||
auto gyro = analogGyroHandles->Get(handle);
|
||||
analogGyroHandles->Free(handle);
|
||||
if (gyro == nullptr) return;
|
||||
if (gyro == nullptr) {
|
||||
return;
|
||||
}
|
||||
SimAnalogGyroData[gyro->index].initialized = false;
|
||||
}
|
||||
|
||||
|
||||
@@ -31,8 +31,9 @@ HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(HAL_PortHandle portHandle,
|
||||
|
||||
HAL_AnalogInputHandle handle = analogInputHandles->Allocate(channel, status);
|
||||
|
||||
if (*status != 0)
|
||||
if (*status != 0) {
|
||||
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
|
||||
}
|
||||
|
||||
// Initialize port structure
|
||||
auto analog_port = analogInputHandles->Get(handle);
|
||||
@@ -58,12 +59,16 @@ void HAL_FreeAnalogInputPort(HAL_AnalogInputHandle analogPortHandle) {
|
||||
auto port = analogInputHandles->Get(analogPortHandle);
|
||||
// no status, so no need to check for a proper free.
|
||||
analogInputHandles->Free(analogPortHandle);
|
||||
if (port == nullptr) return;
|
||||
if (port == nullptr) {
|
||||
return;
|
||||
}
|
||||
SimAnalogInData[port->channel].initialized = false;
|
||||
SimAnalogInData[port->channel].accumulatorInitialized = false;
|
||||
}
|
||||
|
||||
HAL_Bool HAL_CheckAnalogModule(int32_t module) { return module == 1; }
|
||||
HAL_Bool HAL_CheckAnalogModule(int32_t module) {
|
||||
return module == 1;
|
||||
}
|
||||
|
||||
HAL_Bool HAL_CheckAnalogInputChannel(int32_t channel) {
|
||||
return channel < kNumAnalogInputs && channel >= 0;
|
||||
@@ -72,14 +77,18 @@ HAL_Bool HAL_CheckAnalogInputChannel(int32_t channel) {
|
||||
void HAL_SetAnalogInputSimDevice(HAL_AnalogInputHandle handle,
|
||||
HAL_SimDeviceHandle device) {
|
||||
auto port = analogInputHandles->Get(handle);
|
||||
if (port == nullptr) return;
|
||||
if (port == nullptr) {
|
||||
return;
|
||||
}
|
||||
SimAnalogInData[port->channel].simDevice = device;
|
||||
}
|
||||
|
||||
void HAL_SetAnalogSampleRate(double samplesPerSecond, int32_t* status) {
|
||||
// No op
|
||||
}
|
||||
double HAL_GetAnalogSampleRate(int32_t* status) { return kDefaultSampleRate; }
|
||||
double HAL_GetAnalogSampleRate(int32_t* status) {
|
||||
return kDefaultSampleRate;
|
||||
}
|
||||
void HAL_SetAnalogAverageBits(HAL_AnalogInputHandle analogPortHandle,
|
||||
int32_t bits, int32_t* status) {
|
||||
auto port = analogInputHandles->Get(analogPortHandle);
|
||||
|
||||
@@ -47,8 +47,9 @@ HAL_AnalogOutputHandle HAL_InitializeAnalogOutputPort(HAL_PortHandle portHandle,
|
||||
HAL_AnalogOutputHandle handle =
|
||||
analogOutputHandles->Allocate(channel, status);
|
||||
|
||||
if (*status != 0)
|
||||
if (*status != 0) {
|
||||
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
|
||||
}
|
||||
|
||||
auto port = analogOutputHandles->Get(handle);
|
||||
if (port == nullptr) { // would only error on thread issue
|
||||
@@ -66,7 +67,9 @@ HAL_AnalogOutputHandle HAL_InitializeAnalogOutputPort(HAL_PortHandle portHandle,
|
||||
void HAL_FreeAnalogOutputPort(HAL_AnalogOutputHandle analogOutputHandle) {
|
||||
// no status, so no need to check for a proper free.
|
||||
auto port = analogOutputHandles->Get(analogOutputHandle);
|
||||
if (port == nullptr) return;
|
||||
if (port == nullptr) {
|
||||
return;
|
||||
}
|
||||
analogOutputHandles->Free(analogOutputHandle);
|
||||
SimAnalogOutData[port->channel].initialized = false;
|
||||
}
|
||||
|
||||
@@ -99,7 +99,9 @@ void HAL_CleanAnalogTrigger(HAL_AnalogTriggerHandle analogTriggerHandle,
|
||||
int32_t* status) {
|
||||
auto trigger = analogTriggerHandles->Get(analogTriggerHandle);
|
||||
analogTriggerHandles->Free(analogTriggerHandle);
|
||||
if (trigger == nullptr) return;
|
||||
if (trigger == nullptr) {
|
||||
return;
|
||||
}
|
||||
SimAnalogTriggerData[trigger->index].initialized = false;
|
||||
// caller owns the analog input handle.
|
||||
}
|
||||
@@ -128,10 +130,14 @@ void HAL_SetAnalogTriggerLimitsRaw(HAL_AnalogTriggerHandle analogTriggerHandle,
|
||||
|
||||
double trigLower =
|
||||
GetAnalogValueToVoltage(trigger->analogHandle, lower, status);
|
||||
if (status != 0) return;
|
||||
if (status != 0) {
|
||||
return;
|
||||
}
|
||||
double trigUpper =
|
||||
GetAnalogValueToVoltage(trigger->analogHandle, upper, status);
|
||||
if (status != 0) return;
|
||||
if (status != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
SimAnalogTriggerData[trigger->index].triggerUpperBound = trigUpper;
|
||||
SimAnalogTriggerData[trigger->index].triggerLowerBound = trigLower;
|
||||
|
||||
@@ -35,7 +35,9 @@ extern "C" {
|
||||
HAL_DigitalHandle HAL_InitializeDIOPort(HAL_PortHandle portHandle,
|
||||
HAL_Bool input, int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
if (*status != 0) return HAL_kInvalidHandle;
|
||||
if (*status != 0) {
|
||||
return HAL_kInvalidHandle;
|
||||
}
|
||||
|
||||
int16_t channel = getPortHandleChannel(portHandle);
|
||||
if (channel == InvalidHandleIndex) {
|
||||
@@ -46,8 +48,9 @@ HAL_DigitalHandle HAL_InitializeDIOPort(HAL_PortHandle portHandle,
|
||||
auto handle =
|
||||
digitalChannelHandles->Allocate(channel, HAL_HandleEnum::DIO, status);
|
||||
|
||||
if (*status != 0)
|
||||
if (*status != 0) {
|
||||
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
|
||||
}
|
||||
|
||||
auto port = digitalChannelHandles->Get(handle, HAL_HandleEnum::DIO);
|
||||
if (port == nullptr) { // would only occur on thread issue.
|
||||
@@ -72,13 +75,17 @@ void HAL_FreeDIOPort(HAL_DigitalHandle dioPortHandle) {
|
||||
auto port = digitalChannelHandles->Get(dioPortHandle, HAL_HandleEnum::DIO);
|
||||
// no status, so no need to check for a proper free.
|
||||
digitalChannelHandles->Free(dioPortHandle, HAL_HandleEnum::DIO);
|
||||
if (port == nullptr) return;
|
||||
if (port == nullptr) {
|
||||
return;
|
||||
}
|
||||
SimDIOData[port->channel].initialized = false;
|
||||
}
|
||||
|
||||
void HAL_SetDIOSimDevice(HAL_DigitalHandle handle, HAL_SimDeviceHandle device) {
|
||||
auto port = digitalChannelHandles->Get(handle, HAL_HandleEnum::DIO);
|
||||
if (port == nullptr) return;
|
||||
if (port == nullptr) {
|
||||
return;
|
||||
}
|
||||
SimDIOData[port->channel].simDevice = device;
|
||||
}
|
||||
|
||||
@@ -104,7 +111,9 @@ HAL_DigitalPWMHandle HAL_AllocateDigitalPWM(int32_t* status) {
|
||||
void HAL_FreeDigitalPWM(HAL_DigitalPWMHandle pwmGenerator, int32_t* status) {
|
||||
auto port = digitalPWMHandles->Get(pwmGenerator);
|
||||
digitalPWMHandles->Free(pwmGenerator);
|
||||
if (port == nullptr) return;
|
||||
if (port == nullptr) {
|
||||
return;
|
||||
}
|
||||
int32_t id = *port;
|
||||
SimDigitalPWMData[id].initialized = false;
|
||||
}
|
||||
@@ -129,8 +138,12 @@ void HAL_SetDigitalPWMDutyCycle(HAL_DigitalPWMHandle pwmGenerator,
|
||||
return;
|
||||
}
|
||||
int32_t id = *port;
|
||||
if (dutyCycle > 1.0) dutyCycle = 1.0;
|
||||
if (dutyCycle < 0.0) dutyCycle = 0.0;
|
||||
if (dutyCycle > 1.0) {
|
||||
dutyCycle = 1.0;
|
||||
}
|
||||
if (dutyCycle < 0.0) {
|
||||
dutyCycle = 0.0;
|
||||
}
|
||||
SimDigitalPWMData[id].dutyCycle = dutyCycle;
|
||||
}
|
||||
|
||||
@@ -153,7 +166,9 @@ void HAL_SetDIO(HAL_DigitalHandle dioPortHandle, HAL_Bool value,
|
||||
return;
|
||||
}
|
||||
if (value != 0 && value != 1) {
|
||||
if (value != 0) value = 1;
|
||||
if (value != 0) {
|
||||
value = 1;
|
||||
}
|
||||
}
|
||||
SimDIOData[port->channel].value = value;
|
||||
}
|
||||
@@ -176,8 +191,12 @@ HAL_Bool HAL_GetDIO(HAL_DigitalHandle dioPortHandle, int32_t* status) {
|
||||
return false;
|
||||
}
|
||||
HAL_Bool value = SimDIOData[port->channel].value;
|
||||
if (value > 1) value = 1;
|
||||
if (value < 0) value = 0;
|
||||
if (value > 1) {
|
||||
value = 1;
|
||||
}
|
||||
if (value < 0) {
|
||||
value = 0;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -188,8 +207,12 @@ HAL_Bool HAL_GetDIODirection(HAL_DigitalHandle dioPortHandle, int32_t* status) {
|
||||
return false;
|
||||
}
|
||||
HAL_Bool value = SimDIOData[port->channel].isInput;
|
||||
if (value > 1) value = 1;
|
||||
if (value < 0) value = 0;
|
||||
if (value > 1) {
|
||||
value = 1;
|
||||
}
|
||||
if (value < 0) {
|
||||
value = 0;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,9 @@
|
||||
#include "hal/DMA.h"
|
||||
|
||||
extern "C" {
|
||||
HAL_DMAHandle HAL_InitializeDMA(int32_t* status) { return HAL_kInvalidHandle; }
|
||||
HAL_DMAHandle HAL_InitializeDMA(int32_t* status) {
|
||||
return HAL_kInvalidHandle;
|
||||
}
|
||||
void HAL_FreeDMA(HAL_DMAHandle handle) {}
|
||||
|
||||
void HAL_SetDMAPause(HAL_DMAHandle handle, HAL_Bool pause, int32_t* status) {}
|
||||
@@ -47,7 +49,9 @@ void HAL_SetDMAExternalTrigger(HAL_DMAHandle handle,
|
||||
void HAL_StartDMA(HAL_DMAHandle handle, int32_t queueDepth, int32_t* status) {}
|
||||
void HAL_StopDMA(HAL_DMAHandle handle, int32_t* status) {}
|
||||
|
||||
void* HAL_GetDMADirectPointer(HAL_DMAHandle handle) { return nullptr; }
|
||||
void* HAL_GetDMADirectPointer(HAL_DMAHandle handle) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
enum HAL_DMAReadStatus HAL_ReadDMADirect(void* dmaPointer,
|
||||
HAL_DMASample* dmaSample,
|
||||
|
||||
@@ -52,7 +52,9 @@ bool remapDigitalSource(HAL_Handle digitalSourceHandle,
|
||||
}
|
||||
}
|
||||
|
||||
int32_t remapMXPChannel(int32_t channel) { return channel - 10; }
|
||||
int32_t remapMXPChannel(int32_t channel) {
|
||||
return channel - 10;
|
||||
}
|
||||
|
||||
int32_t remapMXPPWMChannel(int32_t channel) {
|
||||
if (channel < 14) {
|
||||
|
||||
@@ -56,9 +56,10 @@ 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) {
|
||||
auto errorHandler = sendErrorHandler.load();
|
||||
if (errorHandler)
|
||||
if (errorHandler) {
|
||||
return errorHandler(isError, errorCode, isLVCode, details, location,
|
||||
callStack, 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;
|
||||
@@ -76,7 +77,9 @@ int32_t HAL_SendError(HAL_Bool isError, int32_t errorCode, HAL_Bool isLVCode,
|
||||
auto curTime = fpga_clock::now();
|
||||
int i;
|
||||
for (i = 0; i < KEEP_MSGS; ++i) {
|
||||
if (prevMsg[i] == details) break;
|
||||
if (prevMsg[i] == details) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
int retval = 0;
|
||||
if (i == KEEP_MSGS || (curTime - prevMsgTime[i]) >= std::chrono::seconds(1)) {
|
||||
@@ -177,9 +180,13 @@ char* HAL_GetJoystickName(int32_t joystickNum) {
|
||||
return name;
|
||||
}
|
||||
|
||||
void HAL_FreeJoystickName(char* name) { std::free(name); }
|
||||
void HAL_FreeJoystickName(char* name) {
|
||||
std::free(name);
|
||||
}
|
||||
|
||||
int32_t HAL_GetJoystickAxisType(int32_t joystickNum, int32_t axis) { return 0; }
|
||||
int32_t HAL_GetJoystickAxisType(int32_t joystickNum, int32_t axis) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t HAL_SetJoystickOutputs(int32_t joystickNum, int64_t outputs,
|
||||
int32_t leftRumble, int32_t rightRumble) {
|
||||
@@ -197,7 +204,9 @@ int32_t HAL_GetMatchInfo(HAL_MatchInfo* info) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void HAL_ObserveUserProgramStarting(void) { HALSIM_SetProgramStarted(); }
|
||||
void HAL_ObserveUserProgramStarting(void) {
|
||||
HALSIM_SetProgramStarted();
|
||||
}
|
||||
|
||||
void HAL_ObserveUserProgramDisabled(void) {
|
||||
// TODO
|
||||
@@ -228,12 +237,16 @@ HAL_Bool HAL_IsNewControlData(void) {
|
||||
std::scoped_lock lock(newDSDataAvailableMutex);
|
||||
int& lastCount = GetThreadLocalLastCount();
|
||||
int currentCount = newDSDataAvailableCounter;
|
||||
if (lastCount == currentCount) return false;
|
||||
if (lastCount == currentCount) {
|
||||
return false;
|
||||
}
|
||||
lastCount = currentCount;
|
||||
return true;
|
||||
}
|
||||
|
||||
void HAL_WaitForDSData(void) { HAL_WaitForDSDataTimeout(0); }
|
||||
void HAL_WaitForDSData(void) {
|
||||
HAL_WaitForDSDataTimeout(0);
|
||||
}
|
||||
|
||||
HAL_Bool HAL_WaitForDSDataTimeout(double timeout) {
|
||||
std::unique_lock lock(newDSDataAvailableMutex);
|
||||
@@ -270,7 +283,9 @@ constexpr int32_t refNumber = 42;
|
||||
static int32_t newDataOccur(uint32_t refNum) {
|
||||
// Since we could get other values, require our specific handle
|
||||
// to signal our threads
|
||||
if (refNum != refNumber) return 0;
|
||||
if (refNum != refNumber) {
|
||||
return 0;
|
||||
}
|
||||
SimDriverStationData->CallNewDataCallbacks();
|
||||
std::scoped_lock lock(newDSDataAvailableMutex);
|
||||
// Nofify all threads
|
||||
@@ -284,11 +299,15 @@ void HAL_InitializeDriverStation(void) {
|
||||
static std::atomic_bool initialized{false};
|
||||
static wpi::mutex initializeMutex;
|
||||
// Initial check, as if it's true initialization has finished
|
||||
if (initialized) return;
|
||||
if (initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::scoped_lock lock(initializeMutex);
|
||||
// Second check in case another thread was waiting
|
||||
if (initialized) return;
|
||||
if (initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
SimDriverStationData->ResetData();
|
||||
|
||||
@@ -300,6 +319,8 @@ void HAL_InitializeDriverStation(void) {
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
void HAL_ReleaseDSMutex(void) { newDataOccur(refNumber); }
|
||||
void HAL_ReleaseDSMutex(void) {
|
||||
newDataOccur(refNumber);
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
|
||||
@@ -62,14 +62,18 @@ HAL_DutyCycleHandle HAL_InitializeDutyCycle(HAL_Handle digitalSourceHandle,
|
||||
void HAL_FreeDutyCycle(HAL_DutyCycleHandle dutyCycleHandle) {
|
||||
auto dutyCycle = dutyCycleHandles->Get(dutyCycleHandle);
|
||||
dutyCycleHandles->Free(dutyCycleHandle);
|
||||
if (dutyCycle == nullptr) return;
|
||||
if (dutyCycle == nullptr) {
|
||||
return;
|
||||
}
|
||||
SimDutyCycleData[dutyCycle->index].initialized = false;
|
||||
}
|
||||
|
||||
void HAL_SetDutyCycleSimDevice(HAL_EncoderHandle handle,
|
||||
HAL_SimDeviceHandle device) {
|
||||
auto dutyCycle = dutyCycleHandles->Get(handle);
|
||||
if (dutyCycle == nullptr) return;
|
||||
if (dutyCycle == nullptr) {
|
||||
return;
|
||||
}
|
||||
SimDutyCycleData[dutyCycle->index].simDevice = device;
|
||||
}
|
||||
|
||||
|
||||
@@ -93,7 +93,9 @@ HAL_EncoderHandle HAL_InitializeEncoder(
|
||||
void HAL_FreeEncoder(HAL_EncoderHandle encoderHandle, int32_t* status) {
|
||||
auto encoder = encoderHandles->Get(encoderHandle);
|
||||
encoderHandles->Free(encoderHandle);
|
||||
if (encoder == nullptr) return;
|
||||
if (encoder == nullptr) {
|
||||
return;
|
||||
}
|
||||
if (isHandleType(encoder->nativeHandle, HAL_HandleEnum::FPGAEncoder)) {
|
||||
fpgaEncoderHandles->Free(encoder->nativeHandle);
|
||||
} else if (isHandleType(encoder->nativeHandle, HAL_HandleEnum::Counter)) {
|
||||
@@ -105,7 +107,9 @@ void HAL_FreeEncoder(HAL_EncoderHandle encoderHandle, int32_t* status) {
|
||||
void HAL_SetEncoderSimDevice(HAL_EncoderHandle handle,
|
||||
HAL_SimDeviceHandle device) {
|
||||
auto encoder = encoderHandles->Get(handle);
|
||||
if (encoder == nullptr) return;
|
||||
if (encoder == nullptr) {
|
||||
return;
|
||||
}
|
||||
SimEncoderData[encoder->index].simDevice = device;
|
||||
}
|
||||
|
||||
|
||||
@@ -85,7 +85,9 @@ int HAL_LoadOneExtension(const char* library) {
|
||||
auto init = reinterpret_cast<halsim_extension_init_func_t*>(
|
||||
DLSYM(handle, "HALSIM_InitExtension"));
|
||||
|
||||
if (init) rc = (*init)();
|
||||
if (init) {
|
||||
rc = (*init)();
|
||||
}
|
||||
|
||||
if (rc != 0) {
|
||||
wpi::outs() << "HAL Extensions: Failed to load extension\n";
|
||||
@@ -114,7 +116,9 @@ int HAL_LoadExtensions(void) {
|
||||
for (auto& libref : libraries) {
|
||||
wpi::SmallString<128> library(libref);
|
||||
rc = HAL_LoadOneExtension(library.c_str());
|
||||
if (rc < 0) break;
|
||||
if (rc < 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
@@ -122,8 +126,9 @@ int HAL_LoadExtensions(void) {
|
||||
void HAL_RegisterExtension(const char* name, void* data) {
|
||||
std::scoped_lock lock(gExtensionRegistryMutex);
|
||||
gExtensionRegistry.emplace_back(name, data);
|
||||
for (auto&& listener : gExtensionListeners)
|
||||
for (auto&& listener : gExtensionListeners) {
|
||||
listener.second(listener.first, name, data);
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_RegisterExtensionListener(void* param,
|
||||
@@ -131,8 +136,9 @@ void HAL_RegisterExtensionListener(void* param,
|
||||
void* data)) {
|
||||
std::scoped_lock lock(gExtensionRegistryMutex);
|
||||
gExtensionListeners.emplace_back(param, func);
|
||||
for (auto&& extension : gExtensionRegistry)
|
||||
for (auto&& extension : gExtensionRegistry) {
|
||||
func(param, extension.first, extension.second);
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_SetShowExtensionsNotFoundMessages(HAL_Bool showMessage) {
|
||||
|
||||
@@ -43,8 +43,9 @@ class SimPeriodicCallbackRegistry : public impl::SimCallbackRegistryBase {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
#endif
|
||||
if (m_callbacks) {
|
||||
for (auto&& cb : *m_callbacks)
|
||||
for (auto&& cb : *m_callbacks) {
|
||||
reinterpret_cast<HALSIM_SimPeriodicCallback>(cb.callback)(cb.param);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -122,14 +123,20 @@ extern "C" {
|
||||
|
||||
HAL_PortHandle HAL_GetPort(int32_t channel) {
|
||||
// Dont allow a number that wouldn't fit in a uint8_t
|
||||
if (channel < 0 || channel >= 255) return HAL_kInvalidHandle;
|
||||
if (channel < 0 || channel >= 255) {
|
||||
return HAL_kInvalidHandle;
|
||||
}
|
||||
return createPortHandle(channel, 1);
|
||||
}
|
||||
|
||||
HAL_PortHandle HAL_GetPortWithModule(int32_t module, int32_t channel) {
|
||||
// Dont allow a number that wouldn't fit in a uint8_t
|
||||
if (channel < 0 || channel >= 255) return HAL_kInvalidHandle;
|
||||
if (module < 0 || module >= 255) return HAL_kInvalidHandle;
|
||||
if (channel < 0 || channel >= 255) {
|
||||
return HAL_kInvalidHandle;
|
||||
}
|
||||
if (module < 0 || module >= 255) {
|
||||
return HAL_kInvalidHandle;
|
||||
}
|
||||
return createPortHandle(channel, module);
|
||||
}
|
||||
|
||||
@@ -250,9 +257,13 @@ const char* HAL_GetErrorMessage(int32_t code) {
|
||||
}
|
||||
}
|
||||
|
||||
HAL_RuntimeType HAL_GetRuntimeType(void) { return runtimeType; }
|
||||
HAL_RuntimeType HAL_GetRuntimeType(void) {
|
||||
return runtimeType;
|
||||
}
|
||||
|
||||
void HALSIM_SetRuntimeType(HAL_RuntimeType type) { runtimeType = type; }
|
||||
void HALSIM_SetRuntimeType(HAL_RuntimeType type) {
|
||||
runtimeType = type;
|
||||
}
|
||||
|
||||
int32_t HAL_GetFPGAVersion(int32_t* status) {
|
||||
return 2018; // Automatically script this at some point
|
||||
@@ -262,13 +273,17 @@ int64_t HAL_GetFPGARevision(int32_t* status) {
|
||||
return 0; // TODO: Find a better number to return;
|
||||
}
|
||||
|
||||
uint64_t HAL_GetFPGATime(int32_t* status) { return hal::GetFPGATime(); }
|
||||
uint64_t HAL_GetFPGATime(int32_t* status) {
|
||||
return hal::GetFPGATime();
|
||||
}
|
||||
|
||||
uint64_t HAL_ExpandFPGATime(uint32_t unexpanded_lower, int32_t* status) {
|
||||
// Capture the current FPGA time. This will give us the upper half of the
|
||||
// clock.
|
||||
uint64_t fpga_time = HAL_GetFPGATime(status);
|
||||
if (*status != 0) return 0;
|
||||
if (*status != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Now, we need to detect the case where the lower bits rolled over after we
|
||||
// sampled. In that case, the upper bits will be 1 bigger than they should
|
||||
@@ -302,11 +317,15 @@ HAL_Bool HAL_Initialize(int32_t timeout, int32_t mode) {
|
||||
static std::atomic_bool initialized{false};
|
||||
static wpi::mutex initializeMutex;
|
||||
// Initial check, as if it's true initialization has finished
|
||||
if (initialized) return true;
|
||||
if (initialized) {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::scoped_lock lock(initializeMutex);
|
||||
// Second check in case another thread was waiting
|
||||
if (initialized) return true;
|
||||
if (initialized) {
|
||||
return true;
|
||||
}
|
||||
|
||||
hal::init::InitializeHAL();
|
||||
|
||||
@@ -334,7 +353,9 @@ HAL_Bool HAL_Initialize(int32_t timeout, int32_t mode) {
|
||||
#endif // _WIN32
|
||||
|
||||
wpi::outs().SetUnbuffered();
|
||||
if (HAL_LoadExtensions() < 0) return false;
|
||||
if (HAL_LoadExtensions() < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true; // Add initialization if we need to at a later point
|
||||
}
|
||||
@@ -355,9 +376,13 @@ void HAL_OnShutdown(void* param, void (*func)(void*)) {
|
||||
gOnShutdown.emplace_back(param, func);
|
||||
}
|
||||
|
||||
void HAL_SimPeriodicBefore(void) { gSimPeriodicBefore(); }
|
||||
void HAL_SimPeriodicBefore(void) {
|
||||
gSimPeriodicBefore();
|
||||
}
|
||||
|
||||
void HAL_SimPeriodicAfter(void) { gSimPeriodicAfter(); }
|
||||
void HAL_SimPeriodicAfter(void) {
|
||||
gSimPeriodicAfter();
|
||||
}
|
||||
|
||||
int32_t HALSIM_RegisterSimPeriodicBeforeCallback(
|
||||
HALSIM_SimPeriodicCallback callback, void* param) {
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
namespace hal {
|
||||
namespace init {
|
||||
std::atomic_bool HAL_IsInitialized{false};
|
||||
void RunInitialize() { HAL_Initialize(500, 0); }
|
||||
void RunInitialize() {
|
||||
HAL_Initialize(500, 0);
|
||||
}
|
||||
} // namespace init
|
||||
} // namespace hal
|
||||
|
||||
@@ -11,7 +11,9 @@ namespace init {
|
||||
extern std::atomic_bool HAL_IsInitialized;
|
||||
extern void RunInitialize();
|
||||
static inline void CheckInit() {
|
||||
if (HAL_IsInitialized.load(std::memory_order_relaxed)) return;
|
||||
if (HAL_IsInitialized.load(std::memory_order_relaxed)) {
|
||||
return;
|
||||
}
|
||||
RunInitialize();
|
||||
}
|
||||
|
||||
|
||||
@@ -37,5 +37,7 @@ int32_t HAL_ReadI2C(HAL_I2CPort port, int32_t deviceAddress, uint8_t* buffer,
|
||||
SimI2CData[port].Read(deviceAddress, buffer, count);
|
||||
return 0;
|
||||
}
|
||||
void HAL_CloseI2C(HAL_I2CPort port) { SimI2CData[port].initialized = false; }
|
||||
void HAL_CloseI2C(HAL_I2CPort port) {
|
||||
SimI2CData[port].initialized = false;
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -125,18 +125,30 @@ static void ProcessInterruptDigitalSynchronous(const char* name, void* param,
|
||||
SynchronousWaitDataHandle handle =
|
||||
static_cast<SynchronousWaitDataHandle>(handleTmp);
|
||||
auto interruptData = synchronousInterruptHandles->Get(handle);
|
||||
if (interruptData == nullptr) return;
|
||||
if (interruptData == nullptr) {
|
||||
return;
|
||||
}
|
||||
auto interrupt = interruptHandles->Get(interruptData->interruptHandle);
|
||||
if (interrupt == nullptr) return;
|
||||
if (interrupt == nullptr) {
|
||||
return;
|
||||
}
|
||||
// Have a valid interrupt
|
||||
if (value->type != HAL_Type::HAL_BOOLEAN) return;
|
||||
if (value->type != HAL_Type::HAL_BOOLEAN) {
|
||||
return;
|
||||
}
|
||||
bool retVal = value->data.v_boolean;
|
||||
// If no change in interrupt, return;
|
||||
if (retVal == interrupt->previousState) return;
|
||||
if (retVal == interrupt->previousState) {
|
||||
return;
|
||||
}
|
||||
// If its a falling change, and we dont fire on falling return
|
||||
if (interrupt->previousState && !interrupt->fireOnDown) return;
|
||||
if (interrupt->previousState && !interrupt->fireOnDown) {
|
||||
return;
|
||||
}
|
||||
// If its a rising change, and we dont fire on rising return.
|
||||
if (!interrupt->previousState && !interrupt->fireOnUp) return;
|
||||
if (!interrupt->previousState && !interrupt->fireOnUp) {
|
||||
return;
|
||||
}
|
||||
|
||||
interruptData->waitPredicate = true;
|
||||
|
||||
@@ -158,11 +170,17 @@ static void ProcessInterruptAnalogSynchronous(const char* name, void* param,
|
||||
SynchronousWaitDataHandle handle =
|
||||
static_cast<SynchronousWaitDataHandle>(handleTmp);
|
||||
auto interruptData = synchronousInterruptHandles->Get(handle);
|
||||
if (interruptData == nullptr) return;
|
||||
if (interruptData == nullptr) {
|
||||
return;
|
||||
}
|
||||
auto interrupt = interruptHandles->Get(interruptData->interruptHandle);
|
||||
if (interrupt == nullptr) return;
|
||||
if (interrupt == nullptr) {
|
||||
return;
|
||||
}
|
||||
// Have a valid interrupt
|
||||
if (value->type != HAL_Type::HAL_DOUBLE) return;
|
||||
if (value->type != HAL_Type::HAL_DOUBLE) {
|
||||
return;
|
||||
}
|
||||
int32_t status = 0;
|
||||
bool retVal = GetAnalogTriggerValue(interrupt->portHandle,
|
||||
interrupt->trigType, &status);
|
||||
@@ -173,11 +191,17 @@ static void ProcessInterruptAnalogSynchronous(const char* name, void* param,
|
||||
interruptData->waitCond.notify_all();
|
||||
}
|
||||
// If no change in interrupt, return;
|
||||
if (retVal == interrupt->previousState) return;
|
||||
if (retVal == interrupt->previousState) {
|
||||
return;
|
||||
}
|
||||
// If its a falling change, and we dont fire on falling return
|
||||
if (interrupt->previousState && !interrupt->fireOnDown) return;
|
||||
if (interrupt->previousState && !interrupt->fireOnDown) {
|
||||
return;
|
||||
}
|
||||
// If its a rising change, and we dont fire on rising return.
|
||||
if (!interrupt->previousState && !interrupt->fireOnUp) return;
|
||||
if (!interrupt->previousState && !interrupt->fireOnUp) {
|
||||
return;
|
||||
}
|
||||
|
||||
interruptData->waitPredicate = true;
|
||||
|
||||
@@ -204,7 +228,9 @@ static int64_t WaitForInterruptDigital(HAL_InterruptHandle handle,
|
||||
|
||||
int32_t digitalIndex = GetDigitalInputChannel(interrupt->portHandle, &status);
|
||||
|
||||
if (status != 0) return WaitResult::Timeout;
|
||||
if (status != 0) {
|
||||
return WaitResult::Timeout;
|
||||
}
|
||||
|
||||
interrupt->previousState = SimDIOData[digitalIndex].value;
|
||||
|
||||
@@ -235,7 +261,9 @@ static int64_t WaitForInterruptDigital(HAL_InterruptHandle handle,
|
||||
(void)synchronousInterruptHandles->Free(dataHandle);
|
||||
|
||||
// Check for what to return
|
||||
if (timedOut) return WaitResult::Timeout;
|
||||
if (timedOut) {
|
||||
return WaitResult::Timeout;
|
||||
}
|
||||
// True => false, Falling
|
||||
if (interrupt->previousState) {
|
||||
// Set our return value and our timestamps
|
||||
@@ -265,12 +293,16 @@ static int64_t WaitForInterruptAnalog(HAL_InterruptHandle handle,
|
||||
interrupt->previousState = GetAnalogTriggerValue(
|
||||
interrupt->portHandle, interrupt->trigType, &status);
|
||||
|
||||
if (status != 0) return WaitResult::Timeout;
|
||||
if (status != 0) {
|
||||
return WaitResult::Timeout;
|
||||
}
|
||||
|
||||
int32_t analogIndex =
|
||||
GetAnalogTriggerInputIndex(interrupt->portHandle, &status);
|
||||
|
||||
if (status != 0) return WaitResult::Timeout;
|
||||
if (status != 0) {
|
||||
return WaitResult::Timeout;
|
||||
}
|
||||
|
||||
int32_t uid = SimAnalogInData[analogIndex].voltage.RegisterCallback(
|
||||
&ProcessInterruptAnalogSynchronous,
|
||||
@@ -299,7 +331,9 @@ static int64_t WaitForInterruptAnalog(HAL_InterruptHandle handle,
|
||||
(void)synchronousInterruptHandles->Free(dataHandle);
|
||||
|
||||
// Check for what to return
|
||||
if (timedOut) return WaitResult::Timeout;
|
||||
if (timedOut) {
|
||||
return WaitResult::Timeout;
|
||||
}
|
||||
// True => false, Falling
|
||||
if (interrupt->previousState) {
|
||||
// Set our return value and our timestamps
|
||||
@@ -342,28 +376,40 @@ static void ProcessInterruptDigitalAsynchronous(const char* name, void* param,
|
||||
uintptr_t handleTmp = reinterpret_cast<uintptr_t>(param);
|
||||
HAL_InterruptHandle handle = static_cast<HAL_InterruptHandle>(handleTmp);
|
||||
auto interrupt = interruptHandles->Get(handle);
|
||||
if (interrupt == nullptr) return;
|
||||
if (interrupt == nullptr) {
|
||||
return;
|
||||
}
|
||||
// Have a valid interrupt
|
||||
if (value->type != HAL_Type::HAL_BOOLEAN) return;
|
||||
if (value->type != HAL_Type::HAL_BOOLEAN) {
|
||||
return;
|
||||
}
|
||||
bool retVal = value->data.v_boolean;
|
||||
// If no change in interrupt, return;
|
||||
if (retVal == interrupt->previousState) return;
|
||||
if (retVal == interrupt->previousState) {
|
||||
return;
|
||||
}
|
||||
int32_t mask = 0;
|
||||
if (interrupt->previousState) {
|
||||
interrupt->previousState = retVal;
|
||||
interrupt->fallingTimestamp = hal::GetFPGATime();
|
||||
mask = 1 << (8 + interrupt->index);
|
||||
if (!interrupt->fireOnDown) return;
|
||||
if (!interrupt->fireOnDown) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
interrupt->previousState = retVal;
|
||||
interrupt->risingTimestamp = hal::GetFPGATime();
|
||||
mask = 1 << (interrupt->index);
|
||||
if (!interrupt->fireOnUp) return;
|
||||
if (!interrupt->fireOnUp) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// run callback
|
||||
auto callback = interrupt->callbackFunction;
|
||||
if (callback == nullptr) return;
|
||||
if (callback == nullptr) {
|
||||
return;
|
||||
}
|
||||
callback(mask, interrupt->callbackParam);
|
||||
}
|
||||
|
||||
@@ -374,31 +420,45 @@ static void ProcessInterruptAnalogAsynchronous(const char* name, void* param,
|
||||
uintptr_t handleTmp = reinterpret_cast<uintptr_t>(param);
|
||||
HAL_InterruptHandle handle = static_cast<HAL_InterruptHandle>(handleTmp);
|
||||
auto interrupt = interruptHandles->Get(handle);
|
||||
if (interrupt == nullptr) return;
|
||||
if (interrupt == nullptr) {
|
||||
return;
|
||||
}
|
||||
// Have a valid interrupt
|
||||
if (value->type != HAL_Type::HAL_DOUBLE) return;
|
||||
if (value->type != HAL_Type::HAL_DOUBLE) {
|
||||
return;
|
||||
}
|
||||
int32_t status = 0;
|
||||
bool retVal = GetAnalogTriggerValue(interrupt->portHandle,
|
||||
interrupt->trigType, &status);
|
||||
if (status != 0) return;
|
||||
if (status != 0) {
|
||||
return;
|
||||
}
|
||||
// If no change in interrupt, return;
|
||||
if (retVal == interrupt->previousState) return;
|
||||
if (retVal == interrupt->previousState) {
|
||||
return;
|
||||
}
|
||||
int mask = 0;
|
||||
if (interrupt->previousState) {
|
||||
interrupt->previousState = retVal;
|
||||
interrupt->fallingTimestamp = hal::GetFPGATime();
|
||||
if (!interrupt->fireOnDown) return;
|
||||
if (!interrupt->fireOnDown) {
|
||||
return;
|
||||
}
|
||||
mask = 1 << (8 + interrupt->index);
|
||||
} else {
|
||||
interrupt->previousState = retVal;
|
||||
interrupt->risingTimestamp = hal::GetFPGATime();
|
||||
if (!interrupt->fireOnUp) return;
|
||||
if (!interrupt->fireOnUp) {
|
||||
return;
|
||||
}
|
||||
mask = 1 << (interrupt->index);
|
||||
}
|
||||
|
||||
// run callback
|
||||
auto callback = interrupt->callbackFunction;
|
||||
if (callback == nullptr) return;
|
||||
if (callback == nullptr) {
|
||||
return;
|
||||
}
|
||||
callback(mask, interrupt->callbackParam);
|
||||
}
|
||||
|
||||
@@ -406,7 +466,9 @@ static void EnableInterruptsDigital(HAL_InterruptHandle handle,
|
||||
Interrupt* interrupt) {
|
||||
int32_t status = 0;
|
||||
int32_t digitalIndex = GetDigitalInputChannel(interrupt->portHandle, &status);
|
||||
if (status != 0) return;
|
||||
if (status != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
interrupt->previousState = SimDIOData[digitalIndex].value;
|
||||
|
||||
@@ -421,12 +483,16 @@ static void EnableInterruptsAnalog(HAL_InterruptHandle handle,
|
||||
int32_t status = 0;
|
||||
int32_t analogIndex =
|
||||
GetAnalogTriggerInputIndex(interrupt->portHandle, &status);
|
||||
if (status != 0) return;
|
||||
if (status != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
status = 0;
|
||||
interrupt->previousState = GetAnalogTriggerValue(
|
||||
interrupt->portHandle, interrupt->trigType, &status);
|
||||
if (status != 0) return;
|
||||
if (status != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
int32_t uid = SimAnalogInData[analogIndex].voltage.RegisterCallback(
|
||||
&ProcessInterruptAnalogAsynchronous,
|
||||
@@ -469,20 +535,26 @@ void HAL_DisableInterrupts(HAL_InterruptHandle interruptHandle,
|
||||
}
|
||||
|
||||
// No need to disable if we are already disabled
|
||||
if (interrupt->callbackId < 0) return;
|
||||
if (interrupt->callbackId < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (interrupt->isAnalog) {
|
||||
// Do analog
|
||||
int32_t status = 0;
|
||||
int32_t analogIndex =
|
||||
GetAnalogTriggerInputIndex(interrupt->portHandle, &status);
|
||||
if (status != 0) return;
|
||||
if (status != 0) {
|
||||
return;
|
||||
}
|
||||
SimAnalogInData[analogIndex].voltage.CancelCallback(interrupt->callbackId);
|
||||
} else {
|
||||
int32_t status = 0;
|
||||
int32_t digitalIndex =
|
||||
GetDigitalInputChannel(interrupt->portHandle, &status);
|
||||
if (status != 0) return;
|
||||
if (status != 0) {
|
||||
return;
|
||||
}
|
||||
SimDIOData[digitalIndex].value.CancelCallback(interrupt->callbackId);
|
||||
}
|
||||
interrupt->callbackId = -1;
|
||||
|
||||
@@ -22,7 +22,9 @@ static std::atomic<uint64_t> programStepTime{0};
|
||||
|
||||
namespace hal {
|
||||
namespace init {
|
||||
void InitializeMockHooks() { wpi::SetNowImpl(GetFPGATime); }
|
||||
void InitializeMockHooks() {
|
||||
wpi::SetNowImpl(GetFPGATime);
|
||||
}
|
||||
} // namespace init
|
||||
} // namespace hal
|
||||
|
||||
@@ -30,11 +32,15 @@ namespace hal {
|
||||
void RestartTiming() {
|
||||
programStartTime = wpi::NowDefault();
|
||||
programStepTime = 0;
|
||||
if (programPauseTime != 0) programPauseTime = programStartTime.load();
|
||||
if (programPauseTime != 0) {
|
||||
programPauseTime = programStartTime.load();
|
||||
}
|
||||
}
|
||||
|
||||
void PauseTiming() {
|
||||
if (programPauseTime == 0) programPauseTime = wpi::NowDefault();
|
||||
if (programPauseTime == 0) {
|
||||
programPauseTime = wpi::NowDefault();
|
||||
}
|
||||
}
|
||||
|
||||
void ResumeTiming() {
|
||||
@@ -44,20 +50,32 @@ void ResumeTiming() {
|
||||
}
|
||||
}
|
||||
|
||||
bool IsTimingPaused() { return programPauseTime != 0; }
|
||||
bool IsTimingPaused() {
|
||||
return programPauseTime != 0;
|
||||
}
|
||||
|
||||
void StepTiming(uint64_t delta) { programStepTime += delta; }
|
||||
void StepTiming(uint64_t delta) {
|
||||
programStepTime += delta;
|
||||
}
|
||||
|
||||
uint64_t GetFPGATime() {
|
||||
uint64_t curTime = programPauseTime;
|
||||
if (curTime == 0) curTime = wpi::NowDefault();
|
||||
if (curTime == 0) {
|
||||
curTime = wpi::NowDefault();
|
||||
}
|
||||
return curTime + programStepTime - programStartTime;
|
||||
}
|
||||
|
||||
double GetFPGATimestamp() { return GetFPGATime() * 1.0e-6; }
|
||||
double GetFPGATimestamp() {
|
||||
return GetFPGATime() * 1.0e-6;
|
||||
}
|
||||
|
||||
void SetProgramStarted() { programStarted = true; }
|
||||
bool GetProgramStarted() { return programStarted; }
|
||||
void SetProgramStarted() {
|
||||
programStarted = true;
|
||||
}
|
||||
bool GetProgramStarted() {
|
||||
return programStarted;
|
||||
}
|
||||
} // namespace hal
|
||||
|
||||
using namespace hal;
|
||||
@@ -72,11 +90,17 @@ void HALSIM_WaitForProgramStart(void) {
|
||||
}
|
||||
}
|
||||
|
||||
void HALSIM_SetProgramStarted(void) { SetProgramStarted(); }
|
||||
void HALSIM_SetProgramStarted(void) {
|
||||
SetProgramStarted();
|
||||
}
|
||||
|
||||
HAL_Bool HALSIM_GetProgramStarted(void) { return GetProgramStarted(); }
|
||||
HAL_Bool HALSIM_GetProgramStarted(void) {
|
||||
return GetProgramStarted();
|
||||
}
|
||||
|
||||
void HALSIM_RestartTiming(void) { RestartTiming(); }
|
||||
void HALSIM_RestartTiming(void) {
|
||||
RestartTiming();
|
||||
}
|
||||
|
||||
void HALSIM_PauseTiming(void) {
|
||||
PauseTiming();
|
||||
@@ -88,7 +112,9 @@ void HALSIM_ResumeTiming(void) {
|
||||
ResumeNotifiers();
|
||||
}
|
||||
|
||||
HAL_Bool HALSIM_IsTimingPaused(void) { return IsTimingPaused(); }
|
||||
HAL_Bool HALSIM_IsTimingPaused(void) {
|
||||
return IsTimingPaused();
|
||||
}
|
||||
|
||||
void HALSIM_StepTiming(uint64_t delta) {
|
||||
WaitNotifiers();
|
||||
|
||||
@@ -68,7 +68,9 @@ void InitializeNotifier() {
|
||||
}
|
||||
} // namespace init
|
||||
|
||||
void PauseNotifiers() { notifiersPaused = true; }
|
||||
void PauseNotifiers() {
|
||||
notifiersPaused = true;
|
||||
}
|
||||
|
||||
void ResumeNotifiers() {
|
||||
notifiersPaused = false;
|
||||
@@ -107,7 +109,9 @@ void WaitNotifiers() {
|
||||
// No longer need to wait for it, put at end so it can be erased
|
||||
std::swap(it, waiters[--end]);
|
||||
}
|
||||
if (count == 0) break;
|
||||
if (count == 0) {
|
||||
break;
|
||||
}
|
||||
waiters.resize(count);
|
||||
notifiersWaiterCond.wait_for(ulock, std::chrono::duration<double>(1));
|
||||
}
|
||||
@@ -148,7 +152,9 @@ void WakeupWaitNotifiers() {
|
||||
// No longer need to wait for it, put at end so it can be erased
|
||||
it.swap(waiters[--end]);
|
||||
}
|
||||
if (count == 0) break;
|
||||
if (count == 0) {
|
||||
break;
|
||||
}
|
||||
waiters.resize(count);
|
||||
notifiersWaiterCond.wait_for(ulock, std::chrono::duration<double>(1));
|
||||
}
|
||||
@@ -171,14 +177,18 @@ HAL_NotifierHandle HAL_InitializeNotifier(int32_t* status) {
|
||||
void HAL_SetNotifierName(HAL_NotifierHandle notifierHandle, const char* name,
|
||||
int32_t* status) {
|
||||
auto notifier = notifierHandles->Get(notifierHandle);
|
||||
if (!notifier) return;
|
||||
if (!notifier) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(notifier->mutex);
|
||||
notifier->name = name;
|
||||
}
|
||||
|
||||
void HAL_StopNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) {
|
||||
auto notifier = notifierHandles->Get(notifierHandle);
|
||||
if (!notifier) return;
|
||||
if (!notifier) {
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
std::scoped_lock lock(notifier->mutex);
|
||||
@@ -190,7 +200,9 @@ void HAL_StopNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) {
|
||||
|
||||
void HAL_CleanNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) {
|
||||
auto notifier = notifierHandles->Free(notifierHandle);
|
||||
if (!notifier) return;
|
||||
if (!notifier) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Just in case HAL_StopNotifier() wasn't called...
|
||||
{
|
||||
@@ -204,7 +216,9 @@ void HAL_CleanNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) {
|
||||
void HAL_UpdateNotifierAlarm(HAL_NotifierHandle notifierHandle,
|
||||
uint64_t triggerTime, int32_t* status) {
|
||||
auto notifier = notifierHandles->Get(notifierHandle);
|
||||
if (!notifier) return;
|
||||
if (!notifier) {
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
std::scoped_lock lock(notifier->mutex);
|
||||
@@ -219,7 +233,9 @@ void HAL_UpdateNotifierAlarm(HAL_NotifierHandle notifierHandle,
|
||||
void HAL_CancelNotifierAlarm(HAL_NotifierHandle notifierHandle,
|
||||
int32_t* status) {
|
||||
auto notifier = notifierHandles->Get(notifierHandle);
|
||||
if (!notifier) return;
|
||||
if (!notifier) {
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
std::scoped_lock lock(notifier->mutex);
|
||||
@@ -230,7 +246,9 @@ void HAL_CancelNotifierAlarm(HAL_NotifierHandle notifierHandle,
|
||||
uint64_t HAL_WaitForNotifierAlarm(HAL_NotifierHandle notifierHandle,
|
||||
int32_t* status) {
|
||||
auto notifier = notifierHandles->Get(notifierHandle);
|
||||
if (!notifier) return 0;
|
||||
if (!notifier) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::unique_lock ulock(notifiersWaiterMutex);
|
||||
std::unique_lock lock(notifier->mutex);
|
||||
@@ -265,8 +283,9 @@ uint64_t HALSIM_GetNextNotifierTimeout(void) {
|
||||
notifierHandles->ForEach([&](HAL_NotifierHandle, Notifier* notifier) {
|
||||
std::scoped_lock lock(notifier->mutex);
|
||||
if (notifier->active && notifier->waitTimeValid &&
|
||||
timeout > notifier->waitTime)
|
||||
timeout > notifier->waitTime) {
|
||||
timeout = notifier->waitTime;
|
||||
}
|
||||
});
|
||||
return timeout;
|
||||
}
|
||||
@@ -275,7 +294,9 @@ int32_t HALSIM_GetNumNotifiers(void) {
|
||||
int32_t count = 0;
|
||||
notifierHandles->ForEach([&](HAL_NotifierHandle, Notifier* notifier) {
|
||||
std::scoped_lock lock(notifier->mutex);
|
||||
if (notifier->active) ++count;
|
||||
if (notifier->active) {
|
||||
++count;
|
||||
}
|
||||
});
|
||||
return count;
|
||||
}
|
||||
@@ -284,7 +305,9 @@ int32_t HALSIM_GetNotifierInfo(struct HALSIM_NotifierInfo* arr, int32_t size) {
|
||||
int32_t num = 0;
|
||||
notifierHandles->ForEach([&](HAL_NotifierHandle handle, Notifier* notifier) {
|
||||
std::scoped_lock lock(notifier->mutex);
|
||||
if (!notifier->active) return;
|
||||
if (!notifier->active) {
|
||||
return;
|
||||
}
|
||||
if (num < size) {
|
||||
arr[num].handle = handle;
|
||||
if (notifier->name.empty()) {
|
||||
|
||||
@@ -51,7 +51,9 @@ HAL_Bool HAL_CheckPDPChannel(int32_t channel) {
|
||||
return channel < kNumPDPChannels && channel >= 0;
|
||||
}
|
||||
|
||||
void HAL_CleanPDP(HAL_PDPHandle handle) { HAL_CleanCAN(handle); }
|
||||
void HAL_CleanPDP(HAL_PDPHandle handle) {
|
||||
HAL_CleanCAN(handle);
|
||||
}
|
||||
|
||||
double HAL_GetPDPTemperature(HAL_PDPHandle handle, int32_t* status) {
|
||||
auto module = hal::can::GetCANModuleFromHandle(handle, status);
|
||||
|
||||
@@ -24,7 +24,9 @@ extern "C" {
|
||||
HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
|
||||
int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
if (*status != 0) return HAL_kInvalidHandle;
|
||||
if (*status != 0) {
|
||||
return HAL_kInvalidHandle;
|
||||
}
|
||||
|
||||
int16_t channel = getPortHandleChannel(portHandle);
|
||||
if (channel == InvalidHandleIndex) {
|
||||
@@ -43,8 +45,9 @@ HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
|
||||
auto handle =
|
||||
digitalChannelHandles->Allocate(channel, HAL_HandleEnum::PWM, status);
|
||||
|
||||
if (*status != 0)
|
||||
if (*status != 0) {
|
||||
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
|
||||
}
|
||||
|
||||
auto port = digitalChannelHandles->Get(handle, HAL_HandleEnum::PWM);
|
||||
if (port == nullptr) { // would only occur on thread issue.
|
||||
@@ -89,7 +92,9 @@ void HAL_SetPWMConfig(HAL_DigitalHandle pwmPortHandle, double max,
|
||||
// calculate the loop time in milliseconds
|
||||
double loopTime =
|
||||
HAL_GetPWMLoopTiming(status) / (kSystemClockTicksPerMicrosecond * 1e3);
|
||||
if (*status != 0) return;
|
||||
if (*status != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
int32_t maxPwm = static_cast<int32_t>((max - kDefaultPwmCenter) / loopTime +
|
||||
kDefaultPwmStepsDown - 1);
|
||||
@@ -249,8 +254,12 @@ double HAL_GetPWMSpeed(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
|
||||
}
|
||||
|
||||
double speed = SimPWMData[port->channel].speed;
|
||||
if (speed > 1) speed = 1;
|
||||
if (speed < -1) speed = -1;
|
||||
if (speed > 1) {
|
||||
speed = 1;
|
||||
}
|
||||
if (speed < -1) {
|
||||
speed = -1;
|
||||
}
|
||||
return speed;
|
||||
}
|
||||
|
||||
@@ -266,8 +275,12 @@ double HAL_GetPWMPosition(HAL_DigitalHandle pwmPortHandle, int32_t* status) {
|
||||
}
|
||||
|
||||
double position = SimPWMData[port->channel].position;
|
||||
if (position > 1) position = 1;
|
||||
if (position < 0) position = 0;
|
||||
if (position > 1) {
|
||||
position = 1;
|
||||
}
|
||||
if (position < 0) {
|
||||
position = 0;
|
||||
}
|
||||
return position;
|
||||
}
|
||||
|
||||
@@ -293,7 +306,11 @@ void HAL_SetPWMPeriodScale(HAL_DigitalHandle pwmPortHandle, int32_t squelchMask,
|
||||
SimPWMData[port->channel].periodScale = squelchMask;
|
||||
}
|
||||
|
||||
int32_t HAL_GetPWMLoopTiming(int32_t* status) { return kExpectedLoopTiming; }
|
||||
int32_t HAL_GetPWMLoopTiming(int32_t* status) {
|
||||
return kExpectedLoopTiming;
|
||||
}
|
||||
|
||||
uint64_t HAL_GetPWMCycleStartTime(int32_t* status) { return 0; }
|
||||
uint64_t HAL_GetPWMCycleStartTime(int32_t* status) {
|
||||
return 0;
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -15,24 +15,64 @@ void InitializePorts() {}
|
||||
} // namespace hal
|
||||
|
||||
extern "C" {
|
||||
int32_t HAL_GetNumAccumulators(void) { return kNumAccumulators; }
|
||||
int32_t HAL_GetNumAnalogTriggers(void) { return kNumAnalogTriggers; }
|
||||
int32_t HAL_GetNumAnalogInputs(void) { return kNumAnalogInputs; }
|
||||
int32_t HAL_GetNumAnalogOutputs(void) { return kNumAnalogOutputs; }
|
||||
int32_t HAL_GetNumCounters(void) { return kNumCounters; }
|
||||
int32_t HAL_GetNumDigitalHeaders(void) { return kNumDigitalHeaders; }
|
||||
int32_t HAL_GetNumPWMHeaders(void) { return kNumPWMHeaders; }
|
||||
int32_t HAL_GetNumDigitalChannels(void) { return kNumDigitalChannels; }
|
||||
int32_t HAL_GetNumPWMChannels(void) { return kNumPWMChannels; }
|
||||
int32_t HAL_GetNumDigitalPWMOutputs(void) { return kNumDigitalPWMOutputs; }
|
||||
int32_t HAL_GetNumEncoders(void) { return kNumEncoders; }
|
||||
int32_t HAL_GetNumInterrupts(void) { return kNumInterrupts; }
|
||||
int32_t HAL_GetNumRelayChannels(void) { return kNumRelayChannels; }
|
||||
int32_t HAL_GetNumRelayHeaders(void) { return kNumRelayHeaders; }
|
||||
int32_t HAL_GetNumPCMModules(void) { return kNumPCMModules; }
|
||||
int32_t HAL_GetNumSolenoidChannels(void) { return kNumSolenoidChannels; }
|
||||
int32_t HAL_GetNumPDPModules(void) { return kNumPDPModules; }
|
||||
int32_t HAL_GetNumPDPChannels(void) { return kNumPDPChannels; }
|
||||
int32_t HAL_GetNumDutyCycles(void) { return kNumDutyCycles; }
|
||||
int32_t HAL_GetNumAddressableLEDs(void) { return kNumAddressableLEDs; }
|
||||
int32_t HAL_GetNumAccumulators(void) {
|
||||
return kNumAccumulators;
|
||||
}
|
||||
int32_t HAL_GetNumAnalogTriggers(void) {
|
||||
return kNumAnalogTriggers;
|
||||
}
|
||||
int32_t HAL_GetNumAnalogInputs(void) {
|
||||
return kNumAnalogInputs;
|
||||
}
|
||||
int32_t HAL_GetNumAnalogOutputs(void) {
|
||||
return kNumAnalogOutputs;
|
||||
}
|
||||
int32_t HAL_GetNumCounters(void) {
|
||||
return kNumCounters;
|
||||
}
|
||||
int32_t HAL_GetNumDigitalHeaders(void) {
|
||||
return kNumDigitalHeaders;
|
||||
}
|
||||
int32_t HAL_GetNumPWMHeaders(void) {
|
||||
return kNumPWMHeaders;
|
||||
}
|
||||
int32_t HAL_GetNumDigitalChannels(void) {
|
||||
return kNumDigitalChannels;
|
||||
}
|
||||
int32_t HAL_GetNumPWMChannels(void) {
|
||||
return kNumPWMChannels;
|
||||
}
|
||||
int32_t HAL_GetNumDigitalPWMOutputs(void) {
|
||||
return kNumDigitalPWMOutputs;
|
||||
}
|
||||
int32_t HAL_GetNumEncoders(void) {
|
||||
return kNumEncoders;
|
||||
}
|
||||
int32_t HAL_GetNumInterrupts(void) {
|
||||
return kNumInterrupts;
|
||||
}
|
||||
int32_t HAL_GetNumRelayChannels(void) {
|
||||
return kNumRelayChannels;
|
||||
}
|
||||
int32_t HAL_GetNumRelayHeaders(void) {
|
||||
return kNumRelayHeaders;
|
||||
}
|
||||
int32_t HAL_GetNumPCMModules(void) {
|
||||
return kNumPCMModules;
|
||||
}
|
||||
int32_t HAL_GetNumSolenoidChannels(void) {
|
||||
return kNumSolenoidChannels;
|
||||
}
|
||||
int32_t HAL_GetNumPDPModules(void) {
|
||||
return kNumPDPModules;
|
||||
}
|
||||
int32_t HAL_GetNumPDPChannels(void) {
|
||||
return kNumPDPChannels;
|
||||
}
|
||||
int32_t HAL_GetNumDutyCycles(void) {
|
||||
return kNumDutyCycles;
|
||||
}
|
||||
int32_t HAL_GetNumAddressableLEDs(void) {
|
||||
return kNumAddressableLEDs;
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -16,8 +16,12 @@ void InitializePower() {}
|
||||
|
||||
// TODO: Fix the naming in here
|
||||
extern "C" {
|
||||
double HAL_GetVinVoltage(int32_t* status) { return SimRoboRioData->vInVoltage; }
|
||||
double HAL_GetVinCurrent(int32_t* status) { return SimRoboRioData->vInCurrent; }
|
||||
double HAL_GetVinVoltage(int32_t* status) {
|
||||
return SimRoboRioData->vInVoltage;
|
||||
}
|
||||
double HAL_GetVinCurrent(int32_t* status) {
|
||||
return SimRoboRioData->vInCurrent;
|
||||
}
|
||||
double HAL_GetUserVoltage6V(int32_t* status) {
|
||||
return SimRoboRioData->userVoltage6V;
|
||||
}
|
||||
|
||||
@@ -36,7 +36,9 @@ extern "C" {
|
||||
HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
|
||||
int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
if (*status != 0) return HAL_kInvalidHandle;
|
||||
if (*status != 0) {
|
||||
return HAL_kInvalidHandle;
|
||||
}
|
||||
|
||||
int16_t channel = getPortHandleChannel(portHandle);
|
||||
if (channel == InvalidHandleIndex) {
|
||||
@@ -44,12 +46,15 @@ HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
|
||||
return HAL_kInvalidHandle;
|
||||
}
|
||||
|
||||
if (!fwd) channel += kNumRelayHeaders; // add 4 to reverse channels
|
||||
if (!fwd) {
|
||||
channel += kNumRelayHeaders; // add 4 to reverse channels
|
||||
}
|
||||
|
||||
auto handle = relayHandles->Allocate(channel, status);
|
||||
|
||||
if (*status != 0)
|
||||
if (*status != 0) {
|
||||
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
|
||||
}
|
||||
|
||||
auto port = relayHandles->Get(handle);
|
||||
if (port == nullptr) { // would only occur on thread issue.
|
||||
@@ -77,11 +82,14 @@ HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
|
||||
void HAL_FreeRelayPort(HAL_RelayHandle relayPortHandle) {
|
||||
auto port = relayHandles->Get(relayPortHandle);
|
||||
relayHandles->Free(relayPortHandle);
|
||||
if (port == nullptr) return;
|
||||
if (port->fwd)
|
||||
if (port == nullptr) {
|
||||
return;
|
||||
}
|
||||
if (port->fwd) {
|
||||
SimRelayData[port->channel].initializedForward = false;
|
||||
else
|
||||
} else {
|
||||
SimRelayData[port->channel].initializedReverse = false;
|
||||
}
|
||||
}
|
||||
|
||||
HAL_Bool HAL_CheckRelayChannel(int32_t channel) {
|
||||
@@ -98,10 +106,11 @@ void HAL_SetRelay(HAL_RelayHandle relayPortHandle, HAL_Bool on,
|
||||
*status = HAL_HANDLE_ERROR;
|
||||
return;
|
||||
}
|
||||
if (port->fwd)
|
||||
if (port->fwd) {
|
||||
SimRelayData[port->channel].forward = on;
|
||||
else
|
||||
} else {
|
||||
SimRelayData[port->channel].reverse = on;
|
||||
}
|
||||
}
|
||||
|
||||
HAL_Bool HAL_GetRelay(HAL_RelayHandle relayPortHandle, int32_t* status) {
|
||||
@@ -110,9 +119,10 @@ HAL_Bool HAL_GetRelay(HAL_RelayHandle relayPortHandle, int32_t* status) {
|
||||
*status = HAL_HANDLE_ERROR;
|
||||
return false;
|
||||
}
|
||||
if (port->fwd)
|
||||
if (port->fwd) {
|
||||
return SimRelayData[port->channel].forward;
|
||||
else
|
||||
} else {
|
||||
return SimRelayData[port->channel].reverse;
|
||||
}
|
||||
}
|
||||
} // extern "C"
|
||||
|
||||
@@ -32,13 +32,17 @@ int32_t HAL_WriteSPI(HAL_SPIPort port, const uint8_t* dataToSend,
|
||||
int32_t HAL_ReadSPI(HAL_SPIPort port, uint8_t* buffer, int32_t count) {
|
||||
return SimSPIData[port].Read(buffer, count);
|
||||
}
|
||||
void HAL_CloseSPI(HAL_SPIPort port) { SimSPIData[port].initialized = false; }
|
||||
void HAL_CloseSPI(HAL_SPIPort port) {
|
||||
SimSPIData[port].initialized = false;
|
||||
}
|
||||
void HAL_SetSPISpeed(HAL_SPIPort port, int32_t speed) {}
|
||||
void HAL_SetSPIOpts(HAL_SPIPort port, HAL_Bool msbFirst,
|
||||
HAL_Bool sampleOnTrailing, HAL_Bool clkIdleHigh) {}
|
||||
void HAL_SetSPIChipSelectActiveHigh(HAL_SPIPort port, int32_t* status) {}
|
||||
void HAL_SetSPIChipSelectActiveLow(HAL_SPIPort port, int32_t* status) {}
|
||||
int32_t HAL_GetSPIHandle(HAL_SPIPort port) { return 0; }
|
||||
int32_t HAL_GetSPIHandle(HAL_SPIPort port) {
|
||||
return 0;
|
||||
}
|
||||
void HAL_SetSPIHandle(HAL_SPIPort port, int32_t handle) {}
|
||||
|
||||
void HAL_InitSPIAuto(HAL_SPIPort port, int32_t bufferSize, int32_t* status) {}
|
||||
|
||||
@@ -26,7 +26,9 @@ HAL_SerialPortHandle HAL_InitializeSerialPortDirect(HAL_SerialPort port,
|
||||
return HAL_kInvalidHandle;
|
||||
}
|
||||
|
||||
int HAL_GetSerialFD(HAL_SerialPortHandle handle, int32_t* status) { return -1; }
|
||||
int HAL_GetSerialFD(HAL_SerialPortHandle handle, int32_t* status) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
void HAL_SetSerialBaudRate(HAL_SerialPortHandle handle, int32_t baud,
|
||||
int32_t* status) {}
|
||||
|
||||
@@ -78,7 +78,9 @@ HAL_SolenoidHandle HAL_InitializeSolenoidPort(HAL_PortHandle portHandle,
|
||||
}
|
||||
void HAL_FreeSolenoidPort(HAL_SolenoidHandle solenoidPortHandle) {
|
||||
auto port = solenoidHandles->Get(solenoidPortHandle);
|
||||
if (port == nullptr) return;
|
||||
if (port == nullptr) {
|
||||
return;
|
||||
}
|
||||
solenoidHandles->Free(solenoidPortHandle);
|
||||
HALSIM_SetPCMSolenoidInitialized(port->module, port->channel, false);
|
||||
int count = 0;
|
||||
|
||||
@@ -41,7 +41,9 @@ void AddressableLEDData::SetData(const HAL_AddressableLEDData* d, int32_t len) {
|
||||
int32_t AddressableLEDData::GetData(HAL_AddressableLEDData* d) {
|
||||
std::scoped_lock lock(m_dataMutex);
|
||||
int32_t len = length;
|
||||
if (d) std::memcpy(d, m_data, len * sizeof(d[0]));
|
||||
if (d) {
|
||||
std::memcpy(d, m_data, len * sizeof(d[0]));
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
@@ -50,8 +52,9 @@ extern "C" {
|
||||
int32_t HALSIM_FindAddressableLEDForChannel(int32_t channel) {
|
||||
for (int i = 0; i < kNumAddressableLEDs; ++i) {
|
||||
if (SimAddressableLEDData[i].initialized &&
|
||||
SimAddressableLEDData[i].outputPort == channel)
|
||||
SimAddressableLEDData[i].outputPort == channel) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -29,8 +29,9 @@ extern "C" {
|
||||
int32_t HALSIM_FindAnalogTriggerForChannel(int32_t channel) {
|
||||
for (int i = 0; i < kNumAnalogTriggers; ++i) {
|
||||
if (SimAnalogTriggerData[i].initialized &&
|
||||
SimAnalogTriggerData[i].inputPort == channel)
|
||||
SimAnalogTriggerData[i].inputPort == channel) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -28,7 +28,9 @@ void CanData::ResetData() {
|
||||
|
||||
extern "C" {
|
||||
|
||||
void HALSIM_ResetCanData(void) { SimCanData->ResetData(); }
|
||||
void HALSIM_ResetCanData(void) {
|
||||
SimCanData->ResetData();
|
||||
}
|
||||
|
||||
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
|
||||
HAL_SIMCALLBACKREGISTRY_DEFINE_CAPI_NOINDEX(TYPE, HALSIM, Can##CAPINAME, \
|
||||
|
||||
@@ -27,7 +27,9 @@ void DIOData::ResetData() {
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void HALSIM_ResetDIOData(int32_t index) { SimDIOData[index].ResetData(); }
|
||||
void HALSIM_ResetDIOData(int32_t index) {
|
||||
SimDIOData[index].ResetData();
|
||||
}
|
||||
|
||||
HAL_SimDeviceHandle HALSIM_GetDIOSimDevice(int32_t index) {
|
||||
return SimDIOData[index].simDevice;
|
||||
|
||||
@@ -26,8 +26,10 @@ void DigitalPWMData::ResetData() {
|
||||
extern "C" {
|
||||
int32_t HALSIM_FindDigitalPWMForChannel(int32_t channel) {
|
||||
for (int i = 0; i < kNumDigitalPWMOutputs; ++i) {
|
||||
if (SimDigitalPWMData[i].initialized && SimDigitalPWMData[i].pin == channel)
|
||||
if (SimDigitalPWMData[i].initialized &&
|
||||
SimDigitalPWMData[i].pin == channel) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -20,7 +20,9 @@ void InitializeDriverStationData() {
|
||||
|
||||
DriverStationData* hal::SimDriverStationData;
|
||||
|
||||
DriverStationData::DriverStationData() { ResetData(); }
|
||||
DriverStationData::DriverStationData() {
|
||||
ResetData();
|
||||
}
|
||||
|
||||
void DriverStationData::ResetData() {
|
||||
enabled.Reset(false);
|
||||
@@ -60,7 +62,8 @@ void DriverStationData::ResetData() {
|
||||
int32_t DriverStationData::RegisterJoystick##name##Callback( \
|
||||
int32_t joystickNum, HAL_Joystick##name##Callback callback, void* param, \
|
||||
HAL_Bool initialNotify) { \
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return 0; \
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) \
|
||||
return 0; \
|
||||
std::scoped_lock lock(m_joystickDataMutex); \
|
||||
int32_t uid = m_joystick##name##Callbacks.Register(callback, param); \
|
||||
if (initialNotify) { \
|
||||
@@ -79,14 +82,16 @@ void DriverStationData::ResetData() {
|
||||
\
|
||||
void DriverStationData::GetJoystick##name(int32_t joystickNum, \
|
||||
HAL_Joystick##name* d) { \
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return; \
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) \
|
||||
return; \
|
||||
std::scoped_lock lock(m_joystickDataMutex); \
|
||||
*d = m_joystickData[joystickNum].data##data2; \
|
||||
} \
|
||||
\
|
||||
void DriverStationData::SetJoystick##name(int32_t joystickNum, \
|
||||
const HAL_Joystick##name* d) { \
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return; \
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) \
|
||||
return; \
|
||||
std::scoped_lock lock(m_joystickDataMutex); \
|
||||
m_joystickData[joystickNum].data##data2 = *d; \
|
||||
m_joystick##name##Callbacks(joystickNum, d); \
|
||||
@@ -100,14 +105,18 @@ DEFINE_CPPAPI_CALLBACKS(Descriptor, descriptor, )
|
||||
|
||||
void DriverStationData::GetJoystickDescriptor(
|
||||
int32_t joystickNum, HAL_JoystickDescriptor* descriptor) {
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return;
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
*descriptor = m_joystickData[joystickNum].descriptor;
|
||||
}
|
||||
|
||||
void DriverStationData::SetJoystickDescriptor(
|
||||
int32_t joystickNum, const HAL_JoystickDescriptor* descriptor) {
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return;
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
m_joystickData[joystickNum].descriptor = *descriptor;
|
||||
// Always ensure name is null terminated
|
||||
@@ -118,8 +127,9 @@ void DriverStationData::SetJoystickDescriptor(
|
||||
int32_t DriverStationData::RegisterJoystickOutputsCallback(
|
||||
int32_t joystickNum, HAL_JoystickOutputsCallback callback, void* param,
|
||||
HAL_Bool initialNotify) {
|
||||
if (joystickNum < 0 || joystickNum >= DriverStationData::kNumJoysticks)
|
||||
if (joystickNum < 0 || joystickNum >= DriverStationData::kNumJoysticks) {
|
||||
return 0;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
int32_t uid = m_joystickOutputsCallbacks.Register(callback, param);
|
||||
if (initialNotify) {
|
||||
@@ -138,7 +148,9 @@ void DriverStationData::GetJoystickOutputs(int32_t joystickNum,
|
||||
int64_t* outputs,
|
||||
int32_t* leftRumble,
|
||||
int32_t* rightRumble) {
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return;
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
*leftRumble = m_joystickData[joystickNum].outputs.leftRumble;
|
||||
*outputs = m_joystickData[joystickNum].outputs.outputs;
|
||||
@@ -148,7 +160,9 @@ void DriverStationData::GetJoystickOutputs(int32_t joystickNum,
|
||||
void DriverStationData::SetJoystickOutputs(int32_t joystickNum, int64_t outputs,
|
||||
int32_t leftRumble,
|
||||
int32_t rightRumble) {
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) return;
|
||||
if (joystickNum < 0 || joystickNum >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
m_joystickData[joystickNum].outputs.leftRumble = leftRumble;
|
||||
m_joystickData[joystickNum].outputs.outputs = outputs;
|
||||
@@ -204,23 +218,32 @@ void DriverStationData::CallNewDataCallbacks() {
|
||||
m_newDataCallbacks(&empty);
|
||||
}
|
||||
|
||||
void DriverStationData::NotifyNewData() { HAL_ReleaseDSMutex(); }
|
||||
void DriverStationData::NotifyNewData() {
|
||||
HAL_ReleaseDSMutex();
|
||||
}
|
||||
|
||||
void DriverStationData::SetJoystickButton(int32_t stick, int32_t button,
|
||||
HAL_Bool state) {
|
||||
if (stick < 0 || stick >= kNumJoysticks) return;
|
||||
if (stick < 0 || stick >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
if (state)
|
||||
if (state) {
|
||||
m_joystickData[stick].buttons.buttons |= 1 << (button - 1);
|
||||
else
|
||||
} else {
|
||||
m_joystickData[stick].buttons.buttons &= ~(1 << (button - 1));
|
||||
}
|
||||
m_joystickButtonsCallbacks(stick, &m_joystickData[stick].buttons);
|
||||
}
|
||||
|
||||
void DriverStationData::SetJoystickAxis(int32_t stick, int32_t axis,
|
||||
double value) {
|
||||
if (stick < 0 || stick >= kNumJoysticks) return;
|
||||
if (axis < 0 || axis >= HAL_kMaxJoystickAxes) return;
|
||||
if (stick < 0 || stick >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
if (axis < 0 || axis >= HAL_kMaxJoystickAxes) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
m_joystickData[stick].axes.axes[axis] = value;
|
||||
m_joystickAxesCallbacks(stick, &m_joystickData[stick].axes);
|
||||
@@ -228,22 +251,30 @@ void DriverStationData::SetJoystickAxis(int32_t stick, int32_t axis,
|
||||
|
||||
void DriverStationData::SetJoystickPOV(int32_t stick, int32_t pov,
|
||||
int32_t value) {
|
||||
if (stick < 0 || stick >= kNumJoysticks) return;
|
||||
if (pov < 0 || pov >= HAL_kMaxJoystickPOVs) return;
|
||||
if (stick < 0 || stick >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
if (pov < 0 || pov >= HAL_kMaxJoystickPOVs) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
m_joystickData[stick].povs.povs[pov] = value;
|
||||
m_joystickPOVsCallbacks(stick, &m_joystickData[stick].povs);
|
||||
}
|
||||
|
||||
void DriverStationData::SetJoystickButtons(int32_t stick, uint32_t buttons) {
|
||||
if (stick < 0 || stick >= kNumJoysticks) return;
|
||||
if (stick < 0 || stick >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
m_joystickData[stick].buttons.buttons = buttons;
|
||||
m_joystickButtonsCallbacks(stick, &m_joystickData[stick].buttons);
|
||||
}
|
||||
|
||||
void DriverStationData::SetJoystickAxisCount(int32_t stick, int32_t count) {
|
||||
if (stick < 0 || stick >= kNumJoysticks) return;
|
||||
if (stick < 0 || stick >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
m_joystickData[stick].axes.count = count;
|
||||
m_joystickData[stick].descriptor.axisCount = count;
|
||||
@@ -252,7 +283,9 @@ void DriverStationData::SetJoystickAxisCount(int32_t stick, int32_t count) {
|
||||
}
|
||||
|
||||
void DriverStationData::SetJoystickPOVCount(int32_t stick, int32_t count) {
|
||||
if (stick < 0 || stick >= kNumJoysticks) return;
|
||||
if (stick < 0 || stick >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
m_joystickData[stick].povs.count = count;
|
||||
m_joystickData[stick].descriptor.povCount = count;
|
||||
@@ -261,7 +294,9 @@ void DriverStationData::SetJoystickPOVCount(int32_t stick, int32_t count) {
|
||||
}
|
||||
|
||||
void DriverStationData::SetJoystickButtonCount(int32_t stick, int32_t count) {
|
||||
if (stick < 0 || stick >= kNumJoysticks) return;
|
||||
if (stick < 0 || stick >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
m_joystickData[stick].buttons.count = count;
|
||||
m_joystickData[stick].descriptor.buttonCount = count;
|
||||
@@ -285,21 +320,27 @@ void DriverStationData::GetJoystickCounts(int32_t stick, int32_t* axisCount,
|
||||
}
|
||||
|
||||
void DriverStationData::SetJoystickIsXbox(int32_t stick, HAL_Bool isXbox) {
|
||||
if (stick < 0 || stick >= kNumJoysticks) return;
|
||||
if (stick < 0 || stick >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
m_joystickData[stick].descriptor.isXbox = isXbox;
|
||||
m_joystickDescriptorCallbacks(stick, &m_joystickData[stick].descriptor);
|
||||
}
|
||||
|
||||
void DriverStationData::SetJoystickType(int32_t stick, int32_t type) {
|
||||
if (stick < 0 || stick >= kNumJoysticks) return;
|
||||
if (stick < 0 || stick >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
m_joystickData[stick].descriptor.type = type;
|
||||
m_joystickDescriptorCallbacks(stick, &m_joystickData[stick].descriptor);
|
||||
}
|
||||
|
||||
void DriverStationData::SetJoystickName(int32_t stick, const char* name) {
|
||||
if (stick < 0 || stick >= kNumJoysticks) return;
|
||||
if (stick < 0 || stick >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
std::strncpy(m_joystickData[stick].descriptor.name, name,
|
||||
sizeof(m_joystickData[stick].descriptor.name) - 1);
|
||||
@@ -309,8 +350,12 @@ void DriverStationData::SetJoystickName(int32_t stick, const char* name) {
|
||||
|
||||
void DriverStationData::SetJoystickAxisType(int32_t stick, int32_t axis,
|
||||
int32_t type) {
|
||||
if (stick < 0 || stick >= kNumJoysticks) return;
|
||||
if (axis < 0 || axis >= HAL_kMaxJoystickAxes) return;
|
||||
if (stick < 0 || stick >= kNumJoysticks) {
|
||||
return;
|
||||
}
|
||||
if (axis < 0 || axis >= HAL_kMaxJoystickAxes) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_joystickDataMutex);
|
||||
m_joystickData[stick].descriptor.axisTypes[axis] = type;
|
||||
m_joystickDescriptorCallbacks(stick, &m_joystickData[stick].descriptor);
|
||||
@@ -351,7 +396,9 @@ void DriverStationData::SetReplayNumber(int32_t replayNumber) {
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void HALSIM_ResetDriverStationData(void) { SimDriverStationData->ResetData(); }
|
||||
void HALSIM_ResetDriverStationData(void) {
|
||||
SimDriverStationData->ResetData();
|
||||
}
|
||||
|
||||
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
|
||||
HAL_SIMDATAVALUE_DEFINE_CAPI_NOINDEX(TYPE, HALSIM, DriverStation##CAPINAME, \
|
||||
|
||||
@@ -30,8 +30,9 @@ extern "C" {
|
||||
int32_t HALSIM_FindDutyCycleForChannel(int32_t channel) {
|
||||
for (int i = 0; i < kNumDutyCycles; ++i) {
|
||||
if (SimDutyCycleData[i].initialized &&
|
||||
SimDutyCycleData[i].digitalChannel == channel)
|
||||
SimDutyCycleData[i].digitalChannel == channel) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -35,10 +35,13 @@ void EncoderData::ResetData() {
|
||||
extern "C" {
|
||||
int32_t HALSIM_FindEncoderForChannel(int32_t channel) {
|
||||
for (int i = 0; i < kNumEncoders; ++i) {
|
||||
if (!SimEncoderData[i].initialized) continue;
|
||||
if (!SimEncoderData[i].initialized) {
|
||||
continue;
|
||||
}
|
||||
if (SimEncoderData[i].digitalChannelA == channel ||
|
||||
SimEncoderData[i].digitalChannelB == channel)
|
||||
SimEncoderData[i].digitalChannelB == channel) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -34,7 +34,9 @@ void I2CData::Read(int32_t deviceAddress, uint8_t* buffer, int32_t count) {
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void HALSIM_ResetI2CData(int32_t index) { SimI2CData[index].ResetData(); }
|
||||
void HALSIM_ResetI2CData(int32_t index) {
|
||||
SimI2CData[index].ResetData();
|
||||
}
|
||||
|
||||
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
|
||||
HAL_SIMDATAVALUE_DEFINE_CAPI(TYPE, HALSIM, I2C##CAPINAME, SimI2CData, \
|
||||
|
||||
@@ -31,7 +31,9 @@ void PCMData::ResetData() {
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void HALSIM_ResetPCMData(int32_t index) { SimPCMData[index].ResetData(); }
|
||||
void HALSIM_ResetPCMData(int32_t index) {
|
||||
SimPCMData[index].ResetData();
|
||||
}
|
||||
|
||||
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
|
||||
HAL_SIMDATAVALUE_DEFINE_CAPI(TYPE, HALSIM, PCM##CAPINAME, SimPCMData, \
|
||||
|
||||
@@ -27,7 +27,9 @@ void PDPData::ResetData() {
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void HALSIM_ResetPDPData(int32_t index) { SimPDPData[index].ResetData(); }
|
||||
void HALSIM_ResetPDPData(int32_t index) {
|
||||
SimPDPData[index].ResetData();
|
||||
}
|
||||
|
||||
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
|
||||
HAL_SIMDATAVALUE_DEFINE_CAPI(TYPE, HALSIM, PDP##CAPINAME, SimPDPData, \
|
||||
|
||||
@@ -27,7 +27,9 @@ void PWMData::ResetData() {
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void HALSIM_ResetPWMData(int32_t index) { SimPWMData[index].ResetData(); }
|
||||
void HALSIM_ResetPWMData(int32_t index) {
|
||||
SimPWMData[index].ResetData();
|
||||
}
|
||||
|
||||
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
|
||||
HAL_SIMDATAVALUE_DEFINE_CAPI(TYPE, HALSIM, PWM##CAPINAME, SimPWMData, \
|
||||
|
||||
@@ -25,7 +25,9 @@ void RelayData::ResetData() {
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void HALSIM_ResetRelayData(int32_t index) { SimRelayData[index].ResetData(); }
|
||||
void HALSIM_ResetRelayData(int32_t index) {
|
||||
SimRelayData[index].ResetData();
|
||||
}
|
||||
|
||||
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
|
||||
HAL_SIMDATAVALUE_DEFINE_CAPI(TYPE, HALSIM, Relay##CAPINAME, SimRelayData, \
|
||||
|
||||
@@ -36,7 +36,9 @@ void RoboRioData::ResetData() {
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void HALSIM_ResetRoboRioData(void) { SimRoboRioData->ResetData(); }
|
||||
void HALSIM_ResetRoboRioData(void) {
|
||||
SimRoboRioData->ResetData();
|
||||
}
|
||||
|
||||
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
|
||||
HAL_SIMDATAVALUE_DEFINE_CAPI_NOINDEX(TYPE, HALSIM, RoboRio##CAPINAME, \
|
||||
|
||||
@@ -49,7 +49,9 @@ int32_t SPIData::ReadAutoReceivedData(uint32_t* buffer, int32_t numToRead,
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
void HALSIM_ResetSPIData(int32_t index) { SimSPIData[index].ResetData(); }
|
||||
void HALSIM_ResetSPIData(int32_t index) {
|
||||
SimSPIData[index].ResetData();
|
||||
}
|
||||
|
||||
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
|
||||
HAL_SIMDATAVALUE_DEFINE_CAPI(TYPE, HALSIM, SPI##CAPINAME, SimSPIData, \
|
||||
|
||||
@@ -22,26 +22,34 @@ void InitializeSimDeviceData() {
|
||||
SimDeviceData* hal::SimSimDeviceData;
|
||||
|
||||
SimDeviceData::Device* SimDeviceData::LookupDevice(HAL_SimDeviceHandle handle) {
|
||||
if (handle <= 0) return nullptr;
|
||||
--handle;
|
||||
if (static_cast<uint32_t>(handle) >= m_devices.size() || !m_devices[handle])
|
||||
if (handle <= 0) {
|
||||
return nullptr;
|
||||
}
|
||||
--handle;
|
||||
if (static_cast<uint32_t>(handle) >= m_devices.size() || !m_devices[handle]) {
|
||||
return nullptr;
|
||||
}
|
||||
return m_devices[handle].get();
|
||||
}
|
||||
|
||||
SimDeviceData::Value* SimDeviceData::LookupValue(HAL_SimValueHandle handle) {
|
||||
if (handle <= 0) return nullptr;
|
||||
if (handle <= 0) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// look up device
|
||||
Device* deviceImpl = LookupDevice(handle >> 16);
|
||||
if (!deviceImpl) return nullptr;
|
||||
if (!deviceImpl) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// look up value
|
||||
handle &= 0xffff;
|
||||
--handle;
|
||||
if (static_cast<uint32_t>(handle) >= deviceImpl->values.size() ||
|
||||
!deviceImpl->values[handle])
|
||||
!deviceImpl->values[handle]) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return deviceImpl->values[handle].get();
|
||||
}
|
||||
@@ -65,7 +73,9 @@ void SimDeviceData::SetDeviceEnabled(const char* prefix, bool enabled) {
|
||||
bool SimDeviceData::IsDeviceEnabled(const char* name) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
for (const auto& elem : m_prefixEnabled) {
|
||||
if (wpi::StringRef{name}.startswith(elem.first)) return elem.second;
|
||||
if (wpi::StringRef{name}.startswith(elem.first)) {
|
||||
return elem.second;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -76,17 +86,23 @@ HAL_SimDeviceHandle SimDeviceData::CreateDevice(const char* name) {
|
||||
// don't create if disabled
|
||||
for (const auto& elem : m_prefixEnabled) {
|
||||
if (wpi::StringRef{name}.startswith(elem.first)) {
|
||||
if (elem.second) break; // enabled
|
||||
return 0; // disabled
|
||||
if (elem.second) {
|
||||
break; // enabled
|
||||
}
|
||||
return 0; // disabled
|
||||
}
|
||||
}
|
||||
|
||||
// check for duplicates and don't overwrite them
|
||||
if (m_deviceMap.count(name) > 0) return 0;
|
||||
if (m_deviceMap.count(name) > 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// don't allow more than 4096 devices (limit driven by 12-bit allocation in
|
||||
// value changed callback uid)
|
||||
if (m_devices.size() >= 4095) return 0;
|
||||
if (m_devices.size() >= 4095) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// create and save
|
||||
auto deviceImpl = std::make_shared<Device>(name);
|
||||
@@ -105,9 +121,13 @@ void SimDeviceData::FreeDevice(HAL_SimDeviceHandle handle) {
|
||||
--handle;
|
||||
|
||||
// see if it exists
|
||||
if (handle < 0 || static_cast<uint32_t>(handle) >= m_devices.size()) return;
|
||||
if (handle < 0 || static_cast<uint32_t>(handle) >= m_devices.size()) {
|
||||
return;
|
||||
}
|
||||
auto deviceImpl = std::move(m_devices[handle]);
|
||||
if (!deviceImpl) return;
|
||||
if (!deviceImpl) {
|
||||
return;
|
||||
}
|
||||
|
||||
// remove from map
|
||||
m_deviceMap.erase(deviceImpl->name);
|
||||
@@ -127,15 +147,21 @@ HAL_SimValueHandle SimDeviceData::CreateValue(
|
||||
|
||||
// look up device
|
||||
Device* deviceImpl = LookupDevice(device);
|
||||
if (!deviceImpl) return 0;
|
||||
if (!deviceImpl) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// check for duplicates and don't overwrite them
|
||||
auto it = deviceImpl->valueMap.find(name);
|
||||
if (it != deviceImpl->valueMap.end()) return 0;
|
||||
if (it != deviceImpl->valueMap.end()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// don't allow more than 4096 values per device (limit driven by 12-bit
|
||||
// allocation in value changed callback uid)
|
||||
if (deviceImpl->values.size() >= 4095) return 0;
|
||||
if (deviceImpl->values.size() >= 4095) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// create and save; encode device into handle
|
||||
auto valueImplPtr = std::make_unique<Value>(name, direction, initialValue);
|
||||
@@ -185,7 +211,9 @@ void SimDeviceData::SetValue(HAL_SimValueHandle handle,
|
||||
const HAL_Value& value) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
Value* valueImpl = LookupValue(handle);
|
||||
if (!valueImpl) return;
|
||||
if (!valueImpl) {
|
||||
return;
|
||||
}
|
||||
|
||||
valueImpl->value = value;
|
||||
|
||||
@@ -205,8 +233,9 @@ int32_t SimDeviceData::RegisterDeviceCreatedCallback(
|
||||
// initial notifications
|
||||
if (initialNotify) {
|
||||
for (auto&& device : m_devices) {
|
||||
if (wpi::StringRef{device->name}.startswith(prefix))
|
||||
if (wpi::StringRef{device->name}.startswith(prefix)) {
|
||||
callback(device->name.c_str(), param, device->handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,7 +243,9 @@ int32_t SimDeviceData::RegisterDeviceCreatedCallback(
|
||||
}
|
||||
|
||||
void SimDeviceData::CancelDeviceCreatedCallback(int32_t uid) {
|
||||
if (uid <= 0) return;
|
||||
if (uid <= 0) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_mutex);
|
||||
m_deviceCreated.Cancel(uid);
|
||||
}
|
||||
@@ -226,7 +257,9 @@ int32_t SimDeviceData::RegisterDeviceFreedCallback(
|
||||
}
|
||||
|
||||
void SimDeviceData::CancelDeviceFreedCallback(int32_t uid) {
|
||||
if (uid <= 0) return;
|
||||
if (uid <= 0) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_mutex);
|
||||
m_deviceFreed.Cancel(uid);
|
||||
}
|
||||
@@ -234,11 +267,14 @@ void SimDeviceData::CancelDeviceFreedCallback(int32_t uid) {
|
||||
HAL_SimDeviceHandle SimDeviceData::GetDeviceHandle(const char* name) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
auto it = m_deviceMap.find(name);
|
||||
if (it == m_deviceMap.end()) return 0;
|
||||
if (auto deviceImpl = it->getValue().lock())
|
||||
return deviceImpl->handle;
|
||||
else
|
||||
if (it == m_deviceMap.end()) {
|
||||
return 0;
|
||||
}
|
||||
if (auto deviceImpl = it->getValue().lock()) {
|
||||
return deviceImpl->handle;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
const char* SimDeviceData::GetDeviceName(HAL_SimDeviceHandle handle) {
|
||||
@@ -246,7 +282,9 @@ const char* SimDeviceData::GetDeviceName(HAL_SimDeviceHandle handle) {
|
||||
|
||||
// look up device
|
||||
Device* deviceImpl = LookupDevice(handle);
|
||||
if (!deviceImpl) return nullptr;
|
||||
if (!deviceImpl) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return deviceImpl->name.c_str();
|
||||
}
|
||||
@@ -255,8 +293,9 @@ void SimDeviceData::EnumerateDevices(const char* prefix, void* param,
|
||||
HALSIM_SimDeviceCallback callback) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
for (auto&& device : m_devices) {
|
||||
if (wpi::StringRef{device->name}.startswith(prefix))
|
||||
if (wpi::StringRef{device->name}.startswith(prefix)) {
|
||||
callback(device->name.c_str(), param, device->handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -265,16 +304,19 @@ int32_t SimDeviceData::RegisterValueCreatedCallback(
|
||||
bool initialNotify) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
Device* deviceImpl = LookupDevice(device);
|
||||
if (!deviceImpl) return -1;
|
||||
if (!deviceImpl) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// register callback
|
||||
int32_t index = deviceImpl->valueCreated.Register(callback, param);
|
||||
|
||||
// initial notifications
|
||||
if (initialNotify) {
|
||||
for (auto&& value : deviceImpl->values)
|
||||
for (auto&& value : deviceImpl->values) {
|
||||
callback(value->name.c_str(), param, value->handle, value->direction,
|
||||
&value->value);
|
||||
}
|
||||
}
|
||||
|
||||
// encode device into uid
|
||||
@@ -282,10 +324,14 @@ int32_t SimDeviceData::RegisterValueCreatedCallback(
|
||||
}
|
||||
|
||||
void SimDeviceData::CancelValueCreatedCallback(int32_t uid) {
|
||||
if (uid <= 0) return;
|
||||
if (uid <= 0) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_mutex);
|
||||
Device* deviceImpl = LookupDevice(uid >> 16);
|
||||
if (!deviceImpl) return;
|
||||
if (!deviceImpl) {
|
||||
return;
|
||||
}
|
||||
deviceImpl->valueCreated.Cancel(uid & 0xffff);
|
||||
}
|
||||
|
||||
@@ -294,15 +340,18 @@ int32_t SimDeviceData::RegisterValueChangedCallback(
|
||||
bool initialNotify) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
Value* valueImpl = LookupValue(handle);
|
||||
if (!valueImpl) return -1;
|
||||
if (!valueImpl) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// register callback
|
||||
int32_t index = valueImpl->changed.Register(callback, param);
|
||||
|
||||
// initial notification
|
||||
if (initialNotify)
|
||||
if (initialNotify) {
|
||||
callback(valueImpl->name.c_str(), param, valueImpl->handle,
|
||||
valueImpl->direction, &valueImpl->value);
|
||||
}
|
||||
|
||||
// encode device and value into uid
|
||||
return (((handle >> 16) & 0xfff) << 19) | ((handle & 0xfff) << 7) |
|
||||
@@ -310,10 +359,14 @@ int32_t SimDeviceData::RegisterValueChangedCallback(
|
||||
}
|
||||
|
||||
void SimDeviceData::CancelValueChangedCallback(int32_t uid) {
|
||||
if (uid <= 0) return;
|
||||
if (uid <= 0) {
|
||||
return;
|
||||
}
|
||||
std::scoped_lock lock(m_mutex);
|
||||
Value* valueImpl = LookupValue(((uid >> 19) << 16) | ((uid >> 7) & 0xfff));
|
||||
if (!valueImpl) return;
|
||||
if (!valueImpl) {
|
||||
return;
|
||||
}
|
||||
valueImpl->changed.Cancel(uid & 0x7f);
|
||||
}
|
||||
|
||||
@@ -321,12 +374,18 @@ HAL_SimValueHandle SimDeviceData::GetValueHandle(HAL_SimDeviceHandle device,
|
||||
const char* name) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
Device* deviceImpl = LookupDevice(device);
|
||||
if (!deviceImpl) return 0;
|
||||
if (!deviceImpl) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// lookup value
|
||||
auto it = deviceImpl->valueMap.find(name);
|
||||
if (it == deviceImpl->valueMap.end()) return 0;
|
||||
if (!it->getValue()) return 0;
|
||||
if (it == deviceImpl->valueMap.end()) {
|
||||
return 0;
|
||||
}
|
||||
if (!it->getValue()) {
|
||||
return 0;
|
||||
}
|
||||
return it->getValue()->handle;
|
||||
}
|
||||
|
||||
@@ -334,11 +393,14 @@ void SimDeviceData::EnumerateValues(HAL_SimDeviceHandle device, void* param,
|
||||
HALSIM_SimValueCallback callback) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
Device* deviceImpl = LookupDevice(device);
|
||||
if (!deviceImpl) return;
|
||||
if (!deviceImpl) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto&& value : deviceImpl->values)
|
||||
for (auto&& value : deviceImpl->values) {
|
||||
callback(value->name.c_str(), param, value->handle, value->direction,
|
||||
&value->value);
|
||||
}
|
||||
}
|
||||
|
||||
const char** SimDeviceData::GetValueEnumOptions(HAL_SimValueHandle handle,
|
||||
@@ -347,7 +409,9 @@ const char** SimDeviceData::GetValueEnumOptions(HAL_SimValueHandle handle,
|
||||
|
||||
std::scoped_lock lock(m_mutex);
|
||||
Value* valueImpl = LookupValue(handle);
|
||||
if (!valueImpl) return nullptr;
|
||||
if (!valueImpl) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// get list of options (safe to return as they never change)
|
||||
auto& options = valueImpl->cstrEnumOptions;
|
||||
@@ -361,7 +425,9 @@ const double* SimDeviceData::GetValueEnumDoubleValues(HAL_SimValueHandle handle,
|
||||
|
||||
std::scoped_lock lock(m_mutex);
|
||||
Value* valueImpl = LookupValue(handle);
|
||||
if (!valueImpl) return nullptr;
|
||||
if (!valueImpl) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// get list of option values (safe to return as they never change)
|
||||
auto& optionValues = valueImpl->enumOptionValues;
|
||||
@@ -418,7 +484,9 @@ const char* HALSIM_GetSimDeviceName(HAL_SimDeviceHandle handle) {
|
||||
}
|
||||
|
||||
HAL_SimDeviceHandle HALSIM_GetSimValueDeviceHandle(HAL_SimValueHandle handle) {
|
||||
if (handle <= 0) return 0;
|
||||
if (handle <= 0) {
|
||||
return 0;
|
||||
}
|
||||
return handle >> 16;
|
||||
}
|
||||
|
||||
@@ -471,6 +539,8 @@ const double* HALSIM_GetSimValueEnumDoubleValues(HAL_SimValueHandle handle,
|
||||
return SimSimDeviceData->GetValueEnumDoubleValues(handle, numOptions);
|
||||
}
|
||||
|
||||
void HALSIM_ResetSimDeviceData(void) { SimSimDeviceData->ResetData(); }
|
||||
void HALSIM_ResetSimDeviceData(void) {
|
||||
SimSimDeviceData->ResetData();
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
|
||||
@@ -33,17 +33,25 @@ class SimUnnamedCallbackRegistry {
|
||||
|
||||
public:
|
||||
void Cancel(int32_t uid) {
|
||||
if (m_callbacks) m_callbacks->erase(uid - 1);
|
||||
if (m_callbacks) {
|
||||
m_callbacks->erase(uid - 1);
|
||||
}
|
||||
}
|
||||
|
||||
void Reset() {
|
||||
if (m_callbacks) m_callbacks->clear();
|
||||
if (m_callbacks) {
|
||||
m_callbacks->clear();
|
||||
}
|
||||
}
|
||||
|
||||
int32_t Register(CallbackFunction callback, void* param) {
|
||||
// Must return -1 on a null callback for error handling
|
||||
if (callback == nullptr) return -1;
|
||||
if (!m_callbacks) m_callbacks = std::make_unique<CallbackVector>();
|
||||
if (callback == nullptr) {
|
||||
return -1;
|
||||
}
|
||||
if (!m_callbacks) {
|
||||
m_callbacks = std::make_unique<CallbackVector>();
|
||||
}
|
||||
return m_callbacks->emplace_back(param,
|
||||
reinterpret_cast<RawFunctor>(callback)) +
|
||||
1;
|
||||
@@ -84,17 +92,25 @@ class SimPrefixCallbackRegistry {
|
||||
|
||||
public:
|
||||
void Cancel(int32_t uid) {
|
||||
if (m_callbacks) m_callbacks->erase(uid - 1);
|
||||
if (m_callbacks) {
|
||||
m_callbacks->erase(uid - 1);
|
||||
}
|
||||
}
|
||||
|
||||
void Reset() {
|
||||
if (m_callbacks) m_callbacks->clear();
|
||||
if (m_callbacks) {
|
||||
m_callbacks->clear();
|
||||
}
|
||||
}
|
||||
|
||||
int32_t Register(const char* prefix, void* param, CallbackFunction callback) {
|
||||
// Must return -1 on a null callback for error handling
|
||||
if (callback == nullptr) return -1;
|
||||
if (!m_callbacks) m_callbacks = std::make_unique<CallbackVector>();
|
||||
if (callback == nullptr) {
|
||||
return -1;
|
||||
}
|
||||
if (!m_callbacks) {
|
||||
m_callbacks = std::make_unique<CallbackVector>();
|
||||
}
|
||||
return m_callbacks->emplace_back(prefix, param, callback) + 1;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user