Add support for SM2235 and SM2335 LED drivers (#3924)

This commit is contained in:
Cossid 2022-12-22 16:04:21 -06:00 committed by GitHub
parent a18ab748fd
commit 53b60ac817
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 456 additions and 0 deletions

View file

@ -208,7 +208,10 @@ esphome/components/shelly_dimmer/* @edge90 @rnauber
esphome/components/sht4x/* @sjtrny
esphome/components/shutdown/* @esphome/core @jsuanet
esphome/components/sim800l/* @glmnet
esphome/components/sm10bit_base/* @Cossid
esphome/components/sm2135/* @BoukeHaarsma23
esphome/components/sm2235/* @Cossid
esphome/components/sm2335/* @Cossid
esphome/components/sml/* @alengwenus
esphome/components/smt100/* @piechade
esphome/components/sn74hc165/* @jesserockz

View file

@ -0,0 +1,44 @@
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,
)
CODEOWNERS = ["@Cossid"]
MULTI_CONF = True
CONF_MAX_POWER_COLOR_CHANNELS = "max_power_color_channels"
CONF_MAX_POWER_WHITE_CHANNELS = "max_power_white_channels"
sm10bit_base_ns = cg.esphome_ns.namespace("sm10bit_base")
Sm10BitBase = sm10bit_base_ns.class_("Sm10BitBase", cg.Component)
SM10BIT_BASE_CONFIG_SCHEMA = cv.Schema(
{
cv.Required(CONF_DATA_PIN): pins.gpio_output_pin_schema,
cv.Required(CONF_CLOCK_PIN): pins.gpio_output_pin_schema,
cv.Optional(CONF_MAX_POWER_COLOR_CHANNELS, default=2): cv.int_range(
min=0, max=15
),
cv.Optional(CONF_MAX_POWER_WHITE_CHANNELS, default=4): cv.int_range(
min=0, max=15
),
}
).extend(cv.COMPONENT_SCHEMA)
async def register_sm10bit_base(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_max_power_color_channels(config[CONF_MAX_POWER_COLOR_CHANNELS]))
cg.add(var.set_max_power_white_channels(config[CONF_MAX_POWER_WHITE_CHANNELS]))
return var

View file

@ -0,0 +1,112 @@
#include "sm10bit_base.h"
#include "esphome/core/log.h"
namespace esphome {
namespace sm10bit_base {
static const char *const TAG = "sm10bit_base";
static const uint8_t SM10BIT_ADDR_STANDBY = 0x0;
static const uint8_t SM10BIT_ADDR_START_3CH = 0x8;
static const uint8_t SM10BIT_ADDR_START_2CH = 0x10;
static const uint8_t SM10BIT_ADDR_START_5CH = 0x18;
// Power current values
// HEX | Binary | RGB level | White level | Config value
// 0x0 | 0000 | RGB 10mA | CW 5mA | 0
// 0x1 | 0001 | RGB 20mA | CW 10mA | 1
// 0x2 | 0010 | RGB 30mA | CW 15mA | 2 - Default spec color value
// 0x3 | 0011 | RGB 40mA | CW 20mA | 3
// 0x4 | 0100 | RGB 50mA | CW 25mA | 4 - Default spec white value
// 0x5 | 0101 | RGB 60mA | CW 30mA | 5
// 0x6 | 0110 | RGB 70mA | CW 35mA | 6
// 0x7 | 0111 | RGB 80mA | CW 40mA | 7
// 0x8 | 1000 | RGB 90mA | CW 45mA | 8
// 0x9 | 1001 | RGB 100mA | CW 50mA | 9
// 0xA | 1010 | RGB 110mA | CW 55mA | 10
// 0xB | 1011 | RGB 120mA | CW 60mA | 11
// 0xC | 1100 | RGB 130mA | CW 65mA | 12
// 0xD | 1101 | RGB 140mA | CW 70mA | 13
// 0xE | 1110 | RGB 150mA | CW 75mA | 14
// 0xF | 1111 | RGB 160mA | CW 80mA | 15
void Sm10BitBase::loop() {
if (!this->update_)
return;
uint8_t data[12];
if (this->pwm_amounts_[0] == 0 && this->pwm_amounts_[1] == 0 && this->pwm_amounts_[2] == 0 &&
this->pwm_amounts_[3] == 0 && this->pwm_amounts_[4] == 0) {
// Off / Sleep
data[0] = this->model_id_ + SM10BIT_ADDR_STANDBY;
for (int i = 1; i < 12; i++)
data[i] = 0;
this->write_buffer_(data, 12);
} else if (this->pwm_amounts_[0] == 0 && this->pwm_amounts_[1] == 0 && this->pwm_amounts_[2] == 0 &&
(this->pwm_amounts_[3] > 0 || this->pwm_amounts_[4] > 0)) {
// Only data on white channels
data[0] = this->model_id_ + SM10BIT_ADDR_START_2CH;
data[1] = 0 << 4 | this->max_power_white_channels_;
for (int i = 2, j = 0; i < 12; i += 2, j++) {
data[i] = this->pwm_amounts_[j] >> 0x8;
data[i + 1] = this->pwm_amounts_[j] & 0xFF;
}
this->write_buffer_(data, 12);
} else if ((this->pwm_amounts_[0] > 0 || this->pwm_amounts_[1] > 0 || this->pwm_amounts_[2] > 0) &&
this->pwm_amounts_[3] == 0 && this->pwm_amounts_[4] == 0) {
// Only data on RGB channels
data[0] = this->model_id_ + SM10BIT_ADDR_START_3CH;
data[1] = this->max_power_color_channels_ << 4 | 0;
for (int i = 2, j = 0; i < 12; i += 2, j++) {
data[i] = this->pwm_amounts_[j] >> 0x8;
data[i + 1] = this->pwm_amounts_[j] & 0xFF;
}
this->write_buffer_(data, 12);
} else {
// All channels
data[0] = this->model_id_ + SM10BIT_ADDR_START_5CH;
data[1] = this->max_power_color_channels_ << 4 | this->max_power_white_channels_;
for (int i = 2, j = 0; i < 12; i += 2, j++) {
data[i] = this->pwm_amounts_[j] >> 0x8;
data[i + 1] = this->pwm_amounts_[j] & 0xFF;
}
this->write_buffer_(data, 12);
}
this->update_ = false;
}
void Sm10BitBase::set_channel_value_(uint8_t channel, uint16_t value) {
if (this->pwm_amounts_[channel] != value) {
this->update_ = true;
this->update_channel_ = channel;
}
this->pwm_amounts_[channel] = value;
}
void Sm10BitBase::write_bit_(bool value) {
this->clock_pin_->digital_write(false);
this->data_pin_->digital_write(value);
this->clock_pin_->digital_write(true);
}
void Sm10BitBase::write_byte_(uint8_t data) {
for (uint8_t mask = 0x80; mask; mask >>= 1) {
this->write_bit_(data & mask);
}
this->clock_pin_->digital_write(false);
this->data_pin_->digital_write(true);
this->clock_pin_->digital_write(true);
}
void Sm10BitBase::write_buffer_(uint8_t *buffer, uint8_t size) {
this->data_pin_->digital_write(false);
for (uint32_t i = 0; i < size; i++) {
this->write_byte_(buffer[i]);
}
this->clock_pin_->digital_write(false);
this->clock_pin_->digital_write(true);
this->data_pin_->digital_write(true);
}
} // namespace sm10bit_base
} // namespace esphome

View file

@ -0,0 +1,63 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/components/output/float_output.h"
#include <vector>
namespace esphome {
namespace sm10bit_base {
class Sm10BitBase : public Component {
public:
class Channel;
void set_model(uint8_t model_id) { model_id_ = model_id; }
void set_data_pin(GPIOPin *data_pin) { data_pin_ = data_pin; }
void set_clock_pin(GPIOPin *clock_pin) { clock_pin_ = clock_pin; }
void set_max_power_color_channels(uint8_t max_power_color_channels) {
max_power_color_channels_ = max_power_color_channels;
}
void set_max_power_white_channels(uint8_t max_power_white_channels) {
max_power_white_channels_ = max_power_white_channels;
}
float get_setup_priority() const override { return setup_priority::HARDWARE; }
void setup() override;
void dump_config() override;
void loop() override;
class Channel : public output::FloatOutput {
public:
void set_parent(Sm10BitBase *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 * 0x3FF);
this->parent_->set_channel_value_(this->channel_, amount);
}
Sm10BitBase *parent_;
uint8_t channel_;
};
protected:
void set_channel_value_(uint8_t channel, uint16_t value);
void write_bit_(bool value);
void write_byte_(uint8_t data);
void write_buffer_(uint8_t *buffer, uint8_t size);
GPIOPin *data_pin_;
GPIOPin *clock_pin_;
uint8_t model_id_;
uint8_t max_power_color_channels_{2};
uint8_t max_power_white_channels_{4};
uint8_t update_channel_;
std::vector<uint16_t> pwm_amounts_;
bool update_{true};
};
} // namespace sm10bit_base
} // namespace esphome

View file

@ -0,0 +1,22 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sm10bit_base
AUTO_LOAD = ["sm10bit_base", "output"]
CODEOWNERS = ["@Cossid"]
MULTI_CONF = True
sm2235_ns = cg.esphome_ns.namespace("sm2235")
SM2235 = sm2235_ns.class_("SM2235", sm10bit_base.Sm10BitBase)
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(SM2235),
}
).extend(sm10bit_base.SM10BIT_BASE_CONFIG_SCHEMA)
async def to_code(config):
var = await sm10bit_base.register_sm10bit_base(config)
cg.add(var.set_model(0xC0))

View file

@ -0,0 +1,28 @@
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 SM2235
DEPENDENCIES = ["sm2235"]
CODEOWNERS = ["@Cossid"]
Channel = SM2235.class_("Channel", output.FloatOutput)
CONF_SM2235_ID = "sm2235_id"
CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
{
cv.GenerateID(CONF_SM2235_ID): cv.use_id(SM2235),
cv.Required(CONF_ID): cv.declare_id(Channel),
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)
parent = await cg.get_variable(config[CONF_SM2235_ID])
cg.add(var.set_parent(parent))
cg.add(var.set_channel(config[CONF_CHANNEL]))

View file

@ -0,0 +1,27 @@
#include "sm2235.h"
#include "esphome/core/log.h"
namespace esphome {
namespace sm2235 {
static const char *const TAG = "sm2235";
void SM2235::setup() {
ESP_LOGCONFIG(TAG, "Setting up sm2235 Output Component...");
this->data_pin_->setup();
this->data_pin_->digital_write(true);
this->clock_pin_->setup();
this->clock_pin_->digital_write(true);
this->pwm_amounts_.resize(5, 0);
}
void SM2235::dump_config() {
ESP_LOGCONFIG(TAG, "sm2235:");
LOG_PIN(" Data Pin: ", this->data_pin_);
LOG_PIN(" Clock Pin: ", this->clock_pin_);
ESP_LOGCONFIG(TAG, " Color Channels Max Power: %u", this->max_power_color_channels_);
ESP_LOGCONFIG(TAG, " White Channels Max Power: %u", this->max_power_white_channels_);
}
} // namespace sm2235
} // namespace esphome

View file

@ -0,0 +1,19 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sm10bit_base/sm10bit_base.h"
#include "esphome/core/hal.h"
namespace esphome {
namespace sm2235 {
class SM2235 : public sm10bit_base::Sm10BitBase {
public:
SM2235() = default;
void setup() override;
void dump_config() override;
};
} // namespace sm2235
} // namespace esphome

View file

@ -0,0 +1,22 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sm10bit_base
AUTO_LOAD = ["sm10bit_base", "output"]
CODEOWNERS = ["@Cossid"]
MULTI_CONF = True
sm2335_ns = cg.esphome_ns.namespace("sm2335")
SM2335 = sm2335_ns.class_("SM2335", sm10bit_base.Sm10BitBase)
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(SM2335),
}
).extend(sm10bit_base.SM10BIT_BASE_CONFIG_SCHEMA)
async def to_code(config):
var = await sm10bit_base.register_sm10bit_base(config)
cg.add(var.set_model(0xC0))

View file

@ -0,0 +1,28 @@
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 SM2335
DEPENDENCIES = ["sm2335"]
CODEOWNERS = ["@Cossid"]
Channel = SM2335.class_("Channel", output.FloatOutput)
CONF_SM2335_ID = "sm2335_id"
CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
{
cv.GenerateID(CONF_SM2335_ID): cv.use_id(SM2335),
cv.Required(CONF_ID): cv.declare_id(Channel),
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)
parent = await cg.get_variable(config[CONF_SM2335_ID])
cg.add(var.set_parent(parent))
cg.add(var.set_channel(config[CONF_CHANNEL]))

View file

@ -0,0 +1,27 @@
#include "sm2335.h"
#include "esphome/core/log.h"
namespace esphome {
namespace sm2335 {
static const char *const TAG = "sm2335";
void SM2335::setup() {
ESP_LOGCONFIG(TAG, "Setting up sm2335 Output Component...");
this->data_pin_->setup();
this->data_pin_->digital_write(true);
this->clock_pin_->setup();
this->clock_pin_->digital_write(true);
this->pwm_amounts_.resize(5, 0);
}
void SM2335::dump_config() {
ESP_LOGCONFIG(TAG, "sm2335:");
LOG_PIN(" Data Pin: ", this->data_pin_);
LOG_PIN(" Clock Pin: ", this->clock_pin_);
ESP_LOGCONFIG(TAG, " Color Channels Max Power: %u", this->max_power_color_channels_);
ESP_LOGCONFIG(TAG, " White Channels Max Power: %u", this->max_power_white_channels_);
}
} // namespace sm2335
} // namespace esphome

View file

@ -0,0 +1,19 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sm10bit_base/sm10bit_base.h"
#include "esphome/core/hal.h"
namespace esphome {
namespace sm2335 {
class SM2335 : public sm10bit_base::Sm10BitBase {
public:
SM2335() = default;
void setup() override;
void dump_config() override;
};
} // namespace sm2335
} // namespace esphome

View file

@ -1465,6 +1465,18 @@ my9231:
num_chips: 2
bit_depth: 16
sm2235:
data_pin: GPIO4
clock_pin: GPIO5
max_power_color_channels: 9
max_power_white_channels: 9
sm2335:
data_pin: GPIO4
clock_pin: GPIO5
max_power_color_channels: 9
max_power_white_channels: 9
bp1658cj:
data_pin: GPIO3
clock_pin: GPIO5
@ -1600,6 +1612,36 @@ output:
- platform: my9231
id: my_5
channel: 5
- platform: sm2235
id: sm2235_red
channel: 1
- platform: sm2235
id: sm2235_green
channel: 0
- platform: sm2235
id: sm2235_blue
channel: 2
- platform: sm2235
id: sm2235_coldwhite
channel: 4
- platform: sm2235
id: sm2235_warmwhite
channel: 3
- platform: sm2335
id: sm2335_red
channel: 1
- platform: sm2335
id: sm2335_green
channel: 0
- platform: sm2335
id: sm2335_blue
channel: 2
- platform: sm2335
id: sm2335_coldwhite
channel: 4
- platform: sm2335
id: sm2335_warmwhite
channel: 3
- platform: slow_pwm
id: id24
pin: GPIO26