Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» General 'MediaFile' type alias (MarlinFirmware#24424)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhapsodyv authored and thinkyhead committed Apr 7, 2023
1 parent 4000db2 commit e3cff7a
Show file tree
Hide file tree
Showing 19 changed files with 135 additions and 55 deletions.
4 changes: 2 additions & 2 deletions Marlin/src/HAL/STM32/eeprom_sdcard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static char _ALIGN(4) HAL_eeprom_data[MARLIN_EEPROM_SIZE];
bool PersistentStore::access_start() {
if (!card.isMounted()) return false;

SdFile file, root = card.getroot();
MediaFile file, root = card.getroot();
if (!file.open(&root, EEPROM_FILENAME, O_RDONLY))
return true;

Expand All @@ -63,7 +63,7 @@ bool PersistentStore::access_start() {
bool PersistentStore::access_finish() {
if (!card.isMounted()) return false;

SdFile file, root = card.getroot();
MediaFile file, root = card.getroot();
int bytes_written = 0;
if (file.open(&root, EEPROM_FILENAME, O_CREAT | O_WRITE | O_TRUNC)) {
bytes_written = file.write(HAL_eeprom_data, MARLIN_EEPROM_SIZE);
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/HAL/STM32F1/eeprom_sdcard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static char _ALIGN(4) HAL_eeprom_data[MARLIN_EEPROM_SIZE];
bool PersistentStore::access_start() {
if (!card.isMounted()) return false;

SdFile file, root = card.getroot();
MediaFile file, root = card.getroot();
if (!file.open(&root, EEPROM_FILENAME, O_RDONLY))
return true; // false aborts the save

Expand All @@ -62,7 +62,7 @@ bool PersistentStore::access_start() {
bool PersistentStore::access_finish() {
if (!card.isMounted()) return false;

SdFile file, root = card.getroot();
MediaFile file, root = card.getroot();
int bytes_written = 0;
if (file.open(&root, EEPROM_FILENAME, O_CREAT | O_WRITE | O_TRUNC)) {
bytes_written = file.write(HAL_eeprom_data, MARLIN_EEPROM_SIZE);
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/feature/powerloss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

bool PrintJobRecovery::enabled; // Initialized by settings.load()

SdFile PrintJobRecovery::file;
MediaFile PrintJobRecovery::file;
job_recovery_info_t PrintJobRecovery::info;
const char PrintJobRecovery::filename[5] = "/PLR";
uint8_t PrintJobRecovery::queue_index_r;
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/feature/powerloss.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class PrintJobRecovery {
public:
static const char filename[5];

static SdFile file;
static MediaFile file;
static job_recovery_info_t info;

static uint8_t queue_index_r; //!< Queue index of the active command
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/lcd/e3v2/jyersui/dwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4663,7 +4663,7 @@ void CrealityDWINClass::Start_Print(bool sd) {
if (sd) {
#if ENABLED(POWER_LOSS_RECOVERY)
if (recovery.valid()) {
SdFile *diveDir = nullptr;
MediaFile *diveDir = nullptr;
const char * const fname = card.diveToFile(true, diveDir, recovery.info.sd_filename);
card.selectFileByName(fname);
}
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/lcd/e3v2/proui/dwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,7 @@ void EachMomentUpdate() {
DWINUI::Draw_Button(BTN_Cancel, 26, 280);
DWINUI::Draw_Button(BTN_Continue, 146, 280);
}
SdFile *dir = nullptr;
MediaFile *dir = nullptr;
const char * const filename = card.diveToFile(true, dir, recovery.info.sd_filename);
card.selectFileByName(filename);
DWINUI::Draw_CenteredString(HMI_data.PopupTxt_Color, 207, card.longest_filename());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
class MediaFileReader {
private:
#if ENABLED(SDSUPPORT)
SdFile root, file;
MediaFile root, file;
#endif

public:
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/lcd/extui/mks_ui/draw_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ static void btn_ok_event_cb(lv_obj_t *btn, lv_event_t event) {
char *cur_name;
cur_name = strrchr(list_file.file_name[sel_id], '/');

SdFile file, *curDir;
MediaFile file, *curDir;
card.abortFilePrintNow();
const char * const fname = card.diveToFile(false, curDir, cur_name);
if (!fname) return;
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/lcd/extui/mks_ui/draw_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,8 @@ char *creat_title_text() {

char *cur_name = strrchr(list_file.file_name[sel_id], '/');

SdFile file;
SdFile *curDir;
MediaFile file;
MediaFile *curDir;
const char * const fname = card.diveToFile(false, curDir, cur_name);
if (!fname) return;
if (file.open(curDir, fname, O_READ)) {
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/lcd/extui/mks_ui/mks_hardware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ void disp_assets_update_progress(FSTR_P const fmsg) {
uint8_t mks_test_flag = 0;
const char *MKSTestPath = "MKS_TEST";
void mks_test_get() {
SdFile dir, root = card.getroot();
MediaFile dir, root = card.getroot();
if (dir.open(&root, MKSTestPath, O_RDONLY))
mks_test_flag = 0x1E;
}
Expand Down
6 changes: 3 additions & 3 deletions Marlin/src/lcd/extui/mks_ui/pic_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,8 @@ uint32_t Pic_Info_Write(uint8_t *P_name, uint32_t P_size) {
#define ASSET_TYPE_TITLE_LOGO 2
#define ASSET_TYPE_G_PREVIEW 3
#define ASSET_TYPE_FONT 4
static void loadAsset(SdFile &dir, dir_t& entry, FSTR_P const fn, int8_t assetType) {
SdFile file;
static void loadAsset(MediaFile &dir, dir_t& entry, FSTR_P const fn, int8_t assetType) {
MediaFile file;
char dosFilename[FILENAME_LENGTH];
createFilename(dosFilename, entry);
if (!file.open(&dir, dosFilename, O_READ)) {
Expand Down Expand Up @@ -488,7 +488,7 @@ uint32_t Pic_Info_Write(uint8_t *P_name, uint32_t P_size) {

void UpdateAssets() {
if (!card.isMounted()) return;
SdFile dir, root = card.getroot();
MediaFile dir, root = card.getroot();
if (dir.open(&root, assetsPath, O_RDONLY)) {

disp_assets_update();
Expand Down
10 changes: 5 additions & 5 deletions Marlin/src/lcd/extui/mks_ui/wifi_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ int32_t lastFragment = 0;

char saveFilePath[50];

static SdFile upload_file, *upload_curDir;
static MediaFile upload_file, *upload_curDir;
static filepos_t pos;

int write_to_file(char *buf, int len) {
Expand Down Expand Up @@ -974,8 +974,8 @@ static void wifi_gcode_exec(uint8_t *cmd_line) {
if (!gcode_preview_over) {
char *cur_name = strrchr(list_file.file_name[sel_id], '/');

SdFile file;
SdFile *curDir;
MediaFile file;
MediaFile *curDir;
card.abortFilePrintNow();
const char * const fname = card.diveToFile(false, curDir, cur_name);
if (!fname) return;
Expand Down Expand Up @@ -1595,7 +1595,7 @@ static void file_fragment_msg_handle(uint8_t * msg, uint16_t msgLen) {
}
}
upload_file.close();
SdFile file, *curDir;
MediaFile file, *curDir;
const char * const fname = card.diveToFile(false, curDir, saveFilePath);
if (file.open(curDir, fname, O_RDWR)) {
gCfgItems.curFilesize = file.fileSize();
Expand Down Expand Up @@ -1969,7 +1969,7 @@ void mks_wifi_firmware_update() {

if (wifi_upload(0) >= 0) {
card.removeFile((char *)ESP_FIRMWARE_FILE_RENAME);
SdFile file, *curDir;
MediaFile file, *curDir;
const char * const fname = card.diveToFile(false, curDir, ESP_FIRMWARE_FILE);
if (file.open(curDir, fname, O_READ)) {
file.rename(curDir, (char *)ESP_FIRMWARE_FILE_RENAME);
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/lcd/extui/mks_ui/wifi_upload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ static const uint32_t defaultTimeout = 500;
static const uint32_t eraseTimeout = 15000;
static const uint32_t blockWriteTimeout = 200;
static const uint32_t blockWriteInterval = 15; // 15ms is long enough, 10ms is mostly too short
static SdFile update_file, *update_curDir;
static MediaFile update_file, *update_curDir;

// Messages corresponding to result codes, should make sense when followed by " error"
const char *resultMessages[] = {
Expand Down
2 changes: 2 additions & 0 deletions Marlin/src/sd/SdFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ class SdFile : public SdBaseFile {
void write_P(PGM_P str);
void writeln_P(PGM_P str);
};

using MediaFile = SdFile;
2 changes: 2 additions & 0 deletions Marlin/src/sd/SdVolume.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,5 @@ class SdVolume {
bool readBlock(uint32_t block, uint8_t *dst) { return sdCard_->readBlock(block, dst); }
bool writeBlock(uint32_t block, const uint8_t *dst) { return sdCard_->writeBlock(block, dst); }
};

using MarlinVolume = SdVolume;
44 changes: 22 additions & 22 deletions Marlin/src/sd/cardreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ IF_DISABLED(NO_SD_AUTOSTART, uint8_t CardReader::autofile_index); // = 0

// private:

SdFile CardReader::root, CardReader::workDir, CardReader::workDirParents[MAX_DIR_DEPTH];
MediaFile CardReader::root, CardReader::workDir, CardReader::workDirParents[MAX_DIR_DEPTH];
uint8_t CardReader::workDirDepth;

#if ENABLED(SDCARD_SORT_ALPHA)
Expand Down Expand Up @@ -133,8 +133,8 @@ uint8_t CardReader::workDirDepth;
#endif

DiskIODriver* CardReader::driver = nullptr;
SdVolume CardReader::volume;
SdFile CardReader::file;
MarlinVolume CardReader::volume;
MediaFile CardReader::file;

#if HAS_MEDIA_SUBCALLS
uint8_t CardReader::file_subcall_ctr;
Expand Down Expand Up @@ -228,7 +228,7 @@ bool CardReader::is_visible_entity(const dir_t &p OPTARG(CUSTOM_FIRMWARE_UPLOAD,
//
// Get the number of (compliant) items in the folder
//
int CardReader::countItems(SdFile dir) {
int CardReader::countItems(MediaFile dir) {
dir_t p;
int c = 0;
while (dir.readDir(&p, longFilename) > 0)
Expand All @@ -244,7 +244,7 @@ int CardReader::countItems(SdFile dir) {
//
// Get file/folder info for an item by index
//
void CardReader::selectByIndex(SdFile dir, const uint8_t index) {
void CardReader::selectByIndex(MediaFile dir, const uint8_t index) {
dir_t p;
for (uint8_t cnt = 0; dir.readDir(&p, longFilename) > 0;) {
if (is_visible_entity(p)) {
Expand All @@ -260,7 +260,7 @@ void CardReader::selectByIndex(SdFile dir, const uint8_t index) {
//
// Get file/folder info for an item by name
//
void CardReader::selectByName(SdFile dir, const char * const match) {
void CardReader::selectByName(MediaFile dir, const char * const match) {
dir_t p;
for (uint8_t cnt = 0; dir.readDir(&p, longFilename) > 0; cnt++) {
if (is_visible_entity(p)) {
Expand All @@ -280,7 +280,7 @@ void CardReader::selectByName(SdFile dir, const char * const match) {
* this can blow up the stack, so a 'depth' parameter would be a
* good addition.
*/
void CardReader::printListing(SdFile parent, const char * const prepend, const uint8_t lsflags
void CardReader::printListing(MediaFile parent, const char * const prepend, const uint8_t lsflags
OPTARG(LONG_FILENAME_HOST_SUPPORT, const char * const prependLong/*=nullptr*/)
) {
const bool includeTime = TERN0(M20_TIMESTAMP_SUPPORT, TEST(lsflags, LS_TIMESTAMP));
Expand All @@ -304,7 +304,7 @@ void CardReader::printListing(SdFile parent, const char * const prepend, const

// Get a new directory object using the full path
// and dive recursively into it.
SdFile child; // child.close() in destructor
MediaFile child; // child.close() in destructor
if (child.open(&parent, dosFilename, O_READ)) {
#if ENABLED(LONG_FILENAME_HOST_SUPPORT)
if (includeLong) {
Expand Down Expand Up @@ -375,7 +375,7 @@ void CardReader::ls(const uint8_t lsflags) {
// Zero out slashes to make segments
for (i = 0; i < pathLen; i++) if (path[i] == '/') path[i] = '\0';

SdFile diveDir = root; // start from the root for segment 1
MediaFile diveDir = root; // start from the root for segment 1
for (i = 0; i < pathLen;) {

if (path[i] == '\0') i++; // move past a single nul
Expand Down Expand Up @@ -404,7 +404,7 @@ void CardReader::ls(const uint8_t lsflags) {
// SERIAL_ECHOPGM("Opening dir: "); SERIAL_ECHOLN(segment);

// Open the sub-item as the new dive parent
SdFile dir;
MediaFile dir;
if (!dir.open(&diveDir, segment, O_READ)) {
SERIAL_EOL();
SERIAL_ECHO_START();
Expand Down Expand Up @@ -604,7 +604,7 @@ void CardReader::getAbsFilenameInCWD(char *dst) {
*dst++ = '/';
uint8_t cnt = 1;

auto appendAtom = [&](SdFile &file) {
auto appendAtom = [&](MediaFile &file) {
file.getDosName(dst);
while (*dst && cnt < MAXPATHNAMELENGTH) { dst++; cnt++; }
if (cnt < MAXPATHNAMELENGTH) { *dst = '/'; dst++; cnt++; }
Expand Down Expand Up @@ -683,7 +683,7 @@ void CardReader::openFileRead(const char * const path, const uint8_t subcall_typ

abortFilePrintNow();

SdFile *diveDir;
MediaFile *diveDir;
const char * const fname = diveToFile(true, diveDir, path);
if (!fname) return openFailed(path);

Expand Down Expand Up @@ -719,7 +719,7 @@ void CardReader::openFileWrite(const char * const path) {

abortFilePrintNow();

SdFile *diveDir;
MediaFile *diveDir;
const char * const fname = diveToFile(false, diveDir, path);
if (!fname) return openFailed(path);

Expand Down Expand Up @@ -747,7 +747,7 @@ bool CardReader::fileExists(const char * const path) {
DEBUG_ECHOLNPGM("fileExists: ", path);

// Dive to the file's directory and get the base name
SdFile *diveDir = nullptr;
MediaFile *diveDir = nullptr;
const char * const fname = diveToFile(false, diveDir, path);
if (!fname) return false;

Expand All @@ -757,7 +757,7 @@ bool CardReader::fileExists(const char * const path) {
//diveDir->close();

// Try to open the file and return the result
SdFile tmpFile;
MediaFile tmpFile;
const bool success = tmpFile.open(diveDir, fname, O_READ);
if (success) tmpFile.close();
return success;
Expand All @@ -771,7 +771,7 @@ void CardReader::removeFile(const char * const name) {

//abortFilePrintNow();

SdFile *itsDirPtr;
MediaFile *itsDirPtr;
const char * const fname = diveToFile(false, itsDirPtr, name);
if (!fname) return;

Expand Down Expand Up @@ -919,23 +919,23 @@ uint16_t CardReader::countFilesInWorkDir() {
* - The workDir points to the last-set navigation target by cd, cdup, cdroot, or diveToFile(true, ...)
*
* On exit:
* - Your curDir pointer contains an SdFile reference to the file's directory.
* - Your curDir pointer contains an MediaFile reference to the file's directory.
* - If update_cwd was 'true' the workDir now points to the file's directory.
*
* Returns a pointer to the last segment (filename) of the given DOS 8.3 path.
* On exit, inDirPtr contains an SdFile reference to the file's directory.
* On exit, inDirPtr contains an MediaFile reference to the file's directory.
*
* A nullptr result indicates an unrecoverable error.
*
* NOTE: End the path with a slash to dive to a folder. In this case the
* returned filename will be blank (points to the end of the path).
*/
const char* CardReader::diveToFile(const bool update_cwd, SdFile* &inDirPtr, const char * const path, const bool echo/*=false*/) {
const char* CardReader::diveToFile(const bool update_cwd, MediaFile* &inDirPtr, const char * const path, const bool echo/*=false*/) {
DEBUG_SECTION(est, "diveToFile", true);

// Track both parent and subfolder
static SdFile newDir1, newDir2;
SdFile *sub = &newDir1, *startDirPtr;
static MediaFile newDir1, newDir2;
MediaFile *sub = &newDir1, *startDirPtr;

// Parsing the path string
const char *atom_ptr = path;
Expand Down Expand Up @@ -1017,7 +1017,7 @@ const char* CardReader::diveToFile(const bool update_cwd, SdFile* &inDirPtr, con
}

void CardReader::cd(const char * relpath) {
SdFile newDir, *parent = &getWorkDir();
MediaFile newDir, *parent = &getWorkDir();

if (newDir.open(parent, relpath, O_READ)) {
workDir = newDir;
Expand Down
Loading

0 comments on commit e3cff7a

Please sign in to comment.