ESPHome 2025.9.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 {
7namespace hbridge {
8
9static const char *const TAG = "switch.hbridge";
10
14
15 // Like GPIOSwitch does, set the pin state both before and after pin setup()
16 this->on_pin_->digital_write(false);
17 this->on_pin_->setup();
18 this->on_pin_->digital_write(false);
19
20 this->off_pin_->digital_write(false);
21 this->off_pin_->setup();
22 this->off_pin_->digital_write(false);
23
24 if (initial_state.has_value())
25 this->write_state(initial_state.value());
26}
27
29 LOG_SWITCH("", "H-Bridge Switch", this);
30 LOG_PIN(" On Pin: ", this->on_pin_);
31 LOG_PIN(" Off Pin: ", this->off_pin_);
32 ESP_LOGCONFIG(TAG, " Pulse length: %" PRId32 " ms", this->pulse_length_);
33 if (this->wait_time_)
34 ESP_LOGCONFIG(TAG, " Wait time %" PRId32 " ms", this->wait_time_);
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 hbridge
93} // namespace esphome
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
virtual void setup()=0
virtual void digital_write(bool value)=0
void write_state(bool state) override
float get_setup_priority() const override
bool has_value() const
Definition optional.h:92
value_type const & value() const
Definition optional.h:94
bool state
The current reported state of the binary sensor.
Definition switch.h:56
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:55
optional< bool > get_initial_state_with_restore_mode()
Returns the initial state of the switch, after applying restore mode rules.
Definition switch.cpp:41
bool state
Definition fan.h:0
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:49
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7