ClassControllCamera.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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 "statusled.h"
  9. #include "CImageBasis.h"
  10. #include "server_ota.h"
  11. #include "server_GPIO.h"
  12. #include "../../include/defines.h"
  13. #include <esp_event.h>
  14. #include <esp_log.h>
  15. #include <esp_system.h>
  16. #include <nvs_flash.h>
  17. #include <sys/param.h>
  18. #include <string.h>
  19. #include "freertos/FreeRTOS.h"
  20. #include "freertos/task.h"
  21. #include "esp_camera.h"
  22. #include "driver/ledc.h"
  23. #include "MainFlowControl.h"
  24. static const char *TAG = "CAM";
  25. /* Camera live stream */
  26. #define PART_BOUNDARY "123456789000000000000987654321"
  27. static const char* _STREAM_CONTENT_TYPE = "multipart/x-mixed-replace;boundary=" PART_BOUNDARY;
  28. static const char* _STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n";
  29. static const char* _STREAM_PART = "Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n";
  30. static camera_config_t camera_config = {
  31. .pin_pwdn = CAM_PIN_PWDN,
  32. .pin_reset = CAM_PIN_RESET,
  33. .pin_xclk = CAM_PIN_XCLK,
  34. .pin_sscb_sda = CAM_PIN_SIOD,
  35. .pin_sscb_scl = CAM_PIN_SIOC,
  36. .pin_d7 = CAM_PIN_D7,
  37. .pin_d6 = CAM_PIN_D6,
  38. .pin_d5 = CAM_PIN_D5,
  39. .pin_d4 = CAM_PIN_D4,
  40. .pin_d3 = CAM_PIN_D3,
  41. .pin_d2 = CAM_PIN_D2,
  42. .pin_d1 = CAM_PIN_D1,
  43. .pin_d0 = CAM_PIN_D0,
  44. .pin_vsync = CAM_PIN_VSYNC,
  45. .pin_href = CAM_PIN_HREF,
  46. .pin_pclk = CAM_PIN_PCLK,
  47. //XCLK 20MHz or 10MHz for OV2640 double FPS (Experimental)
  48. .xclk_freq_hz = 20000000, // Orginal value
  49. // .xclk_freq_hz = 5000000, // Test to get rid of the image errors !!!! Hangs in version 9.2 !!!!
  50. .ledc_timer = LEDC_TIMER_0,
  51. .ledc_channel = LEDC_CHANNEL_0,
  52. .pixel_format = PIXFORMAT_JPEG, //YUV422,GRAYSCALE,RGB565,JPEG
  53. .frame_size = FRAMESIZE_VGA, //QQVGA-UXGA Do not use sizes above QVGA when not JPEG
  54. // .frame_size = FRAMESIZE_UXGA, //QQVGA-UXGA Do not use sizes above QVGA when not JPEG
  55. .jpeg_quality = 12, //0-63 lower number means higher quality
  56. .fb_count = 1, //if more than one, i2s runs in continuous mode. Use only with JPEG
  57. .fb_location = CAMERA_FB_IN_PSRAM, /*!< The location where the frame buffer will be allocated */
  58. .grab_mode = CAMERA_GRAB_LATEST, // only from new esp32cam version
  59. };
  60. CCamera Camera;
  61. uint8_t *demoImage = NULL; // Buffer holding the demo image in bytes
  62. #define DEMO_IMAGE_SIZE 30000 // Max size of demo image in bytes
  63. typedef struct {
  64. httpd_req_t *req;
  65. size_t len;
  66. } jpg_chunking_t;
  67. bool CCamera::testCamera(void) {
  68. bool success;
  69. camera_fb_t *fb = esp_camera_fb_get();
  70. if (fb) {
  71. success = true;
  72. }
  73. else {
  74. success = false;
  75. }
  76. esp_camera_fb_return(fb);
  77. return success;
  78. }
  79. void CCamera::ledc_init(void)
  80. {
  81. #ifdef USE_PWM_LEDFLASH
  82. // Prepare and then apply the LEDC PWM timer configuration
  83. ledc_timer_config_t ledc_timer = { };
  84. ledc_timer.speed_mode = LEDC_MODE;
  85. ledc_timer.timer_num = LEDC_TIMER;
  86. ledc_timer.duty_resolution = LEDC_DUTY_RES;
  87. ledc_timer.freq_hz = LEDC_FREQUENCY; // Set output frequency at 5 kHz
  88. ledc_timer.clk_cfg = LEDC_AUTO_CLK;
  89. ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
  90. // Prepare and then apply the LEDC PWM channel configuration
  91. ledc_channel_config_t ledc_channel = { };
  92. ledc_channel.speed_mode = LEDC_MODE;
  93. ledc_channel.channel = LEDC_CHANNEL;
  94. ledc_channel.timer_sel = LEDC_TIMER;
  95. ledc_channel.intr_type = LEDC_INTR_DISABLE;
  96. ledc_channel.gpio_num = LEDC_OUTPUT_IO;
  97. ledc_channel.duty = 0; // Set duty to 0%
  98. ledc_channel.hpoint = 0;
  99. ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
  100. #endif
  101. }
  102. static size_t jpg_encode_stream(void * arg, size_t index, const void* data, size_t len)
  103. {
  104. jpg_chunking_t *j = (jpg_chunking_t *)arg;
  105. if(!index) {
  106. j->len = 0;
  107. }
  108. if(httpd_resp_send_chunk(j->req, (const char *)data, len) != ESP_OK) {
  109. return 0;
  110. }
  111. j->len += len;
  112. return len;
  113. }
  114. bool CCamera::SetBrightnessContrastSaturation(int _brightness, int _contrast, int _saturation)
  115. {
  116. _brightness = min(2, max(-2, _brightness));
  117. _contrast = min(2, max(-2, _contrast));
  118. _saturation = min(2, max(-2, _saturation));
  119. sensor_t * s = esp_camera_sensor_get();
  120. if (s) {
  121. s->set_saturation(s, _saturation);
  122. s->set_contrast(s, _contrast);
  123. s->set_brightness(s, _brightness);
  124. /* Workaround - bug in cam library - enable bits are set without using bitwise OR logic -> only latest enable setting is used */
  125. /* Library version: https://github.com/espressif/esp32-camera/commit/5c8349f4cf169c8a61283e0da9b8cff10994d3f3 */
  126. /* Reference: https://esp32.com/viewtopic.php?f=19&t=14376#p93178 */
  127. /* The memory structure is as follows for
  128. byte_0 = enable_bits
  129. byte_0->bit0 = enable saturation and hue --> OK
  130. byte_0->bit1 = enable saturation --> OK
  131. byte_0->bit2 = enable brightness and contrast --> OK
  132. byte_0->bit3 = enable green -> blue spitial effect (Antique and blunish and greenish and readdish and b&w) enable
  133. byte_0->bit4 = anable gray -> read spitial effect (Antique and blunish and greenish and readdish and b&w) enable
  134. byte_0->bit5 = remove (UV) in YUV color system
  135. byte_0->bit6 = enable negative
  136. byte_0->bit7 = remove (Y) in YUV color system
  137. byte_1 = saturation1 0-255 --> ?
  138. byte_2 = hue 0-255 --> OK
  139. byte_3 = saturation2 0-255 --> OK
  140. byte_4 = reenter saturation2 in documents --> ?
  141. byte_5 = spital effect green -> blue 0-255 --> ?
  142. byte_6 = spital effect gray -> read 0-255 --> ?
  143. byte_7 = contrast lower byte 0-255 --> OK
  144. byte_8 = contrast higher byte 0-255 --> OK
  145. byte_9 = brightness 0-255 --> OK
  146. byte_10= if byte_10==4 contrast effective --> ?
  147. */
  148. //s->set_reg(s, 0x7C, 0xFF, 2); // Optional feature - hue setting: Select byte 2 in register 0x7C to set hue value
  149. //s->set_reg(s, 0x7D, 0xFF, 0); // Optional feature - hue setting: Hue value 0 - 255
  150. s->set_reg(s, 0xFF, 0x01, 0); // Select DSP bank
  151. s->set_reg(s, 0x7C, 0xFF, 0); // Select byte 0 in register 0x7C
  152. s->set_reg(s, 0x7D, 7, 7); // Set bit 0, 1, 2 in register 0x7D to enable saturation, contrast, brightness and hue control
  153. }
  154. else {
  155. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "SetBrightnessContrastSaturation: Failed to get control structure");
  156. }
  157. if (((_brightness != brightness) || (_contrast != contrast) || (_saturation != saturation)) && isFixedExposure)
  158. EnableAutoExposure(waitbeforepicture_org);
  159. brightness = _brightness;
  160. contrast = _contrast;
  161. saturation = _saturation;
  162. ESP_LOGD(TAG, "brightness %d, contrast: %d, saturation %d", brightness, contrast, saturation);
  163. return true;
  164. }
  165. void CCamera::SetQualitySize(int qual, framesize_t resol)
  166. {
  167. qual = min(63, max(8, qual)); // Limit quality from 8..63 (values lower than 8 tent to be unstable)
  168. sensor_t * s = esp_camera_sensor_get();
  169. if (s) {
  170. s->set_quality(s, qual);
  171. s->set_framesize(s, resol);
  172. }
  173. else {
  174. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "SetQualitySize: Failed to get control structure");
  175. }
  176. ActualResolution = resol;
  177. ActualQuality = qual;
  178. if (resol == FRAMESIZE_QVGA)
  179. {
  180. image_height = 240;
  181. image_width = 320;
  182. }
  183. else if (resol == FRAMESIZE_VGA)
  184. {
  185. image_height = 480;
  186. image_width = 640;
  187. }
  188. }
  189. void CCamera::EnableAutoExposure(int flash_duration)
  190. {
  191. ESP_LOGD(TAG, "EnableAutoExposure");
  192. LEDOnOff(true);
  193. if (flash_duration > 0) {
  194. LightOnOff(true);
  195. const TickType_t xDelay = flash_duration / portTICK_PERIOD_MS;
  196. vTaskDelay( xDelay );
  197. }
  198. camera_fb_t * fb = esp_camera_fb_get();
  199. esp_camera_fb_return(fb);
  200. fb = esp_camera_fb_get();
  201. if (!fb) {
  202. LEDOnOff(false);
  203. LightOnOff(false);
  204. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "EnableAutoExposure: Capture Failed. "
  205. "Check camera module and/or proper electrical connection");
  206. //doReboot();
  207. }
  208. esp_camera_fb_return(fb);
  209. sensor_t * s = esp_camera_sensor_get();
  210. if (s) {
  211. s->set_gain_ctrl(s, 0);
  212. s->set_exposure_ctrl(s, 0);
  213. }
  214. else {
  215. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "EnableAutoExposure: Failed to get control structure to set gain+exposure");
  216. }
  217. LEDOnOff(false);
  218. LightOnOff(false);
  219. isFixedExposure = true;
  220. waitbeforepicture_org = flash_duration;
  221. }
  222. esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
  223. {
  224. #ifdef DEBUG_DETAIL_ON
  225. LogFile.WriteHeapInfo("CaptureToBasisImage - Start");
  226. #endif
  227. _Image->EmptyImage(); //Delete previous stored raw image -> black image
  228. LEDOnOff(true);
  229. if (delay > 0) {
  230. LightOnOff(true);
  231. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  232. vTaskDelay( xDelay );
  233. }
  234. #ifdef DEBUG_DETAIL_ON
  235. LogFile.WriteHeapInfo("CaptureToBasisImage - After LightOn");
  236. #endif
  237. camera_fb_t * fb = esp_camera_fb_get();
  238. esp_camera_fb_return(fb);
  239. fb = esp_camera_fb_get();
  240. if (!fb) {
  241. LEDOnOff(false);
  242. LightOnOff(false);
  243. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "is not working anymore (CaptureToBasisImage) - most probably caused "
  244. "by a hardware problem (instablility, ...). System will reboot.");
  245. doReboot();
  246. return ESP_FAIL;
  247. }
  248. if (demoMode) { // Use images stored on SD-Card instead of camera image
  249. /* Replace Framebuffer with image from SD-Card */
  250. loadNextDemoImage(fb);
  251. }
  252. CImageBasis* _zwImage = new CImageBasis("zwImage");
  253. if (_zwImage) {
  254. _zwImage->LoadFromMemory(fb->buf, fb->len);
  255. }
  256. else {
  257. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: Can't allocate _zwImage");
  258. }
  259. esp_camera_fb_return(fb);
  260. #ifdef DEBUG_DETAIL_ON
  261. LogFile.WriteHeapInfo("CaptureToBasisImage - After fb_get");
  262. #endif
  263. LEDOnOff(false);
  264. if (delay > 0)
  265. LightOnOff(false);
  266. // TickType_t xDelay = 1000 / portTICK_PERIOD_MS;
  267. // vTaskDelay( xDelay ); // wait for power to recover
  268. #ifdef DEBUG_DETAIL_ON
  269. LogFile.WriteHeapInfo("CaptureToBasisImage - After LoadFromMemory");
  270. #endif
  271. stbi_uc* p_target;
  272. stbi_uc* p_source;
  273. int channels = 3;
  274. int width = image_width;
  275. int height = image_height;
  276. #ifdef DEBUG_DETAIL_ON
  277. std::string _zw = "Targetimage: " + std::to_string((int) _Image->rgb_image) + " Size: " + std::to_string(_Image->width) + ", " + std::to_string(_Image->height);
  278. _zw = _zw + " _zwImage: " + std::to_string((int) _zwImage->rgb_image) + " Size: " + std::to_string(_zwImage->width) + ", " + std::to_string(_zwImage->height);
  279. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, _zw);
  280. #endif
  281. for (int x = 0; x < width; ++x)
  282. for (int y = 0; y < height; ++y)
  283. {
  284. p_target = _Image->rgb_image + (channels * (y * width + x));
  285. p_source = _zwImage->rgb_image + (channels * (y * width + x));
  286. p_target[0] = p_source[0];
  287. p_target[1] = p_source[1];
  288. p_target[2] = p_source[2];
  289. }
  290. delete _zwImage;
  291. #ifdef DEBUG_DETAIL_ON
  292. LogFile.WriteHeapInfo("CaptureToBasisImage - Done");
  293. #endif
  294. return ESP_OK;
  295. }
  296. esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
  297. {
  298. string ftype;
  299. LEDOnOff(true); // Switched off to save power !
  300. if (delay > 0) {
  301. LightOnOff(true);
  302. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  303. vTaskDelay( xDelay );
  304. }
  305. camera_fb_t * fb = esp_camera_fb_get();
  306. esp_camera_fb_return(fb);
  307. fb = esp_camera_fb_get();
  308. if (!fb) {
  309. LEDOnOff(false);
  310. LightOnOff(false);
  311. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: Capture Failed. "
  312. "Check camera module and/or proper electrical connection");
  313. //doReboot();
  314. return ESP_FAIL;
  315. }
  316. LEDOnOff(false);
  317. #ifdef DEBUG_DETAIL_ON
  318. ESP_LOGD(TAG, "w %d, h %d, size %d", fb->width, fb->height, fb->len);
  319. #endif
  320. nm = FormatFileName(nm);
  321. #ifdef DEBUG_DETAIL_ON
  322. ESP_LOGD(TAG, "Save Camera to: %s", nm.c_str());
  323. #endif
  324. ftype = toUpper(getFileType(nm));
  325. #ifdef DEBUG_DETAIL_ON
  326. ESP_LOGD(TAG, "Filetype: %s", ftype.c_str());
  327. #endif
  328. uint8_t * buf = NULL;
  329. size_t buf_len = 0;
  330. bool converted = false;
  331. if (ftype.compare("BMP") == 0)
  332. {
  333. frame2bmp(fb, &buf, &buf_len);
  334. converted = true;
  335. }
  336. if (ftype.compare("JPG") == 0)
  337. {
  338. if(fb->format != PIXFORMAT_JPEG){
  339. bool jpeg_converted = frame2jpg(fb, ActualQuality, &buf, &buf_len);
  340. converted = true;
  341. if(!jpeg_converted){
  342. ESP_LOGE(TAG, "JPEG compression failed");
  343. }
  344. } else {
  345. buf_len = fb->len;
  346. buf = fb->buf;
  347. }
  348. }
  349. FILE * fp = fopen(nm.c_str(), "wb");
  350. if (fp == NULL) { // If an error occurs during the file creation
  351. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: Failed to open file " + nm);
  352. }
  353. else {
  354. fwrite(buf, sizeof(uint8_t), buf_len, fp);
  355. fclose(fp);
  356. }
  357. if (converted)
  358. free(buf);
  359. esp_camera_fb_return(fb);
  360. if (delay > 0)
  361. LightOnOff(false);
  362. return ESP_OK;
  363. }
  364. esp_err_t CCamera::CaptureToHTTP(httpd_req_t *req, int delay)
  365. {
  366. esp_err_t res = ESP_OK;
  367. size_t fb_len = 0;
  368. int64_t fr_start = esp_timer_get_time();
  369. LEDOnOff(true);
  370. if (delay > 0) {
  371. LightOnOff(true);
  372. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  373. vTaskDelay( xDelay );
  374. }
  375. camera_fb_t *fb = esp_camera_fb_get();
  376. esp_camera_fb_return(fb);
  377. fb = esp_camera_fb_get();
  378. if (!fb) {
  379. LEDOnOff(false);
  380. LightOnOff(false);
  381. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: Capture Failed. "
  382. "Check camera module and/or proper electrical connection");
  383. httpd_resp_send_500(req);
  384. // doReboot();
  385. return ESP_FAIL;
  386. }
  387. LEDOnOff(false);
  388. res = httpd_resp_set_type(req, "image/jpeg");
  389. if(res == ESP_OK){
  390. res = httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=raw.jpg");
  391. }
  392. if(res == ESP_OK){
  393. if (demoMode) { // Use images stored on SD-Card instead of camera image
  394. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Using Demo image!");
  395. /* Replace Framebuffer with image from SD-Card */
  396. loadNextDemoImage(fb);
  397. res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
  398. }
  399. else {
  400. if(fb->format == PIXFORMAT_JPEG){
  401. fb_len = fb->len;
  402. res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
  403. } else {
  404. jpg_chunking_t jchunk = {req, 0};
  405. res = frame2jpg_cb(fb, 80, jpg_encode_stream, &jchunk)?ESP_OK:ESP_FAIL;
  406. httpd_resp_send_chunk(req, NULL, 0);
  407. fb_len = jchunk.len;
  408. }
  409. }
  410. }
  411. esp_camera_fb_return(fb);
  412. int64_t fr_end = esp_timer_get_time();
  413. ESP_LOGI(TAG, "JPG: %uKB %ums", (uint32_t)(fb_len/1024), (uint32_t)((fr_end - fr_start)/1000));
  414. if (delay > 0)
  415. LightOnOff(false);
  416. return res;
  417. }
  418. esp_err_t CCamera::CaptureToStream(httpd_req_t *req, bool FlashlightOn)
  419. {
  420. esp_err_t res = ESP_OK;
  421. size_t fb_len = 0;
  422. int64_t fr_start;
  423. char * part_buf[64];
  424. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Live stream started");
  425. if (FlashlightOn) {
  426. LEDOnOff(true);
  427. LightOnOff(true);
  428. }
  429. //httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); //stream is blocking web interface, only serving to local
  430. httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
  431. httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
  432. while(1)
  433. {
  434. fr_start = esp_timer_get_time();
  435. camera_fb_t *fb = esp_camera_fb_get();
  436. esp_camera_fb_return(fb);
  437. fb = esp_camera_fb_get();
  438. if (!fb) {
  439. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToStream: Camera framebuffer not available");
  440. break;
  441. }
  442. fb_len = fb->len;
  443. if (res == ESP_OK){
  444. size_t hlen = snprintf((char *)part_buf, sizeof(part_buf), _STREAM_PART, fb_len);
  445. res = httpd_resp_send_chunk(req, (const char *)part_buf, hlen);
  446. }
  447. if (res == ESP_OK){
  448. res = httpd_resp_send_chunk(req, (const char *)fb->buf, fb_len);
  449. }
  450. if (res == ESP_OK){
  451. res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
  452. }
  453. esp_camera_fb_return(fb);
  454. int64_t fr_end = esp_timer_get_time();
  455. ESP_LOGD(TAG, "JPG: %uKB %ums", (uint32_t)(fb_len/1024), (uint32_t)((fr_end - fr_start)/1000));
  456. if (res != ESP_OK){ // Exit loop, e.g. also when closing the webpage
  457. break;
  458. }
  459. int64_t fr_delta_ms = (fr_end - fr_start) / 1000;
  460. if (CAM_LIVESTREAM_REFRESHRATE > fr_delta_ms) {
  461. const TickType_t xDelay = (CAM_LIVESTREAM_REFRESHRATE - fr_delta_ms) / portTICK_PERIOD_MS;
  462. ESP_LOGD(TAG, "Stream: sleep for: %ldms", (long) xDelay*10);
  463. vTaskDelay(xDelay);
  464. }
  465. }
  466. LEDOnOff(false);
  467. LightOnOff(false);
  468. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Live stream stopped");
  469. return res;
  470. }
  471. void CCamera::LightOnOff(bool status)
  472. {
  473. GpioHandler* gpioHandler = gpio_handler_get();
  474. if ((gpioHandler != NULL) && (gpioHandler->isEnabled())) {
  475. ESP_LOGD(TAG, "Use gpioHandler to trigger flashlight");
  476. gpioHandler->flashLightEnable(status);
  477. }
  478. else {
  479. #ifdef USE_PWM_LEDFLASH
  480. if (status) {
  481. ESP_LOGD(TAG, "Internal Flash-LED turn on with PWM %d", led_intensity);
  482. ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, led_intensity));
  483. // Update duty to apply the new value
  484. ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
  485. }
  486. else {
  487. ESP_LOGD(TAG, "Internal Flash-LED turn off PWM");
  488. ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, 0));
  489. ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
  490. }
  491. #else
  492. // Init the GPIO
  493. gpio_pad_select_gpio(FLASH_GPIO);
  494. // Set the GPIO as a push/pull output
  495. gpio_set_direction(FLASH_GPIO, GPIO_MODE_OUTPUT);
  496. if (status)
  497. gpio_set_level(FLASH_GPIO, 1);
  498. else
  499. gpio_set_level(FLASH_GPIO, 0);
  500. #endif
  501. }
  502. }
  503. void CCamera::LEDOnOff(bool status)
  504. {
  505. if (xHandle_task_StatusLED == NULL) {
  506. // Init the GPIO
  507. gpio_pad_select_gpio(BLINK_GPIO);
  508. /* Set the GPIO as a push/pull output */
  509. gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
  510. if (!status)
  511. gpio_set_level(BLINK_GPIO, 1);
  512. else
  513. gpio_set_level(BLINK_GPIO, 0);
  514. }
  515. }
  516. void CCamera::GetCameraParameter(httpd_req_t *req, int &qual, framesize_t &resol)
  517. {
  518. char _query[100];
  519. char _qual[10];
  520. char _size[10];
  521. resol = ActualResolution;
  522. qual = ActualQuality;
  523. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  524. {
  525. ESP_LOGD(TAG, "Query: %s", _query);
  526. if (httpd_query_key_value(_query, "size", _size, 10) == ESP_OK)
  527. {
  528. #ifdef DEBUG_DETAIL_ON
  529. ESP_LOGD(TAG, "Size: %s", _size);
  530. #endif
  531. if (strcmp(_size, "QVGA") == 0)
  532. resol = FRAMESIZE_QVGA; // 320x240
  533. else if (strcmp(_size, "VGA") == 0)
  534. resol = FRAMESIZE_VGA; // 640x480
  535. else if (strcmp(_size, "SVGA") == 0)
  536. resol = FRAMESIZE_SVGA; // 800x600
  537. else if (strcmp(_size, "XGA") == 0)
  538. resol = FRAMESIZE_XGA; // 1024x768
  539. else if (strcmp(_size, "SXGA") == 0)
  540. resol = FRAMESIZE_SXGA; // 1280x1024
  541. else if (strcmp(_size, "UXGA") == 0)
  542. resol = FRAMESIZE_UXGA; // 1600x1200
  543. }
  544. if (httpd_query_key_value(_query, "quality", _qual, 10) == ESP_OK)
  545. {
  546. #ifdef DEBUG_DETAIL_ON
  547. ESP_LOGD(TAG, "Quality: %s", _qual);
  548. #endif
  549. qual = atoi(_qual);
  550. if (qual > 63) // Limit to max. 63
  551. qual = 63;
  552. else if (qual < 8) // Limit to min. 8
  553. qual = 8;
  554. }
  555. }
  556. }
  557. framesize_t CCamera::TextToFramesize(const char * _size)
  558. {
  559. if (strcmp(_size, "QVGA") == 0)
  560. return FRAMESIZE_QVGA; // 320x240
  561. else if (strcmp(_size, "VGA") == 0)
  562. return FRAMESIZE_VGA; // 640x480
  563. else if (strcmp(_size, "SVGA") == 0)
  564. return FRAMESIZE_SVGA; // 800x600
  565. else if (strcmp(_size, "XGA") == 0)
  566. return FRAMESIZE_XGA; // 1024x768
  567. else if (strcmp(_size, "SXGA") == 0)
  568. return FRAMESIZE_SXGA; // 1280x1024
  569. else if (strcmp(_size, "UXGA") == 0)
  570. return FRAMESIZE_UXGA; // 1600x1200
  571. return ActualResolution;
  572. }
  573. CCamera::CCamera()
  574. {
  575. #ifdef DEBUG_DETAIL_ON
  576. ESP_LOGD(TAG, "CreateClassCamera");
  577. #endif
  578. brightness = 0;
  579. contrast = 0;
  580. saturation = 0;
  581. isFixedExposure = false;
  582. ledc_init();
  583. }
  584. esp_err_t CCamera::InitCam()
  585. {
  586. ESP_LOGD(TAG, "Init Camera");
  587. ActualQuality = camera_config.jpeg_quality;
  588. ActualResolution = camera_config.frame_size;
  589. //initialize the camera
  590. esp_camera_deinit(); // De-init in case it was already initialized
  591. esp_err_t err = esp_camera_init(&camera_config);
  592. if (err != ESP_OK) {
  593. ESP_LOGE(TAG, "Camera Init Failed");
  594. return err;
  595. }
  596. CameraInitSuccessful = true;
  597. return ESP_OK;
  598. }
  599. void CCamera::SetLEDIntensity(float _intrel)
  600. {
  601. _intrel = min(_intrel, (float) 100);
  602. _intrel = max(_intrel, (float) 0);
  603. _intrel = _intrel / 100;
  604. led_intensity = (int) (_intrel * 8191);
  605. ESP_LOGD(TAG, "Set led_intensity to %d of 8191", led_intensity);
  606. }
  607. bool CCamera::getCameraInitSuccessful()
  608. {
  609. return CameraInitSuccessful;
  610. }
  611. std::vector<std::string> demoFiles;
  612. void CCamera::useDemoMode()
  613. {
  614. char line[50];
  615. FILE *fd = fopen("/sdcard/demo/files.txt", "r");
  616. if (!fd) {
  617. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Can not start Demo mode, the folder '/sdcard/demo/' does not contain the needed files!");
  618. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "See Details on https://jomjol.github.io/AI-on-the-edge-device-docs/Demo-Mode!");
  619. return;
  620. }
  621. demoImage = (uint8_t*)malloc(DEMO_IMAGE_SIZE);
  622. if (demoImage == NULL) {
  623. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to acquire required memory for demo image!");
  624. return;
  625. }
  626. while (fgets(line, sizeof(line), fd) != NULL) {
  627. line[strlen(line) - 1] = '\0';
  628. demoFiles.push_back(line);
  629. }
  630. fclose(fd);
  631. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Using Demo mode (" + std::to_string(demoFiles.size()) +
  632. " files) instead of real camera image!");
  633. for (auto file : demoFiles) {
  634. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, file);
  635. }
  636. demoMode = true;
  637. }
  638. bool CCamera::loadNextDemoImage(camera_fb_t *fb) {
  639. char filename[50];
  640. int readBytes;
  641. long fileSize;
  642. snprintf(filename, sizeof(filename), "/sdcard/demo/%s", demoFiles[getCountFlowRounds() % demoFiles.size()].c_str());
  643. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Using " + std::string(filename) + " as demo image");
  644. /* Inject saved image */
  645. FILE * fp = fopen(filename, "rb");
  646. if (!fp) {
  647. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to read file: " + std::string(filename) +"!");
  648. return false;
  649. }
  650. fileSize = GetFileSize(filename);
  651. if (fileSize > DEMO_IMAGE_SIZE) {
  652. char buf[100];
  653. snprintf(buf, sizeof(buf), "Demo Image (%d bytes) is larger than provided buffer (%d bytes)!",
  654. (int)fileSize, DEMO_IMAGE_SIZE);
  655. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, std::string(buf));
  656. return false;
  657. }
  658. readBytes = fread(demoImage, 1, DEMO_IMAGE_SIZE, fp);
  659. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "read " + std::to_string(readBytes) + " bytes");
  660. fclose(fp);
  661. fb->buf = demoImage; // Update pointer
  662. fb->len = readBytes;
  663. // ToDo do we also need to set height, width, format and timestamp?
  664. return true;
  665. }
  666. long CCamera::GetFileSize(std::string filename)
  667. {
  668. struct stat stat_buf;
  669. long rc = stat(filename.c_str(), &stat_buf);
  670. return rc == 0 ? stat_buf.st_size : -1;
  671. }