Port Soft-AP web UI and LED engine onto Pico 2 W C firmware.

Serve the ESP32 frontend over httpserver/WebSocket, drive eight WS2812 strips with PIO/DMA patterns on core1, and keep Wi-Fi control on core0.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
jimmyandCursor committed 2026-07-20 22:03:52 +12:00
1 parent b783d97f13
commit b3e9fc9663
28 files changed
+5123 -14

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
#pragma once
#include <array>
#include <cstdint>
#include <string>
#include <vector>
struct RGB {
uint8_t r = 0;
uint8_t g = 0;
uint8_t b = 0;
};
class Preset {
public:
std::string p = "off";
int d = 100;
int b = 127;
std::vector<RGB> c{{255, 255, 255}};
bool a = true;
int n1 = 0, n2 = 0, n3 = 0, n4 = 0;
int n5 = 0, n6 = 0, n7 = 0, n8 = 0;
Preset() = default;
explicit Preset(std::string pattern) : p(std::move(pattern)) {}
};
+97
View File
@@ -0,0 +1,97 @@
#include "leds/presets.hpp"
#include <algorithm>
#include "patterns/patterns.hpp"
namespace {
struct StripConfig { uint pin, count, midpoint; };
constexpr StripConfig kConfig[] = {
{6, 291, 291 / 2}, {29, 290, 290 / 2 - 1}, {3, 283, 283 / 2},
{28, 278, 278 / 2 - 1}, {2, 278, 275 / 2}, {0, 283, 278 / 2 - 1},
{4, 290, 283 / 2}, {7, 291, 290 / 2 - 1},
};
}
Presets::Presets() {
for (size_t i = 0; i < kStripCount; ++i) {
strips[i] = std::make_unique<WS2812B>(kConfig[i].count, kConfig[i].pin, i);
strip_midpoints[i] = kConfig[i].midpoint;
scale_map[i].resize(kConfig[0].count);
for (size_t led = 0; led < scale_map[i].size(); ++led)
scale_map[i][led] = led * kConfig[i].count / kConfig[0].count;
}
}
Presets::~Presets() = default;
std::unique_ptr<Pattern> Presets::make_pattern(const std::string& name) const {
#define MAKE(name) if (name == #name) return std::make_unique<name>();
if (name == "off" || name == "on") return nullptr;
if (name == "blink") return std::make_unique<Blink>();
if (name == "rainbow") return std::make_unique<Rainbow>();
if (name == "pulse") return std::make_unique<Pulse>();
if (name == "transition") return std::make_unique<Transition>();
if (name == "chase") return std::make_unique<Chase>();
if (name == "circle") return std::make_unique<Circle>();
if (name == "roll") return std::make_unique<Roll>();
if (name == "calibration") return std::make_unique<Calibration>();
if (name == "test") return std::make_unique<Test>();
if (name == "grab") return std::make_unique<Grab>();
if (name == "spin") return std::make_unique<Spin>();
if (name == "lift") return std::make_unique<Lift>();
if (name == "flare") return std::make_unique<Flare>();
if (name == "hook") return std::make_unique<Hook>();
if (name == "pose") return std::make_unique<Pose>();
if (name == "point") return std::make_unique<Point>();
if (name == "scale_test") return std::make_unique<ScaleTest>();
return nullptr;
#undef MAKE
}
bool Presets::select(const std::string& name, int next_step) {
auto saved = presets.find(name);
Preset* preset = saved == presets.end() ? nullptr : &saved->second;
const std::string pattern = preset ? preset->p : name;
if (pattern == "off") { off(); current.reset(); selected = name; step = 0; return true; }
if (pattern == "on") {
static Preset default_on("on");
on(preset ? preset : &default_on); current.reset(); selected = name; return true;
}
current = make_pattern(pattern);
if (!current) return false;
static Preset built_in;
if (!preset) { built_in = Preset(pattern); preset = &built_in; }
if (next_step >= 0) step = next_step;
else if (selected != name) step = 0;
current->start(*this, preset);
selected = name;
return true;
}
bool Presets::edit(const std::string& name, const Preset& preset) { presets[name] = preset; return true; }
bool Presets::remove(const std::string& name) { return presets.erase(name) != 0; }
void Presets::tick() { if (current && !current->tick()) current.reset(); }
void Presets::fill(RGB color) { for (auto& strip : strips) strip->fill(color); }
void Presets::fill_n(RGB color, int first, int last) {
first = std::max(0, first); last = std::min<int>(kConfig[0].count, last);
for (int i = first; i < last; ++i) for (size_t s = 0; s < kStripCount; ++s) set(s, i, color);
}
bool Presets::set(size_t strip, int index, RGB color) {
if (strip >= kStripCount || index < 0 || index >= static_cast<int>(scale_map[strip].size())) return false;
strips[strip]->set(scale_map[strip][index], color); return true;
}
RGB Presets::apply_brightness(RGB color, int local) const {
const int effective = std::clamp(local, 0, 255) * b / 255;
return {static_cast<uint8_t>(color.r * effective / 255), static_cast<uint8_t>(color.g * effective / 255),
static_cast<uint8_t>(color.b * effective / 255)};
}
void Presets::show_all() { for (auto& strip : strips) strip->show(); }
void Presets::show_all_parallel() {
uint32_t mask = 0;
for (auto& strip : strips) { strip->prepare_show(); mask |= 1u << strip->dma_channel; }
dma_start_channel_mask(mask);
}
void Presets::off() { fill({0, 0, 0}); show_all_parallel(); }
void Presets::on(Preset* preset) {
const RGB color = preset && !preset->c.empty() ? preset->c[0] : RGB{255, 255, 255};
fill(apply_brightness(color, preset ? preset->b : 255)); show_all_parallel();
}
uint16_t Presets::strip_length(size_t strip) const { return strip < kStripCount ? strips[strip]->num_leds : 0; }
+46
View File
@@ -0,0 +1,46 @@
#pragma once
#include <array>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include "leds/preset.hpp"
#include "leds/ws2812.hpp"
class Pattern;
class Presets {
public:
static constexpr size_t kStripCount = 8;
Presets();
~Presets();
bool select(const std::string& name, int step = -1);
bool edit(const std::string& name, const Preset& preset);
bool remove(const std::string& name);
void tick();
void fill(RGB color);
void fill_n(RGB color, int first, int last);
bool set(size_t strip, int reference_index, RGB color);
RGB apply_brightness(RGB color, int local = 255) const;
void show_all();
void show_all_parallel();
void off();
void on(Preset* preset);
uint16_t strip_length(size_t strip) const;
std::array<std::unique_ptr<WS2812B>, kStripCount> strips;
std::array<uint16_t, kStripCount> strip_midpoints{};
std::array<std::vector<uint16_t>, kStripCount> scale_map;
std::unordered_map<std::string, Preset> presets;
std::unique_ptr<Pattern> current;
std::string selected;
int step = 0;
int last_roll_head = 0;
uint8_t b = 255;
private:
std::unique_ptr<Pattern> make_pattern(const std::string& name) const;
};
+84
View File
@@ -0,0 +1,84 @@
#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); }
+34
View File
@@ -0,0 +1,34 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <vector>
#include "hardware/dma.h"
#include "hardware/pio.h"
struct RGB;
class WS2812B {
public:
WS2812B(uint16_t num_leds, uint pin, uint strip_index, float brightness = 1.0f);
~WS2812B();
WS2812B(const WS2812B&) = delete;
WS2812B& operator=(const WS2812B&) = delete;
void fill(RGB color);
void set(uint16_t index, RGB color);
void show(const uint8_t* buffer = nullptr, size_t offset = 0);
void prepare_show(const uint8_t* buffer = nullptr, size_t offset = 0);
void trigger();
bool busy() const;
uint16_t num_leds;
float brightness;
std::vector<uint8_t> buffer;
int dma_channel = -1;
PIO pio = pio0;
uint sm = 0;
private:
uint pin_;
};