ClassControllCamera.cpp 19 KB

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