ClassControllCamera.cpp 16 KB

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