2019-04-17 12:06:00 +02:00
|
|
|
#pragma once
|
|
|
|
|
2021-09-20 11:47:51 +02:00
|
|
|
#ifdef USE_ESP32
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
#include "esphome/core/component.h"
|
2021-10-10 10:37:05 +02:00
|
|
|
#include "esphome/core/entity_base.h"
|
2019-04-17 12:06:00 +02:00
|
|
|
#include "esphome/core/helpers.h"
|
|
|
|
#include <esp_camera.h>
|
2021-09-20 11:47:51 +02:00
|
|
|
#include <freertos/FreeRTOS.h>
|
|
|
|
#include <freertos/queue.h>
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
namespace esphome {
|
|
|
|
namespace esp32_camera {
|
|
|
|
|
|
|
|
class ESP32Camera;
|
|
|
|
|
2022-01-24 23:53:47 +01:00
|
|
|
/* ---------------- enum classes ---------------- */
|
2022-01-09 23:58:49 +01:00
|
|
|
enum CameraRequester { IDLE, API_REQUESTER, WEB_REQUESTER };
|
|
|
|
|
2022-01-24 23:53:47 +01:00
|
|
|
enum ESP32CameraFrameSize {
|
|
|
|
ESP32_CAMERA_SIZE_160X120, // QQVGA
|
|
|
|
ESP32_CAMERA_SIZE_176X144, // QCIF
|
|
|
|
ESP32_CAMERA_SIZE_240X176, // HQVGA
|
|
|
|
ESP32_CAMERA_SIZE_320X240, // QVGA
|
|
|
|
ESP32_CAMERA_SIZE_400X296, // CIF
|
|
|
|
ESP32_CAMERA_SIZE_640X480, // VGA
|
|
|
|
ESP32_CAMERA_SIZE_800X600, // SVGA
|
|
|
|
ESP32_CAMERA_SIZE_1024X768, // XGA
|
|
|
|
ESP32_CAMERA_SIZE_1280X1024, // SXGA
|
|
|
|
ESP32_CAMERA_SIZE_1600X1200, // UXGA
|
|
|
|
};
|
|
|
|
|
|
|
|
enum ESP32AgcGainCeiling {
|
|
|
|
ESP32_GAINCEILING_2X = GAINCEILING_2X,
|
|
|
|
ESP32_GAINCEILING_4X = GAINCEILING_4X,
|
|
|
|
ESP32_GAINCEILING_8X = GAINCEILING_8X,
|
|
|
|
ESP32_GAINCEILING_16X = GAINCEILING_16X,
|
|
|
|
ESP32_GAINCEILING_32X = GAINCEILING_32X,
|
|
|
|
ESP32_GAINCEILING_64X = GAINCEILING_64X,
|
|
|
|
ESP32_GAINCEILING_128X = GAINCEILING_128X,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum ESP32GainControlMode {
|
|
|
|
ESP32_GC_MODE_MANU = false,
|
|
|
|
ESP32_GC_MODE_AUTO = true,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum ESP32WhiteBalanceMode {
|
|
|
|
ESP32_WB_MODE_AUTO = 0U,
|
|
|
|
ESP32_WB_MODE_SUNNY = 1U,
|
|
|
|
ESP32_WB_MODE_CLOUDY = 2U,
|
|
|
|
ESP32_WB_MODE_OFFICE = 3U,
|
|
|
|
ESP32_WB_MODE_HOME = 4U,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum ESP32SpecialEffect {
|
|
|
|
ESP32_SPECIAL_EFFECT_NONE = 0U,
|
|
|
|
ESP32_SPECIAL_EFFECT_NEGATIVE = 1U,
|
|
|
|
ESP32_SPECIAL_EFFECT_GRAYSCALE = 2U,
|
|
|
|
ESP32_SPECIAL_EFFECT_RED_TINT = 3U,
|
|
|
|
ESP32_SPECIAL_EFFECT_GREEN_TINT = 4U,
|
|
|
|
ESP32_SPECIAL_EFFECT_BLUE_TINT = 5U,
|
|
|
|
ESP32_SPECIAL_EFFECT_SEPIA = 6U,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* ---------------- CameraImage class ---------------- */
|
2019-04-17 12:06:00 +02:00
|
|
|
class CameraImage {
|
|
|
|
public:
|
2022-01-09 23:58:49 +01:00
|
|
|
CameraImage(camera_fb_t *buffer, uint8_t requester);
|
2019-04-17 12:06:00 +02:00
|
|
|
camera_fb_t *get_raw_buffer();
|
|
|
|
uint8_t *get_data_buffer();
|
|
|
|
size_t get_data_length();
|
2022-01-09 23:58:49 +01:00
|
|
|
bool was_requested_by(CameraRequester requester) const;
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
camera_fb_t *buffer_;
|
2022-01-09 23:58:49 +01:00
|
|
|
uint8_t requesters_;
|
2019-04-17 12:06:00 +02:00
|
|
|
};
|
|
|
|
|
2022-01-24 23:53:47 +01:00
|
|
|
/* ---------------- CameraImageReader class ---------------- */
|
2019-04-17 12:06:00 +02:00
|
|
|
class CameraImageReader {
|
|
|
|
public:
|
|
|
|
void set_image(std::shared_ptr<CameraImage> image);
|
|
|
|
size_t available() const;
|
|
|
|
uint8_t *peek_data_buffer();
|
|
|
|
void consume_data(size_t consumed);
|
|
|
|
void return_image();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::shared_ptr<CameraImage> image_;
|
|
|
|
size_t offset_{0};
|
|
|
|
};
|
|
|
|
|
2022-01-24 23:53:47 +01:00
|
|
|
/* ---------------- ESP32Camera class ---------------- */
|
2021-10-10 10:37:05 +02:00
|
|
|
class ESP32Camera : public Component, public EntityBase {
|
2019-04-17 12:06:00 +02:00
|
|
|
public:
|
|
|
|
ESP32Camera(const std::string &name);
|
2021-10-22 12:10:29 +02:00
|
|
|
ESP32Camera();
|
2022-01-24 23:53:47 +01:00
|
|
|
|
|
|
|
/* setters */
|
|
|
|
/* -- pin assignment */
|
2019-04-17 12:06:00 +02:00
|
|
|
void set_data_pins(std::array<uint8_t, 8> pins);
|
|
|
|
void set_vsync_pin(uint8_t pin);
|
|
|
|
void set_href_pin(uint8_t pin);
|
|
|
|
void set_pixel_clock_pin(uint8_t pin);
|
|
|
|
void set_external_clock(uint8_t pin, uint32_t frequency);
|
|
|
|
void set_i2c_pins(uint8_t sda, uint8_t scl);
|
|
|
|
void set_reset_pin(uint8_t pin);
|
|
|
|
void set_power_down_pin(uint8_t pin);
|
2022-01-24 23:53:47 +01:00
|
|
|
/* -- image */
|
|
|
|
void set_frame_size(ESP32CameraFrameSize size);
|
|
|
|
void set_jpeg_quality(uint8_t quality);
|
2019-04-17 12:06:00 +02:00
|
|
|
void set_vertical_flip(bool vertical_flip);
|
|
|
|
void set_horizontal_mirror(bool horizontal_mirror);
|
|
|
|
void set_contrast(int contrast);
|
|
|
|
void set_brightness(int brightness);
|
|
|
|
void set_saturation(int saturation);
|
2022-01-24 23:53:47 +01:00
|
|
|
void set_special_effect(ESP32SpecialEffect effect);
|
|
|
|
/* -- exposure */
|
|
|
|
void set_aec_mode(ESP32GainControlMode mode);
|
|
|
|
void set_aec2(bool aec2);
|
|
|
|
void set_ae_level(int ae_level);
|
|
|
|
void set_aec_value(uint32_t aec_value);
|
|
|
|
/* -- gains */
|
|
|
|
void set_agc_mode(ESP32GainControlMode mode);
|
|
|
|
void set_agc_value(uint8_t agc_value);
|
|
|
|
void set_agc_gain_ceiling(ESP32AgcGainCeiling gain_ceiling);
|
|
|
|
/* -- white balance */
|
|
|
|
void set_wb_mode(ESP32WhiteBalanceMode mode);
|
|
|
|
/* -- test */
|
|
|
|
void set_test_pattern(bool test_pattern);
|
|
|
|
/* -- framerates */
|
2019-04-17 12:06:00 +02:00
|
|
|
void set_max_update_interval(uint32_t max_update_interval);
|
|
|
|
void set_idle_update_interval(uint32_t idle_update_interval);
|
2022-01-24 23:53:47 +01:00
|
|
|
|
|
|
|
/* public API (derivated) */
|
2019-04-17 12:06:00 +02:00
|
|
|
void setup() override;
|
|
|
|
void loop() override;
|
|
|
|
void dump_config() override;
|
|
|
|
float get_setup_priority() const override;
|
2022-01-24 23:53:47 +01:00
|
|
|
/* public API (specific) */
|
|
|
|
void add_image_callback(std::function<void(std::shared_ptr<CameraImage>)> &&f);
|
2022-01-09 23:58:49 +01:00
|
|
|
void start_stream(CameraRequester requester);
|
|
|
|
void stop_stream(CameraRequester requester);
|
|
|
|
void request_image(CameraRequester requester);
|
2022-01-24 23:53:47 +01:00
|
|
|
void update_camera_parameters();
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
protected:
|
2022-01-24 23:53:47 +01:00
|
|
|
/* internal methods */
|
2019-04-17 12:06:00 +02:00
|
|
|
uint32_t hash_base() override;
|
|
|
|
bool has_requested_image_() const;
|
|
|
|
bool can_return_image_() const;
|
|
|
|
|
|
|
|
static void framebuffer_task(void *pv);
|
|
|
|
|
2022-01-24 23:53:47 +01:00
|
|
|
/* attributes */
|
|
|
|
/* camera configuration */
|
2019-04-17 12:06:00 +02:00
|
|
|
camera_config_t config_{};
|
2022-01-24 23:53:47 +01:00
|
|
|
/* -- image */
|
2019-04-17 12:06:00 +02:00
|
|
|
bool vertical_flip_{true};
|
|
|
|
bool horizontal_mirror_{true};
|
|
|
|
int contrast_{0};
|
|
|
|
int brightness_{0};
|
|
|
|
int saturation_{0};
|
2022-01-24 23:53:47 +01:00
|
|
|
ESP32SpecialEffect special_effect_{ESP32_SPECIAL_EFFECT_NONE};
|
|
|
|
/* -- exposure */
|
|
|
|
ESP32GainControlMode aec_mode_{ESP32_GC_MODE_AUTO};
|
|
|
|
bool aec2_{false};
|
|
|
|
int ae_level_{0};
|
|
|
|
uint32_t aec_value_{300};
|
|
|
|
/* -- gains */
|
|
|
|
ESP32GainControlMode agc_mode_{ESP32_GC_MODE_AUTO};
|
|
|
|
uint8_t agc_value_{0};
|
|
|
|
ESP32AgcGainCeiling agc_gain_ceiling_{ESP32_GAINCEILING_2X};
|
|
|
|
/* -- white balance */
|
|
|
|
ESP32WhiteBalanceMode wb_mode_{ESP32_WB_MODE_AUTO};
|
|
|
|
/* -- Test */
|
2019-04-17 12:06:00 +02:00
|
|
|
bool test_pattern_{false};
|
2022-01-24 23:53:47 +01:00
|
|
|
/* -- framerates */
|
|
|
|
uint32_t max_update_interval_{1000};
|
|
|
|
uint32_t idle_update_interval_{15000};
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
esp_err_t init_error_{ESP_OK};
|
|
|
|
std::shared_ptr<CameraImage> current_image_;
|
2022-01-09 23:58:49 +01:00
|
|
|
uint8_t single_requesters_{0};
|
|
|
|
uint8_t stream_requesters_{0};
|
2019-04-17 12:06:00 +02:00
|
|
|
QueueHandle_t framebuffer_get_queue_;
|
|
|
|
QueueHandle_t framebuffer_return_queue_;
|
|
|
|
CallbackManager<void(std::shared_ptr<CameraImage>)> new_image_callback_;
|
2022-01-24 23:53:47 +01:00
|
|
|
|
2022-01-09 23:58:49 +01:00
|
|
|
uint32_t last_idle_request_{0};
|
2019-04-17 12:06:00 +02:00
|
|
|
uint32_t last_update_{0};
|
|
|
|
};
|
|
|
|
|
2021-09-24 18:02:28 +02:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
2019-04-17 12:06:00 +02:00
|
|
|
extern ESP32Camera *global_esp32_camera;
|
|
|
|
|
|
|
|
} // namespace esp32_camera
|
|
|
|
} // namespace esphome
|
|
|
|
|
|
|
|
#endif
|