AwesomeStudioPedal
A programmable, multi-profile foot controller for DAWs, score readers, and studio automation
Loading...
Searching...
No Matches
littlefs_file_system.cpp
Go to the documentation of this file.
1#include "file_system.h"
2#ifndef HOST_TEST_BUILD
3#include <Arduino.h>
4#ifdef NRF52840_XXAA
5#include <Adafruit_LittleFS.h>
6#include <InternalFileSystem.h>
7using namespace Adafruit_LittleFS_Namespace;
8#define FS_INSTANCE InternalFS
9#define FS_BEGIN() InternalFS.begin()
10#define FS_OPEN_R(p) InternalFS.open(p, FILE_O_READ)
11#define FS_OPEN_W(p) InternalFS.open(p, FILE_O_WRITE)
12#else
13#include <LittleFS.h>
14#define FS_INSTANCE LittleFS
15#define FS_BEGIN() LittleFS.begin(true)
16#define FS_OPEN_R(p) LittleFS.open(p, "r")
17#define FS_OPEN_W(p) LittleFS.open(p, "w")
18#endif
19#else
20#include <fstream>
21#endif
22
28{
29public:
30 bool exists(const char* path) override
31 {
32#ifndef HOST_TEST_BUILD
33 return FS_INSTANCE.exists(path);
34#else
35 // Mock implementation for host testing
36 FILE* file = fopen(path, "r");
37 if (file)
38 {
39 fclose(file);
40 return true;
41 }
42 return false;
43#endif
44 }
45
46 bool readFile(const char* path, std::string& content) override
47 {
48#ifndef HOST_TEST_BUILD
49 if (! FS_BEGIN())
50 {
51 return false;
52 }
53
54 File file = FS_OPEN_R(path);
55 if (! file)
56 {
57 return false;
58 }
59
60 String arduinoContent = file.readString();
61 content = arduinoContent.c_str();
62 file.close();
63 return true;
64#else
65 // Host implementation using standard C++ files
66 std::ifstream inFile(path);
67 if (! inFile)
68 {
69 return false;
70 }
71
72 content =
73 std::string((std::istreambuf_iterator<char>(inFile)), std::istreambuf_iterator<char>());
74 return true;
75#endif
76 }
77
78 bool writeFile(const char* path, const std::string& content) override
79 {
80#ifndef HOST_TEST_BUILD
81 if (! FS_BEGIN())
82 {
83 return false;
84 }
85
86 File file = FS_OPEN_W(path);
87 if (! file)
88 {
89 return false;
90 }
91
92 if (file.print(content.c_str()) == 0)
93 {
94 file.close();
95 return false;
96 }
97
98 file.close();
99 return true;
100#else
101 // Host implementation using standard C++ files
102 std::ofstream outFile(path);
103 if (! outFile)
104 {
105 return false;
106 }
107
108 outFile << content;
109 return outFile.good();
110#endif
111 }
112};
113
114// Factory function to create the appropriate file system
116{
117 static LittleFSFileSystem instance;
118 return &instance;
119}
Abstract interface for file system operations.
Definition file_system.h:12
File system implementation using LittleFS for embedded targets.
bool exists(const char *path) override
Check if a file exists.
bool writeFile(const char *path, const std::string &content) override
Write content to a file.
bool readFile(const char *path, std::string &content) override
Read entire file content.
IFileSystem * createFileSystem()