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