@geastack/targets 0.1.77

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