Strip TF and audio; keep touch-only screen test UI.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,132 +0,0 @@
|
||||
#include "audio_test.h"
|
||||
#include "pins.h"
|
||||
|
||||
static const char *g_status = "Speaker OFF";
|
||||
static bool g_enabled = false;
|
||||
static bool g_playing = false;
|
||||
static size_t g_note_i = 0;
|
||||
static uint32_t g_until_ms = 0;
|
||||
|
||||
enum Phase : uint8_t { kStartNote, kWaitNote, kWaitGap };
|
||||
static Phase g_phase = kStartNote;
|
||||
|
||||
struct Note {
|
||||
uint16_t freq;
|
||||
uint16_t ms;
|
||||
};
|
||||
|
||||
// Twinkle Twinkle Little Star
|
||||
static const Note k_music[] = {
|
||||
{262, 280}, {262, 280}, {392, 280}, {392, 280}, {440, 280}, {440, 280}, {392, 520},
|
||||
{349, 280}, {349, 280}, {330, 280}, {330, 280}, {294, 280}, {294, 280}, {262, 520},
|
||||
{392, 280}, {392, 280}, {349, 280}, {349, 280}, {330, 280}, {330, 280}, {294, 520},
|
||||
{392, 280}, {392, 280}, {349, 280}, {349, 280}, {330, 280}, {330, 280}, {294, 520},
|
||||
{262, 280}, {262, 280}, {392, 280}, {392, 280}, {440, 280}, {440, 280}, {392, 520},
|
||||
{349, 280}, {349, 280}, {330, 280}, {330, 280}, {294, 280}, {294, 280}, {262, 700},
|
||||
};
|
||||
|
||||
static constexpr size_t k_music_len = sizeof(k_music) / sizeof(k_music[0]);
|
||||
|
||||
static void speaker_off_pin() {
|
||||
noTone(SPEAKER_PIN);
|
||||
digitalWrite(SPEAKER_PIN, LOW);
|
||||
}
|
||||
|
||||
void audio_init() {
|
||||
pinMode(SPEAKER_PIN, OUTPUT);
|
||||
speaker_off_pin();
|
||||
g_enabled = false;
|
||||
g_playing = false;
|
||||
g_status = "Speaker OFF";
|
||||
Serial.printf("Speaker init GPIO%d (touch-controlled)\n", SPEAKER_PIN);
|
||||
}
|
||||
|
||||
void audio_set_enabled(bool on) {
|
||||
g_enabled = on;
|
||||
if (!on) {
|
||||
audio_stop();
|
||||
g_status = "Speaker OFF";
|
||||
} else {
|
||||
g_status = "Speaker ON";
|
||||
tone(SPEAKER_PIN, 1000);
|
||||
delay(80);
|
||||
speaker_off_pin();
|
||||
}
|
||||
Serial.printf("Speaker %s\n", on ? "ON" : "OFF");
|
||||
}
|
||||
|
||||
bool audio_is_enabled() {
|
||||
return g_enabled;
|
||||
}
|
||||
|
||||
void audio_stop() {
|
||||
g_playing = false;
|
||||
g_note_i = 0;
|
||||
g_phase = kStartNote;
|
||||
speaker_off_pin();
|
||||
if (g_enabled) {
|
||||
g_status = "Speaker ON";
|
||||
}
|
||||
}
|
||||
|
||||
bool audio_is_playing() {
|
||||
return g_playing;
|
||||
}
|
||||
|
||||
void audio_play_music() {
|
||||
if (!g_enabled) {
|
||||
g_status = "Turn ON first";
|
||||
Serial.println("Audio: blocked (speaker off)");
|
||||
return;
|
||||
}
|
||||
g_playing = true;
|
||||
g_note_i = 0;
|
||||
g_phase = kStartNote;
|
||||
g_until_ms = 0;
|
||||
g_status = "Playing...";
|
||||
Serial.println("Audio: play music");
|
||||
}
|
||||
|
||||
void audio_tick() {
|
||||
if (!g_playing || !g_enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
const uint32_t now = millis();
|
||||
if (now < g_until_ms) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (g_phase == kWaitNote) {
|
||||
speaker_off_pin();
|
||||
g_phase = kWaitGap;
|
||||
g_until_ms = now + 35;
|
||||
g_note_i++;
|
||||
return;
|
||||
}
|
||||
|
||||
if (g_phase == kWaitGap) {
|
||||
g_phase = kStartNote;
|
||||
}
|
||||
|
||||
// kStartNote
|
||||
if (g_note_i >= k_music_len) {
|
||||
audio_stop();
|
||||
g_status = "Music done";
|
||||
Serial.println("Audio: music done");
|
||||
return;
|
||||
}
|
||||
|
||||
const Note &n = k_music[g_note_i];
|
||||
if (n.freq == 0) {
|
||||
speaker_off_pin();
|
||||
} else {
|
||||
tone(SPEAKER_PIN, n.freq);
|
||||
}
|
||||
g_until_ms = now + n.ms;
|
||||
g_phase = kWaitNote;
|
||||
}
|
||||
|
||||
const char *audio_last_status() {
|
||||
return g_status;
|
||||
}
|
||||
49
src/main.cpp
49
src/main.cpp
@@ -1,10 +1,7 @@
|
||||
#include <Arduino.h>
|
||||
#include <TFT_eSPI.h>
|
||||
#include <lvgl.h>
|
||||
|
||||
#include "audio_test.h"
|
||||
#include "pins.h"
|
||||
#include "sd_info.h"
|
||||
#include "touch.h"
|
||||
#include "ui.h"
|
||||
|
||||
@@ -12,41 +9,21 @@ 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 uint32_t last_btn_ui_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 FS: %s Cap: %s\n", info.type, info.filesystem,
|
||||
format_bytes(info.card_bytes).c_str());
|
||||
}
|
||||
|
||||
ui_update(info);
|
||||
}
|
||||
static uint32_t last_dbg_ms = 0;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(200);
|
||||
Serial.println();
|
||||
Serial.printf("LVGL %d.%d.%d | CYD TF + Touch Speaker\n", lv_version_major(),
|
||||
Serial.printf("LVGL %d.%d.%d | touch test only\n", lv_version_major(),
|
||||
lv_version_minor(), lv_version_patch());
|
||||
|
||||
pinMode(TFT_BL_PIN, OUTPUT);
|
||||
digitalWrite(TFT_BL_PIN, HIGH);
|
||||
|
||||
audio_init();
|
||||
touch_init();
|
||||
|
||||
lv_init();
|
||||
|
||||
// Sunton 官方:240x320 + ROTATION_270 → 横屏(勿 create 320x240,会花屏)
|
||||
// 触摸见 touch.cpp(官方 mirror_x + 日志补偿)
|
||||
lv_display_t *disp =
|
||||
lv_tft_espi_create(SCREEN_WIDTH, SCREEN_HEIGHT, draw_buf, sizeof(draw_buf));
|
||||
lv_display_set_rotation(disp, LV_DISPLAY_ROTATION_270);
|
||||
@@ -57,31 +34,15 @@ void setup() {
|
||||
lv_indev_set_display(indev, disp);
|
||||
|
||||
ui_create();
|
||||
|
||||
if (!sd_init()) {
|
||||
Serial.println("SD mount failed at boot");
|
||||
}
|
||||
|
||||
refresh_sd_ui();
|
||||
last_refresh_ms = millis();
|
||||
last_btn_ui_ms = millis();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
lv_timer_handler();
|
||||
lv_tick_inc(5);
|
||||
audio_tick();
|
||||
|
||||
uint32_t now = millis();
|
||||
if (now - last_refresh_ms >= REFRESH_MS) {
|
||||
last_refresh_ms = now;
|
||||
refresh_sd_ui();
|
||||
}
|
||||
|
||||
// 播放过程中刷新按钮文字;同步触点调试
|
||||
if (now - last_btn_ui_ms >= 200) {
|
||||
last_btn_ui_ms = now;
|
||||
ui_refresh_audio_buttons();
|
||||
const uint32_t now = millis();
|
||||
if (now - last_dbg_ms >= 50) {
|
||||
last_dbg_ms = now;
|
||||
ui_set_touch_debug(g_dbg_logic_x, g_dbg_logic_y, g_dbg_touch_down);
|
||||
}
|
||||
|
||||
|
||||
137
src/sd_info.cpp
137
src/sd_info.cpp
@@ -1,137 +0,0 @@
|
||||
#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";
|
||||
}
|
||||
}
|
||||
|
||||
static const char *detect_filesystem() {
|
||||
File root = SD.open("/");
|
||||
if (!root) {
|
||||
return "—";
|
||||
}
|
||||
root.close();
|
||||
|
||||
uint64_t total = SD.totalBytes();
|
||||
if (total == 0) {
|
||||
return "FAT";
|
||||
}
|
||||
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);
|
||||
// 触摸 SoftSPI CS 拉高,避免干扰(脚位不同,但防误选)
|
||||
pinMode(CYD_TOUCH_CS, OUTPUT);
|
||||
digitalWrite(CYD_TOUCH_CS, HIGH);
|
||||
|
||||
sd_spi.begin(SD_SCK, SD_MISO, SD_MOSI, SD_CS);
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -4,11 +4,8 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
/**
|
||||
* SoftSPI XPT2046 — Sunton esp32-2432S028R 官方通道 + 线性映射
|
||||
* X CMD=0xD1, Y CMD=0x91, scale /4096 → 240x320
|
||||
* SoftSPI 与官方 HW SPI 极性不同:不做 MIRROR_X(日志证明 mirror 会把 UI Y 压到顶部)
|
||||
* 显示:240x320 + LV_DISPLAY_ROTATION_270;触点直接喂物理坐标,由 LVGL 旋转
|
||||
* 板级:https://github.com/rzeldent/platformio-espressif32-sunton
|
||||
* SoftSPI XPT2046 → 物理 240x320 → 交给 LVGL ROTATION_270
|
||||
* RNT 标定区间 + rot2;红点用变换后 UI 坐标
|
||||
*/
|
||||
|
||||
int16_t g_dbg_phys_x = -1;
|
||||
@@ -59,15 +56,14 @@ void touch_init() {
|
||||
pinMode(CYD_TOUCH_IRQ, INPUT);
|
||||
digitalWrite(CYD_TOUCH_CS, HIGH);
|
||||
digitalWrite(CYD_TOUCH_CLK, LOW);
|
||||
Serial.println("Touch: Sunton 0xD1/0x91 linear, SoftSPI no-mirror");
|
||||
Serial.println("Touch: SoftSPI rot2 map 240x320");
|
||||
}
|
||||
|
||||
void touch_read_cb(lv_indev_t * /*indev*/, lv_indev_data_t *data) {
|
||||
(void)xpt_read(0xD1);
|
||||
uint16_t adc_x = xpt_read(0xD1);
|
||||
uint16_t adc_y = xpt_read(0x91);
|
||||
adc_x = (uint16_t)((adc_x + xpt_read(0xD1)) / 2);
|
||||
adc_y = (uint16_t)((adc_y + xpt_read(0x91)) / 2);
|
||||
uint16_t adc_x = xpt_read(0x90);
|
||||
uint16_t adc_y = xpt_read(0xD0);
|
||||
adc_x = (uint16_t)((adc_x + xpt_read(0x90)) / 2);
|
||||
adc_y = (uint16_t)((adc_y + xpt_read(0xD0)) / 2);
|
||||
|
||||
const bool irq = (digitalRead(CYD_TOUCH_IRQ) == LOW);
|
||||
const bool in_range =
|
||||
@@ -79,40 +75,41 @@ void touch_read_cb(lv_indev_t * /*indev*/, lv_indev_data_t *data) {
|
||||
return;
|
||||
}
|
||||
|
||||
int16_t px = (int16_t)(((uint32_t)adc_x * SCREEN_WIDTH) / 4096);
|
||||
int16_t py = (int16_t)(((uint32_t)adc_y * SCREEN_HEIGHT) / 4096);
|
||||
// setRotation(2)
|
||||
const int16_t p_x = (int16_t)adc_x;
|
||||
const int16_t p_y = (int16_t)(4095 - adc_y);
|
||||
|
||||
if (px < 0) {
|
||||
px = 0;
|
||||
int16_t x = map(p_x, 200, 3700, 1, SCREEN_WIDTH);
|
||||
int16_t y = map(p_y, 240, 3800, 1, SCREEN_HEIGHT);
|
||||
|
||||
if (x < 0) {
|
||||
x = 0;
|
||||
}
|
||||
if (py < 0) {
|
||||
py = 0;
|
||||
if (y < 0) {
|
||||
y = 0;
|
||||
}
|
||||
if (px >= SCREEN_WIDTH) {
|
||||
px = SCREEN_WIDTH - 1;
|
||||
if (x >= SCREEN_WIDTH) {
|
||||
x = SCREEN_WIDTH - 1;
|
||||
}
|
||||
if (py >= SCREEN_HEIGHT) {
|
||||
py = SCREEN_HEIGHT - 1;
|
||||
if (y >= SCREEN_HEIGHT) {
|
||||
y = SCREEN_HEIGHT - 1;
|
||||
}
|
||||
|
||||
// LVGL 270 后预估 UI(红点);触点仍喂物理坐标
|
||||
const int16_t ui_x = py;
|
||||
const int16_t ui_y = (int16_t)((SCREEN_WIDTH - 1) - px);
|
||||
|
||||
g_dbg_phys_x = px;
|
||||
g_dbg_phys_y = py;
|
||||
g_dbg_logic_x = ui_x;
|
||||
g_dbg_logic_y = ui_y;
|
||||
// LVGL 270 → UI
|
||||
g_dbg_phys_x = x;
|
||||
g_dbg_phys_y = y;
|
||||
g_dbg_logic_x = y;
|
||||
g_dbg_logic_y = (int16_t)((SCREEN_WIDTH - 1) - x);
|
||||
g_dbg_touch_down = true;
|
||||
|
||||
static uint32_t last_log = 0;
|
||||
if (millis() - last_log > 100) {
|
||||
if (millis() - last_log > 80) {
|
||||
last_log = millis();
|
||||
Serial.printf("Touch adc(%u,%u) phys(%d,%d) UI~(%d,%d)\n", adc_x, adc_y, px,
|
||||
py, ui_x, ui_y);
|
||||
Serial.printf("Touch phys(%d,%d) UI(%d,%d)\n", x, y, g_dbg_logic_x,
|
||||
g_dbg_logic_y);
|
||||
}
|
||||
|
||||
data->point.x = px;
|
||||
data->point.y = py;
|
||||
data->point.x = x;
|
||||
data->point.y = y;
|
||||
data->state = LV_INDEV_STATE_PRESSED;
|
||||
}
|
||||
|
||||
230
src/ui.cpp
230
src/ui.cpp
@@ -1,164 +1,69 @@
|
||||
#include "ui.h"
|
||||
#include "audio_test.h"
|
||||
#include "pins.h"
|
||||
|
||||
#include <Arduino.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 *lbl_info;
|
||||
static lv_obj_t *lbl_hit;
|
||||
static lv_obj_t *touch_cursor;
|
||||
|
||||
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_target_clicked(lv_event_t *e) {
|
||||
lv_obj_t *btn = (lv_obj_t *)lv_event_get_target(e);
|
||||
const char *name = (const char *)lv_event_get_user_data(e);
|
||||
lv_obj_set_style_bg_color(btn, lv_color_hex(0x2E7D32), 0);
|
||||
if (lbl_hit && name) {
|
||||
char buf[48];
|
||||
snprintf(buf, sizeof(buf), "HIT: %s", name);
|
||||
lv_label_set_text(lbl_hit, buf);
|
||||
}
|
||||
Serial.printf("HIT %s\n", name ? name : "?");
|
||||
}
|
||||
|
||||
static void on_speaker_clicked(lv_event_t *e) {
|
||||
LV_UNUSED(e);
|
||||
audio_set_enabled(!audio_is_enabled());
|
||||
ui_refresh_audio_buttons();
|
||||
}
|
||||
static lv_obj_t *make_target(lv_obj_t *parent, const char *name, int x, int y,
|
||||
int w, int h) {
|
||||
lv_obj_t *btn = lv_button_create(parent);
|
||||
lv_obj_set_pos(btn, x, y);
|
||||
lv_obj_set_size(btn, w, h);
|
||||
lv_obj_set_style_radius(btn, 6, 0);
|
||||
lv_obj_set_style_bg_color(btn, lv_color_hex(0x1565C0), 0);
|
||||
lv_obj_add_flag(btn, LV_OBJ_FLAG_CLICKABLE);
|
||||
lv_obj_add_event_cb(btn, on_target_clicked, LV_EVENT_CLICKED, (void *)name);
|
||||
|
||||
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());
|
||||
}
|
||||
lv_obj_t *lbl = lv_label_create(btn);
|
||||
lv_label_set_text(lbl, name);
|
||||
lv_obj_set_style_text_font(lbl, &lv_font_montserrat_14, 0);
|
||||
lv_obj_center(lbl);
|
||||
return btn;
|
||||
}
|
||||
|
||||
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_color(scr, lv_color_hex(0x102027), 0);
|
||||
lv_obj_set_style_bg_opa(scr, LV_OPA_COVER, 0);
|
||||
lv_obj_clear_flag(scr, LV_OBJ_FLAG_SCROLLABLE);
|
||||
|
||||
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);
|
||||
// 四角 + 中心,专门验触控与绘制是否重合
|
||||
make_target(scr, "TL", 8, 8, 90, 56);
|
||||
make_target(scr, "TR", UI_HOR_RES - 98, 8, 90, 56);
|
||||
make_target(scr, "BL", 8, UI_VER_RES - 64, 90, 56);
|
||||
make_target(scr, "BR", UI_HOR_RES - 98, UI_VER_RES - 64, 90, 56);
|
||||
make_target(scr, "CENTER", UI_HOR_RES / 2 - 60, UI_VER_RES / 2 - 28, 120, 56);
|
||||
|
||||
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);
|
||||
lbl_info = lv_label_create(scr);
|
||||
lv_label_set_text(lbl_info, "Touch: --");
|
||||
lv_obj_set_style_text_color(lbl_info, lv_color_hex(0xFFB74D), 0);
|
||||
lv_obj_set_style_text_font(lbl_info, &lv_font_montserrat_16, 0);
|
||||
lv_obj_align(lbl_info, LV_ALIGN_TOP_MID, 0, 72);
|
||||
|
||||
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);
|
||||
lbl_hit = lv_label_create(scr);
|
||||
lv_label_set_text(lbl_hit, "Tap a button");
|
||||
lv_obj_set_style_text_color(lbl_hit, lv_color_hex(0xECEFF1), 0);
|
||||
lv_obj_set_style_text_font(lbl_hit, &lv_font_montserrat_16, 0);
|
||||
lv_obj_align(lbl_hit, LV_ALIGN_BOTTOM_MID, 0, -72);
|
||||
|
||||
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, 64);
|
||||
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, 150, 60);
|
||||
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, 150, 60);
|
||||
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);
|
||||
|
||||
// 触点光标:落在哪画在哪,验证与按钮重合
|
||||
touch_cursor = lv_obj_create(scr);
|
||||
lv_obj_set_size(touch_cursor, 16, 16);
|
||||
lv_obj_set_size(touch_cursor, 18, 18);
|
||||
lv_obj_set_style_radius(touch_cursor, LV_RADIUS_CIRCLE, 0);
|
||||
lv_obj_set_style_bg_color(touch_cursor, lv_color_hex(0xFF5252), 0);
|
||||
lv_obj_set_style_bg_opa(touch_cursor, LV_OPA_COVER, 0);
|
||||
@@ -167,56 +72,25 @@ void ui_create() {
|
||||
lv_obj_add_flag(touch_cursor, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_clear_flag(touch_cursor, LV_OBJ_FLAG_CLICKABLE);
|
||||
lv_obj_clear_flag(touch_cursor, LV_OBJ_FLAG_SCROLLABLE);
|
||||
|
||||
ui_refresh_audio_buttons();
|
||||
}
|
||||
|
||||
void ui_set_touch_debug(int16_t lx, int16_t ly, bool down) {
|
||||
if (!lbl_touch_dbg) {
|
||||
if (!lbl_info) {
|
||||
return;
|
||||
}
|
||||
if (!down) {
|
||||
lv_label_set_text(lbl_touch_dbg, "Touch: up");
|
||||
lv_label_set_text(lbl_info, "Touch: up");
|
||||
if (touch_cursor) {
|
||||
lv_obj_add_flag(touch_cursor, LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
return;
|
||||
}
|
||||
char buf[40];
|
||||
snprintf(buf, sizeof(buf), "Touch L:%d,%d", (int)lx, (int)ly);
|
||||
lv_label_set_text(lbl_touch_dbg, buf);
|
||||
snprintf(buf, sizeof(buf), "Touch %d,%d", (int)lx, (int)ly);
|
||||
lv_label_set_text(lbl_info, buf);
|
||||
if (touch_cursor) {
|
||||
lv_obj_clear_flag(touch_cursor, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_set_pos(touch_cursor, lx - 8, ly - 8);
|
||||
lv_obj_set_pos(touch_cursor, lx - 9, ly - 9);
|
||||
lv_obj_move_foreground(touch_cursor);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user