roo_io
API Documentation for roo_io
Loading...
Searching...
No Matches
sdfs.cpp
Go to the documentation of this file.
1
2#ifdef ARDUINO
3
5
6#include <Arduino.h>
7
8#if (defined ESP32 || defined ROO_TESTING)
9// Directly use the lower-level POSIX APIs, bypassing Arduino filesystem stuff
10// completely.
12#else
13// Fall back to Arduino filesystem APIs.
15#endif
16
17namespace roo_io {
18
19#if (defined ESP32 || defined ROO_TESTING)
20
21ArduinoSdFs::ArduinoSdFs(uint8_t cs_pin, decltype(::SD)& sd,
22 decltype(::SPI)& spi, uint32_t freq)
25 sd_(sd),
26 spi_(&spi) {}
27
28MountImpl::MountResult ArduinoSdFs::mountImpl(
29 std::function<void()> unmount_fn) {
30 if (!sd_.begin(cs_pin_, *spi_, frequency(), mountPoint(), maxOpenFiles(),
31 formatIfMountFailed())) {
32 return MountImpl::MountError(kGenericMountError);
33 }
34 return MountImpl::Mounted(std::unique_ptr<MountImpl>(
35 new PosixMountImpl(mountPoint(), readOnly(), unmount_fn)));
36}
37
38Filesystem::MediaPresence ArduinoSdFs::checkMediaPresence() {
39 return sd_.totalBytes() > 0 ? kMediaPresent : kMediaAbsent;
40}
41
42#else
43
44ArduinoSdFs::ArduinoSdFs(uint8_t cs_pin)
45 : sd_(::SD),
46#if defined(ARDUINO_ARCH_RP2040)
47 sdfs_(::SDFS),
48#endif
49 cs_pin_(cs_pin),
50 read_only_(false) {
51}
52
53MountImpl::MountResult ArduinoSdFs::mountImpl(
54 std::function<void()> unmount_fn) {
55 if (!sd_.begin(cs_pin_)) {
56 return MountImpl::MountError(kGenericMountError);
57 }
58 return MountImpl::Mounted(std::unique_ptr<MountImpl>(
59#if defined(ARDUINO_ARCH_RP2040)
60 new ArduinoMountImpl(sdfs_, read_only_, unmount_fn)));
61#else
62 new ArduinoMountImpl(sd_, read_only_, unmount_fn)));
63#endif
64}
65
66Filesystem::MediaPresence ArduinoSdFs::checkMediaPresence() {
67 return kMediaPresenceUnknown;
68}
69
70#endif // ESP32
71
72void ArduinoSdFs::unmountImpl() { sd_.end(); }
73
74ArduinoSdFs CreateArduinoSdFs() { return ArduinoSdFs(); }
75
76ArduinoSdFs SD = CreateArduinoSdFs();
77
78} // namespace roo_io
79
80#endif // ARDUINO
Definition byte.h:6
roo::basic_string_view< CharT, Traits > basic_string_view
Definition string_view.h:8