ESPHome  2024.4.1
neopixelbus_light.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ARDUINO
4 
5 #include "esphome/core/macros.h"
7 #include "esphome/core/helpers.h"
8 #include "esphome/core/color.h"
11 
12 #include "NeoPixelBus.h"
13 
14 namespace esphome {
15 namespace neopixelbus {
16 
17 enum class ESPNeoPixelOrder {
18  GBWR = 0b11000110,
19  GBRW = 0b10000111,
20  GBR = 0b10000111,
21  GWBR = 0b11001001,
22  GRBW = 0b01001011,
23  GRB = 0b01001011,
24  GWRB = 0b10001101,
25  GRWB = 0b01001110,
26  BGWR = 0b11010010,
27  BGRW = 0b10010011,
28  BGR = 0b10010011,
29  WGBR = 0b11011000,
30  RGBW = 0b00011011,
31  RGB = 0b00011011,
32  WGRB = 0b10011100,
33  RGWB = 0b00011110,
34  BWGR = 0b11100001,
35  BRGW = 0b01100011,
36  BRG = 0b01100011,
37  WBGR = 0b11100100,
38  RBGW = 0b00100111,
39  RBG = 0b00100111,
40  WRGB = 0b01101100,
41  RWGB = 0b00101101,
42  BWRG = 0b10110001,
43  BRWG = 0b01110010,
44  WBRG = 0b10110100,
45  RBWG = 0b00110110,
46  WRBG = 0b01111000,
47  RWBG = 0b00111001,
48 };
49 
50 template<typename T_METHOD, typename T_COLOR_FEATURE>
52  public:
53  NeoPixelBus<T_COLOR_FEATURE, T_METHOD> *get_controller() const { return this->controller_; }
54 
55  void clear_effect_data() override {
56  for (int i = 0; i < this->size(); i++)
57  this->effect_data_[i] = 0;
58  }
59 
61  void add_leds(uint16_t count_pixels, uint8_t pin) {
62  this->add_leds(new NeoPixelBus<T_COLOR_FEATURE, T_METHOD>(count_pixels, pin));
63  }
64  void add_leds(uint16_t count_pixels, uint8_t pin_clock, uint8_t pin_data) {
65  this->add_leds(new NeoPixelBus<T_COLOR_FEATURE, T_METHOD>(count_pixels, pin_clock, pin_data));
66  }
67  void add_leds(uint16_t count_pixels) { this->add_leds(new NeoPixelBus<T_COLOR_FEATURE, T_METHOD>(count_pixels)); }
68  void add_leds(NeoPixelBus<T_COLOR_FEATURE, T_METHOD> *controller) {
69  this->controller_ = controller;
70  // controller gets initialised in setup() - avoid calling twice (crashes with RMT)
71  // this->controller_->Begin();
72  }
73 
74  // ========== INTERNAL METHODS ==========
75  void setup() override {
76  for (int i = 0; i < this->size(); i++) {
77  (*this)[i] = Color(0, 0, 0, 0);
78  }
79 
80  this->effect_data_ = new uint8_t[this->size()]; // NOLINT
81  this->controller_->Begin();
82  }
83 
85  this->mark_shown_();
86  this->controller_->Dirty();
87 
88  this->controller_->Show();
89  }
90 
91  float get_setup_priority() const override { return setup_priority::HARDWARE; }
92 
93  int32_t size() const override { return this->controller_->PixelCount(); }
94 
96  uint8_t u_order = static_cast<uint8_t>(order);
97  this->rgb_offsets_[0] = (u_order >> 6) & 0b11;
98  this->rgb_offsets_[1] = (u_order >> 4) & 0b11;
99  this->rgb_offsets_[2] = (u_order >> 2) & 0b11;
100  this->rgb_offsets_[3] = (u_order >> 0) & 0b11;
101  }
102 
103  protected:
104  NeoPixelBus<T_COLOR_FEATURE, T_METHOD> *controller_{nullptr};
105  uint8_t *effect_data_{nullptr};
106  uint8_t rgb_offsets_[4]{0, 1, 2, 3};
107 };
108 
109 template<typename T_METHOD, typename T_COLOR_FEATURE = NeoRgbFeature>
110 class NeoPixelRGBLightOutput : public NeoPixelBusLightOutputBase<T_METHOD, T_COLOR_FEATURE> {
111  public:
113  auto traits = light::LightTraits();
114  traits.set_supported_color_modes({light::ColorMode::RGB});
115  return traits;
116  }
117 
118  protected:
119  light::ESPColorView get_view_internal(int32_t index) const override { // NOLINT
120  uint8_t *base = this->controller_->Pixels() + 3ULL * index;
121  return light::ESPColorView(base + this->rgb_offsets_[0], base + this->rgb_offsets_[1], base + this->rgb_offsets_[2],
122  nullptr, this->effect_data_ + index, &this->correction_);
123  }
124 };
125 
126 template<typename T_METHOD, typename T_COLOR_FEATURE = NeoRgbwFeature>
127 class NeoPixelRGBWLightOutput : public NeoPixelBusLightOutputBase<T_METHOD, T_COLOR_FEATURE> {
128  public:
130  auto traits = light::LightTraits();
131  traits.set_supported_color_modes({light::ColorMode::RGB_WHITE});
132  return traits;
133  }
134 
135  protected:
136  light::ESPColorView get_view_internal(int32_t index) const override { // NOLINT
137  uint8_t *base = this->controller_->Pixels() + 4ULL * index;
138  return light::ESPColorView(base + this->rgb_offsets_[0], base + this->rgb_offsets_[1], base + this->rgb_offsets_[2],
139  base + this->rgb_offsets_[3], this->effect_data_ + index, &this->correction_);
140  }
141 };
142 
143 } // namespace neopixelbus
144 } // namespace esphome
145 
146 #endif // USE_ARDUINO
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:34
NeoPixelBus< T_COLOR_FEATURE, T_METHOD > * get_controller() const
RGB color output and a separate white output.
light::ESPColorView get_view_internal(int32_t index) const override
void add_leds(NeoPixelBus< T_COLOR_FEATURE, T_METHOD > *controller)
void write_state(light::LightState *state) override
light::ESPColorView get_view_internal(int32_t index) const override
void add_leds(uint16_t count_pixels, uint8_t pin)
Add some LEDS, can only be called once.
This class is used to represent the capabilities of a light.
Definition: light_traits.h:11
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
Color can be controlled using RGB format (includes a brightness control for the color).
void add_leds(uint16_t count_pixels, uint8_t pin_clock, uint8_t pin_data)
bool state
Definition: fan.h:34