ESPHome 2026.6.3
Loading...
Searching...
No Matches
hbridge_light_output.h
Go to the documentation of this file.
1#pragma once
2
6
7namespace esphome::hbridge {
8
10 public:
11 void set_pina_pin(output::FloatOutput *pina_pin) { this->pina_pin_ = pina_pin; }
12 void set_pinb_pin(output::FloatOutput *pinb_pin) { this->pinb_pin_ = pinb_pin; }
13
15 auto traits = light::LightTraits();
16 traits.set_supported_color_modes({light::ColorMode::COLD_WARM_WHITE});
17 traits.set_min_mireds(153);
18 traits.set_max_mireds(500);
19 return traits;
20 }
21
22 void setup() override { this->stop_poller(); }
23
24 void update() override {
25 // Flip the H-bridge direction to multiplex cold/warm white. update_interval must stay
26 // slower than the output's PWM period (flipping faster collapses the output onto one
27 // channel) but fast enough to avoid flicker (issue #17030).
28 if (!this->forward_direction_) {
29 this->pina_pin_->set_level(this->pina_duty_);
30 this->pinb_pin_->set_level(0);
31 this->forward_direction_ = true;
32 } else {
33 this->pina_pin_->set_level(0);
34 this->pinb_pin_->set_level(this->pinb_duty_);
35 this->forward_direction_ = false;
36 }
37 }
38
39 float get_setup_priority() const override { return setup_priority::HARDWARE; }
40
42 float new_pina, new_pinb;
43 state->current_values_as_cwww(&new_pina, &new_pinb, false);
44
45 this->pina_duty_ = new_pina;
46 this->pinb_duty_ = new_pinb;
47
48 if (new_pina != 0.0f && new_pinb != 0.0f) {
49 // Both channels active — multiplex the H-bridge direction via the poller.
50 if (!this->multiplexing_) {
51 this->multiplexing_ = true;
52 this->start_poller();
53 }
54 } else {
55 // Zero or one channel active — drive pins directly, no multiplexing needed.
56 if (this->multiplexing_) {
57 this->multiplexing_ = false;
58 this->stop_poller();
59 }
60 this->pina_pin_->set_level(new_pina);
61 this->pinb_pin_->set_level(new_pinb);
62 }
63 }
64
65 protected:
68 float pina_duty_{0};
69 float pinb_duty_{0};
70 bool forward_direction_{false};
71 bool multiplexing_{false};
72};
73
74} // namespace esphome::hbridge
This class simplifies creating components that periodically check a state.
Definition component.h:585
void set_pinb_pin(output::FloatOutput *pinb_pin)
void write_state(light::LightState *state) override
light::LightTraits get_traits() override
void set_pina_pin(output::FloatOutput *pina_pin)
Interface to write LightStates to hardware.
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
Base class for all output components that can output a variable level, like PWM.
void set_level(float state)
Set the level of this float output, this is called from the front-end.
bool state
Definition fan.h:2
@ COLD_WARM_WHITE
Cold and warm white output with individually controllable brightness.
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:41