Skip to content

Commit ef4ee91

Browse files
committed
Instantiating an Arduino_SPIFFS object within the module and changing the implementation of Arduino_SPIFFS_File in such way that the pointer to the spiffs structure is solely contained within the class Arduino_SPIFFS
1 parent 2637e09 commit ef4ee91

File tree

7 files changed

+95
-51
lines changed

7 files changed

+95
-51
lines changed

examples/SPIFFSDirectories/SPIFFSDirectories.ino

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@
1212

1313
#include <Arduino_MKRMEM.h>
1414

15-
/**************************************************************************************
16-
* GLOBAL VARIABLES
17-
**************************************************************************************/
18-
19-
Arduino_SPIFFS filesystem;
20-
2115
/**************************************************************************************
2216
* SETUP/LOOP
2317
**************************************************************************************/

examples/SPIFFSFormat/SPIFFSFormat.ino

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@
1212

1313
#include <Arduino_MKRMEM.h>
1414

15-
/**************************************************************************************
16-
* GLOBAL VARIABLES
17-
**************************************************************************************/
18-
19-
Arduino_SPIFFS filesystem;
20-
2115
/**************************************************************************************
2216
* SETUP/LOOP
2317
**************************************************************************************/

examples/SPIFFSUsage/SPIFFSUsage.ino

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@
1919
/* A pangram is a sentence using every letter of a given alphabet at least once. */
2020
static char const PANGRAM[] = "The quick brown fox jumps over the lazy dog.";
2121

22-
/**************************************************************************************
23-
* GLOBAL VARIABLES
24-
**************************************************************************************/
25-
26-
Arduino_SPIFFS filesystem;
27-
2822
/**************************************************************************************
2923
* SETUP/LOOP
3024
**************************************************************************************/
@@ -54,7 +48,7 @@ void setup() {
5448
* write only mode (SPIFFS_WRONLY ). If the file does exist
5549
* delete the existing content (SPIFFS_TRUNC).
5650
*/
57-
File file = filesystem.open("fox.txt", CREATE | WRITE_ONLY | TRUNCATE);
51+
File file = filesystem.open("fox.txt", CREATE | READ_WRITE| TRUNCATE);
5852

5953
int const bytes_to_write = strlen(PANGRAM);
6054
int const bytes_written = file.write((void *)PANGRAM, bytes_to_write);
@@ -66,8 +60,6 @@ void setup() {
6660
Serial.println(" bytes written");
6761
}
6862

69-
file.close();
70-
7163

7264
Serial.println("Retrieving filesystem info ...");
7365
unsigned int bytes_total = 0,
@@ -82,7 +74,7 @@ void setup() {
8274

8375

8476
Serial.println("Reading ...");
85-
file = filesystem.open("fox.txt", READ_ONLY);
77+
file.lseek(0, START); /* Rewind file pointer to the start */
8678

8779
char buf[64] = {0};
8880
int const bytes_read = file.read(buf, sizeof(buf));

src/Arduino_SPIFFS.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ int Arduino_SPIFFS::mount()
5757
File Arduino_SPIFFS::open(const char *path, uint16_t const flags)
5858
{
5959
spiffs_file const fh = SPIFFS_open(&_fs, path, flags, 0);
60-
return File::create(&_fs, fh);
60+
return File::create(fh);
6161
}
6262

6363
Directory Arduino_SPIFFS::opendir(const char *name)
@@ -88,3 +88,9 @@ s32_t w25q16_spi_erase(u32_t addr, u32_t size)
8888
flash.eraseSector(addr);
8989
return SPIFFS_OK;
9090
}
91+
92+
/**************************************************************************************
93+
* EXTERN DECLARATION
94+
**************************************************************************************/
95+
96+
Arduino_SPIFFS filesystem;

src/Arduino_SPIFFS.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ class Arduino_SPIFFS
8787
inline Directory opendir (String const & name) { return opendir(name.c_str()); }
8888

8989

90+
/* In order to allow access to the private functions
91+
* read, write, lseek, eof, tell, close, remove, flush
92+
* we allow Arduino_SPIFFS_File to be a 'friend' of
93+
* 'Arduino_SPIFFS'. Another alternative would be to
94+
* make those functions public but what would users do
95+
* without the availabilty of a file handle?
96+
*/
97+
friend class Arduino_SPIFFS_File;
98+
99+
90100
private:
91101

92102
spiffs _fs;
@@ -95,6 +105,22 @@ class Arduino_SPIFFS
95105
u8_t _spiffs_fds[32*4];
96106
u8_t _spiffs_cache_buf[(SPIFFS_CFG_LOG_PAGE_SZ(0)+32)*4];
97107

108+
109+
inline int read (spiffs_file fh, void * buf, int len) { return SPIFFS_read(&_fs, fh, buf, len); }
110+
inline int write (spiffs_file fh, void * buf, int len) { return SPIFFS_write(&_fs, fh, buf, len); }
111+
inline int lseek (spiffs_file fh, int offs, int whence) { return SPIFFS_lseek(&_fs, fh, offs, whence); }
112+
inline int eof (spiffs_file fh) { return SPIFFS_eof(&_fs, fh); }
113+
inline int tell (spiffs_file fh) { return SPIFFS_tell(&_fs, fh); }
114+
inline int close (spiffs_file fh) { return SPIFFS_close(&_fs, fh); }
115+
inline int remove(spiffs_file fh) { return SPIFFS_fremove(&_fs, fh); }
116+
inline int flush (spiffs_file fh) { return SPIFFS_fflush(&_fs, fh); }
117+
98118
};
99119

120+
/**************************************************************************************
121+
* EXTERN DEFINITION
122+
**************************************************************************************/
123+
124+
extern Arduino_SPIFFS filesystem;
125+
100126
#endif /* ARDUINO_SPIFFS_H_ */

src/Arduino_SPIFFS_File.cpp

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,20 @@
2222

2323
#include "Arduino_SPIFFS_File.h"
2424

25+
#include <Arduino_SPIFFS.h>
26+
27+
/**************************************************************************************
28+
* EXTERN DEFINITION
29+
**************************************************************************************/
30+
31+
extern Arduino_SPIFFS filesystem;
32+
2533
/**************************************************************************************
2634
* CTOR/DTOR
2735
**************************************************************************************/
2836

29-
Arduino_SPIFFS_File::Arduino_SPIFFS_File(spiffs * fs, spiffs_file const fh)
30-
: _fs(fs)
31-
, _fh(fh)
37+
Arduino_SPIFFS_File::Arduino_SPIFFS_File(spiffs_file const fh)
38+
: _fh(fh)
3239
{
3340

3441
}
@@ -42,23 +49,52 @@ Arduino_SPIFFS_File::~Arduino_SPIFFS_File()
4249
* PUBLIC MEMBER FUNCTIONS
4350
**************************************************************************************/
4451

45-
String Arduino_SPIFFS_File::name()
52+
int Arduino_SPIFFS_File::read(void * buf, int len)
53+
{
54+
return filesystem.read(_fh, buf, len);
55+
}
56+
57+
int Arduino_SPIFFS_File::write(void * buf, int len)
58+
{
59+
return filesystem.write(_fh, buf, len);
60+
}
61+
62+
int Arduino_SPIFFS_File::lseek(int offs, int whence)
63+
{
64+
return filesystem.lseek(_fh, offs, whence);
65+
}
66+
67+
int Arduino_SPIFFS_File::eof()
68+
{
69+
return filesystem.eof(_fh);
70+
}
71+
72+
int Arduino_SPIFFS_File::tell()
73+
{
74+
return filesystem.tell(_fh);
75+
}
76+
77+
int Arduino_SPIFFS_File::close()
78+
{
79+
return filesystem.close(_fh);
80+
}
81+
82+
int Arduino_SPIFFS_File::remove()
83+
{
84+
return filesystem.remove(_fh);
85+
}
86+
87+
int Arduino_SPIFFS_File::flush()
4688
{
47-
spiffs_stat stat;
48-
if(SPIFFS_OK == SPIFFS_fstat(_fs, _fh, &stat)) {
49-
return String(reinterpret_cast<const char *>(&(stat.name)));
50-
} else {
51-
return String("ERROR");
52-
}
89+
return filesystem.flush(_fh);
5390
}
5491

55-
Arduino_SPIFFS_File Arduino_SPIFFS_File::create(spiffs * fs, spiffs_file const fh)
92+
Arduino_SPIFFS_File Arduino_SPIFFS_File::create(spiffs_file const fh)
5693
{
57-
return Arduino_SPIFFS_File(fs, fh);
94+
return Arduino_SPIFFS_File(fh);
5895
}
5996

6097
Arduino_SPIFFS_File Arduino_SPIFFS_File::operator = (Arduino_SPIFFS_File const & other)
6198
{
62-
_fs = other.getFs();
6399
_fh = other.getFh();
64100
}

src/Arduino_SPIFFS_File.h

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extern "C"
3131
}
3232

3333
/**************************************************************************************
34-
* CLASS DECLARATION
34+
* CONSTANTS
3535
**************************************************************************************/
3636

3737
static int constexpr START = SPIFFS_SEEK_SET; /* Start to seek from the beginning of the file */
@@ -50,32 +50,28 @@ class Arduino_SPIFFS_File
5050
~Arduino_SPIFFS_File();
5151

5252

53-
String name ();
54-
inline int read (void * buf, int len) { return SPIFFS_read(_fs, _fh, buf, len); }
55-
inline int write (void * buf, int len) { return SPIFFS_write(_fs, _fh, buf, len); }
56-
inline int lseek (int offs, int whence) { return SPIFFS_lseek(_fs, _fh, offs, whence); }
57-
inline int eof () { return SPIFFS_eof(_fs, _fh); }
58-
inline int tell () { return SPIFFS_tell(_fs, _fh); }
59-
inline int close () { return SPIFFS_close(_fs, _fh); }
60-
inline int remove() { return SPIFFS_fremove(_fs, _fh); }
61-
inline int flush () { return SPIFFS_fflush(_fs, _fh); }
53+
int read (void * buf, int len);
54+
int write (void * buf, int len);
55+
int lseek (int offs, int whence);
56+
int eof ();
57+
int tell ();
58+
int close ();
59+
int remove();
60+
int flush ();
6261

6362

64-
static Arduino_SPIFFS_File create(spiffs * fs, spiffs_file const fh);
63+
static Arduino_SPIFFS_File create(spiffs_file const fh);
6564

6665

6766
Arduino_SPIFFS_File operator = (Arduino_SPIFFS_File const & other);
68-
inline spiffs * getFs() const { return _fs; }
6967
inline spiffs_file getFh() const { return _fh; }
7068

7169

7270
private:
7371

74-
spiffs * _fs;
7572
spiffs_file _fh;
7673

77-
Arduino_SPIFFS_File(spiffs * fs, spiffs_file const fh);
78-
74+
Arduino_SPIFFS_File(spiffs_file const fh);
7975

8076
};
8177

0 commit comments

Comments
 (0)