4#ifdef USE_LIGHT_TRANSITION_PUBLISH_INTERVAL
16static const char *
const TAG =
"light";
24 auto wanted =
static_cast<uint8_t
>(requested);
26 if ((
static_cast<uint8_t
>(
mode) & wanted) == wanted)
42 for (
auto *effect : this->
effects_) {
43 effect->init_internal(
this);
51 float min_mireds = traits.get_min_mireds();
59 bool restored =
false;
62 restored = this->
rtc_.
load(&recovered);
73 if (recovered.state && recovered.brightness == 0.0f) {
74 recovered.brightness = 1.0f;
77 call.set_color_mode_if_supported(resolve_color_mode(traits, recovered.color_mode));
78 call.set_state(recovered.state);
79 call.set_brightness_if_supported(recovered.brightness);
80 call.set_color_brightness_if_supported(recovered.color_brightness);
81 call.set_red_if_supported(recovered.red);
82 call.set_green_if_supported(recovered.green);
83 call.set_blue_if_supported(recovered.blue);
84 call.set_white_if_supported(recovered.white);
85 call.set_color_temperature_if_supported(recovered.color_temp);
86 call.set_cold_white_if_supported(recovered.cold_white);
87 call.set_warm_white_if_supported(recovered.warm_white);
88 if (recovered.effect != 0) {
89 call.set_effect(recovered.effect);
91 call.set_transition_length_if_supported(0);
96 ESP_LOGCONFIG(TAG,
"Light '%s'", this->
get_name().c_str());
99#ifdef USE_LIGHT_GAMMA_LUT
101 const unsigned gamma_x100 =
104 const unsigned gamma_x100 = 0;
107 " Default Transition Length: %.1fs\n"
108 " Gamma Correct: %u.%02u",
110#ifdef USE_LIGHT_TRANSITION_PUBLISH_INTERVAL
113 ESP_LOGCONFIG(TAG,
" Transition State Publish Interval: %" PRIu32
"ms",
120 " Min Mireds: %.1f\n"
122 traits.get_min_mireds(), traits.get_max_mireds());
128 if (effect !=
nullptr) {
136 if (values.has_value()) {
142 const bool finished = this->
transformer_->is_finished();
143#ifdef USE_LIGHT_TRANSITION_PUBLISH_INTERVAL
159#ifdef USE_LIGHT_TRANSITION_PUBLISH_INTERVAL
168 listener->on_light_target_state_reached();
189 listener->on_light_remote_values_update();
192#if defined(USE_LIGHT) && defined(USE_CONTROLLER_REGISTRY)
193 ControllerRegistry::notify_light_update(
this);
205 return EFFECT_NONE_REF;
244 bool constant_brightness) {
252 float *white_brightness) {
254 this->
current_values.
as_rgbct(traits.get_min_mireds(), traits.get_max_mireds(), red, green, blue, color_temperature,
262 if (!constant_brightness) {
291 *cold_white = *warm_white = 0;
297 const float white_level = this->
gamma_correct_lut(v.get_state() * v.get_brightness());
298 const float sum = cw_level > 0 || ww_level > 0 ? cw_level + ww_level : 1;
299 *cold_white = white_level * std::max(cw_level, ww_level) * cw_level / sum;
300 *warm_white = white_level * std::max(cw_level, ww_level) * ww_level / sum;
304 this->
current_values.
as_ct(traits.get_min_mireds(), traits.get_max_mireds(), color_temperature, white_brightness);
309#ifdef USE_LIGHT_GAMMA_LUT
316#ifdef USE_LIGHT_GAMMA_LUT
324 float scaled = value * 255.0f;
325 auto idx =
static_cast<uint8_t
>(scaled);
328 float frac = scaled - idx;
331 return (a + frac * (
b - a)) / 65535.0f;
340 uint16_t target =
static_cast<uint16_t
>(value * 65535.0f);
349 float frac =
static_cast<float>(target - a) /
static_cast<float>(
b - a);
350 return (lo + frac) / 255.0f;
356 if (effect_index > std::numeric_limits<uint16_t>::max())
359 if (effect_index == 0)
364 effect->start_internal();
377 if (effect !=
nullptr) {
400 this->
transformer_ = make_unique<LightFlashTransformer>(*
this);
410#ifdef USE_LIGHT_TRANSITION_PUBLISH_INTERVAL
414 if (set_remote_values) {
428#ifdef USE_LIGHT_TRANSITION_PUBLISH_INTERVAL
434 if (set_remote_values) {
BedjetMode mode
BedJet operating mode.
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void enable_loop()
Enable this component's loop.
void disable_loop()
Disable this component's loop.
const StringRef & get_name() const
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
StringRef is a reference to a string owned by something else.
static constexpr StringRef from_lit(const CharT(&s)[N])
This class represents a requested change in a light state.
LightCall & set_state(optional< bool > state)
Set the binary ON/OFF state of the light.
This class represents the color state for a light object.
float get_brightness() const
Get the brightness property of these light color values. In range 0.0 to 1.0.
float get_blue() const
Get the blue property of these light color values. In range 0.0 to 1.0.
float get_white() const
Get the white property of these light color values. In range 0.0 to 1.0.
float get_color_temperature() const
Get the color temperature property of these light color values in mired.
void as_cwww(float *cold_white, float *warm_white, bool constant_brightness=false) const
Convert these light color values to an CWWW representation with the given parameters.
float get_cold_white() const
Get the cold white property of these light color values. In range 0.0 to 1.0.
void as_rgbw(float *red, float *green, float *blue, float *white) const
Convert these light color values to an RGBW representation and write them to red, green,...
void as_rgb(float *red, float *green, float *blue) const
Convert these light color values to an RGB representation and write them to red, green,...
bool is_on() const
Get the binary true/false state of these light color values.
float get_green() const
Get the green property of these light color values. In range 0.0 to 1.0.
void set_color_temperature(float color_temperature)
Set the color temperature property of these light color values in mired.
float get_warm_white() const
Get the warm white property of these light color values. In range 0.0 to 1.0.
void as_ct(float color_temperature_cw, float color_temperature_ww, float *color_temperature, float *white_brightness) const
Convert these light color values to a CT+BR representation with the given parameters.
ColorMode get_color_mode() const
Get the color mode of these light color values.
void as_rgbct(float color_temperature_cw, float color_temperature_ww, float *red, float *green, float *blue, float *color_temperature, float *white_brightness) const
Convert these light color values to an RGB+CT+BR representation with the given parameters.
float get_red() const
Get the red property of these light color values. In range 0.0 to 1.0.
void as_brightness(float *brightness) const
Convert these light color values to a brightness-only representation and write them to brightness.
float get_color_brightness() const
Get the color brightness property of these light color values. In range 0.0 to 1.0.
Interface to write LightStates to hardware.
virtual void write_state(LightState *state)=0
Called from loop() every time the light state has changed, and should should write the new state to h...
virtual std::unique_ptr< LightTransformer > create_default_transition()
Return the default transformer used for transitions.
virtual LightTraits get_traits()=0
Return the LightTraits of this LightOutput.
virtual void update_state(LightState *state)
Called on every update of the current values of the associated LightState, can optionally be used to ...
virtual void setup_state(LightState *state)
Listener interface for light remote value changes.
void add_remote_values_listener(LightRemoteValuesListener *listener)
Add a listener for remote values changes.
FixedVector< LightEffect * > effects_
List of effects for this light.
StringRef get_effect_name()
Return the name of the current effect, or if no effect is active "None".
void start_effect_(uint32_t effect_index)
Internal method to start an effect with the given index.
void current_values_as_rgb(float *red, float *green, float *blue)
float gamma_correct_lut(float value) const
Apply gamma correction using the pre-computed forward LUT.
void stop_effect_()
Internal method to stop the current effect (if one is active).
void current_values_as_rgbct(float *red, float *green, float *blue, float *color_temperature, float *white_brightness)
void disable_loop_if_idle_()
Disable loop if neither transformer nor effect is active.
std::unique_ptr< LightTransformer > transformer_
The currently active transformer for this light (transition/flash).
void dump_config() override
bool transition_publish_enabled_
True while the active transformer publishes current_values on an interval from loop().
LightColorValues remote_values
The remote color values reported to the frontend.
LightOutput * get_output() const
Get the light output associated with this object.
void(* state_callback_)(LightStateRTCState &, bool restored)
Callback that resolves the boot-time state — called once during setup, then cleared.
LightState(LightOutput *output)
void save_remote_values_()
Internal method to save the current remote_values to the preferences.
void add_target_state_reached_listener(LightTargetStateReachedListener *listener)
Add a listener for target state reached.
LightCall turn_on()
Make a light state call.
void set_immediately_(const LightColorValues &target, bool set_remote_values)
Internal method to set the color values to target immediately (with no transition).
float get_gamma_correct() const
The gamma correction factor, read from the entry after the gamma lookup table; 0 without one.
void current_values_as_ct(float *color_temperature, float *white_brightness)
float gamma_uncorrect_lut(float value) const
Reverse gamma correction by binary-searching the forward LUT.
void current_values_as_cwww(float *cold_white, float *warm_white, bool constant_brightness=false)
void publish_state()
Publish the currently active state to the frontend.
void current_values_as_rgbww(float *red, float *green, float *blue, float *cold_white, float *warm_white, bool constant_brightness=false)
void current_values_as_brightness(float *brightness)
void setup() override
Load state from preferences.
const GammaTable * gamma_table_
void start_flash_(const LightColorValues &target, uint32_t length, bool set_remote_values)
Internal method to start a flash for the specified amount of time.
uint32_t transition_state_publish_interval_
void add_effects(const std::initializer_list< LightEffect * > &effects)
Add effects for this light state.
void schedule_write_()
Schedule a write to the light output and enable the loop to process it.
bool next_write_
Whether the light value should be written in the next cycle.
void start_transition_(const LightColorValues &target, uint32_t length, bool set_remote_values)
Internal method to start a transition to the target color with the given length.
bool save_enabled_
Whether this light persists its state to preferences at all.
LightOutput * output_
Store the output to allow effects to have more access.
ESPPreferenceObject rtc_
Object used to store the persisted values of the light.
uint16_t active_effect_index_
1-based index of the active effect, 0 if none; codegen caps effects at MAX_EFFECTS in effects....
void current_values_as_rgbw(float *red, float *green, float *blue, float *white)
uint32_t default_transition_length_
Default transition length for all transitions in ms.
LightColorValues current_values
The current values of the light as outputted to the light.
LightEffect * get_active_effect_()
Internal method to get the currently active effect.
bool is_transformer_active_
std::unique_ptr< std::vector< LightRemoteValuesListener * > > remote_values_listeners_
Listeners for remote values changes.
std::unique_ptr< std::vector< LightTargetStateReachedListener * > > target_state_reached_listeners_
Listeners for target state reached.
uint32_t last_transition_state_publish_
void set_transformer_remote_values_(const LightColorValues &target, bool set_remote_values)
Point remote_values at the new transformer's target and, when this light publishes intermediate state...
Listener interface for light target state reached.
This class is used to represent the capabilities of a light.
uint8_t gamma_table_reverse_search(const uint16_t *table, uint16_t target)
Binary search a monotonically increasing uint16[256] PROGMEM table.
ColorMode
Color modes are a combination of color capabilities that can be used at the same time.
@ UNKNOWN
No color mode configured (cannot be a supported mode, only active when light is off).
@ BRIGHTNESS
Master brightness of the light can be controlled.
@ COLOR_TEMPERATURE
Color temperature can be controlled.
@ COLD_WARM_WHITE
Brightness of cold and warm white output can be controlled.
uint16_t progmem_read_uint16(const uint16_t *addr)
Application App
Global storage of Application pointer - only one Application can exist.