ESPHome 2026.5.1
Loading...
Searching...
No Matches
hbridge_fan.cpp
Go to the documentation of this file.
1#include "hbridge_fan.h"
2#include "esphome/core/log.h"
3
4namespace esphome::hbridge {
5
6static const char *const TAG = "fan.hbridge";
7
8void HBridgeFan::set_hbridge_levels_(float a_level, float b_level) {
9 this->pin_a_->set_level(a_level);
10 this->pin_b_->set_level(b_level);
11 ESP_LOGD(TAG, "Setting speed: a: %.2f, b: %.2f", a_level, b_level);
12}
13
14// constant IN1/IN2, PWM on EN => power control, fast current decay
15// constant IN1/EN, PWM on IN2 => power control, slow current decay
16void HBridgeFan::set_hbridge_levels_(float a_level, float b_level, float enable) {
17 this->pin_a_->set_level(a_level);
18 this->pin_b_->set_level(b_level);
19 this->enable_->set_level(enable);
20 ESP_LOGD(TAG, "Setting speed: a: %.2f, b: %.2f, enable: %.2f", a_level, b_level, enable);
21}
22
24 ESP_LOGD(TAG, "Braking");
25 (this->enable_ == nullptr) ? this->set_hbridge_levels_(1.0f, 1.0f) : this->set_hbridge_levels_(1.0f, 1.0f, 1.0f);
26 return this->make_call().set_state(false);
27}
28
30 // Construct traits before restore so preset modes can be looked up by index
31 this->traits_ = fan::FanTraits(this->oscillating_ != nullptr, true, true, this->speed_count_);
32
33 auto restore = this->restore_state_();
34 if (restore.has_value()) {
35 restore->apply(*this);
36 this->write_state_();
37 }
38}
39
41 LOG_FAN("", "H-Bridge Fan", this);
42 if (this->decay_mode_ == DECAY_MODE_SLOW) {
43 ESP_LOGCONFIG(TAG, " Decay Mode: Slow");
44 } else {
45 ESP_LOGCONFIG(TAG, " Decay Mode: Fast");
46 }
47}
48
50 auto call_state = call.get_state();
51 if (call_state.has_value())
52 this->state = *call_state;
53 auto call_speed = call.get_speed();
54 if (call_speed.has_value())
55 this->speed = *call_speed;
56 auto call_oscillating = call.get_oscillating();
57 if (call_oscillating.has_value())
58 this->oscillating = *call_oscillating;
59 auto call_direction = call.get_direction();
60 if (call_direction.has_value())
61 this->direction = *call_direction;
62 this->apply_preset_mode_(call);
63
64 this->write_state_();
65 this->publish_state();
66}
67
69 float speed = this->state ? static_cast<float>(this->speed) / static_cast<float>(this->speed_count_) : 0.0f;
70 if (speed == 0.0f) { // off means idle
71 (this->enable_ == nullptr) ? this->set_hbridge_levels_(speed, speed)
72 : this->set_hbridge_levels_(speed, speed, speed);
73 } else if (this->direction == fan::FanDirection::FORWARD) {
74 if (this->decay_mode_ == DECAY_MODE_SLOW) {
75 (this->enable_ == nullptr) ? this->set_hbridge_levels_(1.0f - speed, 1.0f)
76 : this->set_hbridge_levels_(1.0f - speed, 1.0f, 1.0f);
77 } else { // DECAY_MODE_FAST
78 (this->enable_ == nullptr) ? this->set_hbridge_levels_(0.0f, speed)
79 : this->set_hbridge_levels_(0.0f, 1.0f, speed);
80 }
81 } else { // fan::FAN_DIRECTION_REVERSE
82 if (this->decay_mode_ == DECAY_MODE_SLOW) {
83 (this->enable_ == nullptr) ? this->set_hbridge_levels_(1.0f, 1.0f - speed)
84 : this->set_hbridge_levels_(1.0f, 1.0f - speed, 1.0f);
85 } else { // DECAY_MODE_FAST
86 (this->enable_ == nullptr) ? this->set_hbridge_levels_(speed, 0.0f)
87 : this->set_hbridge_levels_(1.0f, 0.0f, speed);
88 }
89 }
90
91 if (this->oscillating_ != nullptr)
93}
94
95} // namespace esphome::hbridge
FanCall & set_state(bool binary_state)
Definition fan.h:41
void publish_state()
Definition fan.cpp:223
FanCall make_call()
Definition fan.cpp:159
void apply_preset_mode_(const FanCall &call)
Apply preset mode from a FanCall (handles speed-clears-preset convention)
Definition fan.cpp:214
FanDirection direction
The current direction of the fan.
Definition fan.h:116
bool oscillating
The current oscillation state of the fan.
Definition fan.h:112
bool state
The current on/off state of the fan.
Definition fan.h:110
int speed
The current fan speed level.
Definition fan.h:114
optional< FanRestoreState > restore_state_()
Definition fan.cpp:251
output::FloatOutput * pin_a_
Definition hbridge_fan.h:34
output::BinaryOutput * oscillating_
Definition hbridge_fan.h:37
void set_hbridge_levels_(float a_level, float b_level)
output::FloatOutput * enable_
Definition hbridge_fan.h:36
void control(const fan::FanCall &call) override
output::FloatOutput * pin_b_
Definition hbridge_fan.h:35
virtual void set_state(bool state)
Enable or disable this binary output.
void set_level(float state)
Set the level of this float output, this is called from the front-end.