ClassControllCamera.cpp 36 KB

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