mirror of
https://github.com/esphome/esphome.git
synced 2024-12-22 13:34:54 +01:00
Add support for BP5758D LED driver (#4021)
This commit is contained in:
parent
7e376ae952
commit
917488bbc3
6 changed files with 298 additions and 0 deletions
|
@ -41,6 +41,7 @@ esphome/components/ble_client/* @buxtronix
|
|||
esphome/components/bluetooth_proxy/* @jesserockz
|
||||
esphome/components/bme680_bsec/* @trvrnrth
|
||||
esphome/components/bmp3xx/* @martgras
|
||||
esphome/components/bp5758d/* @Cossid
|
||||
esphome/components/button/* @esphome/core
|
||||
esphome/components/canbus/* @danielschramm @mvturnho
|
||||
esphome/components/cap1188/* @MrEditor97
|
||||
|
|
33
esphome/components/bp5758d/__init__.py
Normal file
33
esphome/components/bp5758d/__init__.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
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
|
||||
|
||||
AUTO_LOAD = ["output"]
|
||||
bp5758d_ns = cg.esphome_ns.namespace("bp5758d")
|
||||
BP5758D = bp5758d_ns.class_("BP5758D", cg.Component)
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(BP5758D),
|
||||
cv.Required(CONF_DATA_PIN): pins.gpio_output_pin_schema,
|
||||
cv.Required(CONF_CLOCK_PIN): pins.gpio_output_pin_schema,
|
||||
}
|
||||
).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))
|
147
esphome/components/bp5758d/bp5758d.cpp
Normal file
147
esphome/components/bp5758d/bp5758d.cpp
Normal file
|
@ -0,0 +1,147 @@
|
|||
#include "bp5758d.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace bp5758d {
|
||||
|
||||
static const char *const TAG = "bp5758d";
|
||||
|
||||
static const uint8_t BP5758D_MODEL_ID = 0b10000000;
|
||||
static const uint8_t BP5758D_ADDR_STANDBY = 0b00000000;
|
||||
// Note, channel start address seems ambiguous and mis-translated.
|
||||
// Documentation states all are "invalid sleep"
|
||||
// All 3 values appear to activate all 5 channels. Using the mapping
|
||||
// from BP1658CJ ordering since it won't break anything.
|
||||
static const uint8_t BP5758D_ADDR_START_3CH = 0b00010000;
|
||||
static const uint8_t BP5758D_ADDR_START_2CH = 0b00100000;
|
||||
static const uint8_t BP5758D_ADDR_START_5CH = 0b00110000;
|
||||
static const uint8_t BP5758D_ALL_DATA_CHANNEL_ENABLEMENT = 0b00011111;
|
||||
|
||||
void BP5758D::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up BP5758D Output Component...");
|
||||
this->data_pin_->setup();
|
||||
this->data_pin_->digital_write(false);
|
||||
this->clock_pin_->setup();
|
||||
this->clock_pin_->digital_write(false);
|
||||
this->channel_current_.resize(5, 0);
|
||||
this->pwm_amounts_.resize(5, 0);
|
||||
}
|
||||
void BP5758D::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "BP5758D:");
|
||||
LOG_PIN(" Data Pin: ", this->data_pin_);
|
||||
LOG_PIN(" Clock Pin: ", this->clock_pin_);
|
||||
}
|
||||
|
||||
void BP5758D::loop() {
|
||||
if (!this->update_)
|
||||
return;
|
||||
|
||||
uint8_t data[17];
|
||||
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] = BP5758D_MODEL_ID + BP5758D_ADDR_STANDBY;
|
||||
for (int i = 1; i < 16; i++)
|
||||
data[i] = 0;
|
||||
this->write_buffer_(data, 17);
|
||||
} 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] = BP5758D_MODEL_ID + BP5758D_ADDR_START_2CH;
|
||||
data[1] = BP5758D_ALL_DATA_CHANNEL_ENABLEMENT;
|
||||
data[2] = 0;
|
||||
data[3] = 0;
|
||||
data[4] = 0;
|
||||
data[5] = this->pwm_amounts_[3] > 0 ? correct_current_level_bits_(this->channel_current_[3]) : 0;
|
||||
data[6] = this->pwm_amounts_[4] > 0 ? correct_current_level_bits_(this->channel_current_[4]) : 0;
|
||||
for (int i = 7, j = 0; i <= 15; i += 2, j++) {
|
||||
data[i] = this->pwm_amounts_[j] & 0x1F;
|
||||
data[i + 1] = (this->pwm_amounts_[j] >> 5) & 0x1F;
|
||||
}
|
||||
this->write_buffer_(data, 17);
|
||||
} 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] = BP5758D_MODEL_ID + BP5758D_ADDR_START_3CH;
|
||||
data[1] = BP5758D_ALL_DATA_CHANNEL_ENABLEMENT;
|
||||
data[2] = this->pwm_amounts_[0] > 0 ? correct_current_level_bits_(this->channel_current_[0]) : 0;
|
||||
data[3] = this->pwm_amounts_[1] > 0 ? correct_current_level_bits_(this->channel_current_[1]) : 0;
|
||||
data[4] = this->pwm_amounts_[2] > 0 ? correct_current_level_bits_(this->channel_current_[2]) : 0;
|
||||
data[5] = 0;
|
||||
data[6] = 0;
|
||||
for (int i = 7, j = 0; i <= 15; i += 2, j++) {
|
||||
data[i] = this->pwm_amounts_[j] & 0x1F;
|
||||
data[i + 1] = (this->pwm_amounts_[j] >> 5) & 0x1F;
|
||||
}
|
||||
this->write_buffer_(data, 17);
|
||||
} else {
|
||||
// All channels
|
||||
data[0] = BP5758D_MODEL_ID + BP5758D_ADDR_START_5CH;
|
||||
data[1] = BP5758D_ALL_DATA_CHANNEL_ENABLEMENT;
|
||||
data[2] = this->pwm_amounts_[0] > 0 ? correct_current_level_bits_(this->channel_current_[0]) : 0;
|
||||
data[3] = this->pwm_amounts_[1] > 0 ? correct_current_level_bits_(this->channel_current_[1]) : 0;
|
||||
data[4] = this->pwm_amounts_[2] > 0 ? correct_current_level_bits_(this->channel_current_[2]) : 0;
|
||||
data[5] = this->pwm_amounts_[3] > 0 ? correct_current_level_bits_(this->channel_current_[3]) : 0;
|
||||
data[6] = this->pwm_amounts_[4] > 0 ? correct_current_level_bits_(this->channel_current_[4]) : 0;
|
||||
for (int i = 7, j = 0; i <= 15; i += 2, j++) {
|
||||
data[i] = this->pwm_amounts_[j] & 0x1F;
|
||||
data[i + 1] = (this->pwm_amounts_[j] >> 5) & 0x1F;
|
||||
}
|
||||
this->write_buffer_(data, 17);
|
||||
}
|
||||
|
||||
this->update_ = false;
|
||||
}
|
||||
|
||||
uint8_t BP5758D::correct_current_level_bits_(uint8_t current) {
|
||||
// Anything below 64 uses normal bitmapping.
|
||||
if (current < 64) {
|
||||
return current;
|
||||
}
|
||||
|
||||
// Anything above 63 needs to be offset by +34 because the driver remaps bit 7 (normally 64) to 30.
|
||||
// (no idea why(!) but it is documented)
|
||||
// Example:
|
||||
// integer 64 would normally put out 0b01000000 but here 0b01000000 = 30 whereas everything lower
|
||||
// is normal, so we add 34 to the integer where
|
||||
// integer 98 = 0b01100010 which is 30 (7th bit adjusted) + 34 (1st-6th bits).
|
||||
return current + 34;
|
||||
}
|
||||
|
||||
void BP5758D::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 BP5758D::set_channel_current_(uint8_t channel, uint8_t current) { this->channel_current_[channel] = current; }
|
||||
|
||||
void BP5758D::write_bit_(bool value) {
|
||||
this->clock_pin_->digital_write(false);
|
||||
this->data_pin_->digital_write(value);
|
||||
this->clock_pin_->digital_write(true);
|
||||
}
|
||||
|
||||
void BP5758D::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 BP5758D::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 bp5758d
|
||||
} // namespace esphome
|
64
esphome/components/bp5758d/bp5758d.h
Normal file
64
esphome/components/bp5758d/bp5758d.h
Normal file
|
@ -0,0 +1,64 @@
|
|||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/components/output/float_output.h"
|
||||
#include <vector>
|
||||
|
||||
namespace esphome {
|
||||
namespace bp5758d {
|
||||
|
||||
class BP5758D : public Component {
|
||||
public:
|
||||
class Channel;
|
||||
|
||||
void set_data_pin(GPIOPin *data_pin) { data_pin_ = data_pin; }
|
||||
void set_clock_pin(GPIOPin *clock_pin) { clock_pin_ = clock_pin; }
|
||||
|
||||
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;
|
||||
|
||||
class Channel : public output::FloatOutput {
|
||||
public:
|
||||
void set_parent(BP5758D *parent) { parent_ = parent; }
|
||||
void set_channel(uint8_t channel) { channel_ = channel; }
|
||||
void set_current(uint8_t current) { current_ = current; }
|
||||
|
||||
protected:
|
||||
void write_state(float state) override {
|
||||
auto amount = static_cast<uint16_t>(state * 0x3FF);
|
||||
// We're enforcing channels start at 1 to mach OUT1-OUT5, we must adjust
|
||||
// to our 0-based array internally here by subtracting 1.
|
||||
this->parent_->set_channel_value_(this->channel_ - 1, amount);
|
||||
this->parent_->set_channel_current_(this->channel_ - 1, this->current_);
|
||||
}
|
||||
|
||||
BP5758D *parent_;
|
||||
uint8_t channel_;
|
||||
uint8_t current_;
|
||||
};
|
||||
|
||||
protected:
|
||||
uint8_t correct_current_level_bits_(uint8_t current);
|
||||
void set_channel_value_(uint8_t channel, uint16_t value);
|
||||
void set_channel_current_(uint8_t channel, uint8_t current);
|
||||
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 update_channel_;
|
||||
std::vector<uint8_t> channel_current_;
|
||||
std::vector<uint16_t> pwm_amounts_;
|
||||
bool update_{true};
|
||||
};
|
||||
|
||||
} // namespace bp5758d
|
||||
} // namespace esphome
|
29
esphome/components/bp5758d/output.py
Normal file
29
esphome/components/bp5758d/output.py
Normal file
|
@ -0,0 +1,29 @@
|
|||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import output
|
||||
from esphome.const import CONF_CHANNEL, CONF_ID, CONF_CURRENT
|
||||
from . import BP5758D
|
||||
|
||||
DEPENDENCIES = ["bp5758d"]
|
||||
|
||||
Channel = BP5758D.class_("Channel", output.FloatOutput)
|
||||
|
||||
CONF_BP5758D_ID = "bp5758d_id"
|
||||
CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
|
||||
{
|
||||
cv.GenerateID(CONF_BP5758D_ID): cv.use_id(BP5758D),
|
||||
cv.Required(CONF_ID): cv.declare_id(Channel),
|
||||
cv.Required(CONF_CHANNEL): cv.int_range(min=1, max=5),
|
||||
cv.Optional(CONF_CURRENT, default=10): cv.int_range(min=0, max=90),
|
||||
}
|
||||
).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_BP5758D_ID])
|
||||
cg.add(var.set_parent(parent))
|
||||
cg.add(var.set_channel(config[CONF_CHANNEL]))
|
||||
cg.add(var.set_current(config[CONF_CURRENT]))
|
|
@ -1465,6 +1465,10 @@ my9231:
|
|||
num_chips: 2
|
||||
bit_depth: 16
|
||||
|
||||
bp5758d:
|
||||
data_pin: GPIO3
|
||||
clock_pin: GPIO5
|
||||
|
||||
output:
|
||||
- platform: gpio
|
||||
pin: GPIO26
|
||||
|
@ -1626,6 +1630,26 @@ output:
|
|||
vref: internal
|
||||
gain: X2
|
||||
power_down: gnd_500k
|
||||
- platform: bp5758d
|
||||
id: bp5758d_red
|
||||
channel: 2
|
||||
current: 10
|
||||
- platform: bp5758d
|
||||
id: bp5758d_green
|
||||
channel: 3
|
||||
current: 10
|
||||
- platform: bp5758d
|
||||
id: bp5758d_blue
|
||||
channel: 1
|
||||
current: 10
|
||||
- platform: bp5758d
|
||||
id: bp5758d_coldwhite
|
||||
channel: 5
|
||||
current: 10
|
||||
- platform: bp5758d
|
||||
id: bp5758d_warmwhite
|
||||
channel: 4
|
||||
current: 10
|
||||
|
||||
e131:
|
||||
|
||||
|
|
Loading…
Reference in a new issue