Implemented support for the TLC5971 as an output component (#6494)

This commit is contained in:
IJssel 2024-04-09 22:03:18 +02:00 committed by GitHub
parent 857b8ef363
commit 3adfed3675
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 303 additions and 0 deletions

View File

@ -364,6 +364,7 @@ esphome/components/text/* @mauritskorse
esphome/components/thermostat/* @kbx81 esphome/components/thermostat/* @kbx81
esphome/components/time/* @OttoWinter esphome/components/time/* @OttoWinter
esphome/components/tlc5947/* @rnauber esphome/components/tlc5947/* @rnauber
esphome/components/tlc5971/* @IJIJI
esphome/components/tm1621/* @Philippe12 esphome/components/tm1621/* @Philippe12
esphome/components/tm1637/* @glmnet esphome/components/tm1637/* @glmnet
esphome/components/tm1638/* @skykingjwc esphome/components/tm1638/* @skykingjwc

View File

@ -0,0 +1,40 @@
# this component is for the "TLC5971 12-Channel, 12-Bit PWM LED Driver" [https://www.ti.com/lit/ds/symlink/tlc5971.pdf],
# which is used e.g. on [https://www.adafruit.com/product/1455]. The code is based on the TLC5947 component by @rnauber.
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome.const import (
CONF_CLOCK_PIN,
CONF_DATA_PIN,
CONF_ID,
CONF_NUM_CHIPS,
)
CODEOWNERS = ["@IJIJI"]
tlc5971_ns = cg.esphome_ns.namespace("tlc5971")
TLC5971 = tlc5971_ns.class_("TLC5971", cg.Component)
MULTI_CONF = True
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(TLC5971),
cv.Required(CONF_DATA_PIN): pins.gpio_output_pin_schema,
cv.Required(CONF_CLOCK_PIN): pins.gpio_output_pin_schema,
cv.Optional(CONF_NUM_CHIPS, default=1): cv.int_range(min=1, max=85),
}
).extend(cv.COMPONENT_SCHEMA)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
data = await cg.gpio_pin_expression(config[CONF_DATA_PIN])
cg.add(var.set_data_pin(data))
clock = await cg.gpio_pin_expression(config[CONF_CLOCK_PIN])
cg.add(var.set_clock_pin(clock))
cg.add(var.set_num_chips(config[CONF_NUM_CHIPS]))

View File

@ -0,0 +1,27 @@
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 TLC5971, tlc5971_ns
DEPENDENCIES = ["tlc5971"]
TLC5971Channel = tlc5971_ns.class_(
"TLC5971Channel", output.FloatOutput, cg.Parented.template(TLC5971)
)
CONF_TLC5971_ID = "tlc5971_id"
CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
{
cv.GenerateID(CONF_TLC5971_ID): cv.use_id(TLC5971),
cv.Required(CONF_ID): cv.declare_id(TLC5971Channel),
cv.Required(CONF_CHANNEL): cv.int_range(min=0, max=65535),
}
).extend(cv.COMPONENT_SCHEMA)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await output.register_output(var, config)
await cg.register_parented(var, config[CONF_TLC5971_ID])
cg.add(var.set_channel(config[CONF_CHANNEL]))

View File

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

View File

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

View File

@ -0,0 +1,101 @@
#include "tlc5971.h"
#include "esphome/core/log.h"
namespace esphome {
namespace tlc5971 {
static const char *const TAG = "tlc5971";
void TLC5971::setup() {
this->data_pin_->setup();
this->data_pin_->digital_write(true);
this->clock_pin_->setup();
this->clock_pin_->digital_write(true);
this->pwm_amounts_.resize(this->num_chips_ * N_CHANNELS_PER_CHIP, 0);
ESP_LOGCONFIG(TAG, "Done setting up TLC5971 output component.");
}
void TLC5971::dump_config() {
ESP_LOGCONFIG(TAG, "TLC5971:");
LOG_PIN(" Data Pin: ", this->data_pin_);
LOG_PIN(" Clock Pin: ", this->clock_pin_);
ESP_LOGCONFIG(TAG, " Number of chips: %u", this->num_chips_);
}
void TLC5971::loop() {
if (!this->update_)
return;
uint32_t command;
// Magic word for write
command = 0x25;
command <<= 5;
// OUTTMG = 1, EXTGCK = 0, TMGRST = 1, DSPRPT = 1, BLANK = 0 -> 0x16
command |= 0x16;
command <<= 7;
command |= 0x7F; // default 100% brigthness
command <<= 7;
command |= 0x7F; // default 100% brigthness
command <<= 7;
command |= 0x7F; // default 100% brigthness
for (uint8_t n = 0; n < num_chips_; n++) {
this->transfer_(command >> 24);
this->transfer_(command >> 16);
this->transfer_(command >> 8);
this->transfer_(command);
// 12 channels per TLC59711
for (int8_t c = 11; c >= 0; c--) {
// 16 bits per channel, send MSB first
this->transfer_(pwm_amounts_.at(n * 12 + c) >> 8);
this->transfer_(pwm_amounts_.at(n * 12 + c));
}
}
this->update_ = false;
}
void TLC5971::transfer_(uint8_t send) {
uint8_t startbit = 0x80;
bool towrite, lastmosi = !(send & startbit);
uint8_t bitdelay_us = (1000000 / 1000000) / 2;
for (uint8_t b = startbit; b != 0; b = b >> 1) {
if (bitdelay_us) {
delayMicroseconds(bitdelay_us);
}
towrite = send & b;
if ((lastmosi != towrite)) {
this->data_pin_->digital_write(towrite);
lastmosi = towrite;
}
this->clock_pin_->digital_write(true);
if (bitdelay_us) {
delayMicroseconds(bitdelay_us);
}
this->clock_pin_->digital_write(false);
}
}
void TLC5971::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 tlc5971
} // namespace esphome

View File

@ -0,0 +1,43 @@
#pragma once
// TLC5971 12-Channel, 16-Bit PWM LED Driver
// https://www.ti.com/lit/ds/symlink/tlc5971.pdf
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/components/output/float_output.h"
#include <vector>
namespace esphome {
namespace tlc5971 {
class TLC5971 : public Component {
public:
const uint8_t N_CHANNELS_PER_CHIP = 12;
void set_data_pin(GPIOPin *data_pin) { data_pin_ = data_pin; }
void set_clock_pin(GPIOPin *clock_pin) { clock_pin_ = clock_pin; }
void set_num_chips(uint8_t num_chips) { num_chips_ = num_chips; }
void setup() override;
void dump_config() override;
float get_setup_priority() const override { return setup_priority::HARDWARE; }
/// Send new values if they were updated.
void loop() override;
void set_channel_value(uint16_t channel, uint16_t value);
protected:
void transfer_(uint8_t send);
GPIOPin *data_pin_;
GPIOPin *clock_pin_;
uint8_t num_chips_;
std::vector<uint16_t> pwm_amounts_;
bool update_{true};
};
} // namespace tlc5971
} // namespace esphome

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,6 @@
substitutions:
clock_pin: GPIO36
data_pin: GPIO35
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: GPIO3
packages:
common: !include common.yaml