Skip to content

Commit 50609f7

Browse files
committed
🎨 Improve structure of the FS code
1 parent d872814 commit 50609f7

File tree

2 files changed

+53
-37
lines changed

2 files changed

+53
-37
lines changed

src/provisioning/tinyusb/Wippersnapper_FS_V2.cpp

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* @file Wippersnapper_FS_V2.cpp
33
*
4-
* Wippersnapper TinyUSB Filesystem
4+
* Wippersnapper TinyUSB Filesystem Driver
55
*
66
* Adafruit invests time and resources providing this open source code,
77
* please support Adafruit and open-source hardware by purchasing
@@ -52,33 +52,38 @@ Adafruit_FlashTransport_RP2040 flashTransport_v2;
5252
#endif
5353

5454
Adafruit_SPIFlash flash_v2(&flashTransport_v2); ///< SPIFlash object
55-
FatVolume wipperFatFs_v2; ///< File system object from Adafruit SDFat
56-
55+
FatVolume wipperFatFs_v2; ///< File system object from Adafruit SDFat library
5756
Adafruit_USBD_MSC usb_msc_v2; /*!< USB mass storage object */
5857

59-
FATFS elmchamFatfs_v2; ///< Elm Cham's fatfs object
60-
uint8_t workbuf_v2[4096]; ///< Working buffer for f_fdisk function.
61-
58+
/**************************************************************************/
59+
/*!
60+
@brief Formats the flash filesystem as FAT12.
61+
@returns FR_OK if filesystem formatted correctly,
62+
otherwise any FRESULT enum.
63+
*/
64+
/**************************************************************************/
6265
FRESULT format_fs_fat12(void) {
63-
// Make filesystem
66+
FATFS elmchamFatfs_v2;
67+
uint8_t workbuf_v2[4096];
68+
69+
// make filesystem
6470
FRESULT r = f_mkfs("", FM_FAT | FM_SFD, 0, workbuf_v2, sizeof(workbuf_v2));
6571

6672
// mount to set disk label
6773
r = f_mount(&elmchamFatfs_v2, "0:", 1);
6874
if (r != FR_OK)
6975
return r;
7076

71-
// set label
77+
// set fs label
7278
r = f_setlabel("WIPPER");
7379
if (r != FR_OK)
7480
return r;
7581

76-
// unmount
82+
// unmount fs
7783
f_unmount("0:");
7884

7985
// sync to make sure all data is written to flash
8086
flash_v2.syncBlocks();
81-
8287
return r;
8388
}
8489

@@ -99,19 +104,19 @@ Wippersnapper_FS_V2::Wippersnapper_FS_V2() {
99104
fsHalt("Failed to initialize the flash chip!");
100105
}
101106

102-
// Check if the filesystem is formatted
103-
_isFormatted = wipperFatFs_v2.begin(&flash_v2);
107+
// Attempt to initialize the filesystem
108+
bool is_fs_formatted = wipperFatFs_v2.begin(&flash_v2);
104109

105110
// If we are not formatted, attempt to format the filesystem as fat12
106-
if (!_isFormatted) {
111+
if (!is_fs_formatted) {
107112
FRESULT rc = format_fs_fat12();
108113

109-
if (format_fs_fat12() != FR_OK) {
114+
if (rc != FR_OK) {
110115
setStatusLEDColor(RED);
111116
fsHalt("FATAL ERROR: Failed to format the filesystem!");
112117
}
113118

114-
// now that we formatted, we need to re-init the filesystem
119+
// Now that we have a formatted filesystem, we need to inititalize it
115120
if (!wipperFatFs_v2.begin(&flash_v2)) {
116121
setStatusLEDColor(RED);
117122
fsHalt("FATAL ERROR: Failed to mount newly created filesystem!");
@@ -140,11 +145,9 @@ Wippersnapper_FS_V2::Wippersnapper_FS_V2() {
140145
"Please edit it to reflect your Adafruit IO and network credentials. "
141146
"When you're done, press RESET on the board.");
142147
#endif
143-
fsHalt(
144-
"INVALID SETTINGS FILE",
145-
"The settings.json file on the WIPPER drive contains default values. "
146-
"Please edit it to reflect your Adafruit IO and network credentials. "
147-
"When you're done, press RESET on the board.");
148+
fsHalt("The settings.json file on the WIPPER drive contains default "
149+
"values\n. Using a text editor, edit it to reflect your Adafruit IO "
150+
"and WiFi credentials. Then, reset the board.");
148151
}
149152
}
150153

@@ -153,8 +156,17 @@ Wippersnapper_FS_V2::Wippersnapper_FS_V2() {
153156
@brief Filesystem destructor
154157
*/
155158
/************************************************************/
156-
Wippersnapper_FS_V2::~Wippersnapper_FS_V2() {}
159+
Wippersnapper_FS_V2::~Wippersnapper_FS_V2() {
160+
// Unmount filesystem
161+
wipperFatFs_v2.end();
162+
}
157163

164+
/**************************************************************************/
165+
/*!
166+
@brief Writes files to the filesystem to disable macOS from indexing.
167+
@returns True if files written successfully, false otherwise.
168+
*/
169+
/**************************************************************************/
158170
bool disableMacOSIndexing() {
159171
wipperFatFs_v2.mkdir("/.fseventsd/");
160172
File32 writeFile = wipperFatFs_v2.open("/.fseventsd/no_log", FILE_WRITE);
@@ -247,9 +259,7 @@ void Wippersnapper_FS_V2::initUSBMSC() {
247259
/**************************************************************************/
248260
bool Wippersnapper_FS_V2::getSecretsFile() {
249261
// Does secrets.json file exist?
250-
if (!wipperFatFs_v2.exists("/secrets.json"))
251-
return false;
252-
return true;
262+
return wipperFatFs_v2.exists("/secrets.json");
253263
}
254264

255265
/**************************************************************************/
@@ -273,9 +283,8 @@ void Wippersnapper_FS_V2::eraseCPFS() {
273283
/**************************************************************************/
274284
void Wippersnapper_FS_V2::eraseBootFile() {
275285
// overwrite previous boot_out file on each boot
276-
if (wipperFatFs_v2.exists("/wipper_boot_out.txt")) {
286+
if (wipperFatFs_v2.exists("/wipper_boot_out.txt"))
277287
wipperFatFs_v2.remove("/wipper_boot_out.txt");
278-
}
279288
}
280289

281290
/**************************************************************************/
@@ -477,15 +486,11 @@ void Wippersnapper_FS_V2::parseSecrets() {
477486
void Wippersnapper_FS_V2::writeToBootOut(PGM_P str) {
478487
// Append error output to FS
479488
File32 bootFile = wipperFatFs_v2.open("/wipper_boot_out.txt", FILE_WRITE);
480-
if (bootFile) {
481-
bootFile.print(str);
482-
bootFile.flush();
483-
bootFile.close();
484-
} else {
485-
WS_DEBUG_PRINTLN("ERROR: Unable to open wipper_boot_out.txt for logging!");
486-
// feels like we should check why, if good use-case ok, otherwise fsHalt
487-
// as indicates fs corruption or disc access issue (maybe latter is okay)
488-
}
489+
if (!bootFile)
490+
fsHalt("ERROR: Unable to open wipper_boot_out.txt for logging!");
491+
bootFile.print(str);
492+
bootFile.flush();
493+
bootFile.close();
489494
}
490495

491496
/**************************************************************************/
@@ -502,12 +507,17 @@ void Wippersnapper_FS_V2::fsHalt(String msg) {
502507
while (1) {
503508
WS_DEBUG_PRINT("Execution Halted: ");
504509
WS_DEBUG_PRINTLN(msg.c_str());
505-
delay(1000);
510+
delay(5000);
506511
yield();
507512
}
508513
}
509514

510515
#ifdef ARDUINO_FUNHOUSE_ESP32S2
516+
/**************************************************************************/
517+
/*!
518+
@brief Creates a default display_config.json file on the filesystem.
519+
*/
520+
/**************************************************************************/
511521
void Wippersnapper_FS_V2::createDisplayConfig() {
512522
// Open file for writing
513523
File32 displayFile = wipperFatFs_v2.open("/display_config.json", FILE_WRITE);
@@ -537,6 +547,13 @@ void Wippersnapper_FS_V2::createDisplayConfig() {
537547
delay(2500); // give FS some time to write the file
538548
}
539549

550+
/**************************************************************************/
551+
/*!
552+
@brief Parses a display_config.json file on the flash filesystem.
553+
@param dispCfg
554+
displayConfig struct to populate.
555+
*/
556+
/**************************************************************************/
540557
void Wippersnapper_FS_V2::parseDisplayConfig(displayConfig &dispCfg) {
541558
// Check if display_config.json file exists, if not, generate it
542559
if (!wipperFatFs_v2.exists("/display_config.json")) {

src/provisioning/tinyusb/Wippersnapper_FS_V2.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ class Wippersnapper_FS_V2 {
6363
void createDisplayConfig();
6464
#endif
6565
private:
66-
bool _isFormatted = false;
6766
bool _is_secrets_file_empty = false;
6867
};
6968

0 commit comments

Comments
 (0)