2026-10-16 22:03:18 +02:00
|
|
|
#include "display/icon_store.h"
|
|
|
|
|
|
|
|
|
|
#include <LittleFS.h>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <map>
|
|
|
|
|
#include <strings.h>
|
|
|
|
|
|
|
|
|
|
#include "display/bitmap_store.h"
|
2026-10-16 22:18:58 +02:00
|
|
|
#include "display/bitmaps.h"
|
2026-10-16 22:03:18 +02:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-16 22:03:18 +02:00
|
|
|
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
|
|
|
|
|
struct CachedIcon
|
|
|
|
|
{
|
|
|
|
|
bool exists;
|
|
|
|
|
Icon icon;
|
|
|
|
|
};
|
|
|
|
|
static std::map<String, CachedIcon> cache;
|
|
|
|
|
static const size_t CACHE_MAX = 64;
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-16 22:03:18 +02:00
|
|
|
std::vector<String> listIcons()
|
|
|
|
|
{
|
|
|
|
|
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);
|
2026-10-16 22:03:18 +02:00
|
|
|
std::sort(names.begin(), names.end(),
|
|
|
|
|
[](const String &a, const String &b) { return strcasecmp(a.c_str(), b.c_str()) < 0; });
|
|
|
|
|
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);
|
2026-10-16 22:03:18 +02:00
|
|
|
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);
|
2026-10-16 22:03:18 +02:00
|
|
|
if (cache.size() >= CACHE_MAX)
|
|
|
|
|
cache.clear();
|
|
|
|
|
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++;
|
|
|
|
|
cache.clear();
|
|
|
|
|
if (!ok)
|
|
|
|
|
{
|
|
|
|
|
LittleFS.remove(path(name)); // not half an icon
|
|
|
|
|
count = -1;
|
|
|
|
|
return "Storage full";
|
|
|
|
|
}
|
|
|
|
|
if (is_new)
|
|
|
|
|
count++;
|
|
|
|
|
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++;
|
|
|
|
|
cache.clear();
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|