ClassControllCamera.cpp 20 KB

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