Initial commit

This commit is contained in:
Adrian Schupp 2024-10-17 21:23:49 +02:00
parent 8bbe4efded
commit 0b247c2703
6 changed files with 169 additions and 0 deletions

View file

@ -0,0 +1,39 @@
import esphome.config_validation as cv
import esphome.codegen as cg
from esphome.components import i2c
from esphome.const import CONF_ID, CONF_VOLTAGE
CODEOWNERS = ["@haudamekki"]
DEPENDENCIES = ["i2c"]
MULTI_CONF = True
gp8211_ns = cg.esphome_ns.namespace("gp8211")
GP8211 = gp8211_ns.class_("GP8211", cg.Component, i2c.I2CDevice)
GP8211Voltage = gp8211_ns.enum("GP8211Voltage")
CONF_GP8211_ID = "gp8211_id"
VOLTAGES = {
"5V": GP8211Voltage.GP8211_VOLTAGE_5V,
"10V": GP8211Voltage.GP8211_VOLTAGE_10V,
}
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(GP8211),
cv.Required(CONF_VOLTAGE): cv.enum(VOLTAGES, upper=True),
}
)
.extend(cv.COMPONENT_SCHEMA)
.extend(i2c.i2c_device_schema(0x58)) # GP8211 I2C-Adresse
)
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_voltage(config[CONF_VOLTAGE]))

View file

@ -0,0 +1,24 @@
#include "gp8211.h"
#include "esphome/core/log.h"
namespace esphome {
namespace gp8211 {
static const char *const TAG = "gp8211";
static const uint8_t RANGE_REGISTER = 0x01; // Register für die Spannungsrange
void GP8211::setup() {
// Wähle den Spannungsmodus basierend auf der YAML-Konfiguration
uint8_t voltage_setting = (this->voltage_ == GP8211_VOLTAGE_10V) ? 0x77 : 0x55; // 0x77 für 0-10V, 0x55 für 0-5V
this->write_register(RANGE_REGISTER, &voltage_setting, 1);
}
void GP8211::dump_config() {
ESP_LOGCONFIG(TAG, "GP8211:");
ESP_LOGCONFIG(TAG, " Voltage: %s Mode activated", this->voltage_ == GP8211_VOLTAGE_10V ? "10V" : "5V");
LOG_I2C_DEVICE(this);
}
} // namespace gp8211
} // namespace esphome

View file

@ -0,0 +1,27 @@
#pragma once
#include "esphome/components/i2c/i2c.h"
#include "esphome/core/component.h"
namespace esphome {
namespace gp8211 {
enum GP8211Voltage {
GP8211_VOLTAGE_5V = 0x00,
GP8211_VOLTAGE_10V = 0x11,
};
class GP8211 : 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_voltage(gp8211::GP8211Voltage voltage) { this->voltage_ = voltage; }
protected:
GP8211Voltage voltage_;
};
} // namespace gp8211
} // namespace esphome

View file

@ -0,0 +1,28 @@
import esphome.config_validation as cv
import esphome.codegen as cg
from esphome.components import i2c, output
from esphome.const import CONF_ID
from .. import gp8211_ns, GP8211, CONF_GP8211_ID
DEPENDENCIES = ["gp8211"]
GP8211Output = gp8211_ns.class_(
"GP8211Output", cg.Component, i2c.I2CDevice, output.FloatOutput
)
CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(GP8211Output),
cv.GenerateID(CONF_GP8211_ID): cv.use_id(GP8211),
}
).extend(cv.COMPONENT_SCHEMA)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await output.register_output(var, config)
await cg.register_parented(var, config[CONF_GP8211_ID])

View file

@ -0,0 +1,32 @@
#include "gp8211_output.h"
#include "esphome/core/log.h"
namespace esphome {
namespace gp8211 {
static const char *const TAG = "gp8211.output";
static const uint8_t OUTPUT_REGISTER = 0x02;
void GP8211Output::dump_config() {
ESP_LOGCONFIG(TAG, "GP8211 Output:");
}
void GP8211Output::write_state(float state) {
// Ausgabe des ursprünglichen Helligkeitswerts von der Light-Komponente
ESP_LOGD(TAG, "Original brightness state received from Light component: %.5f", state);
// Konvertiere den state (0.0 bis 1.0) in einen 15-Bit DAC-Wert (0 bis 32767)
uint16_t value = static_cast<uint16_t>(state * 32767);
ESP_LOGD(TAG, "Calculated DAC value: %u", value); // Ausgabe des berechneten Wertes zur Überprüfung
// Sende den Wert an den DAC (2 Bytes)
i2c::ErrorCode err = this->parent_->write_register(OUTPUT_REGISTER, (uint8_t *)&value, 2);
if (err != i2c::ERROR_OK) {
ESP_LOGE(TAG, "Error writing to GP8211, code %d", err);
}
}
} // namespace gp8211
} // namespace esphome

View file

@ -0,0 +1,19 @@
#pragma once
#include "esphome/components/output/float_output.h"
#include "esphome/core/component.h"
#include "esphome/components/gp8211/gp8211.h"
namespace esphome {
namespace gp8211 {
class GP8211Output : public Component, public output::FloatOutput, public Parented<GP8211> {
public:
void dump_config() override;
float get_setup_priority() const override { return setup_priority::DATA - 1; }
void write_state(float state) override;
};
} // namespace gp8211
} // namespace esphome