ClassControllCamera.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. LEDOnOff(true);
  192. if (delay > 0)
  193. {
  194. LightOnOff(true);
  195. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  196. vTaskDelay( xDelay );
  197. }
  198. #ifdef DEBUG_DETAIL_ON
  199. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After LightOn");
  200. #endif
  201. camera_fb_t * fb = esp_camera_fb_get();
  202. esp_camera_fb_return(fb);
  203. fb = esp_camera_fb_get();
  204. if (!fb) {
  205. LEDOnOff(false);
  206. LightOnOff(false);
  207. ESP_LOGE(TAG, "CaptureToBasisImage: Capture Failed");
  208. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "is not working anymore (CCamera::CaptureToBasisImage) - most probably caused by a hardware problem (instablility, ...). "
  209. "System will reboot.");
  210. doReboot();
  211. return ESP_FAIL;
  212. }
  213. if (demoMode) { // Use images stored on SD-Card instead of camera image
  214. /* Replace Framebuffer with image from SD-Card */
  215. loadNextDemoImage(fb);
  216. }
  217. CImageBasis* _zwImage = new CImageBasis();
  218. _zwImage->LoadFromMemory(fb->buf, fb->len);
  219. esp_camera_fb_return(fb);
  220. #ifdef DEBUG_DETAIL_ON
  221. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After fb_get");
  222. #endif
  223. LEDOnOff(false);
  224. if (delay > 0)
  225. LightOnOff(false);
  226. // TickType_t xDelay = 1000 / portTICK_PERIOD_MS;
  227. // vTaskDelay( xDelay ); // wait for power to recover
  228. #ifdef DEBUG_DETAIL_ON
  229. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After LoadFromMemory");
  230. #endif
  231. stbi_uc* p_target;
  232. stbi_uc* p_source;
  233. int channels = 3;
  234. int width = image_width;
  235. int height = image_height;
  236. #ifdef DEBUG_DETAIL_ON
  237. std::string _zw = "Targetimage: " + std::to_string((int) _Image->rgb_image) + " Size: " + std::to_string(_Image->width) + ", " + std::to_string(_Image->height);
  238. _zw = _zw + " _zwImage: " + std::to_string((int) _zwImage->rgb_image) + " Size: " + std::to_string(_zwImage->width) + ", " + std::to_string(_zwImage->height);
  239. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, _zw);
  240. #endif
  241. for (int x = 0; x < width; ++x)
  242. for (int y = 0; y < height; ++y)
  243. {
  244. p_target = _Image->rgb_image + (channels * (y * width + x));
  245. p_source = _zwImage->rgb_image + (channels * (y * width + x));
  246. p_target[0] = p_source[0];
  247. p_target[1] = p_source[1];
  248. p_target[2] = p_source[2];
  249. }
  250. delete _zwImage;
  251. #ifdef DEBUG_DETAIL_ON
  252. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - Done");
  253. #endif
  254. return ESP_OK;
  255. }
  256. esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
  257. {
  258. string ftype;
  259. LEDOnOff(true); // Switched off to save power !
  260. if (delay > 0)
  261. {
  262. LightOnOff(true);
  263. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  264. vTaskDelay( xDelay );
  265. }
  266. camera_fb_t * fb = esp_camera_fb_get();
  267. esp_camera_fb_return(fb);
  268. fb = esp_camera_fb_get();
  269. if (!fb) {
  270. ESP_LOGE(TAG, "CaptureToFile: Camera Capture Failed");
  271. LEDOnOff(false);
  272. LightOnOff(false);
  273. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Capture Failed (CCamera::CaptureToFile) --> Reboot! "
  274. "Check that your camera module is working and connected properly.");
  275. //doReboot();
  276. return ESP_FAIL;
  277. }
  278. LEDOnOff(false);
  279. #ifdef DEBUG_DETAIL_ON
  280. ESP_LOGD(TAG, "w %d, h %d, size %d", fb->width, fb->height, fb->len);
  281. #endif
  282. nm = FormatFileName(nm);
  283. #ifdef DEBUG_DETAIL_ON
  284. ESP_LOGD(TAG, "Save Camera to: %s", nm.c_str());
  285. #endif
  286. ftype = toUpper(getFileType(nm));
  287. #ifdef DEBUG_DETAIL_ON
  288. ESP_LOGD(TAG, "Filetype: %s", ftype.c_str());
  289. #endif
  290. uint8_t * buf = NULL;
  291. size_t buf_len = 0;
  292. bool converted = false;
  293. if (ftype.compare("BMP") == 0)
  294. {
  295. frame2bmp(fb, &buf, &buf_len);
  296. converted = true;
  297. }
  298. if (ftype.compare("JPG") == 0)
  299. {
  300. if(fb->format != PIXFORMAT_JPEG){
  301. bool jpeg_converted = frame2jpg(fb, ActualQuality, &buf, &buf_len);
  302. converted = true;
  303. if(!jpeg_converted){
  304. ESP_LOGE(TAG, "JPEG compression failed");
  305. }
  306. } else {
  307. buf_len = fb->len;
  308. buf = fb->buf;
  309. }
  310. }
  311. FILE * fp = fopen(nm.c_str(), "wb");
  312. if (fp == NULL) /* If an error occurs during the file creation */
  313. {
  314. fprintf(stderr, "fopen() failed for '%s'\n", nm.c_str());
  315. }
  316. else
  317. {
  318. fwrite(buf, sizeof(uint8_t), buf_len, fp);
  319. fclose(fp);
  320. }
  321. if (converted)
  322. free(buf);
  323. esp_camera_fb_return(fb);
  324. if (delay > 0)
  325. {
  326. LightOnOff(false);
  327. }
  328. return ESP_OK;
  329. }
  330. esp_err_t CCamera::CaptureToHTTP(httpd_req_t *req, int delay)
  331. {
  332. camera_fb_t * fb = NULL;
  333. esp_err_t res = ESP_OK;
  334. size_t fb_len = 0;
  335. int64_t fr_start = esp_timer_get_time();
  336. LEDOnOff(true);
  337. if (delay > 0)
  338. {
  339. LightOnOff(true);
  340. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  341. vTaskDelay( xDelay );
  342. }
  343. fb = esp_camera_fb_get();
  344. esp_camera_fb_return(fb);
  345. fb = esp_camera_fb_get();
  346. if (!fb) {
  347. ESP_LOGE(TAG, "Camera capture failed");
  348. LEDOnOff(false);
  349. LightOnOff(false);
  350. httpd_resp_send_500(req);
  351. // doReboot();
  352. return ESP_FAIL;
  353. }
  354. LEDOnOff(false);
  355. res = httpd_resp_set_type(req, "image/jpeg");
  356. if(res == ESP_OK){
  357. res = httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=raw.jpg");
  358. }
  359. if(res == ESP_OK){
  360. if (demoMode) { // Use images stored on SD-Card instead of camera image
  361. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Using Demo image!");
  362. /* Replace Framebuffer with image from SD-Card */
  363. loadNextDemoImage(fb);
  364. res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
  365. }
  366. else {
  367. if(fb->format == PIXFORMAT_JPEG){
  368. fb_len = fb->len;
  369. res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
  370. } else {
  371. jpg_chunking_t jchunk = {req, 0};
  372. res = frame2jpg_cb(fb, 80, jpg_encode_stream, &jchunk)?ESP_OK:ESP_FAIL;
  373. httpd_resp_send_chunk(req, NULL, 0);
  374. fb_len = jchunk.len;
  375. }
  376. }
  377. }
  378. esp_camera_fb_return(fb);
  379. int64_t fr_end = esp_timer_get_time();
  380. ESP_LOGI(TAG, "JPG: %uKB %ums", (uint32_t)(fb_len/1024), (uint32_t)((fr_end - fr_start)/1000));
  381. if (delay > 0)
  382. {
  383. LightOnOff(false);
  384. }
  385. return res;
  386. }
  387. void CCamera::LightOnOff(bool status)
  388. {
  389. GpioHandler* gpioHandler = gpio_handler_get();
  390. if ((gpioHandler != NULL) && (gpioHandler->isEnabled())) {
  391. ESP_LOGD(TAG, "Use gpioHandler flashLigh");
  392. gpioHandler->flashLightEnable(status);
  393. } else {
  394. #ifdef USE_PWM_LEDFLASH
  395. if (status)
  396. {
  397. ESP_LOGD(TAG, "Internal Flash-LED turn on with PWM %d", led_intensity);
  398. ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, led_intensity));
  399. // Update duty to apply the new value
  400. ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
  401. }
  402. else
  403. {
  404. ESP_LOGD(TAG, "Internal Flash-LED turn off PWM");
  405. ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, 0));
  406. ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
  407. }
  408. #else
  409. // Init the GPIO
  410. gpio_pad_select_gpio(FLASH_GPIO);
  411. // Set the GPIO as a push/pull output
  412. gpio_set_direction(FLASH_GPIO, GPIO_MODE_OUTPUT);
  413. if (status)
  414. gpio_set_level(FLASH_GPIO, 1);
  415. else
  416. gpio_set_level(FLASH_GPIO, 0);
  417. #endif
  418. }
  419. }
  420. void CCamera::LEDOnOff(bool status)
  421. {
  422. // Init the GPIO
  423. gpio_pad_select_gpio(BLINK_GPIO);
  424. /* Set the GPIO as a push/pull output */
  425. gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
  426. if (!status)
  427. gpio_set_level(BLINK_GPIO, 1);
  428. else
  429. gpio_set_level(BLINK_GPIO, 0);
  430. }
  431. void CCamera::GetCameraParameter(httpd_req_t *req, int &qual, framesize_t &resol)
  432. {
  433. char _query[100];
  434. char _qual[10];
  435. char _size[10];
  436. resol = ActualResolution;
  437. qual = ActualQuality;
  438. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  439. {
  440. ESP_LOGD(TAG, "Query: %s", _query);
  441. if (httpd_query_key_value(_query, "size", _size, 10) == ESP_OK)
  442. {
  443. #ifdef DEBUG_DETAIL_ON
  444. ESP_LOGD(TAG, "Size: %s", _size);
  445. #endif
  446. if (strcmp(_size, "QVGA") == 0)
  447. resol = FRAMESIZE_QVGA; // 320x240
  448. if (strcmp(_size, "VGA") == 0)
  449. resol = FRAMESIZE_VGA; // 640x480
  450. if (strcmp(_size, "SVGA") == 0)
  451. resol = FRAMESIZE_SVGA; // 800x600
  452. if (strcmp(_size, "XGA") == 0)
  453. resol = FRAMESIZE_XGA; // 1024x768
  454. if (strcmp(_size, "SXGA") == 0)
  455. resol = FRAMESIZE_SXGA; // 1280x1024
  456. if (strcmp(_size, "UXGA") == 0)
  457. resol = FRAMESIZE_UXGA; // 1600x1200
  458. }
  459. if (httpd_query_key_value(_query, "quality", _qual, 10) == ESP_OK)
  460. {
  461. #ifdef DEBUG_DETAIL_ON
  462. ESP_LOGD(TAG, "Quality: %s", _qual);
  463. #endif
  464. qual = atoi(_qual);
  465. if (qual > 63)
  466. qual = 63;
  467. if (qual < 0)
  468. qual = 0;
  469. }
  470. }
  471. }
  472. framesize_t CCamera::TextToFramesize(const char * _size)
  473. {
  474. if (strcmp(_size, "QVGA") == 0)
  475. return FRAMESIZE_QVGA; // 320x240
  476. if (strcmp(_size, "VGA") == 0)
  477. return FRAMESIZE_VGA; // 640x480
  478. if (strcmp(_size, "SVGA") == 0)
  479. return FRAMESIZE_SVGA; // 800x600
  480. if (strcmp(_size, "XGA") == 0)
  481. return FRAMESIZE_XGA; // 1024x768
  482. if (strcmp(_size, "SXGA") == 0)
  483. return FRAMESIZE_SXGA; // 1280x1024
  484. if (strcmp(_size, "UXGA") == 0)
  485. return FRAMESIZE_UXGA; // 1600x1200
  486. return ActualResolution;
  487. }
  488. CCamera::CCamera()
  489. {
  490. #ifdef DEBUG_DETAIL_ON
  491. ESP_LOGD(TAG, "CreateClassCamera");
  492. #endif
  493. brightness = -5;
  494. contrast = -5;
  495. saturation = -5;
  496. isFixedExposure = false;
  497. ledc_init();
  498. }
  499. esp_err_t CCamera::InitCam()
  500. {
  501. ESP_LOGD(TAG, "Init Camera");
  502. ActualQuality = camera_config.jpeg_quality;
  503. ActualResolution = camera_config.frame_size;
  504. //initialize the camera
  505. esp_camera_deinit(); // De-init in case it was already initialized
  506. esp_err_t err = esp_camera_init(&camera_config);
  507. if (err != ESP_OK) {
  508. ESP_LOGE(TAG, "Camera Init Failed");
  509. return err;
  510. }
  511. CameraInitSuccessful = true;
  512. return ESP_OK;
  513. }
  514. void CCamera::SetLEDIntensity(float _intrel)
  515. {
  516. _intrel = min(_intrel, (float) 100);
  517. _intrel = max(_intrel, (float) 0);
  518. _intrel = _intrel / 100;
  519. led_intensity = (int) (_intrel * 8191);
  520. ESP_LOGD(TAG, "Set led_intensity to %d of 8191", led_intensity);
  521. }
  522. bool CCamera::getCameraInitSuccessful()
  523. {
  524. return CameraInitSuccessful;
  525. }
  526. std::vector<std::string> demoFiles;
  527. void CCamera::useDemoMode()
  528. {
  529. char line[50];
  530. FILE *fd = fopen("/sdcard/demo/files.txt", "r");
  531. if (!fd) {
  532. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can not start Demo mode, the folder '/sdcard/demo/' does not contain the needed files!");
  533. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "See Details on https://jomjol.github.io/AI-on-the-edge-device-docs/Demo-Mode!");
  534. return;
  535. }
  536. demoImage = (uint8_t*)malloc(DEMO_IMAGE_SIZE);
  537. if (demoImage == NULL) {
  538. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to acquire required memory for demo image!");
  539. return;
  540. }
  541. while (fgets(line, sizeof(line), fd) != NULL) {
  542. line[strlen(line) - 1] = '\0';
  543. demoFiles.push_back(line);
  544. }
  545. fclose(fd);
  546. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Using Demo mode (" + std::to_string(demoFiles.size()) +
  547. " files) instead of real camera image!");
  548. for (auto file : demoFiles) {
  549. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, file);
  550. }
  551. demoMode = true;
  552. }
  553. bool CCamera::loadNextDemoImage(camera_fb_t *fb) {
  554. char filename[50];
  555. int readBytes;
  556. long fileSize;
  557. snprintf(filename, sizeof(filename), "/sdcard/demo/%s", demoFiles[getCountFlowRounds() % demoFiles.size()].c_str());
  558. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Using " + std::string(filename) + " as demo image");
  559. /* Inject saved image */
  560. FILE * fp = fopen(filename, "rb");
  561. if (!fp) {
  562. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to read file: " + std::string(filename) +"!");
  563. return false;
  564. }
  565. fileSize = GetFileSize(filename);
  566. if (fileSize > DEMO_IMAGE_SIZE) {
  567. char buf[100];
  568. snprintf(buf, sizeof(buf), "Demo Image (%d bytes) is larger than provided buffer (%d bytes)!",
  569. (int)fileSize, DEMO_IMAGE_SIZE);
  570. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, std::string(buf));
  571. return false;
  572. }
  573. readBytes = fread(demoImage, 1, DEMO_IMAGE_SIZE, fp);
  574. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "read " + std::to_string(readBytes) + " bytes");
  575. fclose(fp);
  576. fb->buf = demoImage; // Update pointer
  577. fb->len = readBytes;
  578. // ToDo do we also need to set height, width, format and timestamp?
  579. return true;
  580. }
  581. long CCamera::GetFileSize(std::string filename)
  582. {
  583. struct stat stat_buf;
  584. long rc = stat(filename.c_str(), &stat_buf);
  585. return rc == 0 ? stat_buf.st_size : -1;
  586. }