ClassControllCamera.cpp 36 KB

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