mirror of
https://github.com/esphome/esphome.git
synced 2024-11-21 11:37:27 +01:00
Added on_image callback to ESP32 Cam (#4860)
This commit is contained in:
parent
b30430b0bd
commit
39d026299e
@ -25,6 +25,11 @@ AUTO_LOAD = ["psram"]
|
||||
|
||||
esp32_camera_ns = cg.esphome_ns.namespace("esp32_camera")
|
||||
ESP32Camera = esp32_camera_ns.class_("ESP32Camera", cg.PollingComponent, cg.EntityBase)
|
||||
ESP32CameraImageData = esp32_camera_ns.struct("CameraImageData")
|
||||
# Triggers
|
||||
ESP32CameraImageTrigger = esp32_camera_ns.class_(
|
||||
"ESP32CameraImageTrigger", automation.Trigger.template()
|
||||
)
|
||||
ESP32CameraStreamStartTrigger = esp32_camera_ns.class_(
|
||||
"ESP32CameraStreamStartTrigger",
|
||||
automation.Trigger.template(),
|
||||
@ -139,6 +144,7 @@ CONF_IDLE_FRAMERATE = "idle_framerate"
|
||||
# stream trigger
|
||||
CONF_ON_STREAM_START = "on_stream_start"
|
||||
CONF_ON_STREAM_STOP = "on_stream_stop"
|
||||
CONF_ON_IMAGE = "on_image"
|
||||
|
||||
camera_range_param = cv.int_range(min=-2, max=2)
|
||||
|
||||
@ -221,6 +227,11 @@ CONFIG_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(
|
||||
),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_ON_IMAGE): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ESP32CameraImageTrigger),
|
||||
}
|
||||
),
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
@ -289,3 +300,9 @@ async def to_code(config):
|
||||
for conf in config.get(CONF_ON_STREAM_STOP, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [], conf)
|
||||
|
||||
for conf in config.get(CONF_ON_IMAGE, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(
|
||||
trigger, [(ESP32CameraImageData, "image")], conf
|
||||
)
|
||||
|
@ -335,8 +335,8 @@ void ESP32Camera::set_idle_update_interval(uint32_t idle_update_interval) {
|
||||
}
|
||||
|
||||
/* ---------------- public API (specific) ---------------- */
|
||||
void ESP32Camera::add_image_callback(std::function<void(std::shared_ptr<CameraImage>)> &&f) {
|
||||
this->new_image_callback_.add(std::move(f));
|
||||
void ESP32Camera::add_image_callback(std::function<void(std::shared_ptr<CameraImage>)> &&callback) {
|
||||
this->new_image_callback_.add(std::move(callback));
|
||||
}
|
||||
void ESP32Camera::add_stream_start_callback(std::function<void()> &&callback) {
|
||||
this->stream_start_callback_.add(std::move(callback));
|
||||
|
@ -86,6 +86,11 @@ class CameraImage {
|
||||
uint8_t requesters_;
|
||||
};
|
||||
|
||||
struct CameraImageData {
|
||||
uint8_t *data;
|
||||
size_t length;
|
||||
};
|
||||
|
||||
/* ---------------- CameraImageReader class ---------------- */
|
||||
class CameraImageReader {
|
||||
public:
|
||||
@ -147,12 +152,12 @@ class ESP32Camera : public Component, public EntityBase {
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override;
|
||||
/* public API (specific) */
|
||||
void add_image_callback(std::function<void(std::shared_ptr<CameraImage>)> &&f);
|
||||
void start_stream(CameraRequester requester);
|
||||
void stop_stream(CameraRequester requester);
|
||||
void request_image(CameraRequester requester);
|
||||
void update_camera_parameters();
|
||||
|
||||
void add_image_callback(std::function<void(std::shared_ptr<CameraImage>)> &&callback);
|
||||
void add_stream_start_callback(std::function<void()> &&callback);
|
||||
void add_stream_stop_callback(std::function<void()> &&callback);
|
||||
|
||||
@ -196,7 +201,7 @@ class ESP32Camera : public Component, public EntityBase {
|
||||
uint8_t stream_requesters_{0};
|
||||
QueueHandle_t framebuffer_get_queue_;
|
||||
QueueHandle_t framebuffer_return_queue_;
|
||||
CallbackManager<void(std::shared_ptr<CameraImage>)> new_image_callback_;
|
||||
CallbackManager<void(std::shared_ptr<CameraImage>)> new_image_callback_{};
|
||||
CallbackManager<void()> stream_start_callback_{};
|
||||
CallbackManager<void()> stream_stop_callback_{};
|
||||
|
||||
@ -207,6 +212,18 @@ class ESP32Camera : public Component, public EntityBase {
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
extern ESP32Camera *global_esp32_camera;
|
||||
|
||||
class ESP32CameraImageTrigger : public Trigger<CameraImageData> {
|
||||
public:
|
||||
explicit ESP32CameraImageTrigger(ESP32Camera *parent) {
|
||||
parent->add_image_callback([this](const std::shared_ptr<esp32_camera::CameraImage> &image) {
|
||||
CameraImageData camera_image_data{};
|
||||
camera_image_data.length = image->get_data_length();
|
||||
camera_image_data.data = image->get_data_buffer();
|
||||
this->trigger(camera_image_data);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
class ESP32CameraStreamStartTrigger : public Trigger<> {
|
||||
public:
|
||||
explicit ESP32CameraStreamStartTrigger(ESP32Camera *parent) {
|
||||
|
@ -861,6 +861,10 @@ esp32_camera:
|
||||
number: GPIO1
|
||||
resolution: 640x480
|
||||
jpeg_quality: 10
|
||||
on_image:
|
||||
then:
|
||||
- lambda: |-
|
||||
ESP_LOGD("main", "image len=%d, data=%c", image.length, image.data[0]);
|
||||
|
||||
esp32_camera_web_server:
|
||||
- port: 8080
|
||||
|
Loading…
Reference in New Issue
Block a user