Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

De-duplicate blit API -> fatfs glue #852

Merged
merged 8 commits into from
Jul 15, 2024
Merged
1 change: 1 addition & 0 deletions 32blit-config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ if (NOT DEFINED BLIT_ONCE)
add_definitions("-DWIN32")
endif()

add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/3rd-party 3rd-party)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/32blit 32blit)

function (blit_assets_yaml TARGET FILE)
Expand Down
16 changes: 10 additions & 6 deletions 32blit-pico/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ set(PICO_SDK_VERSION_MINOR ${PICO_SDK_VERSION_MINOR} PARENT_SCOPE)
set(PICO_SDK_VERSION_REVISION ${PICO_SDK_VERSION_REVISION} PARENT_SCOPE)

# make sure BlitEngine is built with the right exception flags
target_link_libraries(BlitEngine pico_cxx_options)
target_link_libraries(BlitEngine PUBLIC pico_cxx_options)

# also enable function/data sectons
target_compile_options(BlitEngine PRIVATE -ffunction-sections -fdata-sections)

add_subdirectory(../32blit-shared ../32blit-shared)

# driver helper
# can override driver choice by pre-setting BLIT_x_DRIVER
function(blit_driver DRV NAME)
Expand All @@ -31,9 +33,6 @@ endfunction()

add_library(BlitHalPico INTERFACE)
target_sources(BlitHalPico INTERFACE
${CMAKE_CURRENT_LIST_DIR}/../3rd-party/fatfs/ff.c
${CMAKE_CURRENT_LIST_DIR}/../3rd-party/fatfs/ffunicode.c

${CMAKE_CURRENT_LIST_DIR}/display.cpp
${CMAKE_CURRENT_LIST_DIR}/file.cpp
${CMAKE_CURRENT_LIST_DIR}/led.cpp
Expand All @@ -43,10 +42,15 @@ target_sources(BlitHalPico INTERFACE
${CMAKE_CURRENT_LIST_DIR}/usb_descriptors.c
)

target_link_libraries(BlitHalPico INTERFACE hardware_dma hardware_pio hardware_pwm hardware_spi pico_multicore pico_stdlib pico_unique_id pico_rand tinyusb_device)
target_link_libraries(BlitHalPico INTERFACE
hardware_dma hardware_pio hardware_pwm hardware_spi
pico_multicore pico_stdlib pico_unique_id pico_rand
tinyusb_device
FatFsBlitAPI
)

target_include_directories(BlitHalPico INTERFACE
${CMAKE_CURRENT_LIST_DIR} # for tusb_config
${CMAKE_CURRENT_LIST_DIR}/../3rd-party/fatfs
)

target_compile_definitions(BlitHalPico INTERFACE
Expand Down
146 changes: 0 additions & 146 deletions 32blit-pico/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
static FATFS fs;
static bool initialised = false;

std::vector<void *> open_files;

// fatfs io funcs
DSTATUS disk_initialize(BYTE pdrv) {
initialised = storage_init();
Expand Down Expand Up @@ -81,150 +79,6 @@ void init_fs() {
printf("Failed to mount filesystem! (%i)\n", res);
}

bool get_files_open() {
return open_files.size() > 0;
}

void close_open_files() {
while(!open_files.empty())
close_file(open_files.back());
}

void *open_file(const std::string &file, int mode) {
FIL *f = new FIL();

BYTE ff_mode = 0;

if(mode & blit::OpenMode::read)
ff_mode |= FA_READ;

if(mode & blit::OpenMode::write)
ff_mode |= FA_WRITE;

if(mode == blit::OpenMode::write)
ff_mode |= FA_CREATE_ALWAYS;

FRESULT r = f_open(f, file.c_str(), ff_mode);

if(r == FR_OK) {
open_files.push_back(f);
return f;
}

delete f;
return nullptr;
}

int32_t read_file(void *fh, uint32_t offset, uint32_t length, char *buffer) {
FRESULT r = FR_OK;
FIL *f = (FIL *)fh;

if(offset != f_tell(f))
r = f_lseek(f, offset);

if(r == FR_OK){
unsigned int bytes_read;
r = f_read(f, buffer, length, &bytes_read);
if(r == FR_OK){
return bytes_read;
}
}

return -1;
}

int32_t write_file(void *fh, uint32_t offset, uint32_t length, const char *buffer) {
FRESULT r = FR_OK;
FIL *f = (FIL *)fh;

if(offset != f_tell(f))
r = f_lseek(f, offset);

if(r == FR_OK) {
unsigned int bytes_written;
r = f_write(f, buffer, length, &bytes_written);
if(r == FR_OK) {
return bytes_written;
}
}

return -1;
}

int32_t close_file(void *fh) {
FRESULT r;

r = f_close((FIL *)fh);

for(auto it = open_files.begin(); it != open_files.end(); ++it) {
if(*it == fh) {
open_files.erase(it);
break;
}
}

delete (FIL *)fh;
return r == FR_OK ? 0 : -1;
}

uint32_t get_file_length(void *fh) {
return f_size((FIL *)fh);
}

void list_files(const std::string &path, std::function<void(blit::FileInfo &)> callback) {
DIR dir;

if(f_opendir(&dir, path.c_str()) != FR_OK)
return;

FILINFO ent;

while(f_readdir(&dir, &ent) == FR_OK && ent.fname[0]) {
blit::FileInfo info;

info.name = ent.fname;
info.flags = 0;
info.size = ent.fsize;

if(ent.fattrib & AM_DIR)
info.flags |= blit::FileFlags::directory;

callback(info);
}

f_closedir(&dir);
}

bool file_exists(const std::string &path) {
FILINFO info;
return f_stat(path.c_str(), &info) == FR_OK && !(info.fattrib & AM_DIR);
}

bool directory_exists(const std::string &path) {
FILINFO info;
return f_stat(path.c_str(), &info) == FR_OK && (info.fattrib & AM_DIR);
}

bool create_directory(const std::string &path) {
FRESULT r;

// strip trailing slash
if(path.back() == '/')
r = f_mkdir(path.substr(0, path.length() - 1).c_str());
else
r = f_mkdir(path.c_str());

return r == FR_OK || r == FR_EXIST;
}

bool rename_file(const std::string &old_name, const std::string &new_name) {
return f_rename(old_name.c_str(), new_name.c_str()) == FR_OK;
}

bool remove_file(const std::string &path) {
return f_unlink(path.c_str()) == FR_OK;
}

static char save_path[32]; // max game title length is 24 + ".blit/" + "/"

const char *get_save_path() {
Expand Down
21 changes: 1 addition & 20 deletions 32blit-pico/file.hpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
#pragma once

#include <cstdint>
#include <functional>
#include <string>
#include <vector>

#include "engine/file.hpp"
#include "fatfs_blit_api.hpp"

void init_fs();

bool get_files_open();
void close_open_files();

void *open_file(const std::string &file, int mode);
int32_t read_file(void *fh, uint32_t offset, uint32_t length, char *buffer);
int32_t write_file(void *fh, uint32_t offset, uint32_t length, const char *buffer);
int32_t close_file(void *fh);
uint32_t get_file_length(void *fh);
void list_files(const std::string &path, std::function<void(blit::FileInfo &)> callback);
bool file_exists(const std::string &path);
bool directory_exists(const std::string &path);
bool create_directory(const std::string &path);
bool rename_file(const std::string &old_name, const std::string &new_name);
bool remove_file(const std::string &path);
const char *get_save_path();
1 change: 1 addition & 0 deletions 32blit-shared/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(fatfs-blit-api)
4 changes: 4 additions & 0 deletions 32blit-shared/fatfs-blit-api/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_library(FatFsBlitAPI INTERFACE)
target_sources(FatFsBlitAPI INTERFACE ${CMAKE_CURRENT_LIST_DIR}/fatfs_blit_api.cpp)
target_include_directories(FatFsBlitAPI INTERFACE ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(FatFsBlitAPI INTERFACE FatFs)
Loading
Loading