Skip to content

Commit 4feb425

Browse files
LucCurclamas
Luc
authored andcommitted
Unify time modification on SD and SPIFFS (espressif#738)
* Add access to last write date time add example * rename cpp to ino orz * wrong copy -past * No comment orz * Add missing space
1 parent 8305f7c commit 4feb425

File tree

8 files changed

+620
-0
lines changed

8 files changed

+620
-0
lines changed

libraries/FS/src/FS.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ size_t File::write(uint8_t c)
3232
return _p->write(&c, 1);
3333
}
3434

35+
time_t File::getLastWrite()
36+
{
37+
if (!_p) {
38+
return 0;
39+
}
40+
41+
return _p->getLastWrite();
42+
}
43+
3544
size_t File::write(const uint8_t *buf, size_t size)
3645
{
3746
if (!_p) {

libraries/FS/src/FS.h

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class File : public Stream
7070
size_t size() const;
7171
void close();
7272
operator bool() const;
73+
time_t getLastWrite();
7374
const char* name() const;
7475

7576
boolean isDirectory(void);

libraries/FS/src/FSImpl.h

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class FileImpl
3737
virtual size_t position() const = 0;
3838
virtual size_t size() const = 0;
3939
virtual void close() = 0;
40+
virtual time_t getLastWrite() = 0;
4041
virtual const char* name() const = 0;
4142
virtual boolean isDirectory(void) = 0;
4243
virtual FileImplPtr openNextFile(const char* mode) = 0;

libraries/FS/src/vfs_api.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,11 @@ VFSFileImpl::operator bool()
295295
return (_isDirectory && _d != NULL) || _f != NULL;
296296
}
297297

298+
time_t VFSFileImpl::getLastWrite() {
299+
_getStat() ;
300+
return _stat.st_mtime;
301+
}
302+
298303
void VFSFileImpl::_getStat() const
299304
{
300305
if(!_path) {

libraries/FS/src/vfs_api.h

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class VFSFileImpl : public FileImpl
6767
size_t size() const override;
6868
void close() override;
6969
const char* name() const override;
70+
time_t getLastWrite() override;
7071
boolean isDirectory(void) override;
7172
FileImplPtr openNextFile(const char* mode) override;
7273
void rewindDirectory(void) override;
+213
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
/*
2+
* Connect the SD card to the following pins:
3+
*
4+
* SD Card | ESP32
5+
* D2 -
6+
* D3 SS
7+
* CMD MOSI
8+
* VSS GND
9+
* VDD 3.3V
10+
* CLK SCK
11+
* VSS GND
12+
* D0 MISO
13+
* D1 -
14+
*/
15+
16+
#include "FS.h"
17+
#include "SD.h"
18+
#include "SPI.h"
19+
#include <time.h>
20+
#include <WiFi.h>
21+
22+
const char* ssid = "your-ssid";
23+
const char* password = "your-password";
24+
25+
long timezone = 1;
26+
byte daysavetime = 1;
27+
28+
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
29+
Serial.printf("Listing directory: %s\n", dirname);
30+
31+
File root = fs.open(dirname);
32+
if(!root){
33+
Serial.println("Failed to open directory");
34+
return;
35+
}
36+
if(!root.isDirectory()){
37+
Serial.println("Not a directory");
38+
return;
39+
}
40+
41+
File file = root.openNextFile();
42+
while(file){
43+
if(file.isDirectory()){
44+
Serial.print(" DIR : ");
45+
Serial.print (file.name());
46+
time_t t= file.getLastWrite();
47+
struct tm * tmstruct = localtime(&t);
48+
Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n",(tmstruct->tm_year)+1900,( tmstruct->tm_mon)+1, tmstruct->tm_mday,tmstruct->tm_hour , tmstruct->tm_min, tmstruct->tm_sec);
49+
if(levels){
50+
listDir(fs, file.name(), levels -1);
51+
}
52+
} else {
53+
Serial.print(" FILE: ");
54+
Serial.print(file.name());
55+
Serial.print(" SIZE: ");
56+
Serial.print(file.size());
57+
time_t t= file.getLastWrite();
58+
struct tm * tmstruct = localtime(&t);
59+
Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n",(tmstruct->tm_year)+1900,( tmstruct->tm_mon)+1, tmstruct->tm_mday,tmstruct->tm_hour , tmstruct->tm_min, tmstruct->tm_sec);
60+
}
61+
file = root.openNextFile();
62+
}
63+
}
64+
65+
void createDir(fs::FS &fs, const char * path){
66+
Serial.printf("Creating Dir: %s\n", path);
67+
if(fs.mkdir(path)){
68+
Serial.println("Dir created");
69+
} else {
70+
Serial.println("mkdir failed");
71+
}
72+
}
73+
74+
void removeDir(fs::FS &fs, const char * path){
75+
Serial.printf("Removing Dir: %s\n", path);
76+
if(fs.rmdir(path)){
77+
Serial.println("Dir removed");
78+
} else {
79+
Serial.println("rmdir failed");
80+
}
81+
}
82+
83+
void readFile(fs::FS &fs, const char * path){
84+
Serial.printf("Reading file: %s\n", path);
85+
86+
File file = fs.open(path);
87+
if(!file){
88+
Serial.println("Failed to open file for reading");
89+
return;
90+
}
91+
92+
Serial.print("Read from file: ");
93+
while(file.available()){
94+
Serial.write(file.read());
95+
}
96+
file.close();
97+
}
98+
99+
void writeFile(fs::FS &fs, const char * path, const char * message){
100+
Serial.printf("Writing file: %s\n", path);
101+
102+
File file = fs.open(path, FILE_WRITE);
103+
if(!file){
104+
Serial.println("Failed to open file for writing");
105+
return;
106+
}
107+
if(file.print(message)){
108+
Serial.println("File written");
109+
} else {
110+
Serial.println("Write failed");
111+
}
112+
file.close();
113+
}
114+
115+
void appendFile(fs::FS &fs, const char * path, const char * message){
116+
Serial.printf("Appending to file: %s\n", path);
117+
118+
File file = fs.open(path, FILE_APPEND);
119+
if(!file){
120+
Serial.println("Failed to open file for appending");
121+
return;
122+
}
123+
if(file.print(message)){
124+
Serial.println("Message appended");
125+
} else {
126+
Serial.println("Append failed");
127+
}
128+
file.close();
129+
}
130+
131+
void renameFile(fs::FS &fs, const char * path1, const char * path2){
132+
Serial.printf("Renaming file %s to %s\n", path1, path2);
133+
if (fs.rename(path1, path2)) {
134+
Serial.println("File renamed");
135+
} else {
136+
Serial.println("Rename failed");
137+
}
138+
}
139+
140+
void deleteFile(fs::FS &fs, const char * path){
141+
Serial.printf("Deleting file: %s\n", path);
142+
if(fs.remove(path)){
143+
Serial.println("File deleted");
144+
} else {
145+
Serial.println("Delete failed");
146+
}
147+
}
148+
149+
void setup(){
150+
Serial.begin(115200);
151+
// We start by connecting to a WiFi network
152+
Serial.println();
153+
Serial.println();
154+
Serial.print("Connecting to ");
155+
Serial.println(ssid);
156+
157+
WiFi.begin(ssid, password);
158+
159+
while (WiFi.status() != WL_CONNECTED) {
160+
delay(500);
161+
Serial.print(".");
162+
}
163+
Serial.println("WiFi connected");
164+
Serial.println("IP address: ");
165+
Serial.println(WiFi.localIP());
166+
Serial.println("Contacting Time Server");
167+
configTime(3600*timezone, daysavetime*3600, "time.nist.gov", "0.pool.ntp.org", "1.pool.ntp.org");
168+
struct tm tmstruct ;
169+
delay(2000);
170+
tmstruct.tm_year = 0;
171+
getLocalTime(&tmstruct, 5000);
172+
Serial.printf("\nNow is : %d-%02d-%02d %02d:%02d:%02d\n",(tmstruct.tm_year)+1900,( tmstruct.tm_mon)+1, tmstruct.tm_mday,tmstruct.tm_hour , tmstruct.tm_min, tmstruct.tm_sec);
173+
Serial.println("");
174+
175+
if(!SD.begin()){
176+
Serial.println("Card Mount Failed");
177+
return;
178+
}
179+
uint8_t cardType = SD.cardType();
180+
181+
if(cardType == CARD_NONE){
182+
Serial.println("No SD card attached");
183+
return;
184+
}
185+
186+
Serial.print("SD Card Type: ");
187+
if(cardType == CARD_MMC){
188+
Serial.println("MMC");
189+
} else if(cardType == CARD_SD){
190+
Serial.println("SDSC");
191+
} else if(cardType == CARD_SDHC){
192+
Serial.println("SDHC");
193+
} else {
194+
Serial.println("UNKNOWN");
195+
}
196+
197+
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
198+
Serial.printf("SD Card Size: %lluMB\n", cardSize);
199+
200+
listDir(SD, "/", 0);
201+
removeDir(SD, "/mydir");
202+
createDir(SD, "/mydir");
203+
deleteFile(SD, "/hello.txt");
204+
writeFile(SD, "/hello.txt", "Hello ");
205+
appendFile(SD, "/hello.txt", "World!\n");
206+
listDir(SD, "/", 0);
207+
}
208+
209+
void loop(){
210+
211+
}
212+
213+

0 commit comments

Comments
 (0)