Files
DoorCounter/firmware/lib/cv/cv.h
Peter Woolery 21f3bc77d1 feat(reporter): apply server-pushed CV tuning from heartbeat response
Heartbeat POST now captures the response body (up to 2048 bytes) and
looks for a "config" object. If cfg_version advances past the stored
value and all tunable fields pass range validation, the new tuning is
applied to g_cv and persisted to NVS.

- cv_tuning_validate: pure range checker (cv.cpp)
- cv_apply_tuning / cv_get_tuning: mutex-guarded helpers in main.cpp
  exposed via cv_apply.h; 500 ms timeout, drop on contention
- post_json now returns int (HTTP status) and optionally captures the
  response body; existing callers check == 200
- heartbeat: parse → cfg_version check → override present fields →
  validate → apply → save. Silent no-op when server returns no config.
- 3 new native tests (15/15 pass). timercam flash 1,423,897 bytes
  (+9,828 vs baseline).
2026-04-16 17:34:34 -07:00

50 lines
1.3 KiB
C++

// firmware/lib/cv/cv.h
#pragma once
#include <stdint.h>
#include <vector>
static const int CV_W = 96;
static const int CV_H = 96;
static const int CV_PIXELS = CV_W * CV_H;
struct CVTrack {
int id;
float x, y;
bool above_line;
int missed;
};
struct CVTuning {
uint8_t diff_thresh; // per-pixel motion threshold
int min_blob_px; // min foreground pixels for a blob
float max_move; // max inter-frame track jump (px)
int max_missed; // frames before drop
uint8_t line_offset; // 0-100, percent of frame height for virtual line
uint32_t cfg_version; // monotonic; server increments on push
};
struct CVState {
uint8_t background[CV_PIXELS];
bool bg_valid;
uint32_t last_motion_frame;
uint32_t frame_index;
int next_id;
std::vector<CVTrack> tracks;
int entries;
int exits;
CVTuning tuning;
};
struct CVResult {
int entries_delta;
int exits_delta;
};
void cv_init(CVState& state);
CVResult cv_process(CVState& state, const uint8_t* frame);
void cv_reset_counts(CVState& state);
// Pure validator: returns true iff all tunable fields are in range and
// cfg_version is non-zero. No Arduino deps — safe for native tests.
bool cv_tuning_validate(const CVTuning& t);