ClassControllCamera.cpp 36 KB

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