ClassControllCamera.cpp 18 KB

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