ClassControllCamera.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. printf("EnableAutoExposure");
  200. LEDOnOff(true);
  201. if (flashdauer > 0)
  202. LightOnOff(true);
  203. const TickType_t xDelay = flashdauer / portTICK_PERIOD_MS;
  204. vTaskDelay( xDelay );
  205. camera_fb_t * fb = esp_camera_fb_get();
  206. esp_camera_fb_return(fb);
  207. fb = esp_camera_fb_get();
  208. if (!fb) {
  209. ESP_LOGE(TAGCAMERACLASS, "Camera Capture Failed");
  210. LEDOnOff(false);
  211. LightOnOff(false);
  212. doReboot();
  213. }
  214. esp_camera_fb_return(fb);
  215. sensor_t * s = esp_camera_sensor_get();
  216. s->set_gain_ctrl(s, 0);
  217. s->set_exposure_ctrl(s, 0);
  218. LEDOnOff(false);
  219. LightOnOff(false);
  220. isFixedExposure = true;
  221. waitbeforepicture_org = flashdauer;
  222. }
  223. esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
  224. {
  225. string ftype;
  226. uint8_t *zwischenspeicher = NULL;
  227. LEDOnOff(true);
  228. #ifdef DEBUG_DETAIL_ON
  229. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - Start");
  230. #endif
  231. if (delay > 0)
  232. {
  233. LightOnOff(true);
  234. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  235. vTaskDelay( xDelay );
  236. }
  237. #ifdef DEBUG_DETAIL_ON
  238. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After LightOn");
  239. #endif
  240. camera_fb_t * fb = esp_camera_fb_get();
  241. esp_camera_fb_return(fb);
  242. fb = esp_camera_fb_get();
  243. if (!fb) {
  244. ESP_LOGE(TAGCAMERACLASS, "CaptureToBasisImage: Camera Capture Failed");
  245. LEDOnOff(false);
  246. LightOnOff(false);
  247. LogFile.SwitchOnOff(true);
  248. LogFile.WriteToFile("Camera is not working anymore - most propably hardware problem (instablility, ...). "
  249. "System will reboot.");
  250. doReboot();
  251. return ESP_FAIL;
  252. }
  253. int _size = fb->len;
  254. zwischenspeicher = (uint8_t*) malloc(_size);
  255. if (!zwischenspeicher)
  256. {
  257. ESP_LOGE(TAGCAMERACLASS, "Nicht ausreichend Speicherplatz für Bild in Funktion CaptureToBasisImage()");
  258. LogFile.SwitchOnOff(true);
  259. LogFile.WriteToFile("Nicht ausreichend Speicherplatz für Bild in Funktion CaptureToBasisImage()");
  260. }
  261. for (int i = 0; i < _size; ++i)
  262. *(zwischenspeicher + i) = *(fb->buf + i);
  263. esp_camera_fb_return(fb);
  264. #ifdef DEBUG_DETAIL_ON
  265. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After fb_get");
  266. #endif
  267. LEDOnOff(false);
  268. if (delay > 0)
  269. LightOnOff(false);
  270. // TickType_t xDelay = 1000 / portTICK_PERIOD_MS;
  271. // vTaskDelay( xDelay ); // wait for power to recover
  272. uint8_t * buf = NULL;
  273. CImageBasis _zwImage;
  274. _zwImage.LoadFromMemory(zwischenspeicher, _size);
  275. free(zwischenspeicher);
  276. #ifdef DEBUG_DETAIL_ON
  277. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After LoadFromMemory");
  278. #endif
  279. stbi_uc* p_target;
  280. stbi_uc* p_source;
  281. int channels = 3;
  282. int width = image_width;
  283. int height = image_height;
  284. #ifdef DEBUG_DETAIL_ON
  285. std::string _zw = "Targetimage: " + std::to_string((int) _Image->rgb_image) + " Size: " + std::to_string(_Image->width) + ", " + std::to_string(_Image->height);
  286. _zw = _zw + " _zwImage: " + std::to_string((int) _zwImage.rgb_image) + " Size: " + std::to_string(_zwImage.width) + ", " + std::to_string(_zwImage.height);
  287. LogFile.WriteToFile(_zw);
  288. #endif
  289. for (int x = 0; x < width; ++x)
  290. for (int y = 0; y < height; ++y)
  291. {
  292. p_target = _Image->rgb_image + (channels * (y * width + x));
  293. p_source = _zwImage.rgb_image + (channels * (y * width + x));
  294. p_target[0] = p_source[0];
  295. p_target[1] = p_source[1];
  296. p_target[2] = p_source[2];
  297. }
  298. #ifdef DEBUG_DETAIL_ON
  299. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After Copy To Target");
  300. #endif
  301. free(buf);
  302. #ifdef DEBUG_DETAIL_ON
  303. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - Done");
  304. #endif
  305. return ESP_OK;
  306. }
  307. esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
  308. {
  309. string ftype;
  310. LEDOnOff(true); // Abgeschaltet, um Strom zu sparen !!!!!!
  311. if (delay > 0)
  312. {
  313. LightOnOff(true);
  314. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  315. vTaskDelay( xDelay );
  316. }
  317. camera_fb_t * fb = esp_camera_fb_get();
  318. esp_camera_fb_return(fb);
  319. fb = esp_camera_fb_get();
  320. if (!fb) {
  321. ESP_LOGE(TAGCAMERACLASS, "CaptureToFile: Camera Capture Failed");
  322. LEDOnOff(false);
  323. LightOnOff(false);
  324. doReboot();
  325. return ESP_FAIL;
  326. }
  327. LEDOnOff(false);
  328. #ifdef DEBUG_DETAIL_ON
  329. printf("w %d, h %d, size %d\n", fb->width, fb->height, fb->len);
  330. #endif
  331. nm = FormatFileName(nm);
  332. #ifdef DEBUG_DETAIL_ON
  333. printf("Save Camera to : %s\n", nm.c_str());
  334. #endif
  335. ftype = toUpper(getFileType(nm));
  336. #ifdef DEBUG_DETAIL_ON
  337. printf("Filetype: %s\n", ftype.c_str());
  338. #endif
  339. uint8_t * buf = NULL;
  340. size_t buf_len = 0;
  341. bool converted = false;
  342. if (ftype.compare("BMP") == 0)
  343. {
  344. frame2bmp(fb, &buf, &buf_len);
  345. converted = true;
  346. }
  347. if (ftype.compare("JPG") == 0)
  348. {
  349. if(fb->format != PIXFORMAT_JPEG){
  350. bool jpeg_converted = frame2jpg(fb, ActualQuality, &buf, &buf_len);
  351. converted = true;
  352. if(!jpeg_converted){
  353. ESP_LOGE(TAGCAMERACLASS, "JPEG compression failed");
  354. }
  355. } else {
  356. buf_len = fb->len;
  357. buf = fb->buf;
  358. }
  359. }
  360. FILE * fp = OpenFileAndWait(nm.c_str(), "wb");
  361. if (fp == NULL) /* If an error occurs during the file creation */
  362. {
  363. fprintf(stderr, "fopen() failed for '%s'\n", nm.c_str());
  364. }
  365. else
  366. {
  367. fwrite(buf, sizeof(uint8_t), buf_len, fp);
  368. fclose(fp);
  369. }
  370. if (converted)
  371. free(buf);
  372. esp_camera_fb_return(fb);
  373. if (delay > 0)
  374. {
  375. LightOnOff(false);
  376. }
  377. return ESP_OK;
  378. }
  379. esp_err_t CCamera::CaptureToHTTP(httpd_req_t *req, int delay)
  380. {
  381. camera_fb_t * fb = NULL;
  382. esp_err_t res = ESP_OK;
  383. size_t fb_len = 0;
  384. int64_t fr_start = esp_timer_get_time();
  385. LEDOnOff(true);
  386. if (delay > 0)
  387. {
  388. LightOnOff(true);
  389. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  390. vTaskDelay( xDelay );
  391. }
  392. fb = esp_camera_fb_get();
  393. esp_camera_fb_return(fb);
  394. fb = esp_camera_fb_get();
  395. if (!fb) {
  396. ESP_LOGE(TAGCAMERACLASS, "Camera capture failed");
  397. LEDOnOff(false);
  398. LightOnOff(false);
  399. httpd_resp_send_500(req);
  400. // doReboot();
  401. return ESP_FAIL;
  402. }
  403. LEDOnOff(false);
  404. res = httpd_resp_set_type(req, "image/jpeg");
  405. if(res == ESP_OK){
  406. res = httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=raw.jpg");
  407. }
  408. if(res == ESP_OK){
  409. if(fb->format == PIXFORMAT_JPEG){
  410. fb_len = fb->len;
  411. res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
  412. } else {
  413. jpg_chunking_t jchunk = {req, 0};
  414. res = frame2jpg_cb(fb, 80, jpg_encode_stream, &jchunk)?ESP_OK:ESP_FAIL;
  415. httpd_resp_send_chunk(req, NULL, 0);
  416. fb_len = jchunk.len;
  417. }
  418. }
  419. esp_camera_fb_return(fb);
  420. int64_t fr_end = esp_timer_get_time();
  421. ESP_LOGI(TAGCAMERACLASS, "JPG: %uKB %ums", (uint32_t)(fb_len/1024), (uint32_t)((fr_end - fr_start)/1000));
  422. if (delay > 0)
  423. {
  424. LightOnOff(false);
  425. }
  426. return res;
  427. }
  428. void CCamera::LightOnOff(bool status)
  429. {
  430. GpioHandler* gpioHandler = gpio_handler_get();
  431. if ((gpioHandler != NULL) && (gpioHandler->isEnabled())) {
  432. printf("Use gpioHandler flashLigh\n");
  433. gpioHandler->flashLightEnable(status);
  434. } else {
  435. #ifdef USE_PWM_LEDFLASH
  436. if (status)
  437. {
  438. printf("Internal Flash-LED turn on with PWM %d\n", led_intensity);
  439. ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, led_intensity));
  440. // Update duty to apply the new value
  441. ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
  442. }
  443. else
  444. {
  445. printf("Internal Flash-LED turn off PWM\n");
  446. ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, 0));
  447. ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
  448. }
  449. #else
  450. // Init the GPIO
  451. gpio_pad_select_gpio(FLASH_GPIO);
  452. // Set the GPIO as a push/pull output
  453. gpio_set_direction(FLASH_GPIO, GPIO_MODE_OUTPUT);
  454. if (status)
  455. gpio_set_level(FLASH_GPIO, 1);
  456. else
  457. gpio_set_level(FLASH_GPIO, 0);
  458. #endif
  459. }
  460. }
  461. void CCamera::LEDOnOff(bool status)
  462. {
  463. // Init the GPIO
  464. gpio_pad_select_gpio(BLINK_GPIO);
  465. /* Set the GPIO as a push/pull output */
  466. gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
  467. if (!status)
  468. gpio_set_level(BLINK_GPIO, 1);
  469. else
  470. gpio_set_level(BLINK_GPIO, 0);
  471. }
  472. void CCamera::GetCameraParameter(httpd_req_t *req, int &qual, framesize_t &resol)
  473. {
  474. char _query[100];
  475. char _qual[10];
  476. char _size[10];
  477. resol = ActualResolution;
  478. qual = ActualQuality;
  479. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  480. {
  481. printf("Query: "); printf(_query); printf("\n");
  482. if (httpd_query_key_value(_query, "size", _size, 10) == ESP_OK)
  483. {
  484. #ifdef DEBUG_DETAIL_ON
  485. printf("Size: "); printf(_size); printf("\n");
  486. #endif
  487. if (strcmp(_size, "QVGA") == 0)
  488. resol = FRAMESIZE_QVGA; // 320x240
  489. if (strcmp(_size, "VGA") == 0)
  490. resol = FRAMESIZE_VGA; // 640x480
  491. if (strcmp(_size, "SVGA") == 0)
  492. resol = FRAMESIZE_SVGA; // 800x600
  493. if (strcmp(_size, "XGA") == 0)
  494. resol = FRAMESIZE_XGA; // 1024x768
  495. if (strcmp(_size, "SXGA") == 0)
  496. resol = FRAMESIZE_SXGA; // 1280x1024
  497. if (strcmp(_size, "UXGA") == 0)
  498. resol = FRAMESIZE_UXGA; // 1600x1200
  499. }
  500. if (httpd_query_key_value(_query, "quality", _qual, 10) == ESP_OK)
  501. {
  502. #ifdef DEBUG_DETAIL_ON
  503. printf("Quality: "); printf(_qual); printf("\n");
  504. #endif
  505. qual = atoi(_qual);
  506. if (qual > 63)
  507. qual = 63;
  508. if (qual < 0)
  509. qual = 0;
  510. }
  511. };
  512. }
  513. framesize_t CCamera::TextToFramesize(const char * _size)
  514. {
  515. if (strcmp(_size, "QVGA") == 0)
  516. return FRAMESIZE_QVGA; // 320x240
  517. if (strcmp(_size, "VGA") == 0)
  518. return FRAMESIZE_VGA; // 640x480
  519. if (strcmp(_size, "SVGA") == 0)
  520. return FRAMESIZE_SVGA; // 800x600
  521. if (strcmp(_size, "XGA") == 0)
  522. return FRAMESIZE_XGA; // 1024x768
  523. if (strcmp(_size, "SXGA") == 0)
  524. return FRAMESIZE_SXGA; // 1280x1024
  525. if (strcmp(_size, "UXGA") == 0)
  526. return FRAMESIZE_UXGA; // 1600x1200
  527. return ActualResolution;
  528. }
  529. CCamera::CCamera()
  530. {
  531. #ifdef DEBUG_DETAIL_ON
  532. printf("CreateClassCamera\n");
  533. #endif
  534. brightness = -5;
  535. contrast = -5;
  536. saturation = -5;
  537. isFixedExposure = false;
  538. ledc_init();
  539. }
  540. esp_err_t CCamera::InitCam()
  541. {
  542. printf("Init Camera\n");
  543. ActualQuality = camera_config.jpeg_quality;
  544. ActualResolution = camera_config.frame_size;
  545. //initialize the camera
  546. esp_err_t err = esp_camera_init(&camera_config);
  547. if (err != ESP_OK) {
  548. ESP_LOGE(TAGCAMERACLASS, "Camera Init Failed");
  549. return err;
  550. }
  551. return ESP_OK;
  552. }
  553. void CCamera::SetLEDIntensity(float _intrel)
  554. {
  555. _intrel = min(_intrel, (float) 100);
  556. _intrel = max(_intrel, (float) 0);
  557. _intrel = _intrel / 100;
  558. led_intensity = (int) (_intrel * 8191);
  559. printf("Set led_intensity to %d of 8191\n", led_intensity);
  560. }