ClassControllCamera.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. #include "ClassControllCamera.h"
  2. #include "ClassLogFile.h"
  3. #include <stdio.h>
  4. #include "driver/gpio.h"
  5. #include "esp_timer.h"
  6. #include "esp_log.h"
  7. #include "Helper.h"
  8. #include "statusled.h"
  9. #include "CImageBasis.h"
  10. #include "psram.h"
  11. #include "server_ota.h"
  12. #include "server_GPIO.h"
  13. #include "../../include/defines.h"
  14. #include <esp_event.h>
  15. #include <esp_log.h>
  16. #include <esp_system.h>
  17. #include <nvs_flash.h>
  18. #include <sys/param.h>
  19. #include <string.h>
  20. #include <limits.h>
  21. #include <new>
  22. #include <sys/stat.h>
  23. #include "freertos/FreeRTOS.h"
  24. #include "freertos/task.h"
  25. #include "server_camera.h"
  26. #include "esp_camera.h"
  27. #include "esp_heap_caps.h"
  28. #include "driver/ledc.h"
  29. #include "MainFlowControl.h"
  30. #include "ov2640_sharpness.h"
  31. #include "ov2640_specialEffect.h"
  32. #include "ov2640_contrast_brightness.h"
  33. #if (ESP_IDF_VERSION_MAJOR >= 5)
  34. #include "soc/periph_defs.h"
  35. #include "esp_private/periph_ctrl.h"
  36. #include "soc/gpio_sig_map.h"
  37. #include "soc/gpio_periph.h"
  38. #include "soc/io_mux_reg.h"
  39. #include "esp_rom_gpio.h"
  40. #define gpio_pad_select_gpio esp_rom_gpio_pad_select_gpio
  41. #define gpio_matrix_in(a, b, c) esp_rom_gpio_connect_in_signal(a, b, c)
  42. #define gpio_matrix_out(a, b, c, d) esp_rom_gpio_connect_out_signal(a, b, c, d)
  43. #define ets_delay_us(a) esp_rom_delay_us(a)
  44. #endif
  45. CCamera Camera;
  46. camera_controll_config_temp_t CCstatus;
  47. static const char *TAG = "CAM";
  48. /* Camera live stream */
  49. #define PART_BOUNDARY "123456789000000000000987654321"
  50. static const char *_STREAM_CONTENT_TYPE = "multipart/x-mixed-replace;boundary=" PART_BOUNDARY;
  51. static const char *_STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n";
  52. static const char *_STREAM_PART = "Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n";
  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. uint8_t *demoData = NULL;
  564. bool demoBufferAllocated = false;
  565. if (CCstatus.DemoMode) {
  566. if (!loadNextDemoImage(&demoData, &frameLength)) {
  567. esp_camera_fb_return(fb);
  568. LEDOnOff(false);
  569. LightOnOff(false);
  570. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: could not load demo image");
  571. return ESP_FAIL;
  572. }
  573. frameData = demoData;
  574. demoBufferAllocated = true;
  575. }
  576. CImageBasis *tmpImage = new (std::nothrow) CImageBasis("tmpImage");
  577. bool decoded = tmpImage != NULL && frameLength <= static_cast<size_t>(INT_MAX) &&
  578. tmpImage->LoadFromMemory(frameData, static_cast<int>(frameLength));
  579. if (demoBufferAllocated) free_psram_heap("Demo image frame", demoData);
  580. esp_camera_fb_return(fb);
  581. LEDOnOff(false);
  582. if (delay > 0) LightOnOff(false);
  583. if (!decoded || tmpImage == NULL || !tmpImage->ImageOkay()) {
  584. delete tmpImage;
  585. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: JPEG decode failed");
  586. LogFile.WriteHeapInfo("CaptureToBasisImage decode");
  587. return ESP_FAIL;
  588. }
  589. size_t expectedBytes = 0;
  590. if (_Image->width != tmpImage->width || _Image->height != tmpImage->height ||
  591. _Image->channels != tmpImage->channels || tmpImage->channels != 3 ||
  592. tmpImage->width <= 0 || tmpImage->height <= 0 ||
  593. static_cast<size_t>(tmpImage->width) > SIZE_MAX / static_cast<size_t>(tmpImage->height) ||
  594. static_cast<size_t>(tmpImage->width) * static_cast<size_t>(tmpImage->height) > SIZE_MAX / 3U) {
  595. delete tmpImage;
  596. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: decoded dimensions do not match target image");
  597. return ESP_FAIL;
  598. }
  599. expectedBytes = static_cast<size_t>(tmpImage->width) * static_cast<size_t>(tmpImage->height) * 3U;
  600. if (expectedBytes > static_cast<size_t>(INT_MAX)) {
  601. delete tmpImage;
  602. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: decoded image is too large");
  603. return ESP_FAIL;
  604. }
  605. uint8_t *source = tmpImage->RGBImageLock();
  606. if (source == NULL) {
  607. delete tmpImage;
  608. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: decoded image lock failed");
  609. return ESP_FAIL;
  610. }
  611. const bool copied = _Image->CopyFromMemory(source, static_cast<int>(expectedBytes));
  612. tmpImage->RGBImageRelease();
  613. delete tmpImage;
  614. #ifdef DEBUG_DETAIL_ON
  615. LogFile.WriteHeapInfo("CaptureToBasisImage - Done");
  616. #endif
  617. return copied ? ESP_OK : ESP_FAIL;
  618. }
  619. esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
  620. {
  621. string ftype;
  622. LEDOnOff(true); // Status-LED on
  623. if (delay > 0)
  624. {
  625. LightOnOff(true); // Flash-LED on
  626. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  627. vTaskDelay(xDelay);
  628. }
  629. camera_fb_t *warmup = esp_camera_fb_get();
  630. if (warmup != NULL) esp_camera_fb_return(warmup);
  631. camera_fb_t *fb = esp_camera_fb_get();
  632. if (!fb)
  633. {
  634. LEDOnOff(false); // Status-LED off
  635. LightOnOff(false); // Flash-LED off
  636. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: Capture Failed. "
  637. "Check camera module and/or proper electrical connection");
  638. // doReboot();
  639. return ESP_FAIL;
  640. }
  641. LEDOnOff(false); // Status-LED off
  642. #ifdef DEBUG_DETAIL_ON
  643. ESP_LOGD(TAG, "w %d, h %d, size %d", fb->width, fb->height, fb->len);
  644. #endif
  645. nm = FormatFileName(nm);
  646. #ifdef DEBUG_DETAIL_ON
  647. ESP_LOGD(TAG, "Save Camera to: %s", nm.c_str());
  648. #endif
  649. ftype = toUpper(getFileType(nm));
  650. #ifdef DEBUG_DETAIL_ON
  651. ESP_LOGD(TAG, "Filetype: %s", ftype.c_str());
  652. #endif
  653. uint8_t *buf = NULL;
  654. size_t buf_len = 0;
  655. bool converted = false;
  656. bool captureOkay = false;
  657. if (ftype == "BMP") {
  658. converted = true;
  659. captureOkay = frame2bmp(fb, &buf, &buf_len) && buf != NULL && buf_len > 0;
  660. } else if (ftype == "JPG") {
  661. if (fb->format == PIXFORMAT_JPEG) {
  662. buf_len = fb->len;
  663. buf = fb->buf;
  664. captureOkay = (buf != NULL && buf_len > 0);
  665. } else {
  666. converted = true;
  667. captureOkay = frame2jpg(fb, CCstatus.ImageQuality, &buf, &buf_len) &&
  668. buf != NULL && buf_len > 0;
  669. if (!captureOkay) ESP_LOGE(TAG, "JPEG compression failed");
  670. }
  671. }
  672. if (captureOkay) {
  673. FILE *fp = fopen(nm.c_str(), "wb");
  674. if (fp == NULL) {
  675. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: Failed to open file " + nm);
  676. captureOkay = false;
  677. } else {
  678. const size_t written = fwrite(buf, 1, buf_len, fp);
  679. const int closeResult = fclose(fp);
  680. if (written != buf_len || closeResult != 0) {
  681. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: incomplete write to " + nm);
  682. captureOkay = false;
  683. }
  684. }
  685. } else {
  686. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: unsupported image format or conversion failed");
  687. }
  688. if (converted && buf != NULL) free(buf);
  689. esp_camera_fb_return(fb);
  690. if (delay > 0)
  691. {
  692. LightOnOff(false); // Flash-LED off
  693. }
  694. return captureOkay ? ESP_OK : ESP_FAIL;
  695. }
  696. esp_err_t CCamera::CaptureToHTTP(httpd_req_t *req, int delay)
  697. {
  698. esp_err_t res = ESP_OK;
  699. size_t fb_len = 0;
  700. int64_t fr_start = esp_timer_get_time();
  701. LEDOnOff(true); // Status-LED on
  702. if (delay > 0)
  703. {
  704. LightOnOff(true); // Flash-LED on
  705. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  706. vTaskDelay(xDelay);
  707. }
  708. camera_fb_t *warmup = esp_camera_fb_get();
  709. if (warmup != NULL) esp_camera_fb_return(warmup);
  710. camera_fb_t *fb = esp_camera_fb_get();
  711. if (!fb)
  712. {
  713. LEDOnOff(false); // Status-LED off
  714. LightOnOff(false); // Flash-LED off
  715. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: Capture Failed. "
  716. "Check camera module and/or proper electrical connection");
  717. httpd_resp_send_500(req);
  718. // doReboot();
  719. return ESP_FAIL;
  720. }
  721. LEDOnOff(false); // Status-LED off
  722. res = httpd_resp_set_type(req, "image/jpeg");
  723. if (res == ESP_OK)
  724. {
  725. res = httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=raw.jpg");
  726. }
  727. if (res == ESP_OK)
  728. {
  729. if (CCstatus.DemoMode)
  730. {
  731. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Using Demo image!");
  732. uint8_t *demoData = NULL;
  733. size_t demoLength = 0;
  734. if (loadNextDemoImage(&demoData, &demoLength)) {
  735. fb_len = demoLength;
  736. res = httpd_resp_send(req, (const char *)demoData, demoLength);
  737. free_psram_heap("Demo image HTTP frame", demoData);
  738. } else {
  739. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToHTTP: could not load demo image");
  740. res = ESP_FAIL;
  741. }
  742. }
  743. else
  744. {
  745. if (fb->format == PIXFORMAT_JPEG)
  746. {
  747. fb_len = fb->len;
  748. res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
  749. }
  750. else
  751. {
  752. jpg_chunking_t jchunk = {req, 0};
  753. res = frame2jpg_cb(fb, 80, jpg_encode_stream, &jchunk) ? ESP_OK : ESP_FAIL;
  754. httpd_resp_send_chunk(req, NULL, 0);
  755. fb_len = jchunk.len;
  756. }
  757. }
  758. }
  759. esp_camera_fb_return(fb);
  760. int64_t fr_end = esp_timer_get_time();
  761. ESP_LOGI(TAG, "JPG: %dKB %dms", (int)(fb_len / 1024), (int)((fr_end - fr_start) / 1000));
  762. if (delay > 0)
  763. {
  764. LightOnOff(false); // Flash-LED off
  765. }
  766. return res;
  767. }
  768. esp_err_t CCamera::CaptureToStream(httpd_req_t *req, bool FlashlightOn)
  769. {
  770. esp_err_t res = ESP_OK;
  771. size_t fb_len = 0;
  772. int64_t fr_start;
  773. char part_buf[64];
  774. // wenn die Kameraeinstellungen durch Erstellen eines neuen Referenzbildes verändert wurden, müssen sie neu gesetzt werden
  775. if (CFstatus.changedCameraSettings)
  776. {
  777. Camera.setSensorDatenFromCCstatus(); // CCstatus >>> Kamera
  778. Camera.SetQualityZoomSize(CCstatus.ImageQuality, CCstatus.ImageFrameSize, CCstatus.ImageZoomEnabled, CCstatus.ImageZoomOffsetX, CCstatus.ImageZoomOffsetY, CCstatus.ImageZoomSize, CCstatus.ImageVflip);
  779. Camera.LedIntensity = CCstatus.ImageLedIntensity;
  780. CFstatus.changedCameraSettings = false;
  781. }
  782. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Live stream started");
  783. if (FlashlightOn)
  784. {
  785. LEDOnOff(true); // Status-LED on
  786. LightOnOff(true); // Flash-LED on
  787. }
  788. // httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); //stream is blocking web interface, only serving to local
  789. httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
  790. httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
  791. while (1)
  792. {
  793. fr_start = esp_timer_get_time();
  794. camera_fb_t *warmup = esp_camera_fb_get();
  795. if (warmup != NULL) esp_camera_fb_return(warmup);
  796. camera_fb_t *fb = esp_camera_fb_get();
  797. if (!fb)
  798. {
  799. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToStream: Camera framebuffer not available");
  800. break;
  801. }
  802. fb_len = fb->len;
  803. if (res == ESP_OK)
  804. {
  805. size_t hlen = snprintf((char *)part_buf, sizeof(part_buf), _STREAM_PART, fb_len);
  806. res = httpd_resp_send_chunk(req, (const char *)part_buf, hlen);
  807. }
  808. if (res == ESP_OK)
  809. {
  810. res = httpd_resp_send_chunk(req, (const char *)fb->buf, fb_len);
  811. }
  812. if (res == ESP_OK)
  813. {
  814. res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
  815. }
  816. esp_camera_fb_return(fb);
  817. int64_t fr_end = esp_timer_get_time();
  818. ESP_LOGD(TAG, "JPG: %dKB %dms", (int)(fb_len / 1024), (int)((fr_end - fr_start) / 1000));
  819. if (res != ESP_OK)
  820. {
  821. // Exit loop, e.g. also when closing the webpage
  822. break;
  823. }
  824. int64_t fr_delta_ms = (fr_end - fr_start) / 1000;
  825. if (CAM_LIVESTREAM_REFRESHRATE > fr_delta_ms)
  826. {
  827. const TickType_t xDelay = (CAM_LIVESTREAM_REFRESHRATE - fr_delta_ms) / portTICK_PERIOD_MS;
  828. ESP_LOGD(TAG, "Stream: sleep for: %ldms", (long)xDelay * 10);
  829. vTaskDelay(xDelay);
  830. }
  831. }
  832. LEDOnOff(false); // Status-LED off
  833. LightOnOff(false); // Flash-LED off
  834. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Live stream stopped");
  835. return res;
  836. }
  837. void CCamera::LightOnOff(bool status)
  838. {
  839. GpioHandler *gpioHandler = gpio_handler_get();
  840. if ((gpioHandler != NULL) && (gpioHandler->isEnabled()))
  841. {
  842. ESP_LOGD(TAG, "Use gpioHandler to trigger flashlight");
  843. gpioHandler->flashLightEnable(status);
  844. }
  845. else
  846. {
  847. #ifdef USE_PWM_LEDFLASH
  848. if (status)
  849. {
  850. ESP_LOGD(TAG, "Internal Flash-LED turn on with PWM %d", Camera.LedIntensity);
  851. ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, Camera.LedIntensity));
  852. // Update duty to apply the new value
  853. ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
  854. }
  855. else
  856. {
  857. ESP_LOGD(TAG, "Internal Flash-LED turn off PWM");
  858. ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, 0));
  859. ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
  860. }
  861. #else
  862. // Init the GPIO
  863. gpio_pad_select_gpio(FLASH_GPIO);
  864. // Set the GPIO as a push/pull output
  865. gpio_set_direction(FLASH_GPIO, GPIO_MODE_OUTPUT);
  866. if (status)
  867. {
  868. gpio_set_level(FLASH_GPIO, 1);
  869. }
  870. else
  871. {
  872. gpio_set_level(FLASH_GPIO, 0);
  873. }
  874. #endif
  875. }
  876. }
  877. void CCamera::LEDOnOff(bool status)
  878. {
  879. if (xHandle_task_StatusLED == NULL)
  880. {
  881. // Init the GPIO
  882. gpio_pad_select_gpio(BLINK_GPIO);
  883. /* Set the GPIO as a push/pull output */
  884. gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
  885. if (!status)
  886. {
  887. gpio_set_level(BLINK_GPIO, 1);
  888. }
  889. else
  890. {
  891. gpio_set_level(BLINK_GPIO, 0);
  892. }
  893. }
  894. }
  895. void CCamera::SetImageWidthHeightFromResolution(framesize_t resol)
  896. {
  897. if (resol == FRAMESIZE_QVGA)
  898. {
  899. CCstatus.ImageWidth = 320;
  900. CCstatus.ImageHeight = 240;
  901. }
  902. else if (resol == FRAMESIZE_VGA)
  903. {
  904. CCstatus.ImageWidth = 640;
  905. CCstatus.ImageHeight = 480;
  906. }
  907. else if (resol == FRAMESIZE_SVGA)
  908. {
  909. CCstatus.ImageWidth = 800;
  910. CCstatus.ImageHeight = 600;
  911. }
  912. else if (resol == FRAMESIZE_XGA)
  913. {
  914. CCstatus.ImageWidth = 1024;
  915. CCstatus.ImageHeight = 768;
  916. }
  917. else if (resol == FRAMESIZE_HD)
  918. {
  919. CCstatus.ImageWidth = 1280;
  920. CCstatus.ImageHeight = 720;
  921. }
  922. else if (resol == FRAMESIZE_SXGA)
  923. {
  924. CCstatus.ImageWidth = 1280;
  925. CCstatus.ImageHeight = 1024;
  926. }
  927. else if (resol == FRAMESIZE_UXGA)
  928. {
  929. CCstatus.ImageWidth = 1600;
  930. CCstatus.ImageHeight = 1200;
  931. }
  932. else if (resol == FRAMESIZE_QXGA)
  933. {
  934. CCstatus.ImageWidth = 2048;
  935. CCstatus.ImageHeight = 1536;
  936. }
  937. else if (resol == FRAMESIZE_WQXGA)
  938. {
  939. CCstatus.ImageWidth = 2560;
  940. CCstatus.ImageHeight = 1600;
  941. }
  942. else if (resol == FRAMESIZE_QSXGA)
  943. {
  944. CCstatus.ImageWidth = 2560;
  945. CCstatus.ImageHeight = 1920;
  946. }
  947. else
  948. {
  949. CCstatus.ImageWidth = 640;
  950. CCstatus.ImageHeight = 480;
  951. }
  952. }
  953. framesize_t CCamera::TextToFramesize(const char *_size)
  954. {
  955. if (strcmp(_size, "QVGA") == 0)
  956. {
  957. return FRAMESIZE_QVGA; // 320x240
  958. }
  959. else if (strcmp(_size, "VGA") == 0)
  960. {
  961. return FRAMESIZE_VGA; // 640x480
  962. }
  963. else if (strcmp(_size, "SVGA") == 0)
  964. {
  965. return FRAMESIZE_SVGA; // 800x600
  966. }
  967. else if (strcmp(_size, "XGA") == 0)
  968. {
  969. return FRAMESIZE_XGA; // 1024x768
  970. }
  971. else if (strcmp(_size, "SXGA") == 0)
  972. {
  973. return FRAMESIZE_SXGA; // 1280x1024
  974. }
  975. else if (strcmp(_size, "UXGA") == 0)
  976. {
  977. return FRAMESIZE_UXGA; // 1600x1200
  978. }
  979. else if (strcmp(_size, "QXGA") == 0)
  980. {
  981. return FRAMESIZE_QXGA; // 2048x1536
  982. }
  983. else if (strcmp(_size, "WQXGA") == 0)
  984. {
  985. return FRAMESIZE_WQXGA; // 2560x1600
  986. }
  987. else if (strcmp(_size, "QSXGA") == 0)
  988. {
  989. return FRAMESIZE_QSXGA; // 2560x1920
  990. }
  991. else
  992. {
  993. return FRAMESIZE_VGA; // 640x480
  994. }
  995. // return CCstatus.ImageFrameSize;
  996. }
  997. std::vector<std::string> demoFiles;
  998. void CCamera::useDemoMode(void)
  999. {
  1000. char line[50];
  1001. demoFiles.clear();
  1002. FILE *fd = fopen("/sdcard/demo/files.txt", "r");
  1003. if (fd == NULL) {
  1004. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can not start Demo mode: '/sdcard/demo/files.txt' is missing!");
  1005. CCstatus.DemoMode = false;
  1006. return;
  1007. }
  1008. while (fgets(line, sizeof(line), fd) != NULL) {
  1009. size_t length = strlen(line);
  1010. while (length > 0 && (line[length-1] == '\n' || line[length-1] == '\r')) line[--length] = '\0';
  1011. if (length > 0) demoFiles.push_back(line);
  1012. }
  1013. fclose(fd);
  1014. if (demoFiles.empty()) {
  1015. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can not start Demo mode: files.txt contains no image names");
  1016. CCstatus.DemoMode = false;
  1017. return;
  1018. }
  1019. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Using Demo mode (" + std::to_string(demoFiles.size()) + " files) instead of real camera image!");
  1020. for (const auto& file : demoFiles) LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, file);
  1021. CCstatus.DemoMode = true;
  1022. }
  1023. bool CCamera::loadNextDemoImage(uint8_t **data, size_t *length)
  1024. {
  1025. if (data == NULL || length == NULL || demoFiles.empty()) return false;
  1026. *data = NULL;
  1027. *length = 0;
  1028. const size_t index = static_cast<size_t>(getCountFlowRounds()) % demoFiles.size();
  1029. const std::string filename = "/sdcard/demo/" + demoFiles[index];
  1030. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Using " + filename + " as demo image");
  1031. FILE *fp = fopen(filename.c_str(), "rb");
  1032. if (fp == NULL) return false;
  1033. const long fileSize = GetFileSize(filename);
  1034. if (fileSize <= 0 || fileSize > DEMO_IMAGE_SIZE) {
  1035. fclose(fp);
  1036. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Demo image has invalid size: " + std::to_string(fileSize));
  1037. return false;
  1038. }
  1039. uint8_t *buffer = static_cast<uint8_t*>(malloc_psram_heap(
  1040. "Demo image frame", static_cast<size_t>(fileSize), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
  1041. if (buffer == NULL) {
  1042. fclose(fp);
  1043. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Demo image PSRAM allocation failed");
  1044. return false;
  1045. }
  1046. const size_t readBytes = fread(buffer, 1, static_cast<size_t>(fileSize), fp);
  1047. const bool readOkay = (readBytes == static_cast<size_t>(fileSize)) && (ferror(fp) == 0);
  1048. fclose(fp);
  1049. if (!readOkay) {
  1050. free_psram_heap("Demo image frame (short read)", buffer);
  1051. return false;
  1052. }
  1053. *data = buffer;
  1054. *length = readBytes;
  1055. return true;
  1056. }
  1057. long CCamera::GetFileSize(std::string filename)
  1058. {
  1059. struct stat stat_buf;
  1060. long rc = stat(filename.c_str(), &stat_buf);
  1061. return rc == 0 ? stat_buf.st_size : -1;
  1062. }