camera.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "camera.h"
  2. static void ov_camera_pw_gpio_init(void);
  3. static void ov_camera_pw_on(void);
  4. static void ov_camera_pw_off(void);
  5. static const char *TAG = "camera";
  6. camera_fb_t *pic;
  7. static camera_config_t camera_config = {
  8. .pin_pwdn = -1,
  9. .pin_reset = CAM_RST_PIN,
  10. .pin_xclk = CAM_XCLK_PIN,
  11. .pin_sscb_sda = CAM_SDA_PIN,
  12. .pin_sscb_scl = CAM_SCL_PIN,
  13. .pin_d7 = CAM_D7_PIN,
  14. .pin_d6 = CAM_D6_PIN,
  15. .pin_d5 = CAM_D5_PIN,
  16. .pin_d4 = CAM_D4_PIN,
  17. .pin_d3 = CAM_D3_PIN,
  18. .pin_d2 = CAM_D2_PIN,
  19. .pin_d1 = CAM_D1_PIN,
  20. .pin_d0 = CAM_D0_PIN,
  21. .pin_vsync = CAM_VSYNC_PIN,
  22. .pin_href = CAM_HREF_PIN,
  23. .pin_pclk = CAM_PCLK_PIN,
  24. //XCLK 20MHz or 10MHz for OV2640 double FPS (Experimental)
  25. .xclk_freq_hz = CONFIG_CAM_XCLK_FREQ,
  26. .pixel_format = PIXFORMAT_JPEG, //YUV422,GRAYSCALE,RGB565,JPEG
  27. .frame_size = FRAMESIZE_SVGA, //QQVGA-UXGA Do not use sizes above QVGA when not JPEG
  28. .jpeg_quality = JPG_QUALITY, //0-63 lower number means higher quality
  29. .fb_count = 1, //if more than one, i2s runs in continuous mode. Use only with JPEG
  30. .grab_mode = CAMERA_GRAB_WHEN_EMPTY,
  31. };
  32. static void camera_task(void)
  33. {
  34. while (1)
  35. {
  36. ESP_LOGI(TAG, "Taking picture...");
  37. camera_fb_t *pic = esp_camera_fb_get();
  38. // use pic->buf to access the image
  39. ESP_LOGI(TAG, "Picture taken! Its size was: %zu bytes", pic->len);
  40. esp_camera_fb_return(pic);
  41. vTaskDelay(5000 / portTICK_RATE_MS);
  42. }
  43. }
  44. esp_err_t camera_run(void)
  45. {
  46. ESP_LOGD(TAG, "Taking picture...");
  47. pic = esp_camera_fb_get();
  48. if(pic == NULL)
  49. {
  50. return ESP_FAIL;
  51. }
  52. // use pic->buf to access the image
  53. ESP_LOGD(TAG, "Picture taken! Its size was: %zu bytes", pic->len);
  54. return ESP_OK;
  55. }
  56. void camera_stop(void)
  57. {
  58. esp_camera_fb_return(pic);
  59. }
  60. uint8_t * camera_get_fb(void)
  61. {
  62. return pic->buf;
  63. }
  64. size_t camera_get_data_size(void)
  65. {
  66. return pic->len;
  67. }
  68. esp_err_t init_camera(void)
  69. {
  70. ov_camera_pw_gpio_init();
  71. ov_camera_pw_on();
  72. //initialize the camera
  73. esp_err_t err = esp_camera_init(&camera_config);
  74. if (err != ESP_OK)
  75. {
  76. ESP_LOGE(TAG, "Camera Init Failed");
  77. return err;
  78. }
  79. return ESP_OK;
  80. }
  81. static void ov_camera_pw_gpio_init(void)
  82. {
  83. #ifdef CONFIG_OV2640_SUPPORT
  84. uint64_t ov2640_output_gpio = ((1ULL<<CONFIG_CAM_PW_GPIO));
  85. //zero-initialize the config structure.
  86. gpio_config_t io_conf;
  87. //disable interrupt
  88. io_conf.intr_type = GPIO_INTR_DISABLE;
  89. //set as output mode
  90. io_conf.mode = GPIO_MODE_OUTPUT;
  91. //bit mask of the pins that you want to set
  92. io_conf.pin_bit_mask = ov2640_output_gpio;
  93. //disable pull-down mode
  94. io_conf.pull_down_en = 0;
  95. //disable pull-up mode
  96. io_conf.pull_up_en = 0;
  97. //configure GPIO with the given settings
  98. gpio_config(&io_conf);
  99. #endif
  100. }
  101. static void ov_camera_pw_on(void)
  102. {
  103. #ifdef CONFIG_OV2640_SUPPORT
  104. gpio_set_level(CONFIG_CAM_PW_GPIO, CONFIG_CAM_PW_ON_LEVEL?1:0);
  105. #endif
  106. vTaskDelay(100 / portTICK_PERIOD_MS);
  107. }
  108. static void ov_camera_pw_off(void)
  109. {
  110. #ifdef CONFIG_OV2640_SUPPORT
  111. gpio_set_level(CONFIG_CAM_PW_GPIO, CONFIG_CAM_PW_ON_LEVEL?0:1);
  112. #endif
  113. }