Rework tlc5947 to remove AUTO_LOAD (#6503)

This commit is contained in:
Jesse Hills 2024-04-09 15:51:54 +12:00 committed by GitHub
parent c66b2c52c1
commit 12aa272234
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 107 additions and 35 deletions

View file

@ -14,7 +14,6 @@ from esphome.const import (
CONF_LAT_PIN = "lat_pin" CONF_LAT_PIN = "lat_pin"
CONF_OE_PIN = "oe_pin" CONF_OE_PIN = "oe_pin"
AUTO_LOAD = ["output"]
CODEOWNERS = ["@rnauber"] CODEOWNERS = ["@rnauber"]
tlc5947_ns = cg.esphome_ns.namespace("tlc5947") tlc5947_ns = cg.esphome_ns.namespace("tlc5947")

View file

@ -2,18 +2,19 @@ import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.components import output from esphome.components import output
from esphome.const import CONF_CHANNEL, CONF_ID from esphome.const import CONF_CHANNEL, CONF_ID
from . import TLC5947 from .. import TLC5947, tlc5947_ns
DEPENDENCIES = ["tlc5947"] DEPENDENCIES = ["tlc5947"]
CODEOWNERS = ["@rnauber"]
Channel = TLC5947.class_("Channel", output.FloatOutput) TLC5947Channel = tlc5947_ns.class_(
"TLC5947Channel", output.FloatOutput, cg.Parented.template(TLC5947)
)
CONF_TLC5947_ID = "tlc5947_id" CONF_TLC5947_ID = "tlc5947_id"
CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend( CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
{ {
cv.GenerateID(CONF_TLC5947_ID): cv.use_id(TLC5947), cv.GenerateID(CONF_TLC5947_ID): cv.use_id(TLC5947),
cv.Required(CONF_ID): cv.declare_id(Channel), cv.Required(CONF_ID): cv.declare_id(TLC5947Channel),
cv.Required(CONF_CHANNEL): cv.int_range(min=0, max=65535), cv.Required(CONF_CHANNEL): cv.int_range(min=0, max=65535),
} }
).extend(cv.COMPONENT_SCHEMA) ).extend(cv.COMPONENT_SCHEMA)
@ -22,7 +23,5 @@ CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
async def to_code(config): async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
await output.register_output(var, config) await output.register_output(var, config)
await cg.register_parented(var, config[CONF_TLC5947_ID])
parent = await cg.get_variable(config[CONF_TLC5947_ID])
cg.add(var.set_parent(parent))
cg.add(var.set_channel(config[CONF_CHANNEL])) cg.add(var.set_channel(config[CONF_CHANNEL]))

View file

@ -0,0 +1,12 @@
#include "tlc5947_output.h"
namespace esphome {
namespace tlc5947 {
void TLC5947Channel::write_state(float state) {
auto amount = static_cast<uint16_t>(state * 0xfff);
this->parent_->set_channel_value(this->channel_, amount);
}
} // namespace tlc5947
} // namespace esphome

View file

@ -0,0 +1,22 @@
#pragma once
#include "esphome/core/helpers.h"
#include "esphome/components/output/float_output.h"
#include "../tlc5947.h"
namespace esphome {
namespace tlc5947 {
class TLC5947Channel : public output::FloatOutput, public Parented<TLC5947> {
public:
void set_channel(uint8_t channel) { this->channel_ = channel; }
protected:
void write_state(float state) override;
uint8_t channel_;
};
} // namespace tlc5947
} // namespace esphome

View file

@ -60,5 +60,14 @@ void TLC5947::loop() {
this->update_ = false; this->update_ = false;
} }
void TLC5947::set_channel_value(uint16_t channel, uint16_t value) {
if (channel >= this->num_chips_ * N_CHANNELS_PER_CHIP)
return;
if (this->pwm_amounts_[channel] != value) {
this->update_ = true;
}
this->pwm_amounts_[channel] = value;
}
} // namespace tlc5947 } // namespace tlc5947
} // namespace esphome } // namespace esphome

View file

@ -2,18 +2,16 @@
// TLC5947 24-Channel, 12-Bit PWM LED Driver // TLC5947 24-Channel, 12-Bit PWM LED Driver
// https://www.ti.com/lit/ds/symlink/tlc5947.pdf // https://www.ti.com/lit/ds/symlink/tlc5947.pdf
#include <vector>
#include "esphome/components/output/float_output.h"
#include "esphome/core/component.h" #include "esphome/core/component.h"
#include "esphome/core/hal.h" #include "esphome/core/hal.h"
#include "esphome/components/output/float_output.h"
#include <vector>
namespace esphome { namespace esphome {
namespace tlc5947 { namespace tlc5947 {
class TLC5947 : public Component { class TLC5947 : public Component {
public: public:
class Channel;
const uint8_t N_CHANNELS_PER_CHIP = 24; const uint8_t N_CHANNELS_PER_CHIP = 24;
void set_data_pin(GPIOPin *data_pin) { data_pin_ = data_pin; } void set_data_pin(GPIOPin *data_pin) { data_pin_ = data_pin; }
@ -31,31 +29,9 @@ class TLC5947 : public Component {
/// Send new values if they were updated. /// Send new values if they were updated.
void loop() override; void loop() override;
class Channel : public output::FloatOutput { void set_channel_value(uint16_t channel, uint16_t value);
public:
void set_parent(TLC5947 *parent) { parent_ = parent; }
void set_channel(uint8_t channel) { channel_ = channel; }
protected:
void write_state(float state) override {
auto amount = static_cast<uint16_t>(state * 0xfff);
this->parent_->set_channel_value_(this->channel_, amount);
}
TLC5947 *parent_;
uint8_t channel_;
};
protected: protected:
void set_channel_value_(uint16_t channel, uint16_t value) {
if (channel >= this->num_chips_ * N_CHANNELS_PER_CHIP)
return;
if (this->pwm_amounts_[channel] != value) {
this->update_ = true;
}
this->pwm_amounts_[channel] = value;
}
GPIOPin *data_pin_; GPIOPin *data_pin_;
GPIOPin *clock_pin_; GPIOPin *clock_pin_;
GPIOPin *lat_pin_; GPIOPin *lat_pin_;

View file

@ -0,0 +1,13 @@
tlc5947:
clock_pin: ${clock_pin}
data_pin: ${data_pin}
lat_pin: ${lat_pin}
output:
- platform: tlc5947
id: output_1
channel: 0
max_power: 0.8
- platform: tlc5947
id: output_2
channel: 1

View file

@ -0,0 +1,7 @@
substitutions:
clock_pin: GPIO5
data_pin: GPIO4
lat_pin: GPIO3
packages:
common: !include common.yaml

View file

@ -0,0 +1,7 @@
substitutions:
clock_pin: GPIO5
data_pin: GPIO4
lat_pin: GPIO3
packages:
common: !include common.yaml

View file

@ -0,0 +1,7 @@
substitutions:
clock_pin: GPIO15
data_pin: GPIO14
lat_pin: GPIO13
packages:
common: !include common.yaml

View file

@ -0,0 +1,7 @@
substitutions:
clock_pin: GPIO15
data_pin: GPIO14
lat_pin: GPIO13
packages:
common: !include common.yaml

View file

@ -0,0 +1,7 @@
substitutions:
clock_pin: GPIO5
data_pin: GPIO4
lat_pin: GPIO13
packages:
common: !include common.yaml

View file

@ -0,0 +1,7 @@
substitutions:
clock_pin: GPIO5
data_pin: GPIO4
lat_pin: GPIO3
packages:
common: !include common.yaml