ESPHome 2026.6.0-dev
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>
51 public:
52 NeoPixelBus<T_COLOR_FEATURE, T_METHOD> *get_controller() const { return this->controller_; }
53
54 void clear_effect_data() override {
55 for (int i = 0; i < this->size(); i++)
56 this->effect_data_[i] = 0;
57 }
58
60 void add_leds(uint16_t count_pixels, uint8_t pin) {
61 this->add_leds(new NeoPixelBus<T_COLOR_FEATURE, T_METHOD>(count_pixels, pin));
62 }
63 void add_leds(uint16_t count_pixels, uint8_t pin_clock, uint8_t pin_data) {
64 this->add_leds(new NeoPixelBus<T_COLOR_FEATURE, T_METHOD>(count_pixels, pin_clock, pin_data));
65 }
66 void add_leds(uint16_t count_pixels) { this->add_leds(new NeoPixelBus<T_COLOR_FEATURE, T_METHOD>(count_pixels)); }
67 void add_leds(NeoPixelBus<T_COLOR_FEATURE, T_METHOD> *controller) {
68 this->controller_ = controller;
69 // controller gets initialised in setup() - avoid calling twice (crashes with RMT)
70 // this->controller_->Begin();
71 }
72
73 // ========== INTERNAL METHODS ==========
74 void setup() override {
75 for (int i = 0; i < this->size(); i++) {
76 (*this)[i] = Color(0, 0, 0, 0);
77 }
78
79 this->effect_data_ = new uint8_t[this->size()]; // NOLINT
80 this->controller_->Begin();
81 }
82
84 this->mark_shown_();
85 this->controller_->Dirty();
86
87 this->controller_->Show();
88 }
89
90 float get_setup_priority() const override { return setup_priority::HARDWARE; }
91
92 int32_t size() const override { return this->controller_->PixelCount(); }
93
95 uint8_t u_order = static_cast<uint8_t>(order);
96 this->rgb_offsets_[0] = (u_order >> 6) & 0b11;
97 this->rgb_offsets_[1] = (u_order >> 4) & 0b11;
98 this->rgb_offsets_[2] = (u_order >> 2) & 0b11;
99 this->rgb_offsets_[3] = (u_order >> 0) & 0b11;
100 }
101
102 protected:
103 NeoPixelBus<T_COLOR_FEATURE, T_METHOD> *controller_{nullptr};
104 uint8_t *effect_data_{nullptr};
105 uint8_t rgb_offsets_[4]{0, 1, 2, 3};
106};
107
108template<typename T_METHOD, typename T_COLOR_FEATURE = NeoRgbFeature>
109class NeoPixelRGBLightOutput : public NeoPixelBusLightOutputBase<T_METHOD, T_COLOR_FEATURE> {
110 public:
112 auto traits = light::LightTraits();
113 traits.set_supported_color_modes({light::ColorMode::RGB});
114 return traits;
115 }
116
117 protected:
118 light::ESPColorView get_view_internal(int32_t index) const override { // NOLINT
119 uint8_t *base = this->controller_->Pixels() + 3ULL * index;
120 return light::ESPColorView(base + this->rgb_offsets_[0], base + this->rgb_offsets_[1], base + this->rgb_offsets_[2],
121 nullptr, this->effect_data_ + index, &this->correction_);
122 }
123};
124
125template<typename T_METHOD, typename T_COLOR_FEATURE = NeoRgbwFeature>
126class NeoPixelRGBWLightOutput : public NeoPixelBusLightOutputBase<T_METHOD, T_COLOR_FEATURE> {
127 public:
129 auto traits = light::LightTraits();
130 traits.set_supported_color_modes({light::ColorMode::RGB_WHITE});
131 return traits;
132 }
133
134 protected:
135 light::ESPColorView get_view_internal(int32_t index) const override { // NOLINT
136 uint8_t *base = this->controller_->Pixels() + 4ULL * index;
137 return light::ESPColorView(base + this->rgb_offsets_[0], base + this->rgb_offsets_[1], base + this->rgb_offsets_[2],
138 base + this->rgb_offsets_[3], this->effect_data_ + index, &this->correction_);
139 }
140};
141
142} // namespace esphome::neopixelbus
143
144#endif // USE_ARDUINO
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:93
This class is used to represent the capabilities of a light.
Definition light_traits.h:9
void add_leds(uint16_t count_pixels, uint8_t pin)
Add some LEDS, can only be called once.
NeoPixelBus< T_COLOR_FEATURE, T_METHOD > * get_controller() const
NeoPixelBus< T_COLOR_FEATURE, T_METHOD > * controller_
void write_state(light::LightState *state) override
void add_leds(NeoPixelBus< T_COLOR_FEATURE, T_METHOD > *controller)
void add_leds(uint16_t count_pixels, uint8_t pin_clock, uint8_t pin_data)
light::ESPColorView get_view_internal(int32_t index) const override
light::ESPColorView get_view_internal(int32_t index) const override
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:41