Add CYD GY30 light dashboard (BH1750 on SDA22/SCL27).

Port the temperature/sgp30test LVGL layout for lux/level gauges with scene bar and trend chart.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-10-20 11:20:13 +08:00
commit fab6af3ed3
16 changed files with 1044 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
---
description: 每次改动必须提交 Git 并更新文档
alwaysApply: true
---
# Git 与文档
对本项目做任何功能、行为或配置改动后,在结束本次任务前必须:
1. **更新文档**:至少更新 `README.md` 的「功能 / 硬件 / 变更记录」中受影响部分;行为变更写进变更记录表。
2. **提交 Git**:`git add` 相关文件后 `git commit`(按仓库既有 message 风格;中英文均可,说明 why)。
3. **不要**在未提交的情况下声称任务已完成。
例外:用户明确说「先别提交」或「不要改文档」时,以用户当次指令为准。

8
.gitignore vendored Normal file
View File

@@ -0,0 +1,8 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
*.pyc
__pycache__/
.DS_Store

66
README.md Normal file
View File

@@ -0,0 +1,66 @@
# CYD GY30 光照仪表盘
ESP32 Cheap Yellow Display(ESP32-2432S028R)+ GY30(BH1750)+ LVGL 光照仪表盘。
布局与交互参考同目录下的 [temperature](../temperature) / [sgp30test](../sgp30test)。
## 硬件
| 项目 | 说明 |
|------|------|
| 板子 | CYD ESP32-2432S028R(2.8" ILI9341) |
| 传感器 | GY30 / BH1750(光照 lux) |
| SDA | **GPIO22** |
| SCL | **GPIO27** |
| 供电 | USB 5V;传感器接 **3.3V** |
接线:`VCC→3.3V`,`GND→GND`,`SDA→GPIO22`,`SCL→GPIO27`。
## 功能
- 横屏网格布局:双圆弧仪表(LUX / 感知 LEVEL%)+ 底部左场景分级 / 右趋势图
- EMA 平滑、min/max、场景(DARK…DIRECT)、近似 EV、感知亮度条
- 圆弧与亮度条缓动动画;趋势折线 SHIFT 更新
- 短按切换背光(4 档);长按 ≥0.7s 重置 min/max
- 连续读失败容错;常亮运行
## 构建与烧录
依赖 [PlatformIO](https://platformio.org/)。
```bash
pio run -e cyd_28r -t upload
pio device monitor -b 115200
```
### 预编译固件
```powershell
cd firmware\cyd_28r
.\flash.ps1
```
源码更新后重新打包:`.\firmware\export_from_build.ps1`。
| 环境 | 适用 |
|------|------|
| `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
firmware/ # 预编译固件
README.md
```
## 变更记录
| 日期 | 说明 |
|------|------|
| 2026-10-20 | 初版:参考 temperature/sgp30test,GY30(BH1750) SDA=GPIO22 / SCL=GPIO27,LUX/LEVEL 双表盘、场景条、趋势图 |

27
firmware/README.md Normal file
View File

@@ -0,0 +1,27 @@
# 预编译固件(可随时刷写)
当前归档目录:[`cyd_28r/`](cyd_28r/)
对应板子:**CYD ESP32-2432S028R**(ILI9341),GY30 → SDA=GPIO22 / SCL=GPIO27。
版本信息见 [`cyd_28r/VERSION.txt`](cyd_28r/VERSION.txt)。
## 直接刷写(无需重新编译)
```powershell
cd firmware\cyd_28r
.\flash.ps1
# 或指定端口
.\flash.ps1 -Port COM3
```
也可运行 `flash.bat`。
依赖其一:已安装 PlatformIO(`pio`),或 `pip install esptool`。
## 从源码重新打包
在仓库根目录:
```powershell
.\firmware\export_from_build.ps1
```

View File

@@ -0,0 +1,16 @@
name: CYD GY30 light dashboard (cyd_28r)
git: nogit
date: 2026-10-20
board: ESP32-2432S028R (ILI9341)
gy30: SDA=GPIO22 SCL=GPIO27
flash_size: 4MB
offsets:
0x1000 bootloader.bin
0x8000 partitions.bin
0xe000 boot_app0.bin
0x10000 firmware.bin
sha256:
bootloader.bin 3D234A7471F67B013686DABD4DEE7C1FA915C9928463616A94BC9297ACF1ABF8
boot_app0.bin F94C5D786A7A8FAB06AC5D10E33BF37711A6697636DC037559EA19CC410A17F0
firmware.bin A1E436736E886DFAB2C3186120E4A16C24E1446C07372AAE6DD8EF1976B2D9E5
partitions.bin 0A8B5720E7B77FF11F1462458C3A509DEE79224E5279898F26D6A2E3AE0517B7

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,5 @@
@echo off
setlocal
cd /d "%~dp0"
powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0flash.ps1" %*
exit /b %ERRORLEVEL%

View File

@@ -0,0 +1,85 @@
# Flash archived CYD GY30 firmware (no rebuild needed)
# Usage:
# .\flash.ps1
# .\flash.ps1 -Port COM5
# .\flash.ps1 -Port COM5 -Baud 460800
param(
[string]$Port = "",
[int]$Baud = 921600
)
$ErrorActionPreference = "Stop"
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $here
function Find-Esptool {
$pio = Get-Command pio -ErrorAction SilentlyContinue
if ($pio) {
$pkg = Join-Path $env:USERPROFILE ".platformio\packages\tool-esptoolpy\esptool.py"
if (Test-Path $pkg) { return @{ Kind = "py"; Path = $pkg } }
}
$py = Get-Command python -ErrorAction SilentlyContinue
if ($py) {
try {
& python -c "import esptool" 2>$null
if ($LASTEXITCODE -eq 0) { return @{ Kind = "mod"; Path = "esptool" } }
} catch {}
}
$esp = Get-Command esptool.py -ErrorAction SilentlyContinue
if ($esp) { return @{ Kind = "py"; Path = $esp.Source } }
return $null
}
if (-not $Port) {
$ports = @()
try {
Add-Type -AssemblyName System.IO.Ports -ErrorAction SilentlyContinue | Out-Null
$ports = [System.IO.Ports.SerialPort]::GetPortNames() | Sort-Object
} catch {}
if (-not $ports -or $ports.Count -eq 0) {
$ports = Get-CimInstance Win32_SerialPort -ErrorAction SilentlyContinue |
Select-Object -ExpandProperty DeviceID | Sort-Object
}
if (-not $ports -or $ports.Count -eq 0) {
Write-Error "No serial ports found. Plug in the CYD USB cable, then retry with -Port COMx"
}
if ($ports.Count -eq 1) {
$Port = $ports[0]
} else {
Write-Host "Available ports: $($ports -join ', ')"
$Port = $ports[-1]
Write-Host "Using last port: $Port (override with -Port)"
}
}
$tool = Find-Esptool
if (-not $tool) {
Write-Error "esptool not found. Install PlatformIO (`pio`) or: pip install esptool"
}
$argsCommon = @(
"--chip", "esp32",
"--port", $Port,
"--baud", "$Baud",
"--before", "default_reset",
"--after", "hard_reset",
"write_flash", "-z",
"--flash_mode", "dio",
"--flash_freq", "40m",
"--flash_size", "4MB",
"0x1000", "bootloader.bin",
"0x8000", "partitions.bin",
"0xe000", "boot_app0.bin",
"0x10000", "firmware.bin"
)
Write-Host "Flashing $here -> $Port @ $Baud"
if ($tool.Kind -eq "mod") {
& python -m esptool @argsCommon
} else {
& python $tool.Path @argsCommon
}
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
Write-Host "Done. Open serial monitor at 115200 if needed."

Binary file not shown.

View File

@@ -0,0 +1,57 @@
# Rebuild (optional) and refresh firmware/cyd_28r from PlatformIO build output.
# Usage:
# .\firmware\export_from_build.ps1
# .\firmware\export_from_build.ps1 -SkipBuild
param([switch]$SkipBuild)
$ErrorActionPreference = "Stop"
$root = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)
Set-Location $root
if (-not $SkipBuild) {
pio run -e cyd_28r
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
}
$src = Join-Path $root ".pio\build\cyd_28r"
$dst = Join-Path $root "firmware\cyd_28r"
New-Item -ItemType Directory -Force -Path $dst | Out-Null
Copy-Item (Join-Path $src "bootloader.bin") $dst -Force
Copy-Item (Join-Path $src "partitions.bin") $dst -Force
Copy-Item (Join-Path $src "firmware.bin") $dst -Force
$bootApp = Join-Path $env:USERPROFILE ".platformio\packages\framework-arduinoespressif32\tools\partitions\boot_app0.bin"
if (-not (Test-Path $bootApp)) {
Write-Error "boot_app0.bin not found under PlatformIO packages"
}
Copy-Item $bootApp (Join-Path $dst "boot_app0.bin") -Force
$git = "nogit"
try {
$git = (git -C $root rev-parse --short HEAD 2>$null).Trim()
if (-not $git) { $git = "nogit" }
} catch { $git = "nogit" }
$date = Get-Date -Format "yyyy-MM-dd"
$hashes = Get-FileHash (Join-Path $dst "*.bin") -Algorithm SHA256
$lines = @(
"name: CYD GY30 light dashboard (cyd_28r)",
"git: $git",
"date: $date",
"board: ESP32-2432S028R (ILI9341)",
"gy30: SDA=GPIO22 SCL=GPIO27",
"flash_size: 4MB",
"offsets:",
" 0x1000 bootloader.bin",
" 0x8000 partitions.bin",
" 0xe000 boot_app0.bin",
" 0x10000 firmware.bin",
"sha256:"
)
foreach ($h in $hashes) {
$lines += (" {0,-15} {1}" -f (Split-Path $h.Path -Leaf), $h.Hash)
}
$lines | Set-Content -Encoding utf8 (Join-Path $dst "VERSION.txt")
Write-Host "Exported -> $dst (git $git)"

86
include/lv_conf.h Normal file
View File

@@ -0,0 +1,86 @@
#ifndef LV_CONF_H
#define LV_CONF_H
#include <stdint.h>
#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 */

67
platformio.ini Normal file
View File

@@ -0,0 +1,67 @@
; CYD ESP32-2432S028R — LVGL GY30(BH1750) 光照仪表盘
; GY30 SDA -> GPIO22, SCL -> GPIO27
[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
claws/BH1750@^1.3.0
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

View File

@@ -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}")

600
src/main.cpp Normal file
View File

@@ -0,0 +1,600 @@
/**
* CYD ESP32-2432S028R — LVGL GY30(BH1750) 光照仪表盘
* GY30 SDA -> GPIO22, SCL -> GPIO27
*/
#include <Arduino.h>
#include <Wire.h>
#include <lvgl.h>
#include <TFT_eSPI.h>
#include <XPT2046_Touchscreen.h>
#include <BH1750.h>
#include <math.h>
#define I2C_SDA 22
#define I2C_SCL 27
#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 GY_READ_MS 500
#define GY_FAIL_TOLERANCE 3
#define EMA_ALPHA 0.35f
#define HISTORY_LEN 48
#define LUX_ARC_MAX 1000
#define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT / 10 * (LV_COLOR_DEPTH / 8))
/* Layout grid — every region has exclusive space, no overlaps */
#define UI_M 6
#define UI_G 6
#define UI_HDR_H 22
#define UI_GAUGE_H 124
#define UI_GAUGE_W 151
#define UI_ARC 90
#define UI_BOT_Y (UI_HDR_H + UI_G + UI_GAUGE_H + UI_G) /* 158 */
#define UI_BOT_H (SCREEN_HEIGHT - UI_BOT_Y - UI_M) /* 76 */
#define UI_BOT_W (SCREEN_WIDTH - 2 * UI_M) /* 308 */
#define UI_INFO_W 124
#define UI_CHART_W (UI_BOT_W - 12 - UI_INFO_W - UI_G) /* ~160 */
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_LUX = lv_color_hex(0xF0C040);
static const lv_color_t COL_LEVEL = lv_color_hex(0x4DA3FF);
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);
static const lv_color_t COL_CHART_BG = lv_color_hex(0x0E1622);
SPIClass touchscreenSPI = SPIClass(VSPI);
XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ);
BH1750 lightMeter;
static uint32_t draw_buf[DRAW_BUF_SIZE / 4];
static lv_obj_t *arc_lux;
static lv_obj_t *arc_level;
static lv_obj_t *lbl_lux_val;
static lv_obj_t *lbl_level_val;
static lv_obj_t *lbl_lux_range;
static lv_obj_t *lbl_level_range;
static lv_obj_t *lbl_scene;
static lv_obj_t *lbl_ev;
static lv_obj_t *lbl_hint_line;
static lv_obj_t *lbl_status;
static lv_obj_t *lbl_uptime;
static lv_obj_t *lbl_hint;
static lv_obj_t *led_status;
static lv_obj_t *bar_level;
static lv_obj_t *chart;
static lv_chart_series_t *ser_lux;
static lv_chart_series_t *ser_level;
static float disp_lux = NAN;
static float disp_level = NAN;
static float min_lux = NAN;
static float max_lux = NAN;
static float min_level = NAN;
static float max_level = NAN;
static uint32_t last_gy_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 uint32_t hint_until_ms = 0;
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);
}
/* Perceptual 0..100 from lux (log scale, ~0.1..10000 lx) */
static float lux_to_level(float lux) {
if (lux < 0.1f) lux = 0.1f;
if (lux > 100000.0f) lux = 100000.0f;
float lv = (log10f(lux) + 1.0f) / 5.0f * 100.0f; /* 0.1→0, 10000→100 */
return constrain(lv, 0.0f, 100.0f);
}
/* Approximate photographic EV at ISO100 */
static float lux_to_ev(float lux) {
if (lux < 0.1f) lux = 0.1f;
return log2f(lux / 2.5f);
}
static lv_color_t lux_color(float lux) {
if (isnan(lux)) return COL_MUTED;
if (lux < 10.0f) return COL_LEVEL;
if (lux < 200.0f) return COL_OK;
if (lux < 1000.0f) return COL_LUX;
return COL_WARN;
}
static const char *scene_text(float lux) {
if (isnan(lux)) return "NO DATA";
if (lux < 1.0f) return "DARK";
if (lux < 10.0f) return "DIM";
if (lux < 50.0f) return "LOW";
if (lux < 200.0f) return "INDOOR";
if (lux < 500.0f) return "BRIGHT";
if (lux < 2000.0f) return "DAY";
if (lux < 10000.0f) return "SUNNY";
return "DIRECT";
}
static lv_color_t scene_color(float lux) {
if (isnan(lux)) return COL_BAD;
if (lux < 10.0f) return COL_LEVEL;
if (lux < 500.0f) return COL_OK;
if (lux < 2000.0f) return COL_LUX;
return COL_WARN;
}
static void show_hint(const char *text, uint32_t ms) {
lv_label_set_text(lbl_hint, text);
lv_obj_remove_flag(lbl_hint, LV_OBJ_FLAG_HIDDEN);
lv_obj_move_foreground(lbl_hint);
hint_until_ms = millis() + ms;
}
static void reset_minmax(void) {
if (!isnan(disp_lux)) {
min_lux = max_lux = disp_lux;
} else {
min_lux = max_lux = NAN;
}
if (!isnan(disp_level)) {
min_level = max_level = disp_level;
} else {
min_level = max_level = NAN;
}
show_hint("min/max reset", 1200);
}
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;
uint32_t held = millis() - press_ms;
if (held >= 700) {
reset_minmax();
} else if (held < 400) {
set_backlight((bright_level + 1) & 0x03);
char buf[20];
lv_snprintf(buf, sizeof(buf), "brightness %u/3", (unsigned)bright_level);
show_hint(buf, 900);
}
}
}
}
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, 520);
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 void bar_anim_exec(void *obj, int32_t v) {
lv_bar_set_value((lv_obj_t *)obj, v, LV_ANIM_OFF);
}
static void anim_bar(lv_obj_t *bar, int32_t value) {
int32_t cur = lv_bar_get_value(bar);
if (cur == value) return;
lv_anim_t a;
lv_anim_init(&a);
lv_anim_set_var(&a, bar);
lv_anim_set_values(&a, cur, value);
lv_anim_set_duration(&a, 400);
lv_anim_set_exec_cb(&a, bar_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, 12, 0);
lv_obj_set_style_pad_all(p, 6, 0);
lv_obj_set_style_pad_row(p, 0, 0);
lv_obj_set_style_pad_column(p, 0, 0);
lv_obj_clear_flag(p, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_remove_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, UI_ARC, UI_ARC);
lv_obj_align(arc, LV_ALIGN_TOP_MID, 0, 2);
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, 10, LV_PART_MAIN);
lv_obj_set_style_arc_color(arc, accent, LV_PART_INDICATOR);
lv_obj_set_style_arc_width(arc, 10, 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, "LIGHT");
lv_obj_set_style_text_font(title, &lv_font_montserrat_14, 0);
lv_obj_set_style_text_color(title, COL_TEXT, 0);
lv_obj_set_pos(title, UI_M, 4);
lbl_uptime = lv_label_create(scr);
lv_label_set_text(lbl_uptime, "up 00:00");
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_to(lbl_uptime, title, LV_ALIGN_OUT_RIGHT_MID, 8, 1);
led_status = lv_led_create(scr);
lv_obj_set_size(led_status, 9, 9);
lv_obj_align(led_status, LV_ALIGN_TOP_RIGHT, -UI_M, 6);
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, -(UI_M + 16), 4);
lbl_hint = lv_label_create(scr);
lv_label_set_text(lbl_hint, "");
lv_obj_set_style_text_font(lbl_hint, &lv_font_montserrat_12, 0);
lv_obj_set_style_text_color(lbl_hint, COL_WARN, 0);
lv_obj_set_style_bg_color(lbl_hint, COL_BG, 0);
lv_obj_set_style_bg_opa(lbl_hint, LV_OPA_COVER, 0);
lv_obj_set_style_pad_hor(lbl_hint, 6, 0);
lv_obj_align(lbl_hint, LV_ALIGN_TOP_MID, 0, 4);
lv_obj_add_flag(lbl_hint, LV_OBJ_FLAG_HIDDEN);
const int gy = UI_HDR_H + UI_G;
lv_obj_t *p_lux = make_panel(scr, UI_M, gy, UI_GAUGE_W, UI_GAUGE_H);
lv_obj_t *l_cap = lv_label_create(p_lux);
lv_label_set_text(l_cap, "LUX");
lv_obj_set_style_text_font(l_cap, &lv_font_montserrat_12, 0);
lv_obj_set_style_text_color(l_cap, COL_MUTED, 0);
lv_obj_align(l_cap, LV_ALIGN_TOP_LEFT, 0, -2);
arc_lux = make_gauge(p_lux, COL_LUX, LUX_ARC_MAX);
lbl_lux_val = lv_label_create(p_lux);
lv_label_set_text(lbl_lux_val, "----");
lv_obj_set_style_text_font(lbl_lux_val, &lv_font_montserrat_24, 0);
lv_obj_set_style_text_color(lbl_lux_val, COL_LUX, 0);
lv_obj_align(lbl_lux_val, LV_ALIGN_TOP_MID, 0, 32);
lv_obj_t *l_unit = lv_label_create(p_lux);
lv_label_set_text(l_unit, "lx");
lv_obj_set_style_text_font(l_unit, &lv_font_montserrat_12, 0);
lv_obj_set_style_text_color(l_unit, COL_MUTED, 0);
lv_obj_align(l_unit, LV_ALIGN_TOP_MID, 0, 58);
lbl_lux_range = lv_label_create(p_lux);
lv_label_set_text(lbl_lux_range, "-- / --");
lv_obj_set_style_text_font(lbl_lux_range, &lv_font_montserrat_12, 0);
lv_obj_set_style_text_color(lbl_lux_range, COL_MUTED, 0);
lv_obj_align(lbl_lux_range, LV_ALIGN_BOTTOM_MID, 0, 2);
lv_obj_t *p_lvl = make_panel(scr, UI_M + UI_GAUGE_W + UI_G, gy, UI_GAUGE_W, UI_GAUGE_H);
lv_obj_t *v_cap = lv_label_create(p_lvl);
lv_label_set_text(v_cap, "LEVEL");
lv_obj_set_style_text_font(v_cap, &lv_font_montserrat_12, 0);
lv_obj_set_style_text_color(v_cap, COL_MUTED, 0);
lv_obj_align(v_cap, LV_ALIGN_TOP_LEFT, 0, -2);
arc_level = make_gauge(p_lvl, COL_LEVEL, 100);
lbl_level_val = lv_label_create(p_lvl);
lv_label_set_text(lbl_level_val, "--");
lv_obj_set_style_text_font(lbl_level_val, &lv_font_montserrat_24, 0);
lv_obj_set_style_text_color(lbl_level_val, COL_LEVEL, 0);
lv_obj_align(lbl_level_val, LV_ALIGN_TOP_MID, 0, 32);
lv_obj_t *v_unit = lv_label_create(p_lvl);
lv_label_set_text(v_unit, "%");
lv_obj_set_style_text_font(v_unit, &lv_font_montserrat_12, 0);
lv_obj_set_style_text_color(v_unit, COL_MUTED, 0);
lv_obj_align(v_unit, LV_ALIGN_TOP_MID, 0, 58);
lbl_level_range = lv_label_create(p_lvl);
lv_label_set_text(lbl_level_range, "-- / --");
lv_obj_set_style_text_font(lbl_level_range, &lv_font_montserrat_12, 0);
lv_obj_set_style_text_color(lbl_level_range, COL_MUTED, 0);
lv_obj_align(lbl_level_range, LV_ALIGN_BOTTOM_MID, 0, 2);
lv_obj_t *p_bot = make_panel(scr, UI_M, UI_BOT_Y, UI_BOT_W, UI_BOT_H);
lv_obj_t *info = lv_obj_create(p_bot);
lv_obj_set_size(info, UI_INFO_W, UI_BOT_H - 12);
lv_obj_align(info, LV_ALIGN_LEFT_MID, 0, 0);
lv_obj_set_style_bg_opa(info, LV_OPA_TRANSP, 0);
lv_obj_set_style_border_width(info, 0, 0);
lv_obj_set_style_pad_all(info, 0, 0);
lv_obj_clear_flag(info, LV_OBJ_FLAG_SCROLLABLE);
lbl_scene = lv_label_create(info);
lv_label_set_text(lbl_scene, "WAIT");
lv_obj_set_style_text_font(lbl_scene, &lv_font_montserrat_14, 0);
lv_obj_set_style_text_color(lbl_scene, COL_WARN, 0);
lv_obj_set_width(lbl_scene, UI_INFO_W);
lv_label_set_long_mode(lbl_scene, LV_LABEL_LONG_CLIP);
lv_obj_align(lbl_scene, LV_ALIGN_TOP_LEFT, 0, 0);
bar_level = lv_bar_create(info);
lv_obj_set_size(bar_level, UI_INFO_W - 4, 8);
lv_obj_align(bar_level, LV_ALIGN_TOP_LEFT, 0, 20);
lv_bar_set_range(bar_level, 0, 100);
lv_bar_set_value(bar_level, 0, LV_ANIM_OFF);
lv_obj_set_style_bg_color(bar_level, COL_ARC_BG, LV_PART_MAIN);
lv_obj_set_style_bg_opa(bar_level, LV_OPA_COVER, LV_PART_MAIN);
lv_obj_set_style_radius(bar_level, 4, LV_PART_MAIN);
lv_obj_set_style_bg_color(bar_level, COL_WARN, LV_PART_INDICATOR);
lv_obj_set_style_radius(bar_level, 4, LV_PART_INDICATOR);
lbl_ev = lv_label_create(info);
lv_label_set_text(lbl_ev, "EV --.-");
lv_obj_set_style_text_font(lbl_ev, &lv_font_montserrat_12, 0);
lv_obj_set_style_text_color(lbl_ev, COL_MUTED, 0);
lv_obj_set_width(lbl_ev, UI_INFO_W);
lv_label_set_long_mode(lbl_ev, LV_LABEL_LONG_CLIP);
lv_obj_align(lbl_ev, LV_ALIGN_TOP_LEFT, 0, 34);
lbl_hint_line = lv_label_create(info);
lv_label_set_text(lbl_hint_line, "GY30 BH1750");
lv_obj_set_style_text_font(lbl_hint_line, &lv_font_montserrat_12, 0);
lv_obj_set_style_text_color(lbl_hint_line, COL_MUTED, 0);
lv_obj_set_width(lbl_hint_line, UI_INFO_W);
lv_label_set_long_mode(lbl_hint_line, LV_LABEL_LONG_CLIP);
lv_obj_align(lbl_hint_line, LV_ALIGN_TOP_LEFT, 0, 48);
chart = lv_chart_create(p_bot);
lv_obj_set_size(chart, UI_CHART_W, UI_BOT_H - 16);
lv_obj_align(chart, LV_ALIGN_RIGHT_MID, 0, 0);
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, 0, LUX_ARC_MAX);
lv_chart_set_range(chart, LV_CHART_AXIS_SECONDARY_Y, 0, 100);
lv_chart_set_update_mode(chart, LV_CHART_UPDATE_MODE_SHIFT);
lv_obj_set_style_bg_color(chart, COL_CHART_BG, 0);
lv_obj_set_style_bg_opa(chart, LV_OPA_COVER, 0);
lv_obj_set_style_border_color(chart, COL_EDGE, 0);
lv_obj_set_style_border_width(chart, 1, 0);
lv_obj_set_style_radius(chart, 8, 0);
lv_obj_set_style_pad_all(chart, 4, 0);
lv_obj_set_style_line_width(chart, 2, LV_PART_ITEMS);
lv_chart_set_div_line_count(chart, 2, 4);
lv_obj_set_style_line_color(chart, COL_EDGE, LV_PART_MAIN);
lv_obj_set_style_line_opa(chart, LV_OPA_40, LV_PART_MAIN);
ser_lux = lv_chart_add_series(chart, COL_LUX, LV_CHART_AXIS_PRIMARY_Y);
ser_level = lv_chart_add_series(chart, COL_LEVEL, LV_CHART_AXIS_SECONDARY_Y);
lv_obj_move_foreground(title);
lv_obj_move_foreground(lbl_uptime);
lv_obj_move_foreground(lbl_status);
lv_obj_move_foreground(led_status);
lv_obj_move_foreground(lbl_hint);
}
static void refresh_meta(void) {
char buf[40];
uint32_t up = millis() / 1000;
lv_snprintf(buf, sizeof(buf), "up %02lu:%02lu bl%u",
(unsigned long)(up / 60), (unsigned long)(up % 60), (unsigned)bright_level);
lv_label_set_text(lbl_uptime, buf);
if (hint_until_ms && (int32_t)(millis() - hint_until_ms) >= 0) {
hint_until_ms = 0;
lv_obj_add_flag(lbl_hint, LV_OBJ_FLAG_HIDDEN);
}
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[32];
if (ok && !isnan(disp_lux) && !isnan(disp_level)) {
lv_color_t lc = lux_color(disp_lux);
lv_color_t sc = scene_color(disp_lux);
if (disp_lux >= 10000.0f) {
lv_snprintf(buf, sizeof(buf), "%.0fk", disp_lux / 1000.0f);
} else if (disp_lux >= 1000.0f) {
lv_snprintf(buf, sizeof(buf), "%.1fk", disp_lux / 1000.0f);
} else if (disp_lux >= 100.0f) {
lv_snprintf(buf, sizeof(buf), "%.0f", disp_lux);
} else {
lv_snprintf(buf, sizeof(buf), "%.1f", disp_lux);
}
lv_label_set_text(lbl_lux_val, buf);
lv_obj_set_style_text_color(lbl_lux_val, lc, 0);
lv_obj_set_style_arc_color(arc_lux, lc, LV_PART_INDICATOR);
anim_arc(arc_lux, (int)lroundf(constrain(disp_lux, 0.0f, (float)LUX_ARC_MAX)));
lv_snprintf(buf, sizeof(buf), "%.0f", disp_level);
lv_label_set_text(lbl_level_val, buf);
lv_obj_set_style_text_color(lbl_level_val, COL_LEVEL, 0);
anim_arc(arc_level, (int)lroundf(constrain(disp_level, 0.0f, 100.0f)));
lv_snprintf(buf, sizeof(buf), "%.0f / %.0f", min_lux, max_lux);
lv_label_set_text(lbl_lux_range, buf);
lv_snprintf(buf, sizeof(buf), "%.0f / %.0f", min_level, max_level);
lv_label_set_text(lbl_level_range, buf);
float ev = lux_to_ev(disp_lux);
lv_snprintf(buf, sizeof(buf), "EV %.1f", ev);
lv_label_set_text(lbl_ev, buf);
lv_label_set_text(lbl_scene, scene_text(disp_lux));
lv_obj_set_style_text_color(lbl_scene, sc, 0);
anim_bar(bar_level, (int)lroundf(disp_level));
lv_obj_set_style_bg_color(bar_level, sc, LV_PART_INDICATOR);
lv_label_set_text(lbl_hint_line, "tap bl / hold mm");
lv_chart_set_next_value(chart, ser_lux,
(int32_t)lroundf(constrain(disp_lux, 0.0f, (float)LUX_ARC_MAX)));
lv_chart_set_next_value(chart, ser_level, (int32_t)lroundf(disp_level));
refresh_meta();
} else {
lv_label_set_text(lbl_scene, "ERROR");
lv_obj_set_style_text_color(lbl_scene, COL_BAD, 0);
lv_label_set_text(lbl_status, "ERR");
lv_led_set_color(led_status, COL_BAD);
}
}
static void read_gy30(void) {
float lux = lightMeter.readLightLevel();
bool ok = !(lux < 0.0f || isnan(lux));
if (!ok) {
fail_streak++;
Serial.printf("GY30 fail(%u) lux=%.2f\n", fail_streak, lux);
if (fail_streak >= GY_FAIL_TOLERANCE) {
sensor_ok = false;
update_ui(false);
}
return;
}
fail_streak = 0;
sensor_ok = true;
last_ok_ms = millis();
float level = lux_to_level(lux);
disp_lux = ema(disp_lux, lux);
disp_level = ema(disp_level, level);
if (isnan(min_lux) || disp_lux < min_lux) min_lux = disp_lux;
if (isnan(max_lux) || disp_lux > max_lux) max_lux = disp_lux;
if (isnan(min_level) || disp_level < min_level) min_level = disp_level;
if (isnan(max_level) || disp_level > max_level) max_level = disp_level;
Serial.printf("lux=%.1f(%.1f) level=%.0f%% EV=%.1f\n",
disp_lux, lux, disp_level, lux_to_ev(disp_lux));
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 GY30 Light Dashboard");
Serial.printf("LVGL %d.%d.%d SDA=GPIO%d SCL=GPIO%d\n",
lv_version_major(), lv_version_minor(), lv_version_patch(),
I2C_SDA, I2C_SCL);
Wire.begin(I2C_SDA, I2C_SCL);
Wire.setClock(100000);
if (!lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
Serial.println("GY30/BH1750 not found — check wiring (SDA22/SCL27)");
} else {
Serial.println("GY30/BH1750 OK");
}
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(200);
last_gy_ms = 0;
}
void loop() {
uint32_t now = millis();
if (now - last_gy_ms >= GY_READ_MS) {
last_gy_ms = now;
read_gy30();
}
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);
}