Skip to content

Commit

Permalink
refactor: configurable resolution for camera
Browse files Browse the repository at this point in the history
* refactor: support sensor options

* refactor: remove staled camera implementation for we2

* refactor: reimplement camera api for we2

* refactor: we2 camera driver memory layout

* feat: shared base64 buffer from little core's sram

* refactor: opts for esp camera

* refactor: opts for we1 camera

* refactor: reduce el heap size for we2, cleanup

* feat: shared base64 buffer, sensor info includes opts

* refactor: sensor api

* refactor: update sensor api

* docs: add sensor opts api docs
  • Loading branch information
iChizer0 committed Feb 28, 2024
1 parent b557938 commit 3541ac8
Show file tree
Hide file tree
Showing 27 changed files with 451 additions and 429 deletions.
5 changes: 5 additions & 0 deletions core/el_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ typedef struct EL_ATTR_PACKED el_sensor_info_t {
el_sensor_state_t state;
} el_sensor_info_t;

typedef struct el_sensor_opt_t {
uint8_t id;
const char* details;
} el_sensor_opt_t;

typedef enum {
EL_MODEL_FMT_UNDEFINED = 0u,
EL_MODEL_FMT_PACKED_TFLITE = 1u,
Expand Down
21 changes: 16 additions & 5 deletions docs/protocol/at_protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,14 @@ Response:
{
"id": 1,
"type": 1,
"state": 1
"state": 1,
"opt_id": 0,
"opt_detail": "240x240 Auto",
"opts": {
"2": "640x480 Auto",
"1": "480x480 Auto",
"0": "240x240 Auto"
}
}
]
}\n
Expand All @@ -327,7 +334,9 @@ Response:
"data": {
"id": 1,
"type": 1,
"state": 1
"state": 1,
"opt_id": 0,
"opt_detail": "240x240 Auto"
}
}\n
```
Expand Down Expand Up @@ -572,9 +581,9 @@ Note: `"model": {..., "type": <AlgorithmType:Unsigned>, ...}`.

#### Set a default sensor by sensor ID

Pattern: `AT+SENSOR=<SENSOR_ID,ENABLE/DISABLE>\r`
Pattern: `AT+SENSOR=<SENSOR_ID,ENABLE/DISABLE,OPT_ID>\r`

Request: `AT+SENSOR=1,1\r`
Request: `AT+SENSOR=1,1,0\r`

Response:

Expand All @@ -587,7 +596,9 @@ Response:
"sensor": {
"id": 1,
"type": 1,
"state": 1
"state": 1,
"opt_id": 0,
"opt_detail": "240x240 Auto"
}
}
}\n
Expand Down
38 changes: 35 additions & 3 deletions porting/el_camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,38 @@

#include <cstddef>
#include <cstdint>
#include <forward_list>

#include "core/el_types.h"

namespace edgelab {

class Camera {
public:
Camera() : _is_present(false), _is_streaming(false) {}
using SensorOptIdType = decltype(el_sensor_opt_t::id);

Camera(uint32_t supported_opts_mask = 0) : _is_present(false), _is_streaming(false) {
std::forward_list<el_sensor_opt_t> presets = {
el_sensor_opt_t{.id = 0, .details = "240x240 Auto"},
el_sensor_opt_t{.id = 1, .details = "480x480 Auto"},
el_sensor_opt_t{.id = 2, .details = "640x480 Auto"},
el_sensor_opt_t{.id = 3, .details = "1280x720 Auto"},
el_sensor_opt_t{.id = 4, .details = "1920x1080 Auto"}
};

for (auto& opt : presets) {
if (supported_opts_mask & (1 << opt.id)) _supported_opts.push_front(opt);
}

if (!_supported_opts.empty()) {
_current_opt_id = _supported_opts.front().id;
}
}

virtual ~Camera() = default;

virtual el_err_code_t init(size_t width, size_t height) = 0;
virtual el_err_code_t deinit() = 0;
virtual el_err_code_t init(SensorOptIdType opt_id) = 0;
virtual el_err_code_t deinit() = 0;

virtual el_err_code_t start_stream() = 0;
virtual el_err_code_t stop_stream() = 0;
Expand All @@ -51,9 +71,21 @@ class Camera {

bool is_streaming() const { return _is_streaming; }

SensorOptIdType current_opt_id() const { return _current_opt_id; }
const char* current_opt_detail() const {
for (auto& opt : _supported_opts) {
if (opt.id == _current_opt_id) return opt.details;
}
return "Unknown";
}
const std::forward_list<el_sensor_opt_t>& supported_opts() const { return _supported_opts; }

protected:
volatile bool _is_present;
volatile bool _is_streaming;

SensorOptIdType _current_opt_id;
std::forward_list<el_sensor_opt_t> _supported_opts;
};

} // namespace edgelab
Expand Down
8 changes: 7 additions & 1 deletion porting/espressif/el_camera_esp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@

namespace edgelab {

el_err_code_t CameraEsp::init(size_t width, size_t height) {
CameraEsp::CameraEsp() : Camera(0b00000001), config(), fb(nullptr), sensor(nullptr) {}

el_err_code_t CameraEsp::init(SensorOptIdType opt_id) {
if ((1 << opt_id) != 0b00000001) {
return EL_EINVAL;
}

config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = CAMERA_PIN_D0;
Expand Down
4 changes: 2 additions & 2 deletions porting/espressif/el_camera_esp.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ namespace edgelab {

class CameraEsp final : public Camera {
public:
CameraEsp() = default;
CameraEsp();
~CameraEsp() = default;

el_err_code_t init(size_t width, size_t height) override;
el_err_code_t init(SensorOptIdType opt_id) override;
el_err_code_t deinit() override;

el_err_code_t start_stream() override;
Expand Down
10 changes: 8 additions & 2 deletions porting/himax/we1/el_camera_we1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ extern "C" {

namespace edgelab {

el_err_code_t CameraWE1::init(size_t width, size_t height) {
ERROR_T ret = ERROR_NONE;
CameraWE1::CameraWE1() : Camera(0b00000001) {}

el_err_code_t CameraWE1::init(SensorOptIdType opt_id) {
ERROR_T ret = ERROR_NONE;

uint16_t width = 240;
uint16_t height = 240;

this->config.sensor_type = SENSOR_CAMERA;
this->config.data.camera_cfg.width = width;
this->config.data.camera_cfg.height = height;
Expand Down
4 changes: 2 additions & 2 deletions porting/himax/we1/el_camera_we1.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ namespace edgelab {

class CameraWE1 final : public Camera {
public:
CameraWE1() = default;
CameraWE1();
~CameraWE1() = default;

el_err_code_t init(size_t width, size_t height) override;
el_err_code_t init(SensorOptIdType opt_id) override;
el_err_code_t deinit() override;

el_err_code_t start_stream() override;
Expand Down
27 changes: 27 additions & 0 deletions porting/himax/we2/drivers/drv_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef _DRV_COMMON_H_
#define _DRV_COMMON_H_

#include <core/el_debug.h>
#include <core/el_types.h>
#include <inttypes.h>

el_res_t _drv_fit_res(uint16_t width, uint16_t height) {
el_res_t res;

if (width > 320 || height > 240) {
res.width = 640;
res.height = 480;
} else if (width > 160 || height > 120) {
res.width = 320;
res.height = 240;
} else {
res.width = 160;
res.height = 120;
}

EL_LOGD("fit width: %d height: %d", res.width, res.height);

return res;
}

#endif

0 comments on commit 3541ac8

Please sign in to comment.