Skip to content

Commit b7a0efd

Browse files
committed
Simplify interface; inline FileSystemSwitcher
1 parent 3a2b088 commit b7a0efd

File tree

4 files changed

+76
-91
lines changed

4 files changed

+76
-91
lines changed

lib/storage/storage.cpp

Lines changed: 41 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -27,98 +27,54 @@ static constexpr std::uint16_t blockSize = 512; // Should be 512
2727
static const esp_partition_t *partition = nullptr;
2828
static const char *const TAG = "STORAGE";
2929

30-
/**
31-
* Lists files and directories at path.
32-
*/
33-
static void listFiles(const char *const dirname, const std::shared_ptr<fs::FS> fs)
30+
static std::condition_variable stateChangeRequested;
31+
static std::condition_variable stateChanged;
32+
static std::atomic<bool> requestFileSystemActive;
33+
static std::thread stateMachine;
34+
static std::mutex fileSystemState_mutex; //!< used to lock the actual state
35+
static bool fileSystemIsActive; //!< describes the current state
36+
37+
std::shared_ptr<fs::FS> Storage::getFileSystem_locking()
3438
{
35-
std::cout << "Directory: '" << dirname << "'" << std::endl;
36-
File root = fs->open(dirname);
37-
if (!root || !root.isDirectory())
38-
{
39-
ESP_LOGE(TAG, "Error: '%s' is not a directory!\n", dirname);
40-
return;
41-
}
42-
43-
File file = root.openNextFile();
44-
while (file)
39+
std::unique_lock fs_state_lock{fileSystemState_mutex};
40+
if (!fileSystemIsActive)
4541
{
46-
std::cout << "\t" << file.name() << " (" << (file.isDirectory() ? "d" : "f") << ", " << file.size() << " Bytes)"
47-
<< std::endl;
48-
file.close();
49-
file = root.openNextFile();
42+
stateChanged.wait(fs_state_lock, []() { return fileSystemIsActive; });
5043
}
51-
file.close();
52-
root.close();
44+
fs_state_lock.release();
45+
return {&FFat, [](fs::FS *) { fileSystemState_mutex.unlock(); }};
5346
}
5447

55-
class FileSystemSwitcher
48+
static void requestState(const bool fileSystemActive)
5649
{
57-
public:
58-
void begin(const bool fsIsActive)
59-
{
60-
fileSystemIsActive = fsIsActive;
61-
requestFileSystemActive = fsIsActive;
62-
stateMachine = std::thread(&FileSystemSwitcher::processStateRequests, this);
63-
}
50+
requestFileSystemActive = fileSystemActive;
51+
ESP_LOGD(TAG, "request new state: %s", fileSystemActive ? "true" : "false");
52+
stateChangeRequested.notify_all();
53+
}
6454

65-
std::shared_ptr<fs::FS> getFileSystem_locking()
55+
static void processStateRequests()
56+
{
57+
while (true)
6658
{
6759
std::unique_lock fs_state_lock{fileSystemState_mutex};
68-
if (!fileSystemIsActive)
60+
ESP_LOGD(TAG, "waiting for state change request");
61+
stateChangeRequested.wait(fs_state_lock, []() { return requestFileSystemActive != fileSystemIsActive; });
62+
if (fileSystemIsActive = requestFileSystemActive)
6963
{
70-
stateChanged.wait(fs_state_lock, [this]() { return fileSystemIsActive; });
64+
ESP_LOGI(TAG, "mount FS");
65+
usbMsc.mediaPresent(false);
66+
FFat.end(); // invalidate cache
67+
assert(FFat.begin()); // update data
7168
}
72-
fs_state_lock.release();
73-
return {&FFat, [this](fs::FS *) { fileSystemState_mutex.unlock(); }};
74-
}
75-
void requestState(const bool fileSystemActive)
76-
{
77-
requestFileSystemActive = fileSystemActive;
78-
ESP_LOGD(TAG, "request new state: %s", fileSystemActive ? "true" : "false");
79-
stateChangeRequested.notify_all();
80-
}
81-
82-
protected:
83-
bool isFileSystemActive() const
84-
{
85-
return fileSystemIsActive;
86-
}
87-
void processStateRequests()
88-
{
89-
while (true)
69+
else
9070
{
91-
std::unique_lock fs_state_lock{fileSystemState_mutex};
92-
ESP_LOGD(TAG, "waiting for state change request");
93-
stateChangeRequested.wait(fs_state_lock,
94-
[this]() { return requestFileSystemActive != fileSystemIsActive; });
95-
if (fileSystemIsActive = requestFileSystemActive)
96-
{
97-
ESP_LOGD(TAG, "mount FS");
98-
usbMsc.mediaPresent(false);
99-
FFat.end(); // invalidate cache
100-
assert(FFat.begin()); // update data
101-
}
102-
else
103-
{
104-
ESP_LOGD(TAG, "unmount FS");
105-
FFat.end(); // flush and unmount
106-
usbMsc.mediaPresent(true);
107-
}
108-
stateChanged.notify_all();
71+
ESP_LOGI(TAG, "unmount FS");
72+
FFat.end(); // flush and unmount
73+
usbMsc.mediaPresent(true);
10974
}
75+
stateChanged.notify_all();
11076
}
111-
112-
private:
113-
std::condition_variable stateChangeRequested;
114-
std::condition_variable stateChanged;
115-
std::atomic<bool> requestFileSystemActive;
116-
std::thread stateMachine;
117-
std::mutex fileSystemState_mutex; //!< used to lock the actual state
118-
bool fileSystemIsActive; //!< describes the current state
119-
};
120-
121-
static FileSystemSwitcher fileSystemSwitcher;
77+
}
12278

12379
/**
12480
* Callback invoked when received WRITE10 command.
@@ -174,12 +130,12 @@ static void usbStoppedCallback(void *, esp_event_base_t, int32_t, void *)
174130
return;
175131
}
176132
usbIsRunning = false;
177-
fileSystemSwitcher.requestState(true);
133+
requestState(true);
178134
}
179135
static void usbStartedCallback(void *, esp_event_base_t, int32_t, void *)
180136
{
181137
usbIsRunning = true;
182-
fileSystemSwitcher.requestState(false);
138+
requestState(false);
183139
}
184140

185141
void Storage::begin()
@@ -190,7 +146,7 @@ void Storage::begin()
190146
ESP_LOGE(TAG, "Failed to init files system, flash may not be formatted");
191147
return;
192148
}
193-
ESP_LOGI(TAG, "FatFS erfolgreich gemountet.");
149+
ESP_LOGI(TAG, "file system initialized");
194150

195151
partition = check_ffat_partition(FFAT_PARTITION_LABEL);
196152
if (!partition)
@@ -200,7 +156,10 @@ void Storage::begin()
200156
}
201157
ESP_LOGI(TAG, "Flash has a size of %u bytes\n", FFat.totalBytes());
202158

203-
fileSystemSwitcher.begin(true); // define state before callbacks are activated
159+
// define state before callbacks are activated
160+
fileSystemIsActive = true;
161+
requestFileSystemActive = true;
162+
stateMachine = std::thread(processStateRequests);
204163

205164
usbMsc.vendorID("ESP32"); // max 8 chars
206165
usbMsc.productID("USB_MSC"); // max 16 chars
@@ -230,9 +189,3 @@ void Storage::end()
230189
usbIsRunning = false;
231190
FFat.end();
232191
}
233-
234-
void Storage::test()
235-
{
236-
auto fs_p = fileSystemSwitcher.getFileSystem_locking();
237-
listFiles("/", fs_p);
238-
}

lib/storage/storage.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
#pragma once
22
#include <cstddef>
3+
#include <memory>
4+
5+
namespace fs
6+
{
7+
class FS;
8+
}
39

410
struct Storage
511
{
612
static void begin();
713
static void end();
8-
static void test();
14+
static std::shared_ptr<fs::FS> getFileSystem_locking();
915
static std::size_t size();
1016
};

platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ build_flags =
2121
-DARDUINO_USB_MSC_ON_BOOT=0
2222
-DARDUINO_USB_DFU_ON_BOOT=0
2323
-DCONFIG_ARDUHAL_LOG_COLORS=1
24-
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE
24+
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO
2525
-std=gnu++17

src/main.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <Arduino.h>
2+
#include <FS.h>
23
#include <chrono>
34
#include <esp32-hal-log.h>
45
#include <esp_err.h>
@@ -16,6 +17,31 @@ using namespace std::chrono_literals;
1617

1718
static const char *const TAG = "MAIN";
1819

20+
/**
21+
* Lists files and directories at path.
22+
*/
23+
static void listFiles(const char *const dirname, const std::shared_ptr<fs::FS> fs)
24+
{
25+
std::cout << "Directory: '" << dirname << "'" << std::endl;
26+
File root = fs->open(dirname);
27+
if (!root || !root.isDirectory())
28+
{
29+
ESP_LOGE(TAG, "Error: '%s' is not a directory!\n", dirname);
30+
return;
31+
}
32+
33+
File file = root.openNextFile();
34+
while (file)
35+
{
36+
std::cout << "\t" << file.name() << " (" << (file.isDirectory() ? "d" : "f") << ", " << file.size() << " Bytes)"
37+
<< std::endl;
38+
file.close();
39+
file = root.openNextFile();
40+
}
41+
file.close();
42+
root.close();
43+
}
44+
1945
void setup()
2046
{
2147
HWSerial.begin(115200);
@@ -32,6 +58,6 @@ void setup()
3258

3359
void loop()
3460
{
35-
Storage::test();
61+
listFiles("/", Storage::getFileSystem_locking());
3662
std::this_thread::sleep_for(3s);
3763
}

0 commit comments

Comments
 (0)