ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
hbridge_light_output.h
Go to the documentation of this file.
1#pragma once
2
7
8namespace esphome {
9namespace hbridge {
10
12 public:
13 void set_pina_pin(output::FloatOutput *pina_pin) { this->pina_pin_ = pina_pin; }
14 void set_pinb_pin(output::FloatOutput *pinb_pin) { this->pinb_pin_ = pinb_pin; }
15
17 auto traits = light::LightTraits();
18 traits.set_supported_color_modes({light::ColorMode::COLD_WARM_WHITE});
19 traits.set_min_mireds(153);
20 traits.set_max_mireds(500);
21 return traits;
22 }
23
24 void setup() override { this->disable_loop(); }
25
26 void loop() override {
27 // Only called when both channels are active — alternate H-bridge direction
28 // each iteration to multiplex cold and warm white.
29 if (!this->forward_direction_) {
30 this->pina_pin_->set_level(this->pina_duty_);
31 this->pinb_pin_->set_level(0);
32 this->forward_direction_ = true;
33 } else {
34 this->pina_pin_->set_level(0);
35 this->pinb_pin_->set_level(this->pinb_duty_);
36 this->forward_direction_ = false;
37 }
38 }
39
40 float get_setup_priority() const override { return setup_priority::HARDWARE; }
41
43 float new_pina, new_pinb;
44 state->current_values_as_cwww(&new_pina, &new_pinb, false);
45
46 this->pina_duty_ = new_pina;
47 this->pinb_duty_ = new_pinb;
48
49 if (new_pina != 0.0f && new_pinb != 0.0f) {
50 // Both channels active — need loop to alternate H-bridge direction
51 this->high_freq_.start();
52 this->enable_loop();
53 } else {
54 // Zero or one channel active — drive pins directly, no multiplexing needed
55 this->high_freq_.stop();
56 this->disable_loop();
57 this->pina_pin_->set_level(new_pina);
58 this->pinb_pin_->set_level(new_pinb);
59 }
60 }
61
62 protected:
65 float pina_duty_{0};
66 float pinb_duty_{0};
67 bool forward_direction_{false};
69};
70
71} // namespace hbridge
72} // namespace esphome
void enable_loop()
Enable this component's loop.
Definition component.h:258
void disable_loop()
Disable this component's loop.
Helper class to request loop() to be called as fast as possible.
Definition helpers.h:2145
void stop()
Stop running the loop continuously.
Definition helpers.cpp:847
void start()
Start running the loop continuously.
Definition helpers.cpp:841
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.
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:40
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7