@geastack/targets 0.1.100 → 0.1.101

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.
Files changed (47) hide show
  1. package/package.json +7 -7
  2. package/targets/cmake/geatsc-generated-archive-build.mjs +32 -1
  3. package/targets/esp32/app_project.cmake +2 -2
  4. package/targets/esp32/chip_bindings/audio/echo_cancellation.cpp +392 -28
  5. package/targets/esp32/chip_bindings/audio/echo_cancellation.h +18 -2
  6. package/targets/esp32/chip_bindings/audio/es8311.cpp +117 -13
  7. package/targets/esp32/chip_bindings/displays/co5300.cpp +68 -28
  8. package/targets/esp32/components/gea_framework/CMakeLists.txt +17 -1
  9. package/targets/esp32/components/gea_framework/idf_component.yml +4 -0
  10. package/targets/esp32/components/gea_vpx/CMakeLists.txt +1 -1
  11. package/targets/esp32/connectivity/wifi.cpp +800 -543
  12. package/targets/esp32/display.cpp +121 -26
  13. package/targets/esp32/gea_framework.cmake +22 -28
  14. package/targets/esp32/host_sources.cmake +1 -0
  15. package/targets/esp32/services/comparison_benchmark.h +137 -0
  16. package/targets/esp32/services/comparison_gesture.h +133 -0
  17. package/targets/esp32/services/comparison_input_trace.h +64 -0
  18. package/targets/esp32/services/comparison_upload_capture.h +125 -0
  19. package/targets/esp32/services/debugger.h +634 -0
  20. package/targets/esp32/services/device_control.cpp +965 -20
  21. package/targets/esp32/services/frame_scheduler.cpp +128 -4
  22. package/targets/esp32/services/heap.cpp +125 -0
  23. package/targets/esp32/services/opus_benchmark.h +1 -1
  24. package/targets/esp32/services/storage_service.cpp +119 -104
  25. package/targets/esp32-p4-m5stack-tab5/dependencies.lock +27 -16
  26. package/targets/esp32-s3-m5stack-stopwatch/README.md +14 -9
  27. package/targets/esp32-s3-m5stack-stopwatch/dependencies.lock +54 -1
  28. package/targets/esp32-s3-m5stack-stopwatch/main/CMakeLists.txt +10 -7
  29. package/targets/esp32-s3-m5stack-stopwatch/main/buttons.cpp +80 -0
  30. package/targets/esp32-s3-m5stack-stopwatch/main/idf_component.yml +18 -0
  31. package/targets/esp32-s3-m5stack-stopwatch/main/include/board.h +38 -17
  32. package/targets/esp32-s3-m5stack-stopwatch/main/panel_reset.cpp +132 -129
  33. package/targets/esp32-s3-m5stack-stopwatch/main/power.cpp +253 -0
  34. package/targets/esp32-s3-m5stack-stopwatch/main/touch.cpp +158 -88
  35. package/targets/esp32-s3-m5stack-stopwatch/sdkconfig.defaults +13 -0
  36. package/targets/esp32-s3-touch-amoled-1.8/main/CMakeLists.txt +30 -3
  37. package/targets/esp32-s3-touch-amoled-2.06/dependencies.lock +10 -148
  38. package/targets/esp32-s31-espressif-mosaico/README.md +34 -0
  39. package/targets/esp32-s31-espressif-mosaico/dependencies.lock +49 -1
  40. package/targets/esp32-s31-espressif-mosaico/main/CMakeLists.txt +23 -1
  41. package/targets/esp32-s31-espressif-mosaico/main/board_power.cpp +117 -0
  42. package/targets/esp32-s31-espressif-mosaico/main/camera.cpp +326 -0
  43. package/targets/esp32-s31-espressif-mosaico/main/idf_component.yml +11 -0
  44. package/targets/esp32-s31-espressif-mosaico/main/include/board.h +23 -0
  45. package/targets/esp32-s31-espressif-mosaico/main/nand_mount.cpp +172 -0
  46. package/targets/esp32-s31-espressif-mosaico/partitions.csv +7 -3
  47. package/targets/esp32-s31-espressif-mosaico/sdkconfig.defaults +24 -0
@@ -27,6 +27,11 @@ set(GEA_BOARD_EXTRA_REQUIRES
27
27
  esp_codec_dev
28
28
  esp_driver_ledc
29
29
  espressif__esp_tinyusb
30
+ # nand_mount.cpp: FAT on the SPI NAND.
31
+ esp_driver_spi
32
+ fatfs
33
+ espressif__spi_nand_flash
34
+ espressif__spi_nand_flash_fatfs
30
35
  )
31
36
 
32
37
  set(GEA_BOARD_EXTRA_COMPILE_DEFINITIONS
@@ -51,6 +56,8 @@ set(GEA_BOARD_PLATFORM_SOURCES
51
56
  "${CMAKE_CURRENT_LIST_DIR}/touch.cpp"
52
57
  "${CMAKE_CURRENT_LIST_DIR}/board_power.cpp"
53
58
  "${CMAKE_CURRENT_LIST_DIR}/peer_uart.cpp"
59
+ "${CMAKE_CURRENT_LIST_DIR}/nand_mount.cpp"
60
+ "${CMAKE_CURRENT_LIST_DIR}/camera.cpp"
54
61
  "${GEA_CHIPS}/displays/co5300/co5300.cpp"
55
62
  "${CMAKE_CURRENT_LIST_DIR}/../../esp32/display.cpp"
56
63
  "${CMAKE_CURRENT_LIST_DIR}/../../esp32/chip_bindings/displays/co5300.cpp"
@@ -62,6 +69,15 @@ set(GEA_BOARD_PLATFORM_SOURCES
62
69
  "${CMAKE_CURRENT_LIST_DIR}/../../esp32/power_absent.cpp"
63
70
  )
64
71
 
72
+ # The CLI exposes the analyzed camera binding to both CMake passes and the
73
+ # component manager. Other Mosaico apps keep the camera backend absent.
74
+ if(GEA_EMBEDDED_CAPABILITY_CAMERA OR "$ENV{GEA_EMBEDDED_CAPABILITY_CAMERA}" STREQUAL "1")
75
+ list(APPEND GEA_BOARD_EXTRA_REQUIRES mosaico_module_camera esp_driver_jpeg)
76
+ list(APPEND GEA_BOARD_EXTRA_COMPILE_DEFINITIONS GEA_MOSAICO_CAMERA_ENABLED=1)
77
+ else()
78
+ list(APPEND GEA_BOARD_EXTRA_COMPILE_DEFINITIONS GEA_MOSAICO_CAMERA_ENABLED=0)
79
+ endif()
80
+
65
81
  include("${CMAKE_CURRENT_LIST_DIR}/../../esp32-s3-touch-amoled-1.8/main/CMakeLists.txt")
66
82
 
67
83
  # IDF 6.2 brings GCC 16 + picolibc: picolibc declares fflush() nonnull, so the
@@ -69,11 +85,17 @@ include("${CMAKE_CURRENT_LIST_DIR}/../../esp32-s3-touch-amoled-1.8/main/CMakeLis
69
85
  # std::move()s as redundant. Warnings, not errors, until the compiler runtime
70
86
  # and emitter are fixed for this toolchain.
71
87
  if(NOT CMAKE_BUILD_EARLY_EXPANSION)
72
- target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,-u,gea_mosaico_peer_uart_open")
73
88
  target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-error=nonnull -Wno-error=redundant-move)
89
+ # Make the optional native side-link capability discoverable through its
90
+ # weak C API without allocating or driving pins until an app opts in.
91
+ target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,-u,gea_mosaico_peer_uart_open")
74
92
  # board_power.cpp keeps a crash's registers and log across the reset (no UART0 here)
75
93
  # and makes the USB console wait for the host instead of dropping output.
76
94
  target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=esp_panic_handler"
77
95
  "-Wl,--wrap=tinyusb_cdcacm_write_queue_char"
78
96
  "-Wl,--wrap=uart_hal_write_txfifo")
97
+ # board_power.cpp rewrites an older flash partition table to this one.
98
+ idf_build_get_property(_mosaico_build_dir BUILD_DIR)
99
+ target_add_binary_data(${COMPONENT_LIB} "${_mosaico_build_dir}/partition_table/partition-table.bin" BINARY
100
+ DEPENDS partition_table_bin)
79
101
  endif()
@@ -30,6 +30,7 @@
30
30
  // the crash on the console.
31
31
 
32
32
  #include "board.h"
33
+ #include "platform/file_cache.h"
33
34
 
34
35
  #include <algorithm>
35
36
  #include <cstddef>
@@ -41,6 +42,7 @@
41
42
  #include "esp_attr.h"
42
43
  #include "esp_check.h"
43
44
  #include "esp_err.h"
45
+ #include "esp_flash.h"
44
46
  #include "esp_heap_caps.h"
45
47
  #include "esp_log.h"
46
48
  #include "esp_mac.h"
@@ -335,6 +337,84 @@ namespace
335
337
  }
336
338
  #endif
337
339
 
340
+ // The partition table this firmware was built with (main/CMakeLists.txt
341
+ // embeds the build's partition-table.bin). A board flashed with an older
342
+ // layout keeps it until something rewrites 0x8000, and on this board the only
343
+ // other writer is the ROM downloader, which needs BOOT held through a power
344
+ // cycle -- so the app does it, once.
345
+ extern "C" const std::uint8_t partitionTableStart[] asm("_binary_partition_table_bin_start");
346
+ extern "C" const std::uint8_t partitionTableEnd[] asm("_binary_partition_table_bin_end");
347
+
348
+ std::uint32_t readLe32(const std::uint8_t *p)
349
+ {
350
+ return static_cast<std::uint32_t>(p[0]) | (static_cast<std::uint32_t>(p[1]) << 8) |
351
+ (static_cast<std::uint32_t>(p[2]) << 16) | (static_cast<std::uint32_t>(p[3]) << 24);
352
+ }
353
+
354
+ // The built table's entry of `type`/`subtype` (32-byte entries, magic 0x50AA).
355
+ bool builtPartition(std::uint8_t type, std::uint8_t subtype, std::uint32_t &offset, std::uint32_t &size)
356
+ {
357
+ for (const std::uint8_t *entry = partitionTableStart; entry + 32 <= partitionTableEnd; entry += 32)
358
+ {
359
+ if (entry[0] != 0xAA || entry[1] != 0x50)
360
+ break;
361
+ if (entry[2] == type && entry[3] == subtype)
362
+ {
363
+ offset = readLe32(entry + 4);
364
+ size = readLe32(entry + 8);
365
+ return true;
366
+ }
367
+ }
368
+ return false;
369
+ }
370
+
371
+ // Rewrites the flash partition table to the built one when they differ.
372
+ // Only from the first app slot, and only when that slot starts at the same
373
+ // address in both layouts: the running image then never moves. Elsewhere it
374
+ // says so and waits for the next install, which lands in that slot. The
375
+ // otadata is erased with it, so the bootloader boots the first app slot --
376
+ // this one -- whatever the old layout's sequence numbers said.
377
+ void migratePartitionTable()
378
+ {
379
+ constexpr std::uint32_t kTableOffset = CONFIG_PARTITION_TABLE_OFFSET;
380
+ constexpr std::uint32_t kSector = 0x1000;
381
+ const std::size_t bytes = static_cast<std::size_t>(partitionTableEnd - partitionTableStart);
382
+ if (bytes == 0 || bytes > kSector)
383
+ return;
384
+ static std::uint8_t flashed[kSector];
385
+ if (esp_flash_read(nullptr, flashed, kTableOffset, bytes) != ESP_OK)
386
+ return;
387
+ bool same = true;
388
+ for (std::size_t i = 0; i < bytes && same; ++i)
389
+ same = flashed[i] == partitionTableStart[i];
390
+ if (same)
391
+ return;
392
+ std::uint32_t firstApp = 0, firstAppSize = 0, otadata = 0, otadataSize = 0;
393
+ if (!builtPartition(0x00, 0x10, firstApp, firstAppSize) || !builtPartition(0x01, 0x00, otadata, otadataSize))
394
+ return;
395
+ const esp_partition_t *running = esp_ota_get_running_partition();
396
+ if (running == nullptr || running->address != firstApp)
397
+ {
398
+ ESP_LOGW(kTag, "partition table is an older layout; it is rewritten when the app runs from 0x%lx (install once more)",
399
+ static_cast<unsigned long>(firstApp));
400
+ return;
401
+ }
402
+ ESP_LOGW(kTag, "rewriting the partition table to this firmware's layout");
403
+ esp_err_t err = esp_flash_erase_region(nullptr, kTableOffset, kSector);
404
+ if (err == ESP_OK)
405
+ err = esp_flash_write(nullptr, partitionTableStart, kTableOffset, bytes);
406
+ if (err == ESP_OK)
407
+ err = esp_flash_erase_region(nullptr, otadata, otadataSize);
408
+ if (err != ESP_OK)
409
+ {
410
+ ESP_LOGE(kTag, "partition table rewrite failed: %s", esp_err_to_name(err));
411
+ return;
412
+ }
413
+ ESP_LOGW(kTag, "partition table rewritten; restarting into it");
414
+ vTaskDelay(pdMS_TO_TICKS(200));
415
+ esp_restart();
416
+ }
417
+
338
418
  void boardStartTask(void *)
339
419
  {
340
420
  gea::platform::board::powerOnRails();
@@ -343,13 +423,23 @@ namespace
343
423
  if (previousBootCrashed())
344
424
  xTaskCreatePinnedToCore(panicReportTask, "mosaico_panic", 3072, nullptr, 5, nullptr, 0);
345
425
  #endif
426
+ // Mounted here rather than on first use: GEADEV PUSH writes /nand without
427
+ // asking the provider (a blank chip is formatted on this first mount).
428
+ gea::platform::storage::ensureMounted();
346
429
  #if CONFIG_BOOTLOADER_APP_ROLLBACK_CONFIRM_BY_APP
347
430
  confirmRunningApp();
348
431
  #endif
432
+ // After the confirm: the slot this rewrite keeps booting is a proven one.
433
+ migratePartitionTable();
349
434
  vTaskDelete(nullptr);
350
435
  }
351
436
  } // namespace
352
437
 
438
+ namespace gea::platform::storage
439
+ {
440
+ void installNandFileStorage();
441
+ }
442
+
353
443
  namespace gea::platform::board
354
444
  {
355
445
  void powerOnRails()
@@ -376,6 +466,7 @@ namespace gea::platform::board
376
466
  ESP_SYSTEM_INIT_FN(gea_mosaico_board_start, SECONDARY, BIT(0), 999)
377
467
  {
378
468
  powerLock = xSemaphoreCreateMutexStatic(&powerLockStorage);
469
+ gea::platform::storage::installNandFileStorage();
379
470
  if (xTaskCreatePinnedToCore(boardStartTask, "mosaico_start", 4096, nullptr, 10, nullptr, 0) != pdPASS)
380
471
  ESP_EARLY_LOGW(kTag, "board start task not created; display init will open VCC_3V3");
381
472
  return ESP_OK;
@@ -405,12 +496,36 @@ extern "C" size_t __real_tinyusb_cdcacm_write_queue_char(tinyusb_cdcacm_itf_t it
405
496
  extern "C" size_t __wrap_tinyusb_cdcacm_write_queue_char(tinyusb_cdcacm_itf_t itf, char ch)
406
497
  {
407
498
  static bool txStalled = false;
499
+ static TickType_t stalledSince = 0, lastClear = 0, lastBounce = 0;
408
500
  size_t queued = __real_tinyusb_cdcacm_write_queue_char(itf, ch);
409
501
  if (queued || txStalled || xPortInIsrContext() || xTaskGetSchedulerState() != taskSCHEDULER_RUNNING ||
410
502
  !tud_cdc_n_connected(itf))
411
503
  {
412
504
  if (queued)
413
505
  txStalled = false;
506
+ // A FIFO that stays full for seconds while a host holds the port means the
507
+ // IN endpoint stopped completing (seen after long play sessions: the port
508
+ // enumerates, PONG and logs never arrive, only a power cycle recovered).
509
+ // Drop what is queued, and if that is not enough, re-attach to the bus so
510
+ // the host re-enumerates and the console comes back on its own.
511
+ else if (txStalled && !xPortInIsrContext() && xTaskGetSchedulerState() == taskSCHEDULER_RUNNING &&
512
+ xTaskGetTickCount() - stalledSince > pdMS_TO_TICKS(2000) &&
513
+ xTaskGetTickCount() - lastClear > pdMS_TO_TICKS(2000))
514
+ {
515
+ const TickType_t now = xTaskGetTickCount();
516
+ lastClear = now;
517
+ tud_cdc_n_write_clear(itf);
518
+ if (now - lastBounce > pdMS_TO_TICKS(30000) && now - stalledSince > pdMS_TO_TICKS(6000))
519
+ {
520
+ lastBounce = now;
521
+ tud_disconnect();
522
+ vTaskDelay(pdMS_TO_TICKS(50));
523
+ tud_connect();
524
+ }
525
+ queued = __real_tinyusb_cdcacm_write_queue_char(itf, ch);
526
+ if (queued)
527
+ txStalled = false;
528
+ }
414
529
  return queued;
415
530
  }
416
531
  const TickType_t start = xTaskGetTickCount();
@@ -422,6 +537,8 @@ extern "C" size_t __wrap_tinyusb_cdcacm_write_queue_char(tinyusb_cdcacm_itf_t it
422
537
  if (queued)
423
538
  return queued;
424
539
  }
540
+ if (!txStalled)
541
+ stalledSince = xTaskGetTickCount();
425
542
  txStalled = true;
426
543
  return 0;
427
544
  }
@@ -0,0 +1,326 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #include "camera.h"
3
+ #include "display.h"
4
+ #include "image.h"
5
+ #include "host/backends.h"
6
+ #include "host/camera_frame.h"
7
+
8
+ #include <algorithm>
9
+ #include <atomic>
10
+ #include <cstdlib>
11
+ #include <cstring>
12
+ #include <mutex>
13
+ #include <vector>
14
+
15
+ #if GEA_MOSAICO_CAMERA_ENABLED
16
+ #include "driver/jpeg_encode.h"
17
+ #include "esp_log.h"
18
+ #include "esp_timer.h"
19
+ #include "freertos/FreeRTOS.h"
20
+ #include "freertos/semphr.h"
21
+ #include "freertos/task.h"
22
+ #include "mosaico_module_camera.h"
23
+
24
+ namespace gea::platform::camera {
25
+ namespace {
26
+ namespace pixel = gea::framework::graphics::pixel;
27
+ namespace frame = gea::host::camera_frame;
28
+ constexpr char kTag[] = "mosaico_camera";
29
+ constexpr std::int64_t kFrameIntervalUs = 2'000'000;
30
+
31
+ class CameraDriver final : public CameraFrameProvider {
32
+ public:
33
+ mosaico_camera_handle_t camera = nullptr;
34
+ jpeg_encoder_handle_t encoder = nullptr;
35
+ std::uint8_t *rgb = nullptr;
36
+ std::uint8_t *jpeg = nullptr;
37
+ std::size_t rgbCapacity = 0, jpegCapacity = 0;
38
+ frame::Size size;
39
+ std::atomic<bool> running{false};
40
+ std::atomic<bool> mirror{false};
41
+ SemaphoreHandle_t exited = nullptr;
42
+ std::mutex operations;
43
+ std::mutex lock;
44
+ std::string latest;
45
+ std::vector<pixel::native_t> pixels;
46
+
47
+ std::string takeFrameDataUrl() override {
48
+ std::unique_lock guard(lock, std::try_to_lock);
49
+ if (!guard.owns_lock()) return {};
50
+ std::string result;
51
+ result.swap(latest);
52
+ return result;
53
+ }
54
+
55
+ static void captureTask(void *opaque) {
56
+ auto &self = *static_cast<CameraDriver *>(opaque);
57
+ std::int64_t nextEncode = 0;
58
+ while (self.running.load()) {
59
+ mosaico_camera_frame_t source{};
60
+ const auto got = mosaico_camera_get_frame(self.camera, &source);
61
+ if (got != ESP_OK) {
62
+ vTaskDelay(pdMS_TO_TICKS(10));
63
+ continue;
64
+ }
65
+ const auto now = esp_timer_get_time();
66
+ bool converted = false;
67
+ if (now >= nextEncode && source.pixel_format == V4L2_PIX_FMT_UYVY) {
68
+ nextEncode = now + kFrameIntervalUs;
69
+ const std::size_t stride = source.bytes_per_line ? source.bytes_per_line : source.width * 2;
70
+ converted = frame::uyvyToRgb888Ccw90(
71
+ static_cast<const std::uint8_t *>(source.data), source.size,
72
+ source.width, source.height, stride, self.rgb, self.rgbCapacity,
73
+ self.size.width, self.size.height, self.mirror.load());
74
+ }
75
+ // Return every sensor buffer before codec work; draining continues even
76
+ // while the app is disconnected or its UI never asks for another frame.
77
+ const auto returned = mosaico_camera_return_frame(self.camera, &source);
78
+ if (returned != ESP_OK) {
79
+ ESP_LOGE(kTag, "Return camera buffer failed: %s", esp_err_to_name(returned));
80
+ self.running.store(false);
81
+ break;
82
+ }
83
+ if (!converted || !self.running.load()) continue;
84
+ std::vector<pixel::native_t> preview(std::size_t(self.size.width) * self.size.height);
85
+ for (std::size_t i = 0; i < preview.size(); ++i) {
86
+ auto *color = self.rgb + i * 3;
87
+ preview[i] = pixel::nativeColor(color[0], color[1], color[2]);
88
+ // ESP-IDF's RGB888 encoder input is explicitly BGR24 (jpeg_types.h).
89
+ std::swap(color[0], color[2]);
90
+ }
91
+ jpeg_encode_cfg_t config{};
92
+ config.width = self.size.width;
93
+ config.height = self.size.height;
94
+ config.src_type = JPEG_ENCODE_IN_FORMAT_RGB888;
95
+ config.sub_sample = JPEG_DOWN_SAMPLING_YUV420;
96
+ config.image_quality = 70;
97
+ std::uint32_t jpegSize = 0;
98
+ const auto encoded = jpeg_encoder_process(self.encoder, &config, self.rgb,
99
+ self.size.width * self.size.height * 3, self.jpeg, self.jpegCapacity, &jpegSize);
100
+ if (encoded != ESP_OK) {
101
+ ESP_LOGW(kTag, "JPEG encode failed: %s", esp_err_to_name(encoded));
102
+ continue;
103
+ }
104
+ auto url = frame::jpegDataUrl(self.jpeg, jpegSize);
105
+ if (url.empty()) continue;
106
+ std::lock_guard guard(self.lock);
107
+ self.latest.swap(url);
108
+ self.pixels.swap(preview);
109
+ }
110
+ xSemaphoreGive(self.exited);
111
+ vTaskDelete(nullptr);
112
+ }
113
+
114
+ void closeResources() {
115
+ Camera::setFrameProvider(nullptr);
116
+ running.store(false);
117
+ if (exited) {
118
+ // Sensor reads have a 100ms deadline and JPEG has a 500ms deadline.
119
+ // If a driver nevertheless stalls, preserve its live buffers/handles
120
+ // and let a later close/open retry cleanup instead of hanging the app.
121
+ if (xSemaphoreTake(exited, pdMS_TO_TICKS(1500)) != pdTRUE) {
122
+ ESP_LOGE(kTag, "Camera worker did not stop within 1500ms; resources retained");
123
+ return;
124
+ }
125
+ vSemaphoreDelete(exited);
126
+ exited = nullptr;
127
+ }
128
+ if (camera) {
129
+ const auto result = mosaico_camera_del(camera);
130
+ if (result == ESP_OK) {
131
+ camera = nullptr;
132
+ } else {
133
+ // The SDK retains its context when teardown fails; never erase the
134
+ // sole handle to that still-owned device or its outstanding buffers.
135
+ ESP_LOGE(kTag, "Camera cleanup failed: %s", esp_err_to_name(result));
136
+ }
137
+ }
138
+ if (!camera) mosaico_module_mgr_deinit();
139
+ if (encoder) { jpeg_del_encoder_engine(encoder); encoder = nullptr; }
140
+ std::free(rgb); rgb = nullptr; rgbCapacity = 0;
141
+ std::free(jpeg); jpeg = nullptr; jpegCapacity = 0;
142
+ std::lock_guard guard(lock);
143
+ std::string().swap(latest);
144
+ std::vector<pixel::native_t>().swap(pixels);
145
+ size = {};
146
+ }
147
+
148
+ void close() {
149
+ std::lock_guard operation(operations);
150
+ closeResources();
151
+ }
152
+
153
+ bool open() {
154
+ std::lock_guard operation(operations);
155
+ if (running.load()) return true;
156
+ closeResources();
157
+ if (camera) return false;
158
+ mosaico_camera_config_t config = MOSAICO_CAMERA_DEFAULT_CONFIG();
159
+ // The two sensors have different supported sizes. Let the SDK detect
160
+ // either sensor's UYVY default, then downsample for upload on this worker.
161
+ config.frame_timeout_ms = 100;
162
+ auto result = mosaico_camera_new(&config, &camera);
163
+ if (result == ESP_OK) result = mosaico_camera_open(camera);
164
+ mosaico_camera_info_t info{};
165
+ if (result == ESP_OK) result = mosaico_camera_get_info(camera, &info);
166
+ if (result == ESP_OK) {
167
+ std::lock_guard guard(lock);
168
+ size = frame::uploadSize(info.width, info.height);
169
+ if (info.pixel_format != V4L2_PIX_FMT_UYVY || size.width == 0) result = ESP_ERR_NOT_SUPPORTED;
170
+ }
171
+ if (result == ESP_OK) {
172
+ jpeg_encode_memory_alloc_cfg_t memory{};
173
+ memory.buffer_direction = JPEG_ENC_ALLOC_INPUT_BUFFER;
174
+ rgb = static_cast<std::uint8_t *>(jpeg_alloc_encoder_mem(size.width * size.height * 3, &memory, &rgbCapacity));
175
+ memory.buffer_direction = JPEG_ENC_ALLOC_OUTPUT_BUFFER;
176
+ jpeg = static_cast<std::uint8_t *>(jpeg_alloc_encoder_mem(96 * 1024, &memory, &jpegCapacity));
177
+ if (!rgb || !jpeg) result = ESP_ERR_NO_MEM;
178
+ }
179
+ if (result == ESP_OK) {
180
+ jpeg_encode_engine_cfg_t engine{};
181
+ engine.timeout_ms = 500;
182
+ result = jpeg_new_encoder_engine(&engine, &encoder);
183
+ }
184
+ if (result == ESP_OK) result = mosaico_camera_start_stream(camera);
185
+ if (result != ESP_OK) {
186
+ ESP_LOGE(kTag, "Left-slot camera open failed: %s", esp_err_to_name(result));
187
+ closeResources();
188
+ return false;
189
+ }
190
+ exited = xSemaphoreCreateBinary();
191
+ if (!exited) { closeResources(); return false; }
192
+ running.store(true);
193
+ TaskHandle_t worker = nullptr;
194
+ if (xTaskCreatePinnedToCore(captureTask, "gea_camera", 6144, this, 2, &worker, 1) != pdPASS) {
195
+ // No worker exists to signal the completion semaphore.
196
+ vSemaphoreDelete(exited); exited = nullptr;
197
+ closeResources();
198
+ return false;
199
+ }
200
+ Camera::setFrameProvider(this);
201
+ gea::framework::camera::registerCameraSurface();
202
+ ESP_LOGI(kTag, "DVP camera streaming: %ux%u -> upright %dx%d JPEG, one frame per 2s",
203
+ unsigned(info.width), unsigned(info.height), size.width, size.height);
204
+ return true;
205
+ }
206
+ };
207
+
208
+ CameraDriver &driver() { static CameraDriver state; return state; }
209
+ } // namespace
210
+
211
+ bool Camera::isAvailable() { return true; }
212
+ bool Camera::hasPermission() { return true; }
213
+ bool Camera::requestPermission() { return true; }
214
+ bool Camera::open(const std::string &hint, int, int) {
215
+ if (hint != "back" && hint != "front" && hint != "external" && hint != "mosaico-left-dvp") return false;
216
+ return driver().open();
217
+ }
218
+ void Camera::close() { driver().close(); }
219
+ bool Camera::isOpen() { return driver().running.load(); }
220
+ int Camera::width() { auto &state = driver(); std::lock_guard guard(state.lock); return state.size.width; }
221
+ int Camera::height() { auto &state = driver(); std::lock_guard guard(state.lock); return state.size.height; }
222
+ int Camera::orientation() { return 0; }
223
+ Facing Camera::currentFacing() { return Facing::External; }
224
+ std::string Camera::currentFacingString() { return "external"; }
225
+ int Camera::deviceCount() { return 1; }
226
+ DeviceInfo Camera::deviceAt(int index) {
227
+ return index == 0 ? DeviceInfo{"mosaico-left-dvp", Facing::External, width(), height()} : DeviceInfo{};
228
+ }
229
+
230
+ bool Camera::fillPreview(pixel::native_t *dst, int w, int h, int fit, bool mirror) {
231
+ auto &state = driver();
232
+ if (!dst || w <= 0 || h <= 0) return false;
233
+ std::unique_lock guard(state.lock, std::try_to_lock);
234
+ if (!guard.owns_lock() || state.pixels.empty()) return false;
235
+ const int sw = state.size.width, sh = state.size.height;
236
+ const double scale = fit == 1 ? std::min(double(w) / sw, double(h) / sh) : std::max(double(w) / sw, double(h) / sh);
237
+ const double rw = fit == 2 ? w : sw * scale, rh = fit == 2 ? h : sh * scale;
238
+ for (int y = 0; y < h; ++y) for (int x = 0; x < w; ++x) {
239
+ const double sx = (x - (w - rw) / 2) * sw / rw, sy = (y - (h - rh) / 2) * sh / rh;
240
+ dst[y * w + x] = sx < 0 || sy < 0 || sx >= sw || sy >= sh ? pixel::native_t{} :
241
+ state.pixels[int(sy) * sw + (mirror ? sw - 1 - int(sx) : int(sx))];
242
+ }
243
+ return true;
244
+ }
245
+ void Camera::drawPreview(int x, int y, int w, int h) {
246
+ if (w <= 0) w = width();
247
+ if (h <= 0) h = height();
248
+ if (w <= 0 || h <= 0) return;
249
+ std::vector<pixel::native_t> buffer(std::size_t(w) * h);
250
+ if (!fillPreview(buffer.data(), w, h, 2, false)) return;
251
+ struct Raster { const pixel::native_t *pixels; int width; int x; int y; } raster{buffer.data(), w, x, y};
252
+ gea::platform::display::Display::streamRect(x, y, w, h,
253
+ [](pixel::native_t *dst, int columns, int rows, int column, int row, void *context) {
254
+ auto &image = *static_cast<Raster *>(context);
255
+ for (int i = 0; i < rows; ++i) std::memcpy(dst + i * columns,
256
+ image.pixels + (row - image.y + i) * image.width + column - image.x,
257
+ columns * sizeof(pixel::native_t));
258
+ }, &raster);
259
+ }
260
+ int Camera::previewMode() { return 0; }
261
+ void Camera::positionPreviewLayer(int, int, int, int) {}
262
+ void Camera::hidePreviewLayer() {}
263
+ void Camera::presentNativeOverlay() {}
264
+ int Camera::capture(bool mirror) {
265
+ const int w = width(), h = height();
266
+ if (w <= 0 || h <= 0) return -1;
267
+ auto *pixels = static_cast<pixel::native_t *>(std::malloc(std::size_t(w) * h * sizeof(pixel::native_t)));
268
+ if (!pixels) return -1;
269
+ if (!fillPreview(pixels, w, h, 2, mirror)) { std::free(pixels); return -1; }
270
+ const int image = gea::framework::graphics::ImageStore::instance().registerBuffer(pixels, w, h, -1, true);
271
+ if (image < 0) std::free(pixels);
272
+ return image;
273
+ }
274
+ bool Camera::startRecording(const std::string &, double) { return false; }
275
+ double Camera::stopRecording() { return -1; }
276
+ bool Camera::isRecording() { return false; }
277
+ void Camera::setFlash(const std::string &mode) {
278
+ auto &state = driver();
279
+ std::lock_guard operation(state.operations);
280
+ if (!state.camera) return;
281
+ if (mode == "on" || mode == "torch") mosaico_camera_flash_trigger(state.camera);
282
+ else mosaico_camera_flash_stop(state.camera);
283
+ }
284
+ void Camera::setMirror(bool mirror) { driver().mirror.store(mirror); }
285
+ void Camera::setZoom(double) {}
286
+ void Camera::setExposure(const std::string &, double, double, double) {}
287
+ void Camera::setWhiteBalance(const std::string &, double, double) {}
288
+ void Camera::setFocus(const std::string &, double, double) {}
289
+ void Camera::setTorch(const std::string &mode, double) { setFlash(mode); }
290
+ } // namespace gea::platform::camera
291
+
292
+ #else
293
+ // Apps without the camera binding do not download or link DVP/BSP components.
294
+ namespace gea::platform::camera {
295
+ bool Camera::isAvailable() { return false; }
296
+ bool Camera::hasPermission() { return false; }
297
+ bool Camera::requestPermission() { return false; }
298
+ bool Camera::open(const std::string &, int, int) { return false; }
299
+ void Camera::close() {}
300
+ bool Camera::isOpen() { return false; }
301
+ int Camera::width() { return 0; }
302
+ int Camera::height() { return 0; }
303
+ int Camera::orientation() { return 0; }
304
+ Facing Camera::currentFacing() { return Facing::External; }
305
+ std::string Camera::currentFacingString() { return "external"; }
306
+ int Camera::deviceCount() { return 0; }
307
+ DeviceInfo Camera::deviceAt(int) { return {}; }
308
+ void Camera::drawPreview(int, int, int, int) {}
309
+ int Camera::previewMode() { return 0; }
310
+ bool Camera::fillPreview(gea::framework::graphics::pixel::native_t *, int, int, int, bool) { return false; }
311
+ void Camera::positionPreviewLayer(int, int, int, int) {}
312
+ void Camera::hidePreviewLayer() {}
313
+ void Camera::presentNativeOverlay() {}
314
+ int Camera::capture(bool) { return -1; }
315
+ bool Camera::startRecording(const std::string &, double) { return false; }
316
+ double Camera::stopRecording() { return -1; }
317
+ bool Camera::isRecording() { return false; }
318
+ void Camera::setFlash(const std::string &) {}
319
+ void Camera::setZoom(double) {}
320
+ void Camera::setMirror(bool) {}
321
+ void Camera::setExposure(const std::string &, double, double, double) {}
322
+ void Camera::setWhiteBalance(const std::string &, double, double) {}
323
+ void Camera::setFocus(const std::string &, double, double) {}
324
+ void Camera::setTorch(const std::string &, double) {}
325
+ } // namespace gea::platform::camera
326
+ #endif
@@ -6,6 +6,14 @@
6
6
  # prebuilt per-chip libraries; the portable Opus build below stands in for the
7
7
  # former, and an app that asks for echo cancellation is not supported yet.
8
8
  dependencies:
9
+ # Official module ownership, sensor autodetection and DVP stability patches.
10
+ # The pinned Git checkout also supplies its relative BSP/manager components.
11
+ mosaico_module_camera:
12
+ git: https://github.com/esp-mosaico/esp-mosaico-bsp.git
13
+ version: a4bfb426afcf2911c01fdd9b6d6f024019f00f8f
14
+ path: components/mosaico_module_camera
15
+ rules:
16
+ - if: '$GEA_EMBEDDED_CAPABILITY_CAMERA == 1'
9
17
  espressif/esp_lcd_co5300: '^2.0.3'
10
18
  # The board's touch controller is a CST9220 (Espressif's board definition,
11
19
  # esp-brookesia esp_mosaico_v1_2). It may run the HYN212 protocol, which the
@@ -16,6 +24,9 @@ dependencies:
16
24
  # board_power.cpp: the USB-C port is the S31's high-speed OTG controller, so
17
25
  # the console is a TinyUSB CDC interface.
18
26
  espressif/esp_tinyusb: '^2.2.1'
27
+ # nand_mount.cpp: the board's SPI NAND as FAT storage at /nand.
28
+ espressif/spi_nand_flash: '^1.4.4'
29
+ espressif/spi_nand_flash_fatfs: '^1.1.2'
19
30
  espressif/esp_h264:
20
31
  version: '^1.4.1'
21
32
  rules:
@@ -66,6 +66,18 @@ struct PowerConfig {
66
66
  gpio_num_t powerOffRequest;
67
67
  };
68
68
 
69
+ // The 1 Gbit SPI NAND (GD5F1GM7UEYIGR) on SPI3, pins per the esp-dev-kits V1.2
70
+ // user guide. main/nand_mount.cpp mounts it as the board's file storage.
71
+ struct NandConfig {
72
+ spi_host_device_t spiHost;
73
+ gpio_num_t clk;
74
+ gpio_num_t mosi;
75
+ gpio_num_t miso;
76
+ gpio_num_t cs;
77
+ gpio_num_t wp;
78
+ gpio_num_t hold;
79
+ };
80
+
69
81
  struct LauncherButtonConfig {
70
82
  gpio_num_t pin;
71
83
  int activeLevel;
@@ -119,6 +131,16 @@ public:
119
131
  .powerAmplifier = GPIO_NUM_45,
120
132
  };
121
133
 
134
+ static constexpr NandConfig nand{
135
+ .spiHost = SPI3_HOST,
136
+ .clk = GPIO_NUM_20,
137
+ .mosi = GPIO_NUM_21,
138
+ .miso = GPIO_NUM_22,
139
+ .cs = GPIO_NUM_23,
140
+ .wp = GPIO_NUM_25,
141
+ .hold = GPIO_NUM_24,
142
+ };
143
+
122
144
  static constexpr PowerConfig power{
123
145
  .vcc3v3Enable = GPIO_NUM_60,
124
146
  .powerOffRequest = GPIO_NUM_57,
@@ -132,6 +154,7 @@ inline constexpr auto display = Board::display;
132
154
  inline constexpr auto touch = Board::touch;
133
155
  inline constexpr auto audio = Board::audio;
134
156
  inline constexpr auto power = Board::power;
157
+ inline constexpr auto nand = Board::nand;
135
158
 
136
159
  // The "AI" button on the side, active low. BOOT (GPIO61) stays the ROM's
137
160
  // download strap and is left alone.