Files
led-driver/main/ws2812.h
T
jimmyandClaude Haiku 5.5 9a04d28ecf feat: web UI with AP, Wi-Fi client, mDNS, presets, OTA and DDP
- Wi-Fi access point led-XXXXXX plus a client network, configurable
  mDNS name and static client address
- Preset tiles with an editor, drag to reorder, default presets,
  global brightness, Run and Edit modes, collapsible menu on phones
- 35 patterns ported from led-driver, up to 512 LEDs, colour order,
  max brightness and data pin settings
- Over-the-air updates (POST /ota) with rollback, and a CLI in tools/ota.py
- DDP receiver on UDP 4048 with tools/ddp_test.py
- LED refresh every 10 ms; lwIP sockets raised to 16 so mDNS, HTTP
  and DDP can run together

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
2026-11-11 01:54:32 +13:00

83 lines
2.8 KiB
C++

/*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <cstddef>
#include <cstdint>
#include <vector>
#include "driver/gpio.h"
#include "driver/rmt_encoder.h"
#include "driver/rmt_tx.h"
#include "esp_err.h"
/**
* @brief WS2812-style addressable LED strip driven through the RMT peripheral.
*
* Pixel data is kept in a local buffer. set() and fill() only update that buffer;
* call write() to push the buffer out to the strip.
*/
class WS2812 {
public:
/**
* @param gpio GPIO connected to the strip's data input (DI)
* @param num_leds Number of LEDs in the strip
* @param resolution_hz RMT tick resolution; 10 MHz gives 0.1 us ticks, which WS2812 needs
*/
WS2812(gpio_num_t gpio, size_t num_leds, uint32_t resolution_hz = 10000000);
~WS2812();
WS2812(const WS2812 &) = delete;
WS2812 &operator=(const WS2812 &) = delete;
/** @brief Set one pixel in the buffer. Out-of-range indices are ignored. */
void set(size_t index, uint8_t r, uint8_t g, uint8_t b);
/** @brief Set every pixel in the buffer to the same colour. */
void fill(uint8_t r, uint8_t g, uint8_t b);
/**
* @brief Transmit the buffer to the strip and wait until the transfer finishes.
* @return ESP_OK on success, or an error from the RMT driver.
*/
esp_err_t write();
/** @brief Number of LEDs in the strip. */
size_t size() const { return num_leds_; }
/**
* @brief Set the output brightness, 0 (off) to 255 (full). Applied in write();
* the pixel buffer keeps its full-colour values. Defaults to 255.
*/
void setBrightness(uint8_t level) { brightness_ = level; }
/** @brief Current output brightness, 0-255. */
uint8_t brightness() const { return brightness_; }
/**
* @brief Convert HSV to RGB.
*
* h is the hue in degrees (wrapped to [0, 360)); s and v are 0-100 percent.
* Wiki: https://en.wikipedia.org/wiki/HSL_and_HSV
*/
static void hsv2rgb(uint32_t h, uint32_t s, uint32_t v, uint32_t *r, uint32_t *g, uint32_t *b);
/**
* @brief Byte order of the strip, as the letters of the colours in the order they are sent.
* "grb" (the default) is the WS2812 norm; "rgb", "rbg", "gbr", "brg" and "bgr" cover the rest.
* Unknown names keep the current order.
*/
void setOrder(const char *order);
private:
size_t num_leds_;
uint8_t order_[3] = { 1, 0, 2 }; // indices into (r, g, b) for each byte sent: GRB
std::vector<uint8_t> pixels_; // 3 bytes per LED, in the strip's byte order
std::vector<uint8_t> tx_pixels_; // pixels_ scaled by brightness_, sent by write()
uint8_t brightness_ = 255;
rmt_channel_handle_t channel_ = nullptr;
rmt_encoder_handle_t encoder_ = nullptr;
rmt_transmit_config_t tx_config_ = {};
};