Created
April 18, 2023 12:58
-
-
Save bradmartin333/7184c7af538ca900f7cf1f502d46d07c to your computer and use it in GitHub Desktop.
Read and write to mbed filesystem on Arduino Portenta Machine Control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "PluggableUSBMSD.h" | |
#include "QSPIFBlockDevice.h" | |
#include "MBRBlockDevice.h" | |
#include "FATFileSystem.h" | |
REDIRECT_STDOUT_TO(Serial) | |
static QSPIFBlockDevice root; | |
mbed::MBRBlockDevice fs_data(&root, 1); | |
static mbed::FATFileSystem fs("fs"); | |
void USBMSD::begin() { | |
fs.mount(&fs_data); | |
} | |
USBMSD MassStorage(&root); | |
void setup() { | |
Serial.begin(115200); | |
MassStorage.begin(); | |
while(!Serial){} | |
// Write to file | |
FILE* fd = fopen("/fs/numbers.txt", "w"); | |
for (int i = 0; i < 20; i++) { | |
fprintf(fd, "%d\r\n", i); | |
} | |
fclose(fd); | |
// List dir | |
DIR* dir = opendir("/fs/"); | |
struct dirent* de; | |
while ((de = readdir(dir)) != NULL) { | |
Serial.println(&(de->d_name)[0]); | |
} | |
closedir(dir); | |
// Read file | |
fd = fopen("/fs/numbers.txt", "r"); | |
char buff[16] = { 0 }; | |
if (feof(fd)) { | |
Serial.println("Missing or empty file"); | |
} | |
while (!feof(fd)) { | |
int size = fread(&buff[0], 1, 15, fd); | |
fwrite(&buff[0], 1, size, stdout); | |
} | |
fclose(fd); | |
} | |
void loop { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment