mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[hal] Resource classes: Use expected, refactor errors (#8768)
Also revamp SetLastError et al; Instead of taking status by pointer, take by value and return new status instead. Rename from SetLast to Make to make this new usage obvious. Also move declarations for the error functions from duplicated in the per-target HALInternal.hpp to a common ErrorHandling.hpp.
This commit is contained in:
@@ -8,9 +8,9 @@
|
||||
#include <thread>
|
||||
|
||||
#include "HALInitializer.hpp"
|
||||
#include "HALInternal.hpp"
|
||||
#include "PortsInternal.hpp"
|
||||
#include "SmartIo.hpp"
|
||||
#include "wpi/hal/ErrorHandling.hpp"
|
||||
#include "wpi/hal/Errors.h"
|
||||
#include "wpi/hal/handles/HandlesInternal.hpp"
|
||||
#include "wpi/hal/monotonic_clock.hpp"
|
||||
@@ -28,28 +28,21 @@ HAL_DutyCycleHandle HAL_InitializeDutyCycle(int32_t channel,
|
||||
wpi::hal::init::CheckInit();
|
||||
|
||||
if (channel < 0 || channel >= kNumSmartIo) {
|
||||
*status = RESOURCE_OUT_OF_RANGE;
|
||||
wpi::hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for DutyCycle",
|
||||
0, kNumSmartIo, channel);
|
||||
*status = MakeErrorIndexOutOfRange(RESOURCE_OUT_OF_RANGE,
|
||||
"Invalid Index for DutyCycle", 0,
|
||||
kNumSmartIo, channel);
|
||||
return HAL_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
HAL_DigitalHandle handle;
|
||||
auto resource = smartIoHandles->Allocate(channel, HAL_HandleEnum::DUTY_CYCLE,
|
||||
"DutyCycle");
|
||||
|
||||
auto port = smartIoHandles->Allocate(channel, HAL_HandleEnum::DUTY_CYCLE,
|
||||
&handle, status);
|
||||
|
||||
if (*status != 0) {
|
||||
if (port) {
|
||||
wpi::hal::SetLastErrorPreviouslyAllocated(status, "SmartIo", channel,
|
||||
port->previousAllocation);
|
||||
} else {
|
||||
wpi::hal::SetLastErrorIndexOutOfRange(
|
||||
status, "Invalid Index for DutyCycle", 0, kNumSmartIo, channel);
|
||||
}
|
||||
if (!resource) {
|
||||
*status = resource.error();
|
||||
return HAL_INVALID_HANDLE; // failed to allocate. Pass error back.
|
||||
}
|
||||
|
||||
auto [handle, port] = *resource;
|
||||
port->channel = channel;
|
||||
|
||||
*status = port->InitializeMode(SmartIoMode::PwmInput);
|
||||
|
||||
Reference in New Issue
Block a user