Initial CYD LD2420 radar dashboard with flashable firmware archive.

Presence uses ESPHome factory gates/timeout; OT1->GPIO22 RX->GPIO27; prebuilt bins under firmware/cyd_28r.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-10-18 21:53:26 +08:00
commit d5cfc30b3b
16 changed files with 1422 additions and 0 deletions

924
src/main.cpp Normal file
View File

@@ -0,0 +1,924 @@
/**
* CYD ESP32-2432S028R — LVGL LD2420 毫米波雷达仪表盘
*
* 接线 (固件 ≥ v1.5.3):
* LD2420 OT1 -> GPIO22 (模块串口 TX → ESP RX)
* LD2420 RX -> GPIO27 (模块串口 RX → ESP TX)
* VCC 3.3V / GND
*/
#include <Arduino.h>
#include <lvgl.h>
#include <TFT_eSPI.h>
#include <XPT2046_Touchscreen.h>
#include <math.h>
#include <string.h>
/* ---- pins ---- */
#define LD2420_RX_PIN 22 /* ESP RX <- OT1 */
#define LD2420_TX_PIN 27 /* ESP TX -> RX */
#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 UI_REFRESH_MS 250
#define CHART_PUSH_MS 1000
#define SENSOR_STALE_MS 5000
#define HISTORY_LEN 48
#define DIST_ARC_MAX 800 /* cm (~11 gates * 70cm) */
#define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT / 10 * (LV_COLOR_DEPTH / 8))
/* Layout grid — exclusive regions, 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 */
/* LD2420 protocol (wire byte order) */
static const uint8_t CMD_HDR_B[4] = {0xFD, 0xFC, 0xFB, 0xFA};
static const uint8_t CMD_FTR_B[4] = {0x04, 0x03, 0x02, 0x01};
static const uint8_t ENERGY_HDR_B[4] = {0xF1, 0xF2, 0xF3, 0xF4};
static const uint8_t ENERGY_FTR_B[4] = {0xF5, 0xF6, 0xF7, 0xF8};
static const uint16_t CMD_ENABLE_CONF = 0x00FF;
static const uint16_t CMD_DISABLE_CONF = 0x00FE;
static const uint16_t CMD_READ_VERSION = 0x0000;
static const uint16_t CMD_WRITE_SYS = 0x0012;
static const uint16_t CMD_WRITE_ABD = 0x0007;
static const uint16_t CMD_PROTOCOL_VER = 0x0002;
static const uint16_t SYS_MODE_PARAM = 0x0000;
static const uint16_t MODE_SIMPLE = 0x0064;
/*
* Presence semantics (ESPHome / HLK):
* - move OR still energy → occupied
* - presence_timeout keeps ON after last hit; only then OFF
* Factory still thresholds are low so sitting still stays detected.
*/
static const uint32_t CFG_MIN_GATE = 1; /* ~0.7 m */
static const uint32_t CFG_MAX_GATE = 12; /* ~8.4 m */
static const uint32_t CFG_TIMEOUT_S = 10; /* seconds after leave before OFF */
/* ESPHome factory thresholds — still must stay sensitive */
static const uint32_t CFG_MOVE_THRESH[16] = {
60000, 30000, 400, 300, 250, 250, 250, 250,
300, 250, 250, 250, 250, 200, 200, 200};
static const uint32_t CFG_STILL_THRESH[16] = {
40000, 20000, 200, 250, 150, 150, 150, 150,
150, 150, 150, 150, 100, 100, 100, 100};
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_DIST = lv_color_hex(0x4DA3FF);
static const lv_color_t COL_PRES = lv_color_hex(0x3DBF8C);
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);
HardwareSerial RadarSerial(2);
static uint32_t draw_buf[DRAW_BUF_SIZE / 4];
static lv_obj_t *arc_dist;
static lv_obj_t *arc_pres;
static lv_obj_t *lbl_dist_val;
static lv_obj_t *lbl_pres_val;
static lv_obj_t *lbl_dist_range;
static lv_obj_t *lbl_pres_hint;
static lv_obj_t *lbl_state;
static lv_obj_t *lbl_fw;
static lv_obj_t *lbl_baud;
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_presence;
static lv_obj_t *chart;
static lv_chart_series_t *ser_dist;
static lv_chart_series_t *ser_pres;
static float disp_dist = NAN;
static float min_dist = NAN;
static float max_dist = NAN;
static bool presence = false;
static bool sensor_ok = false;
static bool fw_ok = false;
static char fw_version[24] = "----";
static uint32_t radar_baud = 115200;
static bool energy_mode = false;
static uint32_t last_ok_ms = 0;
static uint32_t last_ui_ms = 0;
static uint32_t last_chart_ms = 0;
static uint32_t last_dbg_ms = 0;
static uint32_t presence_since_ms = 0;
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 uint32_t last_seen_ms = 0; /* last time module reported presence energy */
static uint8_t rx_buf[512];
static uint16_t rx_pos = 0;
static void set_backlight(uint8_t level) {
bright_level = level & 0x03;
ledcWrite(0, BRIGHT_PWM[bright_level]);
}
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_dist)) {
min_dist = max_dist = disp_dist;
} else {
min_dist = max_dist = 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, 420);
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, 320);
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_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 lv_color_t dist_color(float cm) {
if (isnan(cm)) return COL_MUTED;
if (cm < 150.0f) return COL_PRES;
if (cm < 400.0f) return COL_DIST;
if (cm < 600.0f) return COL_WARN;
return COL_BAD;
}
static int fw_to_int(const char *ver) {
const char *p = (*ver == 'v' || *ver == 'V') ? ver + 1 : ver;
int result = 0;
for (; *p; p++) {
if (*p == '.') continue;
if (*p < '0' || *p > '9') break;
result = result * 10 + (*p - '0');
}
return result;
}
/* ---- LD2420 command helpers ---- */
static void radar_flush(void) {
while (RadarSerial.available()) RadarSerial.read();
rx_pos = 0;
}
static void radar_write_frame(uint16_t cmd, const uint8_t *data, uint16_t data_len) {
uint8_t buf[64];
uint16_t len = 0;
uint16_t frame_data = (uint16_t)(data_len + 2);
memcpy(&buf[len], CMD_HDR_B, 4); len += 4;
memcpy(&buf[len], &frame_data, 2); len += 2;
memcpy(&buf[len], &cmd, 2); len += 2;
if (data && data_len) {
memcpy(&buf[len], data, data_len);
len += data_len;
}
memcpy(&buf[len], CMD_FTR_B, 4); len += 4;
RadarSerial.write(buf, len);
RadarSerial.flush();
}
static bool radar_wait_ack(uint16_t expect_cmd, uint8_t *out, uint16_t out_cap, uint16_t *out_len,
uint32_t timeout_ms) {
uint8_t buf[128];
uint16_t pos = 0;
uint32_t start = millis();
uint16_t reply_cmd = (uint16_t)(expect_cmd | 0x0100);
while ((millis() - start) < timeout_ms) {
while (RadarSerial.available() && pos < sizeof(buf)) {
buf[pos++] = (uint8_t)RadarSerial.read();
if (pos >= 8) {
for (uint16_t i = 0; i + 4 <= pos; i++) {
if (memcmp(&buf[i], CMD_HDR_B, 4) != 0) continue;
if (i + 10 > pos) continue;
uint16_t flen = 0;
memcpy(&flen, &buf[i + 4], 2);
uint16_t total = (uint16_t)(4 + 2 + flen + 4);
if (i + total > pos) continue;
if (memcmp(&buf[i + total - 4], CMD_FTR_B, 4) != 0) continue;
uint16_t cmd = 0;
memcpy(&cmd, &buf[i + 6], 2);
if (cmd != reply_cmd && cmd != expect_cmd) continue;
uint16_t copy = total;
if (copy > out_cap) copy = out_cap;
memcpy(out, &buf[i], copy);
*out_len = copy;
return true;
}
}
}
delay(2);
}
return false;
}
static bool radar_write_abd_u32(uint16_t param, uint32_t value) {
uint8_t data[6];
memcpy(&data[0], &param, 2);
memcpy(&data[2], &value, 4);
radar_write_frame(CMD_WRITE_ABD, data, 6);
uint8_t ack[64];
uint16_t ack_len = 0;
return radar_wait_ack(CMD_WRITE_ABD, ack, sizeof(ack), &ack_len, 1200);
}
static bool radar_read_abd_u32(uint16_t param, uint32_t *value) {
uint8_t data[2];
memcpy(data, &param, 2);
radar_write_frame(0x0008, data, 2); /* CMD_READ_ABD */
uint8_t ack[64];
uint16_t ack_len = 0;
if (!radar_wait_ack(0x0008, ack, sizeof(ack), &ack_len, 1200)) return false;
if (ack_len < 14) return false;
memcpy(value, &ack[10], 4);
return true;
}
static bool radar_apply_detection_window(void) {
bool ok = true;
if (!radar_write_abd_u32(0x0000, CFG_MIN_GATE)) ok = false;
delay(20);
if (!radar_write_abd_u32(0x0001, CFG_MAX_GATE)) ok = false;
delay(20);
if (!radar_write_abd_u32(0x0004, CFG_TIMEOUT_S)) ok = false;
delay(20);
for (uint8_t g = 0; g < 16; g++) {
if (!radar_write_abd_u32((uint16_t)(0x0010 + g), CFG_MOVE_THRESH[g])) ok = false;
delay(12);
if (!radar_write_abd_u32((uint16_t)(0x0020 + g), CFG_STILL_THRESH[g])) ok = false;
delay(12);
}
uint32_t rmin = 0, rmax = 0, rto = 0;
radar_read_abd_u32(0x0000, &rmin);
radar_read_abd_u32(0x0001, &rmax);
radar_read_abd_u32(0x0004, &rto);
Serial.printf(" set gates %lu..%lu to=%lus (%s)\n",
(unsigned long)CFG_MIN_GATE, (unsigned long)CFG_MAX_GATE,
(unsigned long)CFG_TIMEOUT_S, ok ? "OK" : "FAIL");
Serial.printf(" readback min=%lu max=%lu timeout=%lu\n",
(unsigned long)rmin, (unsigned long)rmax, (unsigned long)rto);
return ok;
}
static bool radar_enable_config(bool enable) {
uint8_t ack[64];
uint16_t ack_len = 0;
if (enable) {
uint8_t data[2];
memcpy(data, &CMD_PROTOCOL_VER, 2);
radar_write_frame(CMD_ENABLE_CONF, data, 2);
return radar_wait_ack(CMD_ENABLE_CONF, ack, sizeof(ack), &ack_len, 1200);
}
radar_write_frame(CMD_DISABLE_CONF, NULL, 0);
return radar_wait_ack(CMD_DISABLE_CONF, ack, sizeof(ack), &ack_len, 1200);
}
static bool radar_read_version(char *out, size_t out_sz) {
uint8_t ack[96];
uint16_t ack_len = 0;
radar_write_frame(CMD_READ_VERSION, NULL, 0);
if (!radar_wait_ack(CMD_READ_VERSION, ack, sizeof(ack), &ack_len, 1500)) return false;
if (ack_len < 13) return false;
uint8_t ver_len = ack[10];
if (ver_len == 0 || (uint16_t)(12 + ver_len) > ack_len - 4) {
/* some firmwares put ASCII after error word without length prefix */
size_t n = 0;
for (uint16_t i = 10; i + 4 < ack_len && n + 1 < out_sz; i++) {
char c = (char)ack[i];
if (c < 32 || c > 126) break;
out[n++] = c;
}
out[n] = '\0';
return n > 0;
}
if (ver_len >= out_sz) ver_len = (uint8_t)(out_sz - 1);
memcpy(out, &ack[12], ver_len);
out[ver_len] = '\0';
return true;
}
static bool radar_set_mode(uint16_t mode) {
uint8_t data[8];
memcpy(&data[0], &SYS_MODE_PARAM, 2);
uint32_t m = mode;
memcpy(&data[2], &m, 4);
radar_write_frame(CMD_WRITE_SYS, data, 6);
uint8_t ack[64];
uint16_t ack_len = 0;
return radar_wait_ack(CMD_WRITE_SYS, ack, sizeof(ack), &ack_len, 1200);
}
static bool radar_probe_at_baud(uint32_t baud) {
Serial.printf("LD2420 probe @ %lu baud RX=%d TX=%d\n",
(unsigned long)baud, LD2420_RX_PIN, LD2420_TX_PIN);
RadarSerial.end();
delay(20);
RadarSerial.begin(baud, SERIAL_8N1, LD2420_RX_PIN, LD2420_TX_PIN);
delay(80);
radar_flush();
if (!radar_enable_config(true)) {
Serial.println(" enable config FAIL");
return false;
}
char ver[24] = {0};
bool got = radar_read_version(ver, sizeof(ver));
if (!got) {
Serial.println(" read version FAIL");
radar_enable_config(false);
return false;
}
strncpy(fw_version, ver, sizeof(fw_version) - 1);
fw_version[sizeof(fw_version) - 1] = '\0';
fw_ok = true;
radar_baud = baud;
/* Simple text ON/OFF is more reliable for presence; energy optional later */
energy_mode = false;
Serial.printf(" FW=%s mode=simple\n", fw_version);
(void)fw_to_int(fw_version);
radar_apply_detection_window();
if (!radar_set_mode(MODE_SIMPLE)) {
Serial.println(" set mode FAIL (continue with default)");
}
radar_enable_config(false);
radar_flush();
return true;
}
static bool radar_init(void) {
static const uint32_t bauds[] = {115200, 256000};
for (uint8_t i = 0; i < 2; i++) {
if (radar_probe_at_baud(bauds[i])) return true;
}
/* last resort: open 115200 and hope for simple-mode text */
RadarSerial.begin(115200, SERIAL_8N1, LD2420_RX_PIN, LD2420_TX_PIN);
radar_baud = 115200;
energy_mode = false;
strncpy(fw_version, "N/A", sizeof(fw_version) - 1);
fw_ok = false;
Serial.println("LD2420: no ACK — listening for simple-mode text");
return false;
}
/*
* Trust module ON/OFF. Module already applies presence_timeout for still/leave.
* Extra software hold only bridges brief UART glitches (not for filtering people out).
*/
static void set_occupied(bool on, float cm) {
bool was = presence;
uint32_t now = millis();
if (on) {
last_seen_ms = now;
presence = true;
if (!was) {
presence_since_ms = now;
Serial.println("UI: OCCUPIED");
}
if (!isnan(cm) && cm > 0.0f) {
disp_dist = cm;
if (isnan(min_dist) || cm < min_dist) min_dist = cm;
if (isnan(max_dist) || cm > max_dist) max_dist = cm;
}
} else {
/* Module said OFF — clear immediately (timeout already elapsed on radar) */
presence = false;
presence_since_ms = 0;
disp_dist = 0.0f;
if (was) Serial.println("UI: CLEAR");
}
sensor_ok = true;
last_ok_ms = now;
}
static void handle_simple_line(const uint8_t *inbuf, int len) {
/* Lines: "ON", "OFF", "Range 123" */
while (len > 0 && (inbuf[len - 1] == '\r' || inbuf[len - 1] == '\n' || inbuf[len - 1] == 0)) {
len--;
}
if (len <= 0) return;
char line[48];
uint8_t n = (len < (int)sizeof(line) - 1) ? (uint8_t)len : (uint8_t)(sizeof(line) - 1);
memcpy(line, inbuf, n);
line[n] = '\0';
if (n == 3 && line[0] == 'O' && line[1] == 'F' && line[2] == 'F') {
set_occupied(false, NAN);
return;
}
if (n == 2 && line[0] == 'O' && line[1] == 'N') {
set_occupied(true, disp_dist); /* keep last range until Range line updates */
return;
}
if (n >= 6 && strncmp(line, "Range", 5) == 0) {
const char *p = line + 5;
while (*p == ' ' || *p == '\t') p++;
if (*p >= '0' && *p <= '9') {
float cm = (float)strtol(p, NULL, 10);
/* Range with value implies target; latch occupied + distance */
set_occupied(true, cm);
}
return;
}
}
static void handle_energy_frame(const uint8_t *buf, int len) {
if (len < 6 + 1 + 2 + 4) return;
uint8_t index = 6;
bool present = (buf[index++] == 1);
uint16_t range = 0;
memcpy(&range, &buf[index], 2);
if (range > 2000) return;
set_occupied(present, present ? (float)range : NAN);
}
static void radar_poll(void) {
while (RadarSerial.available()) {
int b = RadarSerial.read();
if (b < 0) break;
if (rx_pos >= sizeof(rx_buf) - 1) rx_pos = 0;
rx_buf[rx_pos++] = (uint8_t)b;
rx_buf[rx_pos] = 0;
if (rx_pos < 4) continue;
/* discard command ACK frames */
if (memcmp(&rx_buf[rx_pos - 4], CMD_FTR_B, 4) == 0) {
rx_pos = 0;
continue;
}
if (energy_mode && memcmp(&rx_buf[rx_pos - 4], ENERGY_FTR_B, 4) == 0) {
for (int i = (int)rx_pos - 4; i >= 0; i--) {
if (memcmp(&rx_buf[i], ENERGY_HDR_B, 4) == 0) {
handle_energy_frame(&rx_buf[i], rx_pos - (uint16_t)i);
break;
}
}
rx_pos = 0;
continue;
}
if (rx_pos >= 2 && rx_buf[rx_pos - 2] == 0x0D && rx_buf[rx_pos - 1] == 0x0A) {
handle_simple_line(rx_buf, rx_pos);
rx_pos = 0;
}
}
}
/* ---- UI ---- */
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, "RADAR");
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_dist = make_panel(scr, UI_M, gy, UI_GAUGE_W, UI_GAUGE_H);
lv_obj_t *d_cap = lv_label_create(p_dist);
lv_label_set_text(d_cap, "DIST");
lv_obj_set_style_text_font(d_cap, &lv_font_montserrat_12, 0);
lv_obj_set_style_text_color(d_cap, COL_MUTED, 0);
lv_obj_align(d_cap, LV_ALIGN_TOP_LEFT, 0, -2);
arc_dist = make_gauge(p_dist, COL_DIST, DIST_ARC_MAX);
lbl_dist_val = lv_label_create(p_dist);
lv_label_set_text(lbl_dist_val, "---");
lv_obj_set_style_text_font(lbl_dist_val, &lv_font_montserrat_24, 0);
lv_obj_set_style_text_color(lbl_dist_val, COL_DIST, 0);
lv_obj_align(lbl_dist_val, LV_ALIGN_TOP_MID, -6, 32);
lv_obj_t *d_unit = lv_label_create(p_dist);
lv_label_set_text(d_unit, "cm");
lv_obj_set_style_text_font(d_unit, &lv_font_montserrat_12, 0);
lv_obj_set_style_text_color(d_unit, COL_MUTED, 0);
lv_obj_align_to(d_unit, lbl_dist_val, LV_ALIGN_OUT_RIGHT_MID, 2, 2);
lbl_dist_range = lv_label_create(p_dist);
lv_label_set_text(lbl_dist_range, "-- / --");
lv_obj_set_style_text_font(lbl_dist_range, &lv_font_montserrat_12, 0);
lv_obj_set_style_text_color(lbl_dist_range, COL_MUTED, 0);
lv_obj_align(lbl_dist_range, LV_ALIGN_BOTTOM_MID, 0, 2);
lv_obj_t *p_pres = make_panel(scr, UI_M + UI_GAUGE_W + UI_G, gy, UI_GAUGE_W, UI_GAUGE_H);
lv_obj_t *p_cap = lv_label_create(p_pres);
lv_label_set_text(p_cap, "PRESENCE");
lv_obj_set_style_text_font(p_cap, &lv_font_montserrat_12, 0);
lv_obj_set_style_text_color(p_cap, COL_MUTED, 0);
lv_obj_align(p_cap, LV_ALIGN_TOP_LEFT, 0, -2);
arc_pres = make_gauge(p_pres, COL_PRES, 100);
lbl_pres_val = lv_label_create(p_pres);
lv_label_set_text(lbl_pres_val, "----");
lv_obj_set_style_text_font(lbl_pres_val, &lv_font_montserrat_20, 0);
lv_obj_set_style_text_color(lbl_pres_val, COL_MUTED, 0);
lv_obj_align(lbl_pres_val, LV_ALIGN_TOP_MID, 0, 34);
lbl_pres_hint = lv_label_create(p_pres);
lv_label_set_text(lbl_pres_hint, "waiting");
lv_obj_set_style_text_font(lbl_pres_hint, &lv_font_montserrat_12, 0);
lv_obj_set_style_text_color(lbl_pres_hint, COL_MUTED, 0);
lv_obj_align(lbl_pres_hint, 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_state = lv_label_create(info);
lv_label_set_text(lbl_state, "WAIT");
lv_obj_set_style_text_font(lbl_state, &lv_font_montserrat_14, 0);
lv_obj_set_style_text_color(lbl_state, COL_WARN, 0);
lv_obj_set_width(lbl_state, UI_INFO_W);
lv_label_set_long_mode(lbl_state, LV_LABEL_LONG_CLIP);
lv_obj_align(lbl_state, LV_ALIGN_TOP_LEFT, 0, 0);
bar_presence = lv_bar_create(info);
lv_obj_set_size(bar_presence, UI_INFO_W - 4, 8);
lv_obj_align(bar_presence, LV_ALIGN_TOP_LEFT, 0, 20);
lv_bar_set_range(bar_presence, 0, 100);
lv_bar_set_value(bar_presence, 0, LV_ANIM_OFF);
lv_obj_set_style_bg_color(bar_presence, COL_ARC_BG, LV_PART_MAIN);
lv_obj_set_style_bg_opa(bar_presence, LV_OPA_COVER, LV_PART_MAIN);
lv_obj_set_style_radius(bar_presence, 4, LV_PART_MAIN);
lv_obj_set_style_bg_color(bar_presence, COL_WARN, LV_PART_INDICATOR);
lv_obj_set_style_radius(bar_presence, 4, LV_PART_INDICATOR);
lbl_fw = lv_label_create(info);
lv_label_set_text(lbl_fw, "FW ----");
lv_obj_set_style_text_font(lbl_fw, &lv_font_montserrat_12, 0);
lv_obj_set_style_text_color(lbl_fw, COL_TEXT, 0);
lv_obj_set_width(lbl_fw, UI_INFO_W);
lv_label_set_long_mode(lbl_fw, LV_LABEL_LONG_CLIP);
lv_obj_align(lbl_fw, LV_ALIGN_TOP_LEFT, 0, 34);
lbl_baud = lv_label_create(info);
lv_label_set_text(lbl_baud, "UART --");
lv_obj_set_style_text_font(lbl_baud, &lv_font_montserrat_12, 0);
lv_obj_set_style_text_color(lbl_baud, COL_MUTED, 0);
lv_obj_set_width(lbl_baud, UI_INFO_W);
lv_label_set_long_mode(lbl_baud, LV_LABEL_LONG_CLIP);
lv_obj_align(lbl_baud, 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, DIST_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_dist = lv_chart_add_series(chart, COL_DIST, LV_CHART_AXIS_PRIMARY_Y);
ser_pres = lv_chart_add_series(chart, COL_PRES, 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[48];
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 && (millis() - last_ok_ms) < SENSOR_STALE_MS) {
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, presence ? COL_PRES : COL_OK);
} else if (fw_ok) {
lv_label_set_text(lbl_status, "IDLE");
lv_led_set_color(led_status, COL_WARN);
} else {
lv_label_set_text(lbl_status, "NO LINK");
lv_led_set_color(led_status, COL_BAD);
}
}
static void update_ui(void) {
char buf[40];
lv_snprintf(buf, sizeof(buf), "FW %s", fw_version);
lv_label_set_text(lbl_fw, buf);
lv_obj_set_style_text_color(lbl_fw, fw_ok ? COL_TEXT : COL_BAD, 0);
lv_snprintf(buf, sizeof(buf), "%lu %s", (unsigned long)radar_baud,
energy_mode ? "E" : "S");
lv_label_set_text(lbl_baud, buf);
if (!sensor_ok && !fw_ok) {
lv_label_set_text(lbl_state, "NO LINK");
lv_obj_set_style_text_color(lbl_state, COL_BAD, 0);
lv_label_set_text(lbl_pres_val, "----");
lv_obj_set_style_text_color(lbl_pres_val, COL_MUTED, 0);
anim_bar(bar_presence, 0);
refresh_meta();
return;
}
if (presence) {
lv_label_set_text(lbl_state, "OCCUPIED");
lv_obj_set_style_text_color(lbl_state, COL_PRES, 0);
lv_label_set_text(lbl_pres_val, "ON");
lv_obj_set_style_text_color(lbl_pres_val, COL_PRES, 0);
anim_arc(arc_pres, 100);
anim_bar(bar_presence, 100);
lv_obj_set_style_bg_color(bar_presence, COL_PRES, LV_PART_INDICATOR);
if (presence_since_ms) {
uint32_t sec = (millis() - presence_since_ms) / 1000;
lv_snprintf(buf, sizeof(buf), "held %lus", (unsigned long)sec);
lv_label_set_text(lbl_pres_hint, buf);
}
} else {
lv_label_set_text(lbl_state, "CLEAR");
lv_obj_set_style_text_color(lbl_state, COL_MUTED, 0);
lv_label_set_text(lbl_pres_val, "OFF");
lv_obj_set_style_text_color(lbl_pres_val, COL_MUTED, 0);
anim_arc(arc_pres, 0);
anim_bar(bar_presence, 0);
lv_obj_set_style_bg_color(bar_presence, COL_MUTED, LV_PART_INDICATOR);
lv_label_set_text(lbl_pres_hint, "no target");
}
if (!isnan(disp_dist)) {
lv_color_t dc = dist_color(disp_dist);
lv_snprintf(buf, sizeof(buf), "%.0f", disp_dist);
lv_label_set_text(lbl_dist_val, buf);
lv_obj_set_style_text_color(lbl_dist_val, dc, 0);
lv_obj_set_style_arc_color(arc_dist, dc, LV_PART_INDICATOR);
anim_arc(arc_dist, (int)lroundf(constrain(disp_dist, 0.0f, (float)DIST_ARC_MAX)));
if (!isnan(min_dist) && !isnan(max_dist)) {
lv_snprintf(buf, sizeof(buf), "%.0f / %.0f", min_dist, max_dist);
lv_label_set_text(lbl_dist_range, buf);
}
}
uint32_t now = millis();
if (now - last_chart_ms >= CHART_PUSH_MS) {
last_chart_ms = now;
int32_t d = isnan(disp_dist) ? 0 : (int32_t)lroundf(constrain(disp_dist, 0.0f, (float)DIST_ARC_MAX));
lv_chart_set_next_value(chart, ser_dist, d);
lv_chart_set_next_value(chart, ser_pres, presence ? 100 : 0);
}
refresh_meta();
}
void setup() {
Serial.begin(115200);
delay(200);
ledcSetup(0, 5000, 8);
ledcAttachPin(TFT_BL_PIN, 0);
set_backlight(2);
Serial.println("CYD LVGL LD2420 Radar Dashboard");
Serial.printf("LVGL %d.%d.%d OT1->GPIO%d RX->GPIO%d\n",
lv_version_major(), lv_version_minor(), lv_version_patch(),
LD2420_RX_PIN, LD2420_TX_PIN);
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();
bool linked = radar_init();
if (linked) {
char hint[32];
lv_snprintf(hint, sizeof(hint), "FW %s", fw_version);
show_hint(hint, 2000);
} else {
show_hint("radar no ACK", 2500);
}
update_ui();
}
void loop() {
radar_poll();
uint32_t now = millis();
if (now - last_ui_ms >= UI_REFRESH_MS) {
last_ui_ms = now;
update_ui();
}
static uint32_t last_meta_ms = 0;
if (now - last_meta_ms >= 1000) {
last_meta_ms = now;
refresh_meta();
}
if (now - last_dbg_ms >= 1000) {
last_dbg_ms = now;
Serial.printf("pres=%s dist=%.0f cm FW=%s\n",
presence ? "ON" : "OFF",
isnan(disp_dist) ? -1.0f : disp_dist,
fw_version);
}
lv_timer_handler();
lv_tick_inc(5);
delay(5);
}