arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
VFS_SPFFS.h
1#pragma once
2#include <string.h>
3#include <sys/stat.h>
4#include <sys/unistd.h>
5
6#include "AudioTools/Disk/VFS.h"
7#include "esp_err.h"
8#include "esp_spiffs.h"
9
10namespace audio_tools {
11
21class VFS_SPFFS : public VFS {
22 public:
23 VFS_SPFFS(const char* mountPoint = "/spiffs") { mount_point = mountPoint; };
24 void setMountPoint(const char* mp) { mount_point = mp; }
25 bool begin() {
26 LOGI("Initializing SPIFFS");
27
28 conf = {.base_path = mount_point,
29 .partition_label = NULL,
30 .max_files = max_files,
31 .format_if_mount_failed = format_if_mount_failed};
32
33 // Use settings defined above to initialize and mount SPIFFS filesystem.
34 // Note: esp_vfs_spiffs_register is an all-in-one convenience function.
35 LOGI("Mounting filesystem at %s", mount_point);
36 esp_err_t ret = esp_vfs_spiffs_register(&conf);
37
38 if (ret != ESP_OK) {
39 if (ret == ESP_FAIL) {
40 LOGE("Failed to mount or format filesystem");
41 } else if (ret == ESP_ERR_NOT_FOUND) {
42 LOGE("Failed to find SPIFFS partition");
43 } else {
44 LOGE("Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
45 }
46 return false;
47 }
48
49 LOGI("Performing SPIFFS_check().");
50 ret = esp_spiffs_check(conf.partition_label);
51 if (ret != ESP_OK) {
52 LOGE("SPIFFS_check() failed (%s)", esp_err_to_name(ret));
53 return false;
54 } else {
55 LOGI("SPIFFS_check() successful");
56 }
57
58 size_t total = 0, used = 0;
59 ret = esp_spiffs_info(conf.partition_label, &total, &used);
60 if (ret != ESP_OK) {
61 LOGE("Failed to get SPIFFS partition information (%s). Formatting...",
62 esp_err_to_name(ret));
63 // esp_spiffs_format(conf.partition_label);
64 return false;
65 } else {
66 LOGI("Partition size: total: %d, used: %d", total, used);
67 }
68
69 // Check consistency of reported partition size info.
70 if (used > total) {
71 LOGW(
72 "Number of used bytes cannot be larger than total. Performing "
73 "SPIFFS_check().");
74 ret = esp_spiffs_check(conf.partition_label);
75 // Could be also used to mend broken files, to clean unreferenced pages,
76 // etc. More info at
77 // https://github.com/pellepl/spiffs/wiki/FAQ#powerlosses-contd-when-should-i-run-spiffs_check
78 if (ret != ESP_OK) {
79 LOGE("SPIFFS_check() failed (%s)", esp_err_to_name(ret));
80 return false;
81 } else {
82 LOGI("SPIFFS_check() successful");
83 }
84 }
85 return true;
86 }
87
88 void end() {
89 // All done, unmount partition and disable SPIFFS
90 esp_vfs_spiffs_unregister(conf.partition_label);
91 LOGI("SPIFFS unmounted");
92 }
93
94 void setMaxFile(int files) { max_files = files; }
95 void setFormatIfMountFailed(bool format) { format_if_mount_failed = format; }
96
97 protected:
98 esp_vfs_spiffs_conf_t conf;
99 int max_files = 5;
100 bool format_if_mount_failed = true;
101};
102} // namespace audio_tools
ESP32 Virtual File System for the SPFFS. The default mount point is "/spiffs" DRAFT implementation: n...
Definition VFS_SPFFS.h:21
bool begin()
mount the file systems
Definition VFS_SPFFS.h:25
void setMountPoint(const char *mp)
provide the mount point (root directory for the file system)
Definition VFS_SPFFS.h:24
void end()
unmount the file system
Definition VFS_SPFFS.h:88
Definition VFS.h:16
const char * mountPoint()
provides the actual mount point
Definition VFS.h:55
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10