ClassControllCamera.cpp 15 KB

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