ESPHome  2024.11.1
addressable_light_wrapper.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "addressable_light.h"
5 
6 namespace esphome {
7 namespace light {
8 
10  public:
11  explicit AddressableLightWrapper(light::LightState *light_state) : light_state_(light_state) {
12  this->wrapper_state_ = new uint8_t[5]; // NOLINT(cppcoreguidelines-owning-memory)
13  }
14 
15  int32_t size() const override { return 1; }
16 
17  void clear_effect_data() override { this->wrapper_state_[4] = 0; }
18 
20  LightTraits traits;
21 
22  // Choose which color mode to use.
23  // This is ordered by how closely each color mode matches the underlying RGBW data structure used in LightPartition.
24  ColorMode color_mode_precedence[] = {ColorMode::RGB_WHITE,
34 
35  LightTraits parent_traits = this->light_state_->get_traits();
36  for (auto cm : color_mode_precedence) {
37  if (parent_traits.supports_color_mode(cm)) {
38  this->color_mode_ = cm;
39  break;
40  }
41  }
42 
43  // Report a color mode that's compatible with both the partition and the underlying light
44  switch (this->color_mode_) {
49  break;
50 
51  case ColorMode::RGB:
53  break;
54 
55  case ColorMode::WHITE:
60  break;
61 
62  case ColorMode::ON_OFF:
64  break;
65 
66  default:
68  }
69 
70  return traits;
71  }
72 
74  // Don't overwrite state if the underlying light is turned on
75  if (this->light_state_->remote_values.is_on()) {
76  this->mark_shown_();
77  return;
78  }
79 
80  float gamma = this->light_state_->get_gamma_correct();
81  float r = gamma_uncorrect(this->wrapper_state_[0] / 255.0f, gamma);
82  float g = gamma_uncorrect(this->wrapper_state_[1] / 255.0f, gamma);
83  float b = gamma_uncorrect(this->wrapper_state_[2] / 255.0f, gamma);
84  float w = gamma_uncorrect(this->wrapper_state_[3] / 255.0f, gamma);
85 
86  auto call = this->light_state_->make_call();
87 
88  float color_brightness = fmaxf(r, fmaxf(g, b));
89  float brightness = fmaxf(color_brightness, w);
90  if (brightness == 0.0f) {
91  call.set_state(false);
92  } else {
93  color_brightness /= brightness;
94  w /= brightness;
95 
96  call.set_state(true);
97  call.set_color_mode_if_supported(this->color_mode_);
98  call.set_brightness_if_supported(brightness);
99  call.set_color_brightness_if_supported(color_brightness);
100  call.set_red_if_supported(r);
101  call.set_green_if_supported(g);
102  call.set_blue_if_supported(b);
103  call.set_white_if_supported(w);
104  call.set_warm_white_if_supported(w);
105  call.set_cold_white_if_supported(w);
106  }
107  call.set_transition_length_if_supported(0);
108  call.set_publish(false);
109  call.set_save(false);
110  call.perform();
111 
112  this->mark_shown_();
113  }
114 
115  protected:
116  light::ESPColorView get_view_internal(int32_t index) const override {
117  return {&this->wrapper_state_[0], &this->wrapper_state_[1], &this->wrapper_state_[2],
118  &this->wrapper_state_[3], &this->wrapper_state_[4], &this->correction_};
119  }
120 
122  uint8_t *wrapper_state_;
124 };
125 
126 } // namespace light
127 } // namespace esphome
ColorMode
Color modes are a combination of color capabilities that can be used at the same time.
Definition: color_mode.h:49
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:63
bool is_on() const
Get the binary true/false state of these light color values.
light::ESPColorView get_view_internal(int32_t index) const override
float get_gamma_correct() const
Definition: light_state.h:143
RGB color output and a separate white output.
Color temperature can be controlled.
RGB color output, and separate cold and warm white outputs.
AddressableLightWrapper(light::LightState *light_state)
Brightness of cold and warm white output can be controlled.
Light can be turned on/off.
RGB color output and a separate white output with controllable color temperature. ...
Master brightness of the light can be controlled.
White output only (use only if the light also has another color mode such as RGB).
No color mode configured (cannot be a supported mode, only active when light is off).
This class is used to represent the capabilities of a light.
Definition: light_traits.h:11
bool supports_color_mode(ColorMode color_mode) const
Definition: light_traits.h:20
void write_state(light::LightState *state) override
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Color can be controlled using RGB format (includes a brightness control for the color).
LightColorValues remote_values
The remote color values reported to the frontend.
Definition: light_state.h:106
void set_supported_color_modes(std::set< ColorMode > supported_color_modes)
Definition: light_traits.h:16
bool state
Definition: fan.h:34
float gamma_uncorrect(float value, float gamma)
Reverts gamma correction of gamma to value.
Definition: helpers.cpp:550