ESPHome 2026.5.1
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::mcp4461 {
8
9static const char *const TAG = "mcp4461.output";
10
11// public set_level function
13 if (!std::isfinite(state)) {
14 ESP_LOGW(TAG, "Finite state state value is required.");
15 return;
16 }
17 state = clamp(state, 0.0f, 1.0f);
18 if (this->is_inverted()) {
19 state = 1.0f - state;
20 }
21 this->write_state(state);
22}
23
24// floats from other components (like light etc.) are passed as "percentage floats"
25// this function converts them to the 0 - 256 range used by the MCP4461
27 if (this->parent_->set_wiper_level_(this->wiper_, static_cast<uint16_t>(std::roundf(state * 256)))) {
28 this->state_ = state;
29 }
30}
31
32float Mcp4461Wiper::read_state() { return (static_cast<float>(this->parent_->get_wiper_level_(this->wiper_)) / 256.0); }
33
35 this->state_ = this->read_state();
36 return this->state_;
37}
38
40 if (state) {
41 this->turn_on();
42 } else {
43 this->turn_off();
44 }
45}
46
48
50
52 if (this->parent_->increase_wiper_(this->wiper_)) {
53 this->state_ = this->update_state();
54 ESP_LOGV(TAG, "Increased wiper %u to %u", static_cast<uint8_t>(this->wiper_),
55 static_cast<uint16_t>(std::roundf(this->state_ * 256)));
56 }
57}
58
60 if (this->parent_->decrease_wiper_(this->wiper_)) {
61 this->state_ = this->update_state();
62 ESP_LOGV(TAG, "Decreased wiper %u to %u", static_cast<uint8_t>(this->wiper_),
63 static_cast<uint16_t>(std::roundf(this->state_ * 256)));
64 }
65}
66
67void Mcp4461Wiper::enable_terminal(char terminal) { this->parent_->enable_terminal_(this->wiper_, terminal); }
68
69void Mcp4461Wiper::disable_terminal(char terminal) { this->parent_->disable_terminal_(this->wiper_, terminal); }
70
71} // namespace esphome::mcp4461
bool increase_wiper_(Mcp4461WiperIdx wiper)
Definition mcp4461.cpp:324
void disable_wiper_(Mcp4461WiperIdx wiper)
Definition mcp4461.cpp:302
void enable_wiper_(Mcp4461WiperIdx wiper)
Definition mcp4461.cpp:280
void enable_terminal_(Mcp4461WiperIdx wiper, char terminal)
Definition mcp4461.cpp:468
uint16_t get_wiper_level_(Mcp4461WiperIdx wiper)
Definition mcp4461.cpp:196
void disable_terminal_(Mcp4461WiperIdx, char terminal)
Definition mcp4461.cpp:495
bool decrease_wiper_(Mcp4461WiperIdx wiper)
Definition mcp4461.cpp:355
bool set_wiper_level_(Mcp4461WiperIdx wiper, uint16_t value)
Definition mcp4461.cpp:247
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:2