ClassControllCamera.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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. LightOnOff(true);
  179. const TickType_t xDelay = flashdauer / portTICK_PERIOD_MS;
  180. vTaskDelay( xDelay );
  181. camera_fb_t * fb = esp_camera_fb_get();
  182. if (!fb) {
  183. ESP_LOGE(TAGCAMERACLASS, "Camera Capture Failed");
  184. doReboot();
  185. }
  186. esp_camera_fb_return(fb);
  187. sensor_t * s = esp_camera_sensor_get();
  188. s->set_gain_ctrl(s, 0);
  189. s->set_exposure_ctrl(s, 0);
  190. LEDOnOff(false);
  191. LightOnOff(false);
  192. isFixedExposure = true;
  193. waitbeforepicture_org = flashdauer;
  194. }
  195. esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
  196. {
  197. string ftype;
  198. uint8_t *zwischenspeicher = NULL;
  199. LEDOnOff(true);
  200. #ifdef DEBUG_DETAIL_ON
  201. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - Start");
  202. #endif
  203. if (delay > 0)
  204. {
  205. LightOnOff(true);
  206. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  207. vTaskDelay( xDelay );
  208. }
  209. #ifdef DEBUG_DETAIL_ON
  210. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After LightOn");
  211. #endif
  212. camera_fb_t * fb = esp_camera_fb_get();
  213. if (!fb) {
  214. ESP_LOGE(TAGCAMERACLASS, "Camera Capture Failed");
  215. LEDOnOff(false);
  216. doReboot();
  217. return ESP_FAIL;
  218. }
  219. int _size = fb->len;
  220. zwischenspeicher = (uint8_t*) malloc(_size);
  221. for (int i = 0; i < _size; ++i)
  222. *(zwischenspeicher + i) = *(fb->buf + i);
  223. esp_camera_fb_return(fb);
  224. #ifdef DEBUG_DETAIL_ON
  225. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After fb_get");
  226. #endif
  227. LEDOnOff(false);
  228. if (delay > 0)
  229. LightOnOff(false);
  230. // TickType_t xDelay = 1000 / portTICK_PERIOD_MS;
  231. // vTaskDelay( xDelay ); // wait for power to recover
  232. uint8_t * buf = NULL;
  233. CImageBasis _zwImage;
  234. _zwImage.LoadFromMemory(zwischenspeicher, _size);
  235. free(zwischenspeicher);
  236. #ifdef DEBUG_DETAIL_ON
  237. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After LoadFromMemory");
  238. #endif
  239. stbi_uc* p_target;
  240. stbi_uc* p_source;
  241. int channels = 3;
  242. int width = image_width;
  243. int height = image_height;
  244. #ifdef DEBUG_DETAIL_ON
  245. std::string _zw = "Targetimage: " + std::to_string((int) _Image->rgb_image) + " Size: " + std::to_string(_Image->width) + ", " + std::to_string(_Image->height);
  246. _zw = _zw + " _zwImage: " + std::to_string((int) _zwImage.rgb_image) + " Size: " + std::to_string(_zwImage.width) + ", " + std::to_string(_zwImage.height);
  247. LogFile.WriteToFile(_zw);
  248. #endif
  249. for (int x = 0; x < width; ++x)
  250. for (int y = 0; y < height; ++y)
  251. {
  252. p_target = _Image->rgb_image + (channels * (y * width + x));
  253. p_source = _zwImage.rgb_image + (channels * (y * width + x));
  254. p_target[0] = p_source[0];
  255. p_target[1] = p_source[1];
  256. p_target[2] = p_source[2];
  257. }
  258. #ifdef DEBUG_DETAIL_ON
  259. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After Copy To Target");
  260. #endif
  261. free(buf);
  262. #ifdef DEBUG_DETAIL_ON
  263. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - Done");
  264. #endif
  265. return ESP_OK;
  266. }
  267. esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
  268. {
  269. string ftype;
  270. LEDOnOff(true); // Abgeschaltet, um Strom zu sparen !!!!!!
  271. if (delay > 0)
  272. {
  273. LightOnOff(true);
  274. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  275. vTaskDelay( xDelay );
  276. }
  277. camera_fb_t * fb = esp_camera_fb_get();
  278. if (!fb) {
  279. ESP_LOGE(TAGCAMERACLASS, "Camera Capture Failed");
  280. ESP_LOGE(TAGCAMERACLASS, "Reboot ?????");
  281. LEDOnOff(false);
  282. LightOnOff(false);
  283. doReboot();
  284. return ESP_FAIL;
  285. }
  286. LEDOnOff(false);
  287. #ifdef DEBUG_DETAIL_ON
  288. printf("w %d, h %d, size %d\n", fb->width, fb->height, fb->len);
  289. #endif
  290. nm = FormatFileName(nm);
  291. #ifdef DEBUG_DETAIL_ON
  292. printf("Save Camera to : %s\n", nm.c_str());
  293. #endif
  294. ftype = toUpper(getFileType(nm));
  295. #ifdef DEBUG_DETAIL_ON
  296. printf("Filetype: %s\n", ftype.c_str());
  297. #endif
  298. uint8_t * buf = NULL;
  299. size_t buf_len = 0;
  300. bool converted = false;
  301. if (ftype.compare("BMP") == 0)
  302. {
  303. frame2bmp(fb, &buf, &buf_len);
  304. converted = true;
  305. }
  306. if (ftype.compare("JPG") == 0)
  307. {
  308. if(fb->format != PIXFORMAT_JPEG){
  309. bool jpeg_converted = frame2jpg(fb, ActualQuality, &buf, &buf_len);
  310. converted = true;
  311. if(!jpeg_converted){
  312. ESP_LOGE(TAGCAMERACLASS, "JPEG compression failed");
  313. }
  314. } else {
  315. buf_len = fb->len;
  316. buf = fb->buf;
  317. }
  318. }
  319. FILE * fp = OpenFileAndWait(nm.c_str(), "wb");
  320. if (fp == NULL) /* If an error occurs during the file creation */
  321. {
  322. fprintf(stderr, "fopen() failed for '%s'\n", nm.c_str());
  323. }
  324. else
  325. {
  326. fwrite(buf, sizeof(uint8_t), buf_len, fp);
  327. fclose(fp);
  328. }
  329. if (converted)
  330. free(buf);
  331. esp_camera_fb_return(fb);
  332. if (delay > 0)
  333. {
  334. LightOnOff(false);
  335. }
  336. return ESP_OK;
  337. }
  338. esp_err_t CCamera::CaptureToHTTP(httpd_req_t *req, int delay)
  339. {
  340. camera_fb_t * fb = NULL;
  341. esp_err_t res = ESP_OK;
  342. size_t fb_len = 0;
  343. int64_t fr_start = esp_timer_get_time();
  344. LEDOnOff(true);
  345. if (delay > 0)
  346. {
  347. LightOnOff(true);
  348. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  349. vTaskDelay( xDelay );
  350. }
  351. fb = esp_camera_fb_get();
  352. if (!fb) {
  353. ESP_LOGE(TAGCAMERACLASS, "Camera capture failed");
  354. LightOnOff(false);
  355. httpd_resp_send_500(req);
  356. // doReboot();
  357. return ESP_FAIL;
  358. }
  359. LEDOnOff(false);
  360. res = httpd_resp_set_type(req, "image/jpeg");
  361. if(res == ESP_OK){
  362. res = httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=raw.jpg");
  363. }
  364. if(res == ESP_OK){
  365. if(fb->format == PIXFORMAT_JPEG){
  366. fb_len = fb->len;
  367. res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
  368. } else {
  369. jpg_chunking_t jchunk = {req, 0};
  370. res = frame2jpg_cb(fb, 80, jpg_encode_stream, &jchunk)?ESP_OK:ESP_FAIL;
  371. httpd_resp_send_chunk(req, NULL, 0);
  372. fb_len = jchunk.len;
  373. }
  374. }
  375. esp_camera_fb_return(fb);
  376. int64_t fr_end = esp_timer_get_time();
  377. ESP_LOGI(TAGCAMERACLASS, "JPG: %uKB %ums", (uint32_t)(fb_len/1024), (uint32_t)((fr_end - fr_start)/1000));
  378. if (delay > 0)
  379. {
  380. LightOnOff(false);
  381. }
  382. return res;
  383. }
  384. void CCamera::LightOnOff(bool status)
  385. {
  386. // Init the GPIO
  387. gpio_pad_select_gpio(FLASH_GPIO);
  388. /* Set the GPIO as a push/pull output */
  389. gpio_set_direction(FLASH_GPIO, GPIO_MODE_OUTPUT);
  390. if (status)
  391. gpio_set_level(FLASH_GPIO, 1);
  392. else
  393. gpio_set_level(FLASH_GPIO, 0);
  394. }
  395. void CCamera::LEDOnOff(bool status)
  396. {
  397. // Init the GPIO
  398. gpio_pad_select_gpio(BLINK_GPIO);
  399. /* Set the GPIO as a push/pull output */
  400. gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
  401. if (!status)
  402. gpio_set_level(BLINK_GPIO, 1);
  403. else
  404. gpio_set_level(BLINK_GPIO, 0);
  405. }
  406. void CCamera::GetCameraParameter(httpd_req_t *req, int &qual, framesize_t &resol)
  407. {
  408. char _query[100];
  409. char _qual[10];
  410. char _size[10];
  411. resol = ActualResolution;
  412. qual = ActualQuality;
  413. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  414. {
  415. printf("Query: "); printf(_query); printf("\n");
  416. if (httpd_query_key_value(_query, "size", _size, 10) == ESP_OK)
  417. {
  418. #ifdef DEBUG_DETAIL_ON
  419. printf("Size: "); printf(_size); printf("\n");
  420. #endif
  421. if (strcmp(_size, "QVGA") == 0)
  422. resol = FRAMESIZE_QVGA; // 320x240
  423. if (strcmp(_size, "VGA") == 0)
  424. resol = FRAMESIZE_VGA; // 640x480
  425. if (strcmp(_size, "SVGA") == 0)
  426. resol = FRAMESIZE_SVGA; // 800x600
  427. if (strcmp(_size, "XGA") == 0)
  428. resol = FRAMESIZE_XGA; // 1024x768
  429. if (strcmp(_size, "SXGA") == 0)
  430. resol = FRAMESIZE_SXGA; // 1280x1024
  431. if (strcmp(_size, "UXGA") == 0)
  432. resol = FRAMESIZE_UXGA; // 1600x1200
  433. }
  434. if (httpd_query_key_value(_query, "quality", _qual, 10) == ESP_OK)
  435. {
  436. #ifdef DEBUG_DETAIL_ON
  437. printf("Quality: "); printf(_qual); printf("\n");
  438. #endif
  439. qual = atoi(_qual);
  440. if (qual > 63)
  441. qual = 63;
  442. if (qual < 0)
  443. qual = 0;
  444. }
  445. };
  446. }
  447. framesize_t CCamera::TextToFramesize(const char * _size)
  448. {
  449. if (strcmp(_size, "QVGA") == 0)
  450. return FRAMESIZE_QVGA; // 320x240
  451. if (strcmp(_size, "VGA") == 0)
  452. return FRAMESIZE_VGA; // 640x480
  453. if (strcmp(_size, "SVGA") == 0)
  454. return FRAMESIZE_SVGA; // 800x600
  455. if (strcmp(_size, "XGA") == 0)
  456. return FRAMESIZE_XGA; // 1024x768
  457. if (strcmp(_size, "SXGA") == 0)
  458. return FRAMESIZE_SXGA; // 1280x1024
  459. if (strcmp(_size, "UXGA") == 0)
  460. return FRAMESIZE_UXGA; // 1600x1200
  461. return ActualResolution;
  462. }
  463. CCamera::CCamera()
  464. {
  465. #ifdef DEBUG_DETAIL_ON
  466. printf("CreateClassCamera\n");
  467. #endif
  468. brightness = -5;
  469. contrast = -5;
  470. saturation = -5;
  471. isFixedExposure = false;
  472. }
  473. esp_err_t CCamera::InitCam()
  474. {
  475. if(CAM_PIN_PWDN != -1){
  476. // Init the GPIO
  477. gpio_pad_select_gpio(CAM_PIN_PWDN);
  478. /* Set the GPIO as a push/pull output */
  479. gpio_set_direction(CAM_PIN_PWDN, GPIO_MODE_OUTPUT);
  480. gpio_set_level(CAM_PIN_PWDN, 0);
  481. }
  482. printf("Init Camera\n");
  483. ActualQuality = camera_config.jpeg_quality;
  484. ActualResolution = camera_config.frame_size;
  485. //initialize the camera
  486. esp_err_t err = esp_camera_init(&camera_config);
  487. if (err != ESP_OK) {
  488. ESP_LOGE(TAGCAMERACLASS, "Camera Init Failed");
  489. return err;
  490. }
  491. return ESP_OK;
  492. }