fix: prevent wallpaper clustering with 16-entry recency buffer (#1606)

## 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>
This commit is contained in:
zgredex
2026-04-17 00:05:21 +03:00
committed by GitHub
co-authored by Patryk Radtke
parent 0c5dee3c62
commit c4f5c8e931
4 changed files with 64 additions and 14 deletions
+22 -3
View File
@@ -69,7 +69,10 @@ void applyLegacyStatusBarSettings(CrossPointSettings& settings) {
bool JsonSettingsIO::saveState(const CrossPointState& s, const char* path) {
JsonDocument doc;
doc["openEpubPath"] = s.openEpubPath;
doc["lastSleepImage"] = s.lastSleepImage;
JsonArray recentArr = doc["recentSleepImages"].to<JsonArray>();
for (int i = 0; i < CrossPointState::SLEEP_RECENT_COUNT; i++) recentArr.add(s.recentSleepImages[i]);
doc["recentSleepPos"] = s.recentSleepPos;
doc["recentSleepFill"] = s.recentSleepFill;
doc["readerActivityLoadCount"] = s.readerActivityLoadCount;
doc["lastSleepFromReader"] = s.lastSleepFromReader;
@@ -87,8 +90,24 @@ bool JsonSettingsIO::loadState(CrossPointState& s, const char* json) {
}
s.openEpubPath = doc["openEpubPath"] | std::string("");
s.lastSleepImage = doc["lastSleepImage"] | (uint8_t)UINT8_MAX;
s.readerActivityLoadCount = doc["readerActivityLoadCount"] | (uint8_t)0;
memset(s.recentSleepImages, 0, sizeof(s.recentSleepImages));
JsonArrayConst recentArr = doc["recentSleepImages"];
const int actualCount = recentArr.isNull() ? 0
: std::min(static_cast<int>(recentArr.size()),
static_cast<int>(CrossPointState::SLEEP_RECENT_COUNT));
for (int i = 0; i < actualCount; i++) s.recentSleepImages[i] = recentArr[i] | static_cast<uint16_t>(0);
s.recentSleepPos = doc["recentSleepPos"] | static_cast<uint8_t>(0);
if (s.recentSleepPos >= CrossPointState::SLEEP_RECENT_COUNT)
s.recentSleepPos = actualCount > 0 ? s.recentSleepPos % CrossPointState::SLEEP_RECENT_COUNT : 0;
s.recentSleepFill = doc["recentSleepFill"] | static_cast<uint8_t>(0);
s.recentSleepFill = static_cast<uint8_t>(std::min(static_cast<int>(s.recentSleepFill), actualCount));
// Migrate legacy single-image field from old state.json (pre-recency-buffer).
// Only seeds the buffer if the new buffer is empty (fresh migration, not a resave).
if (s.recentSleepFill == 0 && !doc["lastSleepImage"].isNull()) {
const uint8_t legacy = doc["lastSleepImage"] | static_cast<uint8_t>(UINT8_MAX);
if (legacy != UINT8_MAX) s.pushRecentSleep(static_cast<uint16_t>(legacy));
}
s.readerActivityLoadCount = doc["readerActivityLoadCount"] | static_cast<uint8_t>(0);
s.lastSleepFromReader = doc["lastSleepFromReader"] | false;
return true;
}