From dcf32b5dacf66b1346d12749d142b314a4a1cffa Mon Sep 17 00:00:00 2001 From: becem-gharbi Date: Fri, 27 Oct 2023 09:31:54 +0100 Subject: [PATCH] feat: Add led controller --- include/ei_cam.h | 7 +++++++ src/ei_cam.cpp | 26 ++++++++++++++++++++------ src/main.cpp | 2 ++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/include/ei_cam.h b/include/ei_cam.h index 3aa46e3..19e4fa6 100644 --- a/include/ei_cam.h +++ b/include/ei_cam.h @@ -27,6 +27,8 @@ #define EI_CAMERA_RAW_FRAME_BUFFER_ROWS 240 #define EI_CAMERA_FRAME_BYTE_SIZE 3 +#define LEDC_CHANNEL 15 + #define PART_BOUNDARY "123456789000000000000987654321" class EICam @@ -37,12 +39,17 @@ class EICam static ei_impulse_result_t predict(); void startStream(); void stopStream(); + /** + * @param intensity between 0 and 255 + */ + void controlLED(uint8_t intensity); private: static uint8_t *_snapshotBufferForInference; static bool _camInitialized; bool _initCam(void); void _deinitCam(void); + void _initLED(); static bool _captureCamForInference(uint32_t img_width, uint32_t img_height, uint8_t *out_buf); static int _getDataCamForInference(size_t offset, size_t length, float *out_ptr); static esp_err_t _streamHandler(httpd_req_t *req); diff --git a/src/ei_cam.cpp b/src/ei_cam.cpp index c1e615a..327aaf9 100644 --- a/src/ei_cam.cpp +++ b/src/ei_cam.cpp @@ -32,6 +32,8 @@ void EICam::begin(bool logEnabled) _log("Camera initialized\r\n"); } + _initLED(); + _log("\nStarting continuous Inference in 2 seconds...\n"); ei_sleep(2000); } @@ -131,10 +133,11 @@ bool EICam::_captureCamForInference(uint32_t img_width, uint32_t img_height, uin esp_camera_fb_return(fb); + _isCapturingForInference = false; + if (!converted) { _log("[capture] Conversion failed\n"); - _isCapturingForInference = false; return false; } @@ -154,7 +157,6 @@ bool EICam::_captureCamForInference(uint32_t img_width, uint32_t img_height, uin img_height); } - _isCapturingForInference = false; return true; } @@ -199,13 +201,11 @@ bool EICam::_initCam() // XCLK 20MHz or 10MHz for OV2640 double FPS (Experimental) .xclk_freq_hz = 20000000, - .ledc_timer = LEDC_TIMER_0, - .ledc_channel = LEDC_CHANNEL_0, .pixel_format = PIXFORMAT_JPEG, // YUV422,GRAYSCALE,RGB565,JPEG .frame_size = FRAMESIZE_QVGA, // QQVGA-UXGA Do not use sizes above QVGA when not JPEG - .jpeg_quality = 12, // 0-63 lower number means higher quality + .jpeg_quality = 10, // 0-63 lower number means higher quality .fb_count = 1, // if more than one, i2s runs in continuous mode. Use only with JPEG .grab_mode = CAMERA_GRAB_WHEN_EMPTY, }; @@ -253,7 +253,8 @@ void EICam::startStream() void EICam::stopStream() { - if(_streamHttpd) { + if (_streamHttpd) + { httpd_stop(_streamHttpd); } } @@ -342,5 +343,18 @@ esp_err_t EICam::_streamHandler(httpd_req_t *req) break; } } + return res; +} + +void EICam::controlLED(uint8_t intensity) +{ + ledcWrite(LEDC_CHANNEL, intensity); +} + +void EICam::_initLED() +{ + ledcSetup(LEDC_CHANNEL, 4000, 8); + ledcAttachPin(4, LEDC_CHANNEL); + controlLED(0); } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 4978d04..51fc0f3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,6 +20,8 @@ void setup() Serial.print("Camera Stream Ready! Go to: http://"); Serial.print(WiFi.localIP()); Serial.printf("\n"); + + eiCam.controlLED(0); } void loop()