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:
@@ -11,9 +11,9 @@
|
||||
#include <fmt/format.h>
|
||||
|
||||
#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"
|
||||
@@ -32,28 +32,20 @@ HAL_DigitalHandle HAL_InitializePWMPort(int32_t channel,
|
||||
wpi::hal::init::CheckInit();
|
||||
|
||||
if (channel < 0 || channel >= kNumSmartIo) {
|
||||
*status = RESOURCE_OUT_OF_RANGE;
|
||||
wpi::hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for PWM", 0,
|
||||
kNumSmartIo, channel);
|
||||
*status =
|
||||
MakeErrorIndexOutOfRange(RESOURCE_OUT_OF_RANGE, "Invalid Index for PWM",
|
||||
0, kNumSmartIo, channel);
|
||||
return HAL_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
HAL_DigitalHandle handle;
|
||||
auto resource = smartIoHandles->Allocate(channel, HAL_HandleEnum::PWM, "PWM");
|
||||
|
||||
auto port =
|
||||
smartIoHandles->Allocate(channel, HAL_HandleEnum::PWM, &handle, status);
|
||||
|
||||
if (*status != 0) {
|
||||
if (port) {
|
||||
wpi::hal::SetLastErrorPreviouslyAllocated(status, "SmartIo", channel,
|
||||
port->previousAllocation);
|
||||
} else {
|
||||
wpi::hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for PWM", 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::PwmOutput);
|
||||
@@ -113,9 +105,8 @@ void HAL_SetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
|
||||
|
||||
if (microsecondPulseTime < 0 ||
|
||||
(microsecondPulseTime != 0xFFFF && microsecondPulseTime >= 4096)) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
wpi::hal::SetLastError(
|
||||
status,
|
||||
*status = MakeError(
|
||||
PARAMETER_OUT_OF_RANGE,
|
||||
fmt::format("Pulse time {} out of range. Expect [0-4096) or 0xFFFF",
|
||||
microsecondPulseTime));
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user