## Problem Custom sleep wallpapers feel repetitive — the same image appearing multiple times in a short session. Only the single most-recently-shown index was stored (`uint8_t lastSleepImage`), so on collections of 3–5 images, two-pick cycles were common. On larger collections, any image could reappear within the next few picks. ## Solution Add a 16-entry circular recency buffer to `CrossPointState` that excludes recently shown wallpapers from selection. **Recency buffer** (`recentSleepImages[16]`, `recentSleepPos`, `recentSleepFill` — 34 bytes DRAM): - Tracks the last 16 shown wallpaper indices - On each pick, rerolls up to 20 times if the candidate was recently shown - Window auto-shrinks to `numFiles - 1` for small collections (guarantees a non-repeat is always possible) - `isRecentSleep()` clamps to `recentSleepFill` to avoid false positives on unwritten buffer slots - State persisted to `state.json` so the buffer survives sleep/wake cycles **Migration**: - Binary (`state.bin`): old `lastSleepImage` field seeded into the new buffer if valid - JSON (`state.json`): legacy `lastSleepImage` key detected and seeded into the buffer when upgrading from older firmware ## Index type fix `randomFileIndex` upgraded from `uint8_t` to `uint16_t` — silently truncated for collections larger than 255 wallpapers. ## Repeat probability: before vs after Chance of seeing a recently-shown image on the next pick. After: `(numFiles - 16) / numFiles` once collection exceeds the window; 0% while ≤17. | Collection | Before | After | |---|---|---| | 3 | 50% | 0% | | 5 | 75% | 0% | | 10 | 89% | 0% | | 17 | 94% | 0% | | 18 | 94% | 6% | | 20 | 95% | 20% | | 25 | 96% | 36% | | 30 | 97% | 47% | | 50 | 98% | 68% | | 100 | 99% | 84% | ## Memory impact | Addition | Size | |---|---| | `recentSleepImages[16]` | 32 bytes DRAM | | `recentSleepPos` + `recentSleepFill` | 2 bytes DRAM | | **Total** | **34 bytes DRAM** | ## Files changed - `src/CrossPointState.h` — recency buffer fields + `isRecentSleep()` / `pushRecentSleep()` declarations - `src/CrossPointState.cpp` — `isRecentSleep()` / `pushRecentSleep()` implementations + binary migration path - `src/JsonSettingsIO.cpp` — JSON serialisation of buffer state + JSON migration - `src/activities/boot_sleep/SleepActivity.cpp` — retry loop with recency check Co-authored-by: Patryk Radtke <patryk@Patryks-MacBook-Pro.local>
99 lines
2.7 KiB
C++
99 lines
2.7 KiB
C++
#include "CrossPointState.h"
|
|
|
|
#include <HalStorage.h>
|
|
#include <JsonSettingsIO.h>
|
|
#include <Logging.h>
|
|
#include <Serialization.h>
|
|
|
|
#include <algorithm>
|
|
|
|
namespace {
|
|
constexpr uint8_t STATE_FILE_VERSION = 4;
|
|
constexpr char STATE_FILE_BIN[] = "/.crosspoint/state.bin";
|
|
constexpr char STATE_FILE_JSON[] = "/.crosspoint/state.json";
|
|
constexpr char STATE_FILE_BAK[] = "/.crosspoint/state.bin.bak";
|
|
} // namespace
|
|
|
|
CrossPointState CrossPointState::instance;
|
|
|
|
bool CrossPointState::isRecentSleep(uint16_t idx, uint8_t checkCount) const {
|
|
const uint8_t effectiveCount = std::min(checkCount, recentSleepFill);
|
|
for (uint8_t i = 0; i < effectiveCount; i++) {
|
|
const uint8_t slot = (recentSleepPos + SLEEP_RECENT_COUNT - 1 - i) % SLEEP_RECENT_COUNT;
|
|
if (recentSleepImages[slot] == idx) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void CrossPointState::pushRecentSleep(uint16_t idx) {
|
|
recentSleepImages[recentSleepPos] = idx;
|
|
recentSleepPos = (recentSleepPos + 1) % SLEEP_RECENT_COUNT;
|
|
if (recentSleepFill < SLEEP_RECENT_COUNT) recentSleepFill++;
|
|
}
|
|
|
|
bool CrossPointState::saveToFile() const {
|
|
Storage.mkdir("/.crosspoint");
|
|
return JsonSettingsIO::saveState(*this, STATE_FILE_JSON);
|
|
}
|
|
|
|
bool CrossPointState::loadFromFile() {
|
|
// Try JSON first
|
|
if (Storage.exists(STATE_FILE_JSON)) {
|
|
String json = Storage.readFile(STATE_FILE_JSON);
|
|
if (!json.isEmpty()) {
|
|
return JsonSettingsIO::loadState(*this, json.c_str());
|
|
}
|
|
}
|
|
|
|
// Fall back to binary migration
|
|
if (Storage.exists(STATE_FILE_BIN)) {
|
|
if (loadFromBinaryFile()) {
|
|
if (saveToFile()) {
|
|
Storage.rename(STATE_FILE_BIN, STATE_FILE_BAK);
|
|
LOG_DBG("CPS", "Migrated state.bin to state.json");
|
|
return true;
|
|
} else {
|
|
LOG_ERR("CPS", "Failed to save state during migration");
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool CrossPointState::loadFromBinaryFile() {
|
|
FsFile inputFile;
|
|
if (!Storage.openFileForRead("CPS", STATE_FILE_BIN, inputFile)) {
|
|
return false;
|
|
}
|
|
|
|
uint8_t version;
|
|
serialization::readPod(inputFile, version);
|
|
if (version > STATE_FILE_VERSION) {
|
|
LOG_ERR("CPS", "Deserialization failed: Unknown version %u", version);
|
|
return false;
|
|
}
|
|
|
|
serialization::readString(inputFile, openEpubPath);
|
|
if (version >= 2) {
|
|
uint8_t legacyLastSleep = UINT8_MAX;
|
|
serialization::readPod(inputFile, legacyLastSleep);
|
|
if (legacyLastSleep != UINT8_MAX) {
|
|
pushRecentSleep(static_cast<uint16_t>(legacyLastSleep));
|
|
}
|
|
}
|
|
|
|
if (version >= 3) {
|
|
serialization::readPod(inputFile, readerActivityLoadCount);
|
|
}
|
|
|
|
if (version >= 4) {
|
|
serialization::readPod(inputFile, lastSleepFromReader);
|
|
} else {
|
|
lastSleepFromReader = false;
|
|
}
|
|
|
|
return true;
|
|
}
|