ESPHome  2025.4.0
mcp4461_output.cpp
Go to the documentation of this file.
1 #include "mcp4461_output.h"
2 #include <cmath>
3 
4 #include "esphome/core/helpers.h"
5 #include "esphome/core/log.h"
6 
7 namespace esphome {
8 namespace mcp4461 {
9 
10 static const char *const TAG = "mcp4461.output";
11 
12 // public set_level function
14  if (!std::isfinite(state)) {
15  ESP_LOGW(TAG, "Finite state state value is required.");
16  return;
17  }
18  state = clamp(state, 0.0f, 1.0f);
19  if (this->is_inverted()) {
20  state = 1.0f - state;
21  }
22  this->write_state(state);
23 }
24 
25 // floats from other components (like light etc.) are passed as "percentage floats"
26 // this function converts them to the 0 - 256 range used by the MCP4461
28  if (this->parent_->set_wiper_level_(this->wiper_, static_cast<uint16_t>(std::roundf(state * 256)))) {
29  this->state_ = state;
30  }
31 }
32 
33 float Mcp4461Wiper::read_state() { return (static_cast<float>(this->parent_->get_wiper_level_(this->wiper_)) / 256.0); }
34 
36  this->state_ = this->read_state();
37  return this->state_;
38 }
39 
41  if (state) {
42  this->turn_on();
43  } else {
44  this->turn_off();
45  }
46 }
47 
49 
51 
53  if (this->parent_->increase_wiper_(this->wiper_)) {
54  this->state_ = this->update_state();
55  ESP_LOGV(TAG, "Increased wiper %u to %u", static_cast<uint8_t>(this->wiper_),
56  static_cast<uint16_t>(std::roundf(this->state_ * 256)));
57  }
58 }
59 
61  if (this->parent_->decrease_wiper_(this->wiper_)) {
62  this->state_ = this->update_state();
63  ESP_LOGV(TAG, "Decreased wiper %u to %u", static_cast<uint8_t>(this->wiper_),
64  static_cast<uint16_t>(std::roundf(this->state_ * 256)));
65  }
66 }
67 
68 void Mcp4461Wiper::enable_terminal(char terminal) { this->parent_->enable_terminal_(this->wiper_, terminal); }
69 
70 void Mcp4461Wiper::disable_terminal(char terminal) { this->parent_->disable_terminal_(this->wiper_, terminal); }
71 
72 } // namespace mcp4461
73 } // namespace esphome
bool state
Definition: fan.h:34
void write_state(float state) override
void turn_off() override
Disables current output.
bool set_wiper_level_(Mcp4461WiperIdx wiper, uint16_t value)
Definition: mcp4461.cpp:234
void set_level(float state)
Set level of wiper.
void turn_on() override
Enables current output.
bool is_inverted() const
Return whether this binary output is inverted.
Definition: binary_output.h:61
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition: helpers.h:101
void enable_terminal(char terminal)
Enable given terminal.
uint16_t get_wiper_level_(Mcp4461WiperIdx wiper)
Definition: mcp4461.cpp:183
void disable_terminal_(Mcp4461WiperIdx, char terminal)
Definition: mcp4461.cpp:482
void enable_wiper_(Mcp4461WiperIdx wiper)
Definition: mcp4461.cpp:267
bool increase_wiper_(Mcp4461WiperIdx wiper)
Definition: mcp4461.cpp:311
float read_state()
Read current device wiper state without updating internal output state.
void increase_wiper()
Increase wiper by 1 tap.
void disable_wiper_(Mcp4461WiperIdx wiper)
Definition: mcp4461.cpp:289
void disable_terminal(char terminal)
Disable given terminal.
void decrease_wiper()
Decrease wiper by 1 tap.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_state(bool state) override
Enables/Disables current output using bool parameter.
float update_state()
Update current output state using device wiper state.
bool decrease_wiper_(Mcp4461WiperIdx wiper)
Definition: mcp4461.cpp:342
void enable_terminal_(Mcp4461WiperIdx wiper, char terminal)
Definition: mcp4461.cpp:455