ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
hbridge_light_output.h
Go to the documentation of this file.
1#pragma once
2
7
8namespace esphome::hbridge {
9
11 public:
12 void set_pina_pin(output::FloatOutput *pina_pin) { this->pina_pin_ = pina_pin; }
13 void set_pinb_pin(output::FloatOutput *pinb_pin) { this->pinb_pin_ = pinb_pin; }
14
16 auto traits = light::LightTraits();
17 traits.set_supported_color_modes({light::ColorMode::COLD_WARM_WHITE});
18 traits.set_min_mireds(153);
19 traits.set_max_mireds(500);
20 return traits;
21 }
22
23 void setup() override { this->disable_loop(); }
24
25 void loop() override {
26 // Only called when both channels are active — alternate H-bridge direction
27 // each iteration to multiplex cold and warm white.
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 — need loop to alternate H-bridge direction
50 this->high_freq_.start();
51 this->enable_loop();
52 } else {
53 // Zero or one channel active — drive pins directly, no multiplexing needed
54 this->high_freq_.stop();
55 this->disable_loop();
56 this->pina_pin_->set_level(new_pina);
57 this->pinb_pin_->set_level(new_pinb);
58 }
59 }
60
61 protected:
64 float pina_duty_{0};
65 float pinb_duty_{0};
66 bool forward_direction_{false};
68};
69
70} // namespace esphome::hbridge
void enable_loop()
Enable this component's loop.
Definition component.h:246
void disable_loop()
Disable this component's loop.
Helper class to request loop() to be called as fast as possible.
Definition helpers.h:1993
void stop()
Stop running the loop continuously.
Definition helpers.cpp:735
void start()
Start running the loop continuously.
Definition helpers.cpp:729
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