diff --git a/esphome/components/mcp4728/__init__.py b/esphome/components/mcp4728/__init__.py index d130ceb738..a0702c415c 100644 --- a/esphome/components/mcp4728/__init__.py +++ b/esphome/components/mcp4728/__init__.py @@ -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( diff --git a/esphome/components/mcp4728/mcp4728_output.cpp b/esphome/components/mcp4728/mcp4728.cpp similarity index 90% rename from esphome/components/mcp4728/mcp4728_output.cpp rename to esphome/components/mcp4728/mcp4728.cpp index d011967624..1a8568a21c 100644 --- a/esphome/components/mcp4728/mcp4728_output.cpp +++ b/esphome/components/mcp4728/mcp4728.cpp @@ -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(duty_rounded); - this->parent_->set_channel_value_(this->channel_, duty); -} - } // namespace mcp4728 } // namespace esphome diff --git a/esphome/components/mcp4728/mcp4728_output.h b/esphome/components/mcp4728/mcp4728.h similarity index 69% rename from esphome/components/mcp4728/mcp4728_output.h rename to esphome/components/mcp4728/mcp4728.h index 55bcfdccb6..f2262f4a35 100644 --- a/esphome/components/mcp4728/mcp4728_output.h +++ b/esphome/components/mcp4728/mcp4728.h @@ -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 diff --git a/esphome/components/mcp4728/output.py b/esphome/components/mcp4728/output/__init__.py similarity index 96% rename from esphome/components/mcp4728/output.py rename to esphome/components/mcp4728/output/__init__.py index e0913ab98a..20b196ca2c 100644 --- a/esphome/components/mcp4728/output.py +++ b/esphome/components/mcp4728/output/__init__.py @@ -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" diff --git a/esphome/components/mcp4728/output/mcp4728_output.cpp b/esphome/components/mcp4728/output/mcp4728_output.cpp new file mode 100644 index 0000000000..b587e8801b --- /dev/null +++ b/esphome/components/mcp4728/output/mcp4728_output.cpp @@ -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(duty_rounded); + this->parent_->set_channel_value_(this->channel_, duty); +} + +} // namespace mcp4728 +} // namespace esphome diff --git a/esphome/components/mcp4728/output/mcp4728_output.h b/esphome/components/mcp4728/output/mcp4728_output.h new file mode 100644 index 0000000000..453d632f4c --- /dev/null +++ b/esphome/components/mcp4728/output/mcp4728_output.h @@ -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