|
ESP32-RGB-LEDStrip
RGB LED library with non-blocking multi-threading support
|
This project features a modern C++ library for displaying pixels in RGB LED strips (and LED matrices). It is designed to be extensible to other RGB LED controllers (not only LED strips) and CPU architectures. It capitalizes experience from another project.
The code is organized as an Arduino library, but does not use Arduino code, so it can be used in ESP-IDF with minimal changes.
RGB and HSL color models.
You can use one model or the other on each pixel. Conversion is automatic. There is implicit conversion from/to packed RGB format. This is independent from the pixel format required by the pixel driver.
Display pixels all at once.
Compute each pixel color individually then display them all at once. No need for shared variables or thread synchronization.
Display based on priorities (optional).
When having multiple threads using the same LED strip, a thread can obtain exclusive access without blocking semantics. The display priority is independent from the thread priority. This works on non-threaded applications, too.
DMA mode (optional).
The library transparently falls back to PIO mode if not available.
Take a look at the provided examples and the API documentation.
No level shifter
If your LED strip works with the same voltage as your DevKit board, use the following configuration:

Wire Dout to Din in the first pixel via a 300 to 1000 ohm resistor. The resistor should be at the end of the wire closest to the LED strip, not the DevKit board. This resistor can be omitted only if your LED strip already has a resistor in Din. Otherwise your LED strip could burn out.
Then, tell this library not to use open-drain mode in Dout.
Simple level shifter
If your LED strip works with an higher voltage than your DevKit board, use the following configuration:

Then, tell this library to use open-drain mode in Dout.
If VLed is higher than 5V you must increase the resistor value to limit the current into Dout to 15mA or less.
Tag notation:
Dout pads to Din pads.Din an Dout pads must be in the same (unnamed) net. Do not wire Dout to Dout or Din to Din. It does not work.Vled is the DC voltage required by the LED strip.Most LED strips require a 5V power supply and show wrong colors when powered with a 3V3 power supply (but they work). To overcome this, set the global brightness to 127 or less. This workaround will show inaccurate but correct colors without the need to provide a 5V power supply.
Declare a local PixelVector object to handle pixel colors. This type is derived from std::vector adding some handy methods:
Pass that object to LEDStrip::show() to show the pixels all at once. This method is thread-safe but, in this way, no thread has exclusive access to the LED strip.
To explicitly set RGB channels (example):
To explicitly set HSL channels (example):
Note the parameter units and ranges (no floating point required):
A thread can have exclusive access to an LED strip without blocking other threads using the same LED strip.
RgbGuard works the same way as std::lock_guard, but non-blocking:
As simple as that. This works with non-threaded applications as well. The guard is automatically released when the guard variable goes out of scope.
PixelVector instance is not required to match the count of pixels in the LEDStrip instance. It works with any size.To create a PixelVector instance that matches the size of a LEDStrip instance, let's say led_strip:
LEDStrip::shutdown() at program startup because pixels continue to lit after a system reset.LEDStrip instance is required for the shutdown() method only.[!IMPORTANT] The support for LED matrices has not been tested using real hardware.
An LED matrix is simply a regular LED strip with a specific pixel arrangement in rows and columns.
Features:
Any wiring schema via three parameters:
See an example.
[!NOTE] Tiling is not supported.
The LEDStrip class (and descendants) also represents an LED strip able to display pixels in a 2D matrix. For better semantics, you may use the LEDMatrix alias instead. To create an LED matrix use the second constructor:
LedMatrixParameters: specifies the physical arrangement of pixels in the underlying LED strip.PixelMatrix: holds a "[raster graphic](https://en.wikipedia.org/wiki/Raster_graphics)" in the usual row-major format. You can take advantage of raster graphic libraries thanks to PixelMatrix::data(). This is an specialization of PixelVector (and std::vector<Pixel>).The size (the number of rows and columns) of the physical LED matrix must match the size of the PixelMatrix instance, but the library does not enforce this. Otherwise, the library will display pixels in the wrong LEDs. To ensure this constraint, you have two options:
Via assertions, for example:
Via delegation (preferred), for example:
Note: LEDMatrix does not own instances of PixelMatrix.