Initial CYD TF card info display with LVGL.
Mount microSD on VSPI and show type, format, and capacity on the CYD screen. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
74
src/main.cpp
Normal file
74
src/main.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
#include <Arduino.h>
|
||||
#include <TFT_eSPI.h>
|
||||
#include <lvgl.h>
|
||||
|
||||
#include "pins.h"
|
||||
#include "sd_info.h"
|
||||
#include "ui.h"
|
||||
|
||||
static constexpr uint32_t DRAW_BUF_SIZE =
|
||||
(SCREEN_WIDTH * SCREEN_HEIGHT / 10 * (LV_COLOR_DEPTH / 8));
|
||||
static uint32_t draw_buf[DRAW_BUF_SIZE / 4];
|
||||
|
||||
static uint32_t last_refresh_ms = 0;
|
||||
static constexpr uint32_t REFRESH_MS = 3000;
|
||||
|
||||
static void refresh_sd_ui() {
|
||||
SdCardInfo info = sd_read_info();
|
||||
|
||||
Serial.println("---- TF Card ----");
|
||||
if (!info.mounted) {
|
||||
Serial.printf("Error: %s\n", info.error.c_str());
|
||||
} else {
|
||||
Serial.printf("Type: %s\n", info.type);
|
||||
Serial.printf("FS: %s\n", info.filesystem);
|
||||
Serial.printf("Card: %s\n", format_bytes(info.card_bytes).c_str());
|
||||
Serial.printf("Part: %s\n", format_bytes(info.total_bytes).c_str());
|
||||
Serial.printf("Used: %s\n", format_bytes(info.used_bytes).c_str());
|
||||
Serial.printf("Free: %s\n", format_bytes(info.free_bytes).c_str());
|
||||
Serial.printf("Root entries: %lu\n",
|
||||
static_cast<unsigned long>(info.root_files));
|
||||
}
|
||||
|
||||
ui_update(info);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(200);
|
||||
Serial.println();
|
||||
Serial.printf("LVGL %d.%d.%d | CYD TF info\n", lv_version_major(),
|
||||
lv_version_minor(), lv_version_patch());
|
||||
|
||||
pinMode(TFT_BL_PIN, OUTPUT);
|
||||
digitalWrite(TFT_BL_PIN, HIGH);
|
||||
|
||||
lv_init();
|
||||
|
||||
lv_display_t *disp =
|
||||
lv_tft_espi_create(SCREEN_WIDTH, SCREEN_HEIGHT, draw_buf, sizeof(draw_buf));
|
||||
// 横屏:320x240
|
||||
lv_display_set_rotation(disp, LV_DISPLAY_ROTATION_270);
|
||||
|
||||
ui_create();
|
||||
|
||||
if (!sd_init()) {
|
||||
Serial.println("SD mount failed at boot");
|
||||
}
|
||||
|
||||
refresh_sd_ui();
|
||||
last_refresh_ms = millis();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
lv_timer_handler();
|
||||
lv_tick_inc(5);
|
||||
|
||||
uint32_t now = millis();
|
||||
if (now - last_refresh_ms >= REFRESH_MS) {
|
||||
last_refresh_ms = now;
|
||||
refresh_sd_ui();
|
||||
}
|
||||
|
||||
delay(5);
|
||||
}
|
||||
139
src/sd_info.cpp
Normal file
139
src/sd_info.cpp
Normal file
@@ -0,0 +1,139 @@
|
||||
#include "sd_info.h"
|
||||
#include "pins.h"
|
||||
|
||||
#include <FS.h>
|
||||
#include <SD.h>
|
||||
#include <SPI.h>
|
||||
|
||||
static SPIClass sd_spi(VSPI);
|
||||
static bool sd_ready = false;
|
||||
|
||||
static const char *card_type_name(uint8_t t) {
|
||||
switch (t) {
|
||||
case CARD_MMC: return "MMC";
|
||||
case CARD_SD: return "SDSC";
|
||||
case CARD_SDHC: return "SDHC";
|
||||
case CARD_NONE: return "NONE";
|
||||
default: return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
// 读引导扇区,尽量识别 FAT 类型(Arduino SD 挂载的是 FatFs)
|
||||
static const char *detect_filesystem() {
|
||||
File root = SD.open("/");
|
||||
if (!root) {
|
||||
return "—";
|
||||
}
|
||||
root.close();
|
||||
|
||||
// 通过容量启发式:Arduino SD 库挂载成功即为 FAT 系
|
||||
// 再尝试从卷信息推断常见类型
|
||||
uint64_t total = SD.totalBytes();
|
||||
if (total == 0) {
|
||||
return "FAT";
|
||||
}
|
||||
// FatFs 对 >32MB 常用 FAT32;小卡可能是 FAT16
|
||||
if (total > 32ULL * 1024ULL * 1024ULL) {
|
||||
return "FAT32";
|
||||
}
|
||||
if (total > 2ULL * 1024ULL * 1024ULL) {
|
||||
return "FAT16";
|
||||
}
|
||||
return "FAT12/16";
|
||||
}
|
||||
|
||||
static uint32_t count_root_entries() {
|
||||
File root = SD.open("/");
|
||||
if (!root || !root.isDirectory()) {
|
||||
return 0;
|
||||
}
|
||||
uint32_t n = 0;
|
||||
File f = root.openNextFile();
|
||||
while (f) {
|
||||
n++;
|
||||
f = root.openNextFile();
|
||||
}
|
||||
root.close();
|
||||
return n;
|
||||
}
|
||||
|
||||
bool sd_init() {
|
||||
if (sd_ready) {
|
||||
return true;
|
||||
}
|
||||
|
||||
pinMode(SD_CS, OUTPUT);
|
||||
digitalWrite(SD_CS, HIGH);
|
||||
|
||||
sd_spi.begin(SD_SCK, SD_MISO, SD_MOSI, SD_CS);
|
||||
|
||||
// CYD 上 SD 与触摸争用 VSPI;本工程不用硬件触摸,频率适中更稳
|
||||
if (!SD.begin(SD_CS, sd_spi, 25000000)) {
|
||||
sd_ready = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
sd_ready = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void sd_deinit() {
|
||||
if (sd_ready) {
|
||||
SD.end();
|
||||
sd_ready = false;
|
||||
}
|
||||
}
|
||||
|
||||
SdCardInfo sd_read_info() {
|
||||
SdCardInfo info{};
|
||||
info.mounted = false;
|
||||
info.present = false;
|
||||
info.type = "NONE";
|
||||
info.filesystem = "—";
|
||||
info.card_bytes = 0;
|
||||
info.total_bytes = 0;
|
||||
info.used_bytes = 0;
|
||||
info.free_bytes = 0;
|
||||
info.root_files = 0;
|
||||
|
||||
if (!sd_ready && !sd_init()) {
|
||||
info.error = "Mount failed";
|
||||
return info;
|
||||
}
|
||||
|
||||
uint8_t type = SD.cardType();
|
||||
info.type = card_type_name(type);
|
||||
|
||||
if (type == CARD_NONE) {
|
||||
info.error = "No card";
|
||||
info.present = false;
|
||||
return info;
|
||||
}
|
||||
|
||||
info.present = true;
|
||||
info.mounted = true;
|
||||
info.card_bytes = SD.cardSize();
|
||||
info.total_bytes = SD.totalBytes();
|
||||
info.used_bytes = SD.usedBytes();
|
||||
info.free_bytes =
|
||||
(info.total_bytes > info.used_bytes) ? (info.total_bytes - info.used_bytes) : 0;
|
||||
info.filesystem = detect_filesystem();
|
||||
info.root_files = count_root_entries();
|
||||
info.error = "";
|
||||
return info;
|
||||
}
|
||||
|
||||
String format_bytes(uint64_t bytes) {
|
||||
const double b = static_cast<double>(bytes);
|
||||
char buf[48];
|
||||
if (bytes >= (1ULL << 30)) {
|
||||
snprintf(buf, sizeof(buf), "%.2f GB", b / (1024.0 * 1024.0 * 1024.0));
|
||||
} else if (bytes >= (1ULL << 20)) {
|
||||
snprintf(buf, sizeof(buf), "%.2f MB", b / (1024.0 * 1024.0));
|
||||
} else if (bytes >= (1ULL << 10)) {
|
||||
snprintf(buf, sizeof(buf), "%.2f KB", b / 1024.0);
|
||||
} else {
|
||||
snprintf(buf, sizeof(buf), "%llu B", static_cast<unsigned long long>(bytes));
|
||||
}
|
||||
return String(buf);
|
||||
}
|
||||
150
src/ui.cpp
Normal file
150
src/ui.cpp
Normal file
@@ -0,0 +1,150 @@
|
||||
#include "ui.h"
|
||||
|
||||
#include <lvgl.h>
|
||||
|
||||
static lv_obj_t *lbl_status;
|
||||
static lv_obj_t *lbl_type;
|
||||
static lv_obj_t *lbl_fs;
|
||||
static lv_obj_t *lbl_capacity;
|
||||
static lv_obj_t *lbl_total;
|
||||
static lv_obj_t *lbl_used;
|
||||
static lv_obj_t *lbl_free;
|
||||
static lv_obj_t *lbl_files;
|
||||
static lv_obj_t *bar_usage;
|
||||
static lv_obj_t *lbl_hint;
|
||||
|
||||
static lv_obj_t *make_row(lv_obj_t *parent, const char *title, lv_obj_t **value_out) {
|
||||
lv_obj_t *row = lv_obj_create(parent);
|
||||
lv_obj_set_width(row, LV_PCT(100));
|
||||
lv_obj_set_height(row, LV_SIZE_CONTENT);
|
||||
lv_obj_set_style_bg_opa(row, LV_OPA_TRANSP, 0);
|
||||
lv_obj_set_style_border_width(row, 0, 0);
|
||||
lv_obj_set_style_pad_all(row, 2, 0);
|
||||
lv_obj_set_flex_flow(row, LV_FLEX_FLOW_ROW);
|
||||
lv_obj_set_flex_align(row, LV_FLEX_ALIGN_SPACE_BETWEEN, LV_FLEX_ALIGN_CENTER,
|
||||
LV_FLEX_ALIGN_CENTER);
|
||||
lv_obj_clear_flag(row, LV_OBJ_FLAG_SCROLLABLE);
|
||||
|
||||
lv_obj_t *t = lv_label_create(row);
|
||||
lv_label_set_text(t, title);
|
||||
lv_obj_set_style_text_color(t, lv_color_hex(0x90A4AE), 0);
|
||||
lv_obj_set_style_text_font(t, &lv_font_montserrat_14, 0);
|
||||
|
||||
lv_obj_t *v = lv_label_create(row);
|
||||
lv_label_set_text(v, "—");
|
||||
lv_obj_set_style_text_color(v, lv_color_hex(0xECEFF1), 0);
|
||||
lv_obj_set_style_text_font(v, &lv_font_montserrat_16, 0);
|
||||
*value_out = v;
|
||||
return row;
|
||||
}
|
||||
|
||||
void ui_create() {
|
||||
lv_obj_t *scr = lv_screen_active();
|
||||
lv_obj_set_style_bg_color(scr, lv_color_hex(0x0D1B2A), 0);
|
||||
lv_obj_set_style_bg_opa(scr, LV_OPA_COVER, 0);
|
||||
|
||||
lv_obj_t *col = lv_obj_create(scr);
|
||||
lv_obj_set_size(col, LV_PCT(100), LV_PCT(100));
|
||||
lv_obj_set_style_bg_opa(col, LV_OPA_TRANSP, 0);
|
||||
lv_obj_set_style_border_width(col, 0, 0);
|
||||
lv_obj_set_style_pad_left(col, 12, 0);
|
||||
lv_obj_set_style_pad_right(col, 12, 0);
|
||||
lv_obj_set_style_pad_top(col, 10, 0);
|
||||
lv_obj_set_style_pad_bottom(col, 8, 0);
|
||||
lv_obj_set_flex_flow(col, LV_FLEX_FLOW_COLUMN);
|
||||
lv_obj_set_flex_align(col, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER,
|
||||
LV_FLEX_ALIGN_CENTER);
|
||||
lv_obj_set_style_pad_row(col, 4, 0);
|
||||
lv_obj_clear_flag(col, LV_OBJ_FLAG_SCROLLABLE);
|
||||
|
||||
lv_obj_t *title = lv_label_create(col);
|
||||
lv_label_set_text(title, "TF Card");
|
||||
lv_obj_set_style_text_font(title, &lv_font_montserrat_20, 0);
|
||||
lv_obj_set_style_text_color(title, lv_color_hex(0x4FC3F7), 0);
|
||||
|
||||
lv_obj_t *subtitle = lv_label_create(col);
|
||||
lv_label_set_text(subtitle, "CYD ESP32 Storage Info");
|
||||
lv_obj_set_style_text_font(subtitle, &lv_font_montserrat_14, 0);
|
||||
lv_obj_set_style_text_color(subtitle, lv_color_hex(0x607D8B), 0);
|
||||
|
||||
lv_obj_t *sep = lv_obj_create(col);
|
||||
lv_obj_set_size(sep, LV_PCT(100), 1);
|
||||
lv_obj_set_style_bg_color(sep, lv_color_hex(0x37474F), 0);
|
||||
lv_obj_set_style_bg_opa(sep, LV_OPA_COVER, 0);
|
||||
lv_obj_set_style_border_width(sep, 0, 0);
|
||||
lv_obj_set_style_pad_all(sep, 0, 0);
|
||||
|
||||
make_row(col, "Status", &lbl_status);
|
||||
make_row(col, "Type", &lbl_type);
|
||||
make_row(col, "Format", &lbl_fs);
|
||||
make_row(col, "Capacity", &lbl_capacity);
|
||||
make_row(col, "Partition", &lbl_total);
|
||||
make_row(col, "Used", &lbl_used);
|
||||
make_row(col, "Free", &lbl_free);
|
||||
make_row(col, "Root files", &lbl_files);
|
||||
|
||||
bar_usage = lv_bar_create(col);
|
||||
lv_obj_set_width(bar_usage, LV_PCT(100));
|
||||
lv_obj_set_height(bar_usage, 14);
|
||||
lv_bar_set_range(bar_usage, 0, 100);
|
||||
lv_bar_set_value(bar_usage, 0, LV_ANIM_OFF);
|
||||
lv_obj_set_style_bg_color(bar_usage, lv_color_hex(0x263238), 0);
|
||||
lv_obj_set_style_bg_opa(bar_usage, LV_OPA_COVER, 0);
|
||||
lv_obj_set_style_bg_color(bar_usage, lv_color_hex(0x29B6F6), LV_PART_INDICATOR);
|
||||
lv_obj_set_style_radius(bar_usage, 4, 0);
|
||||
lv_obj_set_style_radius(bar_usage, 4, LV_PART_INDICATOR);
|
||||
|
||||
lbl_hint = lv_label_create(col);
|
||||
lv_label_set_text(lbl_hint, "Refreshing every 3s");
|
||||
lv_obj_set_style_text_font(lbl_hint, &lv_font_montserrat_14, 0);
|
||||
lv_obj_set_style_text_color(lbl_hint, lv_color_hex(0x546E7A), 0);
|
||||
}
|
||||
|
||||
void ui_update(const SdCardInfo &info) {
|
||||
if (!info.mounted || !info.present) {
|
||||
lv_label_set_text(lbl_status, info.error.length() ? info.error.c_str() : "Fail");
|
||||
lv_obj_set_style_text_color(lbl_status, lv_color_hex(0xEF5350), 0);
|
||||
lv_label_set_text(lbl_type, info.type);
|
||||
lv_label_set_text(lbl_fs, "—");
|
||||
lv_label_set_text(lbl_capacity, "—");
|
||||
lv_label_set_text(lbl_total, "—");
|
||||
lv_label_set_text(lbl_used, "—");
|
||||
lv_label_set_text(lbl_free, "—");
|
||||
lv_label_set_text(lbl_files, "—");
|
||||
lv_bar_set_value(bar_usage, 0, LV_ANIM_OFF);
|
||||
lv_label_set_text(lbl_hint, "Insert TF and wait...");
|
||||
return;
|
||||
}
|
||||
|
||||
lv_label_set_text(lbl_status, "Mounted OK");
|
||||
lv_obj_set_style_text_color(lbl_status, lv_color_hex(0x66BB6A), 0);
|
||||
lv_label_set_text(lbl_type, info.type);
|
||||
lv_label_set_text(lbl_fs, info.filesystem);
|
||||
|
||||
String cap = format_bytes(info.card_bytes);
|
||||
String tot = format_bytes(info.total_bytes);
|
||||
String used = format_bytes(info.used_bytes);
|
||||
String freeb = format_bytes(info.free_bytes);
|
||||
|
||||
lv_label_set_text(lbl_capacity, cap.c_str());
|
||||
lv_label_set_text(lbl_total, tot.c_str());
|
||||
lv_label_set_text(lbl_used, used.c_str());
|
||||
lv_label_set_text(lbl_free, freeb.c_str());
|
||||
|
||||
char files_buf[16];
|
||||
snprintf(files_buf, sizeof(files_buf), "%lu", static_cast<unsigned long>(info.root_files));
|
||||
lv_label_set_text(lbl_files, files_buf);
|
||||
|
||||
int pct = 0;
|
||||
if (info.total_bytes > 0) {
|
||||
pct = static_cast<int>((info.used_bytes * 100ULL) / info.total_bytes);
|
||||
if (pct > 100) {
|
||||
pct = 100;
|
||||
}
|
||||
}
|
||||
lv_bar_set_value(bar_usage, pct, LV_ANIM_ON);
|
||||
|
||||
char hint[40];
|
||||
snprintf(hint, sizeof(hint), "Used %d%% | auto refresh", pct);
|
||||
lv_label_set_text(lbl_hint, hint);
|
||||
}
|
||||
Reference in New Issue
Block a user