ESPHome  2024.11.2
hbridge_fan.cpp
Go to the documentation of this file.
1 #include "hbridge_fan.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace hbridge {
6 
7 static const char *const TAG = "fan.hbridge";
8 
9 void 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
17 void 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  auto restore = this->restore_state_();
32  if (restore.has_value()) {
33  restore->apply(*this);
34  this->write_state_();
35  }
36 
37  // Construct traits
38  this->traits_ = fan::FanTraits(this->oscillating_ != nullptr, true, true, this->speed_count_);
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  if (call.get_state().has_value())
53  this->state = *call.get_state();
54  if (call.get_speed().has_value())
55  this->speed = *call.get_speed();
56  if (call.get_oscillating().has_value())
57  this->oscillating = *call.get_oscillating();
58  if (call.get_direction().has_value())
59  this->direction = *call.get_direction();
60  this->preset_mode = call.get_preset_mode();
61 
62  this->write_state_();
63  this->publish_state();
64 }
65 
67  float speed = this->state ? static_cast<float>(this->speed) / static_cast<float>(this->speed_count_) : 0.0f;
68  if (speed == 0.0f) { // off means idle
69  (this->enable_ == nullptr) ? this->set_hbridge_levels_(speed, speed)
70  : this->set_hbridge_levels_(speed, speed, speed);
71  } else if (this->direction == fan::FanDirection::FORWARD) {
72  if (this->decay_mode_ == DECAY_MODE_SLOW) {
73  (this->enable_ == nullptr) ? this->set_hbridge_levels_(1.0f - speed, 1.0f)
74  : this->set_hbridge_levels_(1.0f - speed, 1.0f, 1.0f);
75  } else { // DECAY_MODE_FAST
76  (this->enable_ == nullptr) ? this->set_hbridge_levels_(0.0f, speed)
77  : this->set_hbridge_levels_(0.0f, 1.0f, speed);
78  }
79  } else { // fan::FAN_DIRECTION_REVERSE
80  if (this->decay_mode_ == DECAY_MODE_SLOW) {
81  (this->enable_ == nullptr) ? this->set_hbridge_levels_(1.0f, 1.0f - speed)
82  : this->set_hbridge_levels_(1.0f, 1.0f - speed, 1.0f);
83  } else { // DECAY_MODE_FAST
84  (this->enable_ == nullptr) ? this->set_hbridge_levels_(speed, 0.0f)
85  : this->set_hbridge_levels_(1.0f, 0.0f, speed);
86  }
87  }
88 
89  if (this->oscillating_ != nullptr)
90  this->oscillating_->set_state(this->oscillating);
91 }
92 
93 } // namespace hbridge
94 } // namespace esphome
bool state
The current on/off state of the fan.
Definition: fan.h:110
void control(const fan::FanCall &call) override
Definition: hbridge_fan.cpp:51
bool oscillating
The current oscillation state of the fan.
Definition: fan.h:112
FanDirection direction
The current direction of the fan.
Definition: fan.h:116
optional< FanDirection > get_direction() const
Definition: fan.h:74
void publish_state()
Definition: fan.cpp:117
void set_hbridge_levels_(float a_level, float b_level)
Definition: hbridge_fan.cpp:9
optional< int > get_speed() const
Definition: fan.h:65
output::FloatOutput * pin_a_
Definition: hbridge_fan.h:34
virtual void set_state(bool state)
Enable or disable this binary output.
Definition: binary_output.h:34
optional< FanRestoreState > restore_state_()
Definition: fan.cpp:140
bool has_value() const
Definition: optional.h:87
void set_level(float state)
Set the level of this float output, this is called from the front-end.
std::string preset_mode
Definition: fan.h:118
int speed
The current fan speed level.
Definition: fan.h:114
optional< bool > get_state() const
Definition: fan.h:49
optional< bool > get_oscillating() const
Definition: fan.h:58
output::FloatOutput * enable_
Definition: hbridge_fan.h:36
FanCall & set_state(bool binary_state)
Definition: fan.h:41
void set_supported_preset_modes(const std::set< std::string > &preset_modes)
Set the preset modes supported by the fan.
Definition: fan_traits.h:34
FanCall make_call()
Definition: fan.cpp:114
output::FloatOutput * pin_b_
Definition: hbridge_fan.h:35
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::set< std::string > preset_modes_
Definition: hbridge_fan.h:41
output::BinaryOutput * oscillating_
Definition: hbridge_fan.h:37
std::string get_preset_mode() const
Definition: fan.h:79