ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
neopixelbus_light.h
Go to the documentation of this file.
1#pragma once
2
3#if defined(USE_ARDUINO) && !defined(CLANG_TIDY)
4
11
12#include "NeoPixelBus.h"
13
15
16enum class ESPNeoPixelOrder {
17 GBWR = 0b11000110,
18 GBRW = 0b10000111,
19 GBR = 0b10000111,
20 GWBR = 0b11001001,
21 GRBW = 0b01001011,
22 GRB = 0b01001011,
23 GWRB = 0b10001101,
24 GRWB = 0b01001110,
25 BGWR = 0b11010010,
26 BGRW = 0b10010011,
27 BGR = 0b10010011,
28 WGBR = 0b11011000,
29 RGBW = 0b00011011,
30 RGB = 0b00011011,
31 WGRB = 0b10011100,
32 RGWB = 0b00011110,
33 BWGR = 0b11100001,
34 BRGW = 0b01100011,
35 BRG = 0b01100011,
36 WBGR = 0b11100100,
37 RBGW = 0b00100111,
38 RBG = 0b00100111,
39 WRGB = 0b01101100,
40 RWGB = 0b00101101,
41 BWRG = 0b10110001,
42 BRWG = 0b01110010,
43 WBRG = 0b10110100,
44 RBWG = 0b00110110,
45 WRBG = 0b01111000,
46 RWBG = 0b00111001,
47};
48
49template<typename T_METHOD, typename T_COLOR_FEATURE, bool has_white>
51 public:
52 NeoPixelBus<T_COLOR_FEATURE, T_METHOD> *get_controller() const { return this->controller_; }
53
54 void clear_effect_data() override { this->internal_accessor_.clear_effect_data(this->size()); }
55
57 void add_leds(uint16_t count_pixels, uint8_t pin) {
58 this->add_leds(new NeoPixelBus<T_COLOR_FEATURE, T_METHOD>(count_pixels, pin));
59 }
60 void add_leds(uint16_t count_pixels, uint8_t pin_clock, uint8_t pin_data) {
61 this->add_leds(new NeoPixelBus<T_COLOR_FEATURE, T_METHOD>(count_pixels, pin_clock, pin_data));
62 }
63 void add_leds(uint16_t count_pixels) { this->add_leds(new NeoPixelBus<T_COLOR_FEATURE, T_METHOD>(count_pixels)); }
64 void add_leds(NeoPixelBus<T_COLOR_FEATURE, T_METHOD> *controller) {
65 this->controller_ = controller;
66 // controller gets initialised in setup() - avoid calling twice (crashes with RMT)
67 // this->controller_->Begin();
68 }
69
70 // ========== INTERNAL METHODS ==========
71 void setup() override {
72 this->internal_accessor_.effect_data = new uint8_t[this->size()]; // NOLINT
73 this->controller_->Begin();
74 this->internal_accessor_.color_data = this->controller_->Pixels();
75 this->accessor_ = &this->internal_accessor_;
76 }
77
79 this->mark_shown_();
80 this->controller_->Dirty();
81 this->controller_->Show();
82 this->internal_accessor_.color_data = this->controller_->Pixels();
83 }
84
85 float get_setup_priority() const override { return setup_priority::HARDWARE; }
86
87 int32_t size() const override { return this->controller_->PixelCount(); }
88
90 uint8_t u_order = static_cast<uint8_t>(order);
91 this->internal_accessor_.channel_colors.r = (u_order >> 6) & 0b11;
92 this->internal_accessor_.channel_colors.g = (u_order >> 4) & 0b11;
93 this->internal_accessor_.channel_colors.b = (u_order >> 2) & 0b11;
94 this->internal_accessor_.channel_colors.w = has_white ? (u_order >> 0) & 0b11 : light::ChannelColors::NO_WHITE;
95 }
96
97 protected:
100 NeoPixelBus<T_COLOR_FEATURE, T_METHOD> *controller_{nullptr};
101};
102
103template<typename T_METHOD, typename T_COLOR_FEATURE = NeoRgbFeature>
104class NeoPixelRGBLightOutput : public NeoPixelBusLightOutputBase<T_METHOD, T_COLOR_FEATURE, false> {
105 public:
107 auto traits = light::LightTraits();
108 traits.set_supported_color_modes({light::ColorMode::RGB});
109 return traits;
110 }
111};
112
113template<typename T_METHOD, typename T_COLOR_FEATURE = NeoRgbwFeature>
114class NeoPixelRGBWLightOutput : public NeoPixelBusLightOutputBase<T_METHOD, T_COLOR_FEATURE, true> {
115 public:
117 auto traits = light::LightTraits();
118 traits.set_supported_color_modes({light::ColorMode::RGB_WHITE});
119 return traits;
120 }
121};
122
123} // namespace esphome::neopixelbus
124
125#endif // USE_ARDUINO
void clear_effect_data(size_t num_leds)
Zero the effect data of the first num_leds LEDs; does nothing before the effect data is allocated.
Color accessor backed by a buffer whose contents have color correction applied.
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:68
This class is used to represent the capabilities of a light.
Definition light_traits.h:9
light::CorrectedBufferColorAccessor internal_accessor_
void add_leds(NeoPixelBus< T_COLOR_FEATURE, T_METHOD > *controller)
NeoPixelBus< T_COLOR_FEATURE, T_METHOD > * controller_
NeoPixelBus< T_COLOR_FEATURE, T_METHOD > * get_controller() const
void add_leds(uint16_t count_pixels, uint8_t pin_clock, uint8_t pin_data)
void write_state(light::LightState *state) override
void add_leds(uint16_t count_pixels, uint8_t pin)
Add some LEDS, can only be called once.
bool state
Definition fan.h:2
@ RGB_WHITE
RGB color output and a separate white output.
@ RGB
RGB color output.
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:43
static constexpr uint8_t NO_WHITE
Value of w for an RGB LED strip that uses 3 bytes per led.