|
| 1 | + |
| 2 | +#include <esp_event_loop.h> |
| 3 | +#include <esp_log.h> |
| 4 | +#include <esp_system.h> |
| 5 | +#include <nvs_flash.h> |
| 6 | +#include <sys/param.h> |
| 7 | + |
| 8 | +#include "freertos/FreeRTOS.h" |
| 9 | +#include "freertos/task.h" |
| 10 | + |
| 11 | +#include "driver/sdmmc_host.h" |
| 12 | +#include "driver/sdmmc_defs.h" |
| 13 | +#include "sdmmc_cmd.h" |
| 14 | +#include "esp_vfs_fat.h" |
| 15 | + |
| 16 | +#include "esp_camera.h" |
| 17 | + |
| 18 | +static const char *TAG = "example:sd_jpg"; |
| 19 | + |
| 20 | +static camera_config_t camera_config = { |
| 21 | + .pin_pwdn = -1, |
| 22 | + .pin_reset = CONFIG_RESET, |
| 23 | + .pin_xclk = CONFIG_XCLK, |
| 24 | + .pin_sscb_sda = CONFIG_SDA, |
| 25 | + .pin_sscb_scl = CONFIG_SCL, |
| 26 | + |
| 27 | + .pin_d7 = CONFIG_D7, |
| 28 | + .pin_d6 = CONFIG_D6, |
| 29 | + .pin_d5 = CONFIG_D5, |
| 30 | + .pin_d4 = CONFIG_D4, |
| 31 | + .pin_d3 = CONFIG_D3, |
| 32 | + .pin_d2 = CONFIG_D2, |
| 33 | + .pin_d1 = CONFIG_D1, |
| 34 | + .pin_d0 = CONFIG_D0, |
| 35 | + .pin_vsync = CONFIG_VSYNC, |
| 36 | + .pin_href = CONFIG_HREF, |
| 37 | + .pin_pclk = CONFIG_PCLK, |
| 38 | + |
| 39 | + //XCLK 20MHz or 10MHz |
| 40 | + .xclk_freq_hz = CONFIG_XCLK_FREQ, |
| 41 | + .ledc_timer = LEDC_TIMER_0, |
| 42 | + .ledc_channel = LEDC_CHANNEL_0, |
| 43 | + |
| 44 | + .pixel_format = PIXFORMAT_JPEG, //YUV422,GRAYSCALE,RGB565,JPEG |
| 45 | + .frame_size = FRAMESIZE_UXGA, //QQVGA-UXGA Do not use sizes above QVGA when not JPEG |
| 46 | + |
| 47 | + .jpeg_quality = 12, //0-63 lower number means higher quality |
| 48 | + .fb_count = 1 //if more than one, i2s runs in continuous mode. Use only with JPEG |
| 49 | +}; |
| 50 | + |
| 51 | +static esp_err_t init_camera() |
| 52 | +{ |
| 53 | + //initialize the camera |
| 54 | + esp_err_t err = esp_camera_init(&camera_config); |
| 55 | + if (err != ESP_OK) |
| 56 | + { |
| 57 | + ESP_LOGE(TAG, "Camera Init Failed"); |
| 58 | + return err; |
| 59 | + } |
| 60 | + |
| 61 | + return ESP_OK; |
| 62 | +} |
| 63 | + |
| 64 | +static void init_sdcard() |
| 65 | +{ |
| 66 | + esp_err_t ret = ESP_FAIL; |
| 67 | + sdmmc_host_t host = SDMMC_HOST_DEFAULT(); |
| 68 | + sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT(); |
| 69 | + esp_vfs_fat_sdmmc_mount_config_t mount_config = { |
| 70 | + .format_if_mount_failed = false, |
| 71 | + .max_files = 3, |
| 72 | + }; |
| 73 | + sdmmc_card_t *card; |
| 74 | + |
| 75 | + ESP_LOGI(TAG, "Mounting SD card..."); |
| 76 | + ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card); |
| 77 | + |
| 78 | + if (ret == ESP_OK) |
| 79 | + { |
| 80 | + ESP_LOGI(TAG, "SD card mount successfully!"); |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + ESP_LOGE(TAG, "Failed to mount SD card VFAT filesystem. Error: %s", esp_err_to_name(ret)); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void app_main() |
| 89 | +{ |
| 90 | + init_sdcard(); |
| 91 | + init_camera(); |
| 92 | + |
| 93 | + while (1) |
| 94 | + { |
| 95 | + ESP_LOGI(TAG, "Taking picture..."); |
| 96 | + camera_fb_t *pic = esp_camera_fb_get(); |
| 97 | + int64_t timestamp = esp_timer_get_time(); |
| 98 | + |
| 99 | + char *pic_name = malloc(17 + sizeof(int64_t)); |
| 100 | + sprintf(pic_name, "/sdcard/pic_%lli.jpg", timestamp); |
| 101 | + FILE *file = fopen(pic_name, "w"); |
| 102 | + if (file != NULL) |
| 103 | + { |
| 104 | + size_t err = fwrite(pic->buf, 1, pic->len, file); |
| 105 | + ESP_LOGI(TAG, "File saved: %s", pic_name); |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + ESP_LOGE(TAG, "Could not open file =("); |
| 110 | + } |
| 111 | + fclose(file); |
| 112 | + free(pic_name); |
| 113 | + |
| 114 | + vTaskDelay(20000 / portTICK_RATE_MS); |
| 115 | + } |
| 116 | +} |
0 commit comments