Files
tftest/src/ui.cpp
DESKTOP-PUHFO60\dsx2016 b09fca956f Calibrate touch from serial logs: invert physical X axis.
Bottom LVGL buttons were mapped to the top; add on-screen logic coordinate debug.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-17 18:01:05 +08:00

202 lines
6.7 KiB
C++

#include "ui.h"
#include "audio_test.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_audio;
static lv_obj_t *bar_usage;
static lv_obj_t *btn_speaker;
static lv_obj_t *btn_play;
static lv_obj_t *lbl_speaker;
static lv_obj_t *lbl_play;
static lv_obj_t *lbl_touch_dbg;
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, 1, 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_14, 0);
*value_out = v;
return row;
}
static void on_speaker_clicked(lv_event_t *e) {
LV_UNUSED(e);
audio_set_enabled(!audio_is_enabled());
ui_refresh_audio_buttons();
}
static void on_play_clicked(lv_event_t *e) {
LV_UNUSED(e);
if (audio_is_playing()) {
audio_stop();
} else {
audio_play_music();
}
ui_refresh_audio_buttons();
}
void ui_refresh_audio_buttons() {
if (lbl_speaker) {
lv_label_set_text(lbl_speaker, audio_is_enabled() ? "Speaker ON" : "Speaker OFF");
}
if (btn_speaker) {
lv_obj_set_style_bg_color(
btn_speaker,
audio_is_enabled() ? lv_color_hex(0x2E7D32) : lv_color_hex(0x455A64), 0);
}
if (lbl_play) {
lv_label_set_text(lbl_play, audio_is_playing() ? "Stop" : "Play Music");
}
if (btn_play) {
lv_obj_set_style_bg_color(
btn_play,
audio_is_playing() ? lv_color_hex(0xC62828) : lv_color_hex(0x1565C0), 0);
}
if (lbl_audio) {
lv_label_set_text(lbl_audio, audio_last_status());
}
}
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, 10, 0);
lv_obj_set_style_pad_right(col, 10, 0);
lv_obj_set_style_pad_top(col, 6, 0);
lv_obj_set_style_pad_bottom(col, 6, 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, 2, 0);
lv_obj_clear_flag(col, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_t *title = lv_label_create(col);
lv_label_set_text(title, "TF + Speaker");
lv_obj_set_style_text_font(title, &lv_font_montserrat_18, 0);
lv_obj_set_style_text_color(title, lv_color_hex(0x4FC3F7), 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, "Audio", &lbl_audio);
bar_usage = lv_bar_create(col);
lv_obj_set_width(bar_usage, LV_PCT(100));
lv_obj_set_height(bar_usage, 10);
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_color(bar_usage, lv_color_hex(0x29B6F6), LV_PART_INDICATOR);
// 底部两个触控按钮
lv_obj_t *row_btns = lv_obj_create(col);
lv_obj_set_width(row_btns, LV_PCT(100));
lv_obj_set_height(row_btns, 52);
lv_obj_set_style_bg_opa(row_btns, LV_OPA_TRANSP, 0);
lv_obj_set_style_border_width(row_btns, 0, 0);
lv_obj_set_style_pad_all(row_btns, 0, 0);
lv_obj_set_flex_flow(row_btns, LV_FLEX_FLOW_ROW);
lv_obj_set_flex_align(row_btns, LV_FLEX_ALIGN_SPACE_BETWEEN, LV_FLEX_ALIGN_CENTER,
LV_FLEX_ALIGN_CENTER);
lv_obj_clear_flag(row_btns, LV_OBJ_FLAG_SCROLLABLE);
btn_speaker = lv_button_create(row_btns);
lv_obj_set_size(btn_speaker, 148, 50);
lv_obj_set_style_radius(btn_speaker, 8, 0);
lv_obj_add_flag(btn_speaker, LV_OBJ_FLAG_CLICKABLE);
lv_obj_add_event_cb(btn_speaker, on_speaker_clicked, LV_EVENT_CLICKED, nullptr);
lbl_speaker = lv_label_create(btn_speaker);
lv_label_set_text(lbl_speaker, "Speaker OFF");
lv_obj_set_style_text_font(lbl_speaker, &lv_font_montserrat_14, 0);
lv_obj_center(lbl_speaker);
btn_play = lv_button_create(row_btns);
lv_obj_set_size(btn_play, 148, 50);
lv_obj_set_style_radius(btn_play, 8, 0);
lv_obj_add_flag(btn_play, LV_OBJ_FLAG_CLICKABLE);
lv_obj_add_event_cb(btn_play, on_play_clicked, LV_EVENT_CLICKED, nullptr);
lbl_play = lv_label_create(btn_play);
lv_label_set_text(lbl_play, "Play Music");
lv_obj_set_style_text_font(lbl_play, &lv_font_montserrat_14, 0);
lv_obj_center(lbl_play);
lbl_touch_dbg = lv_label_create(col);
lv_label_set_text(lbl_touch_dbg, "Touch: --");
lv_obj_set_style_text_font(lbl_touch_dbg, &lv_font_montserrat_14, 0);
lv_obj_set_style_text_color(lbl_touch_dbg, lv_color_hex(0xFFB74D), 0);
ui_refresh_audio_buttons();
}
void ui_set_touch_debug(int16_t lx, int16_t ly, bool down) {
if (!lbl_touch_dbg) {
return;
}
if (!down) {
lv_label_set_text(lbl_touch_dbg, "Touch: up");
return;
}
char buf[40];
snprintf(buf, sizeof(buf), "Touch L:%d,%d", (int)lx, (int)ly);
lv_label_set_text(lbl_touch_dbg, buf);
}
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_bar_set_value(bar_usage, 0, LV_ANIM_OFF);
} else {
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);
lv_label_set_text(lbl_capacity, cap.c_str());
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);
}
ui_refresh_audio_buttons();
}