ESPHome 2025.5.0
Loading...
Searching...
No Matches
mcp4461_output.cpp
Go to the documentation of this file.
1#include "mcp4461_output.h"
2#include <cmath>
3
5#include "esphome/core/log.h"
6
7namespace esphome {
8namespace mcp4461 {
9
10static 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
33float 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
68void Mcp4461Wiper::enable_terminal(char terminal) { this->parent_->enable_terminal_(this->wiper_, terminal); }
69
70void Mcp4461Wiper::disable_terminal(char terminal) { this->parent_->disable_terminal_(this->wiper_, terminal); }
71
72} // namespace mcp4461
73} // namespace esphome
bool increase_wiper_(Mcp4461WiperIdx wiper)
Definition mcp4461.cpp:311
void disable_wiper_(Mcp4461WiperIdx wiper)
Definition mcp4461.cpp:289
void enable_wiper_(Mcp4461WiperIdx wiper)
Definition mcp4461.cpp:267
void enable_terminal_(Mcp4461WiperIdx wiper, char terminal)
Definition mcp4461.cpp:455
uint16_t get_wiper_level_(Mcp4461WiperIdx wiper)
Definition mcp4461.cpp:183
void disable_terminal_(Mcp4461WiperIdx, char terminal)
Definition mcp4461.cpp:482
bool decrease_wiper_(Mcp4461WiperIdx wiper)
Definition mcp4461.cpp:342
bool set_wiper_level_(Mcp4461WiperIdx wiper, uint16_t value)
Definition mcp4461.cpp:234
void set_level(float state)
Set level of wiper.
void disable_terminal(char terminal)
Disable given terminal.
float update_state()
Update current output state using device wiper state.
void turn_on() override
Enables current output.
void set_state(bool state) override
Enables/Disables current output using bool parameter.
float read_state()
Read current device wiper state without updating internal output state.
void turn_off() override
Disables current output.
void decrease_wiper()
Decrease wiper by 1 tap.
void write_state(float state) override
void increase_wiper()
Increase wiper by 1 tap.
void enable_terminal(char terminal)
Enable given terminal.
bool is_inverted() const
Return whether this binary output is inverted.
bool state
Definition fan.h:0
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition helpers.h:101