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