ClassControllCamera.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  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 = 6, // 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.WaitBeforePicture = 2;
  98. ledc_init();
  99. }
  100. esp_err_t CCamera::InitCam(void)
  101. {
  102. ESP_LOGD(TAG, "Init Camera");
  103. TickType_t cam_xDelay = 100 / portTICK_PERIOD_MS;
  104. CCstatus.ImageQuality = camera_config.jpeg_quality;
  105. CCstatus.ImageFrameSize = camera_config.frame_size;
  106. // De-init in case it was already initialized
  107. esp_camera_deinit();
  108. vTaskDelay(cam_xDelay);
  109. // initialize the camera
  110. esp_err_t err = esp_camera_init(&camera_config);
  111. vTaskDelay(cam_xDelay);
  112. if (err != ESP_OK)
  113. {
  114. ESP_LOGE(TAG, "Camera Init Failed");
  115. return err;
  116. }
  117. CCstatus.CameraInitSuccessful = true;
  118. // Get a reference to the sensor
  119. sensor_t *s = esp_camera_sensor_get();
  120. if (s != NULL)
  121. {
  122. CCstatus.CamSensor_id = s->id.PID;
  123. // Dump camera module, warn for unsupported modules.
  124. switch (CCstatus.CamSensor_id)
  125. {
  126. case OV2640_PID:
  127. ESP_LOGI(TAG, "OV2640 camera module detected");
  128. break;
  129. case OV3660_PID:
  130. ESP_LOGI(TAG, "OV3660 camera module detected");
  131. break;
  132. case OV5640_PID:
  133. ESP_LOGI(TAG, "OV5640 camera module detected");
  134. break;
  135. default:
  136. ESP_LOGE(TAG, "Camera module is unknown and not properly supported!");
  137. CCstatus.CameraInitSuccessful = false;
  138. }
  139. }
  140. if (CCstatus.CameraInitSuccessful)
  141. {
  142. return ESP_OK;
  143. }
  144. else
  145. {
  146. return ESP_FAIL;
  147. }
  148. }
  149. bool CCamera::testCamera(void)
  150. {
  151. bool success;
  152. camera_fb_t *fb = esp_camera_fb_get();
  153. if (fb)
  154. {
  155. success = true;
  156. }
  157. else
  158. {
  159. success = false;
  160. }
  161. esp_camera_fb_return(fb);
  162. return success;
  163. }
  164. void CCamera::ledc_init(void)
  165. {
  166. #ifdef USE_PWM_LEDFLASH
  167. // Prepare and then apply the LEDC PWM timer configuration
  168. ledc_timer_config_t ledc_timer = {};
  169. ledc_timer.speed_mode = LEDC_MODE;
  170. ledc_timer.timer_num = LEDC_TIMER;
  171. ledc_timer.duty_resolution = LEDC_DUTY_RES;
  172. ledc_timer.freq_hz = LEDC_FREQUENCY; // Set output frequency at 5 kHz
  173. ledc_timer.clk_cfg = LEDC_AUTO_CLK;
  174. ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
  175. // Prepare and then apply the LEDC PWM channel configuration
  176. ledc_channel_config_t ledc_channel = {};
  177. ledc_channel.speed_mode = LEDC_MODE;
  178. ledc_channel.channel = LEDC_CHANNEL;
  179. ledc_channel.timer_sel = LEDC_TIMER;
  180. ledc_channel.intr_type = LEDC_INTR_DISABLE;
  181. ledc_channel.gpio_num = LEDC_OUTPUT_IO;
  182. ledc_channel.duty = 0; // Set duty to 0%
  183. ledc_channel.hpoint = 0;
  184. // ledc_channel.flags.output_invert = LEDC_OUTPUT_INVERT;
  185. ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
  186. #endif
  187. }
  188. void CCamera::SetLEDIntensity(float _intrel)
  189. {
  190. _intrel = min(_intrel, (float)100);
  191. _intrel = max(_intrel, (float)0);
  192. _intrel = _intrel / 100;
  193. CCstatus.ImageLedIntensity = (int)(_intrel * 8191);
  194. ESP_LOGD(TAG, "Set led_intensity to %d of 8191", CCstatus.ImageLedIntensity);
  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 *_zwImage = new CImageBasis("zwImage");
  571. if (_zwImage)
  572. {
  573. _zwImage->LoadFromMemory(fb->buf, fb->len);
  574. }
  575. else
  576. {
  577. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: Can't allocate _zwImage");
  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 (_zwImage == NULL)
  594. {
  595. return ESP_OK;
  596. }
  597. stbi_uc *p_target;
  598. stbi_uc *p_source;
  599. int channels = 3;
  600. int width = CCstatus.ImageWidth;
  601. int height = CCstatus.ImageHeight;
  602. #ifdef DEBUG_DETAIL_ON
  603. std::string _zw = "Targetimage: " + std::to_string((int)_Image->rgb_image) + " Size: " + std::to_string(_Image->width) + ", " + std::to_string(_Image->height);
  604. _zw = _zw + " _zwImage: " + std::to_string((int)_zwImage->rgb_image) + " Size: " + std::to_string(_zwImage->width) + ", " + std::to_string(_zwImage->height);
  605. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, _zw);
  606. #endif
  607. for (int x = 0; x < width; ++x)
  608. {
  609. for (int y = 0; y < height; ++y)
  610. {
  611. p_target = _Image->rgb_image + (channels * (y * width + x));
  612. p_source = _zwImage->rgb_image + (channels * (y * width + x));
  613. for (int c = 0; c < channels; c++)
  614. {
  615. p_target[c] = p_source[c];
  616. }
  617. }
  618. }
  619. delete _zwImage;
  620. #ifdef DEBUG_DETAIL_ON
  621. LogFile.WriteHeapInfo("CaptureToBasisImage - Done");
  622. #endif
  623. return ESP_OK;
  624. }
  625. esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
  626. {
  627. string ftype;
  628. LEDOnOff(true); // Status-LED on
  629. if (delay > 0)
  630. {
  631. LightOnOff(true); // Flash-LED on
  632. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  633. vTaskDelay(xDelay);
  634. }
  635. camera_fb_t *fb = esp_camera_fb_get();
  636. esp_camera_fb_return(fb);
  637. fb = esp_camera_fb_get();
  638. if (!fb)
  639. {
  640. LEDOnOff(false); // Status-LED off
  641. LightOnOff(false); // Flash-LED off
  642. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: Capture Failed. "
  643. "Check camera module and/or proper electrical connection");
  644. // doReboot();
  645. return ESP_FAIL;
  646. }
  647. LEDOnOff(false); // Status-LED off
  648. #ifdef DEBUG_DETAIL_ON
  649. ESP_LOGD(TAG, "w %d, h %d, size %d", fb->width, fb->height, fb->len);
  650. #endif
  651. nm = FormatFileName(nm);
  652. #ifdef DEBUG_DETAIL_ON
  653. ESP_LOGD(TAG, "Save Camera to: %s", nm.c_str());
  654. #endif
  655. ftype = toUpper(getFileType(nm));
  656. #ifdef DEBUG_DETAIL_ON
  657. ESP_LOGD(TAG, "Filetype: %s", ftype.c_str());
  658. #endif
  659. uint8_t *buf = NULL;
  660. size_t buf_len = 0;
  661. bool converted = false;
  662. if (ftype.compare("BMP") == 0)
  663. {
  664. frame2bmp(fb, &buf, &buf_len);
  665. converted = true;
  666. }
  667. if (ftype.compare("JPG") == 0)
  668. {
  669. if (fb->format != PIXFORMAT_JPEG)
  670. {
  671. bool jpeg_converted = frame2jpg(fb, CCstatus.ImageQuality, &buf, &buf_len);
  672. converted = true;
  673. if (!jpeg_converted)
  674. {
  675. ESP_LOGE(TAG, "JPEG compression failed");
  676. }
  677. }
  678. else
  679. {
  680. buf_len = fb->len;
  681. buf = fb->buf;
  682. }
  683. }
  684. FILE *fp = fopen(nm.c_str(), "wb");
  685. if (fp == NULL)
  686. {
  687. // If an error occurs during the file creation
  688. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: Failed to open file " + nm);
  689. }
  690. else
  691. {
  692. fwrite(buf, sizeof(uint8_t), buf_len, fp);
  693. fclose(fp);
  694. }
  695. if (converted)
  696. {
  697. free(buf);
  698. }
  699. esp_camera_fb_return(fb);
  700. if (delay > 0)
  701. {
  702. LightOnOff(false); // Flash-LED off
  703. }
  704. return ESP_OK;
  705. }
  706. esp_err_t CCamera::CaptureToHTTP(httpd_req_t *req, int delay)
  707. {
  708. esp_err_t res = ESP_OK;
  709. size_t fb_len = 0;
  710. int64_t fr_start = esp_timer_get_time();
  711. LEDOnOff(true); // Status-LED on
  712. if (delay > 0)
  713. {
  714. LightOnOff(true); // Flash-LED on
  715. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  716. vTaskDelay(xDelay);
  717. }
  718. camera_fb_t *fb = esp_camera_fb_get();
  719. esp_camera_fb_return(fb);
  720. fb = esp_camera_fb_get();
  721. if (!fb)
  722. {
  723. LEDOnOff(false); // Status-LED off
  724. LightOnOff(false); // Flash-LED off
  725. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: Capture Failed. "
  726. "Check camera module and/or proper electrical connection");
  727. httpd_resp_send_500(req);
  728. // doReboot();
  729. return ESP_FAIL;
  730. }
  731. LEDOnOff(false); // Status-LED off
  732. res = httpd_resp_set_type(req, "image/jpeg");
  733. if (res == ESP_OK)
  734. {
  735. res = httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=raw.jpg");
  736. }
  737. if (res == ESP_OK)
  738. {
  739. if (CCstatus.DemoMode)
  740. {
  741. // Use images stored on SD-Card instead of camera image
  742. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Using Demo image!");
  743. /* Replace Framebuffer with image from SD-Card */
  744. loadNextDemoImage(fb);
  745. res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
  746. }
  747. else
  748. {
  749. if (fb->format == PIXFORMAT_JPEG)
  750. {
  751. fb_len = fb->len;
  752. res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
  753. }
  754. else
  755. {
  756. jpg_chunking_t jchunk = {req, 0};
  757. res = frame2jpg_cb(fb, 80, jpg_encode_stream, &jchunk) ? ESP_OK : ESP_FAIL;
  758. httpd_resp_send_chunk(req, NULL, 0);
  759. fb_len = jchunk.len;
  760. }
  761. }
  762. }
  763. esp_camera_fb_return(fb);
  764. int64_t fr_end = esp_timer_get_time();
  765. ESP_LOGI(TAG, "JPG: %dKB %dms", (int)(fb_len / 1024), (int)((fr_end - fr_start) / 1000));
  766. if (delay > 0)
  767. {
  768. LightOnOff(false); // Flash-LED off
  769. }
  770. return res;
  771. }
  772. esp_err_t CCamera::CaptureToStream(httpd_req_t *req, bool FlashlightOn)
  773. {
  774. esp_err_t res = ESP_OK;
  775. size_t fb_len = 0;
  776. int64_t fr_start;
  777. char *part_buf[64];
  778. // wenn die Kameraeinstellungen durch Erstellen eines neuen Referenzbildes verändert wurden, müssen sie neu gesetzt werden
  779. if (CFstatus.changedCameraSettings)
  780. {
  781. Camera.setSensorDatenFromCCstatus(); // CCstatus >>> Kamera
  782. Camera.SetQualityZoomSize(CCstatus.ImageQuality, CCstatus.ImageFrameSize, CCstatus.ImageZoomEnabled, CCstatus.ImageZoomOffsetX, CCstatus.ImageZoomOffsetY, CCstatus.ImageZoomSize, CCstatus.ImageVflip);
  783. CFstatus.changedCameraSettings = false;
  784. }
  785. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Live stream started");
  786. if (FlashlightOn)
  787. {
  788. LEDOnOff(true); // Status-LED on
  789. LightOnOff(true); // Flash-LED on
  790. }
  791. // httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); //stream is blocking web interface, only serving to local
  792. httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
  793. httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
  794. while (1)
  795. {
  796. fr_start = esp_timer_get_time();
  797. camera_fb_t *fb = esp_camera_fb_get();
  798. esp_camera_fb_return(fb);
  799. fb = esp_camera_fb_get();
  800. if (!fb)
  801. {
  802. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToStream: Camera framebuffer not available");
  803. break;
  804. }
  805. fb_len = fb->len;
  806. if (res == ESP_OK)
  807. {
  808. size_t hlen = snprintf((char *)part_buf, sizeof(part_buf), _STREAM_PART, fb_len);
  809. res = httpd_resp_send_chunk(req, (const char *)part_buf, hlen);
  810. }
  811. if (res == ESP_OK)
  812. {
  813. res = httpd_resp_send_chunk(req, (const char *)fb->buf, fb_len);
  814. }
  815. if (res == ESP_OK)
  816. {
  817. res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
  818. }
  819. esp_camera_fb_return(fb);
  820. int64_t fr_end = esp_timer_get_time();
  821. ESP_LOGD(TAG, "JPG: %dKB %dms", (int)(fb_len / 1024), (int)((fr_end - fr_start) / 1000));
  822. if (res != ESP_OK)
  823. {
  824. // Exit loop, e.g. also when closing the webpage
  825. break;
  826. }
  827. int64_t fr_delta_ms = (fr_end - fr_start) / 1000;
  828. if (CAM_LIVESTREAM_REFRESHRATE > fr_delta_ms)
  829. {
  830. const TickType_t xDelay = (CAM_LIVESTREAM_REFRESHRATE - fr_delta_ms) / portTICK_PERIOD_MS;
  831. ESP_LOGD(TAG, "Stream: sleep for: %ldms", (long)xDelay * 10);
  832. vTaskDelay(xDelay);
  833. }
  834. }
  835. LEDOnOff(false); // Status-LED off
  836. LightOnOff(false); // Flash-LED off
  837. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Live stream stopped");
  838. return res;
  839. }
  840. void CCamera::LightOnOff(bool status)
  841. {
  842. GpioHandler *gpioHandler = gpio_handler_get();
  843. if ((gpioHandler != NULL) && (gpioHandler->isEnabled()))
  844. {
  845. ESP_LOGD(TAG, "Use gpioHandler to trigger flashlight");
  846. gpioHandler->flashLightEnable(status);
  847. }
  848. else
  849. {
  850. #ifdef USE_PWM_LEDFLASH
  851. if (status)
  852. {
  853. ESP_LOGD(TAG, "Internal Flash-LED turn on with PWM %d", CCstatus.ImageLedIntensity);
  854. ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, CCstatus.ImageLedIntensity));
  855. // Update duty to apply the new value
  856. ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
  857. }
  858. else
  859. {
  860. ESP_LOGD(TAG, "Internal Flash-LED turn off PWM");
  861. ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, 0));
  862. ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
  863. }
  864. #else
  865. // Init the GPIO
  866. gpio_pad_select_gpio(FLASH_GPIO);
  867. // Set the GPIO as a push/pull output
  868. gpio_set_direction(FLASH_GPIO, GPIO_MODE_OUTPUT);
  869. if (status)
  870. {
  871. gpio_set_level(FLASH_GPIO, 1);
  872. }
  873. else
  874. {
  875. gpio_set_level(FLASH_GPIO, 0);
  876. }
  877. #endif
  878. }
  879. }
  880. void CCamera::LEDOnOff(bool status)
  881. {
  882. if (xHandle_task_StatusLED == NULL)
  883. {
  884. // Init the GPIO
  885. gpio_pad_select_gpio(BLINK_GPIO);
  886. /* Set the GPIO as a push/pull output */
  887. gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
  888. if (!status)
  889. {
  890. gpio_set_level(BLINK_GPIO, 1);
  891. }
  892. else
  893. {
  894. gpio_set_level(BLINK_GPIO, 0);
  895. }
  896. }
  897. }
  898. void CCamera::SetImageWidthHeightFromResolution(framesize_t resol)
  899. {
  900. if (resol == FRAMESIZE_QVGA)
  901. {
  902. CCstatus.ImageWidth = 320;
  903. CCstatus.ImageHeight = 240;
  904. }
  905. else if (resol == FRAMESIZE_VGA)
  906. {
  907. CCstatus.ImageWidth = 640;
  908. CCstatus.ImageHeight = 480;
  909. }
  910. else if (resol == FRAMESIZE_SVGA)
  911. {
  912. CCstatus.ImageWidth = 800;
  913. CCstatus.ImageHeight = 600;
  914. }
  915. else if (resol == FRAMESIZE_XGA)
  916. {
  917. CCstatus.ImageWidth = 1024;
  918. CCstatus.ImageHeight = 768;
  919. }
  920. else if (resol == FRAMESIZE_HD)
  921. {
  922. CCstatus.ImageWidth = 1280;
  923. CCstatus.ImageHeight = 720;
  924. }
  925. else if (resol == FRAMESIZE_SXGA)
  926. {
  927. CCstatus.ImageWidth = 1280;
  928. CCstatus.ImageHeight = 1024;
  929. }
  930. else if (resol == FRAMESIZE_UXGA)
  931. {
  932. CCstatus.ImageWidth = 1600;
  933. CCstatus.ImageHeight = 1200;
  934. }
  935. else if (resol == FRAMESIZE_QXGA)
  936. {
  937. CCstatus.ImageWidth = 2048;
  938. CCstatus.ImageHeight = 1536;
  939. }
  940. else if (resol == FRAMESIZE_WQXGA)
  941. {
  942. CCstatus.ImageWidth = 2560;
  943. CCstatus.ImageHeight = 1600;
  944. }
  945. else if (resol == FRAMESIZE_QSXGA)
  946. {
  947. CCstatus.ImageWidth = 2560;
  948. CCstatus.ImageHeight = 1920;
  949. }
  950. else
  951. {
  952. CCstatus.ImageWidth = 640;
  953. CCstatus.ImageHeight = 480;
  954. }
  955. }
  956. framesize_t CCamera::TextToFramesize(const char *_size)
  957. {
  958. if (strcmp(_size, "QVGA") == 0)
  959. {
  960. return FRAMESIZE_QVGA; // 320x240
  961. }
  962. else if (strcmp(_size, "VGA") == 0)
  963. {
  964. return FRAMESIZE_VGA; // 640x480
  965. }
  966. else if (strcmp(_size, "SVGA") == 0)
  967. {
  968. return FRAMESIZE_SVGA; // 800x600
  969. }
  970. else if (strcmp(_size, "XGA") == 0)
  971. {
  972. return FRAMESIZE_XGA; // 1024x768
  973. }
  974. else if (strcmp(_size, "SXGA") == 0)
  975. {
  976. return FRAMESIZE_SXGA; // 1280x1024
  977. }
  978. else if (strcmp(_size, "UXGA") == 0)
  979. {
  980. return FRAMESIZE_UXGA; // 1600x1200
  981. }
  982. else if (strcmp(_size, "QXGA") == 0)
  983. {
  984. return FRAMESIZE_QXGA; // 2048x1536
  985. }
  986. else if (strcmp(_size, "WQXGA") == 0)
  987. {
  988. return FRAMESIZE_WQXGA; // 2560x1600
  989. }
  990. else if (strcmp(_size, "QSXGA") == 0)
  991. {
  992. return FRAMESIZE_QSXGA; // 2560x1920
  993. }
  994. else
  995. {
  996. return FRAMESIZE_VGA; // 640x480
  997. }
  998. // return CCstatus.ImageFrameSize;
  999. }
  1000. std::vector<std::string> demoFiles;
  1001. void CCamera::useDemoMode(void)
  1002. {
  1003. char line[50];
  1004. FILE *fd = fopen("/sdcard/demo/files.txt", "r");
  1005. if (!fd)
  1006. {
  1007. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can not start Demo mode, the folder '/sdcard/demo/' does not contain the needed files!");
  1008. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "See Details on https://jomjol.github.io/AI-on-the-edge-device-docs/Demo-Mode!");
  1009. return;
  1010. }
  1011. demoImage = (uint8_t *)malloc(DEMO_IMAGE_SIZE);
  1012. if (demoImage == NULL)
  1013. {
  1014. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to acquire required memory for demo image!");
  1015. return;
  1016. }
  1017. while (fgets(line, sizeof(line), fd) != NULL)
  1018. {
  1019. line[strlen(line) - 1] = '\0';
  1020. demoFiles.push_back(line);
  1021. }
  1022. fclose(fd);
  1023. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Using Demo mode (" + std::to_string(demoFiles.size()) + " files) instead of real camera image!");
  1024. for (auto file : demoFiles)
  1025. {
  1026. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, file);
  1027. }
  1028. CCstatus.DemoMode = true;
  1029. }
  1030. bool CCamera::loadNextDemoImage(camera_fb_t *fb)
  1031. {
  1032. char filename[50];
  1033. int readBytes;
  1034. long fileSize;
  1035. snprintf(filename, sizeof(filename), "/sdcard/demo/%s", demoFiles[getCountFlowRounds() % demoFiles.size()].c_str());
  1036. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Using " + std::string(filename) + " as demo image");
  1037. /* Inject saved image */
  1038. FILE *fp = fopen(filename, "rb");
  1039. if (!fp)
  1040. {
  1041. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to read file: " + std::string(filename) + "!");
  1042. return false;
  1043. }
  1044. fileSize = GetFileSize(filename);
  1045. if (fileSize > DEMO_IMAGE_SIZE)
  1046. {
  1047. char buf[100];
  1048. snprintf(buf, sizeof(buf), "Demo Image (%d bytes) is larger than provided buffer (%d bytes)!", (int)fileSize, DEMO_IMAGE_SIZE);
  1049. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, std::string(buf));
  1050. return false;
  1051. }
  1052. readBytes = fread(demoImage, 1, DEMO_IMAGE_SIZE, fp);
  1053. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "read " + std::to_string(readBytes) + " bytes");
  1054. fclose(fp);
  1055. fb->buf = demoImage; // Update pointer
  1056. fb->len = readBytes;
  1057. // ToDo do we also need to set height, width, format and timestamp?
  1058. return true;
  1059. }
  1060. long CCamera::GetFileSize(std::string filename)
  1061. {
  1062. struct stat stat_buf;
  1063. long rc = stat(filename.c_str(), &stat_buf);
  1064. return rc == 0 ? stat_buf.st_size : -1;
  1065. }