Skip to content

Commit

Permalink
feat: Add led controller
Browse files Browse the repository at this point in the history
  • Loading branch information
becem-gharbi committed Oct 27, 2023
1 parent 8384f6a commit dcf32b5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
7 changes: 7 additions & 0 deletions include/ei_cam.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
26 changes: 20 additions & 6 deletions src/ei_cam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -154,7 +157,6 @@ bool EICam::_captureCamForInference(uint32_t img_width, uint32_t img_height, uin
img_height);
}

_isCapturingForInference = false;
return true;
}

Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -253,7 +253,8 @@ void EICam::startStream()

void EICam::stopStream()
{
if(_streamHttpd) {
if (_streamHttpd)
{
httpd_stop(_streamHttpd);
}
}
Expand Down Expand Up @@ -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);
}
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit dcf32b5

Please sign in to comment.