ClassControllCamera.cpp 17 KB

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