esp_camera.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*
  15. * Example Use
  16. *
  17. static camera_config_t camera_example_config = {
  18. .pin_pwdn = PIN_PWDN,
  19. .pin_reset = PIN_RESET,
  20. .pin_xclk = PIN_XCLK,
  21. .pin_sccb_sda = PIN_SIOD,
  22. .pin_sccb_scl = PIN_SIOC,
  23. .pin_d7 = PIN_D7,
  24. .pin_d6 = PIN_D6,
  25. .pin_d5 = PIN_D5,
  26. .pin_d4 = PIN_D4,
  27. .pin_d3 = PIN_D3,
  28. .pin_d2 = PIN_D2,
  29. .pin_d1 = PIN_D1,
  30. .pin_d0 = PIN_D0,
  31. .pin_vsync = PIN_VSYNC,
  32. .pin_href = PIN_HREF,
  33. .pin_pclk = PIN_PCLK,
  34. .xclk_freq_hz = 20000000,
  35. .ledc_timer = LEDC_TIMER_0,
  36. .ledc_channel = LEDC_CHANNEL_0,
  37. .pixel_format = PIXFORMAT_JPEG,
  38. .frame_size = FRAMESIZE_SVGA,
  39. .jpeg_quality = 10,
  40. .fb_count = 2,
  41. .grab_mode = CAMERA_GRAB_WHEN_EMPTY
  42. };
  43. esp_err_t camera_example_init(){
  44. return esp_camera_init(&camera_example_config);
  45. }
  46. esp_err_t camera_example_capture(){
  47. //capture a frame
  48. camera_fb_t * fb = esp_camera_fb_get();
  49. if (!fb) {
  50. ESP_LOGE(TAG, "Frame buffer could not be acquired");
  51. return ESP_FAIL;
  52. }
  53. //replace this with your own function
  54. display_image(fb->width, fb->height, fb->pixformat, fb->buf, fb->len);
  55. //return the frame buffer back to be reused
  56. esp_camera_fb_return(fb);
  57. return ESP_OK;
  58. }
  59. */
  60. #pragma once
  61. #include "esp_err.h"
  62. #include "driver/ledc.h"
  63. #include "sensor.h"
  64. #include "sys/time.h"
  65. #include "sdkconfig.h"
  66. /**
  67. * @brief define for if chip supports camera
  68. */
  69. #define ESP_CAMERA_SUPPORTED (CONFIG_IDF_TARGET_ESP32 | CONFIG_IDF_TARGET_ESP32S3 | \
  70. CONFIG_IDF_TARGET_ESP32S2)
  71. #ifdef __cplusplus
  72. extern "C" {
  73. #endif
  74. /**
  75. * @brief Configuration structure for camera initialization
  76. */
  77. typedef enum {
  78. CAMERA_GRAB_WHEN_EMPTY, /*!< Fills buffers when they are empty. Less resources but first 'fb_count' frames might be old */
  79. CAMERA_GRAB_LATEST /*!< Except when 1 frame buffer is used, queue will always contain the last 'fb_count' frames */
  80. } camera_grab_mode_t;
  81. /**
  82. * @brief Camera frame buffer location
  83. */
  84. typedef enum {
  85. CAMERA_FB_IN_PSRAM, /*!< Frame buffer is placed in external PSRAM */
  86. CAMERA_FB_IN_DRAM /*!< Frame buffer is placed in internal DRAM */
  87. } camera_fb_location_t;
  88. #if CONFIG_CAMERA_CONVERTER_ENABLED
  89. /**
  90. * @brief Camera RGB\YUV conversion mode
  91. */
  92. typedef enum {
  93. CONV_DISABLE,
  94. RGB565_TO_YUV422,
  95. YUV422_TO_RGB565,
  96. YUV422_TO_YUV420
  97. } camera_conv_mode_t;
  98. #endif
  99. /**
  100. * @brief Configuration structure for camera initialization
  101. */
  102. typedef struct {
  103. int pin_pwdn; /*!< GPIO pin for camera power down line */
  104. int pin_reset; /*!< GPIO pin for camera reset line */
  105. int pin_xclk; /*!< GPIO pin for camera XCLK line */
  106. union {
  107. int pin_sccb_sda; /*!< GPIO pin for camera SDA line */
  108. int pin_sscb_sda __attribute__((deprecated("please use pin_sccb_sda instead"))); /*!< GPIO pin for camera SDA line (legacy name) */
  109. };
  110. union {
  111. int pin_sccb_scl; /*!< GPIO pin for camera SCL line */
  112. int pin_sscb_scl __attribute__((deprecated("please use pin_sccb_scl instead"))); /*!< GPIO pin for camera SCL line (legacy name) */
  113. };
  114. int pin_d7; /*!< GPIO pin for camera D7 line */
  115. int pin_d6; /*!< GPIO pin for camera D6 line */
  116. int pin_d5; /*!< GPIO pin for camera D5 line */
  117. int pin_d4; /*!< GPIO pin for camera D4 line */
  118. int pin_d3; /*!< GPIO pin for camera D3 line */
  119. int pin_d2; /*!< GPIO pin for camera D2 line */
  120. int pin_d1; /*!< GPIO pin for camera D1 line */
  121. int pin_d0; /*!< GPIO pin for camera D0 line */
  122. int pin_vsync; /*!< GPIO pin for camera VSYNC line */
  123. int pin_href; /*!< GPIO pin for camera HREF line */
  124. int pin_pclk; /*!< GPIO pin for camera PCLK line */
  125. int xclk_freq_hz; /*!< Frequency of XCLK signal, in Hz. EXPERIMENTAL: Set to 16MHz on ESP32-S2 or ESP32-S3 to enable EDMA mode */
  126. ledc_timer_t ledc_timer; /*!< LEDC timer to be used for generating XCLK */
  127. ledc_channel_t ledc_channel; /*!< LEDC channel to be used for generating XCLK */
  128. pixformat_t pixel_format; /*!< Format of the pixel data: PIXFORMAT_ + YUV422|GRAYSCALE|RGB565|JPEG */
  129. framesize_t frame_size; /*!< Size of the output image: FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA */
  130. int jpeg_quality; /*!< Quality of JPEG output. 0-63 lower means higher quality */
  131. size_t fb_count; /*!< Number of frame buffers to be allocated. If more than one, then each frame will be acquired (double speed) */
  132. camera_fb_location_t fb_location; /*!< The location where the frame buffer will be allocated */
  133. camera_grab_mode_t grab_mode; /*!< When buffers should be filled */
  134. #if CONFIG_CAMERA_CONVERTER_ENABLED
  135. camera_conv_mode_t conv_mode; /*!< RGB<->YUV Conversion mode */
  136. #endif
  137. int sccb_i2c_port; /*!< If pin_sccb_sda is -1, use the already configured I2C bus by number */
  138. } camera_config_t;
  139. /**
  140. * @brief Data structure of camera frame buffer
  141. */
  142. typedef struct {
  143. uint8_t * buf; /*!< Pointer to the pixel data */
  144. size_t len; /*!< Length of the buffer in bytes */
  145. size_t width; /*!< Width of the buffer in pixels */
  146. size_t height; /*!< Height of the buffer in pixels */
  147. pixformat_t format; /*!< Format of the pixel data */
  148. struct timeval timestamp; /*!< Timestamp since boot of the first DMA buffer of the frame */
  149. } camera_fb_t;
  150. #define ESP_ERR_CAMERA_BASE 0x20000
  151. #define ESP_ERR_CAMERA_NOT_DETECTED (ESP_ERR_CAMERA_BASE + 1)
  152. #define ESP_ERR_CAMERA_FAILED_TO_SET_FRAME_SIZE (ESP_ERR_CAMERA_BASE + 2)
  153. #define ESP_ERR_CAMERA_FAILED_TO_SET_OUT_FORMAT (ESP_ERR_CAMERA_BASE + 3)
  154. #define ESP_ERR_CAMERA_NOT_SUPPORTED (ESP_ERR_CAMERA_BASE + 4)
  155. /**
  156. * @brief Initialize the camera driver
  157. *
  158. * @note call camera_probe before calling this function
  159. *
  160. * This function detects and configures camera over I2C interface,
  161. * allocates framebuffer and DMA buffers,
  162. * initializes parallel I2S input, and sets up DMA descriptors.
  163. *
  164. * Currently this function can only be called once and there is
  165. * no way to de-initialize this module.
  166. *
  167. * @param config Camera configuration parameters
  168. *
  169. * @return ESP_OK on success
  170. */
  171. esp_err_t esp_camera_init(const camera_config_t* config);
  172. /**
  173. * @brief Deinitialize the camera driver
  174. *
  175. * @return
  176. * - ESP_OK on success
  177. * - ESP_ERR_INVALID_STATE if the driver hasn't been initialized yet
  178. */
  179. esp_err_t esp_camera_deinit(void);
  180. /**
  181. * @brief Obtain pointer to a frame buffer.
  182. *
  183. * @return pointer to the frame buffer
  184. */
  185. camera_fb_t* esp_camera_fb_get(void);
  186. /**
  187. * @brief Return the frame buffer to be reused again.
  188. *
  189. * @param fb Pointer to the frame buffer
  190. */
  191. void esp_camera_fb_return(camera_fb_t * fb);
  192. /**
  193. * @brief Get a pointer to the image sensor control structure
  194. *
  195. * @return pointer to the sensor
  196. */
  197. sensor_t * esp_camera_sensor_get(void);
  198. /**
  199. * @brief Save camera settings to non-volatile-storage (NVS)
  200. *
  201. * @param key A unique nvs key name for the camera settings
  202. */
  203. esp_err_t esp_camera_save_to_nvs(const char *key);
  204. /**
  205. * @brief Load camera settings from non-volatile-storage (NVS)
  206. *
  207. * @param key A unique nvs key name for the camera settings
  208. */
  209. esp_err_t esp_camera_load_from_nvs(const char *key);
  210. /**
  211. * @brief Return all frame buffers to be reused again.
  212. */
  213. void esp_camera_return_all(void);
  214. #ifdef __cplusplus
  215. }
  216. #endif
  217. #include "img_converters.h"