ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
brizlabs_led_string.cpp
Go to the documentation of this file.
2
3#ifdef USE_ESP32
4
5#include "esphome/core/log.h"
6#include <cstring>
7#include <esp_err.h>
8
10
11ESPHOME_LOG_TAG(TAG, "brizlabs_led_string");
12
14 // This takes direct and full control over the SPI bus and uses it as a clocked signal
15 // generator on MOSI only. (So it's not actually using it for SPI -- cool hack.)
16 // See encoder.h for more details
17 spi_bus_config_t bus = {};
18 bus.mosi_io_num = this->pin_;
19 bus.miso_io_num = bus.sclk_io_num = bus.quadwp_io_num = bus.quadhd_io_num = -1;
20 if (!this->setup_check(spi_bus_initialize(SPI2_HOST, &bus, SPI_DMA_DISABLED) == ESP_OK,
21 LOG_STR("SPI bus init failed")))
22 return;
23 spi_device_interface_config_t dev = {};
24 dev.clock_speed_hz = 100000; // 1 timeslot on the wire = 10 us, which is what their LED modules need
25 dev.spics_io_num = -1;
26 dev.queue_size = NUM_ZONES; // a whole write is queued at once
27 if (!this->setup_check(spi_bus_add_device(SPI2_HOST, &dev, &this->dev_) == ESP_OK, LOG_STR("SPI device init failed")))
28 return;
29 this->accessor_ = &this->internal_accessor_;
30}
31
33 ESP_LOGCONFIG(TAG,
34 "BrizLabs two-wire LED string:\n"
35 " Data (SPI MOSI): GPIO%d\n"
36 " Zones: %d",
37 this->pin_, NUM_ZONES);
38}
39
40// Queues the frame and returns; the SPI driver sends the queued frames in order from its interrupt.
41bool BrizLabsLEDString::queue_frame_(uint8_t zone, uint8_t r, uint8_t g, uint8_t b) {
42 // write_state() only queues once every earlier frame is collected, so in_flight_ is the next free slot
43 uint8_t *frame = this->frames_[this->in_flight_];
44 spi_transaction_t &t = this->trans_[this->in_flight_];
45 t = {};
46 // Must be whole bytes. The zero pad bits only extend the idle-low after the frame's trailer.
47 t.length = (encode(frame, 0, zone, 0xFF, r, g, b) + 7) & ~7u;
48 t.tx_buffer = frame;
49 esp_err_t err = spi_device_queue_trans(this->dev_, &t, 0);
50 if (err != ESP_OK) {
51 // Logged once per run of failures. write_state() retries on every loop until one succeeds.
52 if (!this->status_has_warning()) {
53 ESP_LOGW(TAG, "queue frame: %s", esp_err_to_name(err));
54 }
55 return false;
56 }
57 this->in_flight_++;
58 return true;
59}
60
61// One broadcast frame if all zones match, else one frame per zone.
63 bool uniform = true;
64 for (int z = 0; z < NUM_ZONES; z++) {
65 const uint8_t *p = this->buf_ + z * 3;
66 uniform &= memcmp(p, this->buf_, 3) == 0;
67 }
68 if (uniform)
69 return this->queue_frame_(0, this->buf_[0], this->buf_[1], this->buf_[2]);
70 for (int z = 0; z < NUM_ZONES; z++) {
71 const uint8_t *p = this->buf_ + z * 3;
72 // Stop at the first failure; write_state() queues every zone again once the queued frames have gone out
73 if (!this->queue_frame_(z + 1, p[0], p[1], p[2]))
74 return false;
75 }
76 return true;
77}
78
80 if (this->is_failed())
81 return;
82
83 // Collect the frames that have finished
84 spi_transaction_t *done;
85 while (this->in_flight_ > 0 && spi_device_get_trans_result(this->dev_, &done, 0) == ESP_OK)
86 this->in_flight_--;
87 // The previous write is still on the wire and owns trans_ and frames_: try again on the next loop
88 if (this->in_flight_ > 0) {
89 this->schedule_show();
90 return;
91 }
92
93 this->mark_shown_(); // power supply on/off by pixel state
94
95 // On failure, try again on a later loop so the frame is not lost
96 if (!this->status_comm_result(this->transmit_all_()))
97 this->schedule_show();
98}
99
100} // namespace esphome::brizlabs_led_string
101
102#endif // USE_ESP32
bool is_failed() const
Definition component.h:287
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:235
bool status_has_warning() const
Definition component.h:293
bool queue_frame_(uint8_t zone, uint8_t r, uint8_t g, uint8_t b)
light::CorrectedBufferColorAccessor internal_accessor_
void write_state(light::LightState *state) override
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:73
bool state
Definition fan.h:2
bool z
Definition msa3xx.h:1
ESPHOME_LOG_TAG(TAG, "brizlabs_led_string")
size_t encode(uint8_t *out, uint8_t id, uint8_t zone, uint8_t hdr, uint8_t r, uint8_t g, uint8_t b)
Definition encoder.h:65
SemaphoreHandle_t done