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
|
|
|
|
|
|
|
|
#include "frc/AddressableLED.h"
|
|
|
|
|
|
|
|
|
|
#include <hal/AddressableLED.h>
|
2019-11-20 14:48:14 -08:00
|
|
|
#include <hal/HALBase.h>
|
|
|
|
|
#include <hal/PWM.h>
|
|
|
|
|
#include <hal/Ports.h>
|
2025-02-07 12:37:23 -08:00
|
|
|
#include <hal/UsageReporting.h>
|
2021-05-01 10:28:30 -07:00
|
|
|
#include <wpi/StackTrace.h>
|
2019-11-17 16:39:38 -08:00
|
|
|
|
2021-04-18 20:35:29 -07:00
|
|
|
#include "frc/Errors.h"
|
2019-11-17 16:39:38 -08:00
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
2021-05-23 19:33:33 -07:00
|
|
|
AddressableLED::AddressableLED(int port) : m_port{port} {
|
2019-11-20 14:48:14 -08:00
|
|
|
int32_t status = 0;
|
2019-11-17 16:39:38 -08:00
|
|
|
|
2021-05-01 10:28:30 -07:00
|
|
|
auto stack = wpi::GetStackTrace(1);
|
2025-01-30 18:59:34 -08:00
|
|
|
m_pwmHandle = HAL_InitializePWMPort(port, stack.c_str(), &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Port {}", port);
|
2019-11-20 14:48:14 -08:00
|
|
|
if (m_pwmHandle == HAL_kInvalidHandle) {
|
|
|
|
|
return;
|
2019-11-17 16:39:38 -08:00
|
|
|
}
|
|
|
|
|
|
2019-11-20 14:48:14 -08:00
|
|
|
m_handle = HAL_InitializeAddressableLED(m_pwmHandle, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Port {}", port);
|
2019-11-20 14:48:14 -08:00
|
|
|
if (m_handle == HAL_kInvalidHandle) {
|
2024-09-07 13:58:15 -04:00
|
|
|
HAL_FreePWMPort(m_pwmHandle);
|
2019-11-17 16:39:38 -08:00
|
|
|
}
|
2019-11-20 23:13:39 -05:00
|
|
|
|
2025-02-07 12:37:23 -08:00
|
|
|
HAL_ReportUsage("IO", port, "AddressableLED");
|
2019-11-17 16:39:38 -08:00
|
|
|
}
|
|
|
|
|
|
[hal, wpilib] AddressableLED: add support for other color orders (#7102)
Many LED strips use different color order (GRB in particular is common).
This makes the change at the HAL level. This solves 2 problems; first, no code needs to change in the high level drivers, which was challenging for C++, and second, simulation will behave properly as no conversion is needed. The HAL will accept an array of data objects in the same order no matter what the selected output order is, and will convert before sending it to the FPGA for output.
To accomplish this, NEON bulk load/interleave instructions are utilized. The low level implementation (load, store, and alignment functions) come from the Simd Library. The high level implementations are inspired by the image conversion functions in the simd library, but have diverged significantly.
Much of the implementation uses templates and inlined functions rather than runtime parameters; This is a trade off between the size of the generated code and the amount of function calls done at runtime. Currently, the entire conversion operation is inlined.
2025-02-07 15:36:41 -05:00
|
|
|
void AddressableLED::SetColorOrder(AddressableLED::ColorOrder order) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetAddressableLEDColorOrder(
|
|
|
|
|
m_handle, static_cast<HAL_AddressableLEDColorOrder>(order), &status);
|
|
|
|
|
FRC_CheckErrorStatus(status, "Port {} Color order {}", m_port, order);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-17 16:39:38 -08:00
|
|
|
void AddressableLED::SetLength(int length) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetAddressableLEDLength(m_handle, length, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Port {} length {}", m_port, length);
|
2019-11-17 16:39:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static_assert(sizeof(AddressableLED::LEDData) == sizeof(HAL_AddressableLEDData),
|
|
|
|
|
"LED Structs MUST be the same size");
|
|
|
|
|
|
2022-10-15 16:33:14 -07:00
|
|
|
void AddressableLED::SetData(std::span<const LEDData> ledData) {
|
2019-11-17 16:39:38 -08:00
|
|
|
int32_t status = 0;
|
2022-10-15 16:33:14 -07:00
|
|
|
HAL_WriteAddressableLEDData(m_handle, ledData.data(), ledData.size(),
|
2019-11-17 16:39:38 -08:00
|
|
|
&status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Port {}", m_port);
|
2019-11-17 16:39:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddressableLED::SetData(std::initializer_list<LEDData> ledData) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_WriteAddressableLEDData(m_handle, ledData.begin(), ledData.size(),
|
|
|
|
|
&status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Port {}", m_port);
|
2019-11-17 16:39:38 -08:00
|
|
|
}
|
|
|
|
|
|
2023-04-28 20:54:58 -07:00
|
|
|
void AddressableLED::SetBitTiming(units::nanosecond_t highTime0,
|
|
|
|
|
units::nanosecond_t lowTime0,
|
|
|
|
|
units::nanosecond_t highTime1,
|
|
|
|
|
units::nanosecond_t lowTime1) {
|
2019-11-17 16:39:38 -08:00
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetAddressableLEDBitTiming(
|
2023-04-28 20:54:58 -07:00
|
|
|
m_handle, highTime0.to<int32_t>(), lowTime0.to<int32_t>(),
|
|
|
|
|
highTime1.to<int32_t>(), lowTime1.to<int32_t>(), &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Port {}", m_port);
|
2019-11-17 16:39:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddressableLED::SetSyncTime(units::microsecond_t syncTime) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetAddressableLEDSyncTime(m_handle, syncTime.to<int32_t>(), &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Port {}", m_port);
|
2019-11-17 16:39:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddressableLED::Start() {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_StartAddressableLEDOutput(m_handle, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Port {}", m_port);
|
2019-11-17 16:39:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddressableLED::Stop() {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_StopAddressableLEDOutput(m_handle, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Port {}", m_port);
|
2019-11-17 16:39:38 -08:00
|
|
|
}
|
2019-11-29 15:16:57 -08:00
|
|
|
|
|
|
|
|
void AddressableLED::LEDData::SetHSV(int h, int s, int v) {
|
2020-04-06 02:08:52 -04:00
|
|
|
SetLED(Color::FromHSV(h, s, v));
|
2019-11-29 15:16:57 -08:00
|
|
|
}
|