Files

53 lines
1.9 KiB
C

// frame_ring.h — pre-trigger history in PSRAM.
//
// THE POINT OF THIS FILE
// SPEC C4 says the first usable frame lands 1-3 s after the bird arrives, by
// which time a small bird is gone. That is only true if capture *starts* at the
// trigger. With mains power we can instead capture continuously into a ring and,
// on a trigger, ship the frames from *before* it. Effective latency goes
// negative and C4 stops being a problem.
//
// Cost: ring_frames * ~45 KB of the 8 MB PSRAM. At depth 24 that is ~1 MB for
// roughly 10 s of history at the default 400 ms cadence.
//
// Ownership: the ring owns its buffers and recycles them in place. Anything
// handed to the uploader is a *copy*, so a slow network cannot stall capture
// and there is no refcounting to get wrong.
#pragma once
#include <Arduino.h>
struct RingFrame {
uint8_t* data; // PSRAM, owned by the ring
size_t len;
size_t cap; // allocated size, >= len
uint32_t seq; // monotonic, 0 = never filled
uint32_t at_ms; // millis() at capture
int64_t at_epoch; // wall-clock ms, 0 if time not yet synced
};
// A frame copied out for upload. Caller owns `data` and must free() it.
struct FrameCopy {
uint8_t* data;
size_t len;
uint32_t seq;
uint32_t at_ms;
int64_t at_epoch;
};
bool ringBegin(uint8_t depth);
void ringEnd();
// Store a frame. Copies into the ring's own PSRAM buffer, growing it if the
// JPEG is larger than the slot currently holds.
bool ringPush(const uint8_t* data, size_t len, int64_t epoch_ms);
// Copy out the newest `n` frames, oldest first. Returns how many were written
// into `out` (may be fewer than n if the ring has not filled yet).
// Every returned FrameCopy.data must be free()d by the caller.
size_t ringTakeNewest(size_t n, FrameCopy* out, size_t out_cap);
uint32_t ringSeq(); // sequence of the most recent frame
size_t ringCount(); // frames currently held
size_t ringBytesUsed();