mirror of
https://github.com/esphome/esphome.git
synced 2025-11-05 10:12:37 +01:00
[gp8403] Add gp8413 (15 bits) DAC model (#7726)
Co-authored-by: Djordje Mandic <6750655+DjordjeMandic@users.noreply.github.com> Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Co-authored-by: Jonathan Swoboda <154711427+swoboda1337@users.noreply.github.com> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
This commit is contained in:
parent
71fa88c9d4
commit
968df6cb3f
@ -181,7 +181,7 @@ esphome/components/gdk101/* @Szewcson
|
||||
esphome/components/gl_r01_i2c/* @pkejval
|
||||
esphome/components/globals/* @esphome/core
|
||||
esphome/components/gp2y1010au0f/* @zry98
|
||||
esphome/components/gp8403/* @jesserockz
|
||||
esphome/components/gp8403/* @jesserockz @sebydocky
|
||||
esphome/components/gpio/* @esphome/core
|
||||
esphome/components/gpio/one_wire/* @ssieb
|
||||
esphome/components/gps/* @coogle @ximex
|
||||
|
||||
@ -1,19 +1,25 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import i2c
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_ID, CONF_VOLTAGE
|
||||
from esphome.const import CONF_ID, CONF_MODEL, CONF_VOLTAGE
|
||||
|
||||
CODEOWNERS = ["@jesserockz"]
|
||||
CODEOWNERS = ["@jesserockz", "@sebydocky"]
|
||||
DEPENDENCIES = ["i2c"]
|
||||
MULTI_CONF = True
|
||||
|
||||
gp8403_ns = cg.esphome_ns.namespace("gp8403")
|
||||
GP8403 = gp8403_ns.class_("GP8403", cg.Component, i2c.I2CDevice)
|
||||
GP8403Component = gp8403_ns.class_("GP8403Component", cg.Component, i2c.I2CDevice)
|
||||
|
||||
GP8403Voltage = gp8403_ns.enum("GP8403Voltage")
|
||||
GP8403Model = gp8403_ns.enum("GP8403Model")
|
||||
|
||||
CONF_GP8403_ID = "gp8403_id"
|
||||
|
||||
MODELS = {
|
||||
"GP8403": GP8403Model.GP8403,
|
||||
"GP8413": GP8403Model.GP8413,
|
||||
}
|
||||
|
||||
VOLTAGES = {
|
||||
"5V": GP8403Voltage.GP8403_VOLTAGE_5V,
|
||||
"10V": GP8403Voltage.GP8403_VOLTAGE_10V,
|
||||
@ -22,7 +28,8 @@ VOLTAGES = {
|
||||
CONFIG_SCHEMA = (
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(GP8403),
|
||||
cv.GenerateID(): cv.declare_id(GP8403Component),
|
||||
cv.Optional(CONF_MODEL, default="GP8403"): cv.enum(MODELS, upper=True),
|
||||
cv.Required(CONF_VOLTAGE): cv.enum(VOLTAGES, upper=True),
|
||||
}
|
||||
)
|
||||
@ -35,5 +42,5 @@ async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
await i2c.register_i2c_device(var, config)
|
||||
|
||||
cg.add(var.set_model(config[CONF_MODEL]))
|
||||
cg.add(var.set_voltage(config[CONF_VOLTAGE]))
|
||||
|
||||
@ -8,16 +8,48 @@ namespace gp8403 {
|
||||
static const char *const TAG = "gp8403";
|
||||
|
||||
static const uint8_t RANGE_REGISTER = 0x01;
|
||||
static const uint8_t OUTPUT_REGISTER = 0x02;
|
||||
|
||||
void GP8403::setup() { this->write_register(RANGE_REGISTER, (uint8_t *) (&this->voltage_), 1); }
|
||||
const LogString *model_to_string(GP8403Model model) {
|
||||
switch (model) {
|
||||
case GP8403Model::GP8403:
|
||||
return LOG_STR("GP8403");
|
||||
case GP8403Model::GP8413:
|
||||
return LOG_STR("GP8413");
|
||||
}
|
||||
return LOG_STR("Unknown");
|
||||
};
|
||||
|
||||
void GP8403::dump_config() {
|
||||
void GP8403Component::setup() { this->write_register(RANGE_REGISTER, (uint8_t *) (&this->voltage_), 1); }
|
||||
|
||||
void GP8403Component::dump_config() {
|
||||
ESP_LOGCONFIG(TAG,
|
||||
"GP8403:\n"
|
||||
" Voltage: %dV",
|
||||
this->voltage_ == GP8403_VOLTAGE_5V ? 5 : 10);
|
||||
" Voltage: %dV\n"
|
||||
" Model: %s",
|
||||
this->voltage_ == GP8403_VOLTAGE_5V ? 5 : 10, LOG_STR_ARG(model_to_string(this->model_)));
|
||||
LOG_I2C_DEVICE(this);
|
||||
}
|
||||
|
||||
void GP8403Component::write_state(float state, uint8_t channel) {
|
||||
uint16_t val = 0;
|
||||
switch (this->model_) {
|
||||
case GP8403Model::GP8403:
|
||||
val = ((uint16_t) (4095 * state)) << 4;
|
||||
break;
|
||||
case GP8403Model::GP8413:
|
||||
val = ((uint16_t) (32767 * state)) << 1;
|
||||
break;
|
||||
default:
|
||||
ESP_LOGE(TAG, "Unknown model %s", LOG_STR_ARG(model_to_string(this->model_)));
|
||||
return;
|
||||
}
|
||||
ESP_LOGV(TAG, "Calculated DAC value: %" PRIu16, val);
|
||||
i2c::ErrorCode err = this->write_register(OUTPUT_REGISTER + (2 * channel), (uint8_t *) &val, 2);
|
||||
if (err != i2c::ERROR_OK) {
|
||||
ESP_LOGE(TAG, "Error writing to %s, code %d", LOG_STR_ARG(model_to_string(this->model_)), err);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace gp8403
|
||||
} // namespace esphome
|
||||
|
||||
@ -11,15 +11,24 @@ enum GP8403Voltage {
|
||||
GP8403_VOLTAGE_10V = 0x11,
|
||||
};
|
||||
|
||||
class GP8403 : public Component, public i2c::I2CDevice {
|
||||
enum GP8403Model {
|
||||
GP8403,
|
||||
GP8413,
|
||||
};
|
||||
|
||||
class GP8403Component : public Component, public i2c::I2CDevice {
|
||||
public:
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
|
||||
float get_setup_priority() const override { return setup_priority::DATA; }
|
||||
void set_model(GP8403Model model) { this->model_ = model; }
|
||||
void set_voltage(gp8403::GP8403Voltage voltage) { this->voltage_ = voltage; }
|
||||
|
||||
void write_state(float state, uint8_t channel);
|
||||
|
||||
protected:
|
||||
GP8403Voltage voltage_;
|
||||
GP8403Model model_{GP8403Model::GP8403};
|
||||
};
|
||||
|
||||
} // namespace gp8403
|
||||
|
||||
@ -3,7 +3,7 @@ from esphome.components import i2c, output
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_CHANNEL, CONF_ID
|
||||
|
||||
from .. import CONF_GP8403_ID, GP8403, gp8403_ns
|
||||
from .. import CONF_GP8403_ID, GP8403Component, gp8403_ns
|
||||
|
||||
DEPENDENCIES = ["gp8403"]
|
||||
|
||||
@ -14,7 +14,7 @@ GP8403Output = gp8403_ns.class_(
|
||||
CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(GP8403Output),
|
||||
cv.GenerateID(CONF_GP8403_ID): cv.use_id(GP8403),
|
||||
cv.GenerateID(CONF_GP8403_ID): cv.use_id(GP8403Component),
|
||||
cv.Required(CONF_CHANNEL): cv.int_range(min=0, max=1),
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
@ -7,8 +7,6 @@ namespace gp8403 {
|
||||
|
||||
static const char *const TAG = "gp8403.output";
|
||||
|
||||
static const uint8_t OUTPUT_REGISTER = 0x02;
|
||||
|
||||
void GP8403Output::dump_config() {
|
||||
ESP_LOGCONFIG(TAG,
|
||||
"GP8403 Output:\n"
|
||||
@ -16,13 +14,7 @@ void GP8403Output::dump_config() {
|
||||
this->channel_);
|
||||
}
|
||||
|
||||
void GP8403Output::write_state(float state) {
|
||||
uint16_t value = ((uint16_t) (state * 4095)) << 4;
|
||||
i2c::ErrorCode err = this->parent_->write_register(OUTPUT_REGISTER + (2 * this->channel_), (uint8_t *) &value, 2);
|
||||
if (err != i2c::ERROR_OK) {
|
||||
ESP_LOGE(TAG, "Error writing to GP8403, code %d", err);
|
||||
}
|
||||
}
|
||||
void GP8403Output::write_state(float state) { this->parent_->write_state(state, this->channel_); }
|
||||
|
||||
} // namespace gp8403
|
||||
} // namespace esphome
|
||||
|
||||
@ -8,13 +8,11 @@
|
||||
namespace esphome {
|
||||
namespace gp8403 {
|
||||
|
||||
class GP8403Output : public Component, public output::FloatOutput, public Parented<GP8403> {
|
||||
class GP8403Output : public Component, public output::FloatOutput, public Parented<GP8403Component> {
|
||||
public:
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override { return setup_priority::DATA - 1; }
|
||||
|
||||
void set_channel(uint8_t channel) { this->channel_ = channel; }
|
||||
|
||||
void write_state(float state) override;
|
||||
|
||||
protected:
|
||||
|
||||
@ -4,6 +4,11 @@ gp8403:
|
||||
voltage: 5V
|
||||
- id: gp8403_10v
|
||||
i2c_id: i2c_bus
|
||||
model: GP8403
|
||||
voltage: 10V
|
||||
- id: gp8413
|
||||
i2c_id: i2c_bus
|
||||
model: GP8413
|
||||
voltage: 10V
|
||||
|
||||
output:
|
||||
@ -15,3 +20,7 @@ output:
|
||||
gp8403_id: gp8403_10v
|
||||
id: gp8403_output_1
|
||||
channel: 1
|
||||
- platform: gp8403
|
||||
gp8403_id: gp8413
|
||||
id: gp8413_output_2
|
||||
channel: 1
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user