Neopixel APIs
The Neopixel component provides APIs to control Neopixels. It uses an RMT driver (via the Rmt component) to talk to WS2812, WS2811, WS2813, SK6812, etc. LED strips.
API Reference
Header File
Classes
-
class Neopixel
A class for controlling a Neopixel strip.
This class provides a simple interface for controlling a Neopixel strip. It uses the RMT peripheral to send the data to the strip.
Example 1: QtPy ESP32S3 Neopixel
espp::Neopixel led({ .data_gpio = 39, // Neopixel data pin on QtPy ESP32s3 .power_gpio = 38, // Neopixel power pin on QtPy ESP32s3 }); logger.info("Turning LED off for 1 second"); led.set_color(espp::Rgb(0.0f, 0.0f, 0.0f)); led.show(); std::this_thread::sleep_for(1s); logger.info("Setting LED to red for 1 second"); led.set_color(espp::Rgb(1.0f, 0.0f, 0.0f)); led.show(); std::this_thread::sleep_for(1s); logger.info("Setting LED to green for 1 second"); led.set_color(espp::Rgb(0.0f, 1.0f, 0.0f)); led.show(); std::this_thread::sleep_for(1s); logger.info("Setting LED to blue for 1 second"); led.set_color(espp::Rgb(0.0f, 0.0f, 1.0f)); led.show(); std::this_thread::sleep_for(1s); // Use a task to rotate the LED through the rainbow using HSV auto task_fn = [&led](std::mutex &m, std::condition_variable &cv) { static auto start = std::chrono::high_resolution_clock::now(); auto now = std::chrono::high_resolution_clock::now(); float t = std::chrono::duration<float>(now - start).count(); // rotate through rainbow colors in hsv based on time, hue is 0-360 float hue = (cos(t) * 0.5f + 0.5f) * 360.0f; espp::Hsv hsv(hue, 1.0f, 1.0f); // full brightness (1.0, default) is _really_ bright, so tone it down led.set_color(hsv); // show the new colors led.show(); // NOTE: sleeping in this way allows the sleep to exit early when the // task is being stopped / destroyed { std::unique_lock<std::mutex> lk(m); cv.wait_for(lk, 50ms); } // don't want to stop the task return false; }; logger.info("Starting task to rotate LED through rainbow colors"); auto task = espp::Task({.callback = task_fn, .task_config = { .name = "Neopixel Task", .stack_size_bytes = 5 * 1024, }, .log_level = espp::Logger::Verbosity::WARN}); task.start();
Public Functions
-
explicit Neopixel(const espp::Neopixel::Config &config)
Constructor for the Neopixel.
- Parameters
config – The configuration structure for the Neopixel.
-
inline std::size_t num_leds() const
Get the number of LEDs in the strip.
- Returns
The number of LEDs in the strip.
-
void set_color(const espp::Rgb &rgb, std::size_t index = 0)
Set the color of a single pixel.
- Parameters
rgb – The color to set the pixel to.
index – The index of the pixel to set.
-
inline void set_color(const espp::Hsv &hsv, std::size_t index = 0)
Set the color of a single pixel.
- Parameters
hsv – The color to set the pixel to.
index – The index of the pixel to set.
-
void set_all(const espp::Rgb &rgb)
Set the color of all pixels.
- Parameters
rgb – The color to set all pixels to.
-
inline void set_all(const espp::Hsv &hsv)
Set the color of all pixels.
- Parameters
hsv – The color to set all pixels to.
-
void show()
Show the current pixel data.
-
void set_power(bool on)
Set the power state of the strip.
If the power GPIO is set, this function will set the power state of the strip. If the power GPIO is not set, this function will do nothing.
- Parameters
on – The power state to set.
-
explicit Neopixel(const espp::Neopixel::Config &config)