ClassControllCamera.cpp 36 KB

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