Skip to content

Commit

Permalink
FileIO: Rework for portability
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryzee119 committed Nov 22, 2021
1 parent 74cc8fd commit fd1d17b
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 36 deletions.
2 changes: 2 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ src_filter =
+<USBHost_t36/*.cpp>
+<ILI9341_t3n/src/*.cpp>
+<ILI9341_t3n/src/*.c>
+<teensy41/*.cpp>
+<teensy41/*.c>
+<tft/*.cpp>

build_flags =
Expand Down
60 changes: 27 additions & 33 deletions src/fileio.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
// Copyright 2020, Ryan Wendland, usb64
// SPDX-License-Identifier: MIT

#include <Arduino.h>
#include <SD.h>
#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");
}
}

Expand All @@ -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;
}

Expand All @@ -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();
}

/*
Expand All @@ -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();
}
9 changes: 6 additions & 3 deletions src/fileio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
85 changes: 85 additions & 0 deletions src/teensy41/fileio_t4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2020, Ryan Wendland, usb64
// SPDX-License-Identifier: MIT

#include <Arduino.h>
#include <SD.h>

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();
}

0 comments on commit fd1d17b

Please sign in to comment.