1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-30 06:33:51 +00:00

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
13 changed files with 456 additions and 0 deletions

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