ClassControllCamera.cpp 37 KB

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