Void Sentry
An AI-driven, real-time Sentry Turret platform leveraging asynchronous I/O and computer vision to deliver high-precision autonomous motion tracking on resource-constrained embedded hardware.
Loading...
Searching...
No Matches
frame_store.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <array>
11#include <atomic>
12#include <cstddef>
13#include <cstdint>
14#include <optional>
15
16#include <freertos/FreeRTOS.h>
17#include <freertos/semphr.h>
18#include <freertos/task.h>
19
20#include "frame_slots.h"
21
23 uint32_t published = 0;
24 uint32_t dropped_no_slot = 0;
25 uint32_t dropped_oversize = 0;
26 uint32_t dropped_corrupt = 0;
27};
28
29class FrameStore final : public frames::SlotReleaser
30{
31public:
32 static constexpr size_t kSlotBytes = 32 * 1024;
33
35 bool begin();
36
37 // Writer side (camera task only)
38 std::optional<size_t> begin_write();
39 uint8_t* write_buffer(size_t slot) { return buffers_[slot]; }
40 void publish(size_t slot, const frames::FrameMeta& meta);
41 void abort_write(size_t slot, frames::AbortReason reason);
42
43 // Reader side (any task)
44 std::optional<frames::FrameLease> acquire_latest();
45 const uint8_t* data(const frames::FrameLease& lease) const { return buffers_[lease.slot()]; }
46
48 static constexpr size_t kMaxSubscribers = 2;
49
55 bool add_subscriber(TaskHandle_t task);
56
58 void remove_subscriber(TaskHandle_t task);
59
66 TaskHandle_t start_subscriber_task(TaskFunction_t entry, const char* name, uint32_t stack_bytes,
67 UBaseType_t priority, BaseType_t core, void* arg);
68
69 FrameStoreStats stats();
70
72 bool ready() const { return ready_; }
73
74 void release_slot(size_t slot) override;
75
76private:
77 frames::FrameSlots slots_;
78 std::array<uint8_t*, frames::kSlotCount> buffers_{};
79 SemaphoreHandle_t mutex_ = nullptr;
80 std::array<std::atomic<TaskHandle_t>, kMaxSubscribers> subscribers_{};
81 bool ready_ = false;
82};
Definition frame_store.h:30
static constexpr size_t kMaxSubscribers
Maximum number of tasks woken on each publish (stream task + detection task).
Definition frame_store.h:48
void remove_subscriber(TaskHandle_t task)
Unregister a task. Unknown or null handles are ignored. Remove before deleting the task.
Definition frame_store.cpp:101
bool ready() const
True only after a fully successful begin(). Every other call is a safe no-op before that.
Definition frame_store.h:72
bool add_subscriber(TaskHandle_t task)
Register a task to wake (xTaskNotifyGive) on every publish.
Definition frame_store.cpp:77
TaskHandle_t start_subscriber_task(TaskFunction_t entry, const char *name, uint32_t stack_bytes, UBaseType_t priority, BaseType_t core, void *arg)
Creates a task pinned to core and registers it as a subscriber, so it is woken on every publish.
Definition frame_store.cpp:144
bool begin()
Create the mutex and allocate all slot buffers in PSRAM. False if any allocation fails.
Definition frame_store.cpp:11
Move-only handle on a published frame. Releases its slot exactly once, on release() or destruction.
Definition frame_slots.h:47
Definition frame_slots.h:93
Whatever a FrameLease must notify when it is released (FrameSlots natively, FrameStore on device).
Definition frame_slots.h:37
Slot bookkeeping for camera frames: who may write which slot, and RAII leases for readers.
Definition frame_store.h:22
Definition frame_slots.h:26