mirror of
https://github.com/esphome/esphome.git
synced 2024-11-21 22:48:10 +01:00
Rework tlc5947 to remove AUTO_LOAD (#6503)
This commit is contained in:
parent
c66b2c52c1
commit
12aa272234
13 changed files with 107 additions and 35 deletions
|
@ -14,7 +14,6 @@ from esphome.const import (
|
|||
CONF_LAT_PIN = "lat_pin"
|
||||
CONF_OE_PIN = "oe_pin"
|
||||
|
||||
AUTO_LOAD = ["output"]
|
||||
CODEOWNERS = ["@rnauber"]
|
||||
|
||||
tlc5947_ns = cg.esphome_ns.namespace("tlc5947")
|
||||
|
|
|
@ -2,18 +2,19 @@ import esphome.codegen as cg
|
|||
import esphome.config_validation as cv
|
||||
from esphome.components import output
|
||||
from esphome.const import CONF_CHANNEL, CONF_ID
|
||||
from . import TLC5947
|
||||
from .. import TLC5947, tlc5947_ns
|
||||
|
||||
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"
|
||||
CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
|
||||
{
|
||||
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),
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA)
|
||||
|
@ -22,7 +23,5 @@ CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
|
|||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await output.register_output(var, config)
|
||||
|
||||
parent = await cg.get_variable(config[CONF_TLC5947_ID])
|
||||
cg.add(var.set_parent(parent))
|
||||
await cg.register_parented(var, config[CONF_TLC5947_ID])
|
||||
cg.add(var.set_channel(config[CONF_CHANNEL]))
|
12
esphome/components/tlc5947/output/tlc5947_output.cpp
Normal file
12
esphome/components/tlc5947/output/tlc5947_output.cpp
Normal 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
|
22
esphome/components/tlc5947/output/tlc5947_output.h
Normal file
22
esphome/components/tlc5947/output/tlc5947_output.h
Normal 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
|
|
@ -60,5 +60,14 @@ void TLC5947::loop() {
|
|||
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 esphome
|
||||
|
|
|
@ -2,18 +2,16 @@
|
|||
// TLC5947 24-Channel, 12-Bit PWM LED Driver
|
||||
// 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/hal.h"
|
||||
#include "esphome/components/output/float_output.h"
|
||||
#include <vector>
|
||||
|
||||
namespace esphome {
|
||||
namespace tlc5947 {
|
||||
|
||||
class TLC5947 : public Component {
|
||||
public:
|
||||
class Channel;
|
||||
|
||||
const uint8_t N_CHANNELS_PER_CHIP = 24;
|
||||
|
||||
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.
|
||||
void loop() override;
|
||||
|
||||
class Channel : public output::FloatOutput {
|
||||
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_;
|
||||
};
|
||||
void set_channel_value(uint16_t channel, uint16_t value);
|
||||
|
||||
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 *clock_pin_;
|
||||
GPIOPin *lat_pin_;
|
||||
|
|
13
tests/components/tlc5947/common.yaml
Normal file
13
tests/components/tlc5947/common.yaml
Normal 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
|
7
tests/components/tlc5947/test.esp32-c3-idf.yaml
Normal file
7
tests/components/tlc5947/test.esp32-c3-idf.yaml
Normal file
|
@ -0,0 +1,7 @@
|
|||
substitutions:
|
||||
clock_pin: GPIO5
|
||||
data_pin: GPIO4
|
||||
lat_pin: GPIO3
|
||||
|
||||
packages:
|
||||
common: !include common.yaml
|
7
tests/components/tlc5947/test.esp32-c3.yaml
Normal file
7
tests/components/tlc5947/test.esp32-c3.yaml
Normal file
|
@ -0,0 +1,7 @@
|
|||
substitutions:
|
||||
clock_pin: GPIO5
|
||||
data_pin: GPIO4
|
||||
lat_pin: GPIO3
|
||||
|
||||
packages:
|
||||
common: !include common.yaml
|
7
tests/components/tlc5947/test.esp32-idf.yaml
Normal file
7
tests/components/tlc5947/test.esp32-idf.yaml
Normal file
|
@ -0,0 +1,7 @@
|
|||
substitutions:
|
||||
clock_pin: GPIO15
|
||||
data_pin: GPIO14
|
||||
lat_pin: GPIO13
|
||||
|
||||
packages:
|
||||
common: !include common.yaml
|
7
tests/components/tlc5947/test.esp32.yaml
Normal file
7
tests/components/tlc5947/test.esp32.yaml
Normal file
|
@ -0,0 +1,7 @@
|
|||
substitutions:
|
||||
clock_pin: GPIO15
|
||||
data_pin: GPIO14
|
||||
lat_pin: GPIO13
|
||||
|
||||
packages:
|
||||
common: !include common.yaml
|
7
tests/components/tlc5947/test.esp8266.yaml
Normal file
7
tests/components/tlc5947/test.esp8266.yaml
Normal file
|
@ -0,0 +1,7 @@
|
|||
substitutions:
|
||||
clock_pin: GPIO5
|
||||
data_pin: GPIO4
|
||||
lat_pin: GPIO13
|
||||
|
||||
packages:
|
||||
common: !include common.yaml
|
7
tests/components/tlc5947/test.rp2040.yaml
Normal file
7
tests/components/tlc5947/test.rp2040.yaml
Normal file
|
@ -0,0 +1,7 @@
|
|||
substitutions:
|
||||
clock_pin: GPIO5
|
||||
data_pin: GPIO4
|
||||
lat_pin: GPIO3
|
||||
|
||||
packages:
|
||||
common: !include common.yaml
|
Loading…
Reference in a new issue