Files
cube/fx_extras.cpp
mikeandClaude Opus 5.5 9089fd6431 Zweiter Regler „Effekt 2“: zwei Parameter je Effekt
- Ohne Angabe Tempo und Farbe (66 Effekte); Musik: Empfindlichkeit und
  Farbe; Feuer: Hitze und Farbe; Gewitter, Regen, Schnee, Weihnachtsbaum,
  Geschenk, Fraktalwolken: zweiter Regler Tempo
- Eigene Regler 2: Blitzlicht/Blaulicht Nachleuchten (kein Tempo, damit
  sie nicht schneller blitzen), Lampe Weiß (weißer oder satter)
- Tempo, Farbe und Empfindlichkeit wirken, egal auf welchem Regler
- API: param2 in /api/state und /api/effects; gespeichert als id=w1:w2
  (alte Form id=w1 wird weiter gelesen)
- App: zweiter Regler, Spezialeffekt-Knöpfe etwas flacher, damit alles
  ohne Scrollen passt

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 16:12:12 +01:00

307 lines
11 KiB
C++

// Kategorie Extras: Lampe, Laufschrift, Uhr, Wetter
#include "fx.h"
#include <time.h>
CRGB lampColor(255, 160, 60); // warmweiß; in der App wählbar
String tickerText = "HALLO"; // in der App einstellbar
// --- Lampe: der ganze Würfel ruhig in einer Farbe ------------------------------
void renderLamp(uint32_t now) {
CRGB c = lampColor;
if (fxParam2 > 128) { // Regler 2 "Weiß": über der Mitte weißer, darunter satter
c = blend(c, CRGB::White, (fxParam2 - 128) * 2);
} else if (fxParam2 < 128) {
uint8_t w = min(c.r, min(c.g, c.b));
c -= CRGB(w, w, w).nscale8(min(255, (128 - fxParam2) * 2));
}
fill_solid(leds, NUM_LEDS, c.nscale8(highlight));
}
// --- Schrift: 5 LEDs hoch, je Spalte ein Byte (Bit 0 = oberste Zeile) -----------
struct Glyph {
uint16_t codepoint;
uint8_t width;
uint8_t cols[5];
};
static const Glyph FONT[] = {
{0x0020, 2, {0x00, 0x00}}, // Leerzeichen
{0x0021, 1, {0x17}}, // !
{0x0022, 3, {0x03, 0x00, 0x03}}, // "
{0x0025, 3, {0x19, 0x04, 0x13}}, // %
{0x0027, 1, {0x03}}, // '
{0x0028, 2, {0x0E, 0x11}}, // (
{0x0029, 2, {0x11, 0x0E}}, // )
{0x002A, 3, {0x05, 0x02, 0x05}}, // *
{0x002B, 3, {0x04, 0x0E, 0x04}}, // +
{0x002C, 1, {0x18}}, // ,
{0x002D, 3, {0x04, 0x04, 0x04}}, // -
{0x002E, 1, {0x10}}, // .
{0x002F, 3, {0x18, 0x04, 0x03}}, // /
{0x0030, 3, {0x1F, 0x11, 0x1F}}, // 0
{0x0031, 3, {0x12, 0x1F, 0x10}}, // 1
{0x0032, 3, {0x19, 0x15, 0x12}}, // 2
{0x0033, 3, {0x11, 0x15, 0x0A}}, // 3
{0x0034, 3, {0x07, 0x04, 0x1F}}, // 4
{0x0035, 3, {0x17, 0x15, 0x09}}, // 5
{0x0036, 3, {0x1E, 0x15, 0x1D}}, // 6
{0x0037, 3, {0x01, 0x1D, 0x03}}, // 7
{0x0038, 3, {0x1F, 0x15, 0x1F}}, // 8
{0x0039, 3, {0x17, 0x15, 0x0F}}, // 9
{0x003A, 1, {0x0A}}, // :
{0x003B, 1, {0x1A}}, // ;
{0x003D, 3, {0x0A, 0x0A, 0x0A}}, // =
{0x003F, 3, {0x01, 0x15, 0x02}}, // ?
{0x0041, 3, {0x1E, 0x05, 0x1E}}, // A
{0x0042, 3, {0x1F, 0x15, 0x0A}}, // B
{0x0043, 3, {0x0E, 0x11, 0x11}}, // C
{0x0044, 3, {0x1F, 0x11, 0x0E}}, // D
{0x0045, 3, {0x1F, 0x15, 0x11}}, // E
{0x0046, 3, {0x1F, 0x05, 0x01}}, // F
{0x0047, 4, {0x0E, 0x11, 0x15, 0x0C}}, // G
{0x0048, 3, {0x1F, 0x04, 0x1F}}, // H
{0x0049, 3, {0x11, 0x1F, 0x11}}, // I
{0x004A, 3, {0x08, 0x10, 0x0F}}, // J
{0x004B, 4, {0x1F, 0x04, 0x0A, 0x11}}, // K
{0x004C, 3, {0x1F, 0x10, 0x10}}, // L
{0x004D, 5, {0x1F, 0x02, 0x04, 0x02, 0x1F}}, // M
{0x004E, 4, {0x1F, 0x02, 0x04, 0x1F}}, // N
{0x004F, 4, {0x0E, 0x11, 0x11, 0x0E}}, // O
{0x0050, 3, {0x1F, 0x05, 0x02}}, // P
{0x0051, 4, {0x0E, 0x11, 0x09, 0x16}}, // Q
{0x0052, 3, {0x1F, 0x05, 0x1A}}, // R
{0x0053, 3, {0x12, 0x15, 0x09}}, // S
{0x0054, 3, {0x01, 0x1F, 0x01}}, // T
{0x0055, 3, {0x1F, 0x10, 0x1F}}, // U
{0x0056, 3, {0x07, 0x18, 0x07}}, // V
{0x0057, 5, {0x1F, 0x08, 0x04, 0x08, 0x1F}}, // W
{0x0058, 3, {0x1B, 0x04, 0x1B}}, // X
{0x0059, 3, {0x03, 0x1C, 0x03}}, // Y
{0x005A, 3, {0x19, 0x15, 0x13}}, // Z
{0x005F, 3, {0x10, 0x10, 0x10}}, // _
{0x00B0, 3, {0x07, 0x05, 0x07}}, // °
{0x00C4, 3, {0x1D, 0x0A, 0x1D}}, // Ä
{0x00D6, 4, {0x0D, 0x12, 0x12, 0x0D}}, // Ö
{0x00DC, 3, {0x1D, 0x10, 0x1D}}, // Ü
{0x2665, 5, {0x06, 0x0F, 0x1E, 0x0F, 0x06}}, // ♥
};
static const Glyph *findGlyph(uint16_t cp) {
if ((cp >= 'a' && cp <= 'z') || cp == 0xE4 || cp == 0xF6 || cp == 0xFC) cp -= 32; // klein -> groß, auch äöü
if (cp == 0x2764) cp = 0x2665; // ❤ wie ♥
for (const Glyph &g : FONT)
if (g.codepoint == cp) return &g;
return nullptr;
}
// Nächstes Zeichen aus einem UTF-8-Text
static uint16_t nextChar(const char *&s) {
uint8_t c = *s++;
if (c < 0x80) return c;
uint8_t extra = (c & 0xE0) == 0xC0 ? 1 : (c & 0xF0) == 0xE0 ? 2 : 3;
uint32_t cp = c & (0x3F >> extra);
while (extra-- && (*s & 0xC0) == 0x80) cp = cp << 6 | (*s++ & 0x3F);
return cp > 0xFFFF ? '?' : cp;
}
// Text in Spalten zerlegen, nach jedem Zeichen eine leere Spalte. Das Ergebnis
// bleibt stehen, bis ein anderer Text kommt.
static uint8_t textCols[640];
static uint16_t textLen = 0;
static uint16_t layoutText(const char *text) {
static String laidOut;
if (laidOut == text) return textLen;
laidOut = text;
textLen = 0;
for (const char *s = text; *s;) {
uint16_t cp = nextChar(s);
uint8_t repeat = 1;
if (cp == 0xDF) { // ß als SS
cp = 'S';
repeat = 2;
}
const Glyph *g = findGlyph(cp);
if (!g) g = findGlyph('?');
while (repeat--) {
if (textLen + g->width + 1 > sizeof(textCols)) return textLen;
memcpy(&textCols[textLen], g->cols, g->width);
textLen += g->width;
textCols[textLen++] = 0;
}
}
return textLen;
}
const uint16_t STEP_MS = 160; // so lange steht die Schrift, bevor sie eine Spalte weiterrückt
const uint8_t GAP = 5; // leere Spalten, bevor der Text von vorn beginnt
static CRGB textColor; // Farbe für solid()
static uint8_t textHue; // Farbverlauf für rainbow()
static CRGB solid(uint16_t) { return textColor; }
static CRGB rainbow(uint16_t col) { return CHSV(textHue + col * 6, 240, highlight); }
// Zeichnet die Textspalten ab "first" einmal rund um die 16 Randsäulen. Am echten
// Würfel läuft ringPos auf jeder Seite von außen gesehen von rechts nach links
// (geprüft: vorwärts belegt war die Schrift spiegelverkehrt), deshalb wird der Ring
// rückwärts belegt; mit wachsendem "first" wandert die Schrift nach links.
// period > 0: Text wiederholt sich; period = 0: nur einmal.
static void drawText(int32_t first, uint16_t period, CRGB (*paint)(uint16_t col)) {
for (uint8_t i = 0; i < 16; i++) {
int32_t c = first + i;
if (period) c = (c % period + period) % period;
if (c < 0 || c >= textLen || !textCols[c]) continue;
uint8_t x, y;
ringPos(15 - i, x, y);
CRGB color = paint(c);
for (uint8_t row = 0; row < 5; row++)
if (textCols[c] >> row & 1) at(x, y, TOP - row) = color;
}
}
// --- Laufschrift: der Text aus der App läuft in Regenbogenfarben um den Würfel ----
void renderTicker(uint32_t now) {
uint32_t t = now - fxStart;
uint16_t len = layoutText(tickerText.length() ? tickerText.c_str() : "HALLO");
FastLED.clear();
textHue = t / 30;
drawText(t / STEP_MS, len + GAP, rainbow);
}
// --- Uhr: die Uhrzeit läuft um den Würfel, die Mittelsäule füllt sich jede Minute --
void renderClock(uint32_t now) {
uint32_t t = now - fxStart;
time_t secs = time(nullptr);
struct tm tm;
localtime_r(&secs, &tm);
bool synced = tm.tm_year >= 2024 - 1900; // Zeit kommt per NTP aus dem Internet
char text[8];
if (synced) snprintf(text, sizeof(text), "%02d:%02d", tm.tm_hour, tm.tm_min);
else strcpy(text, "--:--");
uint16_t len = layoutText(text);
FastLED.clear();
textColor = CRGB(highlight, scale8(highlight, 200), scale8(highlight, 140)); // warmweiß
drawText(t / STEP_MS, len + GAP, solid);
if (!synced) return;
float filled = tm.tm_sec / 12.0f; // alle 12 s eine LED mehr
for (uint8_t z = 0; z < NUM_LAYERS; z++)
at(2, 2, z) = CRGB(0, scale8(highlight, 90), scale8(highlight, 200)).nscale8(uint8_t(constrain(filled - z, 0.0f, 1.0f) * 255));
}
// --- Wetter: das aktuelle Wetter am Ort aus der App, dazu alle 15 s die Temperatur --
enum Sky : uint8_t { SKY_CLEAR, SKY_PARTLY, SKY_OVERCAST, SKY_FOG, SKY_RAIN, SKY_SNOW, SKY_THUNDER };
// WMO-Wettercodes von Open-Meteo
static Sky skyFor(uint8_t code) {
if (code <= 1) return SKY_CLEAR;
if (code == 2) return SKY_PARTLY;
if (code == 3) return SKY_OVERCAST;
if (code == 45 || code == 48) return SKY_FOG;
if (code >= 95) return SKY_THUNDER;
if ((code >= 71 && code <= 77) || code == 85 || code == 86) return SKY_SNOW;
return SKY_RAIN; // Niesel, Regen, Schauer
}
// Sonne oben in der Mitte, darunter blauer Himmel
static void drawSun(uint32_t t) {
float pulse = 220 + 35 * sinf(t * 0.002f);
FOR_XYZ {
float glow = constrain(1 - dist3(x, y, z, MID, MID, TOP + 0.3f) / 2.6f, 0.0f, 1.0f);
CRGB c(0, brightness / 5, brightness / 2);
c += CRGB(highlight, scale8(highlight, 170), scale8(highlight, 20)).nscale8(uint8_t(glow * glow * pulse));
at(x, y, z) = c;
}
}
// Wolken ziehen durch die oberen drei Etagen; cover = Bedeckung 0..255
static void drawClouds(uint32_t t, uint8_t cover, uint8_t grey) {
for (uint8_t z = 2; z < NUM_LAYERS; z++)
for (uint8_t y = 0; y < CUBE_SIZE; y++)
for (uint8_t x = 0; x < CUBE_SIZE; x++) {
uint8_t v = qsub8(inoise8(x * 60 + t / 12, y * 60, z * 80 + t / 40), 255 - cover);
v = qadd8(v, v);
at(x, y, z) = blend(at(x, y, z), CRGB(grey, grey, qadd8(grey, grey / 16)), v);
}
}
static void drawFog(uint32_t t) {
FOR_XYZ {
uint8_t g = scale8(min(255, brightness * 2), 90 + inoise8(x * 40 + t / 20, y * 40, z * 40 + t / 30) / 2);
at(x, y, z) = CRGB(g, g, g);
}
}
// -10 °C blau, 0 °C türkis, 10 °C grün, 20 °C gelb-orange, 30 °C rot
static CRGB temperatureColor(float celsius) {
float c = constrain(celsius, -10.0f, 30.0f);
return CHSV(uint8_t(160 - (c + 10) * 4), 210, highlight);
}
void renderWeather(uint32_t now) {
uint32_t t = now - fxStart;
char place[48];
float lat, lon;
bool hasPlace = getLocation(place, sizeof(place), lat, lon);
WeatherInfo w = weatherNow();
if (!hasPlace || !w.valid) { // ohne Ort oder bis die ersten Daten da sind: Hinweis
uint16_t len = layoutText(hasPlace ? "LADE WETTER" : "ORT IN DER APP EINSTELLEN");
FastLED.clear();
textColor = CRGB(scale8(highlight, 200), scale8(highlight, 200), scale8(highlight, 200));
drawText(t / STEP_MS, len + GAP, solid);
return;
}
// Wetterbild; ändert es sich (oder Tag/Nacht), startet das Bild neu
static uint32_t started = 0;
static uint8_t shown = 255;
Sky sky = skyFor(w.code);
uint8_t key = sky * 2 + w.isDay;
if (started != fxStart) {
started = fxStart;
shown = 255;
}
if (key != shown) {
shown = key;
fresh = true;
for (Particle &p : particles) p.life = 0;
}
switch (sky) {
case SKY_CLEAR:
if (w.isDay) drawSun(t);
else renderStarrySky(now);
break;
case SKY_PARTLY:
if (w.isDay) {
drawSun(t);
drawClouds(t, 140, min(255, brightness * 3));
} else {
renderMoonlight(now);
}
break;
case SKY_OVERCAST:
FastLED.clear();
drawClouds(t, 230, min(255, brightness * 2));
break;
case SKY_FOG: drawFog(t); break;
case SKY_RAIN: renderRain(now); break;
case SKY_SNOW: renderSnow(now); break;
case SKY_THUNDER: renderStorm(now); break;
}
// alle 15 s läuft die Temperatur einmal um den Würfel, das Wetterbild wird dafür dunkler
char text[12];
snprintf(text, sizeof(text), "%d°", int(lroundf(w.temperature)));
uint16_t len = layoutText(text);
int32_t first = int32_t(t % 15000 / STEP_MS) - 16;
if (first < len) {
for (CRGB &c : leds) c.nscale8(80);
textColor = temperatureColor(w.temperature);
drawText(first, 0, solid);
}
}