WS2812 RMT Driver
API reference
Loading...
Searching...
No Matches
ws2812_cpp.hpp
Go to the documentation of this file.
1
7#pragma once
8#include "rmt_wrapper.hpp"
9#include "ws2812_control.h"
10#include "ws2812_version.h"
11#include <cstdint>
12#include <vector>
13
14#ifdef __cplusplus
15
17enum class LedType { RGB, RGBW };
18
27public:
34 explicit WS2812Strip(gpio_num_t gpio = (gpio_num_t)CONFIG_WS2812_LED_RMT_TX_GPIO,
35 int channel = CONFIG_WS2812_LED_RMT_TX_CHANNEL, uint32_t num_leds = NUM_LEDS,
36#if CONFIG_WS2812_LED_TYPE_RGBW
38#else
39 LedType type = LedType::RGB,
40#endif
41 uint16_t t0h = WS2812_T0H, uint16_t t1h = WS2812_T1H,
42 uint16_t t0l = WS2812_T0L, uint16_t t1l = WS2812_T1L,
43 uint8_t brightness = WS2812_DEFAULT_BRIGHTNESS);
44
54 esp_err_t Begin();
55
63 void SetPixel(uint32_t index, uint32_t rgbw);
64
66 uint32_t Length() const;
67
74 esp_err_t Show();
75
81 void SetBrightness(uint8_t value);
82
91 void SetNumLeds(uint32_t num_leds);
92
98 void SetTimings(uint16_t t0h, uint16_t t1h, uint16_t t0l, uint16_t t1l);
99
106 static uint32_t ColorWheel(uint8_t pos);
107
109 static constexpr uint32_t kResetTimeUs = 300;
110
111 // ===========================================================================
112 // Driver Version
113 // ===========================================================================
114
116 static constexpr const char* GetDriverVersion() noexcept {
117 return HF_WS2812_RMT_VERSION_STRING;
118 }
119
121 static constexpr uint8_t GetDriverVersionMajor() noexcept {
122 return HF_WS2812_RMT_VERSION_MAJOR;
123 }
124
126 static constexpr uint8_t GetDriverVersionMinor() noexcept {
127 return HF_WS2812_RMT_VERSION_MINOR;
128 }
129
131 static constexpr uint8_t GetDriverVersionPatch() noexcept {
132 return HF_WS2812_RMT_VERSION_PATCH;
133 }
134
135private:
136 static constexpr uint16_t kResetTicks = kResetTimeUs * 10; // 100 ns ticks
137
138 std::vector<uint32_t> pixels_;
139 std::vector<rmt_symbol_word_t> buffer_;
140 ws2812::RmtTx rmt_;
141 gpio_num_t gpio_;
142 int channel_;
143 LedType type_;
144 uint16_t t0h_;
145 uint16_t t1h_;
146 uint16_t t0l_;
147 uint16_t t1l_;
148 uint8_t brightness_;
149 uint32_t num_leds_;
150};
151
153inline const char* GetWS2812DriverVersion() noexcept {
154 return HF_WS2812_RMT_VERSION_STRING;
155}
156
157#endif // __cplusplus
C++ convenience wrapper for a chain of WS2812 LEDs.
Definition ws2812_cpp.hpp:26
void SetPixel(uint32_t index, uint32_t rgbw)
Set the colour of a single LED.
Definition ws2812_cpp.cpp:30
void SetTimings(uint16_t t0h, uint16_t t1h, uint16_t t0l, uint16_t t1l)
Update the bit timing parameters.
Definition ws2812_cpp.cpp:92
static constexpr uint32_t kResetTimeUs
Definition ws2812_cpp.hpp:109
uint32_t Length() const
Definition ws2812_cpp.cpp:36
esp_err_t Begin()
Report whether the strip's RMT channel is ready, retrying its set-up if it failed.
Definition ws2812_cpp.cpp:26
static constexpr uint8_t GetDriverVersionMinor() noexcept
Get the compiled driver minor version number.
Definition ws2812_cpp.hpp:126
esp_err_t Show()
Send the currently stored colours to the LED strip.
Definition ws2812_cpp.cpp:40
static constexpr const char * GetDriverVersion() noexcept
Get the compiled driver version string.
Definition ws2812_cpp.hpp:116
static uint32_t ColorWheel(uint8_t pos)
Generate a colour from a 0-255 position on a colour wheel.
Definition ws2812_cpp.cpp:99
static constexpr uint8_t GetDriverVersionMajor() noexcept
Get the compiled driver major version number.
Definition ws2812_cpp.hpp:121
void SetNumLeds(uint32_t num_leds)
Resize the strip to a new LED count at runtime.
Definition ws2812_cpp.cpp:86
static constexpr uint8_t GetDriverVersionPatch() noexcept
Get the compiled driver patch version number.
Definition ws2812_cpp.hpp:131
void SetBrightness(uint8_t value)
Set global brightness for the strip.
Definition ws2812_cpp.cpp:82
Definition rmt_wrapper.hpp:52
A modern C++17 header‑only wrapper around the ESP‑IDF v5.3 “new” RMT.
ESP-IDF RMT backend for WS2812 / NeoPixel LED chains.
#define WS2812_T1L
Definition ws2812_control.h:43
#define WS2812_T0L
Definition ws2812_control.h:42
#define NUM_LEDS
Definition ws2812_control.h:26
#define WS2812_T0H
Definition ws2812_control.h:38
#define WS2812_DEFAULT_BRIGHTNESS
Definition ws2812_control.h:46
#define WS2812_T1H
Definition ws2812_control.h:40
const char * GetWS2812DriverVersion() noexcept
Get the WS2812 driver version string (free function).
Definition ws2812_cpp.hpp:153
LedType
Definition ws2812_cpp.hpp:17