ClassControllCamera.cpp 19 KB

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