ESPHome 2026.5.0-dev
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 {
5namespace hbridge {
6
7static const char *const TAG = "fan.hbridge";
8
9void HBridgeFan::set_hbridge_levels_(float a_level, float b_level) {
10 this->pin_a_->set_level(a_level);
11 this->pin_b_->set_level(b_level);
12 ESP_LOGD(TAG, "Setting speed: a: %.2f, b: %.2f", a_level, b_level);
13}
14
15// constant IN1/IN2, PWM on EN => power control, fast current decay
16// constant IN1/EN, PWM on IN2 => power control, slow current decay
17void HBridgeFan::set_hbridge_levels_(float a_level, float b_level, float enable) {
18 this->pin_a_->set_level(a_level);
19 this->pin_b_->set_level(b_level);
20 this->enable_->set_level(enable);
21 ESP_LOGD(TAG, "Setting speed: a: %.2f, b: %.2f, enable: %.2f", a_level, b_level, enable);
22}
23
25 ESP_LOGD(TAG, "Braking");
26 (this->enable_ == nullptr) ? this->set_hbridge_levels_(1.0f, 1.0f) : this->set_hbridge_levels_(1.0f, 1.0f, 1.0f);
27 return this->make_call().set_state(false);
28}
29
31 // Construct traits before restore so preset modes can be looked up by index
32 this->traits_ = fan::FanTraits(this->oscillating_ != nullptr, true, true, this->speed_count_);
33
34 auto restore = this->restore_state_();
35 if (restore.has_value()) {
36 restore->apply(*this);
37 this->write_state_();
38 }
39}
40
42 LOG_FAN("", "H-Bridge Fan", this);
43 if (this->decay_mode_ == DECAY_MODE_SLOW) {
44 ESP_LOGCONFIG(TAG, " Decay Mode: Slow");
45 } else {
46 ESP_LOGCONFIG(TAG, " Decay Mode: Fast");
47 }
48}
49
51 auto call_state = call.get_state();
52 if (call_state.has_value())
53 this->state = *call_state;
54 auto call_speed = call.get_speed();
55 if (call_speed.has_value())
56 this->speed = *call_speed;
57 auto call_oscillating = call.get_oscillating();
58 if (call_oscillating.has_value())
59 this->oscillating = *call_oscillating;
60 auto call_direction = call.get_direction();
61 if (call_direction.has_value())
62 this->direction = *call_direction;
63 this->apply_preset_mode_(call);
64
65 this->write_state_();
66 this->publish_state();
67}
68
70 float speed = this->state ? static_cast<float>(this->speed) / static_cast<float>(this->speed_count_) : 0.0f;
71 if (speed == 0.0f) { // off means idle
72 (this->enable_ == nullptr) ? this->set_hbridge_levels_(speed, speed)
73 : this->set_hbridge_levels_(speed, speed, speed);
74 } else if (this->direction == fan::FanDirection::FORWARD) {
75 if (this->decay_mode_ == DECAY_MODE_SLOW) {
76 (this->enable_ == nullptr) ? this->set_hbridge_levels_(1.0f - speed, 1.0f)
77 : this->set_hbridge_levels_(1.0f - speed, 1.0f, 1.0f);
78 } else { // DECAY_MODE_FAST
79 (this->enable_ == nullptr) ? this->set_hbridge_levels_(0.0f, speed)
80 : this->set_hbridge_levels_(0.0f, 1.0f, speed);
81 }
82 } else { // fan::FAN_DIRECTION_REVERSE
83 if (this->decay_mode_ == DECAY_MODE_SLOW) {
84 (this->enable_ == nullptr) ? this->set_hbridge_levels_(1.0f, 1.0f - speed)
85 : this->set_hbridge_levels_(1.0f, 1.0f - speed, 1.0f);
86 } else { // DECAY_MODE_FAST
87 (this->enable_ == nullptr) ? this->set_hbridge_levels_(speed, 0.0f)
88 : this->set_hbridge_levels_(1.0f, 0.0f, speed);
89 }
90 }
91
92 if (this->oscillating_ != nullptr)
94}
95
96} // namespace hbridge
97} // namespace esphome
FanCall & set_state(bool binary_state)
Definition fan.h:42
void publish_state()
Definition fan.cpp:224
FanCall make_call()
Definition fan.cpp:160
void apply_preset_mode_(const FanCall &call)
Apply preset mode from a FanCall (handles speed-clears-preset convention)
Definition fan.cpp:215
FanDirection direction
The current direction of the fan.
Definition fan.h:117
bool oscillating
The current oscillation state of the fan.
Definition fan.h:113
bool state
The current on/off state of the fan.
Definition fan.h:111
int speed
The current fan speed level.
Definition fan.h:115
optional< FanRestoreState > restore_state_()
Definition fan.cpp:252
output::FloatOutput * pin_a_
Definition hbridge_fan.h:35
output::BinaryOutput * oscillating_
Definition hbridge_fan.h:38
void set_hbridge_levels_(float a_level, float b_level)
output::FloatOutput * enable_
Definition hbridge_fan.h:37
void control(const fan::FanCall &call) override
output::FloatOutput * pin_b_
Definition hbridge_fan.h:36
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.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7