Skip to content

Commit 50cfe3a

Browse files
committed
WIP: Design non-static storage class
1 parent 50a0912 commit 50cfe3a

2 files changed

Lines changed: 57 additions & 3 deletions

File tree

lib/storage/storage.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "storage.hpp"
2+
#include <iostream>
3+
4+
const esp_partition_t *check_ffat_partition(const char *label); // defined in FFat.cpp
5+
6+
Storage::Storage(const bool formatFsOnFail, const char *const partitionLabel, ESPUSB &usb, fs::F_Fat &fs, const std::size_t blockSize):
7+
partition(check_ffat_partition(partitionLabel)),
8+
blockSize(blockSize),
9+
fs(fs),
10+
usb(usb)
11+
{
12+
if(!partition){
13+
std::cerr << "Error with partition!" << std::endl;
14+
return;
15+
}
16+
17+
// initialize file system
18+
const auto basePath = rootPath + partitionLabel;
19+
constexpr auto maxOpenFiles = 10;
20+
if(!fs.begin(formatFsOnFail, basePath.c_str(), maxOpenFiles, partitionLabel)){
21+
std::cerr << "File-system initialization failed!" << std::endl;
22+
return;
23+
}
24+
std::cout << "Storage has a size of " << size() << " bytes." << std::endl;
25+
26+
// subscribe to USB events
27+
callbackSubscribers.push_back(this);
28+
usb.onEvent(ARDUINO_USB_STARTED_EVENT, callbackUsbStarted_s);
29+
usb.onEvent(ARDUINO_USB_STOPPED_EVENT, callbackUsbStopped_s);
30+
}
31+
32+
std::size_t Storage::size() const
33+
{
34+
return fs.totalBytes();
35+
}

lib/storage/storage.hpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
#pragma once
22
#include <FFat.h>
3+
#include <USB.h>
34
#include <USBMSC.h>
45
#include <cstddef>
56
#include <esp_partition.h>
7+
#include <optional>
8+
#include <string>
9+
#include <vector>
610

711
class Storage
812
{
913
public:
10-
void begin(const char *partitionLabel = FFAT_PARTITION_LABEL);
11-
void end();
14+
Storage(bool formatFsOnFail = false, const char *partitionLabel = FFAT_PARTITION_LABEL, ESPUSB &usb = USB,
15+
fs::F_Fat &fs = FFat, std::size_t blockSize = 512);
1216
bool isFileSystemReady() const;
1317
void setCallbackFsReady();
1418
void setCallbackFsBusy();
19+
std::size_t size() const;
20+
std::string getFileContents(const char *path) const;
21+
~Storage();
22+
23+
static constexpr std::string rootPath = "/";
24+
25+
protected:
26+
fs::FS &getFileSystem();
1527

1628
private:
1729
const esp_partition_t *partition;
@@ -20,4 +32,11 @@ class Storage
2032
*/
2133
USBMSC usbMsc;
2234
const std::size_t blockSize;
23-
};
35+
fs::F_Fat &fs;
36+
ESPUSB &usb;
37+
static std::vector<Storage *> callbackSubscribers;
38+
static esp_event_handler_t callbackUsbStarted_s;
39+
void callbackUsbStarted();
40+
static esp_event_handler_t callbackUsbStopped_s;
41+
void callbackUsbStopped();
42+
};

0 commit comments

Comments
 (0)