ESPHome 2026.3.3
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_);
34
35 auto restore = this->restore_state_();
36 if (restore.has_value()) {
37 restore->apply(*this);
38 this->write_state_();
39 }
40}
41
43 LOG_FAN("", "H-Bridge Fan", this);
44 if (this->decay_mode_ == DECAY_MODE_SLOW) {
45 ESP_LOGCONFIG(TAG, " Decay Mode: Slow");
46 } else {
47 ESP_LOGCONFIG(TAG, " Decay Mode: Fast");
48 }
49}
50
52 auto call_state = call.get_state();
53 if (call_state.has_value())
54 this->state = *call_state;
55 auto call_speed = call.get_speed();
56 if (call_speed.has_value())
57 this->speed = *call_speed;
58 auto call_oscillating = call.get_oscillating();
59 if (call_oscillating.has_value())
60 this->oscillating = *call_oscillating;
61 auto call_direction = call.get_direction();
62 if (call_direction.has_value())
63 this->direction = *call_direction;
64 this->apply_preset_mode_(call);
65
66 this->write_state_();
67 this->publish_state();
68}
69
71 float speed = this->state ? static_cast<float>(this->speed) / static_cast<float>(this->speed_count_) : 0.0f;
72 if (speed == 0.0f) { // off means idle
73 (this->enable_ == nullptr) ? this->set_hbridge_levels_(speed, speed)
74 : this->set_hbridge_levels_(speed, speed, speed);
75 } else if (this->direction == fan::FanDirection::FORWARD) {
76 if (this->decay_mode_ == DECAY_MODE_SLOW) {
77 (this->enable_ == nullptr) ? this->set_hbridge_levels_(1.0f - speed, 1.0f)
78 : this->set_hbridge_levels_(1.0f - speed, 1.0f, 1.0f);
79 } else { // DECAY_MODE_FAST
80 (this->enable_ == nullptr) ? this->set_hbridge_levels_(0.0f, speed)
81 : this->set_hbridge_levels_(0.0f, 1.0f, speed);
82 }
83 } else { // fan::FAN_DIRECTION_REVERSE
84 if (this->decay_mode_ == DECAY_MODE_SLOW) {
85 (this->enable_ == nullptr) ? this->set_hbridge_levels_(1.0f, 1.0f - speed)
86 : this->set_hbridge_levels_(1.0f, 1.0f - speed, 1.0f);
87 } else { // DECAY_MODE_FAST
88 (this->enable_ == nullptr) ? this->set_hbridge_levels_(speed, 0.0f)
89 : this->set_hbridge_levels_(1.0f, 0.0f, speed);
90 }
91 }
92
93 if (this->oscillating_ != nullptr)
95}
96
97} // namespace hbridge
98} // namespace esphome
FanCall & set_state(bool binary_state)
Definition fan.h:42
void publish_state()
Definition fan.cpp:197
FanCall make_call()
Definition fan.cpp:144
void apply_preset_mode_(const FanCall &call)
Apply preset mode from a FanCall (handles speed-clears-preset convention)
Definition fan.cpp:187
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:225
void set_supported_preset_modes(std::initializer_list< const char * > preset_modes)
Set the preset modes supported by the fan (from initializer list).
Definition fan_traits.h:36
output::FloatOutput * pin_a_
Definition hbridge_fan.h:32
std::vector< const char * > preset_modes_
Definition hbridge_fan.h:39
output::BinaryOutput * oscillating_
Definition hbridge_fan.h:35
void set_hbridge_levels_(float a_level, float b_level)
output::FloatOutput * enable_
Definition hbridge_fan.h:34
void control(const fan::FanCall &call) override
output::FloatOutput * pin_b_
Definition hbridge_fan.h:33
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