mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
[hal] Report previous allocation location for indexed resource duplicates (#3322)
This commit is contained in:
@@ -4,7 +4,10 @@
|
||||
|
||||
#include "hal/AnalogGyro.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "HALInitializer.h"
|
||||
#include "HALInternal.h"
|
||||
#include "PortsInternal.h"
|
||||
#include "hal/AnalogAccumulator.h"
|
||||
#include "hal/Errors.h"
|
||||
@@ -15,6 +18,7 @@ namespace {
|
||||
struct AnalogGyro {
|
||||
HAL_AnalogInputHandle handle;
|
||||
uint8_t index;
|
||||
std::string previousAllocation;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
@@ -34,36 +38,41 @@ void InitializeAnalogGyro() {
|
||||
|
||||
extern "C" {
|
||||
HAL_GyroHandle HAL_InitializeAnalogGyro(HAL_AnalogInputHandle analogHandle,
|
||||
const char* allocationLocation,
|
||||
int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
// Handle will be type checked by HAL_IsAccumulatorChannel
|
||||
int16_t channel = getHandleIndex(analogHandle);
|
||||
if (!HAL_IsAccumulatorChannel(analogHandle, status)) {
|
||||
if (*status == 0) {
|
||||
*status = HAL_INVALID_ACCUMULATOR_CHANNEL;
|
||||
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Gyro",
|
||||
0, kNumAccumulators, channel);
|
||||
}
|
||||
return HAL_kInvalidHandle;
|
||||
}
|
||||
|
||||
// handle known to be correct, so no need to type check
|
||||
int16_t channel = getHandleIndex(analogHandle);
|
||||
|
||||
auto handle = analogGyroHandles->Allocate(channel, status);
|
||||
HAL_GyroHandle handle;
|
||||
auto gyro = analogGyroHandles->Allocate(channel, &handle, status);
|
||||
|
||||
if (*status != 0) {
|
||||
if (gyro) {
|
||||
hal::SetLastErrorPreviouslyAllocated(status, "Analog Gyro", channel,
|
||||
gyro->previousAllocation);
|
||||
} else {
|
||||
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Analog Gyro",
|
||||
0, kNumAccumulators, channel);
|
||||
}
|
||||
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
|
||||
}
|
||||
|
||||
// Initialize port structure
|
||||
auto gyro = analogGyroHandles->Get(handle);
|
||||
if (gyro == nullptr) { // would only error on thread issue
|
||||
*status = HAL_HANDLE_ERROR;
|
||||
return HAL_kInvalidHandle;
|
||||
}
|
||||
|
||||
gyro->handle = analogHandle;
|
||||
gyro->index = channel;
|
||||
|
||||
SimAnalogGyroData[channel].initialized = true;
|
||||
|
||||
gyro->previousAllocation = allocationLocation ? allocationLocation : "";
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user