From eb21ec95eb748c096bb25a82418b44767ee5eb2f Mon Sep 17 00:00:00 2001 From: "DESKTOP-PUHFO60\\dsx2016" Date: Sat, 17 Oct 2026 14:49:17 +0800 Subject: [PATCH] Add CYD LVGL climate dashboard with DHT11 on GPIO22. Include always-on UI, README, and a rule to commit and update docs after each change. Co-authored-by: Cursor --- .cursor/rules/git-and-docs.mdc | 14 + .gitignore | 8 + README.md | 68 +++++ include/lv_conf.h | 86 ++++++ platformio.ini | 67 +++++ scripts/exclude_lvgl_asm.py | 13 + src/main.cpp | 474 +++++++++++++++++++++++++++++++++ 7 files changed, 730 insertions(+) create mode 100644 .cursor/rules/git-and-docs.mdc create mode 100644 .gitignore create mode 100644 README.md create mode 100644 include/lv_conf.h create mode 100644 platformio.ini create mode 100644 scripts/exclude_lvgl_asm.py create mode 100644 src/main.cpp diff --git a/.cursor/rules/git-and-docs.mdc b/.cursor/rules/git-and-docs.mdc new file mode 100644 index 0000000..8cd4cb7 --- /dev/null +++ b/.cursor/rules/git-and-docs.mdc @@ -0,0 +1,14 @@ +--- +description: 每次改动必须提交 Git 并更新文档 +alwaysApply: true +--- + +# Git 与文档 + +对本项目做任何功能、行为或配置改动后,在结束本次任务前必须: + +1. **更新文档**:至少更新 `README.md` 的「功能 / 硬件 / 变更记录」中受影响部分;行为变更写进变更记录表。 +2. **提交 Git**:`git add` 相关文件后 `git commit`(按仓库既有 message 风格;中英文均可,说明 why)。 +3. **不要**在未提交的情况下声称任务已完成。 + +例外:用户明确说「先别提交」或「不要改文档」时,以用户当次指令为准。 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..39ce04d --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch +*.pyc +__pycache__/ +.DS_Store diff --git a/README.md b/README.md new file mode 100644 index 0000000..86e1dc3 --- /dev/null +++ b/README.md @@ -0,0 +1,68 @@ +# CYD 温湿度仪表盘 + +ESP32 Cheap Yellow Display(ESP32-2432S028R)+ DHT11 + LVGL 温湿度仪表盘。 + +## 硬件 + +| 项目 | 说明 | +|------|------| +| 板子 | CYD ESP32-2432S028R(2.8" ILI9341) | +| 传感器 | DHT11 | +| DATA | **GPIO22**(CN1 / P3) | +| 供电 | USB 5V;电池需外接充电管理(板载 JST 多为供电口) | + +接线:`VCC→3.3V`,`GND→GND`,`DATA→GPIO22`。 + +## 功能 + +- 横屏温湿度圆弧仪表 + 数字读数 +- EMA 平滑、min/max、舒适度、体感温度 / 露点 +- 温湿度趋势折线 +- 点按屏幕切换背光亮度(4 档) +- 偶发 DHT 读失败容错(连续失败才报错) +- 常亮运行(无自动熄屏 / 休眠) + +## 构建与烧录 + +依赖 [PlatformIO](https://platformio.org/)。 + +```bash +pio run -e cyd_28r -t upload +pio device monitor -b 115200 +``` + +### 环境选择 + +| 环境 | 适用 | +|------|------| +| `cyd_28r` | 标准 ILI9341 CYD(默认) | +| `cyd_28r_inv` | 颜色反相 / 发白 | +| `cyd_28_st7789` | USB-C 双口 / ST7789 变体 | + +## 项目结构 + +``` +platformio.ini # 板级与库依赖 +include/lv_conf.h # LVGL 配置 +src/main.cpp # 仪表盘主程序 +scripts/exclude_lvgl_asm.py # 排除 ESP32 不兼容的 LVGL ASM +.cursor/rules/ # Cursor 协作规则 +README.md # 本文档 +``` + +## 变更约定 + +每次功能或行为改动须: + +1. 更新本 README(及必要时的注释) +2. `git add` + `git commit` 提交 + +详见 `.cursor/rules/git-and-docs.mdc`。 + +## 变更记录 + +| 日期 | 说明 | +|------|------| +| 2026-10-17 | 初版:LVGL 仪表盘 + DHT11(GPIO22),平滑/舒适度/趋势/背光 | +| 2026-10-17 | 曾加入 ECO 熄屏休眠,后改回常亮正常版 | +| 2026-10-17 | 初始化 Git,补充 README 与提交约定 | diff --git a/include/lv_conf.h b/include/lv_conf.h new file mode 100644 index 0000000..6c23cfa --- /dev/null +++ b/include/lv_conf.h @@ -0,0 +1,86 @@ +#ifndef LV_CONF_H +#define LV_CONF_H + +#include + +#define LV_COLOR_DEPTH 16 + +#define LV_MEM_SIZE (48 * 1024U) +#define LV_DEF_REFR_PERIOD 16 +#define LV_DPI_DEF 130 + +/* ESP32 无 Helium,关掉以免 .S 汇编被错误预处理 */ +#define LV_USE_DRAW_SW_ASM LV_DRAW_SW_ASM_NONE + +#define LV_USE_OS LV_OS_NONE +#define LV_USE_STDLIB_MALLOC LV_STDLIB_BUILTIN +#define LV_USE_STDLIB_STRING LV_STDLIB_BUILTIN +#define LV_USE_STDLIB_SPRINTF LV_STDLIB_BUILTIN + +#define LV_USE_LOG 0 +#define LV_USE_ASSERT_NULL 1 +#define LV_USE_ASSERT_MALLOC 1 + +#define LV_FONT_MONTSERRAT_12 1 +#define LV_FONT_MONTSERRAT_14 1 +#define LV_FONT_MONTSERRAT_16 1 +#define LV_FONT_MONTSERRAT_20 1 +#define LV_FONT_MONTSERRAT_24 1 +#define LV_FONT_MONTSERRAT_28 1 +#define LV_FONT_MONTSERRAT_32 1 +#define LV_FONT_MONTSERRAT_40 1 +#define LV_FONT_DEFAULT &lv_font_montserrat_14 + +#define LV_USE_FLOAT 1 +#define LV_USE_MATRIX 0 + +#define LV_USE_LABEL 1 +#define LV_LABEL_TEXT_SELECTION 0 +#define LV_LABEL_LONG_TXT_HINT 1 + +#define LV_USE_ARC 1 +#define LV_USE_BAR 1 +#define LV_USE_BTN 1 +#define LV_USE_BUTTON 1 +#define LV_USE_IMG 1 +#define LV_USE_IMAGE 1 +#define LV_USE_LINE 1 +#define LV_USE_SLIDER 1 +#define LV_USE_SWITCH 1 +#define LV_USE_TEXTAREA 0 +#define LV_USE_CHECKBOX 0 +#define LV_USE_DROPDOWN 0 +#define LV_USE_ROLLER 0 +#define LV_USE_TABLE 0 +#define LV_USE_CALENDAR 0 +#define LV_USE_CHART 1 +#define LV_USE_KEYBOARD 0 +#define LV_USE_LIST 0 +#define LV_USE_MENU 0 +#define LV_USE_MSGBOX 0 +#define LV_USE_SPAN 0 +#define LV_USE_SPINBOX 0 +#define LV_USE_SPINNER 1 +#define LV_USE_TABVIEW 0 +#define LV_USE_TILEVIEW 0 +#define LV_USE_WIN 0 +#define LV_USE_LED 1 +#define LV_USE_SCALE 1 + +#define LV_USE_THEME_DEFAULT 1 +#define LV_THEME_DEFAULT_DARK 1 +#define LV_THEME_DEFAULT_GROW 0 +#define LV_THEME_DEFAULT_TRANSITION_TIME 80 + +#define LV_USE_FLEX 1 +#define LV_USE_GRID 1 + +#define LV_USE_TFT_ESPI 1 + +#define LV_USE_OBSERVER 0 +#define LV_USE_SYSMON 0 +#define LV_USE_PROFILER 0 +#define LV_USE_DEMO_WIDGETS 0 +#define LV_USE_DEMO_BENCHMARK 0 + +#endif /* LV_CONF_H */ diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..1a9833a --- /dev/null +++ b/platformio.ini @@ -0,0 +1,67 @@ +; CYD ESP32-2432S028R — LVGL 温湿度仪表盘 +; DHT11 DATA -> GPIO22 + +[platformio] +default_envs = cyd_28r + +[env] +platform = espressif32@6.9.0 +board = esp32dev +framework = arduino +monitor_speed = 115200 +upload_speed = 921600 +board_build.partitions = min_spiffs.csv +lib_deps = + lvgl/lvgl@9.2.2 + bodmer/TFT_eSPI@2.5.43 + https://github.com/PaulStoffregen/XPT2046_Touchscreen.git + beegee-tokyo/DHT sensor library for ESPx@^1.19 +extra_scripts = pre:scripts/exclude_lvgl_asm.py +build_flags = + -D CORE_DEBUG_LEVEL=0 + -D USER_SETUP_LOADED=1 + -D USE_HSPI_PORT=1 + -D ILI9341_2_DRIVER=1 + -D TFT_WIDTH=240 + -D TFT_HEIGHT=320 + -D TFT_MISO=12 + -D TFT_MOSI=13 + -D TFT_SCLK=14 + -D TFT_CS=15 + -D TFT_DC=2 + -D TFT_RST=-1 + -D TFT_BL=21 + -D TFT_BACKLIGHT_ON=HIGH + -D SPI_FREQUENCY=40000000 + -D SPI_READ_FREQUENCY=20000000 + -D LOAD_GLCD=1 + -D LOAD_FONT2=0 + -D LOAD_FONT4=0 + -D LOAD_FONT6=0 + -D LOAD_FONT7=0 + -D LOAD_FONT8=0 + -D LOAD_GFXFF=0 + -D SMOOTH_FONT=0 + -D TOUCH_CS=-1 + -D LV_CONF_INCLUDE_SIMPLE=1 + -I include + +; 标准 CYD 2.8" 电阻屏 (ILI9341) +[env:cyd_28r] +build_flags = + ${env.build_flags} + +; 颜色反相版本(若画面色偏/发白可改用此环境) +[env:cyd_28r_inv] +build_flags = + ${env.build_flags} + -D TFT_INVERSION_ON=1 + +; USB-C 双口 / 7789 变体 +[env:cyd_28_st7789] +build_flags = + ${env.build_flags} + -U ILI9341_2_DRIVER + -D ST7789_DRIVER=1 + -D TFT_RGB_ORDER=TFT_BGR + -D TFT_INVERSION_ON=1 diff --git a/scripts/exclude_lvgl_asm.py b/scripts/exclude_lvgl_asm.py new file mode 100644 index 0000000..5200ba1 --- /dev/null +++ b/scripts/exclude_lvgl_asm.py @@ -0,0 +1,13 @@ +"""Exclude LVGL Helium/Neon .S files that break ESP32 (xtensa) builds.""" +import shutil +from pathlib import Path + +Import("env") # type: ignore # noqa: F821 + +LIBDEPS = Path(env["PROJECT_LIBDEPS_DIR"]) / env["PIOENV"] / "lvgl" # type: ignore # noqa: F821 + +for sub in ("draw/sw/blend/helium", "draw/sw/blend/neon"): + target = LIBDEPS / "src" / sub + if target.exists(): + shutil.rmtree(target, ignore_errors=True) + print(f"Removed LVGL ASM path: {target}") diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..1b7a466 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,474 @@ +/** + * CYD ESP32-2432S028R — LVGL 温湿度仪表盘 + * DHT11 DATA -> GPIO22 + */ + +#include +#include +#include +#include +#include +#include + +#define DHT_PIN 22 +#define TFT_BL_PIN 21 + +#define XPT2046_IRQ 36 +#define XPT2046_MOSI 32 +#define XPT2046_MISO 39 +#define XPT2046_CLK 25 +#define XPT2046_CS 33 + +#define SCREEN_WIDTH 320 +#define SCREEN_HEIGHT 240 +#define DHT_READ_MS 2500 +#define DHT_FAIL_TOLERANCE 3 +#define EMA_ALPHA 0.35f +#define HISTORY_LEN 48 +#define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT / 10 * (LV_COLOR_DEPTH / 8)) + +static const lv_color_t COL_BG = lv_color_hex(0x0A1018); +static const lv_color_t COL_PANEL = lv_color_hex(0x121C2A); +static const lv_color_t COL_EDGE = lv_color_hex(0x1E2C40); +static const lv_color_t COL_TEXT = lv_color_hex(0xF2F5FA); +static const lv_color_t COL_MUTED = lv_color_hex(0x7E90A8); +static const lv_color_t COL_TEMP_COOL = lv_color_hex(0x4DA3FF); +static const lv_color_t COL_TEMP_OK = lv_color_hex(0x3DBF8C); +static const lv_color_t COL_TEMP_WARM = lv_color_hex(0xF0A04B); +static const lv_color_t COL_TEMP_HOT = lv_color_hex(0xE85D4C); +static const lv_color_t COL_HUM = lv_color_hex(0x3DB8A8); +static const lv_color_t COL_OK = lv_color_hex(0x4CAF7A); +static const lv_color_t COL_WARN = lv_color_hex(0xE6B84D); +static const lv_color_t COL_BAD = lv_color_hex(0xE05A5A); +static const lv_color_t COL_ARC_BG = lv_color_hex(0x243247); + +SPIClass touchscreenSPI = SPIClass(VSPI); +XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ); +DHTesp dht; + +static uint32_t draw_buf[DRAW_BUF_SIZE / 4]; + +static lv_obj_t *arc_temp; +static lv_obj_t *arc_hum; +static lv_obj_t *lbl_temp_val; +static lv_obj_t *lbl_hum_val; +static lv_obj_t *lbl_temp_range; +static lv_obj_t *lbl_hum_range; +static lv_obj_t *lbl_comfort; +static lv_obj_t *lbl_feels; +static lv_obj_t *lbl_status; +static lv_obj_t *lbl_uptime; +static lv_obj_t *led_status; +static lv_obj_t *bar_comfort; +static lv_obj_t *chart; +static lv_chart_series_t *ser_temp; +static lv_chart_series_t *ser_hum; + +static float disp_temp = NAN; +static float disp_hum = NAN; +static float min_temp = NAN; +static float max_temp = NAN; +static float min_hum = NAN; +static float max_hum = NAN; +static uint32_t last_dht_ms = 0; +static uint32_t last_ok_ms = 0; +static uint8_t fail_streak = 0; +static bool sensor_ok = false; +static uint8_t bright_level = 2; +static const uint8_t BRIGHT_PWM[4] = {40, 90, 160, 255}; + +static void set_backlight(uint8_t level) { + bright_level = level & 0x03; + ledcWrite(0, BRIGHT_PWM[bright_level]); +} + +static float ema(float prev, float sample) { + if (isnan(prev)) return sample; + return prev + EMA_ALPHA * (sample - prev); +} + +static float dew_point_c(float t, float h) { + if (h <= 0.1f) h = 0.1f; + if (h > 100.0f) h = 100.0f; + const float a = 17.27f; + const float b = 237.7f; + float alpha = (a * t) / (b + t) + logf(h / 100.0f); + return (b * alpha) / (a - alpha); +} + +static float heat_index_c(float t, float h) { + if (t < 20.0f) return t - (50.0f - h) * 0.03f; + float tf = t * 1.8f + 32.0f; + float hi_f = -42.379f + 2.04901523f * tf + 10.14333127f * h + - 0.22475541f * tf * h - 0.00683783f * tf * tf + - 0.05481717f * h * h + 0.00122874f * tf * tf * h + + 0.00085282f * tf * h * h - 0.00000199f * tf * tf * h * h; + return (hi_f - 32.0f) / 1.8f; +} + +static lv_color_t temp_color(float t) { + if (isnan(t)) return COL_MUTED; + if (t < 18.0f) return COL_TEMP_COOL; + if (t < 26.0f) return COL_TEMP_OK; + if (t < 30.0f) return COL_TEMP_WARM; + return COL_TEMP_HOT; +} + +static const char *comfort_text(float t, float h) { + if (isnan(t) || isnan(h)) return "NO DATA"; + if (t < 16 || t > 30 || h < 30 || h > 75) return "UNCOMFORTABLE"; + if (t >= 20 && t <= 26 && h >= 40 && h <= 60) return "COMFORTABLE"; + return "ACCEPTABLE"; +} + +static lv_color_t comfort_color(float t, float h) { + if (isnan(t) || isnan(h)) return COL_BAD; + if (t < 16 || t > 30 || h < 30 || h > 75) return COL_BAD; + if (t >= 20 && t <= 26 && h >= 40 && h <= 60) return COL_OK; + return COL_WARN; +} + +static int comfort_score(float t, float h) { + if (isnan(t) || isnan(h)) return 0; + float t_score = 100.0f - fabsf(t - 23.0f) * 8.0f; + float h_score = 100.0f - fabsf(h - 50.0f) * 2.0f; + int score = (int)lroundf((t_score + h_score) * 0.5f); + return constrain(score, 0, 100); +} + +static void touchscreen_read(lv_indev_t *indev, lv_indev_data_t *data) { + LV_UNUSED(indev); + static bool was_pressed = false; + static uint32_t press_ms = 0; + + if (touchscreen.tirqTouched() && touchscreen.touched()) { + TS_Point p = touchscreen.getPoint(); + data->point.x = map(p.y, 240, 3800, 1, SCREEN_WIDTH); + data->point.y = map(p.x, 200, 3700, SCREEN_HEIGHT, 1); + data->state = LV_INDEV_STATE_PRESSED; + if (!was_pressed) { + was_pressed = true; + press_ms = millis(); + } + } else { + data->state = LV_INDEV_STATE_RELEASED; + if (was_pressed) { + was_pressed = false; + if (millis() - press_ms < 400) { + set_backlight((bright_level + 1) & 0x03); + } + } + } +} + +static void arc_anim_exec(void *obj, int32_t v) { + lv_arc_set_value((lv_obj_t *)obj, v); +} + +static void anim_arc(lv_obj_t *arc, int32_t value) { + int32_t cur = lv_arc_get_value(arc); + if (cur == value) return; + lv_anim_t a; + lv_anim_init(&a); + lv_anim_set_var(&a, arc); + lv_anim_set_values(&a, cur, value); + lv_anim_set_duration(&a, 450); + lv_anim_set_exec_cb(&a, arc_anim_exec); + lv_anim_set_path_cb(&a, lv_anim_path_ease_out); + lv_anim_start(&a); +} + +static lv_obj_t *make_panel(lv_obj_t *parent, int x, int y, int w, int h) { + lv_obj_t *p = lv_obj_create(parent); + lv_obj_set_pos(p, x, y); + lv_obj_set_size(p, w, h); + lv_obj_set_style_bg_color(p, COL_PANEL, 0); + lv_obj_set_style_bg_opa(p, LV_OPA_COVER, 0); + lv_obj_set_style_border_color(p, COL_EDGE, 0); + lv_obj_set_style_border_width(p, 1, 0); + lv_obj_set_style_radius(p, 14, 0); + lv_obj_set_style_pad_all(p, 6, 0); + lv_obj_clear_flag(p, LV_OBJ_FLAG_SCROLLABLE); + return p; +} + +static lv_obj_t *make_gauge(lv_obj_t *parent, lv_color_t accent, int max_val) { + lv_obj_t *arc = lv_arc_create(parent); + lv_obj_set_size(arc, 112, 112); + lv_obj_align(arc, LV_ALIGN_TOP_MID, 0, 14); + lv_arc_set_rotation(arc, 135); + lv_arc_set_bg_angles(arc, 0, 270); + lv_arc_set_range(arc, 0, max_val); + lv_arc_set_value(arc, 0); + lv_obj_remove_style(arc, NULL, LV_PART_KNOB); + lv_obj_clear_flag(arc, LV_OBJ_FLAG_CLICKABLE); + lv_obj_set_style_arc_color(arc, COL_ARC_BG, LV_PART_MAIN); + lv_obj_set_style_arc_width(arc, 12, LV_PART_MAIN); + lv_obj_set_style_arc_color(arc, accent, LV_PART_INDICATOR); + lv_obj_set_style_arc_width(arc, 12, LV_PART_INDICATOR); + lv_obj_set_style_arc_rounded(arc, true, LV_PART_MAIN); + lv_obj_set_style_arc_rounded(arc, true, LV_PART_INDICATOR); + return arc; +} + +static void create_ui(void) { + lv_obj_t *scr = lv_screen_active(); + lv_obj_set_style_bg_color(scr, COL_BG, 0); + lv_obj_set_style_bg_opa(scr, LV_OPA_COVER, 0); + lv_obj_clear_flag(scr, LV_OBJ_FLAG_SCROLLABLE); + + lv_obj_t *title = lv_label_create(scr); + lv_label_set_text(title, "ROOM CLIMATE"); + lv_obj_set_style_text_font(title, &lv_font_montserrat_16, 0); + lv_obj_set_style_text_color(title, COL_TEXT, 0); + lv_obj_align(title, LV_ALIGN_TOP_LEFT, 10, 6); + + lbl_uptime = lv_label_create(scr); + lv_label_set_text(lbl_uptime, "tap: brightness"); + lv_obj_set_style_text_font(lbl_uptime, &lv_font_montserrat_12, 0); + lv_obj_set_style_text_color(lbl_uptime, COL_MUTED, 0); + lv_obj_align(lbl_uptime, LV_ALIGN_TOP_LEFT, 10, 24); + + led_status = lv_led_create(scr); + lv_obj_set_size(led_status, 10, 10); + lv_obj_align(led_status, LV_ALIGN_TOP_RIGHT, -10, 10); + lv_led_set_color(led_status, COL_WARN); + lv_led_on(led_status); + + lbl_status = lv_label_create(scr); + lv_label_set_text(lbl_status, "INIT"); + lv_obj_set_style_text_font(lbl_status, &lv_font_montserrat_12, 0); + lv_obj_set_style_text_color(lbl_status, COL_MUTED, 0); + lv_obj_align(lbl_status, LV_ALIGN_TOP_RIGHT, -26, 8); + + lv_obj_t *p_temp = make_panel(scr, 6, 40, 152, 132); + lv_obj_t *t_cap = lv_label_create(p_temp); + lv_label_set_text(t_cap, "TEMP"); + lv_obj_set_style_text_font(t_cap, &lv_font_montserrat_12, 0); + lv_obj_set_style_text_color(t_cap, COL_MUTED, 0); + lv_obj_align(t_cap, LV_ALIGN_TOP_LEFT, 2, 0); + + arc_temp = make_gauge(p_temp, COL_TEMP_OK, 50); + lbl_temp_val = lv_label_create(p_temp); + lv_label_set_text(lbl_temp_val, "--.-"); + lv_obj_set_style_text_font(lbl_temp_val, &lv_font_montserrat_28, 0); + lv_obj_set_style_text_color(lbl_temp_val, COL_TEMP_OK, 0); + lv_obj_align(lbl_temp_val, LV_ALIGN_CENTER, 0, 6); + + lv_obj_t *t_unit = lv_label_create(p_temp); + lv_label_set_text(t_unit, "C"); + lv_obj_set_style_text_font(t_unit, &lv_font_montserrat_12, 0); + lv_obj_set_style_text_color(t_unit, COL_MUTED, 0); + lv_obj_align(t_unit, LV_ALIGN_CENTER, 36, 10); + + lbl_temp_range = lv_label_create(p_temp); + lv_label_set_text(lbl_temp_range, "min -- max --"); + lv_obj_set_style_text_font(lbl_temp_range, &lv_font_montserrat_12, 0); + lv_obj_set_style_text_color(lbl_temp_range, COL_MUTED, 0); + lv_obj_align(lbl_temp_range, LV_ALIGN_BOTTOM_MID, 0, 0); + + lv_obj_t *p_hum = make_panel(scr, 162, 40, 152, 132); + lv_obj_t *h_cap = lv_label_create(p_hum); + lv_label_set_text(h_cap, "HUM"); + lv_obj_set_style_text_font(h_cap, &lv_font_montserrat_12, 0); + lv_obj_set_style_text_color(h_cap, COL_MUTED, 0); + lv_obj_align(h_cap, LV_ALIGN_TOP_LEFT, 2, 0); + + arc_hum = make_gauge(p_hum, COL_HUM, 100); + lbl_hum_val = lv_label_create(p_hum); + lv_label_set_text(lbl_hum_val, "--.-"); + lv_obj_set_style_text_font(lbl_hum_val, &lv_font_montserrat_28, 0); + lv_obj_set_style_text_color(lbl_hum_val, COL_HUM, 0); + lv_obj_align(lbl_hum_val, LV_ALIGN_CENTER, 0, 6); + + lv_obj_t *h_unit = lv_label_create(p_hum); + lv_label_set_text(h_unit, "%"); + lv_obj_set_style_text_font(h_unit, &lv_font_montserrat_12, 0); + lv_obj_set_style_text_color(h_unit, COL_MUTED, 0); + lv_obj_align(h_unit, LV_ALIGN_CENTER, 36, 10); + + lbl_hum_range = lv_label_create(p_hum); + lv_label_set_text(lbl_hum_range, "min -- max --"); + lv_obj_set_style_text_font(lbl_hum_range, &lv_font_montserrat_12, 0); + lv_obj_set_style_text_color(lbl_hum_range, COL_MUTED, 0); + lv_obj_align(lbl_hum_range, LV_ALIGN_BOTTOM_MID, 0, 0); + + lv_obj_t *p_bot = make_panel(scr, 6, 176, 308, 58); + lbl_comfort = lv_label_create(p_bot); + lv_label_set_text(lbl_comfort, "WAITING"); + lv_obj_set_style_text_font(lbl_comfort, &lv_font_montserrat_14, 0); + lv_obj_set_style_text_color(lbl_comfort, COL_WARN, 0); + lv_obj_align(lbl_comfort, LV_ALIGN_TOP_LEFT, 4, 0); + + lbl_feels = lv_label_create(p_bot); + lv_label_set_text(lbl_feels, "FEELS -- DEW --"); + lv_obj_set_style_text_font(lbl_feels, &lv_font_montserrat_12, 0); + lv_obj_set_style_text_color(lbl_feels, COL_MUTED, 0); + lv_obj_align(lbl_feels, LV_ALIGN_TOP_RIGHT, -4, 2); + + bar_comfort = lv_bar_create(p_bot); + lv_obj_set_size(bar_comfort, 120, 10); + lv_obj_align(bar_comfort, LV_ALIGN_TOP_LEFT, 4, 20); + lv_bar_set_range(bar_comfort, 0, 100); + lv_bar_set_value(bar_comfort, 0, LV_ANIM_OFF); + lv_obj_set_style_bg_color(bar_comfort, COL_ARC_BG, LV_PART_MAIN); + lv_obj_set_style_bg_opa(bar_comfort, LV_OPA_COVER, LV_PART_MAIN); + lv_obj_set_style_radius(bar_comfort, 5, LV_PART_MAIN); + lv_obj_set_style_bg_color(bar_comfort, COL_WARN, LV_PART_INDICATOR); + lv_obj_set_style_radius(bar_comfort, 5, LV_PART_INDICATOR); + + chart = lv_chart_create(p_bot); + lv_obj_set_size(chart, 168, 28); + lv_obj_align(chart, LV_ALIGN_BOTTOM_RIGHT, -2, -2); + lv_chart_set_type(chart, LV_CHART_TYPE_LINE); + lv_chart_set_point_count(chart, HISTORY_LEN); + lv_chart_set_range(chart, LV_CHART_AXIS_PRIMARY_Y, 10, 40); + lv_chart_set_range(chart, LV_CHART_AXIS_SECONDARY_Y, 0, 100); + lv_obj_set_style_bg_opa(chart, LV_OPA_TRANSP, 0); + lv_obj_set_style_border_width(chart, 0, 0); + lv_obj_set_style_pad_all(chart, 0, 0); + lv_chart_set_div_line_count(chart, 0, 0); + ser_temp = lv_chart_add_series(chart, COL_TEMP_WARM, LV_CHART_AXIS_PRIMARY_Y); + ser_hum = lv_chart_add_series(chart, COL_HUM, LV_CHART_AXIS_SECONDARY_Y); +} + +static void refresh_meta(void) { + char buf[40]; + uint32_t up = millis() / 1000; + lv_snprintf(buf, sizeof(buf), "up %02lu:%02lu bl %u/3", + (unsigned long)(up / 60), (unsigned long)(up % 60), (unsigned)bright_level); + lv_label_set_text(lbl_uptime, buf); + + if (sensor_ok) { + uint32_t age = (millis() - last_ok_ms) / 1000; + lv_snprintf(buf, sizeof(buf), "LIVE %lus", (unsigned long)age); + lv_label_set_text(lbl_status, buf); + lv_led_set_color(led_status, COL_OK); + } +} + +static void update_ui(bool ok) { + char buf[40]; + + if (ok && !isnan(disp_temp) && !isnan(disp_hum)) { + lv_color_t tc = temp_color(disp_temp); + + lv_snprintf(buf, sizeof(buf), "%.1f", disp_temp); + lv_label_set_text(lbl_temp_val, buf); + lv_obj_set_style_text_color(lbl_temp_val, tc, 0); + lv_obj_set_style_arc_color(arc_temp, tc, LV_PART_INDICATOR); + anim_arc(arc_temp, (int)lroundf(constrain(disp_temp, 0.0f, 50.0f))); + + lv_snprintf(buf, sizeof(buf), "%.0f", disp_hum); + lv_label_set_text(lbl_hum_val, buf); + anim_arc(arc_hum, (int)lroundf(constrain(disp_hum, 0.0f, 100.0f))); + + lv_snprintf(buf, sizeof(buf), "min %.0f max %.0f", min_temp, max_temp); + lv_label_set_text(lbl_temp_range, buf); + lv_snprintf(buf, sizeof(buf), "min %.0f max %.0f", min_hum, max_hum); + lv_label_set_text(lbl_hum_range, buf); + + float feels = heat_index_c(disp_temp, disp_hum); + float dew = dew_point_c(disp_temp, disp_hum); + lv_snprintf(buf, sizeof(buf), "FEELS %.1f DEW %.1f", feels, dew); + lv_label_set_text(lbl_feels, buf); + + lv_color_t cc = comfort_color(disp_temp, disp_hum); + lv_label_set_text(lbl_comfort, comfort_text(disp_temp, disp_hum)); + lv_obj_set_style_text_color(lbl_comfort, cc, 0); + lv_bar_set_value(bar_comfort, comfort_score(disp_temp, disp_hum), LV_ANIM_ON); + lv_obj_set_style_bg_color(bar_comfort, cc, LV_PART_INDICATOR); + + lv_chart_set_next_value(chart, ser_temp, (int32_t)lroundf(disp_temp)); + lv_chart_set_next_value(chart, ser_hum, (int32_t)lroundf(disp_hum)); + refresh_meta(); + } else { + lv_label_set_text(lbl_comfort, "SENSOR ERROR"); + lv_obj_set_style_text_color(lbl_comfort, COL_BAD, 0); + lv_label_set_text(lbl_status, "ERR"); + lv_led_set_color(led_status, COL_BAD); + } +} + +static void read_dht(void) { + TempAndHumidity data = dht.getTempAndHumidity(); + float t = data.temperature; + float h = data.humidity; + bool ok = !(isnan(h) || isnan(t) || dht.getStatus() != DHTesp::ERROR_NONE); + + if (!ok) { + fail_streak++; + Serial.printf("DHT fail(%u): %s\n", fail_streak, dht.getStatusString()); + if (fail_streak >= DHT_FAIL_TOLERANCE) { + sensor_ok = false; + update_ui(false); + } + return; + } + + fail_streak = 0; + sensor_ok = true; + last_ok_ms = millis(); + + disp_temp = ema(disp_temp, t); + disp_hum = ema(disp_hum, h); + + if (isnan(min_temp) || disp_temp < min_temp) min_temp = disp_temp; + if (isnan(max_temp) || disp_temp > max_temp) max_temp = disp_temp; + if (isnan(min_hum) || disp_hum < min_hum) min_hum = disp_hum; + if (isnan(max_hum) || disp_hum > max_hum) max_hum = disp_hum; + + Serial.printf("T=%.1f(%.1f) C H=%.0f(%.0f) %%\n", disp_temp, t, disp_hum, h); + update_ui(true); +} + +void setup() { + Serial.begin(115200); + delay(200); + + ledcSetup(0, 5000, 8); + ledcAttachPin(TFT_BL_PIN, 0); + set_backlight(2); + + Serial.println("CYD LVGL Climate Dashboard"); + Serial.printf("LVGL %d.%d.%d DHT=GPIO%d\n", + lv_version_major(), lv_version_minor(), lv_version_patch(), DHT_PIN); + + pinMode(DHT_PIN, INPUT_PULLUP); + dht.setup(DHT_PIN, DHTesp::DHT11); + + lv_init(); + + touchscreenSPI.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS); + touchscreen.begin(touchscreenSPI); + touchscreen.setRotation(2); + + lv_display_t *disp = lv_tft_espi_create(240, 320, draw_buf, sizeof(draw_buf)); + lv_display_set_rotation(disp, LV_DISPLAY_ROTATION_270); + + lv_indev_t *indev = lv_indev_create(); + lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER); + lv_indev_set_read_cb(indev, touchscreen_read); + + create_ui(); + delay(1500); + last_dht_ms = 0; +} + +void loop() { + uint32_t now = millis(); + if (now - last_dht_ms >= DHT_READ_MS) { + last_dht_ms = now; + read_dht(); + } + + static uint32_t last_meta_ms = 0; + if (now - last_meta_ms >= 1000) { + last_meta_ms = now; + refresh_meta(); + } + + lv_timer_handler(); + lv_tick_inc(5); + delay(5); +}