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>
98 lines
4.5 KiB
C++
98 lines
4.5 KiB
C++
#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; }
|