mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-05 12:30:28 +02:00
refactor mcp4728 component (#5609)
Co-authored-by: Samuel Sieb <samuel@sieb.net>
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
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, CONF_MCP4728_ID, mcp4728_ns
|
||||
|
||||
DEPENDENCIES = ["mcp4728"]
|
||||
|
||||
MCP4728Channel = mcp4728_ns.class_("MCP4728Channel", output.FloatOutput)
|
||||
CONF_VREF = "vref"
|
||||
CONF_POWER_DOWN = "power_down"
|
||||
|
||||
MCP4728Vref = mcp4728_ns.enum("MCP4728Vref")
|
||||
VREF_OPTIONS = {
|
||||
"vdd": MCP4728Vref.MCP4728_VREF_VDD,
|
||||
"internal": MCP4728Vref.MCP4728_VREF_INTERNAL_2_8V,
|
||||
}
|
||||
|
||||
MCP4728Gain = mcp4728_ns.enum("MCP4728Gain")
|
||||
GAIN_OPTIONS = {"X1": MCP4728Gain.MCP4728_GAIN_X1, "X2": MCP4728Gain.MCP4728_GAIN_X2}
|
||||
|
||||
MCP4728PwrDown = mcp4728_ns.enum("MCP4728PwrDown")
|
||||
PWRDOWN_OPTIONS = {
|
||||
"normal": MCP4728PwrDown.MCP4728_PD_NORMAL,
|
||||
"gnd_1k": MCP4728PwrDown.MCP4728_PD_GND_1KOHM,
|
||||
"gnd_100k": MCP4728PwrDown.MCP4728_PD_GND_100KOHM,
|
||||
"gnd_500k": MCP4728PwrDown.MCP4728_PD_GND_500KOHM,
|
||||
}
|
||||
|
||||
MCP4728ChannelIdx = mcp4728_ns.enum("MCP4728ChannelIdx")
|
||||
CHANNEL_OPTIONS = {
|
||||
"A": MCP4728ChannelIdx.MCP4728_CHANNEL_A,
|
||||
"B": MCP4728ChannelIdx.MCP4728_CHANNEL_B,
|
||||
"C": MCP4728ChannelIdx.MCP4728_CHANNEL_C,
|
||||
"D": MCP4728ChannelIdx.MCP4728_CHANNEL_D,
|
||||
}
|
||||
|
||||
CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.declare_id(MCP4728Channel),
|
||||
cv.GenerateID(CONF_MCP4728_ID): cv.use_id(MCP4728Component),
|
||||
cv.Required(CONF_CHANNEL): cv.enum(CHANNEL_OPTIONS, upper=True),
|
||||
cv.Optional(CONF_VREF, default="vdd"): cv.enum(VREF_OPTIONS, upper=False),
|
||||
cv.Optional(CONF_POWER_DOWN, default="normal"): cv.enum(
|
||||
PWRDOWN_OPTIONS, upper=False
|
||||
),
|
||||
cv.Optional(CONF_GAIN, default="X1"): cv.enum(GAIN_OPTIONS, upper=True),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
paren = await cg.get_variable(config[CONF_MCP4728_ID])
|
||||
var = cg.new_Pvariable(
|
||||
config[CONF_ID],
|
||||
paren,
|
||||
config[CONF_CHANNEL],
|
||||
config[CONF_VREF],
|
||||
config[CONF_GAIN],
|
||||
config[CONF_POWER_DOWN],
|
||||
)
|
||||
await output.register_output(var, config)
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user