Files
led-hoop/c/lib/leds/ws2812.cpp
T
jimmyandClaude Opus 5.5 bf0429c505 Embed gzipped web assets at build time and type LED edit commands.
Generate the web UI headers from c/web/ via scripts/embed_file.py (minify + gzip) and serve them with Content-Encoding, replacing the checked-in web_*.h blobs. Move the LED driver/presets into lib/leds, and parse preset edits into a typed cmd_preset_t on core0 instead of passing raw JSON to core1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 22:18:10 +13:00

85 lines
3.4 KiB
C++

#include "leds/ws2812.hpp"
#include "hardware/clocks.h"
#include "hardware/gpio.h"
#include "leds/preset.hpp"
namespace {
constexpr uint T1 = 2, T2 = 5, T3 = 3;
const uint16_t ws2812_instructions[] = {
static_cast<uint16_t>(pio_encode_out(pio_x, 1) | pio_encode_sideset(1, 0) | pio_encode_delay(T3 - 1)),
static_cast<uint16_t>(pio_encode_jmp_not_x(3) | pio_encode_sideset(1, 1) | pio_encode_delay(T1 - 1)),
static_cast<uint16_t>(pio_encode_jmp(0) | pio_encode_sideset(1, 1) | pio_encode_delay(T2 - 1)),
static_cast<uint16_t>(pio_encode_nop() | pio_encode_sideset(1, 0) | pio_encode_delay(T2 - 1)),
};
const pio_program ws2812_program = {
.instructions = ws2812_instructions, .length = 4, .origin = -1,
};
bool pio_program_added[2] = {};
uint pio_offsets[2] = {};
uint program_offset(PIO pio) {
const uint index = pio == pio0 ? 0 : 1;
if (!pio_program_added[index]) {
pio_offsets[index] = pio_add_program(pio, &ws2812_program);
pio_program_added[index] = true;
}
return pio_offsets[index];
}
} // namespace
WS2812B::WS2812B(uint16_t count, uint pin, uint strip_index, float level)
: num_leds(count), brightness(level), buffer(static_cast<size_t>(count) * 3),
pio(strip_index < 4 ? pio0 : pio1), sm(strip_index % 4), pin_(pin) {
const uint offset = program_offset(pio);
pio_gpio_init(pio, pin_);
pio_sm_set_consecutive_pindirs(pio, sm, pin_, 1, true);
pio_sm_config config = pio_get_default_sm_config();
sm_config_set_wrap(&config, offset, offset + 3);
sm_config_set_sideset(&config, 1, false, false);
sm_config_set_sideset_pins(&config, pin_);
sm_config_set_out_shift(&config, false, true, 8);
sm_config_set_fifo_join(&config, PIO_FIFO_JOIN_TX);
sm_config_set_clkdiv(&config, static_cast<float>(clock_get_hz(clk_sys)) / 8000000.0f);
pio_sm_init(pio, sm, offset, &config);
pio_sm_set_enabled(pio, sm, true);
dma_channel = dma_claim_unused_channel(true);
dma_channel_config dma_config = dma_channel_get_default_config(dma_channel);
channel_config_set_transfer_data_size(&dma_config, DMA_SIZE_8);
channel_config_set_dreq(&dma_config, pio_get_dreq(pio, sm, true));
channel_config_set_read_increment(&dma_config, true);
channel_config_set_write_increment(&dma_config, false);
dma_channel_configure(dma_channel, &dma_config, &pio->txf[sm], nullptr,
buffer.size(), false);
}
WS2812B::~WS2812B() {
if (dma_channel >= 0) dma_channel_unclaim(dma_channel);
}
void WS2812B::set(uint16_t index, RGB color) {
if (index >= num_leds) return;
const size_t pos = static_cast<size_t>(index) * 3;
buffer[pos] = static_cast<uint8_t>(color.g * brightness);
buffer[pos + 1] = static_cast<uint8_t>(color.r * brightness);
buffer[pos + 2] = static_cast<uint8_t>(color.b * brightness);
}
void WS2812B::fill(RGB color) {
for (uint16_t i = 0; i < num_leds; ++i) set(i, color);
}
void WS2812B::prepare_show(const uint8_t* source, size_t offset) {
if (!source) source = buffer.data();
dma_channel_set_read_addr(dma_channel, source + offset, false);
dma_channel_set_trans_count(dma_channel, buffer.size(), false);
}
void WS2812B::trigger() { dma_channel_start(dma_channel); }
void WS2812B::show(const uint8_t* source, size_t offset) {
prepare_show(source, offset);
trigger();
}
bool WS2812B::busy() const { return dma_channel_is_busy(dma_channel); }