ClassControllCamera.cpp 16 KB

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