refactor mcp4728 component (#5609)

Co-authored-by: Samuel Sieb <samuel@sieb.net>
This commit is contained in:
Samuel Sieb 2023-10-26 22:01:18 -07:00 committed by GitHub
parent 907d43827c
commit c83cb30935
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 52 additions and 34 deletions

View File

@ -10,6 +10,7 @@ CONF_STORE_IN_EEPROM = "store_in_eeprom"
mcp4728_ns = cg.esphome_ns.namespace("mcp4728")
MCP4728Component = mcp4728_ns.class_("MCP4728Component", cg.Component, i2c.I2CDevice)
CONF_MCP4728_ID = "mcp4728_id"
CONFIG_SCHEMA = (
cv.Schema(

View File

@ -1,4 +1,4 @@
#include "mcp4728_output.h"
#include "mcp4728.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
@ -110,12 +110,5 @@ void MCP4728Component::select_gain_(MCP4728ChannelIdx channel, MCP4728Gain gain)
this->update_ = true;
}
void MCP4728Channel::write_state(float state) {
const uint16_t max_duty = 4095;
const float duty_rounded = roundf(state * max_duty);
auto duty = static_cast<uint16_t>(duty_rounded);
this->parent_->set_channel_value_(this->channel_, duty);
}
} // namespace mcp4728
} // namespace esphome

View File

@ -1,7 +1,6 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/output/float_output.h"
#include "esphome/components/i2c/i2c.h"
namespace esphome {
@ -64,28 +63,5 @@ class MCP4728Component : public Component, public i2c::I2CDevice {
bool update_ = false;
};
class MCP4728Channel : public output::FloatOutput {
public:
MCP4728Channel(MCP4728Component *parent, MCP4728ChannelIdx channel, MCP4728Vref vref, MCP4728Gain gain,
MCP4728PwrDown pwrdown)
: parent_(parent), channel_(channel), vref_(vref), gain_(gain), pwrdown_(pwrdown) {
// update VREF
parent->select_vref_(channel, vref_);
// update PD
parent->select_power_down_(channel, pwrdown_);
// update GAIN
parent->select_gain_(channel, gain_);
}
protected:
void write_state(float state) override;
MCP4728Component *parent_;
MCP4728ChannelIdx channel_;
MCP4728Vref vref_;
MCP4728Gain gain_;
MCP4728PwrDown pwrdown_;
};
} // namespace mcp4728
} // namespace esphome

View File

@ -2,12 +2,11 @@ import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import output
from esphome.const import CONF_CHANNEL, CONF_ID, CONF_GAIN
from . import MCP4728Component, mcp4728_ns
from .. import MCP4728Component, CONF_MCP4728_ID, mcp4728_ns
DEPENDENCIES = ["mcp4728"]
MCP4728Channel = mcp4728_ns.class_("MCP4728Channel", output.FloatOutput)
CONF_MCP4728_ID = "mcp4728_id"
CONF_VREF = "vref"
CONF_POWER_DOWN = "power_down"

View File

@ -0,0 +1,17 @@
#include "mcp4728_output.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
namespace esphome {
namespace mcp4728 {
void MCP4728Channel::write_state(float state) {
const uint16_t max_duty = 4095;
const float duty_rounded = roundf(state * max_duty);
auto duty = static_cast<uint16_t>(duty_rounded);
this->parent_->set_channel_value_(this->channel_, duty);
}
} // namespace mcp4728
} // namespace esphome

View File

@ -0,0 +1,32 @@
#pragma once
#include "../mcp4728.h"
#include "esphome/core/component.h"
#include "esphome/components/output/float_output.h"
#include "esphome/components/i2c/i2c.h"
namespace esphome {
namespace mcp4728 {
class MCP4728Channel : public output::FloatOutput {
public:
MCP4728Channel(MCP4728Component *parent, MCP4728ChannelIdx channel, MCP4728Vref vref, MCP4728Gain gain,
MCP4728PwrDown pwrdown)
: parent_(parent), channel_(channel) {
// update VREF
parent->select_vref_(channel, vref);
// update PD
parent->select_power_down_(channel, pwrdown);
// update GAIN
parent->select_gain_(channel, gain);
}
protected:
void write_state(float state) override;
MCP4728Component *parent_;
MCP4728ChannelIdx channel_;
};
} // namespace mcp4728
} // namespace esphome