diff --git a/main/main.cpp b/main/main.cpp index 3e696a0..e7c7468 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -16,9 +16,12 @@ #include "spi_master_lobo.h" #include "img_hacking.c" #include "EPD.h" +#include "storage.h" #define APP_VERSION "0.1" +static const char *TAG = "main"; + static struct tm* tm_info; static char tmp_buff[128]; static time_t time_now, time_last = 0; @@ -88,10 +91,15 @@ void fs_init() vfs_spiffs_register(); if (spiffs_is_mounted) { ESP_LOGI(tag, "File system mounted."); - } - else { + } else { ESP_LOGE(tag, "Error mounting file system."); } + + if (storage_init() == 0) { + ESP_LOGI(tag, "SD card mounted."); + } else { + ESP_LOGE(tag, "Error mounting SD card."); + } } extern "C" void app_main() @@ -103,33 +111,49 @@ extern "C" void app_main() printf("\n LilyBook v%s\n\n", APP_VERSION); + fs_init(); + display_connect(); EPD_DisplayClearFull(); - - fs_init(); printf("==== START ====\r\n\n"); + FILE* f = fopen("/sdcard/book.txt", "r"); + _gs = 1; uint32_t tstart; int pass = 0; - int f = DEFAULT_FONT; - while (1) { - EPD_DisplayClearPart(); + int font = DEFAULT_FONT; + while (1) { EPD_fillScreen(_bg); _fg = 15; _bg = 0; - EPD_setFont(f++ % USER_FONT, NULL); - EPD_print("Welcome to LilyBook", 10, 10); + EPD_setFont(font++ % USER_FONT, NULL); + + char text[128]; + + if (f == NULL) { + ESP_LOGE(TAG, "Failed to open file for reading"); + sprintf(text, "Could not open SD card."); + f = fopen("/sdcard/book.txt", "r"); + } else { + if (fgets(text, sizeof(text), f) == NULL) { + ESP_LOGI(TAG, "End of file, closing."); + fclose(f); + f = NULL; + } else { + ESP_LOGI(TAG, "Read content: %s", text); + } + } + + text_wrap = 1; + EPD_print(text, 10, 10); EPD_UpdateScreen(); EPD_wait(5000); - - EPD_PowerOff(); - EPD_wait(8000); } } diff --git a/main/storage.cpp b/main/storage.cpp new file mode 100644 index 0000000..3c37487 --- /dev/null +++ b/main/storage.cpp @@ -0,0 +1,102 @@ + +#include +#include +#include +#include +#include "esp_err.h" +#include "esp_log.h" +#include "esp_vfs_fat.h" +#include "driver/sdmmc_host.h" +#include "driver/sdspi_host.h" +#include "sdmmc_cmd.h" +#include "storage.h" + +static const char *TAG = "storage"; + +// This example can use SDMMC and SPI peripherals to communicate with SD card. +// By default, SDMMC peripheral is used. +// To enable SPI mode, uncomment the following line: + +//#define USE_SPI_MODE + +// When testing SD and SPI modes, keep in mind that once the card has been +// initialized in SPI mode, it can not be reinitialized in SD mode without +// toggling power to the card. + +#ifdef USE_SPI_MODE +// Pin mapping when using SPI mode. +// With this mapping, SD card can be used both in SPI and 1-line SD mode. +// Note that a pull-up on CS line is required in SD mode. +#define PIN_NUM_MISO ((gpio_num_t)2) +#define PIN_NUM_MOSI ((gpio_num_t)15) +#define PIN_NUM_CLK ((gpio_num_t)14) +#define PIN_NUM_CS ((gpio_num_t)13) +#endif //USE_SPI_MODE + +int storage_init() +{ +#ifndef USE_SPI_MODE + ESP_LOGI(TAG, "Using SDMMC peripheral"); + sdmmc_host_t host = SDMMC_HOST_DEFAULT(); + + // This initializes the slot without card detect (CD) and write protect (WP) signals. + // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals. + sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT(); + + // To use 1-line SD mode, uncomment the following line: + slot_config.width = 1; + + // GPIOs 15, 2, 4, 12, 13 should have external 10k pull-ups. + // Internal pull-ups are not sufficient. However, enabling internal pull-ups + // does make a difference some boards, so we do that here. + gpio_set_pull_mode((gpio_num_t)15, GPIO_PULLUP_ONLY); // CMD, needed in 4- and 1- line modes + gpio_set_pull_mode((gpio_num_t)2, GPIO_PULLUP_ONLY); // D0, needed in 4- and 1-line modes + //gpio_set_pull_mode((gpio_num_t)4, GPIO_PULLUP_ONLY); // D1, needed in 4-line mode only + //gpio_set_pull_mode((gpio_num_t)12, GPIO_PULLUP_ONLY); // D2, needed in 4-line mode only + gpio_set_pull_mode((gpio_num_t)13, GPIO_PULLUP_ONLY); // D3, needed in 4- and 1-line modes + +#else + ESP_LOGI(TAG, "Using SPI peripheral"); + + sdmmc_host_t host = SDSPI_HOST_DEFAULT(); + sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT(); + slot_config.gpio_miso = PIN_NUM_MISO; + slot_config.gpio_mosi = PIN_NUM_MOSI; + slot_config.gpio_sck = PIN_NUM_CLK; + slot_config.gpio_cs = PIN_NUM_CS; + // This initializes the slot without card detect (CD) and write protect (WP) signals. + // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals. +#endif //USE_SPI_MODE + + // Options for mounting the filesystem. + // If format_if_mount_failed is set to true, SD card will be partitioned and + // formatted in case when mounting fails. + esp_vfs_fat_sdmmc_mount_config_t mount_config = { + .format_if_mount_failed = false, + .max_files = 5, + .allocation_unit_size = 16 * 1024 + }; + + // Use settings defined above to initialize SD card and mount FAT filesystem. + // Note: esp_vfs_fat_sdmmc_mount is an all-in-one convenience function. + // Please check its source code and implement error recovery when developing + // production applications. + sdmmc_card_t* card; + esp_err_t ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card); + + if (ret != ESP_OK) { + if (ret == ESP_FAIL) { + ESP_LOGE(TAG, "Failed to mount filesystem. " + "If you want the card to be formatted, set format_if_mount_failed = true."); + } else { + ESP_LOGE(TAG, "Failed to initialize the card (%s). " + "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret)); + } + return -1; + } + + // Card has been initialized, print its properties + sdmmc_card_print_info(stdout, card); + + return 0; +} diff --git a/main/storage.h b/main/storage.h new file mode 100644 index 0000000..ef12232 --- /dev/null +++ b/main/storage.h @@ -0,0 +1,2 @@ + +int storage_init(); \ No newline at end of file