// Einstellungen im Flash (NVS), damit der Würfel nach dem Ausschalten dort // weitermacht, wo er war: Effekt, automatischer Wechsel, Helligkeit, Favoriten, // beide Effekt-Regler je Effekt, Farbe der Lampe, Laufschrift und Ort fürs Wetter. // Gespeichert wird erst 2 s nach der letzten Änderung, damit z. B. ein Regler, // der viele Änderungen schickt, nicht jedes Mal den Flash beschreibt. #include "cube.h" #include static Preferences prefs; static bool dirty = false; static uint32_t changedAt = 0; void settingsChanged() { dirty = true; changedAt = millis(); } static String loadString(const char *key, const char *fallback) { return prefs.isKey(key) ? prefs.getString(key, fallback) : String(fallback); } void settingsLoad() { prefs.begin("cube", false); setBaseBrightness(prefs.getUChar("brightness", BRIGHTNESS)); autoAdvance = prefs.getBool("auto", true); autoAll = prefs.getBool("all", false); playFavorites = prefs.getBool("playFav", false); // Favoriten als ids mit Komma; unbekannte (z. B. nach einem Update) fallen weg String favs = loadString("favorites", ""); uint8_t list[MAX_FAVORITES], n = 0; for (int start = 0; start < (int)favs.length() && n < MAX_FAVORITES;) { int end = favs.indexOf(',', start); if (end < 0) end = favs.length(); int i = findEffect(favs.substring(start, end).c_str()); if (i >= 0) list[n++] = i; start = end + 1; } setFavorites(list, n); // Regler "Effekt 1" und "Effekt 2" als "id=wert:wert2" mit Komma (ältere Fassung: // "id=wert"), nur die von der Mitte verschobenen String params = loadString("params", ""); for (int start = 0; start < (int)params.length();) { int end = params.indexOf(',', start); if (end < 0) end = params.length(); String item = params.substring(start, end); int eq = item.indexOf('='), colon = item.indexOf(':'); int i = eq > 0 ? findEffect(item.substring(0, eq).c_str()) : -1; if (i >= 0) { setEffectParam(i, 0, constrain(item.substring(eq + 1, colon > eq ? colon : item.length()).toInt(), 0, 255)); if (colon > eq) setEffectParam(i, 1, constrain(item.substring(colon + 1).toInt(), 0, 255)); } start = end + 1; } lampColor = CRGB(prefs.getUInt("lamp", 0xFFA03C)); tickerText = loadString("text", "HALLO"); setLocation(loadString("place", "").c_str(), prefs.getFloat("lat", 0), prefs.getFloat("lon", 0)); int effect = findEffect(loadString("effect", "").c_str()); if (effect >= 0) selectEffect(effect); } void settingsUpdate(uint32_t now) { if (!dirty || now - changedAt < 2000) return; dirty = false; prefs.putString("effect", effectId(currentEffect())); prefs.putUChar("brightness", brightness); prefs.putBool("auto", autoAdvance); prefs.putBool("all", autoAll); prefs.putBool("playFav", playFavorites); String favs; for (uint8_t k = 0; k < numFavorites(); k++) { if (k) favs += ','; favs += effectId(favorite(k)); } prefs.putString("favorites", favs); String params; for (uint8_t i = 0; i < numEffects(); i++) if (effectParam(i, 0) != 128 || effectParam(i, 1) != 128) { if (params.length()) params += ','; params += effectId(i); params += '='; params += effectParam(i, 0); params += ':'; params += effectParam(i, 1); } prefs.putString("params", params); prefs.putUInt("lamp", uint32_t(lampColor.r) << 16 | uint32_t(lampColor.g) << 8 | lampColor.b); prefs.putString("text", tickerText); char place[48]; float lat, lon; getLocation(place, sizeof(place), lat, lon); prefs.putString("place", place); prefs.putFloat("lat", lat); prefs.putFloat("lon", lon); Serial.println("Einstellungen gespeichert"); }