ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
hbridge_switch.cpp
Go to the documentation of this file.
1#include "hbridge_switch.h"
2#include "esphome/core/log.h"
3
4#include <cinttypes>
5
6namespace esphome::hbridge {
7
8static const char *const TAG = "switch.hbridge";
9
12 optional<bool> initial_state = this->get_initial_state_with_restore_mode();
13
14 // Like GPIOSwitch does, set the pin state both before and after pin setup()
15 this->on_pin_->digital_write(false);
16 this->on_pin_->setup();
17 this->on_pin_->digital_write(false);
18
19 this->off_pin_->digital_write(false);
20 this->off_pin_->setup();
21 this->off_pin_->digital_write(false);
22
23 if (initial_state.has_value())
24 this->write_state(initial_state.value());
25}
26
28 LOG_SWITCH("", "H-Bridge Switch", this);
29 LOG_PIN(" On Pin: ", this->on_pin_);
30 LOG_PIN(" Off Pin: ", this->off_pin_);
31 ESP_LOGCONFIG(TAG, " Pulse length: %" PRId32 " ms", this->pulse_length_);
32 if (this->wait_time_)
33 ESP_LOGCONFIG(TAG, " Wait time %" PRId32 " ms", this->wait_time_);
34}
35
37 this->desired_state_ = state;
38 if (!this->timer_running_)
39 this->timer_fn_();
40}
41
43 uint32_t next_timeout = 0;
44
45 while ((uint8_t) this->desired_state_ != this->relay_state_) {
46 switch (this->relay_state_) {
47 case RELAY_STATE_ON:
48 case RELAY_STATE_OFF:
50 if (this->desired_state_) {
51 this->on_pin_->digital_write(true);
53 } else {
54 this->off_pin_->digital_write(true);
56 }
57 next_timeout = this->pulse_length_;
58 if (!this->optimistic_)
59 this->publish_state(this->desired_state_);
60 break;
61
63 this->on_pin_->digital_write(false);
65 if (this->optimistic_)
66 this->publish_state(true);
67 next_timeout = this->wait_time_;
68 break;
69
71 this->off_pin_->digital_write(false);
73 if (this->optimistic_)
74 this->publish_state(false);
75 next_timeout = this->wait_time_;
76 break;
77 }
78
79 if (next_timeout) {
80 this->timer_running_ = true;
81 this->set_timeout(next_timeout, [this]() { this->timer_fn_(); });
82 return;
83 }
84
85 // In the case where ON/OFF state has been reached but we need to
86 // immediately change back again to reach desired_state_, we loop.
87 }
88 this->timer_running_ = false;
89}
90
91} // namespace esphome::hbridge
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:493
virtual void setup()=0
virtual void digital_write(bool value)=0
void write_state(bool state) override
float get_setup_priority() const override
bool state
The current reported state of the binary sensor.
Definition switch.h:55
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:56
optional< bool > get_initial_state_with_restore_mode()
Returns the initial state of the switch, after applying restore mode rules.
Definition switch.cpp:42
bool state
Definition fan.h:2
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:41
static void uint32_t