ESPHome 2025.5.0
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
13 ESP_LOGCONFIG(TAG, "Setting up H-Bridge Switch '%s'...", this->name_.c_str());
14
16
17 // Like GPIOSwitch does, set the pin state both before and after pin setup()
18 this->on_pin_->digital_write(false);
19 this->on_pin_->setup();
20 this->on_pin_->digital_write(false);
21
22 this->off_pin_->digital_write(false);
23 this->off_pin_->setup();
24 this->off_pin_->digital_write(false);
25
26 if (initial_state.has_value())
27 this->write_state(initial_state.value());
28}
29
31 LOG_SWITCH("", "H-Bridge Switch", this);
32 LOG_PIN(" On Pin: ", this->on_pin_);
33 LOG_PIN(" Off Pin: ", this->off_pin_);
34 ESP_LOGCONFIG(TAG, " Pulse length: %" PRId32 " ms", this->pulse_length_);
35 if (this->wait_time_)
36 ESP_LOGCONFIG(TAG, " Wait time %" PRId32 " ms", this->wait_time_);
37}
38
40 this->desired_state_ = state;
41 if (!this->timer_running_)
42 this->timer_fn_();
43}
44
46 uint32_t next_timeout = 0;
47
48 while ((uint8_t) this->desired_state_ != this->relay_state_) {
49 switch (this->relay_state_) {
50 case RELAY_STATE_ON:
51 case RELAY_STATE_OFF:
53 if (this->desired_state_) {
54 this->on_pin_->digital_write(true);
56 } else {
57 this->off_pin_->digital_write(true);
59 }
60 next_timeout = this->pulse_length_;
61 if (!this->optimistic_)
62 this->publish_state(this->desired_state_);
63 break;
64
66 this->on_pin_->digital_write(false);
68 if (this->optimistic_)
69 this->publish_state(true);
70 next_timeout = this->wait_time_;
71 break;
72
74 this->off_pin_->digital_write(false);
76 if (this->optimistic_)
77 this->publish_state(false);
78 next_timeout = this->wait_time_;
79 break;
80 }
81
82 if (next_timeout) {
83 this->timer_running_ = true;
84 this->set_timeout(next_timeout, [this]() { this->timer_fn_(); });
85 return;
86 }
87
88 // In the case where ON/OFF state has been reached but we need to
89 // immediately change back again to reach desired_state_, we loop.
90 }
91 this->timer_running_ = false;
92}
93
94} // namespace hbridge
95} // namespace esphome
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.cpp:72
virtual void setup()=0
virtual void digital_write(bool value)=0
constexpr const char * c_str() const
Definition string_ref.h:68
void write_state(bool state) override
float get_setup_priority() const override
bool has_value() const
Definition optional.h:87
value_type const & value() const
Definition optional.h:89
bool state
The current reported state of the binary sensor.
Definition switch.h:53
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:47
optional< bool > get_initial_state_with_restore_mode()
Returns the initial state of the switch, after applying restore mode rules.
Definition switch.cpp:33
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:18
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7