arduino-emulator
Loading...
Searching...
No Matches
SdFat.h
1/*
2 SDFat.h
3 Copyright (c) 2025 Phil Schatzmann. All right reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19#pragma once
20
21#include "Stream.h"
22#include <fstream>
23#include <iostream>
24#include <string>
25#include <sys/stat.h>
26#include <sys/types.h>
27#ifdef USE_FILESYSTEM
28#include <filesystem>
29#endif
30
31#define SD_SCK_MHZ(maxMhz) (1000000UL * (maxMhz))
32#define SPI_FULL_SPEED SD_SCK_MHZ(50)
33#define SPI_DIV3_SPEED SD_SCK_MHZ(16)
34#define SPI_HALF_SPEED SD_SCK_MHZ(4)
35#define SPI_DIV6_SPEED SD_SCK_MHZ(8)
36#define SPI_QUARTER_SPEED SD_SCK_MHZ(2)
37#define SPI_EIGHTH_SPEED SD_SCK_MHZ(1)
38#define SPI_SIXTEENTH_SPEED SD_SCK_HZ(500000)
39
40#define O_RDONLY ios::in
41#define O_WRONLY ios::out
42#define O_RDWR ios::in | ios::out
43#define O_AT_END ios::ate
44#define O_APPEND ios::ate
45#define O_CREAT ios::trunc
46#define O_TRUNC ios::trunc
47#define O_EXCL 0
48#define O_SYNC 0
49#define O_READ O_RDONLY
50#define O_WRITE O_WRONLY
51
52#ifndef SS
53#define SS 0
54#endif
55
56using namespace std;
57
63class SdSpiConfig {
64 SdSpiConfig(int a = 0, int b = 0, int c = 0) {}
65};
66
71class SdFat {
72public:
73 bool begin(int cs = SS, int speed = 0) { return true; }
74 bool begin(SdSpiConfig &cfg) { return true; }
75 void errorHalt(const char *msg) {
76 Serial.println(msg);
77 exit(0);
78 }
79 void initErrorHalt() { exit(0); }
80
81 bool exists(char *name) {
82 struct stat info;
83 return stat(name, &info) == 0;
84 }
85 void end(){}
86};
91class SdFile : public Stream {
92public:
93 bool isOpen() {
94 bool result = file.is_open();
95 return result;
96 }
97
98 bool open(const char *name, int flags) {
99 this->filename = name;
100 struct stat info;
101 int rc = stat(name, &info);
102 if ((flags == O_RDONLY) && rc == -1){
103 // if we want to read but the file does not exist we fail
104 return false;
105 } else if (rc == 0 && info.st_mode & S_IFDIR) {
106 // file exists and it is a directory
107 is_dir = true;
108 size = 0;
109#ifdef USE_FILESYSTEM
110 dir_path = std::filesystem::path(filename);
111 iterator = std::filesystem::directory_iterator({dir_path});
112#endif
113 return true;
114 } else {
115 is_dir = false;
116 size = rc!=-1 ? info.st_size: 0;
117 file.open(filename.c_str(), (std::ios_base::openmode)flags);
118 return isOpen();
119 }
120 }
121
122 bool close() {
123 file.close();
124 return !isOpen();
125 }
126
127 size_t readBytes(uint8_t *buffer, size_t length) {
128 return file.readsome((char *)buffer, length);
129 }
130 size_t write(const uint8_t *buffer, size_t size) override {
131 file.write((const char *)buffer, size);
132 return file.good() ? size : 0;
133 }
134 size_t write(uint8_t ch) override {
135 file.put(ch);
136 return file.good() ? 1 : 0;
137 }
138 int available() override { return size - file.tellg(); };
139 int availableForWrite() override { return 1024; }
140 int read() override { return file.get(); }
141 int peek() override { return file.peek(); }
142 void flush() override {}
143 void getName(char *str, size_t len) { strncpy(str, filename.c_str(), len); }
144 bool isDir() { return is_dir; }
145 bool isHidden() { return false; }
146 bool rewind() {
147 pos = 0;
148 return is_dir;
149 }
150 bool openNext(SdFile &dir, int flags = O_RDONLY) {
151 #ifdef USE_FILESYSTEM
152 if (dir.isDir() && dir.iterator != end(dir.iterator)) {
153 std::filesystem::directory_entry entry = *dir.iterator++;
154 return open(entry.path().c_str(), flags);
155 }
156 #endif
157 return false;
158 }
159 int dirIndex() { return pos; }
160
161protected:
162 std::fstream file;
163 size_t size = 0;
164 bool is_dir;
165 int pos = 0;
166 std::string filename;
167#ifdef USE_FILESYSTEM
168 std::filesystem::directory_iterator iterator;
169 std::filesystem::path dir_path;
170#endif
171};
C++ std based emulatoion ofr SdFat.
Definition SD.h:224
C++ std based emulatoion ofr SdFile.
Definition SdFat.h:91
C++ std based emulatoion of SdSpiConfig.
Definition SD.h:70