Add storage status to SystemInfo

This commit is contained in:
Ken Van Hoeylandt 2025-09-06 23:00:38 +02:00
parent d86c11d2ac
commit 67b79b0efa

View File

@ -5,10 +5,16 @@
#include <Tactility/Tactility.h>
#include <lvgl.h>
#include <utility>
#ifdef ESP_PLATFORM
#include <esp_vfs_fat.h>
#include <Tactility/MountPoints.h>
#endif
namespace tt::app::systeminfo {
#define TAG "system_info"
constexpr auto* TAG = "SystemInfo";
static size_t getHeapFree() {
#ifdef ESP_PLATFORM
@ -42,7 +48,60 @@ static size_t getSpiTotal() {
#endif
}
static void addMemoryBar(lv_obj_t* parent, const char* label, size_t used, size_t total) {
enum class StorageUnit {
Bytes,
Kilobytes,
Megabytes,
Gigabytes
};
static StorageUnit getStorageUnit(uint64_t value) {
using enum StorageUnit;
if (value / (1024 * 1024 * 1024) > 0) {
return Gigabytes;
} else if (value / (1024 * 1024) > 0) {
return Megabytes;
} else if (value / 1024 > 0) {
return Kilobytes;
} else {
return Bytes;
}
}
static std::string getStorageUnitString(StorageUnit unit) {
using enum StorageUnit;
switch (unit) {
case Bytes:
return "bytes";
case Kilobytes:
return "kB";
case Megabytes:
return "MB";
case Gigabytes:
return "GB";
default:
std::unreachable();
}
}
static uint64_t getStorageValue(StorageUnit unit, uint64_t bytes) {
using enum StorageUnit;
switch (unit) {
case Bytes:
return bytes;
case Kilobytes:
return bytes / 1024;
case Megabytes:
return bytes / 1024 / 1024;
case Gigabytes:
return bytes / 1024 / 1024 / 1024;
default:
std::unreachable();
}
}
static void addMemoryBar(lv_obj_t* parent, const char* label, uint64_t free, uint64_t total) {
uint64_t used = total - free;
auto* container = lv_obj_create(parent);
lv_obj_set_size(container, LV_PCT(100), LV_SIZE_CONTENT);
lv_obj_set_style_pad_all(container, 0, 0);
@ -65,7 +124,11 @@ static void addMemoryBar(lv_obj_t* parent, const char* label, size_t used, size_
lv_bar_set_value(bar, (int32_t)used, LV_ANIM_OFF);
auto* bottom_label = lv_label_create(parent);
lv_label_set_text_fmt(bottom_label, "%u / %u kB", (used / 1024), (total / 1024));
const auto unit = getStorageUnit(total);
const auto unit_label = getStorageUnitString(unit);
const auto used_converted = getStorageValue(unit, used);
const auto total_converted = getStorageValue(unit, total);
lv_label_set_text_fmt(bottom_label, "%llu / %llu %s", used_converted, total_converted, unit_label.c_str());
lv_obj_set_width(bottom_label, LV_PCT(100));
lv_obj_set_style_text_align(bottom_label, LV_TEXT_ALIGN_RIGHT, 0);
}
@ -142,11 +205,43 @@ class SystemInfoApp : public App {
lv_obj_set_flex_flow(memory_wrapper, LV_FLEX_FLOW_COLUMN);
lv_obj_set_size(memory_wrapper, LV_PCT(100), LV_SIZE_CONTENT);
addMemoryBar(memory_wrapper, "Internal", getHeapTotal() - getHeapFree(), getHeapTotal());
addMemoryBar(memory_wrapper, "Internal", getHeapFree(), getHeapTotal());
if (getSpiTotal() > 0) {
addMemoryBar(memory_wrapper, "External", getSpiTotal() - getSpiFree(), getSpiTotal());
addMemoryBar(memory_wrapper, "External", getSpiFree(), getSpiTotal());
}
#ifdef ESP_PLATFORM
// Wrapper for the memory usage bars
auto* storage_label = lv_label_create(wrapper);
lv_label_set_text(storage_label, "Storage usage");
auto* storage_wrapper = lv_obj_create(wrapper);
lv_obj_set_flex_flow(storage_wrapper, LV_FLEX_FLOW_COLUMN);
lv_obj_set_size(storage_wrapper, LV_PCT(100), LV_SIZE_CONTENT);
uint64_t storage_total = 0;
uint64_t storage_free = 0;
if (esp_vfs_fat_info(file::MOUNT_POINT_SYSTEM, &storage_total, &storage_free) == ESP_OK) {
addMemoryBar(storage_wrapper, file::MOUNT_POINT_SYSTEM, storage_free, storage_total);
}
if (esp_vfs_fat_info(file::MOUNT_POINT_DATA, &storage_total, &storage_free) == ESP_OK) {
addMemoryBar(storage_wrapper, file::MOUNT_POINT_DATA, storage_free, storage_total);
}
const auto sdcard_devices = hal::findDevices<hal::sdcard::SdCardDevice>(hal::Device::Type::SdCard);
for (const auto& sdcard : sdcard_devices) {
if (sdcard->isMounted() && esp_vfs_fat_info(sdcard->getMountPath().c_str(), &storage_total, &storage_free) == ESP_OK) {
addMemoryBar(
storage_wrapper,
sdcard->getMountPath().c_str(),
storage_free,
storage_total
);
}
}
#endif
#if configUSE_TRACE_FACILITY
auto* tasks_label = lv_label_create(wrapper);
lv_label_set_text(tasks_label, "Tasks");