added rgb_order configuration

This commit is contained in:
2026-10-30 10:36:50 +01:00
parent 8c7cfc1fa9
commit 2962fb2ce7
4 changed files with 96 additions and 7 deletions
+47 -4
View File
@@ -3,6 +3,25 @@
namespace esphome::spi_led_strip {
static const LogString *rgb_order_to_string(RGBOrder rgb_order) {
switch (rgb_order) {
case RGBOrder::ORDER_RGB:
return LOG_STR("RGB");
case RGBOrder::ORDER_RBG:
return LOG_STR("RBG");
case RGBOrder::ORDER_GRB:
return LOG_STR("GRB");
case RGBOrder::ORDER_GBR:
return LOG_STR("GBR");
case RGBOrder::ORDER_BGR:
return LOG_STR("BGR");
case RGBOrder::ORDER_BRG:
return LOG_STR("BRG");
default:
return LOG_STR("UNKNOWN");
}
}
SpiLedStrip::SpiLedStrip(uint16_t num_leds, SpiLedChipset chipset) : num_leds_(num_leds), chipset_(chipset) {
RAMAllocator<uint8_t> allocator;
#ifdef USE_SPI_LED_STRIP_WS2801
@@ -50,9 +69,10 @@ void SpiLedStrip::dump_config() {
TAG,
"SPI LED Strip:\n"
" Chipset: %s\n"
" LEDs: %d",
" LEDs: %d\n"
" RGB order: %s",
this->chipset_ == SpiLedChipset::SPI_LED_CHIPSET_WS2801 ? LOG_STR_LITERAL("WS2801") : LOG_STR_LITERAL("APA102"),
this->num_leds_);
this->num_leds_, LOG_STR_ARG(rgb_order_to_string(this->rgb_order_)));
if (this->data_rate_ >= spi::DATA_RATE_1MHZ) {
esph_log_config(TAG, " Data rate: %uMHz", (unsigned) (this->data_rate_ / 1000000));
} else {
@@ -75,15 +95,38 @@ void SpiLedStrip::write_state(light::LightState *state) {
this->disable();
}
light::ESPColorView SpiLedStrip::get_view_internal(int32_t index) const {
// byte offsets of red, green and blue within the color bytes of one LED
uint8_t r, g, b;
switch (this->rgb_order_) {
case RGBOrder::ORDER_RGB:
r = 0, g = 1, b = 2;
break;
case RGBOrder::ORDER_RBG:
r = 0, g = 2, b = 1;
break;
case RGBOrder::ORDER_GRB:
r = 1, g = 0, b = 2;
break;
case RGBOrder::ORDER_GBR:
r = 2, g = 0, b = 1;
break;
case RGBOrder::ORDER_BRG:
r = 1, g = 2, b = 0;
break;
case RGBOrder::ORDER_BGR:
default:
r = 2, g = 1, b = 0;
break;
}
#ifdef USE_SPI_LED_STRIP_WS2801
if (this->chipset_ == SpiLedChipset::SPI_LED_CHIPSET_WS2801) {
size_t pos = index * 3;
return {this->buf_ + pos + 0, this->buf_ + pos + 1, this->buf_ + pos + 2, nullptr,
return {this->buf_ + pos + r, this->buf_ + pos + g, this->buf_ + pos + b, nullptr,
this->effect_data_ + index, &this->correction_};
}
#endif
size_t pos = index * 4 + 5;
return {this->buf_ + pos + 2, this->buf_ + pos + 1, this->buf_ + pos + 0, nullptr,
return {this->buf_ + pos + r, this->buf_ + pos + g, this->buf_ + pos + b, nullptr,
this->effect_data_ + index, &this->correction_};
}
} // namespace esphome::spi_led_strip