@geastack/targets 0.1.110 → 0.1.111
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/targets/esp32/chip_bindings/audio/ns4168.cpp +2 -2
- package/targets/esp32/chip_bindings/displays/rgb_panel.cpp +323 -4
- package/targets/esp32/chip_bindings/expanders/elecrow_stc8h1k17.cpp +155 -0
- package/targets/esp32/chip_bindings/expanders/elecrow_stc8h1k17.h +20 -0
- package/targets/esp32/chip_bindings/power/elecrow_stc8h1k17.cpp +35 -0
- package/targets/esp32/services/app_state.cpp +20 -2
- package/targets/esp32/services/device_control.cpp +59 -23
- package/targets/esp32-p4/CMakeLists.txt +18 -0
- package/targets/esp32-p4/main/CMakeLists.txt +564 -0
- package/targets/esp32-p4/main/Kconfig.projbuild +14 -0
- package/targets/esp32-p4/main/app_main.cpp +177 -0
- package/targets/esp32-p4/main/camera.cpp +1958 -0
- package/targets/esp32-p4/main/fatfs_ffconf.h +383 -0
- package/targets/esp32-p4/main/i2c.cpp +39 -0
- package/targets/esp32-p4/main/idf_component.yml +26 -0
- package/targets/esp32-p4/main/launcher_button.cpp +192 -0
- package/targets/esp32-p4/main/sdcard_mount.cpp +132 -0
- package/targets/esp32-p4/partitions.csv +8 -0
- package/targets/esp32-p4/sdkconfig.defaults +110 -0
- package/targets/esp32-p4-elecrow-crowpanel-advanced-5.0.json +161 -0
- package/targets.json +19 -0
|
@@ -0,0 +1,1958 @@
|
|
|
1
|
+
#include "camera.h"
|
|
2
|
+
|
|
3
|
+
#include "canvas.h"
|
|
4
|
+
#include "display.h"
|
|
5
|
+
#include "i2c.h"
|
|
6
|
+
#include "board.h"
|
|
7
|
+
#include "chip_bindings/expanders/io_expander.h"
|
|
8
|
+
#include "image.h"
|
|
9
|
+
#include "pixel.h"
|
|
10
|
+
|
|
11
|
+
#include <algorithm>
|
|
12
|
+
#include <array>
|
|
13
|
+
#include <cerrno>
|
|
14
|
+
#include <cmath>
|
|
15
|
+
#include <cstdint>
|
|
16
|
+
#include <cstdlib>
|
|
17
|
+
#include <cstring>
|
|
18
|
+
#include <inttypes.h>
|
|
19
|
+
#include <string>
|
|
20
|
+
|
|
21
|
+
#include <fcntl.h>
|
|
22
|
+
#include <sys/ioctl.h>
|
|
23
|
+
#include <sys/mman.h>
|
|
24
|
+
#include <unistd.h>
|
|
25
|
+
|
|
26
|
+
#include "driver/i2c_master.h"
|
|
27
|
+
#include "driver/ppa.h"
|
|
28
|
+
#include "driver/sdmmc_host.h"
|
|
29
|
+
#include "esp_cache.h"
|
|
30
|
+
#include "esp_heap_caps.h"
|
|
31
|
+
#include "esp_log.h"
|
|
32
|
+
#include "esp_timer.h"
|
|
33
|
+
#include "esp_vfs_fat.h"
|
|
34
|
+
#include "esp_video_init.h"
|
|
35
|
+
#include "esp_video_device.h" // ESP_VIDEO_H264_DEVICE_NAME (/dev/video11 HW encoder)
|
|
36
|
+
#include "esp_video_isp_ioctl.h"
|
|
37
|
+
#include "linux/videodev2.h"
|
|
38
|
+
#include "sd_pwr_ctrl_by_on_chip_ldo.h" // ESP32-P4 SD power is via the on-chip LDO
|
|
39
|
+
#include "sdmmc_cmd.h"
|
|
40
|
+
|
|
41
|
+
#include "freertos/FreeRTOS.h"
|
|
42
|
+
#include "freertos/semphr.h" // capture/preview serialization (opLock_)
|
|
43
|
+
#include "freertos/task.h"
|
|
44
|
+
|
|
45
|
+
#include <cstdio>
|
|
46
|
+
|
|
47
|
+
// Hardware Motion-JPEG recording uses the ESP32-P4 JPEG encoder. Guarded so the
|
|
48
|
+
// build stays green if the esp_driver_jpeg component isn't present; recording
|
|
49
|
+
// then degrades to a no-op (startRecording returns false).
|
|
50
|
+
#if __has_include("driver/jpeg_encode.h")
|
|
51
|
+
#include "driver/jpeg_encode.h"
|
|
52
|
+
#define GEA_P4_HAS_JPEG_ENCODER 1
|
|
53
|
+
#else
|
|
54
|
+
#define GEA_P4_HAS_JPEG_ENCODER 0
|
|
55
|
+
#endif
|
|
56
|
+
|
|
57
|
+
namespace pixel = gea::framework::graphics::pixel;
|
|
58
|
+
|
|
59
|
+
namespace gea::platform::esp32_p4_waveshare::camera {
|
|
60
|
+
|
|
61
|
+
namespace {
|
|
62
|
+
|
|
63
|
+
constexpr char kTag[] = "p4_camera";
|
|
64
|
+
constexpr char kDevicePath[] = "/dev/video0";
|
|
65
|
+
// Preview defaults: the panel's size.
|
|
66
|
+
constexpr int kDefaultWidth = GEA_EMBEDDED_DISPLAY_WIDTH;
|
|
67
|
+
constexpr int kDefaultHeight = GEA_EMBEDDED_DISPLAY_HEIGHT;
|
|
68
|
+
constexpr int kBufferCount = 3;
|
|
69
|
+
constexpr int kSccbFrequencyHz = 400000;
|
|
70
|
+
// How the sensor is mounted relative to the panel, in quarter turns
|
|
71
|
+
// counter-clockwise (the board definition's camera.rotation).
|
|
72
|
+
#if GEA_BOARD_HAS_CAMERA
|
|
73
|
+
constexpr int kPreviewRotateCcw = gea::platform::board::camera.rotateCcw;
|
|
74
|
+
#else
|
|
75
|
+
constexpr int kPreviewRotateCcw = 0;
|
|
76
|
+
#endif
|
|
77
|
+
// Default auto-exposure target brightness the ISP pipeline starts from;
|
|
78
|
+
// setExposure() shifts it to brighten/darken the auto-exposed preview.
|
|
79
|
+
constexpr int kAeTargetDefault = 0x50;
|
|
80
|
+
|
|
81
|
+
struct MappedBuffer {
|
|
82
|
+
std::uint8_t *data = nullptr;
|
|
83
|
+
std::size_t length = 0;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
struct CapturedFrame {
|
|
87
|
+
const std::uint8_t *data = nullptr;
|
|
88
|
+
std::size_t bytes = 0;
|
|
89
|
+
int width = 0;
|
|
90
|
+
int height = 0;
|
|
91
|
+
std::uint32_t format = 0;
|
|
92
|
+
bool mirror = false;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
struct PreviewRaster {
|
|
96
|
+
CapturedFrame *frame = nullptr;
|
|
97
|
+
int destX = 0;
|
|
98
|
+
int destY = 0;
|
|
99
|
+
int destW = 0;
|
|
100
|
+
int destH = 0;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
int clampByte(int value)
|
|
104
|
+
{
|
|
105
|
+
return std::min(255, std::max(0, value));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
std::uint16_t yuvToRgb565(int y, int u, int v)
|
|
109
|
+
{
|
|
110
|
+
const int c = y - 16;
|
|
111
|
+
const int d = u - 128;
|
|
112
|
+
const int e = v - 128;
|
|
113
|
+
const int r = clampByte((298 * c + 409 * e + 128) >> 8);
|
|
114
|
+
const int g = clampByte((298 * c - 100 * d - 208 * e + 128) >> 8);
|
|
115
|
+
const int b = clampByte((298 * c + 516 * d + 128) >> 8);
|
|
116
|
+
return pixel::fromRgb888(r, g, b);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
std::uint16_t framePixel(const CapturedFrame &frame, int x, int y)
|
|
120
|
+
{
|
|
121
|
+
if (!frame.data || frame.width <= 0 || frame.height <= 0) return 0;
|
|
122
|
+
x = std::min(frame.width - 1, std::max(0, x));
|
|
123
|
+
y = std::min(frame.height - 1, std::max(0, y));
|
|
124
|
+
if (frame.mirror) x = frame.width - 1 - x;
|
|
125
|
+
|
|
126
|
+
switch (frame.format) {
|
|
127
|
+
case V4L2_PIX_FMT_RGB565: {
|
|
128
|
+
const std::size_t offset = static_cast<std::size_t>(y * frame.width + x) * 2;
|
|
129
|
+
if (offset + 1 >= frame.bytes) return 0;
|
|
130
|
+
const std::uint16_t rgb565 = static_cast<std::uint16_t>(frame.data[offset] | (frame.data[offset + 1] << 8));
|
|
131
|
+
return pixel::fromRgb565(rgb565);
|
|
132
|
+
}
|
|
133
|
+
case V4L2_PIX_FMT_RGB24: {
|
|
134
|
+
const std::size_t offset = static_cast<std::size_t>(y * frame.width + x) * 3;
|
|
135
|
+
if (offset + 2 >= frame.bytes) return 0;
|
|
136
|
+
return pixel::fromRgb888(frame.data[offset], frame.data[offset + 1], frame.data[offset + 2]);
|
|
137
|
+
}
|
|
138
|
+
case V4L2_PIX_FMT_YUYV:
|
|
139
|
+
case V4L2_PIX_FMT_YUV422P: {
|
|
140
|
+
const int pairX = x & ~1;
|
|
141
|
+
const std::size_t offset = static_cast<std::size_t>(y * frame.width + pairX) * 2;
|
|
142
|
+
if (offset + 3 >= frame.bytes) return 0;
|
|
143
|
+
const int y0 = frame.data[offset];
|
|
144
|
+
const int u = frame.data[offset + 1];
|
|
145
|
+
const int y1 = frame.data[offset + 2];
|
|
146
|
+
const int v = frame.data[offset + 3];
|
|
147
|
+
return yuvToRgb565((x & 1) ? y1 : y0, u, v);
|
|
148
|
+
}
|
|
149
|
+
case V4L2_PIX_FMT_GREY:
|
|
150
|
+
case V4L2_PIX_FMT_SBGGR8: {
|
|
151
|
+
const std::size_t offset = static_cast<std::size_t>(y * frame.width + x);
|
|
152
|
+
if (offset >= frame.bytes) return 0;
|
|
153
|
+
const int v = frame.data[offset];
|
|
154
|
+
return pixel::fromRgb888(v, v, v);
|
|
155
|
+
}
|
|
156
|
+
default:
|
|
157
|
+
return 0;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
void rasterPreview(std::uint16_t *pixels, int width, int height, int originX, int originY, void *user)
|
|
162
|
+
{
|
|
163
|
+
auto *preview = static_cast<PreviewRaster *>(user);
|
|
164
|
+
if (!preview || !preview->frame) return;
|
|
165
|
+
CapturedFrame &frame = *preview->frame;
|
|
166
|
+
const int previewW = std::max(1, preview->destW);
|
|
167
|
+
const int previewH = std::max(1, preview->destH);
|
|
168
|
+
for (int y = 0; y < height; ++y) {
|
|
169
|
+
const int srcY = ((originY + y - preview->destY) * frame.height) / previewH;
|
|
170
|
+
for (int x = 0; x < width; ++x) {
|
|
171
|
+
const int srcX = ((originX + x - preview->destX) * frame.width) / previewW;
|
|
172
|
+
pixels[y * width + x] = framePixel(frame, srcX, srcY);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
} // namespace
|
|
178
|
+
|
|
179
|
+
class CameraDriver {
|
|
180
|
+
public:
|
|
181
|
+
static CameraDriver &instance()
|
|
182
|
+
{
|
|
183
|
+
static CameraDriver driver;
|
|
184
|
+
return driver;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
bool isAvailable() const { return GEA_BOARD_HAS_CAMERA != 0; }
|
|
188
|
+
bool hasPermission() const { return true; }
|
|
189
|
+
bool requestPermission() const { return true; }
|
|
190
|
+
|
|
191
|
+
bool open(const std::string &facingHint, int preferredWidth, int preferredHeight)
|
|
192
|
+
{
|
|
193
|
+
ensureOpLock();
|
|
194
|
+
close();
|
|
195
|
+
if (!initializeVideo()) return false;
|
|
196
|
+
fd_ = ::open(kDevicePath, O_RDWR);
|
|
197
|
+
if (fd_ < 0) {
|
|
198
|
+
ESP_LOGE(kTag, "open %s failed: errno=%d", kDevicePath, errno);
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (facingHint == "back") facing_ = gea::platform::camera::Facing::Back;
|
|
203
|
+
else if (facingHint == "external") facing_ = gea::platform::camera::Facing::External;
|
|
204
|
+
else facing_ = gea::platform::camera::Facing::Front;
|
|
205
|
+
mirror_ = facing_ == gea::platform::camera::Facing::Front;
|
|
206
|
+
|
|
207
|
+
if (!queryAndConfigureFormat(preferredWidth, preferredHeight)) {
|
|
208
|
+
close();
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
configureFlipControls();
|
|
212
|
+
if (!mapBuffers(kBufferCount)) {
|
|
213
|
+
close();
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
if (!setStreaming(true)) {
|
|
217
|
+
close();
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
open_ = true;
|
|
221
|
+
ESP_LOGI(kTag, "camera ready %dx%d format=%c%c%c%c facing=%s",
|
|
222
|
+
width_,
|
|
223
|
+
height_,
|
|
224
|
+
static_cast<char>(format_ & 0xff),
|
|
225
|
+
static_cast<char>((format_ >> 8) & 0xff),
|
|
226
|
+
static_cast<char>((format_ >> 16) & 0xff),
|
|
227
|
+
static_cast<char>((format_ >> 24) & 0xff),
|
|
228
|
+
currentFacingString().c_str());
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
void close()
|
|
233
|
+
{
|
|
234
|
+
if (fd_ >= 0) {
|
|
235
|
+
if (streaming_) setStreaming(false);
|
|
236
|
+
for (auto &buffer : buffers_) {
|
|
237
|
+
if (buffer.data && buffer.length > 0) {
|
|
238
|
+
munmap(buffer.data, buffer.length);
|
|
239
|
+
}
|
|
240
|
+
buffer = {};
|
|
241
|
+
}
|
|
242
|
+
::close(fd_);
|
|
243
|
+
}
|
|
244
|
+
fd_ = -1;
|
|
245
|
+
open_ = false;
|
|
246
|
+
streaming_ = false;
|
|
247
|
+
bufferCount_ = 0;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
bool isOpen() const { return open_; }
|
|
251
|
+
int width() const { return width_; }
|
|
252
|
+
int height() const { return height_; }
|
|
253
|
+
int orientation() const { return 0; }
|
|
254
|
+
gea::platform::camera::Facing currentFacing() const { return facing_; }
|
|
255
|
+
|
|
256
|
+
std::string currentFacingString() const
|
|
257
|
+
{
|
|
258
|
+
switch (facing_) {
|
|
259
|
+
case gea::platform::camera::Facing::Back:
|
|
260
|
+
return "back";
|
|
261
|
+
case gea::platform::camera::Facing::External:
|
|
262
|
+
return "external";
|
|
263
|
+
case gea::platform::camera::Facing::Front:
|
|
264
|
+
default:
|
|
265
|
+
return "front";
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
int deviceCount() const { return GEA_BOARD_HAS_CAMERA ? 1 : 0; }
|
|
270
|
+
|
|
271
|
+
gea::platform::camera::DeviceInfo deviceAt(int index) const
|
|
272
|
+
{
|
|
273
|
+
#if GEA_BOARD_HAS_CAMERA
|
|
274
|
+
if (index != 0) return {};
|
|
275
|
+
return {gea::platform::board::camera.name, gea::platform::camera::Facing::Front, kDefaultWidth, kDefaultHeight};
|
|
276
|
+
#else
|
|
277
|
+
(void)index;
|
|
278
|
+
return {};
|
|
279
|
+
#endif
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
void drawPreview(int x, int y, int destW, int destH)
|
|
283
|
+
{
|
|
284
|
+
if (!open_) return;
|
|
285
|
+
if (destW <= 0) destW = width_;
|
|
286
|
+
if (destH <= 0) destH = height_;
|
|
287
|
+
withFrame([&](CapturedFrame &frame) {
|
|
288
|
+
PreviewRaster preview{.frame = &frame, .destX = x, .destY = y, .destW = destW, .destH = destH};
|
|
289
|
+
gea::platform::display::Display::streamRect(x, y, destW, destH, rasterPreview, &preview);
|
|
290
|
+
recordFrameIfDue(frame);
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// NativeOverlay: the camera owns the viewfinder rect and PPA-scales each frame
|
|
295
|
+
// straight into the display framebuffer (presentNativeOverlay), so the renderer
|
|
296
|
+
// never blits/replays/flushes the camera region.
|
|
297
|
+
int previewMode() const { return 1; }
|
|
298
|
+
void positionPreviewLayer(int x, int y, int width, int height)
|
|
299
|
+
{
|
|
300
|
+
vfX_ = x;
|
|
301
|
+
vfY_ = y;
|
|
302
|
+
vfW_ = width;
|
|
303
|
+
vfH_ = height;
|
|
304
|
+
}
|
|
305
|
+
void hidePreviewLayer()
|
|
306
|
+
{
|
|
307
|
+
vfW_ = 0;
|
|
308
|
+
vfH_ = 0;
|
|
309
|
+
}
|
|
310
|
+
void presentNativeOverlay()
|
|
311
|
+
{
|
|
312
|
+
if (open_ && vfW_ > 0 && vfH_ > 0) presentDirect(vfX_, vfY_, vfW_, vfH_);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// Scale + convert the latest frame into the caller's RGB565 buffer using the
|
|
316
|
+
// requested fit (0=cover, 1=contain, 2=fill). Drives the <camera> leaf.
|
|
317
|
+
bool fillPreview(std::uint16_t *dst, int dstW, int dstH, int fit, bool mirror)
|
|
318
|
+
{
|
|
319
|
+
if (!open_ || !dst || dstW <= 0 || dstH <= 0) return false;
|
|
320
|
+
bool wrote = false;
|
|
321
|
+
const std::int64_t t0 = esp_timer_get_time();
|
|
322
|
+
std::int64_t tAfterDq = 0;
|
|
323
|
+
withFrame([&](CapturedFrame &frame) {
|
|
324
|
+
tAfterDq = esp_timer_get_time();
|
|
325
|
+
const bool oldMirror = frame.mirror;
|
|
326
|
+
frame.mirror = mirror || mirror_;
|
|
327
|
+
// HW scale+rotate via the P4 PPA; fall back to the CPU scaler if the
|
|
328
|
+
// PPA path can't handle this frame/format (or isn't available).
|
|
329
|
+
if (!fillScaledPpa(frame, dst, dstW, dstH, fit))
|
|
330
|
+
fillScaled(frame, dst, dstW, dstH, fit, kPreviewRotateCcw);
|
|
331
|
+
frame.mirror = oldMirror;
|
|
332
|
+
recordFrameIfDue(frame);
|
|
333
|
+
wrote = true;
|
|
334
|
+
});
|
|
335
|
+
if (wrote) {
|
|
336
|
+
const std::int64_t t2 = esp_timer_get_time();
|
|
337
|
+
static std::int64_t accDq = 0, accScale = 0;
|
|
338
|
+
static int n = 0;
|
|
339
|
+
accDq += tAfterDq - t0;
|
|
340
|
+
accScale += t2 - tAfterDq;
|
|
341
|
+
if (++n >= 30) {
|
|
342
|
+
ESP_LOGI(kTag, "preview perf: dqbuf=%lldus scale+qbuf=%lldus dst=%dx%d (avg/30)",
|
|
343
|
+
(long long)(accDq / 30), (long long)(accScale / 30), dstW, dstH);
|
|
344
|
+
accDq = accScale = 0;
|
|
345
|
+
n = 0;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
return wrote;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
int capture(bool mirror)
|
|
352
|
+
{
|
|
353
|
+
if (!open_) return -1;
|
|
354
|
+
int imageId = -1;
|
|
355
|
+
withFrame([&](CapturedFrame &frame) {
|
|
356
|
+
const int w = width_;
|
|
357
|
+
const int h = height_;
|
|
358
|
+
auto *pixels = static_cast<std::uint16_t *>(
|
|
359
|
+
heap_caps_malloc(static_cast<std::size_t>(w) * h * sizeof(std::uint16_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
|
|
360
|
+
if (!pixels) return;
|
|
361
|
+
const bool oldMirror = frame.mirror;
|
|
362
|
+
frame.mirror = mirror || mirror_;
|
|
363
|
+
for (int y = 0; y < h; ++y) {
|
|
364
|
+
for (int x = 0; x < w; ++x) {
|
|
365
|
+
pixels[y * w + x] = framePixel(frame, x, y);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
frame.mirror = oldMirror;
|
|
369
|
+
imageId = gea::framework::graphics::ImageStore::instance().registerBuffer(pixels, w, h, -1);
|
|
370
|
+
if (imageId < 0) heap_caps_free(pixels);
|
|
371
|
+
});
|
|
372
|
+
return imageId;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
void setFlash(const std::string &mode) { setTorch(mode, 0.0); }
|
|
376
|
+
|
|
377
|
+
void setZoom(double factor)
|
|
378
|
+
{
|
|
379
|
+
// The SC2336 has no optical/sensor zoom, so this is a digital zoom applied
|
|
380
|
+
// in the preview scaler (ppaScaleRotateInto crops + up-scales by this factor).
|
|
381
|
+
zoom_ = factor > 1.0 ? factor : 1.0;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
void setMirror(bool mirror)
|
|
385
|
+
{
|
|
386
|
+
mirror_ = mirror;
|
|
387
|
+
configureFlipControls();
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// The SC2336/ISP pipeline only exposes the auto-exposure *target* brightness
|
|
391
|
+
// (ESP_CAM_SENSOR_EXPOSURE_VAL, 2..235) — the AE loop then drives the sensor to
|
|
392
|
+
// hit it. Map `bias` (EV-ish steps) around the default target so the button
|
|
393
|
+
// visibly brightens/darkens the live image while staying auto-exposed.
|
|
394
|
+
void setExposure(const std::string &mode, double bias, double iso, double durationMs)
|
|
395
|
+
{
|
|
396
|
+
(void)mode;
|
|
397
|
+
(void)iso;
|
|
398
|
+
(void)durationMs;
|
|
399
|
+
int target = kAeTargetDefault + static_cast<int>(bias * 45.0);
|
|
400
|
+
if (target < 16) target = 16;
|
|
401
|
+
if (target > 230) target = 230;
|
|
402
|
+
setExtControl(V4L2_CID_EXPOSURE, target);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// White balance lives in the ISP (V4L2_CID_USER_ESP_ISP_WB). enable=false hands
|
|
406
|
+
// it back to the AWB algorithm; enable=true applies fixed red/blue gains so a
|
|
407
|
+
// low colour temperature looks warm (boost red) and a high one looks cool
|
|
408
|
+
// (boost blue).
|
|
409
|
+
void setWhiteBalance(const std::string &mode, double temperatureK, double tint)
|
|
410
|
+
{
|
|
411
|
+
(void)tint;
|
|
412
|
+
if (mode == "auto" || mode == "continuous") {
|
|
413
|
+
setIspWb(false, 1.0f, 1.0f); // resume AWB
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
float redGain = 1.6f, blueGain = 1.6f; // neutral manual
|
|
417
|
+
if (temperatureK > 0.0 && temperatureK <= 4500.0) {
|
|
418
|
+
redGain = 2.1f;
|
|
419
|
+
blueGain = 1.25f; // warm
|
|
420
|
+
} else if (temperatureK >= 6500.0) {
|
|
421
|
+
redGain = 1.25f;
|
|
422
|
+
blueGain = 2.1f; // cool
|
|
423
|
+
}
|
|
424
|
+
setIspWb(true, redGain, blueGain);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// mode: "auto" | "continuous" | "locked"
|
|
428
|
+
void setFocus(const std::string &mode, double, double)
|
|
429
|
+
{
|
|
430
|
+
setControl(V4L2_CID_FOCUS_AUTO, mode != "locked" ? 1 : 0);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// The SC2336 module on this board has no controllable LED, so torch is a
|
|
434
|
+
// no-op here; the hook exists so the unified API stays uniform.
|
|
435
|
+
void setTorch(const std::string &, double) {}
|
|
436
|
+
|
|
437
|
+
bool startRecording(const std::string &path, double fps);
|
|
438
|
+
double stopRecording();
|
|
439
|
+
bool isRecording() const { return recording_; }
|
|
440
|
+
|
|
441
|
+
// ---- Full-resolution capture-to-buffer (driven by the GEADEV dev commands) ----
|
|
442
|
+
// These take over the camera for their full duration (preview frames are
|
|
443
|
+
// skipped via opLock_) and hand back a heap_caps buffer the caller must free
|
|
444
|
+
// with heap_caps_free(). Both run at the sensor's native size (1920x1080 in the
|
|
445
|
+
// configured mode).
|
|
446
|
+
|
|
447
|
+
// Grab one frame and HW-JPEG encode it at full resolution (quality 90). The
|
|
448
|
+
// live preview already streams RGB565, which is a valid JPEG-encoder input, so
|
|
449
|
+
// no format switch is needed for the still.
|
|
450
|
+
bool captureStillJpeg(std::uint8_t **outBuf, std::size_t *outLen, int *outW, int *outH)
|
|
451
|
+
{
|
|
452
|
+
#if GEA_P4_HAS_JPEG_ENCODER
|
|
453
|
+
if (!open_ || !outBuf || !outLen) return false;
|
|
454
|
+
ensureOpLock();
|
|
455
|
+
xSemaphoreTake(opLock_, portMAX_DELAY);
|
|
456
|
+
bool ok = false;
|
|
457
|
+
// JPEG HW encoder input must be RGB565 here; restore it if a prior clip left
|
|
458
|
+
// the device in YUV420 (recordH264Clip already restores, but be defensive).
|
|
459
|
+
if (format_ != V4L2_PIX_FMT_RGB565) reconfigureFormat(V4L2_PIX_FMT_RGB565, kBufferCount);
|
|
460
|
+
|
|
461
|
+
jpeg_encoder_handle_t enc = nullptr;
|
|
462
|
+
jpeg_encode_engine_cfg_t engineCfg = {};
|
|
463
|
+
engineCfg.timeout_ms = 2000;
|
|
464
|
+
if (format_ == V4L2_PIX_FMT_RGB565 && jpeg_new_encoder_engine(&engineCfg, &enc) == ESP_OK) {
|
|
465
|
+
withFrameLocked([&](CapturedFrame &frame) {
|
|
466
|
+
if (frame.format != V4L2_PIX_FMT_RGB565) return;
|
|
467
|
+
const std::size_t cap = static_cast<std::size_t>(frame.width) * frame.height * 2u;
|
|
468
|
+
jpeg_encode_memory_alloc_cfg_t memCfg = {};
|
|
469
|
+
memCfg.buffer_direction = JPEG_ENC_ALLOC_OUTPUT_BUFFER;
|
|
470
|
+
std::size_t allocated = 0;
|
|
471
|
+
auto *jbuf = static_cast<std::uint8_t *>(jpeg_alloc_encoder_mem(cap, &memCfg, &allocated));
|
|
472
|
+
if (!jbuf) return;
|
|
473
|
+
jpeg_encode_cfg_t jc = {};
|
|
474
|
+
jc.height = static_cast<std::uint32_t>(frame.height);
|
|
475
|
+
jc.width = static_cast<std::uint32_t>(frame.width);
|
|
476
|
+
jc.src_type = JPEG_ENCODE_IN_FORMAT_RGB565;
|
|
477
|
+
jc.sub_sample = JPEG_DOWN_SAMPLING_YUV420;
|
|
478
|
+
jc.image_quality = 90;
|
|
479
|
+
std::uint32_t jlen = 0;
|
|
480
|
+
const esp_err_t e = jpeg_encoder_process(enc, &jc, frame.data,
|
|
481
|
+
static_cast<std::uint32_t>(frame.bytes), jbuf,
|
|
482
|
+
static_cast<std::uint32_t>(allocated), &jlen);
|
|
483
|
+
if (e == ESP_OK && jlen > 0) {
|
|
484
|
+
*outBuf = jbuf;
|
|
485
|
+
*outLen = jlen;
|
|
486
|
+
if (outW) *outW = frame.width;
|
|
487
|
+
if (outH) *outH = frame.height;
|
|
488
|
+
ok = true;
|
|
489
|
+
} else {
|
|
490
|
+
heap_caps_free(jbuf);
|
|
491
|
+
}
|
|
492
|
+
});
|
|
493
|
+
jpeg_del_encoder_engine(enc);
|
|
494
|
+
}
|
|
495
|
+
xSemaphoreGive(opLock_);
|
|
496
|
+
return ok;
|
|
497
|
+
#else
|
|
498
|
+
(void)outBuf;
|
|
499
|
+
(void)outLen;
|
|
500
|
+
(void)outW;
|
|
501
|
+
(void)outH;
|
|
502
|
+
return false;
|
|
503
|
+
#endif
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// Record `seconds` of 1920x1080 H.264 via the P4 hardware encoder (/dev/video11
|
|
507
|
+
// V4L2 M2M). Switches /dev/video0 to YUV420 (the encoder's input format), runs
|
|
508
|
+
// the capture->encode loop at the sensor frame rate writing the Annex-B
|
|
509
|
+
// elementary stream straight to `path` on the SD card (no PSRAM size cap), then
|
|
510
|
+
// restores RGB565 so the preview resumes.
|
|
511
|
+
bool recordH264ClipToFile(double seconds, const char *path, std::size_t *outBytes, int *outW, int *outH,
|
|
512
|
+
double *outFps, int *outFrames)
|
|
513
|
+
{
|
|
514
|
+
if (!open_ || !path) return false;
|
|
515
|
+
ensureOpLock();
|
|
516
|
+
xSemaphoreTake(opLock_, portMAX_DELAY);
|
|
517
|
+
|
|
518
|
+
bool ok = false;
|
|
519
|
+
int m2m = -1;
|
|
520
|
+
std::uint8_t *m2mCap = nullptr;
|
|
521
|
+
std::size_t m2mCapLen = 0;
|
|
522
|
+
std::FILE *outFile = nullptr;
|
|
523
|
+
const int W = width_;
|
|
524
|
+
const int H = height_;
|
|
525
|
+
const std::size_t yuvLen = static_cast<std::size_t>(W) * H * 3u / 2u;
|
|
526
|
+
|
|
527
|
+
do {
|
|
528
|
+
// 0) Reserve the clip buffer FIRST, while PSRAM still has a large contiguous
|
|
529
|
+
// free region. Doing it after reconfigureFormat() + the encoder's ~8MB
|
|
530
|
+
// capture buffer fragments the heap and a 7MB contiguous alloc then fails.
|
|
531
|
+
// Allocated once and reused across captures.
|
|
532
|
+
const std::size_t accCap = 7u * 1024u * 1024u;
|
|
533
|
+
if (!clipBuf_) {
|
|
534
|
+
clipBuf_ = static_cast<std::uint8_t *>(heap_caps_malloc(accCap, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
|
|
535
|
+
clipCap_ = clipBuf_ ? accCap : 0;
|
|
536
|
+
}
|
|
537
|
+
if (!clipBuf_) {
|
|
538
|
+
ESP_LOGE(kTag, "h264: clip buffer alloc (%u) failed", static_cast<unsigned>(accCap));
|
|
539
|
+
break;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// 1) Camera output -> YUV420 (encoder input). 2 ring buffers keep PSRAM
|
|
543
|
+
// headroom for the encoder's large capture buffer (~W*H*4) + the clip.
|
|
544
|
+
if (!reconfigureFormat(V4L2_PIX_FMT_YUV420, 2)) {
|
|
545
|
+
ESP_LOGE(kTag, "h264: failed to switch sensor to YUV420");
|
|
546
|
+
break;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
// 2) Open + configure the HW H.264 M2M encoder device.
|
|
550
|
+
m2m = ::open(ESP_VIDEO_H264_DEVICE_NAME, O_RDWR);
|
|
551
|
+
if (m2m < 0) {
|
|
552
|
+
ESP_LOGE(kTag, "h264: open %s failed errno=%d", ESP_VIDEO_H264_DEVICE_NAME, errno);
|
|
553
|
+
break;
|
|
554
|
+
}
|
|
555
|
+
setH264Ctrl(m2m, V4L2_CID_MPEG_VIDEO_H264_I_PERIOD, 30);
|
|
556
|
+
setH264Ctrl(m2m, V4L2_CID_MPEG_VIDEO_BITRATE, 5000000); // 5 Mbps target (good 1080p)
|
|
557
|
+
// Writing to SD removes the old PSRAM size cap, so we can afford higher
|
|
558
|
+
// quality. min_qp=22 still bounds per-frame size enough that the HW rate
|
|
559
|
+
// controller (slow to adapt) can't balloon a detailed scene unboundedly.
|
|
560
|
+
setH264Ctrl(m2m, V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 22);
|
|
561
|
+
setH264Ctrl(m2m, V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 51);
|
|
562
|
+
|
|
563
|
+
v4l2_format ofmt{};
|
|
564
|
+
ofmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
|
|
565
|
+
ofmt.fmt.pix.width = static_cast<std::uint32_t>(W);
|
|
566
|
+
ofmt.fmt.pix.height = static_cast<std::uint32_t>(H);
|
|
567
|
+
ofmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
|
|
568
|
+
if (ioctl(m2m, VIDIOC_S_FMT, &ofmt) != 0) {
|
|
569
|
+
ESP_LOGE(kTag, "h264: S_FMT output failed errno=%d", errno);
|
|
570
|
+
break;
|
|
571
|
+
}
|
|
572
|
+
v4l2_requestbuffers oreq{};
|
|
573
|
+
oreq.count = 1;
|
|
574
|
+
oreq.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
|
|
575
|
+
oreq.memory = V4L2_MEMORY_USERPTR;
|
|
576
|
+
if (ioctl(m2m, VIDIOC_REQBUFS, &oreq) != 0) {
|
|
577
|
+
ESP_LOGE(kTag, "h264: REQBUFS output failed errno=%d", errno);
|
|
578
|
+
break;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
v4l2_format cfmt{};
|
|
582
|
+
cfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
583
|
+
cfmt.fmt.pix.width = static_cast<std::uint32_t>(W);
|
|
584
|
+
cfmt.fmt.pix.height = static_cast<std::uint32_t>(H);
|
|
585
|
+
cfmt.fmt.pix.pixelformat = V4L2_PIX_FMT_H264;
|
|
586
|
+
if (ioctl(m2m, VIDIOC_S_FMT, &cfmt) != 0) {
|
|
587
|
+
ESP_LOGE(kTag, "h264: S_FMT capture failed errno=%d", errno);
|
|
588
|
+
break;
|
|
589
|
+
}
|
|
590
|
+
v4l2_requestbuffers creq{};
|
|
591
|
+
creq.count = 1;
|
|
592
|
+
creq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
593
|
+
creq.memory = V4L2_MEMORY_MMAP;
|
|
594
|
+
if (ioctl(m2m, VIDIOC_REQBUFS, &creq) != 0) {
|
|
595
|
+
ESP_LOGE(kTag, "h264: REQBUFS capture failed errno=%d", errno);
|
|
596
|
+
break;
|
|
597
|
+
}
|
|
598
|
+
v4l2_buffer qcap{};
|
|
599
|
+
qcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
600
|
+
qcap.memory = V4L2_MEMORY_MMAP;
|
|
601
|
+
qcap.index = 0;
|
|
602
|
+
if (ioctl(m2m, VIDIOC_QUERYBUF, &qcap) != 0) {
|
|
603
|
+
ESP_LOGE(kTag, "h264: QUERYBUF failed errno=%d", errno);
|
|
604
|
+
break;
|
|
605
|
+
}
|
|
606
|
+
m2mCapLen = qcap.length;
|
|
607
|
+
m2mCap = static_cast<std::uint8_t *>(
|
|
608
|
+
mmap(nullptr, qcap.length, PROT_READ | PROT_WRITE, MAP_SHARED, m2m, qcap.m.offset));
|
|
609
|
+
if (m2mCap == reinterpret_cast<void *>(static_cast<intptr_t>(-1))) {
|
|
610
|
+
m2mCap = nullptr;
|
|
611
|
+
ESP_LOGE(kTag, "h264: mmap capture failed errno=%d", errno);
|
|
612
|
+
break;
|
|
613
|
+
}
|
|
614
|
+
if (ioctl(m2m, VIDIOC_QBUF, &qcap) != 0) {
|
|
615
|
+
ESP_LOGE(kTag, "h264: initial QBUF capture failed errno=%d", errno);
|
|
616
|
+
break;
|
|
617
|
+
}
|
|
618
|
+
int type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
619
|
+
if (ioctl(m2m, VIDIOC_STREAMON, &type) != 0) {
|
|
620
|
+
ESP_LOGE(kTag, "h264: STREAMON capture failed errno=%d", errno);
|
|
621
|
+
break;
|
|
622
|
+
}
|
|
623
|
+
type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
|
|
624
|
+
if (ioctl(m2m, VIDIOC_STREAMON, &type) != 0) {
|
|
625
|
+
ESP_LOGE(kTag, "h264: STREAMON output failed errno=%d", errno);
|
|
626
|
+
break;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
// Warm up: right after the YUV420 format switch + stream-on the ISP/sensor
|
|
630
|
+
// emits a handful of black/invalid frames. Drain them (no encode) so the
|
|
631
|
+
// clip opens on a real frame instead of black.
|
|
632
|
+
for (int i = 0; i < 10; ++i) {
|
|
633
|
+
v4l2_buffer wb{};
|
|
634
|
+
wb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
635
|
+
wb.memory = V4L2_MEMORY_MMAP;
|
|
636
|
+
if (ioctl(fd_, VIDIOC_DQBUF, &wb) == 0) ioctl(fd_, VIDIOC_QBUF, &wb);
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
// 3) Record into the PSRAM clip buffer (allocated in step 0), then bulk-write
|
|
640
|
+
// to SD after. A per-frame fwrite inside the encode loop stalls it and ~halves
|
|
641
|
+
// the frame rate, so we buffer in RAM (fast memcpy) and write the clip once.
|
|
642
|
+
std::uint8_t *acc = clipBuf_;
|
|
643
|
+
std::size_t accLen = 0;
|
|
644
|
+
int frames = 0;
|
|
645
|
+
const std::int64_t startUs = esp_timer_get_time();
|
|
646
|
+
const std::int64_t durUs = static_cast<std::int64_t>(seconds * 1e6);
|
|
647
|
+
const std::int64_t hardStopUs = startUs + durUs + 5 * 1000000; // safety
|
|
648
|
+
|
|
649
|
+
while (true) {
|
|
650
|
+
const std::int64_t now = esp_timer_get_time();
|
|
651
|
+
if (now - startUs >= durUs) break;
|
|
652
|
+
if (now >= hardStopUs) break;
|
|
653
|
+
|
|
654
|
+
v4l2_buffer capBuf{};
|
|
655
|
+
capBuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
656
|
+
capBuf.memory = V4L2_MEMORY_MMAP;
|
|
657
|
+
if (ioctl(fd_, VIDIOC_DQBUF, &capBuf) != 0) {
|
|
658
|
+
vTaskDelay(1);
|
|
659
|
+
continue;
|
|
660
|
+
}
|
|
661
|
+
const int idx = static_cast<int>(capBuf.index);
|
|
662
|
+
if (idx < 0 || idx >= bufferCount_ || !buffers_[idx].data) {
|
|
663
|
+
ioctl(fd_, VIDIOC_QBUF, &capBuf);
|
|
664
|
+
continue;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
// Feed the camera frame to the encoder (zero-copy via USERPTR). Use the
|
|
668
|
+
// full YUV420 size, not bytesused (the ISP reports the RAW10 input size).
|
|
669
|
+
v4l2_buffer mout{};
|
|
670
|
+
mout.index = 0;
|
|
671
|
+
mout.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
|
|
672
|
+
mout.memory = V4L2_MEMORY_USERPTR;
|
|
673
|
+
mout.m.userptr = reinterpret_cast<unsigned long>(buffers_[idx].data);
|
|
674
|
+
mout.length = static_cast<std::uint32_t>(yuvLen);
|
|
675
|
+
if (ioctl(m2m, VIDIOC_QBUF, &mout) != 0) {
|
|
676
|
+
ioctl(fd_, VIDIOC_QBUF, &capBuf);
|
|
677
|
+
continue;
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
v4l2_buffer mcap{};
|
|
681
|
+
mcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
682
|
+
mcap.memory = V4L2_MEMORY_MMAP;
|
|
683
|
+
if (ioctl(m2m, VIDIOC_DQBUF, &mcap) == 0) {
|
|
684
|
+
const std::size_t n = mcap.bytesused;
|
|
685
|
+
if (n > 0 && accLen + n <= clipCap_) {
|
|
686
|
+
std::memcpy(acc + accLen, m2mCap, n);
|
|
687
|
+
accLen += n;
|
|
688
|
+
++frames;
|
|
689
|
+
}
|
|
690
|
+
ioctl(m2m, VIDIOC_QBUF, &mcap); // requeue the encoder output buffer
|
|
691
|
+
}
|
|
692
|
+
ioctl(fd_, VIDIOC_QBUF, &capBuf); // requeue the camera frame
|
|
693
|
+
|
|
694
|
+
v4l2_buffer mout2{};
|
|
695
|
+
mout2.index = 0;
|
|
696
|
+
mout2.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
|
|
697
|
+
mout2.memory = V4L2_MEMORY_USERPTR;
|
|
698
|
+
ioctl(m2m, VIDIOC_DQBUF, &mout2); // reclaim the encoder input buffer
|
|
699
|
+
|
|
700
|
+
if (accLen + 256u * 1024u > clipCap_) break; // out of clip-buffer headroom
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
// fps is measured over the CAPTURE window only — not the SD write that
|
|
704
|
+
// follows — so the remuxed clip plays back at real time.
|
|
705
|
+
const double durS = (esp_timer_get_time() - startUs) / 1e6;
|
|
706
|
+
|
|
707
|
+
// Bulk-write the captured clip to the SD card (sequential, fast).
|
|
708
|
+
outFile = std::fopen(path, "wb");
|
|
709
|
+
if (!outFile) {
|
|
710
|
+
ESP_LOGE(kTag, "h264: fopen %s failed errno=%d", path, errno);
|
|
711
|
+
break;
|
|
712
|
+
}
|
|
713
|
+
const std::size_t wrote = std::fwrite(acc, 1, accLen, outFile);
|
|
714
|
+
std::fflush(outFile);
|
|
715
|
+
std::fclose(outFile);
|
|
716
|
+
outFile = nullptr;
|
|
717
|
+
if (wrote != accLen) {
|
|
718
|
+
ESP_LOGE(kTag, "h264: short SD write %u/%u", static_cast<unsigned>(wrote), static_cast<unsigned>(accLen));
|
|
719
|
+
accLen = wrote;
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
if (outBytes) *outBytes = accLen;
|
|
723
|
+
if (outW) *outW = W;
|
|
724
|
+
if (outH) *outH = H;
|
|
725
|
+
if (outFrames) *outFrames = frames;
|
|
726
|
+
if (outFps) *outFps = durS > 0.0 ? frames / durS : 0.0;
|
|
727
|
+
ok = accLen > 0;
|
|
728
|
+
} while (false);
|
|
729
|
+
|
|
730
|
+
if (outFile) std::fclose(outFile);
|
|
731
|
+
|
|
732
|
+
// Teardown the encoder.
|
|
733
|
+
if (m2m >= 0) {
|
|
734
|
+
int type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
735
|
+
ioctl(m2m, VIDIOC_STREAMOFF, &type);
|
|
736
|
+
type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
|
|
737
|
+
ioctl(m2m, VIDIOC_STREAMOFF, &type);
|
|
738
|
+
if (m2mCap) munmap(m2mCap, m2mCapLen);
|
|
739
|
+
::close(m2m);
|
|
740
|
+
}
|
|
741
|
+
// clipBuf_ is persistent and reused across captures — never freed here.
|
|
742
|
+
|
|
743
|
+
// Restore RGB565 so the live preview resumes.
|
|
744
|
+
reconfigureFormat(V4L2_PIX_FMT_RGB565, kBufferCount);
|
|
745
|
+
xSemaphoreGive(opLock_);
|
|
746
|
+
return ok;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
// Mount SD (if needed), capture one full-res JPEG, and write it to `path`.
|
|
750
|
+
bool captureStillToSd(const char *path, std::size_t *outBytes, int *outW, int *outH)
|
|
751
|
+
{
|
|
752
|
+
if (!ensureSdMounted() || !path) return false;
|
|
753
|
+
std::uint8_t *buf = nullptr;
|
|
754
|
+
std::size_t len = 0;
|
|
755
|
+
int w = 0, h = 0;
|
|
756
|
+
if (!captureStillJpeg(&buf, &len, &w, &h) || !buf) return false;
|
|
757
|
+
std::FILE *f = std::fopen(path, "wb");
|
|
758
|
+
bool ok = false;
|
|
759
|
+
if (f) {
|
|
760
|
+
ok = std::fwrite(buf, 1, len, f) == len;
|
|
761
|
+
std::fflush(f);
|
|
762
|
+
std::fclose(f);
|
|
763
|
+
} else {
|
|
764
|
+
ESP_LOGE(kTag, "still: fopen %s failed errno=%d", path, errno);
|
|
765
|
+
}
|
|
766
|
+
heap_caps_free(buf);
|
|
767
|
+
if (ok) {
|
|
768
|
+
if (outBytes) *outBytes = len;
|
|
769
|
+
if (outW) *outW = w;
|
|
770
|
+
if (outH) *outH = h;
|
|
771
|
+
}
|
|
772
|
+
return ok;
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
// Mount SD (if needed) and record an H.264 clip straight to `path`.
|
|
776
|
+
bool recordClipToSd(double seconds, const char *path, std::size_t *outBytes, int *outW, int *outH,
|
|
777
|
+
double *outFps, int *outFrames)
|
|
778
|
+
{
|
|
779
|
+
if (!ensureSdMounted()) return false;
|
|
780
|
+
return recordH264ClipToFile(seconds, path, outBytes, outW, outH, outFps, outFrames);
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
private:
|
|
784
|
+
// Preview / one-shot frame access from the render task. Serialized against the
|
|
785
|
+
// long-running capture ops (captureStillJpeg / recordH264Clip) via opLock_:
|
|
786
|
+
// while a capture holds the lock — it may reconfigure /dev/video0's pixel
|
|
787
|
+
// format (RGB565 <-> YUV420) — preview frames are skipped rather than racing a
|
|
788
|
+
// half-reconfigured device. A capture in progress => return false (the renderer
|
|
789
|
+
// just keeps the last frame on screen for the capture's duration).
|
|
790
|
+
template <typename Callback>
|
|
791
|
+
bool withFrame(Callback callback)
|
|
792
|
+
{
|
|
793
|
+
if (fd_ < 0 || !streaming_) return false;
|
|
794
|
+
if (opLock_ && xSemaphoreTake(opLock_, 0) != pdTRUE) return false;
|
|
795
|
+
const bool ok = withFrameLocked(callback);
|
|
796
|
+
if (opLock_) xSemaphoreGive(opLock_);
|
|
797
|
+
return ok;
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
// Dequeue one frame, invoke callback, requeue. Caller must already hold opLock_
|
|
801
|
+
// (or run before the preview task exists). Used directly by the capture ops.
|
|
802
|
+
template <typename Callback>
|
|
803
|
+
bool withFrameLocked(Callback callback)
|
|
804
|
+
{
|
|
805
|
+
if (fd_ < 0 || !streaming_) return false;
|
|
806
|
+
v4l2_buffer buffer{};
|
|
807
|
+
buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
808
|
+
buffer.memory = V4L2_MEMORY_MMAP;
|
|
809
|
+
const std::int64_t dqStartUs = esp_timer_get_time();
|
|
810
|
+
if (ioctl(fd_, VIDIOC_DQBUF, &buffer) != 0) {
|
|
811
|
+
ESP_LOGW(kTag, "DQBUF failed: errno=%d", errno);
|
|
812
|
+
return false;
|
|
813
|
+
}
|
|
814
|
+
const std::int64_t cbStartUs = esp_timer_get_time();
|
|
815
|
+
const int index = static_cast<int>(buffer.index);
|
|
816
|
+
if (index >= 0 && index < bufferCount_ && buffers_[index].data) {
|
|
817
|
+
// Use the whole mmap'd buffer, not VIDIOC_DQBUF's bytesused: on this
|
|
818
|
+
// board the ISP demosaics the RAW10 binning frame to a full 1280x960
|
|
819
|
+
// RGB565 image (2,457,600 bytes), but bytesused reports the RAW10 *input*
|
|
820
|
+
// size (1,536,000) — trusting it cropped a complete frame to 1280x600.
|
|
821
|
+
// The buffer is fully written each frame, so its length is the truth.
|
|
822
|
+
const std::size_t usedBytes = buffers_[index].length;
|
|
823
|
+
static bool s_msyncTested = false;
|
|
824
|
+
if (!s_msyncTested) {
|
|
825
|
+
s_msyncTested = true;
|
|
826
|
+
const std::int64_t m0 = esp_timer_get_time();
|
|
827
|
+
esp_cache_msync(buffers_[index].data, usedBytes, ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED);
|
|
828
|
+
const std::int64_t m1 = esp_timer_get_time();
|
|
829
|
+
ESP_LOGI(kTag, "TEST: esp_cache_msync %u bytes C2M = %lldus", static_cast<unsigned>(usedBytes), m1 - m0);
|
|
830
|
+
}
|
|
831
|
+
CapturedFrame frame{
|
|
832
|
+
.data = buffers_[index].data,
|
|
833
|
+
.bytes = usedBytes,
|
|
834
|
+
.width = width_,
|
|
835
|
+
.height = height_,
|
|
836
|
+
.format = format_,
|
|
837
|
+
.mirror = mirror_,
|
|
838
|
+
};
|
|
839
|
+
callback(frame);
|
|
840
|
+
}
|
|
841
|
+
const std::int64_t cbEndUs = esp_timer_get_time();
|
|
842
|
+
// TEMP perf: dq-wait vs callback (scale+present) split, avg over 30 frames.
|
|
843
|
+
dbgDqUs_ += cbStartUs - dqStartUs;
|
|
844
|
+
dbgCbUs_ += cbEndUs - cbStartUs;
|
|
845
|
+
if (++dbgFrames_ >= 30) {
|
|
846
|
+
ESP_LOGI(kTag, "withFrame perf: dqbuf=%lldus callback=%lldus (avg/30)",
|
|
847
|
+
dbgDqUs_ / dbgFrames_, dbgCbUs_ / dbgFrames_);
|
|
848
|
+
dbgDqUs_ = dbgCbUs_ = 0;
|
|
849
|
+
dbgFrames_ = 0;
|
|
850
|
+
}
|
|
851
|
+
if (ioctl(fd_, VIDIOC_QBUF, &buffer) != 0) {
|
|
852
|
+
ESP_LOGE(kTag, "QBUF failed: errno=%d", errno);
|
|
853
|
+
return false;
|
|
854
|
+
}
|
|
855
|
+
return true;
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
bool initializeVideo()
|
|
859
|
+
{
|
|
860
|
+
if (videoInitialized_) return true;
|
|
861
|
+
#if !GEA_BOARD_HAS_CAMERA
|
|
862
|
+
return false;
|
|
863
|
+
#else
|
|
864
|
+
#if GEA_BOARD_HAS_EXPANDER
|
|
865
|
+
// A sensor reset routed through the board's I/O expander: pulse it low,
|
|
866
|
+
// then release it before esp_video probes the sensor.
|
|
867
|
+
const int resetLine = gea::platform::board::camera.expanderReset;
|
|
868
|
+
auto &boardControl = gea::platform::esp32::chip_bindings::expanders::ioExpander();
|
|
869
|
+
if (resetLine >= 0 && boardControl.init()) {
|
|
870
|
+
boardControl.writePin(resetLine, false);
|
|
871
|
+
vTaskDelay(pdMS_TO_TICKS(10));
|
|
872
|
+
boardControl.writePin(resetLine, true);
|
|
873
|
+
vTaskDelay(pdMS_TO_TICKS(20));
|
|
874
|
+
}
|
|
875
|
+
#endif
|
|
876
|
+
// SCCB has a bus of its own, so esp_video creates it on the other I2C port.
|
|
877
|
+
esp_video_init_csi_config_t csiConfig{};
|
|
878
|
+
csiConfig.sccb_config.init_sccb = true;
|
|
879
|
+
csiConfig.sccb_config.i2c_config.port = I2C_NUM_0;
|
|
880
|
+
csiConfig.sccb_config.i2c_config.sda_pin = gea::platform::board::camera.sccbSda;
|
|
881
|
+
csiConfig.sccb_config.i2c_config.scl_pin = gea::platform::board::camera.sccbScl;
|
|
882
|
+
csiConfig.sccb_config.freq = kSccbFrequencyHz;
|
|
883
|
+
csiConfig.reset_pin = gea::platform::board::camera.reset;
|
|
884
|
+
csiConfig.pwdn_pin = -1;
|
|
885
|
+
|
|
886
|
+
esp_video_init_config_t videoConfig{};
|
|
887
|
+
videoConfig.csi = &csiConfig;
|
|
888
|
+
|
|
889
|
+
const esp_err_t err = esp_video_init(&videoConfig);
|
|
890
|
+
if (err != ESP_OK) {
|
|
891
|
+
ESP_LOGE(kTag, "esp_video_init failed: %s", esp_err_to_name(err));
|
|
892
|
+
return false;
|
|
893
|
+
}
|
|
894
|
+
videoInitialized_ = true;
|
|
895
|
+
return true;
|
|
896
|
+
#endif
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
bool queryAndConfigureFormat(int preferredWidth, int preferredHeight)
|
|
900
|
+
{
|
|
901
|
+
v4l2_capability capability{};
|
|
902
|
+
if (ioctl(fd_, VIDIOC_QUERYCAP, &capability) != 0) {
|
|
903
|
+
ESP_LOGE(kTag, "VIDIOC_QUERYCAP failed: errno=%d", errno);
|
|
904
|
+
return false;
|
|
905
|
+
}
|
|
906
|
+
ESP_LOGI(kTag, "driver=%s card=%s bus=%s", capability.driver, capability.card, capability.bus_info);
|
|
907
|
+
|
|
908
|
+
auto fourcc = [](std::uint32_t f, char *out) {
|
|
909
|
+
out[0] = static_cast<char>(f & 0xff);
|
|
910
|
+
out[1] = static_cast<char>((f >> 8) & 0xff);
|
|
911
|
+
out[2] = static_cast<char>((f >> 16) & 0xff);
|
|
912
|
+
out[3] = static_cast<char>((f >> 24) & 0xff);
|
|
913
|
+
out[4] = '\0';
|
|
914
|
+
};
|
|
915
|
+
|
|
916
|
+
// The ISP pipeline's default output format/size for the active sensor mode.
|
|
917
|
+
v4l2_format format{};
|
|
918
|
+
format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
919
|
+
if (ioctl(fd_, VIDIOC_G_FMT, &format) != 0) {
|
|
920
|
+
ESP_LOGE(kTag, "VIDIOC_G_FMT failed: errno=%d", errno);
|
|
921
|
+
return false;
|
|
922
|
+
}
|
|
923
|
+
char cc[5];
|
|
924
|
+
fourcc(format.fmt.pix.pixelformat, cc);
|
|
925
|
+
ESP_LOGI(kTag, "default fmt %s %ux%u", cc, format.fmt.pix.width, format.fmt.pix.height);
|
|
926
|
+
|
|
927
|
+
// Log every output format the device (ISP) actually supports — RGB565 may
|
|
928
|
+
// not be one of them, so we negotiate rather than hardcode it.
|
|
929
|
+
for (int i = 0;; ++i) {
|
|
930
|
+
v4l2_fmtdesc desc{};
|
|
931
|
+
desc.index = static_cast<std::uint32_t>(i);
|
|
932
|
+
desc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
933
|
+
if (ioctl(fd_, VIDIOC_ENUM_FMT, &desc) != 0) break;
|
|
934
|
+
fourcc(desc.pixelformat, cc);
|
|
935
|
+
ESP_LOGI(kTag, "supported fmt[%d]=%s", i, cc);
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
// Prefer a format framePixel() can convert, at the ISP's native output size
|
|
939
|
+
// (don't override width/height — the ISP locks them to the sensor mode chosen
|
|
940
|
+
// at esp_video init via CONFIG_CAMERA_SC2336_MIPI_*; here the 1024x600 RAW8
|
|
941
|
+
// mode is selected there, not here).
|
|
942
|
+
const std::uint32_t prefs[] = {
|
|
943
|
+
V4L2_PIX_FMT_RGB565, V4L2_PIX_FMT_RGB24, V4L2_PIX_FMT_YUYV, V4L2_PIX_FMT_YUV422P,
|
|
944
|
+
};
|
|
945
|
+
const std::uint32_t nativeW = format.fmt.pix.width;
|
|
946
|
+
const std::uint32_t nativeH = format.fmt.pix.height;
|
|
947
|
+
bool set = false;
|
|
948
|
+
for (std::uint32_t pf : prefs) {
|
|
949
|
+
v4l2_format trial = format;
|
|
950
|
+
trial.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
951
|
+
trial.fmt.pix.width = nativeW;
|
|
952
|
+
trial.fmt.pix.height = nativeH;
|
|
953
|
+
trial.fmt.pix.pixelformat = pf;
|
|
954
|
+
if (ioctl(fd_, VIDIOC_S_FMT, &trial) == 0) {
|
|
955
|
+
format = trial;
|
|
956
|
+
fourcc(pf, cc);
|
|
957
|
+
ESP_LOGI(kTag, "using fmt %s %ux%u", cc, format.fmt.pix.width, format.fmt.pix.height);
|
|
958
|
+
set = true;
|
|
959
|
+
break;
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
if (!set) {
|
|
963
|
+
// Nothing preferred took — fall back to the ISP default (framePixel also
|
|
964
|
+
// handles SBGGR8/GREY), so we still stream rather than bail.
|
|
965
|
+
std::memset(&format, 0, sizeof(format));
|
|
966
|
+
format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
967
|
+
if (ioctl(fd_, VIDIOC_G_FMT, &format) != 0) {
|
|
968
|
+
ESP_LOGE(kTag, "VIDIOC_G_FMT fallback failed: errno=%d", errno);
|
|
969
|
+
return false;
|
|
970
|
+
}
|
|
971
|
+
fourcc(format.fmt.pix.pixelformat, cc);
|
|
972
|
+
ESP_LOGW(kTag, "no preferred fmt accepted; streaming default %s %ux%u",
|
|
973
|
+
cc, format.fmt.pix.width, format.fmt.pix.height);
|
|
974
|
+
}
|
|
975
|
+
(void)preferredWidth;
|
|
976
|
+
(void)preferredHeight;
|
|
977
|
+
|
|
978
|
+
width_ = static_cast<int>(format.fmt.pix.width);
|
|
979
|
+
height_ = static_cast<int>(format.fmt.pix.height);
|
|
980
|
+
format_ = format.fmt.pix.pixelformat;
|
|
981
|
+
return width_ > 0 && height_ > 0;
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
bool mapBuffers(int count)
|
|
985
|
+
{
|
|
986
|
+
v4l2_requestbuffers request{};
|
|
987
|
+
request.count = static_cast<std::uint32_t>(std::min(count, kBufferCount));
|
|
988
|
+
request.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
989
|
+
request.memory = V4L2_MEMORY_MMAP;
|
|
990
|
+
if (ioctl(fd_, VIDIOC_REQBUFS, &request) != 0) {
|
|
991
|
+
ESP_LOGE(kTag, "VIDIOC_REQBUFS failed: errno=%d", errno);
|
|
992
|
+
return false;
|
|
993
|
+
}
|
|
994
|
+
bufferCount_ = std::min<int>(request.count, kBufferCount);
|
|
995
|
+
if (bufferCount_ < 2) {
|
|
996
|
+
ESP_LOGE(kTag, "camera returned too few buffers: %d", bufferCount_);
|
|
997
|
+
return false;
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
for (int i = 0; i < bufferCount_; ++i) {
|
|
1001
|
+
v4l2_buffer buffer{};
|
|
1002
|
+
buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
1003
|
+
buffer.memory = V4L2_MEMORY_MMAP;
|
|
1004
|
+
buffer.index = i;
|
|
1005
|
+
if (ioctl(fd_, VIDIOC_QUERYBUF, &buffer) != 0) {
|
|
1006
|
+
ESP_LOGE(kTag, "VIDIOC_QUERYBUF(%d) failed: errno=%d", i, errno);
|
|
1007
|
+
return false;
|
|
1008
|
+
}
|
|
1009
|
+
void *mapped = mmap(nullptr, buffer.length, PROT_READ | PROT_WRITE, MAP_SHARED, fd_, buffer.m.offset);
|
|
1010
|
+
if (mapped == reinterpret_cast<void *>(static_cast<intptr_t>(-1))) {
|
|
1011
|
+
ESP_LOGE(kTag, "mmap camera buffer %d failed: errno=%d", i, errno);
|
|
1012
|
+
return false;
|
|
1013
|
+
}
|
|
1014
|
+
buffers_[i] = {static_cast<std::uint8_t *>(mapped), buffer.length};
|
|
1015
|
+
if (ioctl(fd_, VIDIOC_QBUF, &buffer) != 0) {
|
|
1016
|
+
ESP_LOGE(kTag, "initial QBUF(%d) failed: errno=%d", i, errno);
|
|
1017
|
+
return false;
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
return true;
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
bool setStreaming(bool enabled)
|
|
1024
|
+
{
|
|
1025
|
+
const int type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
1026
|
+
if (ioctl(fd_, enabled ? VIDIOC_STREAMON : VIDIOC_STREAMOFF, &type) != 0) {
|
|
1027
|
+
ESP_LOGE(kTag, "%s failed: errno=%d", enabled ? "VIDIOC_STREAMON" : "VIDIOC_STREAMOFF", errno);
|
|
1028
|
+
return false;
|
|
1029
|
+
}
|
|
1030
|
+
streaming_ = enabled;
|
|
1031
|
+
return true;
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
// Switch /dev/video0's output pixel format in place (the sensor mode and
|
|
1035
|
+
// resolution are unchanged; only the ISP output format + buffer count change).
|
|
1036
|
+
// Caller must hold opLock_. Used to flip between RGB565 (preview / JPEG still)
|
|
1037
|
+
// and YUV420 (the H.264 encoder's input format).
|
|
1038
|
+
bool reconfigureFormat(std::uint32_t pixfmt, int count)
|
|
1039
|
+
{
|
|
1040
|
+
if (fd_ < 0) return false;
|
|
1041
|
+
if (streaming_) setStreaming(false);
|
|
1042
|
+
for (auto &buffer : buffers_) {
|
|
1043
|
+
if (buffer.data && buffer.length > 0) munmap(buffer.data, buffer.length);
|
|
1044
|
+
buffer = {};
|
|
1045
|
+
}
|
|
1046
|
+
v4l2_requestbuffers freeReq{};
|
|
1047
|
+
freeReq.count = 0;
|
|
1048
|
+
freeReq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
1049
|
+
freeReq.memory = V4L2_MEMORY_MMAP;
|
|
1050
|
+
ioctl(fd_, VIDIOC_REQBUFS, &freeReq); // release the old buffer set
|
|
1051
|
+
|
|
1052
|
+
v4l2_format format{};
|
|
1053
|
+
format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
1054
|
+
format.fmt.pix.width = static_cast<std::uint32_t>(width_);
|
|
1055
|
+
format.fmt.pix.height = static_cast<std::uint32_t>(height_);
|
|
1056
|
+
format.fmt.pix.pixelformat = pixfmt;
|
|
1057
|
+
if (ioctl(fd_, VIDIOC_S_FMT, &format) != 0) {
|
|
1058
|
+
ESP_LOGE(kTag, "reconfigureFormat S_FMT 0x%" PRIx32 " failed errno=%d", pixfmt, errno);
|
|
1059
|
+
return false;
|
|
1060
|
+
}
|
|
1061
|
+
width_ = static_cast<int>(format.fmt.pix.width);
|
|
1062
|
+
height_ = static_cast<int>(format.fmt.pix.height);
|
|
1063
|
+
format_ = format.fmt.pix.pixelformat;
|
|
1064
|
+
if (!mapBuffers(count)) return false;
|
|
1065
|
+
return setStreaming(true);
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
// Set one H.264 M2M encoder control (bitrate / I-period / QP). The encoder uses
|
|
1069
|
+
// the extended-control API with the codec control class.
|
|
1070
|
+
bool setH264Ctrl(int fd, std::uint32_t id, int value)
|
|
1071
|
+
{
|
|
1072
|
+
v4l2_ext_control ctrl{};
|
|
1073
|
+
ctrl.id = id;
|
|
1074
|
+
ctrl.value = value;
|
|
1075
|
+
v4l2_ext_controls ctrls{};
|
|
1076
|
+
ctrls.ctrl_class = V4L2_CID_CODEC_CLASS;
|
|
1077
|
+
ctrls.count = 1;
|
|
1078
|
+
ctrls.controls = &ctrl;
|
|
1079
|
+
if (ioctl(fd, VIDIOC_S_EXT_CTRLS, &ctrls) != 0) {
|
|
1080
|
+
ESP_LOGW(kTag, "h264 ctrl 0x%" PRIx32 "=%d failed errno=%d", id, value, errno);
|
|
1081
|
+
return false;
|
|
1082
|
+
}
|
|
1083
|
+
return true;
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
void ensureOpLock()
|
|
1087
|
+
{
|
|
1088
|
+
if (!opLock_) opLock_ = xSemaphoreCreateMutex();
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
// Mount the microSD card at /sdcard (SDMMC slot 1, 4-bit). Waveshare ESP32-P4
|
|
1092
|
+
// wiring: CLK=43, CMD=44, D0..D3=39..42. The P4's SD I/O rail is powered by the
|
|
1093
|
+
// on-chip LDO (channel 4), which must be brought up before the host. Mounts once.
|
|
1094
|
+
bool ensureSdMounted()
|
|
1095
|
+
{
|
|
1096
|
+
if (sdMounted_) return true;
|
|
1097
|
+
|
|
1098
|
+
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
|
|
1099
|
+
host.max_freq_khz = SDMMC_FREQ_HIGHSPEED; // 40 MHz
|
|
1100
|
+
sd_pwr_ctrl_ldo_config_t ldoConfig = {};
|
|
1101
|
+
ldoConfig.ldo_chan_id = 4; // ESP32-P4 internal LDO feeding the SD I/O rail
|
|
1102
|
+
if (!sdPwr_ && sd_pwr_ctrl_new_on_chip_ldo(&ldoConfig, &sdPwr_) != ESP_OK) {
|
|
1103
|
+
ESP_LOGE(kTag, "sd: on-chip LDO power-ctrl init failed");
|
|
1104
|
+
sdPwr_ = nullptr;
|
|
1105
|
+
}
|
|
1106
|
+
if (sdPwr_) host.pwr_ctrl_handle = sdPwr_;
|
|
1107
|
+
|
|
1108
|
+
sdmmc_slot_config_t slot = SDMMC_SLOT_CONFIG_DEFAULT();
|
|
1109
|
+
slot.width = 4;
|
|
1110
|
+
slot.clk = static_cast<gpio_num_t>(43);
|
|
1111
|
+
slot.cmd = static_cast<gpio_num_t>(44);
|
|
1112
|
+
slot.d0 = static_cast<gpio_num_t>(39);
|
|
1113
|
+
slot.d1 = static_cast<gpio_num_t>(40);
|
|
1114
|
+
slot.d2 = static_cast<gpio_num_t>(41);
|
|
1115
|
+
slot.d3 = static_cast<gpio_num_t>(42);
|
|
1116
|
+
slot.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
|
|
1117
|
+
|
|
1118
|
+
esp_vfs_fat_sdmmc_mount_config_t mountConfig = {};
|
|
1119
|
+
mountConfig.format_if_mount_failed = false;
|
|
1120
|
+
mountConfig.max_files = 4;
|
|
1121
|
+
mountConfig.allocation_unit_size = 64 * 1024;
|
|
1122
|
+
|
|
1123
|
+
const esp_err_t err = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot, &mountConfig, &sdCard_);
|
|
1124
|
+
if (err != ESP_OK) {
|
|
1125
|
+
ESP_LOGE(kTag, "sd: mount failed: %s", esp_err_to_name(err));
|
|
1126
|
+
return false;
|
|
1127
|
+
}
|
|
1128
|
+
sdMounted_ = true;
|
|
1129
|
+
const std::uint64_t mb =
|
|
1130
|
+
(static_cast<std::uint64_t>(sdCard_->csd.capacity) * sdCard_->csd.sector_size) >> 20;
|
|
1131
|
+
ESP_LOGI(kTag, "sd: mounted /sdcard (%llu MB)", static_cast<unsigned long long>(mb));
|
|
1132
|
+
return true;
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
bool setControl(std::uint32_t id, int value)
|
|
1136
|
+
{
|
|
1137
|
+
if (fd_ < 0) return false;
|
|
1138
|
+
v4l2_control control{};
|
|
1139
|
+
control.id = id;
|
|
1140
|
+
control.value = value;
|
|
1141
|
+
if (ioctl(fd_, VIDIOC_S_CTRL, &control) != 0) {
|
|
1142
|
+
ESP_LOGW(kTag, "VIDIOC_S_CTRL 0x%" PRIx32 "=%d failed: errno=%d", id, value, errno);
|
|
1143
|
+
return false;
|
|
1144
|
+
}
|
|
1145
|
+
return true;
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
// esp_video exposes sensor/ISP controls only through the EXTENDED control API
|
|
1149
|
+
// (VIDIOC_S_EXT_CTRLS); the legacy VIDIOC_S_CTRL above returns EINVAL for them.
|
|
1150
|
+
// Scalar variant (e.g. V4L2_CID_EXPOSURE -> sensor AE target).
|
|
1151
|
+
bool setExtControl(std::uint32_t id, int value)
|
|
1152
|
+
{
|
|
1153
|
+
if (fd_ < 0) return false;
|
|
1154
|
+
v4l2_ext_control ctrl{};
|
|
1155
|
+
ctrl.id = id;
|
|
1156
|
+
ctrl.value = value;
|
|
1157
|
+
v4l2_ext_controls ctrls{};
|
|
1158
|
+
ctrls.ctrl_class = V4L2_CTRL_CLASS_USER;
|
|
1159
|
+
ctrls.count = 1;
|
|
1160
|
+
ctrls.controls = &ctrl;
|
|
1161
|
+
if (ioctl(fd_, VIDIOC_S_EXT_CTRLS, &ctrls) != 0) {
|
|
1162
|
+
ESP_LOGW(kTag, "S_EXT_CTRLS 0x%" PRIx32 "=%d failed: errno=%d", id, value, errno);
|
|
1163
|
+
return false;
|
|
1164
|
+
}
|
|
1165
|
+
return true;
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
// Compound variant for the ISP white-balance control (a struct passed by pointer).
|
|
1169
|
+
bool setIspWb(bool enable, float redGain, float blueGain)
|
|
1170
|
+
{
|
|
1171
|
+
if (fd_ < 0) return false;
|
|
1172
|
+
esp_video_isp_wb_t wb{};
|
|
1173
|
+
wb.enable = enable;
|
|
1174
|
+
wb.red_gain = redGain;
|
|
1175
|
+
wb.blue_gain = blueGain;
|
|
1176
|
+
v4l2_ext_control ctrl{};
|
|
1177
|
+
ctrl.id = V4L2_CID_USER_ESP_ISP_WB;
|
|
1178
|
+
ctrl.size = sizeof(wb);
|
|
1179
|
+
ctrl.p_u8 = reinterpret_cast<std::uint8_t *>(&wb);
|
|
1180
|
+
v4l2_ext_controls ctrls{};
|
|
1181
|
+
ctrls.ctrl_class = V4L2_CTRL_CLASS_USER;
|
|
1182
|
+
ctrls.count = 1;
|
|
1183
|
+
ctrls.controls = &ctrl;
|
|
1184
|
+
if (ioctl(fd_, VIDIOC_S_EXT_CTRLS, &ctrls) != 0) {
|
|
1185
|
+
ESP_LOGW(kTag, "S_EXT_CTRLS ISP_WB(en=%d r=%.2f b=%.2f) failed: errno=%d", enable, redGain, blueGain, errno);
|
|
1186
|
+
return false;
|
|
1187
|
+
}
|
|
1188
|
+
return true;
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
void configureFlipControls()
|
|
1192
|
+
{
|
|
1193
|
+
setControl(V4L2_CID_VFLIP, 1);
|
|
1194
|
+
setControl(V4L2_CID_HFLIP, mirror_ ? 1 : 0);
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1197
|
+
// Scale the latest frame into dst (RGB565), honouring the fit mode:
|
|
1198
|
+
// 0=cover (crop to dst aspect), 1=contain (letterbox), 2=fill (stretch).
|
|
1199
|
+
// rotateCcw rotates the source by 90*rotateCcw degrees counter-clockwise
|
|
1200
|
+
// while scaling. A module mounted 90deg off the landscape
|
|
1201
|
+
// viewport, so the preview rotates it back: fit + crop math run on the
|
|
1202
|
+
// *rotated* source dimensions, and each pixel read remaps back into the real
|
|
1203
|
+
// (unrotated) frame so a single pass both un-rotates and fills the viewfinder.
|
|
1204
|
+
void fillScaled(const CapturedFrame &frame, std::uint16_t *dst, int dstW, int dstH, int fit, int rotateCcw = 0)
|
|
1205
|
+
{
|
|
1206
|
+
const int srcW = frame.width;
|
|
1207
|
+
const int srcH = frame.height;
|
|
1208
|
+
if (srcW <= 0 || srcH <= 0) return;
|
|
1209
|
+
|
|
1210
|
+
const int rot = ((rotateCcw % 4) + 4) % 4;
|
|
1211
|
+
const bool swap = (rot & 1) != 0;
|
|
1212
|
+
const int rsrcW = swap ? srcH : srcW; // rotated-source width
|
|
1213
|
+
const int rsrcH = swap ? srcW : srcH; // rotated-source height
|
|
1214
|
+
|
|
1215
|
+
double sx0 = 0.0, sy0 = 0.0, sw = rsrcW, sh = rsrcH; // sampled (rotated) source span
|
|
1216
|
+
int dx0 = 0, dy0 = 0, dw = dstW, dh = dstH; // dst image rect (contain)
|
|
1217
|
+
const double srcAspect = static_cast<double>(rsrcW) / rsrcH;
|
|
1218
|
+
const double dstAspect = static_cast<double>(dstW) / dstH;
|
|
1219
|
+
if (fit == 0) { // cover
|
|
1220
|
+
if (srcAspect > dstAspect) {
|
|
1221
|
+
sw = rsrcH * dstAspect;
|
|
1222
|
+
sx0 = (rsrcW - sw) / 2.0;
|
|
1223
|
+
} else {
|
|
1224
|
+
sh = rsrcW / dstAspect;
|
|
1225
|
+
sy0 = (rsrcH - sh) / 2.0;
|
|
1226
|
+
}
|
|
1227
|
+
} else if (fit == 1) { // contain
|
|
1228
|
+
if (srcAspect > dstAspect) {
|
|
1229
|
+
dh = static_cast<int>(dstW / srcAspect);
|
|
1230
|
+
dy0 = (dstH - dh) / 2;
|
|
1231
|
+
} else {
|
|
1232
|
+
dw = static_cast<int>(dstH * srcAspect);
|
|
1233
|
+
dx0 = (dstW - dw) / 2;
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
// Per-pixel source addressing in 16.16 fixed point. The ESP32-P4 FPU is
|
|
1238
|
+
// single-precision only, so the previous per-pixel `double` divides were
|
|
1239
|
+
// software-emulated (~1 s/frame for a 468x628 viewfinder). Precompute the
|
|
1240
|
+
// source step once and accumulate per pixel — integer add, no divide.
|
|
1241
|
+
const int spanW = (fit == 1) ? dw : dstW;
|
|
1242
|
+
const int spanH = (fit == 1) ? dh : dstH;
|
|
1243
|
+
const std::int64_t sxStep = spanW > 0 ? static_cast<std::int64_t>(sw * 65536.0) / spanW : 0;
|
|
1244
|
+
const std::int64_t syStep = spanH > 0 ? static_cast<std::int64_t>(sh * 65536.0) / spanH : 0;
|
|
1245
|
+
const std::int64_t sxBase = static_cast<std::int64_t>(sx0 * 65536.0);
|
|
1246
|
+
const std::int64_t syBase = static_cast<std::int64_t>(sy0 * 65536.0);
|
|
1247
|
+
|
|
1248
|
+
// Map a rotated-source coord (lx, ly) back to the real frame and read it.
|
|
1249
|
+
const auto sampleRotated = [&](int lx, int ly) -> std::uint16_t {
|
|
1250
|
+
int ox, oy;
|
|
1251
|
+
switch (rot) {
|
|
1252
|
+
case 1: ox = srcW - 1 - ly; oy = lx; break; // 90 CCW
|
|
1253
|
+
case 2: ox = srcW - 1 - lx; oy = srcH - 1 - ly; break; // 180
|
|
1254
|
+
case 3: ox = ly; oy = srcH - 1 - lx; break; // 270 CCW (= 90 CW)
|
|
1255
|
+
default: ox = lx; oy = ly; break; // 0
|
|
1256
|
+
}
|
|
1257
|
+
return framePixel(frame, ox, oy);
|
|
1258
|
+
};
|
|
1259
|
+
|
|
1260
|
+
for (int y = 0; y < dstH; ++y) {
|
|
1261
|
+
std::uint16_t *row = dst + static_cast<std::size_t>(y) * dstW;
|
|
1262
|
+
if (fit == 1 && (y < dy0 || y >= dy0 + dh)) {
|
|
1263
|
+
std::memset(row, 0, static_cast<std::size_t>(dstW) * sizeof(std::uint16_t));
|
|
1264
|
+
continue;
|
|
1265
|
+
}
|
|
1266
|
+
const std::int64_t syFx = (fit == 1)
|
|
1267
|
+
? syBase + static_cast<std::int64_t>(y - dy0) * syStep
|
|
1268
|
+
: syBase + static_cast<std::int64_t>(y) * syStep;
|
|
1269
|
+
const int ly = static_cast<int>(syFx >> 16);
|
|
1270
|
+
if (fit == 1) {
|
|
1271
|
+
int x = 0;
|
|
1272
|
+
for (; x < dx0 && x < dstW; ++x) row[x] = 0; // left bar
|
|
1273
|
+
std::int64_t sxFx = sxBase;
|
|
1274
|
+
for (; x < dstW && x < dx0 + dw; ++x) {
|
|
1275
|
+
row[x] = sampleRotated(static_cast<int>(sxFx >> 16), ly);
|
|
1276
|
+
sxFx += sxStep;
|
|
1277
|
+
}
|
|
1278
|
+
for (; x < dstW; ++x) row[x] = 0; // right bar
|
|
1279
|
+
} else {
|
|
1280
|
+
std::int64_t sxFx = sxBase;
|
|
1281
|
+
for (int x = 0; x < dstW; ++x) {
|
|
1282
|
+
row[x] = sampleRotated(static_cast<int>(sxFx >> 16), ly);
|
|
1283
|
+
sxFx += sxStep;
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
}
|
|
1288
|
+
|
|
1289
|
+
bool initPpa()
|
|
1290
|
+
{
|
|
1291
|
+
if (ppaClient_) return true;
|
|
1292
|
+
ppa_client_config_t cfg = {};
|
|
1293
|
+
cfg.oper_type = PPA_OPERATION_SRM;
|
|
1294
|
+
cfg.max_pending_trans_num = 1;
|
|
1295
|
+
cfg.data_burst_length = PPA_DATA_BURST_LENGTH_128;
|
|
1296
|
+
if (ppa_register_client(&cfg, &ppaClient_) != ESP_OK) {
|
|
1297
|
+
ppaClient_ = nullptr;
|
|
1298
|
+
return false;
|
|
1299
|
+
}
|
|
1300
|
+
return true;
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
// Black the camera surface once (and after a resize) and flush it to RAM: the
|
|
1304
|
+
// PPA's scaled+rotated output block can land a pixel or two short of the
|
|
1305
|
+
// surface (scale quantisation), so the few edge pixels it never writes stay
|
|
1306
|
+
// black instead of showing uninitialised noise.
|
|
1307
|
+
void primePpaSurface(std::uint16_t *dst, std::size_t bytes)
|
|
1308
|
+
{
|
|
1309
|
+
if (dst == ppaLastDst_ && bytes == ppaLastBytes_) return;
|
|
1310
|
+
ppaLastDst_ = dst;
|
|
1311
|
+
ppaLastBytes_ = bytes;
|
|
1312
|
+
std::memset(dst, 0, bytes);
|
|
1313
|
+
esp_cache_msync(dst, bytes, ESP_CACHE_MSYNC_FLAG_DIR_C2M);
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
// Core PPA scale+rotate: the latest frame lands as a dstW x dstH block at
|
|
1317
|
+
// (outOffX,outOffY) inside the outPicW x outPicH output picture. The PPA
|
|
1318
|
+
// truncates the scale to 1/16 steps and sets output = floor(block*scale), so a
|
|
1319
|
+
// plain cover scale lands a few px short (black edge); round the cover scale UP
|
|
1320
|
+
// to the next 1/16, then size the centred input crop so the truncated output
|
|
1321
|
+
// is EXACTLY dstW x dstH. Returns false if the PPA can't service this frame.
|
|
1322
|
+
bool ppaScaleRotateInto(const CapturedFrame &frame, std::uint16_t *outBuf, std::size_t outBytes,
|
|
1323
|
+
int outPicW, int outPicH, int outOffX, int outOffY, int dstW, int dstH,
|
|
1324
|
+
int rotCcw = kPreviewRotateCcw)
|
|
1325
|
+
{
|
|
1326
|
+
if (!frame.data || frame.width <= 0 || frame.height <= 0) return false;
|
|
1327
|
+
if (frame.format != V4L2_PIX_FMT_RGB565) return false; // RGB565 in/out only
|
|
1328
|
+
if (!initPpa()) return false;
|
|
1329
|
+
// PPA output to PSRAM must be cache-line aligned (queried in initPpa).
|
|
1330
|
+
if ((reinterpret_cast<std::uintptr_t>(outBuf) & (ppaOutAlign_ - 1)) != 0) return false;
|
|
1331
|
+
|
|
1332
|
+
const int rot = ((rotCcw % 4) + 4) % 4;
|
|
1333
|
+
const bool swap = (rot & 1) != 0;
|
|
1334
|
+
ppa_srm_rotation_angle_t angle = PPA_SRM_ROTATION_ANGLE_0;
|
|
1335
|
+
if (rot == 1) angle = PPA_SRM_ROTATION_ANGLE_90;
|
|
1336
|
+
else if (rot == 2) angle = PPA_SRM_ROTATION_ANGLE_180;
|
|
1337
|
+
else if (rot == 3) angle = PPA_SRM_ROTATION_ANGLE_270;
|
|
1338
|
+
|
|
1339
|
+
const int Fw = frame.width, Fh = frame.height;
|
|
1340
|
+
const int rotW = swap ? Fh : Fw;
|
|
1341
|
+
const int rotH = swap ? Fw : Fh;
|
|
1342
|
+
constexpr int kFrag = 16; // PPA_LL_SRM_SCALING_FRAG_MAX
|
|
1343
|
+
// Digital zoom: scaling up past the cover factor shrinks the source block to
|
|
1344
|
+
// a centered crop, so the same output shows a zoomed-in region. zoom_==1 is
|
|
1345
|
+
// the plain cover fit. (As a bonus, zooming in reads fewer pixels -> faster.)
|
|
1346
|
+
const double zoom = zoom_ > 1.0 ? zoom_ : 1.0;
|
|
1347
|
+
const double sCover = std::max(static_cast<double>(dstW) / rotW, static_cast<double>(dstH) / rotH);
|
|
1348
|
+
int sNum = static_cast<int>(std::ceil(sCover * zoom * kFrag)); // scale = sNum/16, >= cover*zoom
|
|
1349
|
+
if (sNum < 1) sNum = 1;
|
|
1350
|
+
const float scale = static_cast<float>(sNum) / kFrag;
|
|
1351
|
+
const int needForW = (dstW * kFrag + sNum - 1) / sNum; // ceil(dstW*16/sNum)
|
|
1352
|
+
const int needForH = (dstH * kFrag + sNum - 1) / sNum;
|
|
1353
|
+
const int blockW = std::min(Fw, swap ? needForH : needForW);
|
|
1354
|
+
const int blockH = std::min(Fh, swap ? needForW : needForH);
|
|
1355
|
+
if (blockW <= 0 || blockH <= 0) return false;
|
|
1356
|
+
const int offX = (Fw - blockW) / 2;
|
|
1357
|
+
const int offY = (Fh - blockH) / 2;
|
|
1358
|
+
|
|
1359
|
+
ppa_srm_oper_config_t cfg = {};
|
|
1360
|
+
cfg.in.buffer = frame.data;
|
|
1361
|
+
cfg.in.pic_w = static_cast<std::uint32_t>(Fw);
|
|
1362
|
+
cfg.in.pic_h = static_cast<std::uint32_t>(Fh);
|
|
1363
|
+
cfg.in.block_w = static_cast<std::uint32_t>(blockW);
|
|
1364
|
+
cfg.in.block_h = static_cast<std::uint32_t>(blockH);
|
|
1365
|
+
cfg.in.block_offset_x = static_cast<std::uint32_t>(offX);
|
|
1366
|
+
cfg.in.block_offset_y = static_cast<std::uint32_t>(offY);
|
|
1367
|
+
cfg.in.srm_cm = PPA_SRM_COLOR_MODE_RGB565;
|
|
1368
|
+
cfg.out.buffer = outBuf;
|
|
1369
|
+
cfg.out.buffer_size = static_cast<std::uint32_t>(outBytes);
|
|
1370
|
+
cfg.out.pic_w = static_cast<std::uint32_t>(outPicW);
|
|
1371
|
+
cfg.out.pic_h = static_cast<std::uint32_t>(outPicH);
|
|
1372
|
+
cfg.out.block_offset_x = static_cast<std::uint32_t>(outOffX);
|
|
1373
|
+
cfg.out.block_offset_y = static_cast<std::uint32_t>(outOffY);
|
|
1374
|
+
cfg.out.srm_cm = PPA_SRM_COLOR_MODE_RGB565;
|
|
1375
|
+
cfg.rotation_angle = angle;
|
|
1376
|
+
cfg.scale_x = scale;
|
|
1377
|
+
cfg.scale_y = scale;
|
|
1378
|
+
cfg.mode = PPA_TRANS_MODE_BLOCKING;
|
|
1379
|
+
const std::int64_t ppaT0 = esp_timer_get_time();
|
|
1380
|
+
const bool ppaOk = ppa_do_scale_rotate_mirror(ppaClient_, &cfg) == ESP_OK;
|
|
1381
|
+
const std::int64_t ppaT1 = esp_timer_get_time();
|
|
1382
|
+
static std::int64_t s_ppaAcc = 0;
|
|
1383
|
+
static int s_ppaN = 0;
|
|
1384
|
+
s_ppaAcc += ppaT1 - ppaT0;
|
|
1385
|
+
if (++s_ppaN >= 30) {
|
|
1386
|
+
ESP_LOGI(kTag, "TEST: ppa_do_scale_rotate_mirror avg=%lldus in=%dx%d block=%dx%d out=%dx%d dst=%dx%d scale=%.3f",
|
|
1387
|
+
s_ppaAcc / s_ppaN, Fw, Fh, blockW, blockH, outPicW, outPicH, dstW, dstH, scale);
|
|
1388
|
+
s_ppaAcc = 0;
|
|
1389
|
+
s_ppaN = 0;
|
|
1390
|
+
}
|
|
1391
|
+
return ppaOk;
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
// Framebuffer preview mode: PPA into the camera surface (the renderer blits
|
|
1395
|
+
// it). Cover fit only; returns false so the caller uses the CPU scaler.
|
|
1396
|
+
bool fillScaledPpa(const CapturedFrame &frame, std::uint16_t *dst, int dstW, int dstH, int fit)
|
|
1397
|
+
{
|
|
1398
|
+
if (fit != 0) return false; // cover only
|
|
1399
|
+
if ((reinterpret_cast<std::uintptr_t>(dst) & 127u) != 0) return false;
|
|
1400
|
+
const std::size_t outBytes = (static_cast<std::size_t>(dstW) * dstH * 2 + 127) & ~static_cast<std::size_t>(127);
|
|
1401
|
+
primePpaSurface(dst, outBytes);
|
|
1402
|
+
if (!ppaScaleRotateInto(frame, dst, outBytes, dstW, dstH, 0, 0, dstW, dstH)) return false;
|
|
1403
|
+
// PPA wrote the surface via DMA — drop stale CPU cache lines for the blit.
|
|
1404
|
+
esp_cache_msync(dst, outBytes, ESP_CACHE_MSYNC_FLAG_DIR_M2C);
|
|
1405
|
+
return true;
|
|
1406
|
+
}
|
|
1407
|
+
|
|
1408
|
+
// NativeOverlay direct-present: PPA-scale the latest frame straight into the
|
|
1409
|
+
// display's logical framebuffer at (vfX,vfY) and flush only that rect — no
|
|
1410
|
+
// surface, no display-list blit, no full replay. The camera owns this rect, so
|
|
1411
|
+
// the renderer never draws or flushes it.
|
|
1412
|
+
bool presentDirect(int vfX, int vfY, int vfW, int vfH)
|
|
1413
|
+
{
|
|
1414
|
+
if (!open_ || vfW <= 0 || vfH <= 0) return false;
|
|
1415
|
+
// One-pass fast path: scale+rotate the latest frame STRAIGHT into the live panel
|
|
1416
|
+
// scanout buffer at the viewfinder's panel rect, composing the camera's own sensor
|
|
1417
|
+
// rotation with the display orientation's rotation. The DPI panel refreshes the
|
|
1418
|
+
// buffer continuously, so there is nothing else to do — no off-screen landscape
|
|
1419
|
+
// canvas, no second rotate-to-panel flush. That second PPA pass was ~half the
|
|
1420
|
+
// per-frame cost (scale 34ms + rotate 29ms); fusing it ~doubles the preview fps.
|
|
1421
|
+
// The camera owns this rect; the UI never composes into it.
|
|
1422
|
+
std::uint16_t *panelBuf = nullptr;
|
|
1423
|
+
int panelBufW = 0, panelBufH = 0, pX = 0, pY = 0, pW = 0, pH = 0, rotSteps = 0, flip = 0;
|
|
1424
|
+
if (gea::platform::display::Display::panelDirectTarget(vfX, vfY, vfW, vfH, &panelBuf, &panelBufW, &panelBufH,
|
|
1425
|
+
&pX, &pY, &pW, &pH, &rotSteps, &flip)) {
|
|
1426
|
+
const std::size_t panelBytes = (static_cast<std::size_t>(panelBufW) * panelBufH * 2 + 127) & ~static_cast<std::size_t>(127);
|
|
1427
|
+
bool ok = false;
|
|
1428
|
+
withFrame([&](CapturedFrame &frame) {
|
|
1429
|
+
// Full-panel (flip==1): scale into the non-scanned back buffer, then swap it
|
|
1430
|
+
// in at vblank — the DSI never sees a half-written frame, so no tearing.
|
|
1431
|
+
// Sub-rect (flip==0): scale into the live buffer in place (windowed).
|
|
1432
|
+
if (!ppaScaleRotateInto(frame, panelBuf, panelBytes, panelBufW, panelBufH, pX, pY, pW, pH, kPreviewRotateCcw + rotSteps)) return;
|
|
1433
|
+
if (flip) gea::platform::display::Display::flipPanelToBack();
|
|
1434
|
+
recordFrameIfDue(frame);
|
|
1435
|
+
ok = true;
|
|
1436
|
+
});
|
|
1437
|
+
return ok;
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1440
|
+
// Fallback (no panel scanout buffer available): compose into the off-screen logical
|
|
1441
|
+
// framebuffer + flush/rotate the rect.
|
|
1442
|
+
auto *canvas = gea::platform::display::Display::canvas();
|
|
1443
|
+
if (!canvas || !canvas->pixels()) return false;
|
|
1444
|
+
std::uint16_t *fb = const_cast<std::uint16_t *>(canvas->pixels());
|
|
1445
|
+
const int fbW = canvas->width();
|
|
1446
|
+
const int fbH = canvas->height();
|
|
1447
|
+
if (fbW <= 0 || fbH <= 0 || canvas->strideBytes() != fbW * 2) return false; // tightly-packed only
|
|
1448
|
+
if (vfX < 0 || vfY < 0 || vfX + vfW > fbW || vfY + vfH > fbH) return false;
|
|
1449
|
+
const std::size_t fbBytes = (static_cast<std::size_t>(fbW) * fbH * 2 + 127) & ~static_cast<std::size_t>(127);
|
|
1450
|
+
bool ok = false;
|
|
1451
|
+
withFrame([&](CapturedFrame &frame) {
|
|
1452
|
+
if (!ppaScaleRotateInto(frame, fb, fbBytes, fbW, fbH, vfX, vfY, vfW, vfH)) return;
|
|
1453
|
+
std::uint16_t *rows = fb + static_cast<std::size_t>(vfY) * fbW;
|
|
1454
|
+
esp_cache_msync(rows, static_cast<std::size_t>(vfH) * fbW * 2, ESP_CACHE_MSYNC_FLAG_DIR_M2C);
|
|
1455
|
+
const gea::platform::display::DisplayFlushRect rect{vfX, vfY, vfX + vfW - 1, vfY + vfH - 1};
|
|
1456
|
+
gea::platform::display::Display::flushRects(&rect, 1);
|
|
1457
|
+
recordFrameIfDue(frame);
|
|
1458
|
+
ok = true;
|
|
1459
|
+
});
|
|
1460
|
+
return ok;
|
|
1461
|
+
}
|
|
1462
|
+
|
|
1463
|
+
// Encode the current frame to JPEG (HW) and append to the open recording
|
|
1464
|
+
// file, throttled to the requested fps. Driven from the preview path.
|
|
1465
|
+
void recordFrameIfDue(const CapturedFrame &frame)
|
|
1466
|
+
{
|
|
1467
|
+
#if GEA_P4_HAS_JPEG_ENCODER
|
|
1468
|
+
if (!recording_ || !recFile_ || !recJpegEnc_ || !frame.data) return;
|
|
1469
|
+
if (frame.format != V4L2_PIX_FMT_RGB565) return; // encoder configured for RGB565
|
|
1470
|
+
const std::int64_t now = esp_timer_get_time();
|
|
1471
|
+
const std::int64_t interval = static_cast<std::int64_t>(1000000.0 / (recFps_ > 0.0 ? recFps_ : 15.0));
|
|
1472
|
+
if (recLastFrameUs_ != 0 && now - recLastFrameUs_ < interval) return;
|
|
1473
|
+
recLastFrameUs_ = now;
|
|
1474
|
+
|
|
1475
|
+
jpeg_encode_cfg_t cfg = {};
|
|
1476
|
+
cfg.height = static_cast<uint32_t>(frame.height);
|
|
1477
|
+
cfg.width = static_cast<uint32_t>(frame.width);
|
|
1478
|
+
cfg.src_type = JPEG_ENCODE_IN_FORMAT_RGB565;
|
|
1479
|
+
cfg.sub_sample = JPEG_DOWN_SAMPLING_YUV420;
|
|
1480
|
+
cfg.image_quality = 80;
|
|
1481
|
+
uint32_t outLen = 0;
|
|
1482
|
+
const esp_err_t err = jpeg_encoder_process(recJpegEnc_, &cfg, frame.data,
|
|
1483
|
+
static_cast<uint32_t>(frame.bytes), recJpegOut_,
|
|
1484
|
+
static_cast<uint32_t>(recJpegOutCap_), &outLen);
|
|
1485
|
+
if (err == ESP_OK && outLen > 0) {
|
|
1486
|
+
std::fwrite(recJpegOut_, 1, outLen, recFile_);
|
|
1487
|
+
++recFrameCount_;
|
|
1488
|
+
}
|
|
1489
|
+
#else
|
|
1490
|
+
(void)frame;
|
|
1491
|
+
#endif
|
|
1492
|
+
}
|
|
1493
|
+
|
|
1494
|
+
bool videoInitialized_ = false;
|
|
1495
|
+
bool open_ = false;
|
|
1496
|
+
bool streaming_ = false;
|
|
1497
|
+
bool mirror_ = true;
|
|
1498
|
+
// Serializes the render task's preview frames against the long capture ops
|
|
1499
|
+
// (still / clip), which reconfigure the device mid-stream. Created lazily.
|
|
1500
|
+
SemaphoreHandle_t opLock_ = nullptr;
|
|
1501
|
+
// microSD state — captures are written here (no PSRAM size cap, no serial-link
|
|
1502
|
+
// corruption) and pulled off afterwards via the reliable GEADEV GET protocol.
|
|
1503
|
+
bool sdMounted_ = false;
|
|
1504
|
+
sdmmc_card_t *sdCard_ = nullptr;
|
|
1505
|
+
sd_pwr_ctrl_handle_t sdPwr_ = nullptr;
|
|
1506
|
+
// Persistent H.264 clip buffer: the encode loop accumulates here (fast memcpy)
|
|
1507
|
+
// and the whole clip is bulk-written to SD afterwards. Allocated once and reused.
|
|
1508
|
+
std::uint8_t *clipBuf_ = nullptr;
|
|
1509
|
+
std::size_t clipCap_ = 0;
|
|
1510
|
+
double zoom_ = 1.0;
|
|
1511
|
+
int fd_ = -1;
|
|
1512
|
+
int width_ = kDefaultWidth;
|
|
1513
|
+
int height_ = kDefaultHeight;
|
|
1514
|
+
int bufferCount_ = 0;
|
|
1515
|
+
std::int64_t dbgDqUs_ = 0, dbgCbUs_ = 0;
|
|
1516
|
+
int dbgFrames_ = 0;
|
|
1517
|
+
std::uint32_t format_ = V4L2_PIX_FMT_RGB565;
|
|
1518
|
+
gea::platform::camera::Facing facing_ = gea::platform::camera::Facing::Front;
|
|
1519
|
+
std::array<MappedBuffer, kBufferCount> buffers_{};
|
|
1520
|
+
|
|
1521
|
+
// PPA HW scale/rotate state (lazy-initialised on first preview frame). The PPA
|
|
1522
|
+
// scales straight into the camera surface; ppaLastDst_ tracks which surface
|
|
1523
|
+
// has been blacked so edge pixels the PPA never writes don't show noise.
|
|
1524
|
+
ppa_client_handle_t ppaClient_ = nullptr;
|
|
1525
|
+
std::size_t ppaOutAlign_ = 64; // PPA output cache-line alignment (P4 PSRAM = 64 B)
|
|
1526
|
+
std::uint16_t *ppaLastDst_ = nullptr;
|
|
1527
|
+
std::size_t ppaLastBytes_ = 0;
|
|
1528
|
+
// NativeOverlay viewfinder rect (logical framebuffer px), set by the renderer.
|
|
1529
|
+
int vfX_ = 0, vfY_ = 0, vfW_ = 0, vfH_ = 0;
|
|
1530
|
+
|
|
1531
|
+
// Motion-JPEG recording state.
|
|
1532
|
+
bool recording_ = false;
|
|
1533
|
+
std::FILE *recFile_ = nullptr;
|
|
1534
|
+
double recFps_ = 15.0;
|
|
1535
|
+
std::int64_t recStartUs_ = 0;
|
|
1536
|
+
std::int64_t recLastFrameUs_ = 0;
|
|
1537
|
+
std::uint8_t *recJpegOut_ = nullptr;
|
|
1538
|
+
std::size_t recJpegOutCap_ = 0;
|
|
1539
|
+
int recFrameCount_ = 0;
|
|
1540
|
+
#if GEA_P4_HAS_JPEG_ENCODER
|
|
1541
|
+
jpeg_encoder_handle_t recJpegEnc_ = nullptr;
|
|
1542
|
+
#endif
|
|
1543
|
+
};
|
|
1544
|
+
|
|
1545
|
+
bool CameraDriver::startRecording(const std::string &path, double fps)
|
|
1546
|
+
{
|
|
1547
|
+
#if GEA_P4_HAS_JPEG_ENCODER
|
|
1548
|
+
if (recording_ || !open_) return false;
|
|
1549
|
+
recFile_ = std::fopen(path.c_str(), "wb");
|
|
1550
|
+
if (!recFile_) {
|
|
1551
|
+
ESP_LOGE(kTag, "recording: fopen %s failed: errno=%d", path.c_str(), errno);
|
|
1552
|
+
return false;
|
|
1553
|
+
}
|
|
1554
|
+
jpeg_encode_engine_cfg_t engineCfg = {};
|
|
1555
|
+
engineCfg.timeout_ms = 200;
|
|
1556
|
+
if (jpeg_new_encoder_engine(&engineCfg, &recJpegEnc_) != ESP_OK) {
|
|
1557
|
+
ESP_LOGE(kTag, "recording: jpeg_new_encoder_engine failed");
|
|
1558
|
+
std::fclose(recFile_);
|
|
1559
|
+
recFile_ = nullptr;
|
|
1560
|
+
return false;
|
|
1561
|
+
}
|
|
1562
|
+
// JPEG of an RGB565 frame is comfortably smaller than the raw frame; size
|
|
1563
|
+
// the output to the raw byte count as a safe upper bound.
|
|
1564
|
+
const std::size_t cap = static_cast<std::size_t>(width_) * static_cast<std::size_t>(height_) * 2u;
|
|
1565
|
+
jpeg_encode_memory_alloc_cfg_t memCfg = {};
|
|
1566
|
+
memCfg.buffer_direction = JPEG_ENC_ALLOC_OUTPUT_BUFFER;
|
|
1567
|
+
std::size_t allocated = 0;
|
|
1568
|
+
recJpegOut_ = static_cast<std::uint8_t *>(jpeg_alloc_encoder_mem(cap, &memCfg, &allocated));
|
|
1569
|
+
if (!recJpegOut_) {
|
|
1570
|
+
ESP_LOGE(kTag, "recording: jpeg output alloc (%zu) failed", cap);
|
|
1571
|
+
jpeg_del_encoder_engine(recJpegEnc_);
|
|
1572
|
+
recJpegEnc_ = nullptr;
|
|
1573
|
+
std::fclose(recFile_);
|
|
1574
|
+
recFile_ = nullptr;
|
|
1575
|
+
return false;
|
|
1576
|
+
}
|
|
1577
|
+
recJpegOutCap_ = allocated;
|
|
1578
|
+
recFps_ = fps > 0.0 ? fps : 15.0;
|
|
1579
|
+
recStartUs_ = esp_timer_get_time();
|
|
1580
|
+
recLastFrameUs_ = 0;
|
|
1581
|
+
recFrameCount_ = 0;
|
|
1582
|
+
recording_ = true;
|
|
1583
|
+
ESP_LOGI(kTag, "recording started -> %s @ %.0ffps", path.c_str(), recFps_);
|
|
1584
|
+
return true;
|
|
1585
|
+
#else
|
|
1586
|
+
(void)path;
|
|
1587
|
+
(void)fps;
|
|
1588
|
+
ESP_LOGW(kTag, "recording unavailable: esp_driver_jpeg component not present");
|
|
1589
|
+
return false;
|
|
1590
|
+
#endif
|
|
1591
|
+
}
|
|
1592
|
+
|
|
1593
|
+
double CameraDriver::stopRecording()
|
|
1594
|
+
{
|
|
1595
|
+
#if GEA_P4_HAS_JPEG_ENCODER
|
|
1596
|
+
if (!recording_) return -1.0;
|
|
1597
|
+
recording_ = false;
|
|
1598
|
+
const double durationMs = (esp_timer_get_time() - recStartUs_) / 1000.0;
|
|
1599
|
+
if (recFile_) {
|
|
1600
|
+
std::fclose(recFile_);
|
|
1601
|
+
recFile_ = nullptr;
|
|
1602
|
+
}
|
|
1603
|
+
if (recJpegOut_) {
|
|
1604
|
+
heap_caps_free(recJpegOut_);
|
|
1605
|
+
recJpegOut_ = nullptr;
|
|
1606
|
+
}
|
|
1607
|
+
if (recJpegEnc_) {
|
|
1608
|
+
jpeg_del_encoder_engine(recJpegEnc_);
|
|
1609
|
+
recJpegEnc_ = nullptr;
|
|
1610
|
+
}
|
|
1611
|
+
ESP_LOGI(kTag, "recording stopped: %d frames, %.0f ms", recFrameCount_, durationMs);
|
|
1612
|
+
return durationMs;
|
|
1613
|
+
#else
|
|
1614
|
+
return -1.0;
|
|
1615
|
+
#endif
|
|
1616
|
+
}
|
|
1617
|
+
|
|
1618
|
+
} // namespace gea::platform::esp32_p4_waveshare::camera
|
|
1619
|
+
|
|
1620
|
+
namespace gea::platform::camera {
|
|
1621
|
+
|
|
1622
|
+
bool Camera::isAvailable()
|
|
1623
|
+
{
|
|
1624
|
+
return esp32_p4_waveshare::camera::CameraDriver::instance().isAvailable();
|
|
1625
|
+
}
|
|
1626
|
+
|
|
1627
|
+
bool Camera::hasPermission()
|
|
1628
|
+
{
|
|
1629
|
+
return esp32_p4_waveshare::camera::CameraDriver::instance().hasPermission();
|
|
1630
|
+
}
|
|
1631
|
+
|
|
1632
|
+
bool Camera::requestPermission()
|
|
1633
|
+
{
|
|
1634
|
+
return esp32_p4_waveshare::camera::CameraDriver::instance().requestPermission();
|
|
1635
|
+
}
|
|
1636
|
+
|
|
1637
|
+
bool Camera::open(const std::string &facingHint, int preferredWidth, int preferredHeight)
|
|
1638
|
+
{
|
|
1639
|
+
return esp32_p4_waveshare::camera::CameraDriver::instance().open(facingHint, preferredWidth, preferredHeight);
|
|
1640
|
+
}
|
|
1641
|
+
|
|
1642
|
+
void Camera::close()
|
|
1643
|
+
{
|
|
1644
|
+
esp32_p4_waveshare::camera::CameraDriver::instance().close();
|
|
1645
|
+
}
|
|
1646
|
+
|
|
1647
|
+
bool Camera::isOpen()
|
|
1648
|
+
{
|
|
1649
|
+
return esp32_p4_waveshare::camera::CameraDriver::instance().isOpen();
|
|
1650
|
+
}
|
|
1651
|
+
|
|
1652
|
+
int Camera::width()
|
|
1653
|
+
{
|
|
1654
|
+
return esp32_p4_waveshare::camera::CameraDriver::instance().width();
|
|
1655
|
+
}
|
|
1656
|
+
|
|
1657
|
+
int Camera::height()
|
|
1658
|
+
{
|
|
1659
|
+
return esp32_p4_waveshare::camera::CameraDriver::instance().height();
|
|
1660
|
+
}
|
|
1661
|
+
|
|
1662
|
+
int Camera::orientation()
|
|
1663
|
+
{
|
|
1664
|
+
return esp32_p4_waveshare::camera::CameraDriver::instance().orientation();
|
|
1665
|
+
}
|
|
1666
|
+
|
|
1667
|
+
Facing Camera::currentFacing()
|
|
1668
|
+
{
|
|
1669
|
+
return esp32_p4_waveshare::camera::CameraDriver::instance().currentFacing();
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
std::string Camera::currentFacingString()
|
|
1673
|
+
{
|
|
1674
|
+
return esp32_p4_waveshare::camera::CameraDriver::instance().currentFacingString();
|
|
1675
|
+
}
|
|
1676
|
+
|
|
1677
|
+
int Camera::deviceCount()
|
|
1678
|
+
{
|
|
1679
|
+
return esp32_p4_waveshare::camera::CameraDriver::instance().deviceCount();
|
|
1680
|
+
}
|
|
1681
|
+
|
|
1682
|
+
DeviceInfo Camera::deviceAt(int index)
|
|
1683
|
+
{
|
|
1684
|
+
return esp32_p4_waveshare::camera::CameraDriver::instance().deviceAt(index);
|
|
1685
|
+
}
|
|
1686
|
+
|
|
1687
|
+
void Camera::drawPreview(int x, int y, int destW, int destH)
|
|
1688
|
+
{
|
|
1689
|
+
esp32_p4_waveshare::camera::CameraDriver::instance().drawPreview(x, y, destW, destH);
|
|
1690
|
+
}
|
|
1691
|
+
|
|
1692
|
+
int Camera::capture(bool mirror)
|
|
1693
|
+
{
|
|
1694
|
+
return esp32_p4_waveshare::camera::CameraDriver::instance().capture(mirror);
|
|
1695
|
+
}
|
|
1696
|
+
|
|
1697
|
+
void Camera::setFlash(const std::string &mode)
|
|
1698
|
+
{
|
|
1699
|
+
esp32_p4_waveshare::camera::CameraDriver::instance().setFlash(mode);
|
|
1700
|
+
}
|
|
1701
|
+
|
|
1702
|
+
void Camera::setZoom(double factor)
|
|
1703
|
+
{
|
|
1704
|
+
esp32_p4_waveshare::camera::CameraDriver::instance().setZoom(factor);
|
|
1705
|
+
}
|
|
1706
|
+
|
|
1707
|
+
void Camera::setMirror(bool mirror)
|
|
1708
|
+
{
|
|
1709
|
+
esp32_p4_waveshare::camera::CameraDriver::instance().setMirror(mirror);
|
|
1710
|
+
}
|
|
1711
|
+
|
|
1712
|
+
int Camera::previewMode()
|
|
1713
|
+
{
|
|
1714
|
+
return esp32_p4_waveshare::camera::CameraDriver::instance().previewMode();
|
|
1715
|
+
}
|
|
1716
|
+
|
|
1717
|
+
bool Camera::fillPreview(std::uint16_t *dst, int dstWidth, int dstHeight, int fit, bool mirror)
|
|
1718
|
+
{
|
|
1719
|
+
return esp32_p4_waveshare::camera::CameraDriver::instance().fillPreview(dst, dstWidth, dstHeight, fit, mirror);
|
|
1720
|
+
}
|
|
1721
|
+
|
|
1722
|
+
void Camera::positionPreviewLayer(int x, int y, int width, int height)
|
|
1723
|
+
{
|
|
1724
|
+
esp32_p4_waveshare::camera::CameraDriver::instance().positionPreviewLayer(x, y, width, height);
|
|
1725
|
+
}
|
|
1726
|
+
|
|
1727
|
+
void Camera::hidePreviewLayer()
|
|
1728
|
+
{
|
|
1729
|
+
esp32_p4_waveshare::camera::CameraDriver::instance().hidePreviewLayer();
|
|
1730
|
+
}
|
|
1731
|
+
|
|
1732
|
+
void Camera::presentNativeOverlay()
|
|
1733
|
+
{
|
|
1734
|
+
esp32_p4_waveshare::camera::CameraDriver::instance().presentNativeOverlay();
|
|
1735
|
+
}
|
|
1736
|
+
|
|
1737
|
+
bool Camera::startRecording(const std::string &path, double fps)
|
|
1738
|
+
{
|
|
1739
|
+
return esp32_p4_waveshare::camera::CameraDriver::instance().startRecording(path, fps);
|
|
1740
|
+
}
|
|
1741
|
+
|
|
1742
|
+
double Camera::stopRecording()
|
|
1743
|
+
{
|
|
1744
|
+
return esp32_p4_waveshare::camera::CameraDriver::instance().stopRecording();
|
|
1745
|
+
}
|
|
1746
|
+
|
|
1747
|
+
bool Camera::isRecording()
|
|
1748
|
+
{
|
|
1749
|
+
return esp32_p4_waveshare::camera::CameraDriver::instance().isRecording();
|
|
1750
|
+
}
|
|
1751
|
+
|
|
1752
|
+
void Camera::setExposure(const std::string &mode, double bias, double iso, double durationMs)
|
|
1753
|
+
{
|
|
1754
|
+
esp32_p4_waveshare::camera::CameraDriver::instance().setExposure(mode, bias, iso, durationMs);
|
|
1755
|
+
}
|
|
1756
|
+
|
|
1757
|
+
void Camera::setWhiteBalance(const std::string &mode, double temperatureK, double tint)
|
|
1758
|
+
{
|
|
1759
|
+
esp32_p4_waveshare::camera::CameraDriver::instance().setWhiteBalance(mode, temperatureK, tint);
|
|
1760
|
+
}
|
|
1761
|
+
|
|
1762
|
+
void Camera::setFocus(const std::string &mode, double pointX, double pointY)
|
|
1763
|
+
{
|
|
1764
|
+
esp32_p4_waveshare::camera::CameraDriver::instance().setFocus(mode, pointX, pointY);
|
|
1765
|
+
}
|
|
1766
|
+
|
|
1767
|
+
void Camera::setTorch(const std::string &mode, double level)
|
|
1768
|
+
{
|
|
1769
|
+
esp32_p4_waveshare::camera::CameraDriver::instance().setTorch(mode, level);
|
|
1770
|
+
}
|
|
1771
|
+
|
|
1772
|
+
} // namespace gea::platform::camera
|
|
1773
|
+
|
|
1774
|
+
// ---------------------------------------------------------------------------
|
|
1775
|
+
// GEADEV dev-command extension (ESP32-P4 only). Streams a captured still / clip
|
|
1776
|
+
// out over the serial console as base64, framed like `GEADEV SCREENSHOT`. This
|
|
1777
|
+
// is the strong override of the weak `geaHandleExtraDevCommand` hook declared in
|
|
1778
|
+
// targets/esp32/services/device_control.cpp; targets without a camera keep the
|
|
1779
|
+
// weak no-op, so this adds no commands there.
|
|
1780
|
+
//
|
|
1781
|
+
// GEADEV CAMSTILL -> GEADEV:CAMSTILL BEGIN ... / GEADEV:DATA <b64> / END (full-res JPEG)
|
|
1782
|
+
// GEADEV CAMCLIP [secs] -> GEADEV:CAMCLIP BEGIN ... / GEADEV:DATA <b64> / END (1080p H.264, default 10s)
|
|
1783
|
+
// ---------------------------------------------------------------------------
|
|
1784
|
+
namespace {
|
|
1785
|
+
|
|
1786
|
+
// zlib-compatible CRC-32 (poly 0xEDB88820, init/final ~0). Lets the host verify each
|
|
1787
|
+
// pulled chunk with Python's zlib.crc32 and re-request only the bad ones.
|
|
1788
|
+
std::uint32_t crc32z(const std::uint8_t *p, std::size_t n)
|
|
1789
|
+
{
|
|
1790
|
+
std::uint32_t c = 0xFFFFFFFFu;
|
|
1791
|
+
for (std::size_t i = 0; i < n; ++i) {
|
|
1792
|
+
c ^= p[i];
|
|
1793
|
+
for (int k = 0; k < 8; ++k) c = (c >> 1) ^ (0xEDB88820u & (0u - (c & 1u)));
|
|
1794
|
+
}
|
|
1795
|
+
return c ^ 0xFFFFFFFFu;
|
|
1796
|
+
}
|
|
1797
|
+
|
|
1798
|
+
// Emit `len` bytes as base64 in 76-char `GEADEV:DATA` lines. Caller owns the framing
|
|
1799
|
+
// (BEGIN/END), the stdout lock and the log suppression.
|
|
1800
|
+
void emitBase64(const std::uint8_t *data, std::size_t len)
|
|
1801
|
+
{
|
|
1802
|
+
static const char kAlphabet[] =
|
|
1803
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
1804
|
+
char line[80];
|
|
1805
|
+
int col = 0;
|
|
1806
|
+
auto emit = [&](char c) {
|
|
1807
|
+
line[col++] = c;
|
|
1808
|
+
if (col >= 76) {
|
|
1809
|
+
std::printf("GEADEV:DATA ");
|
|
1810
|
+
std::fwrite(line, 1, static_cast<std::size_t>(col), stdout);
|
|
1811
|
+
std::printf("\n");
|
|
1812
|
+
col = 0;
|
|
1813
|
+
}
|
|
1814
|
+
};
|
|
1815
|
+
std::size_t i = 0;
|
|
1816
|
+
for (; i + 3 <= len; i += 3) {
|
|
1817
|
+
const std::uint32_t v = (static_cast<std::uint32_t>(data[i]) << 16) |
|
|
1818
|
+
(static_cast<std::uint32_t>(data[i + 1]) << 8) |
|
|
1819
|
+
static_cast<std::uint32_t>(data[i + 2]);
|
|
1820
|
+
emit(kAlphabet[(v >> 18) & 0x3f]);
|
|
1821
|
+
emit(kAlphabet[(v >> 12) & 0x3f]);
|
|
1822
|
+
emit(kAlphabet[(v >> 6) & 0x3f]);
|
|
1823
|
+
emit(kAlphabet[v & 0x3f]);
|
|
1824
|
+
}
|
|
1825
|
+
const std::size_t rem = len - i;
|
|
1826
|
+
if (rem == 1) {
|
|
1827
|
+
const std::uint32_t v = static_cast<std::uint32_t>(data[i]) << 16;
|
|
1828
|
+
emit(kAlphabet[(v >> 18) & 0x3f]);
|
|
1829
|
+
emit(kAlphabet[(v >> 12) & 0x3f]);
|
|
1830
|
+
emit('=');
|
|
1831
|
+
emit('=');
|
|
1832
|
+
} else if (rem == 2) {
|
|
1833
|
+
const std::uint32_t v = (static_cast<std::uint32_t>(data[i]) << 16) |
|
|
1834
|
+
(static_cast<std::uint32_t>(data[i + 1]) << 8);
|
|
1835
|
+
emit(kAlphabet[(v >> 18) & 0x3f]);
|
|
1836
|
+
emit(kAlphabet[(v >> 12) & 0x3f]);
|
|
1837
|
+
emit(kAlphabet[(v >> 6) & 0x3f]);
|
|
1838
|
+
emit('=');
|
|
1839
|
+
}
|
|
1840
|
+
if (col > 0) {
|
|
1841
|
+
std::printf("GEADEV:DATA ");
|
|
1842
|
+
std::fwrite(line, 1, static_cast<std::size_t>(col), stdout);
|
|
1843
|
+
std::printf("\n");
|
|
1844
|
+
}
|
|
1845
|
+
}
|
|
1846
|
+
|
|
1847
|
+
// Stream a byte range [offset, offset+reqLen) of an SD file as a CRC-tagged base64
|
|
1848
|
+
// block: `GEADEV:GET BEGIN ... crc=XX` / `GEADEV:DATA ...` / `GEADEV:GET END`. The
|
|
1849
|
+
// host verifies the CRC and re-requests the chunk on mismatch -> bit-perfect pull.
|
|
1850
|
+
// ESP_LOG is silenced for the block so the render task's log lines can't corrupt it.
|
|
1851
|
+
void streamFileRange(const char *path, long offset, std::size_t reqLen)
|
|
1852
|
+
{
|
|
1853
|
+
std::FILE *f = std::fopen(path, "rb");
|
|
1854
|
+
if (!f) {
|
|
1855
|
+
std::printf("GEADEV:ERR GET no-file path=%s\n", path);
|
|
1856
|
+
std::fflush(stdout);
|
|
1857
|
+
return;
|
|
1858
|
+
}
|
|
1859
|
+
if (reqLen == 0 || reqLen > 1u * 1024u * 1024u) reqLen = 256u * 1024u;
|
|
1860
|
+
auto *buf = static_cast<std::uint8_t *>(heap_caps_malloc(reqLen, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
|
|
1861
|
+
if (!buf) {
|
|
1862
|
+
std::fclose(f);
|
|
1863
|
+
std::printf("GEADEV:ERR GET no-mem bytes=%u\n", static_cast<unsigned>(reqLen));
|
|
1864
|
+
std::fflush(stdout);
|
|
1865
|
+
return;
|
|
1866
|
+
}
|
|
1867
|
+
std::fseek(f, offset, SEEK_SET);
|
|
1868
|
+
const std::size_t got = std::fread(buf, 1, reqLen, f);
|
|
1869
|
+
std::fclose(f);
|
|
1870
|
+
const std::uint32_t crc = crc32z(buf, got);
|
|
1871
|
+
|
|
1872
|
+
esp_log_level_set("*", ESP_LOG_NONE);
|
|
1873
|
+
flockfile(stdout);
|
|
1874
|
+
std::printf("GEADEV:GET BEGIN path=%s offset=%ld len=%u crc=%08x\n",
|
|
1875
|
+
path, offset, static_cast<unsigned>(got), static_cast<unsigned>(crc));
|
|
1876
|
+
emitBase64(buf, got);
|
|
1877
|
+
std::printf("GEADEV:GET END\n");
|
|
1878
|
+
std::fflush(stdout);
|
|
1879
|
+
funlockfile(stdout);
|
|
1880
|
+
esp_log_level_set("*", ESP_LOG_INFO); // restore the dev-default log level
|
|
1881
|
+
heap_caps_free(buf);
|
|
1882
|
+
}
|
|
1883
|
+
|
|
1884
|
+
} // namespace
|
|
1885
|
+
|
|
1886
|
+
extern "C" bool geaHandleExtraDevCommand(const char *command, char *args)
|
|
1887
|
+
{
|
|
1888
|
+
using gea::platform::esp32_p4_waveshare::camera::CameraDriver;
|
|
1889
|
+
if (!command) return false;
|
|
1890
|
+
|
|
1891
|
+
// Capture commands write to the SD card and return only metadata; the host then
|
|
1892
|
+
// pulls the file with GET (chunked + CRC-verified). This keeps the clip clean and
|
|
1893
|
+
// uncapped (no PSRAM buffer) and the transfer bit-perfect.
|
|
1894
|
+
if (std::strcmp(command, "CAMSTILL") == 0) {
|
|
1895
|
+
std::size_t bytes = 0;
|
|
1896
|
+
int w = 0, h = 0;
|
|
1897
|
+
if (!CameraDriver::instance().captureStillToSd("/sdcard/frame.jpg", &bytes, &w, &h)) {
|
|
1898
|
+
std::printf("GEADEV:ERR CAMSTILL capture-failed\n");
|
|
1899
|
+
} else {
|
|
1900
|
+
std::printf("GEADEV:CAMSTILL OK path=/sdcard/frame.jpg bytes=%u width=%d height=%d\n",
|
|
1901
|
+
static_cast<unsigned>(bytes), w, h);
|
|
1902
|
+
}
|
|
1903
|
+
std::fflush(stdout);
|
|
1904
|
+
return true;
|
|
1905
|
+
}
|
|
1906
|
+
|
|
1907
|
+
if (std::strcmp(command, "CAMCLIP") == 0) {
|
|
1908
|
+
double seconds = 10.0;
|
|
1909
|
+
if (args && *args) {
|
|
1910
|
+
const double v = std::atof(args);
|
|
1911
|
+
if (v >= 0.5 && v <= 30.0) seconds = v;
|
|
1912
|
+
}
|
|
1913
|
+
std::size_t bytes = 0;
|
|
1914
|
+
int w = 0, h = 0, frames = 0;
|
|
1915
|
+
double fps = 0.0;
|
|
1916
|
+
if (!CameraDriver::instance().recordClipToSd(seconds, "/sdcard/clip.h264", &bytes, &w, &h, &fps, &frames)) {
|
|
1917
|
+
std::printf("GEADEV:ERR CAMCLIP record-failed\n");
|
|
1918
|
+
} else {
|
|
1919
|
+
std::printf("GEADEV:CAMCLIP OK path=/sdcard/clip.h264 bytes=%u frames=%d fps=%.2f width=%d height=%d\n",
|
|
1920
|
+
static_cast<unsigned>(bytes), frames, fps, w, h);
|
|
1921
|
+
}
|
|
1922
|
+
std::fflush(stdout);
|
|
1923
|
+
return true;
|
|
1924
|
+
}
|
|
1925
|
+
|
|
1926
|
+
if (std::strcmp(command, "STAT") == 0) {
|
|
1927
|
+
char p[160] = {0};
|
|
1928
|
+
if (!args || std::sscanf(args, "%159s", p) != 1) {
|
|
1929
|
+
std::printf("GEADEV:ERR STAT usage=GEADEV_STAT_path\n");
|
|
1930
|
+
} else {
|
|
1931
|
+
std::FILE *f = std::fopen(p, "rb");
|
|
1932
|
+
long size = -1;
|
|
1933
|
+
if (f) {
|
|
1934
|
+
std::fseek(f, 0, SEEK_END);
|
|
1935
|
+
size = std::ftell(f);
|
|
1936
|
+
std::fclose(f);
|
|
1937
|
+
}
|
|
1938
|
+
std::printf("GEADEV:STAT path=%s exists=%d size=%ld\n", p, size >= 0 ? 1 : 0, size);
|
|
1939
|
+
}
|
|
1940
|
+
std::fflush(stdout);
|
|
1941
|
+
return true;
|
|
1942
|
+
}
|
|
1943
|
+
|
|
1944
|
+
if (std::strcmp(command, "GET") == 0) {
|
|
1945
|
+
char p[160] = {0};
|
|
1946
|
+
long offset = 0;
|
|
1947
|
+
long len = 0;
|
|
1948
|
+
if (!args || std::sscanf(args, "%159s %ld %ld", p, &offset, &len) < 1 || !p[0]) {
|
|
1949
|
+
std::printf("GEADEV:ERR GET usage=GEADEV_GET_path_offset_len\n");
|
|
1950
|
+
std::fflush(stdout);
|
|
1951
|
+
return true;
|
|
1952
|
+
}
|
|
1953
|
+
streamFileRange(p, offset, static_cast<std::size_t>(len > 0 ? len : 0));
|
|
1954
|
+
return true;
|
|
1955
|
+
}
|
|
1956
|
+
|
|
1957
|
+
return false;
|
|
1958
|
+
}
|