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

165 lines
3.8 KiB
C++
Raw Normal View History

#include "display/icon_store.h"
#include <LittleFS.h>
#include <algorithm>
#include <map>
#include <strings.h>
#include "display/bitmap_store.h"
static const char *DIR = "/icons";
// Written when the bitmaps were moved (not a valid name, so never listed)
static const char *MIGRATED = "/icons/.migrated";
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);
}
std::vector<String> listIcons()
{
std::vector<String> names = storedNames(DIR, ".bin");
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))
return false;
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();
}
}
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;
}
bool iconMigrationDone()
{
return !bitmapStorageAvailable() || LittleFS.exists(MIGRATED);
}
void setIconMigrationDone()
{
if (!ensureDir())
return;
File file = LittleFS.open(MIGRATED, "w");
if (file)
{
file.print("1");
file.close();
}
}