Files
Flipdot-display/src/esp32/display/icon_store.cpp
T

259 lines
6.7 KiB
C++
Raw Normal View History

#include "display/icon_store.h"
#include <LittleFS.h>
#include <algorithm>
#include <iterator>
#include <map>
#include <strings.h>
#include "display/bitmap_store.h"
2026-10-16 22:18:58 +02:00
#include "display/bitmaps.h"
static const char *DIR = "/icons";
// Written when the bitmaps were moved (not a valid name, so never listed)
2026-10-16 22:18:58 +02:00
// Built into the firmware (full screen bitmaps, cut to icons): stored at boot so they can
// be edited, and the fallback when they are not stored
struct BuiltinIcon
{
const char *name;
const uint8_t *bitmap; // PROGMEM, BITMAP_BYTES
};
static const BuiltinIcon BUILTIN[] = {
{"Pikachu", pikachu_clock_bitmap},
{"Pikachu yawning", pikachu_yawning_clock_bitmap},
};
static bool builtinIcon(const char *name, Icon &icon)
{
for (const BuiltinIcon &b : BUILTIN)
if (strcasecmp(b.name, name) == 0)
{
uint8_t bitmap[BITMAP_BYTES];
memcpy_P(bitmap, b.bitmap, BITMAP_BYTES);
iconFromBitmap(bitmap, icon);
return true;
}
return false;
}
bool builtinIconName(const char *name)
{
for (const BuiltinIcon &b : BUILTIN)
if (strcasecmp(b.name, name) == 0)
return true;
return false;
}
static uint32_t revision = 0;
// Counted once (listing the directory is slow), then kept up to date; -1: not yet
static int count = -1;
// Like the bitmaps (display/bitmap_store.cpp): images try a name per state, and pages are
// drawn for every frame and preview, so icons and missing names are kept in RAM. Room for
// more names than there are icons: when the cache is cleared during a pass over all icons
// (Icons tab, page previews), every one of them is read again (6.5 s with 66 icons and a
// limit of 64). About 270 bytes per name.
struct CachedIcon
{
bool exists;
Icon icon;
};
static std::map<String, CachedIcon> cache;
static const size_t CACHE_MAX = 128;
// After a save or delete: only this name (in any case, as it was looked up) is read again.
// Clearing everything made the next list of all icons read each one again (7 s).
static void forget(const char *name)
{
for (auto it = cache.begin(); it != cache.end();)
it = it->first.equalsIgnoreCase(name) ? cache.erase(it) : std::next(it);
}
// Full: first forget the missing names, only then the icons
static void makeRoom()
{
if (cache.size() < CACHE_MAX)
return;
for (auto it = cache.begin(); it != cache.end();)
it = it->second.exists ? std::next(it) : cache.erase(it);
if (cache.size() >= CACHE_MAX)
cache.clear();
}
static String path(const char *name)
{
return String(DIR) + "/" + name + ".bin";
}
static bool ensureDir()
{
if (!bitmapStorageAvailable())
return false;
return LittleFS.exists(DIR) || LittleFS.mkdir(DIR);
}
2026-10-16 22:18:58 +02:00
void iconStoreBegin()
{
for (const BuiltinIcon &b : BUILTIN)
{
Icon icon;
if (!bitmapStorageAvailable() || LittleFS.exists(path(b.name)) || !builtinIcon(b.name, icon))
continue;
const char *error = saveIcon(b.name, icon);
Serial.printf("Icon \"%s\" stored from the firmware: %s\n", b.name, error ? error : "ok");
}
}
// The sorted names, listed once (about a second) and then kept up to date
static std::vector<String> listed;
static bool listed_valid = false;
static bool nameBefore(const String &a, const String &b)
{
return strcasecmp(a.c_str(), b.c_str()) < 0;
}
static void listAdd(const char *name)
{
// It may replace a built-in one in another case: list again
if (builtinIconName(name))
listed_valid = false;
if (!listed_valid || std::find(listed.begin(), listed.end(), String(name)) != listed.end())
return;
listed.insert(std::lower_bound(listed.begin(), listed.end(), String(name), nameBefore), String(name));
}
static void listRemove(const char *name)
{
// A built-in one stays in the list (from the firmware): list again
if (builtinIconName(name))
listed_valid = false;
auto it = std::find(listed.begin(), listed.end(), String(name));
if (listed_valid && it != listed.end())
listed.erase(it);
}
std::vector<String> listIcons()
{
if (listed_valid)
return listed;
std::vector<String> names = storedNames(DIR, ".bin");
2026-10-16 22:18:58 +02:00
for (const BuiltinIcon &b : BUILTIN) // not stored (no storage)
if (std::none_of(names.begin(), names.end(), [&b](const String &n) { return strcasecmp(n.c_str(), b.name) == 0; }))
names.push_back(b.name);
std::sort(names.begin(), names.end(), nameBefore);
listed = names;
listed_valid = true;
return names;
}
uint32_t iconListRevision()
{
return revision;
}
bool iconExists(const char *name)
{
Icon icon;
return loadIcon(name, icon); // through the cache
}
bool loadIcon(const char *name, Icon &icon)
{
if (!bitmapStorageAvailable() || !validBitmapName(name))
2026-10-16 22:18:58 +02:00
return builtinIcon(name, icon);
auto cached = cache.find(name);
if (cached != cache.end())
{
if (cached->second.exists)
icon = cached->second.icon;
return cached->second.exists;
}
// Checked first: open() logs an error for a missing file
bool ok = false;
Icon loaded;
if (LittleFS.exists(path(name)))
{
File file = LittleFS.open(path(name), "r");
if (file)
{
uint8_t size[2];
ok = file.read(size, 2) == 2 && validIconSize(size[0], size[1]);
if (ok)
{
loaded.width = size[0];
loaded.height = size[1];
size_t bytes = iconBytes(loaded.width, loaded.height);
ok = file.read(loaded.data, bytes) == bytes;
}
file.close();
}
}
2026-10-16 22:18:58 +02:00
if (!ok)
ok = builtinIcon(name, loaded);
makeRoom();
CachedIcon &entry = cache[name];
entry.exists = ok;
if (ok)
{
entry.icon = loaded;
icon = loaded;
}
return ok;
}
const char *saveIcon(const char *name, const Icon &icon)
{
if (!validBitmapName(name))
return "Invalid name: use 1-24 letters, digits, spaces, - or _";
if (!validIconSize(icon.width, icon.height))
return "Icons are 1 x 1 to 84 x 19 dots";
if (!ensureDir())
return "No storage (install the new flash layout over USB, see README)";
bool is_new = !LittleFS.exists(path(name));
if (count < 0)
count = storedNames(DIR, ".bin").size();
if (is_new && count >= MAX_ICONS)
return "Too many icons, delete some first";
File file = LittleFS.open(path(name), "w");
if (!file)
return "Could not write the file";
uint8_t size[2] = {icon.width, icon.height};
size_t bytes = iconBytes(icon.width, icon.height);
bool ok = file.write(size, 2) == 2 && file.write(icon.data, bytes) == bytes;
file.close();
revision++;
bitmapStorageChanged();
forget(name);
if (!ok)
{
listed_valid = false;
LittleFS.remove(path(name)); // not half an icon
count = -1;
return "Storage full";
}
if (is_new)
{
count++;
listAdd(name);
}
return nullptr;
}
const char *deleteIcon(const char *name)
{
if (!bitmapStorageAvailable() || !validBitmapName(name) || !LittleFS.exists(path(name)))
return "Icon not found";
if (!LittleFS.remove(path(name)))
return "Could not delete the file";
if (count > 0)
count--;
revision++;
bitmapStorageChanged();
forget(name);
listRemove(name);
return nullptr;
}