Files
qtalker---/firmware/esp32p4-sensor-node/main/sensor_registry.c
Indiana 348b5fc778 feat(firmware): ESP32-P4 sensor node — Workstream I core skeleton
New firmware/esp32p4-sensor-node/ ESP-IDF (C, FreeRTOS) project skeleton
per docs/superpowers/specs/2026-07-23-esp32-sensor-node-design.md's
Workstream I:

- Wi-Fi station-mode connect with exponential-backoff reconnect
  (wifi_manager.c), credentials from a gitignored main/device_config.h
  the seeker fills in (template: device_config.h.example).
- Telemetry HTTP client (telemetry_client.c) POSTing the spec's exact
  contract shape to /api/device/telemetry with a Bearer token, via
  esp_http_client + cJSON.
- BME280 I2C driver (bme280.c) with Bosch's public double-precision
  compensation formulas, using ESP-IDF's newer driver/i2c_master.h API.
- LD2410 mmWave presence driver (ld2410.c) over UART, chosen over a
  plain PIR for its distance/motion data richness — its frame-offset
  parsing is flagged as the least-certain code in the firmware.
- sensor_driver_t registry (sensor_driver.h, sensor_registry.c) so new
  sensors are a new driver file + one array line, no main-loop changes.
- README.md: build steps, manual-config walkthrough, wiring/pinouts,
  and an explicit "what's verified vs. not" section plus a real
  hardware caveat (ESP32-P4 has no integrated Wi-Fi radio).

UNVERIFIED AGAINST REAL HARDWARE per the spec's honesty-policy note —
no ESP-IDF toolchain or physical boards available in this environment.
Syntax-checked with gcc against hand-written ESP-IDF API stubs (not
committed) as a best-effort substitute for a real idf.py build.

Workstream J (RTL-SDR experimental module) is explicitly out of scope
here; firmware/esp32p4-sensor-node/components/ is left in place for it.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-24 01:13:44 +00:00

74 lines
2.8 KiB
C

// Sensor driver registry -- see sensor_registry.h.
//
// ADDING A NEW SENSOR: write its driver as a new .c/.h pair implementing
// sensor_driver_t's init()/read() (see sensor_driver.h), add the .c file to
// main/CMakeLists.txt's SRCS list, #include its header below, and add one
// line to the s_drivers[] array. Nothing else in this firmware needs to
// change -- app_main.c's task loop and telemetry_client.c's POST logic both
// iterate the registry generically and have no per-sensor-type branches.
#include <stdbool.h>
#include "sensor_registry.h"
#include "bme280.h"
#include "ld2410.h"
#include "esp_log.h"
static const char *TAG = "sensor_registry";
// The concrete list of sensor drivers compiled into this firmware. Order
// matters only for init() sequencing (e.g. bus setup before device use);
// read() order just determines readings array ordering in the POST body.
static const sensor_driver_t s_drivers[] = {
{ .name = "bme280", .init = bme280_init, .read = bme280_read },
{ .name = "ld2410", .init = ld2410_init, .read = ld2410_read },
// Add new drivers here, e.g.:
// { .name = "my_sensor", .init = my_sensor_init, .read = my_sensor_read },
};
static const size_t s_driver_count = sizeof(s_drivers) / sizeof(s_drivers[0]);
// Tracks which drivers initialized successfully so a failed driver's read()
// isn't called every cycle (and doesn't spam logs) -- but the array stays
// a fixed compile-time list either way, no dynamic registration needed at
// this project's scale.
static bool s_driver_ready[sizeof(s_drivers) / sizeof(s_drivers[0])];
void sensor_registry_init_all(void) {
for (size_t i = 0; i < s_driver_count; i++) {
const sensor_driver_t *drv = &s_drivers[i];
if (drv->init == NULL) {
s_driver_ready[i] = true;
continue;
}
esp_err_t err = drv->init();
if (err == ESP_OK) {
s_driver_ready[i] = true;
ESP_LOGI(TAG, "driver '%s' initialized", drv->name);
} else {
s_driver_ready[i] = false;
ESP_LOGW(TAG, "driver '%s' failed to initialize (%s) -- it will be skipped",
drv->name, esp_err_to_name(err));
}
}
}
esp_err_t sensor_registry_collect(sensor_reading_t *out, size_t max_out, size_t *out_count) {
size_t total = 0;
for (size_t i = 0; i < s_driver_count && total < max_out; i++) {
if (!s_driver_ready[i]) {
continue;
}
const sensor_driver_t *drv = &s_drivers[i];
size_t produced = 0;
esp_err_t err = drv->read(&out[total], max_out - total, &produced);
if (err != ESP_OK) {
ESP_LOGW(TAG, "driver '%s' read failed (%s) -- skipping this cycle",
drv->name, esp_err_to_name(err));
continue;
}
total += produced;
}
*out_count = total;
return ESP_OK;
}