From fd1d17b18cfc3c600d6495182cc2727b71f85479 Mon Sep 17 00:00:00 2001 From: Ryzee119 Date: Tue, 23 Nov 2021 10:12:27 +1030 Subject: [PATCH] FileIO: Rework for portability --- platformio.ini | 2 + src/fileio.cpp | 60 ++++++++++++--------------- src/fileio.h | 9 ++-- src/teensy41/fileio_t4.cpp | 85 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 36 deletions(-) create mode 100644 src/teensy41/fileio_t4.cpp diff --git a/platformio.ini b/platformio.ini index 965f8bbb..4ac8ac47 100644 --- a/platformio.ini +++ b/platformio.ini @@ -60,6 +60,8 @@ src_filter = + + + + + + + + build_flags = diff --git a/src/fileio.cpp b/src/fileio.cpp index 19e0ac9f..8c0088de 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -1,20 +1,19 @@ // Copyright 2020, Ryan Wendland, usb64 // SPDX-License-Identifier: MIT -#include -#include #include "usb64_conf.h" #include "printf.h" +#include "fileio.h" void fileio_init() { - if (!SD.sdfs.begin(SdioConfig(FIFO_SDIO))) + if (!fileio_dev_init()) { debug_print_error("[FILEIO] ERROR: Could not open SD Card\n"); } else { - debug_print_fatfs("[FILEIO] Opened SD card OK! Size: %lld MB\n", SD.totalSize()/1024/1024); + debug_print_fatfs("[FILEIO] Opened SD card OK!\n"); } } @@ -31,30 +30,31 @@ void fileio_init() uint32_t fileio_list_directory(char **list, uint32_t max) { int file_count = 0; - File root = SD.open("/"); + int root = fileio_dev_open_dir("/"); - if (root == false) + if (root == 0) { debug_print_error("[FILEIO] ERROR: Could not read SD Card\n"); return 0; } + debug_print_fatfs("[FILEIO] fileio_list_directory %08x!\n", root); + while (true) { - File entry = root.openNextFile(); - if (entry == false) - break; + const char* filename = fileio_dev_get_next_filename(root); - if (!entry.isDirectory()) + if (filename == NULL) { - debug_print_fatfs("Found file: %s\n", entry.name()); - list[file_count] = (char *)malloc(strlen(entry.name()) + 1); - strcpy(list[file_count], entry.name()); - file_count++; + break; } - entry.close(); + + debug_print_fatfs("Found file: %s\n", filename); + list[file_count] = (char *)malloc(strlen(filename) + 1); + strcpy(list[file_count], filename); + file_count++; } - root.close(); + fileio_dev_close_dir(root); return file_count; } @@ -69,21 +69,19 @@ uint32_t fileio_list_directory(char **list, uint32_t max) */ void fileio_write_to_file(char *filename, uint8_t *data, uint32_t len) { - FsFile fil = SD.sdfs.open(filename, O_WRITE | O_CREAT); - if (fil == false) + int ret = fileio_dev_write(filename, data, len); + if (ret == -1) { debug_print_error("[FILEIO] ERROR: Could not open %s for WRITE\n", filename); - return; } - if (fil.write(data, len) != len) + else if (ret == -2) { debug_print_error("[FILEIO] ERROR: Could not write %s\n", filename); } - else + else if (ret == -3) { - debug_print_status("[FILEIO] Writing %s for %u bytes ok!\n", filename, len); + debug_print_status("[FILEIO] Writing %s for %lu bytes ok!\n", filename, len); } - fil.close(); } /* @@ -99,22 +97,18 @@ void fileio_write_to_file(char *filename, uint8_t *data, uint32_t len) */ void fileio_read_from_file(char *filename, uint32_t file_offset, uint8_t *data, uint32_t len) { - FsFile fil = SD.sdfs.open(filename, O_READ); - if (fil == false) + int ret = fileio_dev_read(filename, file_offset, data, len); + + if (ret == -1) { debug_print_error("[FILEIO] ERROR: Could not open %s for READ\n", filename); - return; } - - fil.seekSet(file_offset); - - if (fil.read(data, len) != (int)len) + else if (ret == -2) { debug_print_error("[FILEIO] ERROR: Could not read %s\n", filename); } - else + else if (ret == -3) { - debug_print_status("[FILEIO] Reading %s for %u bytes ok!\n", filename, len); + debug_print_status("[FILEIO] Reading %s for %lu bytes ok!\n", filename, len); } - fil.close(); } diff --git a/src/fileio.h b/src/fileio.h index 1dedebf8..0640a8a0 100644 --- a/src/fileio.h +++ b/src/fileio.h @@ -12,8 +12,11 @@ void fileio_write_to_file(char *filename, uint8_t *data, uint32_t len); void fileio_read_from_file(char *filename, uint32_t file_offset, uint8_t *data, uint32_t len); uint32_t fileio_list_directory(char **list, uint32_t max); -int fileio_open_file_readonly(const char *filename); -void fileio_close_file(); -int fileio_get_line(char* buffer, int max_len); +bool fileio_dev_init(); +int fileio_dev_open_dir(const char* dir); +void fileio_dev_close_dir(int handle); +const char* fileio_dev_get_next_filename(int handle); +int fileio_dev_read(char *filename, uint32_t file_offset, uint8_t *data, uint32_t len); +int fileio_dev_write(char *filename, uint8_t *data, uint32_t len); #endif diff --git a/src/teensy41/fileio_t4.cpp b/src/teensy41/fileio_t4.cpp new file mode 100644 index 00000000..914274f7 --- /dev/null +++ b/src/teensy41/fileio_t4.cpp @@ -0,0 +1,85 @@ +// Copyright 2020, Ryan Wendland, usb64 +// SPDX-License-Identifier: MIT + +#include +#include + +bool fileio_dev_init() +{ + return SD.sdfs.begin(SdioConfig(FIFO_SDIO)); +} + +int fileio_dev_open_dir(const char* dir) +{ + File *handle = new File(); + *handle = SD.open("/"); + if (*handle == false) + { + return 0; + } + else + { + return (int)handle; + } +} + +const char *fileio_dev_get_next_filename(int handle) +{ + File *_handle = (File *)handle; + const char *filename = NULL; + File entry = _handle->openNextFile(); + + if (entry == false) + { + return NULL; + } + filename = entry.name(); + entry.close(); + return filename; +} + +void fileio_dev_close_dir(int handle) +{ + File *_handle = (File *)handle; + _handle->close(); + delete _handle; +} + +int fileio_dev_read(char *filename, uint32_t file_offset, uint8_t *data, uint32_t len) +{ + FsFile fil = SD.sdfs.open(filename, O_READ); + if (fil == false) + { + return -1; + } + + fil.seekSet(file_offset); + + if (fil.read(data, len) != (int)len) + { + return -2; + } + else + { + return -3; + } + fil.close(); +} + +int fileio_dev_write(char *filename, uint8_t *data, uint32_t len) +{ + FsFile fil = SD.sdfs.open(filename, O_WRITE | O_CREAT); + if (fil == false) + { + return -1; + } + if (fil.write(data, len) != len) + { + return -2; + } + else + { + return -3; + } + fil.close(); +}