[hal, wpilib] Update Addressable LED support (#8100)

This commit is contained in:
Peter Johnson
2025-07-21 21:52:10 -07:00
committed by GitHub
parent 8aa312fb6f
commit f3af50fc8e
40 changed files with 857 additions and 1104 deletions

View File

@@ -4,6 +4,8 @@
#include "frc/AddressableLED.h"
#include <algorithm>
#include <hal/AddressableLED.h>
#include <hal/HALBase.h>
#include <hal/PWM.h>
@@ -12,39 +14,39 @@
#include <wpi/StackTrace.h>
#include "frc/Errors.h"
#include "frc/SensorUtil.h"
using namespace frc;
AddressableLED::AddressableLED(int port) : m_port{port} {
AddressableLED::AddressableLED(int channel) : m_channel{channel} {
if (!SensorUtil::CheckDigitalChannel(channel)) {
throw FRC_MakeError(err::ChannelIndexOutOfRange, "Channel {}", channel);
}
int32_t status = 0;
auto stack = wpi::GetStackTrace(1);
m_pwmHandle = HAL_InitializePWMPort(port, stack.c_str(), &status);
FRC_CheckErrorStatus(status, "Port {}", port);
if (m_pwmHandle == HAL_kInvalidHandle) {
return;
}
m_handle = HAL_InitializeAddressableLED(channel, stack.c_str(), &status);
FRC_CheckErrorStatus(status, "Channel {}", channel);
m_handle = HAL_InitializeAddressableLED(m_pwmHandle, &status);
FRC_CheckErrorStatus(status, "Port {}", port);
if (m_handle == HAL_kInvalidHandle) {
HAL_FreePWMPort(m_pwmHandle);
}
HAL_ReportUsage("IO", port, "AddressableLED");
HAL_ReportUsage("IO", channel, "AddressableLED");
}
void AddressableLED::SetColorOrder(AddressableLED::ColorOrder order) {
m_colorOrder = order;
}
void AddressableLED::SetStart(int start) {
m_start = start;
int32_t status = 0;
HAL_SetAddressableLEDColorOrder(
m_handle, static_cast<HAL_AddressableLEDColorOrder>(order), &status);
FRC_CheckErrorStatus(status, "Port {} Color order {}", m_port, order);
HAL_SetAddressableLEDStart(m_handle, start, &status);
FRC_CheckErrorStatus(status, "Channel {} start {}", m_channel, start);
}
void AddressableLED::SetLength(int length) {
m_length = length;
int32_t status = 0;
HAL_SetAddressableLEDLength(m_handle, length, &status);
FRC_CheckErrorStatus(status, "Port {} length {}", m_port, length);
FRC_CheckErrorStatus(status, "Channel {} length {}", m_channel, length);
}
static_assert(sizeof(AddressableLED::LEDData) == sizeof(HAL_AddressableLEDData),
@@ -52,45 +54,25 @@ static_assert(sizeof(AddressableLED::LEDData) == sizeof(HAL_AddressableLEDData),
void AddressableLED::SetData(std::span<const LEDData> ledData) {
int32_t status = 0;
HAL_WriteAddressableLEDData(m_handle, ledData.data(), ledData.size(),
&status);
FRC_CheckErrorStatus(status, "Port {}", m_port);
HAL_SetAddressableLEDData(
m_start, std::min(static_cast<size_t>(m_length), ledData.size()),
static_cast<HAL_AddressableLEDColorOrder>(m_colorOrder), ledData.data(),
&status);
FRC_CheckErrorStatus(status, "Port {}", m_channel);
}
void AddressableLED::SetData(std::initializer_list<LEDData> ledData) {
int32_t status = 0;
HAL_WriteAddressableLEDData(m_handle, ledData.begin(), ledData.size(),
&status);
FRC_CheckErrorStatus(status, "Port {}", m_port);
SetData(std::span{ledData.begin(), ledData.end()});
}
void AddressableLED::SetBitTiming(units::nanosecond_t highTime0,
units::nanosecond_t lowTime0,
units::nanosecond_t highTime1,
units::nanosecond_t lowTime1) {
void AddressableLED::SetGlobalData(int start, ColorOrder colorOrder,
std::span<const LEDData> ledData) {
int32_t status = 0;
HAL_SetAddressableLEDBitTiming(
m_handle, highTime0.to<int32_t>(), lowTime0.to<int32_t>(),
highTime1.to<int32_t>(), lowTime1.to<int32_t>(), &status);
FRC_CheckErrorStatus(status, "Port {}", m_port);
}
void AddressableLED::SetSyncTime(units::microsecond_t syncTime) {
int32_t status = 0;
HAL_SetAddressableLEDSyncTime(m_handle, syncTime.to<int32_t>(), &status);
FRC_CheckErrorStatus(status, "Port {}", m_port);
}
void AddressableLED::Start() {
int32_t status = 0;
HAL_StartAddressableLEDOutput(m_handle, &status);
FRC_CheckErrorStatus(status, "Port {}", m_port);
}
void AddressableLED::Stop() {
int32_t status = 0;
HAL_StopAddressableLEDOutput(m_handle, &status);
FRC_CheckErrorStatus(status, "Port {}", m_port);
HAL_SetAddressableLEDData(
start, ledData.size(),
static_cast<HAL_AddressableLEDColorOrder>(colorOrder), ledData.data(),
&status);
FRC_CheckErrorStatus(status, "");
}
void AddressableLED::LEDData::SetHSV(int h, int s, int v) {

View File

@@ -9,108 +9,90 @@
#include <hal/simulation/AddressableLEDData.h>
#include "frc/AddressableLED.h"
using namespace frc;
using namespace frc::sim;
AddressableLEDSim::AddressableLEDSim() : m_index{0} {}
AddressableLEDSim::AddressableLEDSim(int channel) : m_channel{channel} {}
AddressableLEDSim::AddressableLEDSim(const AddressableLED& addressableLED)
: m_index{0} {}
AddressableLEDSim AddressableLEDSim::CreateForChannel(int pwmChannel) {
int index = HALSIM_FindAddressableLEDForChannel(pwmChannel);
if (index < 0) {
throw std::out_of_range("no addressable LED found for PWM channel");
}
return AddressableLEDSim{index};
}
AddressableLEDSim AddressableLEDSim::CreateForIndex(int index) {
return AddressableLEDSim{index};
}
: m_channel{addressableLED.GetChannel()} {}
std::unique_ptr<CallbackStore> AddressableLEDSim::RegisterInitializedCallback(
NotifyCallback callback, bool initialNotify) {
auto store = std::make_unique<CallbackStore>(
m_index, -1, callback, &HALSIM_CancelAddressableLEDInitializedCallback);
m_channel, -1, callback, &HALSIM_CancelAddressableLEDInitializedCallback);
store->SetUid(HALSIM_RegisterAddressableLEDInitializedCallback(
m_index, &CallbackStoreThunk, store.get(), initialNotify));
m_channel, &CallbackStoreThunk, store.get(), initialNotify));
return store;
}
bool AddressableLEDSim::GetInitialized() const {
return HALSIM_GetAddressableLEDInitialized(m_index);
return HALSIM_GetAddressableLEDInitialized(m_channel);
}
void AddressableLEDSim::SetInitialized(bool initialized) {
HALSIM_SetAddressableLEDInitialized(m_index, initialized);
HALSIM_SetAddressableLEDInitialized(m_channel, initialized);
}
std::unique_ptr<CallbackStore> AddressableLEDSim::RegisterOutputPortCallback(
std::unique_ptr<CallbackStore> AddressableLEDSim::RegisterStartCallback(
NotifyCallback callback, bool initialNotify) {
auto store = std::make_unique<CallbackStore>(
m_index, -1, callback, &HALSIM_CancelAddressableLEDOutputPortCallback);
store->SetUid(HALSIM_RegisterAddressableLEDOutputPortCallback(
m_index, &CallbackStoreThunk, store.get(), initialNotify));
m_channel, -1, callback, &HALSIM_CancelAddressableLEDStartCallback);
store->SetUid(HALSIM_RegisterAddressableLEDStartCallback(
m_channel, &CallbackStoreThunk, store.get(), initialNotify));
return store;
}
int AddressableLEDSim::GetOutputPort() const {
return HALSIM_GetAddressableLEDOutputPort(m_index);
int AddressableLEDSim::GetStart() const {
return HALSIM_GetAddressableLEDStart(m_channel);
}
void AddressableLEDSim::SetOutputPort(int outputPort) {
HALSIM_SetAddressableLEDOutputPort(m_index, outputPort);
void AddressableLEDSim::SetStart(int start) {
HALSIM_SetAddressableLEDStart(m_channel, start);
}
std::unique_ptr<CallbackStore> AddressableLEDSim::RegisterLengthCallback(
NotifyCallback callback, bool initialNotify) {
auto store = std::make_unique<CallbackStore>(
m_index, -1, callback, &HALSIM_CancelAddressableLEDLengthCallback);
m_channel, -1, callback, &HALSIM_CancelAddressableLEDLengthCallback);
store->SetUid(HALSIM_RegisterAddressableLEDLengthCallback(
m_index, &CallbackStoreThunk, store.get(), initialNotify));
m_channel, &CallbackStoreThunk, store.get(), initialNotify));
return store;
}
int AddressableLEDSim::GetLength() const {
return HALSIM_GetAddressableLEDLength(m_index);
return HALSIM_GetAddressableLEDLength(m_channel);
}
void AddressableLEDSim::SetLength(int length) {
HALSIM_SetAddressableLEDLength(m_index, length);
HALSIM_SetAddressableLEDLength(m_channel, length);
}
std::unique_ptr<CallbackStore> AddressableLEDSim::RegisterRunningCallback(
NotifyCallback callback, bool initialNotify) {
auto store = std::make_unique<CallbackStore>(
m_index, -1, callback, &HALSIM_CancelAddressableLEDRunningCallback);
store->SetUid(HALSIM_RegisterAddressableLEDRunningCallback(
m_index, &CallbackStoreThunk, store.get(), initialNotify));
return store;
int AddressableLEDSim::GetData(struct HAL_AddressableLEDData* data) const {
return GetGlobalData(GetStart(), GetLength(), data);
}
int AddressableLEDSim::GetRunning() const {
return HALSIM_GetAddressableLEDRunning(m_index);
}
void AddressableLEDSim::SetRunning(bool running) {
HALSIM_SetAddressableLEDRunning(m_index, running);
void AddressableLEDSim::SetData(struct HAL_AddressableLEDData* data) {
SetGlobalData(GetStart(), GetLength(), data);
}
std::unique_ptr<CallbackStore> AddressableLEDSim::RegisterDataCallback(
ConstBufferCallback callback, bool initialNotify) {
auto store = std::make_unique<CallbackStore>(
m_index, -1, callback, &HALSIM_CancelAddressableLEDDataCallback);
-1, callback, &HALSIM_CancelAddressableLEDDataCallback);
store->SetUid(HALSIM_RegisterAddressableLEDDataCallback(
m_index, &ConstBufferCallbackStoreThunk, store.get()));
&ConstBufferCallbackStoreThunk, store.get()));
return store;
}
int AddressableLEDSim::GetData(struct HAL_AddressableLEDData* data) const {
return HALSIM_GetAddressableLEDData(m_index, data);
int AddressableLEDSim::GetGlobalData(int start, int length,
struct HAL_AddressableLEDData* data) {
return HALSIM_GetAddressableLEDData(start, length, data);
}
void AddressableLEDSim::SetData(struct HAL_AddressableLEDData* data,
int length) {
HALSIM_SetAddressableLEDData(m_index, data, length);
void AddressableLEDSim::SetGlobalData(int start, int length,
struct HAL_AddressableLEDData* data) {
HALSIM_SetAddressableLEDData(start, length, data);
}

View File

@@ -9,7 +9,6 @@
#include <hal/AddressableLED.h>
#include <hal/AddressableLEDTypes.h>
#include <hal/PWM.h>
#include <hal/Types.h>
#include <units/time.h>
@@ -21,15 +20,15 @@ namespace frc {
/**
* A class for driving addressable LEDs, such as WS2812B, WS2815, and NeoPixels.
*
* By default, the timing supports WS2812B and WS2815 LEDs, but is configurable
* using SetBitTiming()
*
* Some LEDs use a different color order than the default GRB. The color order
* is configurable using SetColorOrder().
*
* <p>Only 1 LED driver is currently supported by the roboRIO. However,
* multiple LED strips can be connected in series and controlled from the
* single driver.
* Up to 1024 LEDs may be controlled in total across all AddressableLED
* instances. A single global buffer is used for all instances. The start
* position used for LED data for the output is set via SetStart() and the
* length of the strip is set via SetLength(). Both of these default to zero, so
* multiple instances will access the same pixel data unless SetStart() is
* called to adjust the starting point.
*/
class AddressableLED {
public:
@@ -52,7 +51,6 @@ class AddressableLED {
r = _r;
g = _g;
b = _b;
padding = 0;
}
/**
@@ -101,15 +99,22 @@ class AddressableLED {
};
/**
* Constructs a new driver for a specific port.
* Constructs a new driver for a specific channel.
*
* @param port the output port to use (Must be a PWM header)
* @param channel the output channel to use
*/
explicit AddressableLED(int port);
explicit AddressableLED(int channel);
AddressableLED(AddressableLED&&) = default;
AddressableLED& operator=(AddressableLED&&) = default;
/**
* Gets the channel for this addressable LED.
*
* @return channel
*/
int GetChannel() const { return m_channel; }
/**
* Sets the color order for this AddressableLED. The default order is GRB.
*
@@ -119,79 +124,59 @@ class AddressableLED {
*/
void SetColorOrder(ColorOrder order);
/**
* Sets the display start of the LED strip in the global buffer.
*
* @param start the strip start, in LEDs
*/
void SetStart(int start);
/**
* Gets the display start of the LED strip in the global buffer.
*
* @return the strip start, in LEDs
*/
int GetStart() const { return m_start; }
/**
* Sets the length of the LED strip.
*
* <p>Calling this is an expensive call, so its best to call it once, then
* just update data.
*
* <p>The max length is 5460 LEDs.
*
* @param length the strip length
* @param length the strip length, in LEDs
*/
void SetLength(int length);
/**
* Sets the led output data.
*
* <p>If the output is enabled, this will start writing the next data cycle.
* It is safe to call, even while output is enabled.
* Sets the LED output data. This will write to the global buffer starting at
* the location set by SetStart() and up to the length set by SetLength().
*
* @param ledData the buffer to write
*/
void SetData(std::span<const LEDData> ledData);
/**
* Sets the led output data.
*
* <p>If the output is enabled, this will start writing the next data cycle.
* It is safe to call, even while output is enabled.
* Sets the LED output data. This will write to the global buffer starting at
* the location set by SetStart() and up to the length set by SetLength().
*
* @param ledData the buffer to write
*/
void SetData(std::initializer_list<LEDData> ledData);
/**
* Sets the bit timing.
* Sets the LED output data at an arbitrary location in the global buffer.
*
* <p>By default, the driver is set up to drive WS2812B and WS2815, so nothing
* needs to be set for those.
*
* @param highTime0 high time for 0 bit (default 400 ns)
* @param lowTime0 low time for 0 bit (default 900 ns)
* @param highTime1 high time for 1 bit (default 900 ns)
* @param lowTime1 low time for 1 bit (default 600 ns)
* @param start the start location, in LEDs
* @param colorOrder the color order
* @param ledData the buffer to write
*/
void SetBitTiming(units::nanosecond_t highTime0, units::nanosecond_t lowTime0,
units::nanosecond_t highTime1,
units::nanosecond_t lowTime1);
/**
* Sets the sync time.
*
* <p>The sync time is the time to hold output so LEDs enable. Default set for
* WS2812B and WS2815.
*
* @param syncTime the sync time (default 280us)
*/
void SetSyncTime(units::microsecond_t syncTime);
/**
* Starts the output.
*
* <p>The output writes continuously.
*/
void Start();
/**
* Stops the output.
*/
void Stop();
static void SetGlobalData(int start, ColorOrder colorOrder,
std::span<const LEDData> ledData);
private:
hal::Handle<HAL_DigitalHandle, HAL_FreePWMPort> m_pwmHandle;
hal::Handle<HAL_AddressableLEDHandle, HAL_FreeAddressableLED> m_handle;
int m_port;
int m_channel;
int m_start{0};
int m_length{0};
ColorOrder m_colorOrder{kGRB};
};
constexpr auto format_as(AddressableLED::ColorOrder order) {

View File

@@ -22,9 +22,11 @@ namespace sim {
class AddressableLEDSim {
public:
/**
* Constructs for the first addressable LED.
* Constructs an addressable LED for a specific channel.
*
* @param channel output channel
*/
AddressableLEDSim();
explicit AddressableLEDSim(int channel);
/**
* Constructs from an AddressableLED object.
@@ -33,25 +35,6 @@ class AddressableLEDSim {
*/
explicit AddressableLEDSim(const AddressableLED& addressableLED);
/**
* Creates an AddressableLEDSim for a PWM channel.
*
* @param pwmChannel PWM channel
* @return Simulated object
* @throws std::out_of_range if no AddressableLED is configured for that
* channel
*/
static AddressableLEDSim CreateForChannel(int pwmChannel);
/**
* Creates an AddressableLEDSim for a simulated index.
* The index is incremented for each simulated AddressableLED.
*
* @param index simulator index
* @return Simulated object
*/
static AddressableLEDSim CreateForIndex(int index);
/**
* Register a callback on the Initialized property.
*
@@ -79,30 +62,30 @@ class AddressableLEDSim {
void SetInitialized(bool initialized);
/**
* Register a callback on the output port.
* Register a callback on the start.
*
* @param callback the callback that will be called whenever the output port
* @param callback the callback that will be called whenever the start
* is changed
* @param initialNotify if true, the callback will be run on the initial value
* @return the CallbackStore object associated with this callback
*/
[[nodiscard]]
std::unique_ptr<CallbackStore> RegisterOutputPortCallback(
NotifyCallback callback, bool initialNotify);
std::unique_ptr<CallbackStore> RegisterStartCallback(NotifyCallback callback,
bool initialNotify);
/**
* Get the output port.
* Get the start.
*
* @return the output port
* @return the start
*/
int GetOutputPort() const;
int GetStart() const;
/**
* Change the output port.
* Change the start.
*
* @param outputPort the new output port
* @param start the new start
*/
void SetOutputPort(int outputPort);
void SetStart(int start);
/**
* Register a callback on the length.
@@ -130,44 +113,6 @@ class AddressableLEDSim {
*/
void SetLength(int length);
/**
* Register a callback on whether the LEDs are running.
*
* @param callback the callback that will be called whenever the LED state is
* changed
* @param initialNotify if true, the callback will be run on the initial value
* @return the CallbackStore object associated with this callback
*/
[[nodiscard]]
std::unique_ptr<CallbackStore> RegisterRunningCallback(
NotifyCallback callback, bool initialNotify);
/**
* Check if the LEDs are running.
*
* @return true if they are
*/
int GetRunning() const;
/**
* Change whether the LEDs are active.
*
* @param running the new value
*/
void SetRunning(bool running);
/**
* Register a callback on the LED data.
*
* @param callback the callback that will be called whenever the LED data is
* changed
* @param initialNotify if true, the callback will be run on the initial value
* @return the CallbackStore object associated with this callback
*/
[[nodiscard]]
std::unique_ptr<CallbackStore> RegisterDataCallback(
ConstBufferCallback callback, bool initialNotify);
/**
* Get the LED data.
*
@@ -180,14 +125,44 @@ class AddressableLEDSim {
* Change the LED data.
*
* @param data the new data
* @param length the length of the LED data
*/
void SetData(struct HAL_AddressableLEDData* data, int length);
void SetData(struct HAL_AddressableLEDData* data);
/**
* Register a callback on the LED data.
*
* @param callback the callback that will be called whenever the LED data is
* changed
* @param initialNotify if true, the callback will be run on the initial value
* @return the CallbackStore object associated with this callback
*/
[[nodiscard]]
static std::unique_ptr<CallbackStore> RegisterDataCallback(
ConstBufferCallback callback, bool initialNotify);
/**
* Get the global LED data.
*
* @param start the start of the LED data
* @param length the length of the LED data
* @param data output parameter to fill with LED data
* @return the length of the LED data
*/
static int GetGlobalData(int start, int length,
struct HAL_AddressableLEDData* data);
/**
* Change the global LED data.
*
* @param start the start of the LED data
* @param length the length of the LED data
* @param data the new data
*/
static void SetGlobalData(int start, int length,
struct HAL_AddressableLEDData* data);
private:
explicit AddressableLEDSim(int index) : m_index{index} {}
int m_index;
int m_channel;
};
} // namespace sim
} // namespace frc