From 056e19f2fed4ece482e116d77100fe1aa6729a95 Mon Sep 17 00:00:00 2001
From: Rames The Generic <ramesthegeneric@gmail.com>
Date: Sat, 11 Jan 2025 14:24:43 -0500
Subject: [PATCH] Finalize Babble Board Configs (#80)

* Update Babble Board Pinout and Enable Emitters

* Update Babble Board Conf

* Lower AEC value for Babble boards

* Remove Comments

* Add Firmware cropping to for the Babble Board
---
 ESP/ini/boards.ini                      | 44 +++++++++++++++++++++++--
 ESP/lib/src/io/camera/cameraHandler.cpp | 29 +++++++++++++++-
 2 files changed, 70 insertions(+), 3 deletions(-)

diff --git a/ESP/ini/boards.ini b/ESP/ini/boards.ini
index 8115cbf..868b7fc 100644
--- a/ESP/ini/boards.ini
+++ b/ESP/ini/boards.ini
@@ -190,7 +190,10 @@ upload_flags = --no-stub
 
 [env:Babble-wrooms-s3]
 board = esp32-s3-devkitc-1
-board_build.flash_mode = qio  ; qio_qspi
+board_upload.flashsize = "4MB"
+board_upload.flash_size=4MB
+board_upload.maximum_size = 4194304
+board_build.flash_mode = qio
 board_build.arduino.memory_type = qio_qspi
 build_type = debug
 build_flags = ${env.build_flags}
@@ -200,9 +203,27 @@ build_flags = ${env.build_flags}
 			 ${pinoutSWROOMBABBLES3.build_flags}
 upload_flags = --no-stub
 
+[env:Babble-wrooms-s3_release]
+board = esp32-s3-devkitc-1
+board_upload.flashsize = "4MB"
+board_upload.flash_size=4MB
+board_upload.maximum_size = 4194304
+board_build.flash_mode = qio
+board_build.arduino.memory_type = qio_qspi
+build_type = debug
+build_flags = ${env.build_flags}
+			 -DCORE_DEBUG_LEVEL=1
+			 -DDEBUG_MODE=0
+             -DSERIAL_MANAGER_USE_HIGHER_FREQUENCY
+			 ${pinoutSWROOMBABBLES3.build_flags}
+upload_flags = --no-stub
+
 [env:Babble_USB-wrooms-s3]
 board = esp32-s3-devkitc-1
-board_build.flash_mode = qio  ; qio_qspi
+board_upload.flashsize = "4MB"
+board_upload.flash_size=4MB
+board_upload.maximum_size = 4194304
+board_build.flash_mode = qio
 board_build.arduino.memory_type = qio_qspi
 build_type = debug
 build_flags = ${env.build_flags}
@@ -211,6 +232,25 @@ build_flags = ${env.build_flags}
 			 -DETVR_EYE_TRACKER_USB_API
 			 -DARDUINO_USB_MODE=1
 			 -DARDUINO_USB_CDC_ON_BOOT=1
+			 -DARDUINO_FLASH
+             -DSERIAL_MANAGER_USE_HIGHER_FREQUENCY
+			 ${pinoutSWROOMBABBLES3.build_flags}
+upload_flags = --no-stub
+
+[env:Babble_USB-wrooms-s3_release]
+board = esp32-s3-devkitc-1
+board_upload.flashsize = "4MB"
+board_upload.flash_size=4MB
+board_upload.maximum_size = 4194304
+board_build.flash_mode = qio
+board_build.arduino.memory_type = qio_qspi
+build_type = debug
+build_flags = ${env.build_flags}
+			 -DCORE_DEBUG_LEVEL=1
+			 -DDEBUG_MODE=0
+			 -DETVR_EYE_TRACKER_USB_API
+			 -DARDUINO_USB_MODE=1
+			 -DARDUINO_USB_CDC_ON_BOOT=1
              -DSERIAL_MANAGER_USE_HIGHER_FREQUENCY
 			 ${pinoutSWROOMBABBLES3.build_flags}
 upload_flags = --no-stub
diff --git a/ESP/lib/src/io/camera/cameraHandler.cpp b/ESP/lib/src/io/camera/cameraHandler.cpp
index 7b51bdb..aa9c2b3 100644
--- a/ESP/lib/src/io/camera/cameraHandler.cpp
+++ b/ESP/lib/src/io/camera/cameraHandler.cpp
@@ -108,6 +108,10 @@ void CameraHandler::setupCameraSensor() {
   camera_sensor->set_aec2(camera_sensor, 0);         // 0 = disable , 1 = enable
   camera_sensor->set_ae_level(camera_sensor, 0);     // -2 to 2
   camera_sensor->set_aec_value(camera_sensor, 300);  // 0 to 1200
+  // Use a lower aec value for babble to better isolate the face with illuminators 
+  #ifdef CONFIG_CAMERA_MODULE_SWROOM_BABBLE_S3
+    camera_sensor->set_aec_value(camera_sensor, 100);  // 0 to 1200
+  #endif
 
   // controls the gain
   camera_sensor->set_gain_ctrl(camera_sensor, 0);  // 0 = disable , 1 = enable
@@ -195,7 +199,29 @@ void CameraHandler::loadConfigData() {
   log_d("Loading camera config data done");
 }
 
-int CameraHandler::setCameraResolution(framesize_t frameSize) {
+#ifdef CONFIG_CAMERA_MODULE_SWROOM_BABBLE_S3
+int CameraHandler::setCameraResolution(framesize_t frameSize) {   // For Babble, use a firmware crop as shown by Physdude
+  if (camera_sensor->pixformat == PIXFORMAT_JPEG) {
+    try {
+        int outputSize = 240;
+
+        int baseRes = 2; //CIF 
+        int ROIsize = 240; 
+        int startPointX = 80; 
+        int startPointY = 28; 
+
+        return camera_sensor->set_res_raw(camera_sensor, baseRes, 0, 0, 0, startPointX, startPointY, ROIsize, ROIsize, outputSize, outputSize, 0, 0);
+
+    } catch (...) {
+      // they sent us a malformed or unsupported frameSize - rather than crash -
+      // tell them about it
+      return -1;
+    }
+  }
+  return -1;
+}
+#else
+int CameraHandler::setCameraResolution(framesize_t frameSize) {   // By default, use the standard method. 
   if (camera_sensor->pixformat == PIXFORMAT_JPEG) {
     try {
       return camera_sensor->set_framesize(camera_sensor, frameSize);
@@ -207,6 +233,7 @@ int CameraHandler::setCameraResolution(framesize_t frameSize) {
   }
   return -1;
 }
+#endif
 
 int CameraHandler::setVFlip(int direction) {
   return camera_sensor->set_vflip(camera_sensor, direction);