1
0
mirror of https://github.com/esphome/esphome.git synced 2025-04-14 23:00:29 +01:00

Update mcp4461_output.cpp

Correct calculation of non-absolute floating range values, like eg. passed by light component
This commit is contained in:
Oliver Kleinecke 2025-02-16 14:45:34 +01:00 committed by GitHub
parent 34378386c9
commit 65f498fa3e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,14 +9,16 @@ namespace mcp4461 {
static const char *const TAG = "mcp4461.output";
// floats from other components (like light etc.) are passed as "percentage floats"
// this function converts them to the 0 - 256 range used by the MCP4461
void Mcp4461Wiper::write_state(float state) {
if (this->parent_->set_wiper_level_(this->wiper_, static_cast<uint16_t>(state * 1000))) {
if (this->parent_->set_wiper_level_(this->wiper_, static_cast<uint16_t>(std::roundf(state * 256)))) {
this->state_ = state;
}
}
float Mcp4461Wiper::read_state() {
return (static_cast<float>(this->parent_->get_wiper_level_(this->wiper_)) / 1000.0);
return (static_cast<float>(this->parent_->get_wiper_level_(this->wiper_)) / 256.0);
}
float Mcp4461Wiper::update_state() {
@ -24,19 +26,24 @@ float Mcp4461Wiper::update_state() {
return this->state_;
}
void Mcp4461Wiper::enable_wiper() { this->parent_->enable_wiper_(this->wiper_); }
void Mcp4461Wiper::set_state(bool state) {
if (state) { this->turn_on(); }
else { this->turn_off(); }
}
void Mcp4461Wiper::disable_wiper() { this->parent_->disable_wiper_(this->wiper_); }
void Mcp4461Wiper::turn_on() { this->parent_->enable_wiper_(this->wiper_); }
void Mcp4461Wiper::turn_off() { this->parent_->disable_wiper_(this->wiper_); }
void Mcp4461Wiper::increase_wiper() {
if (this->parent_->increase_wiper_(this->wiper_)) {
this->state_ = this->state_ + 0.001;
this->state_ = this->update_state();
}
}
void Mcp4461Wiper::decrease_wiper() {
if (this->parent_->decrease_wiper_(this->wiper_)) {
this->state_ = this->state_ - 0.001;
this->state_ = this->update_state();
}
}