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

Refactor out staticDIR #3162

Draft
wants to merge 1 commit into
base: community
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions src/deluge/gui/ui/browser/browser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "gui/ui/browser/browser.h"
#include "definitions_cxx.hpp"
#include "extern.h"
#include "fatfs.hpp"
#include "gui/context_menu/delete_file.h"
#include "gui/l10n/l10n.h"
#include "gui/ui_timer_manager.h"
Expand All @@ -35,6 +36,7 @@
#include "storage/file_item.h"
#include "storage/storage_manager.h"
#include "util/functions.h"
#include "util/try.h"
#include <cstring>
#include <new>

Expand Down Expand Up @@ -269,18 +271,7 @@ Error Browser::readFileItemsForFolder(char const* filePrefixHere, bool allowFold
return error;
}

FRESULT result = f_opendir(&staticDIR, currentDir.get());
if (result) {
return fresultToDelugeErrorCode(result);
}

/*
error = fileItems.allocateMemory(FILE_ITEMS_MAX_NUM_ELEMENTS, false);
if (error != Error::NONE) {
f_closedir(&staticDIR);
return error;
}
*/
FatFS::Directory dir = D_TRY_CATCH(FatFS::Directory::open(currentDir.get()), error, { return toError(error); });

numFileItemsDeletedAtStart = 0;
numFileItemsDeletedAtEnd = 0;
Expand All @@ -304,10 +295,12 @@ Error Browser::readFileItemsForFolder(char const* filePrefixHere, bool allowFold
audioFileManager.loadAnyEnqueuedClusters();
FilePointer thisFilePointer;

result = f_readdir_get_filepointer(&staticDIR, &staticFNO, &thisFilePointer); /* Read a directory item */
std::tie(staticFNO, thisFilePointer) = D_TRY_CATCH(dir.read_and_get_filepointer(), error, {
m-m-adams marked this conversation as resolved.
Show resolved Hide resolved
break; // Break on error
});

if (result != FR_OK || staticFNO.fname[0] == 0) {
break; /* Break on error or end of dir */
if (staticFNO.fname[0] == 0) {
break; /* Break on end of dir */
}
if (staticFNO.fname[0] == '.') {
continue; /* Ignore dot entry */
Expand Down Expand Up @@ -388,7 +381,10 @@ Error Browser::readFileItemsForFolder(char const* filePrefixHere, bool allowFold
}
}

f_closedir(&staticDIR);
D_TRY_CATCH(dir.close(), error, {
emptyFileItems();
return toError(error);
});

if (error != Error::NONE) {
emptyFileItems();
Expand Down
19 changes: 11 additions & 8 deletions src/deluge/gui/ui/browser/sample_browser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

#include "definitions_cxx.hpp"
#include "fatfs.hpp"
#include "hid/button.h"
#include "model/sample/sample.h"
#include <ranges>
Expand Down Expand Up @@ -70,6 +71,7 @@
#include "storage/wave_table/wave_table.h"
#include "util/d_string.h"
#include "util/functions.h"
#include "util/try.h"
#include <cstring>

namespace params = deluge::modulation::params;
Expand Down Expand Up @@ -1191,11 +1193,10 @@ bool SampleBrowser::loadAllSamplesInFolder(bool detectPitch, int32_t* getNumSamp
previouslyViewedFilename = currentFileItem->filename.get();
}

FRESULT result = f_opendir(&staticDIR, dirToLoad.get());
if (result != FR_OK) {
FatFS::Directory dir = D_TRY_CATCH(FatFS::Directory::open(dirToLoad.get()), error, {
display->displayError(Error::SD_CARD);
return false;
}
});

int32_t numSamples = 0;

Expand Down Expand Up @@ -1240,9 +1241,12 @@ bool SampleBrowser::loadAllSamplesInFolder(bool detectPitch, int32_t* getNumSamp
audioFileManager.loadAnyEnqueuedClusters();
FilePointer thisFilePointer;

result = f_readdir_get_filepointer(&staticDIR, &staticFNO, &thisFilePointer); /* Read a directory item */
/* Read a directory item */
std::tie(staticFNO, thisFilePointer) = D_TRY_CATCH(dir.read_and_get_filepointer(), error, {
break; // Break on error
});

if (result != FR_OK || staticFNO.fname[0] == 0) {
if (staticFNO.fname[0] == 0) {
break; // Break on error or end of dir
}
if (staticFNO.fname[0] == '.') {
Expand Down Expand Up @@ -1278,7 +1282,7 @@ bool SampleBrowser::loadAllSamplesInFolder(bool detectPitch, int32_t* getNumSamp

if (!maybeNewSample.has_value() || maybeNewSample.value() == nullptr) {
error = maybeNewSample.error_or(Error::NONE);
f_closedir(&staticDIR);
error = dir.close().transform_error(toError).error_or(Error::NONE);
goto removeReasonsFromSamplesAndGetOut;
}

Expand All @@ -1301,7 +1305,7 @@ bool SampleBrowser::loadAllSamplesInFolder(bool detectPitch, int32_t* getNumSamp

numSamples++;
}
f_closedir(&staticDIR);
error = dir.close().transform_error(toError).error_or(Error::NONE);

if (getPrefixAndDirLength) {
// If just one file, there's no prefix.
Expand Down Expand Up @@ -1872,7 +1876,6 @@ bool SampleBrowser::importFolderAsKit() {
range = source->getOrCreateFirstRange();
if (!range) {
getOut:
f_closedir(&staticDIR);
display->displayError(Error::INSUFFICIENT_RAM);
goto doReturnFalse;
}
Expand Down
6 changes: 0 additions & 6 deletions src/deluge/gui/ui/browser/slot_browser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,12 @@ Error SlotBrowser::beginSlotSession(bool shouldDrawKeys, bool allowIfNoFolder) {

// But we won't try to open the folder yet, because we don't yet know what it should be.

/*
FRESULT result = f_opendir(&staticDIR, currentDir.get());
if (result != FR_OK && !allowIfNoFolder) return false;
*/

bool success = Browser::opened();
if (!success) {
return Error::UNSPECIFIED;
}

if (shouldDrawKeys) {

drawKeys();
}

Expand Down
15 changes: 9 additions & 6 deletions src/deluge/gui/views/instrument_clip_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "gui/views/instrument_clip_view.h"
#include "definitions_cxx.hpp"
#include "extern.h"
#include "fatfs.hpp"
#include "gui/colour/colour.h"
#include "gui/l10n/l10n.h"
#include "gui/menu_item/colour.h"
Expand Down Expand Up @@ -93,6 +94,7 @@
#include "util/cfunctions.h"
#include "util/functions.h"
#include "util/lookuptables/lookuptables.h"
#include "util/try.h"
#include <limits>
#include <new>
#include <stdint.h>
Expand Down Expand Up @@ -1558,17 +1560,18 @@ ActionResult InstrumentClipView::padAction(int32_t x, int32_t y, int32_t velocit

// Open directory of current audio file
*slashAddress = 0;
FRESULT result = f_opendir(&staticDIR, path);
*slashAddress = '/';
if (result != FR_OK) {

FatFS::Directory dir = D_TRY_CATCH(FatFS::Directory::open(path), error, {
*slashAddress = '/';
display->displayError(Error::SD_CARD);
return ActionResult::DEALT_WITH;
}
});
*slashAddress = '/';

// Select random audio file from directory
int32_t fileCount = 0;
while (f_readdir(&staticDIR, &staticFNO) == FR_OK && staticFNO.fname[0] != 0) {
std::expected<FatFS::FileInfo, FatFS::Error> maybeFNO;
while ((maybeFNO = dir.read()) && maybeFNO->fname[0] != 0) {
staticFNO = maybeFNO.value();
audioFileManager.loadAnyEnqueuedClusters();
if (staticFNO.fattrib & AM_DIR || !isAudioFilename(staticFNO.fname)) {
continue;
Expand Down
61 changes: 32 additions & 29 deletions src/deluge/storage/audio/audio_file_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "definitions.h"
#include "definitions_cxx.hpp"
#include "extern.h"
#include "fatfs.hpp"
#include "gui/l10n/l10n.h"
#include "gui/ui/ui.h"
#include "hid/display/display.h"
Expand Down Expand Up @@ -235,35 +236,37 @@ Error AudioFileManager::getUnusedAudioRecordingFilePath(String& filePath, String
// recordings will be much smaller
if (highestUsedAudioRecordingNumberNeedsReChecking[folderID]) {

FRESULT result = f_opendir(&staticDIR, audioRecordingFolderNames[folderID]);
if (result == FR_OK) {

while (true) {
loadAnyEnqueuedClusters();
FRESULT result = f_readdir(&staticDIR, &staticFNO); /* Read a directory item */
if (__builtin_expect(result != FR_OK, 0)) {
return Error::SD_CARD;
}

if (__builtin_expect((*(uint32_t*)staticFNO.altname & 0x00FFFFFF) == 0x00434552, 1)) { // "REC"
if (*(uint32_t*)&staticFNO.altname[8] == 0x5641572E) { // ".WAV"

int32_t thisSlot = memToUIntOrError(&staticFNO.altname[3], &staticFNO.altname[8]);
if (thisSlot == -1) {
continue;
}

if (thisSlot > highestUsedAudioRecordingNumber[folderID]) {
highestUsedAudioRecordingNumber[folderID] = thisSlot;
}
}
}
else if (!staticFNO.altname[0]) {
break; /* Break on end of dir */
}
}
// f_closedir(&staticDIR);
}
D_TRY_CATCH( //
FatFS::Directory::open(audioRecordingFolderNames[folderID])
.and_then([&](FatFS::Directory dir) -> std::expected<FatFS::Directory, FatFS::Error> {
while (true) {
loadAnyEnqueuedClusters();
staticFNO = D_TRY(dir.read()); // Read a directory item
if ((*(uint32_t*)staticFNO.altname & 0x00FFFFFF) == 0x00434552) [[likely]] { // "REC"
if (*(uint32_t*)&staticFNO.altname[8] == 0x5641572E) { // ".WAV"

int32_t thisSlot = memToUIntOrError(&staticFNO.altname[3], &staticFNO.altname[8]);
if (thisSlot == -1) {
continue;
}

if (thisSlot > highestUsedAudioRecordingNumber[folderID]) {
highestUsedAudioRecordingNumber[folderID] = thisSlot;
}
}
}
else if (!staticFNO.altname[0]) {
break; /* Break on end of dir */
}
}
return dir;
})
.and_then([](auto dir) { return dir.close(); })
.transform_error(toError),
error, {
// if we encounter any error, return an SD_CARD error
return Error::SD_CARD;
});

highestUsedAudioRecordingNumberNeedsReChecking[folderID] = false;
}
Expand Down
5 changes: 2 additions & 3 deletions src/deluge/storage/storage_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ void routineForSD(void);

FirmwareVersion song_firmware_version = FirmwareVersion::current();
FILINFO staticFNO;
DIR staticDIR;
XMLSerializer smSerializer;
XMLDeserializer smDeserializer;
JsonSerializer smJsonSerializer;
Expand Down Expand Up @@ -162,7 +161,7 @@ std::expected<FatFS::File, Error> StorageManager::createFile(char const* filePat
// Otherwise, just return the appropriate error.
else {

error = fatfsErrorToDelugeError(opened.error());
error = toError(opened.error());
if (error == Error::SD_CARD) {
error = Error::WRITE_FAIL; // Get a bit more specific if we only got the most general error.
}
Expand Down Expand Up @@ -256,7 +255,7 @@ Error StorageManager::initSD() {
}

// Otherwise, we can mount the filesystem...
bool success = D_TRY_CATCH(fileSystem.mount(1).transform_error(fatfsErrorToDelugeError), error, {
bool success = D_TRY_CATCH(fileSystem.mount(1).transform_error(toError), error, {
return error; //<
});
if (success) {
Expand Down
1 change: 0 additions & 1 deletion src/deluge/storage/storage_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,6 @@ Error openInstrumentFile(OutputType outputType, FilePointer* filePointer);

extern FirmwareVersion song_firmware_version;
extern FILINFO staticFNO;
extern DIR staticDIR;
extern const bool writeJsonFlag;

inline bool isCardReady() {
Expand Down
2 changes: 1 addition & 1 deletion src/deluge/util/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2162,7 +2162,7 @@ Error fresultToDelugeErrorCode(FRESULT result) {
}
}

Error fatfsErrorToDelugeError(FatFS::Error result) {
Error toError(FatFS::Error result) {
switch (result) {
case FatFS::Error::NO_FILESYSTEM:
return Error::SD_CARD_NO_FILESYSTEM;
Expand Down
2 changes: 1 addition & 1 deletion src/deluge/util/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ Error fresultToDelugeErrorCode(FRESULT result);
namespace FatFS {
enum class Error;
}
Error fatfsErrorToDelugeError(FatFS::Error result);
Error toError(FatFS::Error result);

[[gnu::always_inline]] inline void writeInt16(char** address, uint16_t number) {
*(uint16_t*)*address = number;
Expand Down
2 changes: 0 additions & 2 deletions src/fatfs/fatfs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ using FileInfo = FILINFO;

class Directory {
public:
Directory(Directory &) = default; // Copy constructor
Directory(Directory &&) = default; // Move constructor
~Directory() { f_closedir(&dir_); }

/* Open a directory */
Expand Down
Loading