2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2019-11-17 16:39:38 -08:00
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/hal/AddressableLED.h"
|
2019-11-17 16:39:38 -08:00
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
|
2026-01-04 00:41:53 -08:00
|
|
|
#include "DigitalInternal.hpp"
|
|
|
|
|
#include "HALInitializer.hpp"
|
|
|
|
|
#include "HALInternal.hpp"
|
|
|
|
|
#include "PortsInternal.hpp"
|
|
|
|
|
#include "mockdata/AddressableLEDDataInternal.hpp"
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/hal/Errors.h"
|
2026-01-04 00:41:53 -08:00
|
|
|
#include "wpi/hal/handles/HandlesInternal.hpp"
|
2019-11-17 15:05:56 -08:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
using namespace wpi::hal;
|
2019-11-17 15:05:56 -08:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
namespace wpi::hal::init {
|
2025-07-21 21:52:10 -07:00
|
|
|
void InitializeAddressableLED() {}
|
2025-11-07 20:00:05 -05:00
|
|
|
} // namespace wpi::hal::init
|
2019-11-17 15:05:56 -08:00
|
|
|
|
2019-11-17 16:39:38 -08:00
|
|
|
extern "C" {
|
|
|
|
|
HAL_AddressableLEDHandle HAL_InitializeAddressableLED(
|
2025-07-21 21:52:10 -07:00
|
|
|
int32_t channel, const char* allocationLocation, int32_t* status) {
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::hal::init::CheckInit();
|
2019-11-17 15:05:56 -08:00
|
|
|
|
2025-07-21 21:52:10 -07:00
|
|
|
if (channel < 0 || channel >= kNumAddressableLEDs) {
|
|
|
|
|
*status = RESOURCE_OUT_OF_RANGE;
|
2025-11-07 20:01:58 -05:00
|
|
|
wpi::hal::SetLastErrorIndexOutOfRange(status,
|
|
|
|
|
"Invalid Index for AddressableLED", 0,
|
|
|
|
|
kNumAddressableLEDs, channel);
|
2019-11-18 15:25:04 -08:00
|
|
|
return HAL_kInvalidHandle;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-21 21:52:10 -07:00
|
|
|
HAL_DigitalHandle handle;
|
2019-11-18 15:25:04 -08:00
|
|
|
|
2025-07-21 21:52:10 -07:00
|
|
|
auto port = digitalChannelHandles->Allocate(
|
|
|
|
|
channel, HAL_HandleEnum::AddressableLED, &handle, status);
|
2019-11-17 15:05:56 -08:00
|
|
|
|
2025-07-21 21:52:10 -07:00
|
|
|
if (*status != 0) {
|
|
|
|
|
if (port) {
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::hal::SetLastErrorPreviouslyAllocated(status, "PWM or DIO", channel,
|
2025-11-07 20:01:58 -05:00
|
|
|
port->previousAllocation);
|
2025-07-21 21:52:10 -07:00
|
|
|
} else {
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::hal::SetLastErrorIndexOutOfRange(status,
|
2025-11-07 20:01:58 -05:00
|
|
|
"Invalid Index for AddressableLED",
|
|
|
|
|
0, kNumAddressableLEDs, channel);
|
2025-07-21 21:52:10 -07:00
|
|
|
}
|
|
|
|
|
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
|
|
|
|
|
2025-07-21 21:52:10 -07:00
|
|
|
port->channel = static_cast<uint8_t>(channel);
|
|
|
|
|
|
|
|
|
|
SimAddressableLEDData[channel].start = 0;
|
|
|
|
|
SimAddressableLEDData[channel].length = 0;
|
|
|
|
|
SimAddressableLEDData[channel].initialized = true;
|
|
|
|
|
port->previousAllocation = allocationLocation ? allocationLocation : "";
|
|
|
|
|
|
2019-11-17 15:05:56 -08:00
|
|
|
return handle;
|
2019-11-17 16:39:38 -08:00
|
|
|
}
|
|
|
|
|
|
2019-11-17 15:05:56 -08:00
|
|
|
void HAL_FreeAddressableLED(HAL_AddressableLEDHandle handle) {
|
2025-07-21 21:52:10 -07:00
|
|
|
auto port =
|
|
|
|
|
digitalChannelHandles->Get(handle, HAL_HandleEnum::AddressableLED);
|
|
|
|
|
// no status, so no need to check for a proper free.
|
|
|
|
|
digitalChannelHandles->Free(handle, HAL_HandleEnum::AddressableLED);
|
|
|
|
|
if (port == nullptr) {
|
2019-11-17 15:05:56 -08:00
|
|
|
return;
|
|
|
|
|
}
|
2025-07-21 21:52:10 -07:00
|
|
|
SimAddressableLEDData[port->channel].initialized = false;
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
2019-11-17 16:39:38 -08:00
|
|
|
|
2025-07-21 21:52:10 -07:00
|
|
|
void HAL_SetAddressableLEDStart(HAL_AddressableLEDHandle handle, int32_t start,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
auto port =
|
|
|
|
|
digitalChannelHandles->Get(handle, HAL_HandleEnum::AddressableLED);
|
|
|
|
|
if (!port) {
|
2019-11-17 15:05:56 -08:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-07-21 21:52:10 -07:00
|
|
|
if (start > HAL_kAddressableLEDMaxLength || start < 0) {
|
2019-11-17 15:05:56 -08:00
|
|
|
*status = PARAMETER_OUT_OF_RANGE;
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::hal::SetLastError(
|
2021-06-06 16:13:58 -07:00
|
|
|
status,
|
|
|
|
|
fmt::format(
|
2025-07-21 21:52:10 -07:00
|
|
|
"LED start must be less than or equal to {}. {} was requested",
|
|
|
|
|
HAL_kAddressableLEDMaxLength, start));
|
2019-11-17 15:05:56 -08:00
|
|
|
return;
|
|
|
|
|
}
|
2025-07-21 21:52:10 -07:00
|
|
|
SimAddressableLEDData[port->channel].start = start;
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
2019-11-17 16:39:38 -08:00
|
|
|
|
2025-07-21 21:52:10 -07:00
|
|
|
void HAL_SetAddressableLEDLength(HAL_AddressableLEDHandle handle,
|
2019-11-17 15:05:56 -08:00
|
|
|
int32_t length, int32_t* status) {
|
2025-07-21 21:52:10 -07:00
|
|
|
auto port =
|
|
|
|
|
digitalChannelHandles->Get(handle, HAL_HandleEnum::AddressableLED);
|
|
|
|
|
if (!port) {
|
2019-11-17 15:05:56 -08:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-07-21 21:52:10 -07:00
|
|
|
if (length > HAL_kAddressableLEDMaxLength || length < 0) {
|
2019-11-17 15:05:56 -08:00
|
|
|
*status = PARAMETER_OUT_OF_RANGE;
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::hal::SetLastError(
|
2021-06-06 16:13:58 -07:00
|
|
|
status,
|
|
|
|
|
fmt::format(
|
2025-07-21 21:52:10 -07:00
|
|
|
"LED length must be less than or equal to {}. {} was requested",
|
|
|
|
|
HAL_kAddressableLEDMaxLength, length));
|
2019-11-17 15:05:56 -08:00
|
|
|
return;
|
|
|
|
|
}
|
2025-07-21 21:52:10 -07:00
|
|
|
SimAddressableLEDData[port->channel].length = length;
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
2019-11-17 16:39:38 -08:00
|
|
|
|
2025-07-21 21:52:10 -07:00
|
|
|
void HAL_SetAddressableLEDData(int32_t start, int32_t length,
|
|
|
|
|
HAL_AddressableLEDColorOrder colorOrder,
|
|
|
|
|
const struct HAL_AddressableLEDData* data,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
SimAddressableLEDDataBuffer->SetData(start, length, data);
|
2019-11-17 15:05:56 -08:00
|
|
|
}
|
2019-11-17 16:39:38 -08:00
|
|
|
} // extern "C"
|