ClassControllCamera.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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 "CImageBasis.h"
  9. #include "server_ota.h"
  10. #include "server_GPIO.h"
  11. #include "../../include/defines.h"
  12. #include <esp_event.h>
  13. #include <esp_log.h>
  14. #include <esp_system.h>
  15. #include <nvs_flash.h>
  16. #include <sys/param.h>
  17. #include <string.h>
  18. #include "freertos/FreeRTOS.h"
  19. #include "freertos/task.h"
  20. #include "esp_camera.h"
  21. #include "driver/ledc.h"
  22. #include "server_tflite.h"
  23. static const char *TAG = "CAM";
  24. static camera_config_t camera_config = {
  25. .pin_pwdn = CAM_PIN_PWDN,
  26. .pin_reset = CAM_PIN_RESET,
  27. .pin_xclk = CAM_PIN_XCLK,
  28. .pin_sscb_sda = CAM_PIN_SIOD,
  29. .pin_sscb_scl = CAM_PIN_SIOC,
  30. .pin_d7 = CAM_PIN_D7,
  31. .pin_d6 = CAM_PIN_D6,
  32. .pin_d5 = CAM_PIN_D5,
  33. .pin_d4 = CAM_PIN_D4,
  34. .pin_d3 = CAM_PIN_D3,
  35. .pin_d2 = CAM_PIN_D2,
  36. .pin_d1 = CAM_PIN_D1,
  37. .pin_d0 = CAM_PIN_D0,
  38. .pin_vsync = CAM_PIN_VSYNC,
  39. .pin_href = CAM_PIN_HREF,
  40. .pin_pclk = CAM_PIN_PCLK,
  41. //XCLK 20MHz or 10MHz for OV2640 double FPS (Experimental)
  42. .xclk_freq_hz = 20000000, // Orginal value
  43. // .xclk_freq_hz = 5000000, // Test to get rid of the image errors !!!! Hangs in version 9.2 !!!!
  44. .ledc_timer = LEDC_TIMER_0,
  45. .ledc_channel = LEDC_CHANNEL_0,
  46. .pixel_format = PIXFORMAT_JPEG, //YUV422,GRAYSCALE,RGB565,JPEG
  47. .frame_size = FRAMESIZE_VGA, //QQVGA-UXGA Do not use sizes above QVGA when not JPEG
  48. // .frame_size = FRAMESIZE_UXGA, //QQVGA-UXGA Do not use sizes above QVGA when not JPEG
  49. .jpeg_quality = 12, //0-63 lower number means higher quality
  50. .fb_count = 1, //if more than one, i2s runs in continuous mode. Use only with JPEG
  51. .fb_location = CAMERA_FB_IN_PSRAM, /*!< The location where the frame buffer will be allocated */
  52. .grab_mode = CAMERA_GRAB_LATEST, // only from new esp32cam version
  53. };
  54. CCamera Camera;
  55. uint8_t *demoImage = NULL; // Buffer holding the demo image in bytes
  56. #define DEMO_IMAGE_SIZE 30000 // Max size of demo image in bytes
  57. typedef struct {
  58. httpd_req_t *req;
  59. size_t len;
  60. } jpg_chunking_t;
  61. bool CCamera::testCamera(void) {
  62. bool success;
  63. camera_fb_t *fb = esp_camera_fb_get();
  64. if (fb) {
  65. success = true;
  66. }
  67. else {
  68. success = false;
  69. }
  70. esp_camera_fb_return(fb);
  71. return success;
  72. }
  73. void CCamera::ledc_init(void)
  74. {
  75. #ifdef USE_PWM_LEDFLASH
  76. // Prepare and then apply the LEDC PWM timer configuration
  77. ledc_timer_config_t ledc_timer = { };
  78. ledc_timer.speed_mode = LEDC_MODE;
  79. ledc_timer.timer_num = LEDC_TIMER;
  80. ledc_timer.duty_resolution = LEDC_DUTY_RES;
  81. ledc_timer.freq_hz = LEDC_FREQUENCY; // Set output frequency at 5 kHz
  82. ledc_timer.clk_cfg = LEDC_AUTO_CLK;
  83. ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
  84. // Prepare and then apply the LEDC PWM channel configuration
  85. ledc_channel_config_t ledc_channel = { };
  86. ledc_channel.speed_mode = LEDC_MODE;
  87. ledc_channel.channel = LEDC_CHANNEL;
  88. ledc_channel.timer_sel = LEDC_TIMER;
  89. ledc_channel.intr_type = LEDC_INTR_DISABLE;
  90. ledc_channel.gpio_num = LEDC_OUTPUT_IO;
  91. ledc_channel.duty = 0; // Set duty to 0%
  92. ledc_channel.hpoint = 0;
  93. ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
  94. #endif
  95. }
  96. static size_t jpg_encode_stream(void * arg, size_t index, const void* data, size_t len){
  97. jpg_chunking_t *j = (jpg_chunking_t *)arg;
  98. if(!index){
  99. j->len = 0;
  100. }
  101. if(httpd_resp_send_chunk(j->req, (const char *)data, len) != ESP_OK){
  102. return 0;
  103. }
  104. j->len += len;
  105. return len;
  106. }
  107. bool CCamera::SetBrightnessContrastSaturation(int _brightness, int _contrast, int _saturation)
  108. {
  109. bool result = false;
  110. sensor_t * s = esp_camera_sensor_get();
  111. if (_brightness > -100)
  112. _brightness = min(2, max(-2, _brightness));
  113. if (_contrast > -100)
  114. _contrast = min(2, max(-2, _contrast));
  115. if (_saturation > -100)
  116. _saturation = min(2, max(-2, _saturation));
  117. if (_saturation > -100)
  118. s->set_saturation(s, _saturation);
  119. if (_contrast > -100)
  120. s->set_contrast(s, _contrast);
  121. if (_brightness > -100)
  122. s->set_brightness(s, _brightness);
  123. if ((_brightness != brightness) && (_brightness > -100))
  124. result = true;
  125. if ((_contrast != contrast) && (_contrast > -100))
  126. result = true;
  127. if ((_saturation != saturation) && (_saturation > -100))
  128. result = true;
  129. if (_brightness > -100)
  130. brightness = _brightness;
  131. if (_contrast > -100)
  132. contrast = _contrast;
  133. if (_saturation > -100)
  134. saturation = _saturation;
  135. if (result && isFixedExposure)
  136. EnableAutoExposure(waitbeforepicture_org);
  137. return result;
  138. }
  139. void CCamera::SetQualitySize(int qual, framesize_t resol)
  140. {
  141. sensor_t * s = esp_camera_sensor_get();
  142. s->set_quality(s, qual);
  143. s->set_framesize(s, resol);
  144. ActualResolution = resol;
  145. ActualQuality = qual;
  146. if (resol == FRAMESIZE_QVGA)
  147. {
  148. image_height = 240;
  149. image_width = 320;
  150. }
  151. if (resol == FRAMESIZE_VGA)
  152. {
  153. image_height = 480;
  154. image_width = 640;
  155. }
  156. }
  157. void CCamera::EnableAutoExposure(int flash_duration)
  158. {
  159. ESP_LOGD(TAG, "EnableAutoExposure");
  160. LEDOnOff(true);
  161. if (flash_duration > 0)
  162. LightOnOff(true);
  163. const TickType_t xDelay = flash_duration / portTICK_PERIOD_MS;
  164. vTaskDelay( xDelay );
  165. camera_fb_t * fb = esp_camera_fb_get();
  166. esp_camera_fb_return(fb);
  167. fb = esp_camera_fb_get();
  168. if (!fb) {
  169. ESP_LOGE(TAG, "Camera Capture Failed");
  170. LEDOnOff(false);
  171. LightOnOff(false);
  172. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Capture Failed (Procedure 'EnableAutoExposure') --> Reboot! "
  173. "Check that your camera module is working and connected properly.");
  174. //doReboot();
  175. }
  176. esp_camera_fb_return(fb);
  177. sensor_t * s = esp_camera_sensor_get();
  178. s->set_gain_ctrl(s, 0);
  179. s->set_exposure_ctrl(s, 0);
  180. LEDOnOff(false);
  181. LightOnOff(false);
  182. isFixedExposure = true;
  183. waitbeforepicture_org = flash_duration;
  184. }
  185. esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
  186. {
  187. #ifdef DEBUG_DETAIL_ON
  188. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - Start");
  189. #endif
  190. _Image->EmptyImage(); //Delete previous stored raw image -> black image
  191. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG__SHOW_TAKE_IMAGE_PROCESS
  192. tfliteflow.SetNewAlgROI(false);
  193. #endif
  194. LEDOnOff(true);
  195. if (delay > 0)
  196. {
  197. LightOnOff(true);
  198. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  199. vTaskDelay( xDelay );
  200. }
  201. #ifdef DEBUG_DETAIL_ON
  202. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After LightOn");
  203. #endif
  204. camera_fb_t * fb = esp_camera_fb_get();
  205. esp_camera_fb_return(fb);
  206. fb = esp_camera_fb_get();
  207. if (!fb) {
  208. LEDOnOff(false);
  209. LightOnOff(false);
  210. ESP_LOGE(TAG, "CaptureToBasisImage: Capture Failed");
  211. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "is not working anymore (CCamera::CaptureToBasisImage) - most probably caused by a hardware problem (instablility, ...). "
  212. "System will reboot.");
  213. doReboot();
  214. return ESP_FAIL;
  215. }
  216. if (demoMode) { // Use images stored on SD-Card instead of camera image
  217. /* Replace Framebuffer with image from SD-Card */
  218. loadNextDemoImage(fb);
  219. }
  220. CImageBasis* _zwImage = new CImageBasis();
  221. _zwImage->LoadFromMemory(fb->buf, fb->len);
  222. esp_camera_fb_return(fb);
  223. #ifdef DEBUG_DETAIL_ON
  224. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After fb_get");
  225. #endif
  226. LEDOnOff(false);
  227. if (delay > 0)
  228. LightOnOff(false);
  229. // TickType_t xDelay = 1000 / portTICK_PERIOD_MS;
  230. // vTaskDelay( xDelay ); // wait for power to recover
  231. #ifdef DEBUG_DETAIL_ON
  232. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After LoadFromMemory");
  233. #endif
  234. stbi_uc* p_target;
  235. stbi_uc* p_source;
  236. int channels = 3;
  237. int width = image_width;
  238. int height = image_height;
  239. #ifdef DEBUG_DETAIL_ON
  240. std::string _zw = "Targetimage: " + std::to_string((int) _Image->rgb_image) + " Size: " + std::to_string(_Image->width) + ", " + std::to_string(_Image->height);
  241. _zw = _zw + " _zwImage: " + std::to_string((int) _zwImage->rgb_image) + " Size: " + std::to_string(_zwImage->width) + ", " + std::to_string(_zwImage->height);
  242. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, _zw);
  243. #endif
  244. for (int x = 0; x < width; ++x)
  245. for (int y = 0; y < height; ++y)
  246. {
  247. p_target = _Image->rgb_image + (channels * (y * width + x));
  248. p_source = _zwImage->rgb_image + (channels * (y * width + x));
  249. p_target[0] = p_source[0];
  250. p_target[1] = p_source[1];
  251. p_target[2] = p_source[2];
  252. }
  253. #ifdef DEBUG_DETAIL_ON
  254. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After Copy To Target");
  255. #endif
  256. delete _zwImage;
  257. #ifdef DEBUG_DETAIL_ON
  258. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - Done");
  259. #endif
  260. return ESP_OK;
  261. }
  262. esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
  263. {
  264. string ftype;
  265. LEDOnOff(true); // Switched off to save power !
  266. if (delay > 0)
  267. {
  268. LightOnOff(true);
  269. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  270. vTaskDelay( xDelay );
  271. }
  272. camera_fb_t * fb = esp_camera_fb_get();
  273. esp_camera_fb_return(fb);
  274. fb = esp_camera_fb_get();
  275. if (!fb) {
  276. ESP_LOGE(TAG, "CaptureToFile: Camera Capture Failed");
  277. LEDOnOff(false);
  278. LightOnOff(false);
  279. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Capture Failed (CCamera::CaptureToFile) --> Reboot! "
  280. "Check that your camera module is working and connected properly.");
  281. //doReboot();
  282. return ESP_FAIL;
  283. }
  284. LEDOnOff(false);
  285. #ifdef DEBUG_DETAIL_ON
  286. ESP_LOGD(TAG, "w %d, h %d, size %d", fb->width, fb->height, fb->len);
  287. #endif
  288. nm = FormatFileName(nm);
  289. #ifdef DEBUG_DETAIL_ON
  290. ESP_LOGD(TAG, "Save Camera to: %s", nm.c_str());
  291. #endif
  292. ftype = toUpper(getFileType(nm));
  293. #ifdef DEBUG_DETAIL_ON
  294. ESP_LOGD(TAG, "Filetype: %s", ftype.c_str());
  295. #endif
  296. uint8_t * buf = NULL;
  297. size_t buf_len = 0;
  298. bool converted = false;
  299. if (ftype.compare("BMP") == 0)
  300. {
  301. frame2bmp(fb, &buf, &buf_len);
  302. converted = true;
  303. }
  304. if (ftype.compare("JPG") == 0)
  305. {
  306. if(fb->format != PIXFORMAT_JPEG){
  307. bool jpeg_converted = frame2jpg(fb, ActualQuality, &buf, &buf_len);
  308. converted = true;
  309. if(!jpeg_converted){
  310. ESP_LOGE(TAG, "JPEG compression failed");
  311. }
  312. } else {
  313. buf_len = fb->len;
  314. buf = fb->buf;
  315. }
  316. }
  317. FILE * fp = fopen(nm.c_str(), "wb");
  318. if (fp == NULL) /* If an error occurs during the file creation */
  319. {
  320. fprintf(stderr, "fopen() failed for '%s'\n", nm.c_str());
  321. }
  322. else
  323. {
  324. fwrite(buf, sizeof(uint8_t), buf_len, fp);
  325. fclose(fp);
  326. }
  327. if (converted)
  328. free(buf);
  329. esp_camera_fb_return(fb);
  330. if (delay > 0)
  331. {
  332. LightOnOff(false);
  333. }
  334. return ESP_OK;
  335. }
  336. esp_err_t CCamera::CaptureToHTTP(httpd_req_t *req, int delay)
  337. {
  338. camera_fb_t * fb = NULL;
  339. esp_err_t res = ESP_OK;
  340. size_t fb_len = 0;
  341. int64_t fr_start = esp_timer_get_time();
  342. LEDOnOff(true);
  343. if (delay > 0)
  344. {
  345. LightOnOff(true);
  346. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  347. vTaskDelay( xDelay );
  348. }
  349. fb = esp_camera_fb_get();
  350. esp_camera_fb_return(fb);
  351. fb = esp_camera_fb_get();
  352. if (!fb) {
  353. ESP_LOGE(TAG, "Camera capture failed");
  354. LEDOnOff(false);
  355. LightOnOff(false);
  356. httpd_resp_send_500(req);
  357. // doReboot();
  358. return ESP_FAIL;
  359. }
  360. LEDOnOff(false);
  361. res = httpd_resp_set_type(req, "image/jpeg");
  362. if(res == ESP_OK){
  363. res = httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=raw.jpg");
  364. }
  365. if(res == ESP_OK){
  366. if (demoMode) { // Use images stored on SD-Card instead of camera image
  367. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Using Demo image!");
  368. /* Replace Framebuffer with image from SD-Card */
  369. loadNextDemoImage(fb);
  370. res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
  371. }
  372. else {
  373. if(fb->format == PIXFORMAT_JPEG){
  374. fb_len = fb->len;
  375. res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
  376. } else {
  377. jpg_chunking_t jchunk = {req, 0};
  378. res = frame2jpg_cb(fb, 80, jpg_encode_stream, &jchunk)?ESP_OK:ESP_FAIL;
  379. httpd_resp_send_chunk(req, NULL, 0);
  380. fb_len = jchunk.len;
  381. }
  382. }
  383. }
  384. esp_camera_fb_return(fb);
  385. int64_t fr_end = esp_timer_get_time();
  386. ESP_LOGI(TAG, "JPG: %uKB %ums", (uint32_t)(fb_len/1024), (uint32_t)((fr_end - fr_start)/1000));
  387. if (delay > 0)
  388. {
  389. LightOnOff(false);
  390. }
  391. return res;
  392. }
  393. void CCamera::LightOnOff(bool status)
  394. {
  395. GpioHandler* gpioHandler = gpio_handler_get();
  396. if ((gpioHandler != NULL) && (gpioHandler->isEnabled())) {
  397. ESP_LOGD(TAG, "Use gpioHandler flashLigh");
  398. gpioHandler->flashLightEnable(status);
  399. } else {
  400. #ifdef USE_PWM_LEDFLASH
  401. if (status)
  402. {
  403. ESP_LOGD(TAG, "Internal Flash-LED turn on with PWM %d", led_intensity);
  404. ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, led_intensity));
  405. // Update duty to apply the new value
  406. ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
  407. }
  408. else
  409. {
  410. ESP_LOGD(TAG, "Internal Flash-LED turn off PWM");
  411. ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, 0));
  412. ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
  413. }
  414. #else
  415. // Init the GPIO
  416. gpio_pad_select_gpio(FLASH_GPIO);
  417. // Set the GPIO as a push/pull output
  418. gpio_set_direction(FLASH_GPIO, GPIO_MODE_OUTPUT);
  419. if (status)
  420. gpio_set_level(FLASH_GPIO, 1);
  421. else
  422. gpio_set_level(FLASH_GPIO, 0);
  423. #endif
  424. }
  425. }
  426. void CCamera::LEDOnOff(bool status)
  427. {
  428. // Init the GPIO
  429. gpio_pad_select_gpio(BLINK_GPIO);
  430. /* Set the GPIO as a push/pull output */
  431. gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
  432. if (!status)
  433. gpio_set_level(BLINK_GPIO, 1);
  434. else
  435. gpio_set_level(BLINK_GPIO, 0);
  436. }
  437. void CCamera::GetCameraParameter(httpd_req_t *req, int &qual, framesize_t &resol)
  438. {
  439. char _query[100];
  440. char _qual[10];
  441. char _size[10];
  442. resol = ActualResolution;
  443. qual = ActualQuality;
  444. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  445. {
  446. ESP_LOGD(TAG, "Query: %s", _query);
  447. if (httpd_query_key_value(_query, "size", _size, 10) == ESP_OK)
  448. {
  449. #ifdef DEBUG_DETAIL_ON
  450. ESP_LOGD(TAG, "Size: %s", _size);
  451. #endif
  452. if (strcmp(_size, "QVGA") == 0)
  453. resol = FRAMESIZE_QVGA; // 320x240
  454. if (strcmp(_size, "VGA") == 0)
  455. resol = FRAMESIZE_VGA; // 640x480
  456. if (strcmp(_size, "SVGA") == 0)
  457. resol = FRAMESIZE_SVGA; // 800x600
  458. if (strcmp(_size, "XGA") == 0)
  459. resol = FRAMESIZE_XGA; // 1024x768
  460. if (strcmp(_size, "SXGA") == 0)
  461. resol = FRAMESIZE_SXGA; // 1280x1024
  462. if (strcmp(_size, "UXGA") == 0)
  463. resol = FRAMESIZE_UXGA; // 1600x1200
  464. }
  465. if (httpd_query_key_value(_query, "quality", _qual, 10) == ESP_OK)
  466. {
  467. #ifdef DEBUG_DETAIL_ON
  468. ESP_LOGD(TAG, "Quality: %s", _qual);
  469. #endif
  470. qual = atoi(_qual);
  471. if (qual > 63)
  472. qual = 63;
  473. if (qual < 0)
  474. qual = 0;
  475. }
  476. }
  477. }
  478. framesize_t CCamera::TextToFramesize(const char * _size)
  479. {
  480. if (strcmp(_size, "QVGA") == 0)
  481. return FRAMESIZE_QVGA; // 320x240
  482. if (strcmp(_size, "VGA") == 0)
  483. return FRAMESIZE_VGA; // 640x480
  484. if (strcmp(_size, "SVGA") == 0)
  485. return FRAMESIZE_SVGA; // 800x600
  486. if (strcmp(_size, "XGA") == 0)
  487. return FRAMESIZE_XGA; // 1024x768
  488. if (strcmp(_size, "SXGA") == 0)
  489. return FRAMESIZE_SXGA; // 1280x1024
  490. if (strcmp(_size, "UXGA") == 0)
  491. return FRAMESIZE_UXGA; // 1600x1200
  492. return ActualResolution;
  493. }
  494. CCamera::CCamera()
  495. {
  496. #ifdef DEBUG_DETAIL_ON
  497. ESP_LOGD(TAG, "CreateClassCamera");
  498. #endif
  499. brightness = -5;
  500. contrast = -5;
  501. saturation = -5;
  502. isFixedExposure = false;
  503. ledc_init();
  504. }
  505. esp_err_t CCamera::InitCam()
  506. {
  507. ESP_LOGD(TAG, "Init Camera");
  508. ActualQuality = camera_config.jpeg_quality;
  509. ActualResolution = camera_config.frame_size;
  510. //initialize the camera
  511. esp_camera_deinit(); // De-init in case it was already initialized
  512. esp_err_t err = esp_camera_init(&camera_config);
  513. if (err != ESP_OK) {
  514. ESP_LOGE(TAG, "Camera Init Failed");
  515. return err;
  516. }
  517. CameraInitSuccessful = true;
  518. return ESP_OK;
  519. }
  520. void CCamera::SetLEDIntensity(float _intrel)
  521. {
  522. _intrel = min(_intrel, (float) 100);
  523. _intrel = max(_intrel, (float) 0);
  524. _intrel = _intrel / 100;
  525. led_intensity = (int) (_intrel * 8191);
  526. ESP_LOGD(TAG, "Set led_intensity to %d of 8191", led_intensity);
  527. }
  528. bool CCamera::getCameraInitSuccessful()
  529. {
  530. return CameraInitSuccessful;
  531. }
  532. std::vector<std::string> demoFiles;
  533. void CCamera::useDemoMode()
  534. {
  535. char line[50];
  536. FILE *fd = fopen("/sdcard/demo/files.txt", "r");
  537. if (!fd) {
  538. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can not start Demo mode, the folder '/sdcard/demo/' does not contain the needed files!");
  539. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "See Details on https://jomjol.github.io/AI-on-the-edge-device-docs/Demo-Mode!");
  540. return;
  541. }
  542. demoImage = (uint8_t*)malloc(DEMO_IMAGE_SIZE);
  543. if (demoImage == NULL) {
  544. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to acquire required memory for demo image!");
  545. return;
  546. }
  547. while (fgets(line, sizeof(line), fd) != NULL) {
  548. line[strlen(line) - 1] = '\0';
  549. demoFiles.push_back(line);
  550. }
  551. fclose(fd);
  552. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Using Demo mode (" + std::to_string(demoFiles.size()) +
  553. " files) instead of real camera image!");
  554. for (auto file : demoFiles) {
  555. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, file);
  556. }
  557. demoMode = true;
  558. }
  559. bool CCamera::loadNextDemoImage(camera_fb_t *fb) {
  560. char filename[50];
  561. int readBytes;
  562. long fileSize;
  563. snprintf(filename, sizeof(filename), "/sdcard/demo/%s", demoFiles[getCountFlowRounds() % demoFiles.size()].c_str());
  564. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Using " + std::string(filename) + " as demo image");
  565. /* Inject saved image */
  566. FILE * fp = fopen(filename, "rb");
  567. if (!fp) {
  568. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to read file: " + std::string(filename) +"!");
  569. return false;
  570. }
  571. fileSize = GetFileSize(filename);
  572. if (fileSize > DEMO_IMAGE_SIZE) {
  573. char buf[100];
  574. snprintf(buf, sizeof(buf), "Demo Image (%d bytes) is larger than provided buffer (%d bytes)!",
  575. (int)fileSize, DEMO_IMAGE_SIZE);
  576. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, std::string(buf));
  577. return false;
  578. }
  579. readBytes = fread(demoImage, 1, DEMO_IMAGE_SIZE, fp);
  580. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "read " + std::to_string(readBytes) + " bytes");
  581. fclose(fp);
  582. fb->buf = demoImage; // Update pointer
  583. fb->len = readBytes;
  584. // ToDo do we also need to set height, width, format and timestamp?
  585. return true;
  586. }
  587. long CCamera::GetFileSize(std::string filename)
  588. {
  589. struct stat stat_buf;
  590. long rc = stat(filename.c_str(), &stat_buf);
  591. return rc == 0 ? stat_buf.st_size : -1;
  592. }