ClassControllCamera.cpp 25 KB

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