ClassControllCamera.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. #include "ClassControllCamera.h"
  2. #include "ClassLogFile.h"
  3. #include <stdio.h>
  4. #include "driver/gpio.h"
  5. #include "esp_timer.h"
  6. #include "esp_log.h"
  7. #include "Helper.h"
  8. #include "statusled.h"
  9. #include "CImageBasis.h"
  10. #include "psram.h"
  11. #include "server_ota.h"
  12. #include "server_GPIO.h"
  13. #include "../../include/defines.h"
  14. #include <esp_event.h>
  15. #include <esp_log.h>
  16. #include <esp_system.h>
  17. #include <nvs_flash.h>
  18. #include <sys/param.h>
  19. #include <string.h>
  20. #include <limits.h>
  21. #include <new>
  22. #include <sys/stat.h>
  23. #include "freertos/FreeRTOS.h"
  24. #include "freertos/task.h"
  25. #include "server_camera.h"
  26. #include "esp_camera.h"
  27. #include "esp_heap_caps.h"
  28. #include "driver/ledc.h"
  29. #include "MainFlowControl.h"
  30. #include "ov2640_sharpness.h"
  31. #include "ov2640_specialEffect.h"
  32. #include "ov2640_contrast_brightness.h"
  33. #if (ESP_IDF_VERSION_MAJOR >= 5)
  34. #include "soc/periph_defs.h"
  35. #include "esp_private/periph_ctrl.h"
  36. #include "soc/gpio_sig_map.h"
  37. #include "soc/gpio_periph.h"
  38. #include "soc/io_mux_reg.h"
  39. #include "esp_rom_gpio.h"
  40. #define gpio_pad_select_gpio esp_rom_gpio_pad_select_gpio
  41. #define gpio_matrix_in(a, b, c) esp_rom_gpio_connect_in_signal(a, b, c)
  42. #define gpio_matrix_out(a, b, c, d) esp_rom_gpio_connect_out_signal(a, b, c, d)
  43. #define ets_delay_us(a) esp_rom_delay_us(a)
  44. #endif
  45. CCamera Camera;
  46. camera_controll_config_temp_t CCstatus;
  47. static const char *TAG = "CAM";
  48. /* Camera live stream */
  49. #define PART_BOUNDARY "123456789000000000000987654321"
  50. static const char *_STREAM_CONTENT_TYPE = "multipart/x-mixed-replace;boundary=" PART_BOUNDARY;
  51. static const char *_STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n";
  52. static const char *_STREAM_PART = "Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n";
  53. #define DEMO_IMAGE_SIZE 30000 // Max size of demo image in bytes
  54. // Camera module bus communications frequency.
  55. // Originally: config.xclk_freq_mhz = 20000000, but this lead to visual artifacts on many modules.
  56. // See https://github.com/espressif/esp32-camera/issues/150#issuecomment-726473652 et al.
  57. #if !defined(XCLK_FREQ_MHZ)
  58. // int xclk = 8;
  59. int xclk = 20; // Orginal value
  60. #else
  61. int xclk = XCLK_FREQ_MHZ;
  62. #endif
  63. static camera_config_t camera_config = {
  64. .pin_pwdn = CAM_PIN_PWDN,
  65. .pin_reset = CAM_PIN_RESET,
  66. .pin_xclk = CAM_PIN_XCLK,
  67. .pin_sscb_sda = CAM_PIN_SIOD,
  68. .pin_sscb_scl = CAM_PIN_SIOC,
  69. .pin_d7 = CAM_PIN_D7,
  70. .pin_d6 = CAM_PIN_D6,
  71. .pin_d5 = CAM_PIN_D5,
  72. .pin_d4 = CAM_PIN_D4,
  73. .pin_d3 = CAM_PIN_D3,
  74. .pin_d2 = CAM_PIN_D2,
  75. .pin_d1 = CAM_PIN_D1,
  76. .pin_d0 = CAM_PIN_D0,
  77. .pin_vsync = CAM_PIN_VSYNC,
  78. .pin_href = CAM_PIN_HREF,
  79. .pin_pclk = CAM_PIN_PCLK,
  80. .xclk_freq_hz = (xclk * 1000000),
  81. .ledc_timer = LEDC_TIMER_0, // LEDC timer to be used for generating XCLK
  82. .ledc_channel = LEDC_CHANNEL_0, // LEDC channel to be used for generating XCLK
  83. .pixel_format = PIXFORMAT_JPEG, // YUV422,GRAYSCALE,RGB565,JPEG
  84. .frame_size = FRAMESIZE_VGA, // QQVGA-UXGA Do not use sizes above QVGA when not JPEG
  85. // .frame_size = FRAMESIZE_UXGA, //QQVGA-UXGA Do not use sizes above QVGA when not JPEG
  86. .jpeg_quality = 12, // 0-63 lower number means higher quality
  87. .fb_count = 1, // if more than one, i2s runs in continuous mode. Use only with JPEG
  88. .fb_location = CAMERA_FB_IN_PSRAM, /*!< The location where the frame buffer will be allocated */
  89. .grab_mode = CAMERA_GRAB_LATEST, // only from new esp32cam version
  90. };
  91. typedef struct
  92. {
  93. httpd_req_t *req;
  94. size_t len;
  95. } jpg_chunking_t;
  96. CCamera::CCamera(void)
  97. {
  98. #ifdef DEBUG_DETAIL_ON
  99. ESP_LOGD(TAG, "CreateClassCamera");
  100. #endif
  101. CCstatus.ImageQuality = camera_config.jpeg_quality;
  102. CCstatus.ImageFrameSize = camera_config.frame_size;
  103. SetLEDIntensity(LedIntensity);
  104. CCstatus.WaitBeforePicture = 2;
  105. ledc_init();
  106. }
  107. esp_err_t CCamera::InitCam(void)
  108. {
  109. ESP_LOGD(TAG, "Init Camera");
  110. // De-init in case it was already initialized
  111. PowerResetCamera();
  112. vTaskDelay(pdMS_TO_TICKS(200));
  113. // initialize the camera
  114. esp_err_t err = esp_camera_init(&camera_config);
  115. if (err != ESP_OK)
  116. {
  117. ESP_LOGE(TAG, "Camera Init Failed");
  118. return err;
  119. }
  120. CCstatus.CameraInitSuccessful = true;
  121. // Get a reference to the sensor
  122. sensor_t *s = esp_camera_sensor_get();
  123. if (s != NULL)
  124. {
  125. CCstatus.CamSensor_id = s->id.PID;
  126. // Dump camera module, warn for unsupported modules.
  127. switch (CCstatus.CamSensor_id)
  128. {
  129. case OV2640_PID:
  130. ESP_LOGI(TAG, "OV2640 camera module detected");
  131. break;
  132. case OV3660_PID:
  133. ESP_LOGI(TAG, "OV3660 camera module detected");
  134. break;
  135. case OV5640_PID:
  136. ESP_LOGI(TAG, "OV5640 camera module detected");
  137. break;
  138. default:
  139. ESP_LOGE(TAG, "Camera module is unknown and not properly supported!");
  140. CCstatus.CameraInitSuccessful = false;
  141. }
  142. }
  143. if (CCstatus.CameraInitSuccessful)
  144. {
  145. return ESP_OK;
  146. }
  147. else
  148. {
  149. return ESP_FAIL;
  150. }
  151. }
  152. bool CCamera::testCamera(void)
  153. {
  154. bool success;
  155. camera_fb_t *fb = esp_camera_fb_get();
  156. if (fb)
  157. {
  158. success = true;
  159. }
  160. else
  161. {
  162. success = false;
  163. }
  164. esp_camera_fb_return(fb);
  165. return success;
  166. }
  167. void CCamera::ledc_init(void)
  168. {
  169. #ifdef USE_PWM_LEDFLASH
  170. // Prepare and then apply the LEDC PWM timer configuration
  171. ledc_timer_config_t ledc_timer = {};
  172. ledc_timer.speed_mode = LEDC_MODE;
  173. ledc_timer.timer_num = LEDC_TIMER;
  174. ledc_timer.duty_resolution = LEDC_DUTY_RES;
  175. ledc_timer.freq_hz = LEDC_FREQUENCY; // Set output frequency at 5 kHz
  176. ledc_timer.clk_cfg = LEDC_AUTO_CLK;
  177. ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
  178. // Prepare and then apply the LEDC PWM channel configuration
  179. ledc_channel_config_t ledc_channel = {};
  180. ledc_channel.speed_mode = LEDC_MODE;
  181. ledc_channel.channel = LEDC_CHANNEL;
  182. ledc_channel.timer_sel = LEDC_TIMER;
  183. ledc_channel.intr_type = LEDC_INTR_DISABLE;
  184. ledc_channel.gpio_num = LEDC_OUTPUT_IO;
  185. ledc_channel.duty = 0; // Set duty to 0%
  186. ledc_channel.hpoint = 0;
  187. // ledc_channel.flags.output_invert = LEDC_OUTPUT_INVERT;
  188. ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
  189. #endif
  190. }
  191. int CCamera::SetLEDIntensity(int _intrel)
  192. {
  193. // CCstatus.ImageLedIntensity = (int)(std::min(std::max((float)0, _intrel), (float)100) / 100 * 8191)
  194. Camera.LedIntensity = (int)((float)(std::min(std::max(0, _intrel), 100)) / 100 * 8191);
  195. ESP_LOGD(TAG, "Set led_intensity to %i of 8191", Camera.LedIntensity);
  196. return Camera.LedIntensity;
  197. }
  198. bool CCamera::getCameraInitSuccessful(void)
  199. {
  200. return CCstatus.CameraInitSuccessful;
  201. }
  202. esp_err_t CCamera::setSensorDatenFromCCstatus(void)
  203. {
  204. sensor_t *s = esp_camera_sensor_get();
  205. if (s != NULL)
  206. {
  207. s->set_framesize(s, CCstatus.ImageFrameSize);
  208. // s->set_contrast(s, CCstatus.ImageContrast); // -2 to 2
  209. // s->set_brightness(s, CCstatus.ImageBrightness); // -2 to 2
  210. SetCamContrastBrightness(s, CCstatus.ImageContrast, CCstatus.ImageBrightness);
  211. s->set_saturation(s, CCstatus.ImageSaturation); // -2 to 2
  212. s->set_quality(s, CCstatus.ImageQuality); // 0 - 63
  213. // s->set_gainceiling(s, CCstatus.ImageGainceiling); // Image gain (GAINCEILING_x2, x4, x8, x16, x32, x64 or x128)
  214. SetCamGainceiling(s, CCstatus.ImageGainceiling);
  215. s->set_gain_ctrl(s, CCstatus.ImageAgc); // 0 = disable , 1 = enable
  216. s->set_exposure_ctrl(s, CCstatus.ImageAec); // 0 = disable , 1 = enable
  217. s->set_hmirror(s, CCstatus.ImageHmirror); // 0 = disable , 1 = enable
  218. s->set_vflip(s, CCstatus.ImageVflip); // 0 = disable , 1 = enable
  219. s->set_whitebal(s, CCstatus.ImageAwb); // 0 = disable , 1 = enable
  220. s->set_aec2(s, CCstatus.ImageAec2); // 0 = disable , 1 = enable
  221. s->set_aec_value(s, CCstatus.ImageAecValue); // 0 to 1200
  222. // s->set_special_effect(s, CCstatus.ImageSpecialEffect); // 0 to 6 (0 - No Effect, 1 - Negative, 2 - Grayscale, 3 - Red Tint, 4 - Green Tint, 5 - Blue Tint, 6 - Sepia)
  223. SetCamSpecialEffect(s, CCstatus.ImageSpecialEffect);
  224. s->set_wb_mode(s, CCstatus.ImageWbMode); // 0 to 4 - if awb_gain enabled (0 - Auto, 1 - Sunny, 2 - Cloudy, 3 - Office, 4 - Home)
  225. s->set_ae_level(s, CCstatus.ImageAeLevel); // -2 to 2
  226. s->set_dcw(s, CCstatus.ImageDcw); // 0 = disable , 1 = enable
  227. s->set_bpc(s, CCstatus.ImageBpc); // 0 = disable , 1 = enable
  228. s->set_wpc(s, CCstatus.ImageWpc); // 0 = disable , 1 = enable
  229. s->set_awb_gain(s, CCstatus.ImageAwbGain); // 0 = disable , 1 = enable
  230. s->set_agc_gain(s, CCstatus.ImageAgcGain); // 0 to 30
  231. s->set_raw_gma(s, CCstatus.ImageRawGma); // 0 = disable , 1 = enable
  232. s->set_lenc(s, CCstatus.ImageLenc); // 0 = disable , 1 = enable
  233. // s->set_sharpness(s, CCstatus.ImageSharpness); // auto-sharpness is not officially supported, default to 0
  234. SetCamSharpness(CCstatus.ImageAutoSharpness, CCstatus.ImageSharpness);
  235. s->set_denoise(s, CCstatus.ImageDenoiseLevel); // The OV2640 does not support it, OV3660 and OV5640 (0 to 8)
  236. TickType_t cam_xDelay = 100 / portTICK_PERIOD_MS;
  237. vTaskDelay(cam_xDelay);
  238. return ESP_OK;
  239. }
  240. else
  241. {
  242. return ESP_FAIL;
  243. }
  244. }
  245. esp_err_t CCamera::getSensorDatenToCCstatus(void)
  246. {
  247. sensor_t *s = esp_camera_sensor_get();
  248. if (s != NULL)
  249. {
  250. CCstatus.CamSensor_id = s->id.PID;
  251. CCstatus.ImageFrameSize = (framesize_t)s->status.framesize;
  252. CCstatus.ImageContrast = s->status.contrast;
  253. CCstatus.ImageBrightness = s->status.brightness;
  254. CCstatus.ImageSaturation = s->status.saturation;
  255. CCstatus.ImageQuality = s->status.quality;
  256. CCstatus.ImageGainceiling = (gainceiling_t)s->status.gainceiling;
  257. CCstatus.ImageAgc = s->status.agc;
  258. CCstatus.ImageAec = s->status.aec;
  259. CCstatus.ImageHmirror = s->status.hmirror;
  260. CCstatus.ImageVflip = s->status.vflip;
  261. CCstatus.ImageAwb = s->status.awb;
  262. CCstatus.ImageAec2 = s->status.aec2;
  263. CCstatus.ImageAecValue = s->status.aec_value;
  264. CCstatus.ImageSpecialEffect = s->status.special_effect;
  265. CCstatus.ImageWbMode = s->status.wb_mode;
  266. CCstatus.ImageAeLevel = s->status.ae_level;
  267. CCstatus.ImageDcw = s->status.dcw;
  268. CCstatus.ImageBpc = s->status.bpc;
  269. CCstatus.ImageWpc = s->status.wpc;
  270. CCstatus.ImageAwbGain = s->status.awb_gain;
  271. CCstatus.ImageAgcGain = s->status.agc_gain;
  272. CCstatus.ImageRawGma = s->status.raw_gma;
  273. CCstatus.ImageLenc = s->status.lenc;
  274. // CCstatus.ImageSharpness = s->status.sharpness; // gibt -1 zurück, da es nicht unterstützt wird
  275. CCstatus.ImageDenoiseLevel = s->status.denoise;
  276. return ESP_OK;
  277. }
  278. else
  279. {
  280. return ESP_FAIL;
  281. }
  282. }
  283. // on the OV5640, gainceiling must be set with the real value (x2>>>gainceilingLevel = 2, .... x128>>>gainceilingLevel = 128)
  284. int CCamera::SetCamGainceiling(sensor_t *s, gainceiling_t gainceilingLevel)
  285. {
  286. int ret = 0;
  287. if (CCstatus.CamSensor_id == OV2640_PID)
  288. {
  289. ret = s->set_gainceiling(s, gainceilingLevel); // Image gain (GAINCEILING_x2, x4, x8, x16, x32, x64 or x128)
  290. }
  291. else
  292. {
  293. int _level = (1 << ((int)gainceilingLevel + 1));
  294. ret = s->set_reg(s, 0x3A18, 0xFF, (_level >> 8) & 3) || s->set_reg(s, 0x3A19, 0xFF, _level & 0xFF);
  295. if (ret == 0)
  296. {
  297. // ESP_LOGD(TAG, "Set gainceiling to: %d", gainceilingLevel);
  298. s->status.gainceiling = gainceilingLevel;
  299. }
  300. }
  301. return ret;
  302. }
  303. void CCamera::SetCamSharpness(bool autoSharpnessEnabled, int sharpnessLevel)
  304. {
  305. sensor_t *s = esp_camera_sensor_get();
  306. if (s != NULL)
  307. {
  308. if (CCstatus.CamSensor_id == OV2640_PID)
  309. {
  310. sharpnessLevel = min(2, max(-2, sharpnessLevel));
  311. // The OV2640 does not officially support sharpness, so the detour is made with the ov2640_sharpness.cpp.
  312. if (autoSharpnessEnabled)
  313. {
  314. ov2640_enable_auto_sharpness(s);
  315. }
  316. else
  317. {
  318. ov2640_set_sharpness(s, sharpnessLevel);
  319. }
  320. }
  321. else
  322. {
  323. sharpnessLevel = min(3, max(-3, sharpnessLevel));
  324. // for CAMERA_OV5640 and CAMERA_OV3660
  325. if (autoSharpnessEnabled)
  326. {
  327. // autoSharpness is not supported, default to zero
  328. s->set_sharpness(s, 0);
  329. }
  330. else
  331. {
  332. s->set_sharpness(s, sharpnessLevel);
  333. }
  334. }
  335. }
  336. else
  337. {
  338. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "SetCamSharpness, Failed to get Cam control structure");
  339. }
  340. }
  341. void CCamera::SetCamSpecialEffect(sensor_t *s, int specialEffect)
  342. {
  343. if (CCstatus.CamSensor_id == OV2640_PID)
  344. {
  345. ov2640_set_special_effect(s, specialEffect);
  346. }
  347. else
  348. {
  349. s->set_special_effect(s, specialEffect);
  350. }
  351. }
  352. void CCamera::SetCamContrastBrightness(sensor_t *s, int _contrast, int _brightness)
  353. {
  354. if (CCstatus.CamSensor_id == OV2640_PID)
  355. {
  356. ov2640_set_contrast_brightness(s, _contrast, _brightness);
  357. }
  358. else
  359. {
  360. s->set_contrast(s, _contrast); // -2 to 2
  361. s->set_brightness(s, _brightness); // -2 to 2
  362. }
  363. }
  364. // - It always zooms to the image center when offsets are zero
  365. // - if imageSize = 0 then the image is not zoomed
  366. // - if imageSize = max value, then the image is fully zoomed in
  367. // - a zoom step is >>> Width + 32 px / Height + 24 px
  368. void CCamera::SanitizeZoomParams(int imageSize, int frameSizeX, int frameSizeY, int &imageWidth, int &imageHeight, int &zoomOffsetX, int &zoomOffsetY)
  369. {
  370. // for OV2640, This works only if the aspect ratio of 4:3 is preserved in the window size.
  371. // use only values divisible by 8 without remainder
  372. imageWidth = CCstatus.ImageWidth + (imageSize * 4 * 8);
  373. imageHeight = CCstatus.ImageHeight + (imageSize * 3 * 8);
  374. int _maxX = frameSizeX - imageWidth;
  375. int _maxY = frameSizeY - imageHeight;
  376. if ((abs(zoomOffsetX) * 2) > _maxX)
  377. {
  378. if (zoomOffsetX > 0)
  379. {
  380. zoomOffsetX = _maxX;
  381. }
  382. else
  383. {
  384. zoomOffsetX = 0;
  385. }
  386. }
  387. else
  388. {
  389. if (zoomOffsetX > 0)
  390. {
  391. zoomOffsetX = ((_maxX / 2) + zoomOffsetX);
  392. }
  393. else
  394. {
  395. zoomOffsetX = ((_maxX / 2) + zoomOffsetX);
  396. }
  397. }
  398. if ((abs(zoomOffsetY) * 2) > _maxY)
  399. {
  400. if (zoomOffsetY > 0)
  401. {
  402. zoomOffsetY = _maxY;
  403. }
  404. else
  405. {
  406. zoomOffsetY = 0;
  407. }
  408. }
  409. else
  410. {
  411. if (zoomOffsetY > 0)
  412. {
  413. zoomOffsetY = ((_maxY / 2) + zoomOffsetY);
  414. }
  415. else
  416. {
  417. zoomOffsetY = ((_maxY / 2) + zoomOffsetY);
  418. }
  419. }
  420. }
  421. void CCamera::SetZoomSize(bool zoomEnabled, int zoomOffsetX, int zoomOffsetY, int imageSize, int imageVflip)
  422. {
  423. sensor_t *s = esp_camera_sensor_get();
  424. if (s != NULL)
  425. {
  426. if (zoomEnabled)
  427. {
  428. int _imageSize_temp = 0;
  429. int _imageWidth = CCstatus.ImageWidth;
  430. int _imageHeight = CCstatus.ImageHeight;
  431. int _offsetx = zoomOffsetX;
  432. int _offsety = zoomOffsetY;
  433. int frameSizeX;
  434. int frameSizeY;
  435. switch (CCstatus.CamSensor_id)
  436. {
  437. case OV5640_PID:
  438. frameSizeX = 2592;
  439. frameSizeY = 1944;
  440. // max imageSize = ((frameSizeX - CCstatus.ImageWidth) / 8 / 4) - 1
  441. // 59 = ((2560 - 640) / 8 / 4) - 1
  442. if (imageSize < 59)
  443. {
  444. _imageSize_temp = (59 - imageSize);
  445. }
  446. SanitizeZoomParams(_imageSize_temp, frameSizeX, frameSizeY, _imageWidth, _imageHeight, _offsetx, _offsety);
  447. SetCamWindow(s, frameSizeX, frameSizeY, _offsetx, _offsety, _imageWidth, _imageHeight, CCstatus.ImageWidth, CCstatus.ImageHeight, imageVflip);
  448. break;
  449. case OV3660_PID:
  450. frameSizeX = 2048;
  451. frameSizeY = 1536;
  452. // max imageSize = ((frameSizeX - CCstatus.ImageWidth) / 8 / 4) -1
  453. // 43 = ((2048 - 640) / 8 / 4) - 1
  454. if (imageSize < 43)
  455. {
  456. _imageSize_temp = (43 - imageSize);
  457. }
  458. SanitizeZoomParams(_imageSize_temp, frameSizeX, frameSizeY, _imageWidth, _imageHeight, _offsetx, _offsety);
  459. SetCamWindow(s, frameSizeX, frameSizeY, _offsetx, _offsety, _imageWidth, _imageHeight, CCstatus.ImageWidth, CCstatus.ImageHeight, imageVflip);
  460. break;
  461. case OV2640_PID:
  462. frameSizeX = 1600;
  463. frameSizeY = 1200;
  464. // max imageSize = ((frameSizeX - CCstatus.ImageWidth) / 8 / 4) -1
  465. // 29 = ((1600 - 640) / 8 / 4) - 1
  466. if (imageSize < 29)
  467. {
  468. _imageSize_temp = (29 - imageSize);
  469. }
  470. SanitizeZoomParams(_imageSize_temp, frameSizeX, frameSizeY, _imageWidth, _imageHeight, _offsetx, _offsety);
  471. SetCamWindow(s, frameSizeX, frameSizeY, _offsetx, _offsety, _imageWidth, _imageHeight, CCstatus.ImageWidth, CCstatus.ImageHeight, imageVflip);
  472. break;
  473. default:
  474. // do nothing
  475. break;
  476. }
  477. }
  478. else
  479. {
  480. s->set_framesize(s, CCstatus.ImageFrameSize);
  481. }
  482. }
  483. }
  484. void CCamera::SetQualityZoomSize(int qual, framesize_t resol, bool zoomEnabled, int zoomOffsetX, int zoomOffsetY, int imageSize, int imageVflip)
  485. {
  486. sensor_t *s = esp_camera_sensor_get();
  487. // OV2640 has no lower limit on jpeg quality
  488. if (CCstatus.CamSensor_id == OV5640_PID)
  489. {
  490. qual = min(63, max(8, qual));
  491. }
  492. SetImageWidthHeightFromResolution(resol);
  493. if (s != NULL)
  494. {
  495. s->set_quality(s, qual);
  496. SetZoomSize(zoomEnabled, zoomOffsetX, zoomOffsetY, imageSize, imageVflip);
  497. }
  498. else
  499. {
  500. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "SetQualityZoomSize, Failed to get Cam control structure");
  501. }
  502. }
  503. void CCamera::SetCamWindow(sensor_t *s, int frameSizeX, int frameSizeY, int xOffset, int yOffset, int xTotal, int yTotal, int xOutput, int yOutput, int imageVflip)
  504. {
  505. if (CCstatus.CamSensor_id == OV2640_PID)
  506. {
  507. s->set_res_raw(s, 0, 0, 0, 0, xOffset, yOffset, xTotal, yTotal, xOutput, yOutput, false, false);
  508. }
  509. else
  510. {
  511. // for CAMERA_OV5640 and CAMERA_OV3660
  512. bool scale = !(xOutput == xTotal && yOutput == yTotal);
  513. bool binning = (xTotal >= (frameSizeX >> 1));
  514. if (imageVflip == true)
  515. {
  516. s->set_res_raw(s, xOffset, yOffset, xOffset + xTotal - 1, yOffset + yTotal - 1, 0, 0, frameSizeX, frameSizeY, xOutput, yOutput, scale, binning);
  517. }
  518. else
  519. {
  520. s->set_res_raw(s, xOffset, yOffset, xOffset + xTotal, yOffset + yTotal, 0, 0, frameSizeX, frameSizeY, xOutput, yOutput, scale, binning);
  521. }
  522. }
  523. }
  524. static size_t jpg_encode_stream(void *arg, size_t index, const void *data, size_t len)
  525. {
  526. jpg_chunking_t *j = (jpg_chunking_t *)arg;
  527. if (!index)
  528. {
  529. j->len = 0;
  530. }
  531. if (httpd_resp_send_chunk(j->req, (const char *)data, len) != ESP_OK)
  532. {
  533. return 0;
  534. }
  535. j->len += len;
  536. return len;
  537. }
  538. esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
  539. {
  540. if (_Image == NULL || !_Image->ImageOkay()) {
  541. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: target image is unavailable");
  542. return ESP_FAIL;
  543. }
  544. #ifdef DEBUG_DETAIL_ON
  545. LogFile.WriteHeapInfo("CaptureToBasisImage - Start");
  546. #endif
  547. LEDOnOff(true);
  548. if (delay > 0) {
  549. LightOnOff(true);
  550. vTaskDelay(pdMS_TO_TICKS(delay));
  551. }
  552. camera_fb_t *warmup = esp_camera_fb_get();
  553. if (warmup != NULL) esp_camera_fb_return(warmup);
  554. camera_fb_t *fb = esp_camera_fb_get();
  555. if (fb == NULL) {
  556. LEDOnOff(false);
  557. LightOnOff(false);
  558. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: camera capture failed");
  559. return ESP_FAIL;
  560. }
  561. uint8_t *frameData = fb->buf;
  562. size_t frameLength = fb->len;
  563. uint8_t *demoData = NULL;
  564. bool demoBufferAllocated = false;
  565. if (CCstatus.DemoMode) {
  566. if (!loadNextDemoImage(&demoData, &frameLength)) {
  567. esp_camera_fb_return(fb);
  568. LEDOnOff(false);
  569. LightOnOff(false);
  570. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: could not load demo image");
  571. return ESP_FAIL;
  572. }
  573. frameData = demoData;
  574. demoBufferAllocated = true;
  575. }
  576. CImageBasis *tmpImage = new (std::nothrow) CImageBasis("tmpImage");
  577. bool decoded = tmpImage != NULL && frameLength <= static_cast<size_t>(INT_MAX) &&
  578. tmpImage->LoadFromMemory(frameData, static_cast<int>(frameLength));
  579. if (demoBufferAllocated) free_psram_heap("Demo image frame", demoData);
  580. esp_camera_fb_return(fb);
  581. LEDOnOff(false);
  582. if (delay > 0) LightOnOff(false);
  583. if (!decoded || tmpImage == NULL || !tmpImage->ImageOkay()) {
  584. delete tmpImage;
  585. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: JPEG decode failed");
  586. LogFile.WriteHeapInfo("CaptureToBasisImage decode");
  587. return ESP_FAIL;
  588. }
  589. size_t expectedBytes = 0;
  590. if (_Image->width != tmpImage->width || _Image->height != tmpImage->height ||
  591. _Image->channels != tmpImage->channels || tmpImage->channels != 3 ||
  592. tmpImage->width <= 0 || tmpImage->height <= 0 ||
  593. static_cast<size_t>(tmpImage->width) > SIZE_MAX / static_cast<size_t>(tmpImage->height) ||
  594. static_cast<size_t>(tmpImage->width) * static_cast<size_t>(tmpImage->height) > SIZE_MAX / 3U) {
  595. delete tmpImage;
  596. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: decoded dimensions do not match target image");
  597. return ESP_FAIL;
  598. }
  599. expectedBytes = static_cast<size_t>(tmpImage->width) * static_cast<size_t>(tmpImage->height) * 3U;
  600. if (expectedBytes > static_cast<size_t>(INT_MAX)) {
  601. delete tmpImage;
  602. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: decoded image is too large");
  603. return ESP_FAIL;
  604. }
  605. uint8_t *source = tmpImage->RGBImageLock();
  606. if (source == NULL) {
  607. delete tmpImage;
  608. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: decoded image lock failed");
  609. return ESP_FAIL;
  610. }
  611. const bool copied = _Image->CopyFromMemory(source, static_cast<int>(expectedBytes));
  612. tmpImage->RGBImageRelease();
  613. delete tmpImage;
  614. #ifdef DEBUG_DETAIL_ON
  615. LogFile.WriteHeapInfo("CaptureToBasisImage - Done");
  616. #endif
  617. return copied ? ESP_OK : ESP_FAIL;
  618. }
  619. esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
  620. {
  621. string ftype;
  622. LEDOnOff(true); // Status-LED on
  623. if (delay > 0)
  624. {
  625. LightOnOff(true); // Flash-LED on
  626. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  627. vTaskDelay(xDelay);
  628. }
  629. camera_fb_t *warmup = esp_camera_fb_get();
  630. if (warmup != NULL) esp_camera_fb_return(warmup);
  631. camera_fb_t *fb = esp_camera_fb_get();
  632. if (!fb)
  633. {
  634. LEDOnOff(false); // Status-LED off
  635. LightOnOff(false); // Flash-LED off
  636. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: Capture Failed. "
  637. "Check camera module and/or proper electrical connection");
  638. // doReboot();
  639. return ESP_FAIL;
  640. }
  641. LEDOnOff(false); // Status-LED off
  642. #ifdef DEBUG_DETAIL_ON
  643. ESP_LOGD(TAG, "w %d, h %d, size %d", fb->width, fb->height, fb->len);
  644. #endif
  645. nm = FormatFileName(nm);
  646. #ifdef DEBUG_DETAIL_ON
  647. ESP_LOGD(TAG, "Save Camera to: %s", nm.c_str());
  648. #endif
  649. ftype = toUpper(getFileType(nm));
  650. #ifdef DEBUG_DETAIL_ON
  651. ESP_LOGD(TAG, "Filetype: %s", ftype.c_str());
  652. #endif
  653. uint8_t *buf = NULL;
  654. size_t buf_len = 0;
  655. bool converted = false;
  656. if (ftype.compare("BMP") == 0)
  657. {
  658. frame2bmp(fb, &buf, &buf_len);
  659. converted = true;
  660. }
  661. if (ftype.compare("JPG") == 0)
  662. {
  663. if (fb->format != PIXFORMAT_JPEG)
  664. {
  665. bool jpeg_converted = frame2jpg(fb, CCstatus.ImageQuality, &buf, &buf_len);
  666. converted = true;
  667. if (!jpeg_converted)
  668. {
  669. ESP_LOGE(TAG, "JPEG compression failed");
  670. }
  671. }
  672. else
  673. {
  674. buf_len = fb->len;
  675. buf = fb->buf;
  676. }
  677. }
  678. FILE *fp = fopen(nm.c_str(), "wb");
  679. if (fp == NULL)
  680. {
  681. // If an error occurs during the file creation
  682. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: Failed to open file " + nm);
  683. }
  684. else
  685. {
  686. fwrite(buf, sizeof(uint8_t), buf_len, fp);
  687. fclose(fp);
  688. }
  689. if (converted)
  690. {
  691. free(buf);
  692. }
  693. esp_camera_fb_return(fb);
  694. if (delay > 0)
  695. {
  696. LightOnOff(false); // Flash-LED off
  697. }
  698. return ESP_OK;
  699. }
  700. esp_err_t CCamera::CaptureToHTTP(httpd_req_t *req, int delay)
  701. {
  702. esp_err_t res = ESP_OK;
  703. size_t fb_len = 0;
  704. int64_t fr_start = esp_timer_get_time();
  705. LEDOnOff(true); // Status-LED on
  706. if (delay > 0)
  707. {
  708. LightOnOff(true); // Flash-LED on
  709. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  710. vTaskDelay(xDelay);
  711. }
  712. camera_fb_t *warmup = esp_camera_fb_get();
  713. if (warmup != NULL) esp_camera_fb_return(warmup);
  714. camera_fb_t *fb = esp_camera_fb_get();
  715. if (!fb)
  716. {
  717. LEDOnOff(false); // Status-LED off
  718. LightOnOff(false); // Flash-LED off
  719. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: Capture Failed. "
  720. "Check camera module and/or proper electrical connection");
  721. httpd_resp_send_500(req);
  722. // doReboot();
  723. return ESP_FAIL;
  724. }
  725. LEDOnOff(false); // Status-LED off
  726. res = httpd_resp_set_type(req, "image/jpeg");
  727. if (res == ESP_OK)
  728. {
  729. res = httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=raw.jpg");
  730. }
  731. if (res == ESP_OK)
  732. {
  733. if (CCstatus.DemoMode)
  734. {
  735. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Using Demo image!");
  736. uint8_t *demoData = NULL;
  737. size_t demoLength = 0;
  738. if (loadNextDemoImage(&demoData, &demoLength)) {
  739. fb_len = demoLength;
  740. res = httpd_resp_send(req, (const char *)demoData, demoLength);
  741. free_psram_heap("Demo image HTTP frame", demoData);
  742. } else {
  743. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToHTTP: could not load demo image");
  744. res = ESP_FAIL;
  745. }
  746. }
  747. else
  748. {
  749. if (fb->format == PIXFORMAT_JPEG)
  750. {
  751. fb_len = fb->len;
  752. res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
  753. }
  754. else
  755. {
  756. jpg_chunking_t jchunk = {req, 0};
  757. res = frame2jpg_cb(fb, 80, jpg_encode_stream, &jchunk) ? ESP_OK : ESP_FAIL;
  758. httpd_resp_send_chunk(req, NULL, 0);
  759. fb_len = jchunk.len;
  760. }
  761. }
  762. }
  763. esp_camera_fb_return(fb);
  764. int64_t fr_end = esp_timer_get_time();
  765. ESP_LOGI(TAG, "JPG: %dKB %dms", (int)(fb_len / 1024), (int)((fr_end - fr_start) / 1000));
  766. if (delay > 0)
  767. {
  768. LightOnOff(false); // Flash-LED off
  769. }
  770. return res;
  771. }
  772. esp_err_t CCamera::CaptureToStream(httpd_req_t *req, bool FlashlightOn)
  773. {
  774. esp_err_t res = ESP_OK;
  775. size_t fb_len = 0;
  776. int64_t fr_start;
  777. char part_buf[64];
  778. // wenn die Kameraeinstellungen durch Erstellen eines neuen Referenzbildes verändert wurden, müssen sie neu gesetzt werden
  779. if (CFstatus.changedCameraSettings)
  780. {
  781. Camera.setSensorDatenFromCCstatus(); // CCstatus >>> Kamera
  782. Camera.SetQualityZoomSize(CCstatus.ImageQuality, CCstatus.ImageFrameSize, CCstatus.ImageZoomEnabled, CCstatus.ImageZoomOffsetX, CCstatus.ImageZoomOffsetY, CCstatus.ImageZoomSize, CCstatus.ImageVflip);
  783. Camera.LedIntensity = CCstatus.ImageLedIntensity;
  784. CFstatus.changedCameraSettings = false;
  785. }
  786. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Live stream started");
  787. if (FlashlightOn)
  788. {
  789. LEDOnOff(true); // Status-LED on
  790. LightOnOff(true); // Flash-LED on
  791. }
  792. // httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); //stream is blocking web interface, only serving to local
  793. httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
  794. httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
  795. while (1)
  796. {
  797. fr_start = esp_timer_get_time();
  798. camera_fb_t *warmup = esp_camera_fb_get();
  799. if (warmup != NULL) esp_camera_fb_return(warmup);
  800. camera_fb_t *fb = esp_camera_fb_get();
  801. if (!fb)
  802. {
  803. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToStream: Camera framebuffer not available");
  804. break;
  805. }
  806. fb_len = fb->len;
  807. if (res == ESP_OK)
  808. {
  809. size_t hlen = snprintf((char *)part_buf, sizeof(part_buf), _STREAM_PART, fb_len);
  810. res = httpd_resp_send_chunk(req, (const char *)part_buf, hlen);
  811. }
  812. if (res == ESP_OK)
  813. {
  814. res = httpd_resp_send_chunk(req, (const char *)fb->buf, fb_len);
  815. }
  816. if (res == ESP_OK)
  817. {
  818. res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
  819. }
  820. esp_camera_fb_return(fb);
  821. int64_t fr_end = esp_timer_get_time();
  822. ESP_LOGD(TAG, "JPG: %dKB %dms", (int)(fb_len / 1024), (int)((fr_end - fr_start) / 1000));
  823. if (res != ESP_OK)
  824. {
  825. // Exit loop, e.g. also when closing the webpage
  826. break;
  827. }
  828. int64_t fr_delta_ms = (fr_end - fr_start) / 1000;
  829. if (CAM_LIVESTREAM_REFRESHRATE > fr_delta_ms)
  830. {
  831. const TickType_t xDelay = (CAM_LIVESTREAM_REFRESHRATE - fr_delta_ms) / portTICK_PERIOD_MS;
  832. ESP_LOGD(TAG, "Stream: sleep for: %ldms", (long)xDelay * 10);
  833. vTaskDelay(xDelay);
  834. }
  835. }
  836. LEDOnOff(false); // Status-LED off
  837. LightOnOff(false); // Flash-LED off
  838. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Live stream stopped");
  839. return res;
  840. }
  841. void CCamera::LightOnOff(bool status)
  842. {
  843. GpioHandler *gpioHandler = gpio_handler_get();
  844. if ((gpioHandler != NULL) && (gpioHandler->isEnabled()))
  845. {
  846. ESP_LOGD(TAG, "Use gpioHandler to trigger flashlight");
  847. gpioHandler->flashLightEnable(status);
  848. }
  849. else
  850. {
  851. #ifdef USE_PWM_LEDFLASH
  852. if (status)
  853. {
  854. ESP_LOGD(TAG, "Internal Flash-LED turn on with PWM %d", Camera.LedIntensity);
  855. ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, Camera.LedIntensity));
  856. // Update duty to apply the new value
  857. ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
  858. }
  859. else
  860. {
  861. ESP_LOGD(TAG, "Internal Flash-LED turn off PWM");
  862. ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, 0));
  863. ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
  864. }
  865. #else
  866. // Init the GPIO
  867. gpio_pad_select_gpio(FLASH_GPIO);
  868. // Set the GPIO as a push/pull output
  869. gpio_set_direction(FLASH_GPIO, GPIO_MODE_OUTPUT);
  870. if (status)
  871. {
  872. gpio_set_level(FLASH_GPIO, 1);
  873. }
  874. else
  875. {
  876. gpio_set_level(FLASH_GPIO, 0);
  877. }
  878. #endif
  879. }
  880. }
  881. void CCamera::LEDOnOff(bool status)
  882. {
  883. if (xHandle_task_StatusLED == NULL)
  884. {
  885. // Init the GPIO
  886. gpio_pad_select_gpio(BLINK_GPIO);
  887. /* Set the GPIO as a push/pull output */
  888. gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
  889. if (!status)
  890. {
  891. gpio_set_level(BLINK_GPIO, 1);
  892. }
  893. else
  894. {
  895. gpio_set_level(BLINK_GPIO, 0);
  896. }
  897. }
  898. }
  899. void CCamera::SetImageWidthHeightFromResolution(framesize_t resol)
  900. {
  901. if (resol == FRAMESIZE_QVGA)
  902. {
  903. CCstatus.ImageWidth = 320;
  904. CCstatus.ImageHeight = 240;
  905. }
  906. else if (resol == FRAMESIZE_VGA)
  907. {
  908. CCstatus.ImageWidth = 640;
  909. CCstatus.ImageHeight = 480;
  910. }
  911. else if (resol == FRAMESIZE_SVGA)
  912. {
  913. CCstatus.ImageWidth = 800;
  914. CCstatus.ImageHeight = 600;
  915. }
  916. else if (resol == FRAMESIZE_XGA)
  917. {
  918. CCstatus.ImageWidth = 1024;
  919. CCstatus.ImageHeight = 768;
  920. }
  921. else if (resol == FRAMESIZE_HD)
  922. {
  923. CCstatus.ImageWidth = 1280;
  924. CCstatus.ImageHeight = 720;
  925. }
  926. else if (resol == FRAMESIZE_SXGA)
  927. {
  928. CCstatus.ImageWidth = 1280;
  929. CCstatus.ImageHeight = 1024;
  930. }
  931. else if (resol == FRAMESIZE_UXGA)
  932. {
  933. CCstatus.ImageWidth = 1600;
  934. CCstatus.ImageHeight = 1200;
  935. }
  936. else if (resol == FRAMESIZE_QXGA)
  937. {
  938. CCstatus.ImageWidth = 2048;
  939. CCstatus.ImageHeight = 1536;
  940. }
  941. else if (resol == FRAMESIZE_WQXGA)
  942. {
  943. CCstatus.ImageWidth = 2560;
  944. CCstatus.ImageHeight = 1600;
  945. }
  946. else if (resol == FRAMESIZE_QSXGA)
  947. {
  948. CCstatus.ImageWidth = 2560;
  949. CCstatus.ImageHeight = 1920;
  950. }
  951. else
  952. {
  953. CCstatus.ImageWidth = 640;
  954. CCstatus.ImageHeight = 480;
  955. }
  956. }
  957. framesize_t CCamera::TextToFramesize(const char *_size)
  958. {
  959. if (strcmp(_size, "QVGA") == 0)
  960. {
  961. return FRAMESIZE_QVGA; // 320x240
  962. }
  963. else if (strcmp(_size, "VGA") == 0)
  964. {
  965. return FRAMESIZE_VGA; // 640x480
  966. }
  967. else if (strcmp(_size, "SVGA") == 0)
  968. {
  969. return FRAMESIZE_SVGA; // 800x600
  970. }
  971. else if (strcmp(_size, "XGA") == 0)
  972. {
  973. return FRAMESIZE_XGA; // 1024x768
  974. }
  975. else if (strcmp(_size, "SXGA") == 0)
  976. {
  977. return FRAMESIZE_SXGA; // 1280x1024
  978. }
  979. else if (strcmp(_size, "UXGA") == 0)
  980. {
  981. return FRAMESIZE_UXGA; // 1600x1200
  982. }
  983. else if (strcmp(_size, "QXGA") == 0)
  984. {
  985. return FRAMESIZE_QXGA; // 2048x1536
  986. }
  987. else if (strcmp(_size, "WQXGA") == 0)
  988. {
  989. return FRAMESIZE_WQXGA; // 2560x1600
  990. }
  991. else if (strcmp(_size, "QSXGA") == 0)
  992. {
  993. return FRAMESIZE_QSXGA; // 2560x1920
  994. }
  995. else
  996. {
  997. return FRAMESIZE_VGA; // 640x480
  998. }
  999. // return CCstatus.ImageFrameSize;
  1000. }
  1001. std::vector<std::string> demoFiles;
  1002. void CCamera::useDemoMode(void)
  1003. {
  1004. char line[50];
  1005. demoFiles.clear();
  1006. FILE *fd = fopen("/sdcard/demo/files.txt", "r");
  1007. if (fd == NULL) {
  1008. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can not start Demo mode: '/sdcard/demo/files.txt' is missing!");
  1009. CCstatus.DemoMode = false;
  1010. return;
  1011. }
  1012. while (fgets(line, sizeof(line), fd) != NULL) {
  1013. size_t length = strlen(line);
  1014. while (length > 0 && (line[length-1] == '\n' || line[length-1] == '\r')) line[--length] = '\0';
  1015. if (length > 0) demoFiles.push_back(line);
  1016. }
  1017. fclose(fd);
  1018. if (demoFiles.empty()) {
  1019. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can not start Demo mode: files.txt contains no image names");
  1020. CCstatus.DemoMode = false;
  1021. return;
  1022. }
  1023. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Using Demo mode (" + std::to_string(demoFiles.size()) + " files) instead of real camera image!");
  1024. for (const auto& file : demoFiles) LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, file);
  1025. CCstatus.DemoMode = true;
  1026. }
  1027. bool CCamera::loadNextDemoImage(uint8_t **data, size_t *length)
  1028. {
  1029. if (data == NULL || length == NULL || demoFiles.empty()) return false;
  1030. *data = NULL;
  1031. *length = 0;
  1032. const size_t index = static_cast<size_t>(getCountFlowRounds()) % demoFiles.size();
  1033. const std::string filename = "/sdcard/demo/" + demoFiles[index];
  1034. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Using " + filename + " as demo image");
  1035. FILE *fp = fopen(filename.c_str(), "rb");
  1036. if (fp == NULL) return false;
  1037. const long fileSize = GetFileSize(filename);
  1038. if (fileSize <= 0 || fileSize > DEMO_IMAGE_SIZE) {
  1039. fclose(fp);
  1040. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Demo image has invalid size: " + std::to_string(fileSize));
  1041. return false;
  1042. }
  1043. uint8_t *buffer = static_cast<uint8_t*>(malloc_psram_heap(
  1044. "Demo image frame", static_cast<size_t>(fileSize), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
  1045. if (buffer == NULL) {
  1046. fclose(fp);
  1047. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Demo image PSRAM allocation failed");
  1048. return false;
  1049. }
  1050. const size_t readBytes = fread(buffer, 1, static_cast<size_t>(fileSize), fp);
  1051. const bool readOkay = (readBytes == static_cast<size_t>(fileSize)) && (ferror(fp) == 0);
  1052. fclose(fp);
  1053. if (!readOkay) {
  1054. free_psram_heap("Demo image frame (short read)", buffer);
  1055. return false;
  1056. }
  1057. *data = buffer;
  1058. *length = readBytes;
  1059. return true;
  1060. }
  1061. long CCamera::GetFileSize(std::string filename)
  1062. {
  1063. struct stat stat_buf;
  1064. long rc = stat(filename.c_str(), &stat_buf);
  1065. return rc == 0 ? stat_buf.st_size : -1;
  1066. }