ClassControllCamera.cpp 18 KB

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