ESPHome 2026.10.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}
36
38 this->desired_state_ = state;
39 if (!this->timer_running_)
40 this->timer_fn_();
41}
42
44 uint32_t next_timeout = 0;
45
46 while ((uint8_t) this->desired_state_ != this->relay_state_) {
47 switch (this->relay_state_) {
48 case RELAY_STATE_ON:
49 case RELAY_STATE_OFF:
51 if (this->desired_state_) {
52 this->on_pin_->digital_write(true);
54 } else {
55 this->off_pin_->digital_write(true);
57 }
58 next_timeout = this->pulse_length_;
59 if (!this->optimistic_)
60 this->publish_state(this->desired_state_);
61 break;
62
64 this->on_pin_->digital_write(false);
66 if (this->optimistic_)
67 this->publish_state(true);
68 next_timeout = this->wait_time_;
69 break;
70
72 this->off_pin_->digital_write(false);
74 if (this->optimistic_)
75 this->publish_state(false);
76 next_timeout = this->wait_time_;
77 break;
78 }
79
80 if (next_timeout) {
81 this->timer_running_ = true;
82 this->set_timeout(next_timeout, [this]() { this->timer_fn_(); });
83 return;
84 }
85
86 // In the case where ON/OFF state has been reached but we need to
87 // immediately change back again to reach desired_state_, we loop.
88 }
89 this->timer_running_ = false;
90}
91
92} // namespace esphome::hbridge
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
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:57
optional< bool > get_initial_state_with_restore_mode()
Returns the initial state of the switch, after applying restore mode rules.
Definition switch.cpp:43
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:43
static void uint32_t