ClassControllCamera.cpp 19 KB

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