ClassControllCamera.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. #define BOARD_ESP32CAM_AITHINKER
  11. #include <esp_event_loop.h>
  12. #include <esp_log.h>
  13. #include <esp_system.h>
  14. #include <nvs_flash.h>
  15. #include <sys/param.h>
  16. #include <string.h>
  17. #include "freertos/FreeRTOS.h"
  18. #include "freertos/task.h"
  19. #include "esp_camera.h"
  20. // #define DEBUG_DETAIL_ON
  21. // ESP32Cam (AiThinker) PIN Map
  22. #define CAM_PIN_PWDN (gpio_num_t) 32
  23. #define CAM_PIN_RESET -1 //software reset will be performed
  24. #define CAM_PIN_XCLK 0
  25. #define CAM_PIN_SIOD 26
  26. #define CAM_PIN_SIOC 27
  27. #define CAM_PIN_D7 35
  28. #define CAM_PIN_D6 34
  29. #define CAM_PIN_D5 39
  30. #define CAM_PIN_D4 36
  31. #define CAM_PIN_D3 21
  32. #define CAM_PIN_D2 19
  33. #define CAM_PIN_D1 18
  34. #define CAM_PIN_D0 5
  35. #define CAM_PIN_VSYNC 25
  36. #define CAM_PIN_HREF 23
  37. #define CAM_PIN_PCLK 22
  38. static const char *TAG = "example:take_picture";
  39. static camera_config_t camera_config = {
  40. .pin_pwdn = CAM_PIN_PWDN,
  41. .pin_reset = CAM_PIN_RESET,
  42. .pin_xclk = CAM_PIN_XCLK,
  43. .pin_sscb_sda = CAM_PIN_SIOD,
  44. .pin_sscb_scl = CAM_PIN_SIOC,
  45. .pin_d7 = CAM_PIN_D7,
  46. .pin_d6 = CAM_PIN_D6,
  47. .pin_d5 = CAM_PIN_D5,
  48. .pin_d4 = CAM_PIN_D4,
  49. .pin_d3 = CAM_PIN_D3,
  50. .pin_d2 = CAM_PIN_D2,
  51. .pin_d1 = CAM_PIN_D1,
  52. .pin_d0 = CAM_PIN_D0,
  53. .pin_vsync = CAM_PIN_VSYNC,
  54. .pin_href = CAM_PIN_HREF,
  55. .pin_pclk = CAM_PIN_PCLK,
  56. //XCLK 20MHz or 10MHz for OV2640 double FPS (Experimental)
  57. // .xclk_freq_hz = 20000000, // Orginalwert
  58. .xclk_freq_hz = 5000000, // Test, um die Bildfehler los zu werden !!!!
  59. .ledc_timer = LEDC_TIMER_0,
  60. .ledc_channel = LEDC_CHANNEL_0,
  61. .pixel_format = PIXFORMAT_JPEG, //YUV422,GRAYSCALE,RGB565,JPEG
  62. .frame_size = FRAMESIZE_VGA, //QQVGA-UXGA Do not use sizes above QVGA when not JPEG
  63. // .frame_size = FRAMESIZE_UXGA, //QQVGA-UXGA Do not use sizes above QVGA when not JPEG
  64. .jpeg_quality = 5, //0-63 lower number means higher quality
  65. .fb_count = 1 //if more than one, i2s runs in continuous mode. Use only with JPEG
  66. };
  67. #include "driver/ledc.h"
  68. CCamera Camera;
  69. #define FLASH_GPIO GPIO_NUM_4
  70. #define BLINK_GPIO GPIO_NUM_33
  71. typedef struct {
  72. httpd_req_t *req;
  73. size_t len;
  74. } jpg_chunking_t;
  75. #define LEDC_LS_CH2_GPIO (4)
  76. #define LEDC_LS_CH2_CHANNEL LEDC_CHANNEL_2
  77. #define LEDC_LS_TIMER LEDC_TIMER_1
  78. #define LEDC_LS_MODE LEDC_LOW_SPEED_MODE
  79. #define LEDC_TEST_DUTY (4000)
  80. void test(){
  81. ledc_channel_config_t ledc_channel = { };
  82. ledc_channel.channel = LEDC_LS_CH2_CHANNEL;
  83. ledc_channel.duty = 0;
  84. ledc_channel.gpio_num = FLASH_GPIO;
  85. ledc_channel.speed_mode = LEDC_LS_MODE;
  86. ledc_channel.hpoint = 0;
  87. ledc_channel.timer_sel = LEDC_LS_TIMER;
  88. ledc_channel_config(&ledc_channel);
  89. ledc_set_duty(ledc_channel.speed_mode, ledc_channel.channel, LEDC_TEST_DUTY);
  90. ledc_update_duty(ledc_channel.speed_mode, ledc_channel.channel);
  91. vTaskDelay(1000 / portTICK_PERIOD_MS);
  92. };
  93. static size_t jpg_encode_stream(void * arg, size_t index, const void* data, size_t len){
  94. jpg_chunking_t *j = (jpg_chunking_t *)arg;
  95. if(!index){
  96. j->len = 0;
  97. }
  98. if(httpd_resp_send_chunk(j->req, (const char *)data, len) != ESP_OK){
  99. return 0;
  100. }
  101. j->len += len;
  102. return len;
  103. }
  104. bool CCamera::SetBrightnessContrastSaturation(int _brightness, int _contrast, int _saturation)
  105. {
  106. bool result = false;
  107. sensor_t * s = esp_camera_sensor_get();
  108. if (_brightness > -100)
  109. _brightness = min(2, max(-2, _brightness));
  110. if (_contrast > -100)
  111. _contrast = min(2, max(-2, _contrast));
  112. // _saturation = min(2, max(-2, _saturation));
  113. // s->set_saturation(s, _saturation);
  114. if (_contrast > -100)
  115. s->set_contrast(s, _contrast);
  116. if (_brightness > -100)
  117. s->set_brightness(s, _brightness);
  118. if ((_brightness != brightness) && (_brightness > -100))
  119. result = true;
  120. if ((_contrast != contrast) && (_contrast > -100))
  121. result = true;
  122. if ((_saturation != saturation) && (_saturation > -100))
  123. result = true;
  124. if (_brightness > -100)
  125. brightness = _brightness;
  126. if (_contrast > -100)
  127. contrast = _contrast;
  128. if (_saturation > -100)
  129. saturation = _saturation;
  130. if (result && isFixedExposure)
  131. EnableAutoExposure(waitbeforepicture_org);
  132. return result;
  133. }
  134. void CCamera::SetQualitySize(int qual, framesize_t resol)
  135. {
  136. sensor_t * s = esp_camera_sensor_get();
  137. s->set_quality(s, qual);
  138. s->set_framesize(s, resol);
  139. ActualResolution = resol;
  140. ActualQuality = qual;
  141. if (resol == FRAMESIZE_QVGA)
  142. {
  143. image_height = 240;
  144. image_width = 320;
  145. }
  146. if (resol == FRAMESIZE_VGA)
  147. {
  148. image_height = 480;
  149. image_width = 640;
  150. }
  151. // No higher Mode than VGA, damit der Kameraspeicher ausreicht.
  152. /*
  153. if (resol == FRAMESIZE_SVGA)
  154. {
  155. image_height = 600;
  156. image_width = 800;
  157. }
  158. if (resol == FRAMESIZE_XGA)
  159. {
  160. image_height = 768;
  161. image_width = 1024;
  162. }
  163. if (resol == FRAMESIZE_SXGA)
  164. {
  165. image_height = 1024;
  166. image_width = 1280;
  167. }
  168. if (resol == FRAMESIZE_UXGA)
  169. {
  170. image_height = 1200;
  171. image_width = 1600;
  172. }
  173. */
  174. }
  175. void CCamera::EnableAutoExposure(int flashdauer)
  176. {
  177. LEDOnOff(true);
  178. if (flashdauer > 0)
  179. LightOnOff(true);
  180. const TickType_t xDelay = flashdauer / portTICK_PERIOD_MS;
  181. vTaskDelay( xDelay );
  182. camera_fb_t * fb = esp_camera_fb_get();
  183. if (!fb) {
  184. ESP_LOGE(TAGCAMERACLASS, "Camera Capture Failed");
  185. LEDOnOff(false);
  186. LightOnOff(false);
  187. doReboot();
  188. }
  189. esp_camera_fb_return(fb);
  190. sensor_t * s = esp_camera_sensor_get();
  191. s->set_gain_ctrl(s, 0);
  192. s->set_exposure_ctrl(s, 0);
  193. LEDOnOff(false);
  194. LightOnOff(false);
  195. isFixedExposure = true;
  196. waitbeforepicture_org = flashdauer;
  197. }
  198. esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
  199. {
  200. string ftype;
  201. uint8_t *zwischenspeicher = NULL;
  202. LEDOnOff(true);
  203. #ifdef DEBUG_DETAIL_ON
  204. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - Start");
  205. #endif
  206. if (delay > 0)
  207. {
  208. LightOnOff(true);
  209. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  210. vTaskDelay( xDelay );
  211. }
  212. #ifdef DEBUG_DETAIL_ON
  213. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After LightOn");
  214. #endif
  215. camera_fb_t * fb = esp_camera_fb_get();
  216. if (!fb) {
  217. ESP_LOGE(TAGCAMERACLASS, "Camera Capture Failed");
  218. LEDOnOff(false);
  219. LightOnOff(false);
  220. doReboot();
  221. return ESP_FAIL;
  222. }
  223. int _size = fb->len;
  224. zwischenspeicher = (uint8_t*) malloc(_size);
  225. for (int i = 0; i < _size; ++i)
  226. *(zwischenspeicher + i) = *(fb->buf + i);
  227. esp_camera_fb_return(fb);
  228. #ifdef DEBUG_DETAIL_ON
  229. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After fb_get");
  230. #endif
  231. LEDOnOff(false);
  232. if (delay > 0)
  233. LightOnOff(false);
  234. // TickType_t xDelay = 1000 / portTICK_PERIOD_MS;
  235. // vTaskDelay( xDelay ); // wait for power to recover
  236. uint8_t * buf = NULL;
  237. CImageBasis _zwImage;
  238. _zwImage.LoadFromMemory(zwischenspeicher, _size);
  239. free(zwischenspeicher);
  240. #ifdef DEBUG_DETAIL_ON
  241. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After LoadFromMemory");
  242. #endif
  243. stbi_uc* p_target;
  244. stbi_uc* p_source;
  245. int channels = 3;
  246. int width = image_width;
  247. int height = image_height;
  248. #ifdef DEBUG_DETAIL_ON
  249. std::string _zw = "Targetimage: " + std::to_string((int) _Image->rgb_image) + " Size: " + std::to_string(_Image->width) + ", " + std::to_string(_Image->height);
  250. _zw = _zw + " _zwImage: " + std::to_string((int) _zwImage.rgb_image) + " Size: " + std::to_string(_zwImage.width) + ", " + std::to_string(_zwImage.height);
  251. LogFile.WriteToFile(_zw);
  252. #endif
  253. for (int x = 0; x < width; ++x)
  254. for (int y = 0; y < height; ++y)
  255. {
  256. p_target = _Image->rgb_image + (channels * (y * width + x));
  257. p_source = _zwImage.rgb_image + (channels * (y * width + x));
  258. p_target[0] = p_source[0];
  259. p_target[1] = p_source[1];
  260. p_target[2] = p_source[2];
  261. }
  262. #ifdef DEBUG_DETAIL_ON
  263. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After Copy To Target");
  264. #endif
  265. free(buf);
  266. #ifdef DEBUG_DETAIL_ON
  267. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - Done");
  268. #endif
  269. return ESP_OK;
  270. }
  271. esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
  272. {
  273. string ftype;
  274. LEDOnOff(true); // Abgeschaltet, um Strom zu sparen !!!!!!
  275. if (delay > 0)
  276. {
  277. LightOnOff(true);
  278. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  279. vTaskDelay( xDelay );
  280. }
  281. camera_fb_t * fb = esp_camera_fb_get();
  282. if (!fb) {
  283. ESP_LOGE(TAGCAMERACLASS, "Camera Capture Failed");
  284. ESP_LOGE(TAGCAMERACLASS, "Reboot ?????");
  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. // Init the GPIO
  392. gpio_pad_select_gpio(FLASH_GPIO);
  393. /* Set the GPIO as a push/pull output */
  394. gpio_set_direction(FLASH_GPIO, GPIO_MODE_OUTPUT);
  395. if (status)
  396. gpio_set_level(FLASH_GPIO, 1);
  397. else
  398. gpio_set_level(FLASH_GPIO, 0);
  399. }
  400. void CCamera::LEDOnOff(bool status)
  401. {
  402. // Init the GPIO
  403. gpio_pad_select_gpio(BLINK_GPIO);
  404. /* Set the GPIO as a push/pull output */
  405. gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
  406. if (!status)
  407. gpio_set_level(BLINK_GPIO, 1);
  408. else
  409. gpio_set_level(BLINK_GPIO, 0);
  410. }
  411. void CCamera::GetCameraParameter(httpd_req_t *req, int &qual, framesize_t &resol)
  412. {
  413. char _query[100];
  414. char _qual[10];
  415. char _size[10];
  416. resol = ActualResolution;
  417. qual = ActualQuality;
  418. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  419. {
  420. printf("Query: "); printf(_query); printf("\n");
  421. if (httpd_query_key_value(_query, "size", _size, 10) == ESP_OK)
  422. {
  423. #ifdef DEBUG_DETAIL_ON
  424. printf("Size: "); printf(_size); printf("\n");
  425. #endif
  426. if (strcmp(_size, "QVGA") == 0)
  427. resol = FRAMESIZE_QVGA; // 320x240
  428. if (strcmp(_size, "VGA") == 0)
  429. resol = FRAMESIZE_VGA; // 640x480
  430. if (strcmp(_size, "SVGA") == 0)
  431. resol = FRAMESIZE_SVGA; // 800x600
  432. if (strcmp(_size, "XGA") == 0)
  433. resol = FRAMESIZE_XGA; // 1024x768
  434. if (strcmp(_size, "SXGA") == 0)
  435. resol = FRAMESIZE_SXGA; // 1280x1024
  436. if (strcmp(_size, "UXGA") == 0)
  437. resol = FRAMESIZE_UXGA; // 1600x1200
  438. }
  439. if (httpd_query_key_value(_query, "quality", _qual, 10) == ESP_OK)
  440. {
  441. #ifdef DEBUG_DETAIL_ON
  442. printf("Quality: "); printf(_qual); printf("\n");
  443. #endif
  444. qual = atoi(_qual);
  445. if (qual > 63)
  446. qual = 63;
  447. if (qual < 0)
  448. qual = 0;
  449. }
  450. };
  451. }
  452. framesize_t CCamera::TextToFramesize(const char * _size)
  453. {
  454. if (strcmp(_size, "QVGA") == 0)
  455. return FRAMESIZE_QVGA; // 320x240
  456. if (strcmp(_size, "VGA") == 0)
  457. return FRAMESIZE_VGA; // 640x480
  458. if (strcmp(_size, "SVGA") == 0)
  459. return FRAMESIZE_SVGA; // 800x600
  460. if (strcmp(_size, "XGA") == 0)
  461. return FRAMESIZE_XGA; // 1024x768
  462. if (strcmp(_size, "SXGA") == 0)
  463. return FRAMESIZE_SXGA; // 1280x1024
  464. if (strcmp(_size, "UXGA") == 0)
  465. return FRAMESIZE_UXGA; // 1600x1200
  466. return ActualResolution;
  467. }
  468. CCamera::CCamera()
  469. {
  470. #ifdef DEBUG_DETAIL_ON
  471. printf("CreateClassCamera\n");
  472. #endif
  473. brightness = -5;
  474. contrast = -5;
  475. saturation = -5;
  476. isFixedExposure = false;
  477. }
  478. esp_err_t CCamera::InitCam()
  479. {
  480. if(CAM_PIN_PWDN != -1){
  481. // Init the GPIO
  482. gpio_pad_select_gpio(CAM_PIN_PWDN);
  483. /* Set the GPIO as a push/pull output */
  484. gpio_set_direction(CAM_PIN_PWDN, GPIO_MODE_OUTPUT);
  485. gpio_set_level(CAM_PIN_PWDN, 0);
  486. }
  487. printf("Init Camera\n");
  488. ActualQuality = camera_config.jpeg_quality;
  489. ActualResolution = camera_config.frame_size;
  490. //initialize the camera
  491. esp_err_t err = esp_camera_init(&camera_config);
  492. if (err != ESP_OK) {
  493. ESP_LOGE(TAGCAMERACLASS, "Camera Init Failed");
  494. return err;
  495. }
  496. return ESP_OK;
  497. }