// ld2410.h — HLK-LD2410 24 GHz presence radar. // // The module streams a report frame a few times a second on UART (256000 8N1 by // factory default) and simultaneously drives an active-high OUT pin. We use // both: the pin is a zero-latency "something is there", the UART report carries // the distance and energy needed to reject swaying branches and passing people. // // Frame formats (HLK datasheet): // report F4 F3 F2 F1 | len(2,LE) | payload | F8 F7 F6 F5 // command FD FC FB FA | len(2,LE) | payload | 04 03 02 01 // // Basic report payload (type 0x02): // [0] 0x02 type [1] 0xAA head // [2] target state: 0=none 1=moving 2=stationary 3=both // [3..4] moving distance cm (LE) [5] moving energy 0..100 // [6..7] stationary distance cm (LE) [8] stationary energy 0..100 // [9..10] detection distance cm (LE) // [11] 0x55 tail [12] 0x00 check // // These offsets are transcribed from the datasheet, not verified against real // hardware. Run `radarSelfTest()` once the module is wired and check the log // before trusting the parsed values. #pragma once #include enum RadarTarget : uint8_t { TARGET_NONE = 0, TARGET_MOVING = 1, TARGET_STATIONARY = 2, TARGET_BOTH = 3, }; struct RadarReading { bool valid; // a well-formed frame was decoded uint8_t target; // RadarTarget uint16_t moving_cm; uint8_t moving_energy; // 0..100 uint16_t stationary_cm; uint8_t stationary_energy; // 0..100 uint16_t detect_cm; bool out_pin; // raw presence pin, independent of UART uint32_t at_ms; // millis() when decoded }; // Bring up UART1 on the configured pins and set the OUT pin as an input. // // Probes for the module's baud rate rather than assuming one: the LD2410 // ships at 256000 and can be reconfigured to 115200 and others, and a factory // reset puts it back. Autodetecting means neither event takes the trigger // offline on a device you cannot physically reach. Pin it with // `g_cfg.radar_baud` to skip the probe. // // Returns false if no baud produced a valid frame. That is not fatal — the // OUT pin path (radar_mode = RADAR_OUT_PIN_ONLY) needs no UART at all. bool radarBegin(); // The baud actually in use, or 0 if nothing was decoded. uint32_t radarBaud(); // Pump the UART. Call often (the radar task does, every few ms). Returns true // when a new report frame was decoded since the last call. bool radarPoll(); // Most recent decoded report, plus a live read of the OUT pin. RadarReading radarLast(); // Apply the configured distance window, energy floor and stationary policy. // This is the single place that decides "is this worth waking the camera for", // so tuning it in the field is one config push, not a firmware change. bool radarIsTrigger(const RadarReading& r); // Log raw bytes for a few seconds so you can confirm baud rate and framing // when the hardware arrives. Blocking; bring-up only. void radarSelfTest(uint32_t duration_ms = 5000); // Enter/leave the module's configuration mode. Needed before any parameter // write; the module stops reporting while in config mode. bool radarEnterConfig(); bool radarExitConfig(); // Per-gate sensitivity. gate 0..8 (~0.75 m each), values 0..100. // Only call between radarEnterConfig()/radarExitConfig(). bool radarSetGateSensitivity(uint8_t gate, uint8_t moving, uint8_t stationary);