You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+139-37
Original file line number
Diff line number
Diff line change
@@ -47,7 +47,7 @@
47
47
48
48
# SPIFlashFileSystem 1.2.0
49
49
50
-
The SPIFlashFileSystem (SFFS) library implements a basic [wear leveling](https://en.wikipedia.org/wiki/Wear_leveling) file system intended for use with SPI Flash devices (using either the built-in [hardware.spiflash](https://electricimp.com/docs/api/hardware/spiflash) object on imp003+, or an external SPI Flash plus the [SPIFlash library](https://github.com/electricimp/spiflash) on the imp001 and imp002).
50
+
The SPIFlashFileSystem (SFFS) library implements a basic [wear leveling](https://en.wikipedia.org/wiki/Wear_leveling) file system intended for use with SPI Flash devices, using either the built-in [hardware.spiflash](https://electricimp.com/docs/api/hardware/spiflash) object on imp003 and above, or an external SPI Flash plus the [SPIFlash library](https://github.com/electricimp/spiflash) on the imp001 and imp002.
51
51
52
52
**To add this library to your project, add `#require "SPIFlashFileSystem.class.nut:1.2.0"` to the top of your device code.**
53
53
@@ -135,7 +135,7 @@ The SPIFlashFileSystem consists of three classes:
135
135
136
136
-[SPIFlashFileSystem](#spiflashfilesystem)— The main programming interface for the filesystem
137
137
-[SPIFlashFileSystem.File](#spiflashfilesystemfile)— An object representing an open file that you can use to read, write, etc
138
-
- SPIFlashFileSystem.FAT — The File Allocation Table (not used by application developer)
138
+
-[SPIFlashFileSystem.FAT](#spiflashfilesystemfat)— The File Allocation Table (not used by application developer)
139
139
140
140
## SPIFlashFileSystem
141
141
@@ -175,8 +175,6 @@ sffs.init();
175
175
The *init()* method initializes the FAT, and must be called before invoking other SPIFlashFileSystem methods. The *init()* method takes an optional callback method with one parameter, an array: a directory of files currently stored within the SPI flash.
176
176
177
177
```squirrel
178
-
#require "SPIFlashFileSystem.class.nut:1.2.0"
179
-
180
178
// Allocate the first 2 MB to the file system
181
179
sffs <- SPIFlashFileSystem(0x000000, 0x200000);
182
180
sffs.init(function(files) {
@@ -192,9 +190,9 @@ sffs.init(function(files) {
192
190
193
191
If the *init()* method is called while the filesystem has files open, a `SPIFlashFileSystem.ERR_OPEN_FILE` error will be thrown.
194
192
195
-
### getFileList()
193
+
### getFileList(*[orderByDate]*)
196
194
197
-
The *getFileList()* returns an array of file information identical to that passed into the *init()* callback:
195
+
The *getFileList()* returns an array of file information identical to that passed into the *init()* callback. It takes an optional parameter: orderByData, a Boolean value that specifies whether the returned files should be sorted into date order for you. The default value is `false`.
198
196
199
197
```squirrel
200
198
local files = sffs.getFileList();
@@ -230,6 +228,44 @@ local filename = "HelloWorld.txt";
This method returns information about the filesystem in the form of a table with the following keys:
234
+
235
+
| Key | Description |
236
+
| ----------- | --------------- |
237
+
|*size*| The filesystem size in bytes |
238
+
|*len*| The number of files in the filesystem |
239
+
|*start*| The address of the first byte of SPI flash assigned to the filesystem |
240
+
|*end*| The address of the last byte of SPI flash assigned to the filesystem |
241
+
|*pages*| The number of pages available in the filesystem |
242
+
243
+
```squirrel
244
+
local d = sffs.dimensions();
245
+
```
246
+
247
+
### created(*fileRef*)
248
+
249
+
Gets the creation timestamp for a specified file reference (id or file name).
250
+
251
+
```squirrel
252
+
// get creation date by file name
253
+
sffs.created("file.txt");
254
+
255
+
// get creation date by id
256
+
sffs.created(5);
257
+
```
258
+
259
+
### getFreeSpace()
260
+
261
+
This method returns an estimate of the free space available in the filesystem. Smaller files have more overhead than larger files so it is impossible to know exactly how much space is free. The information is returned as a table with two keys:
262
+
263
+
| Key | Description |
264
+
| ------------- | --------------- |
265
+
|*free*| The estimated free space in bytes |
266
+
|*freeable*| The estimated space in bytes that may be freed through further garbage collection |
267
+
268
+
233
269
### isFileOpen(*filename*)
234
270
235
271
Returns `true` or `false` according to whether or not the specified file is currently open.
If the *eraseFile* method is called while the specified file is open, a `SPIFlashFileSystem.ERR_OPEN_FILE` error will be thrown.
281
317
318
+
### eraseFiles()
319
+
320
+
This method erases all files within the filesystem. It will return with an error if it is called when there are open files.
321
+
282
322
### setAutoGc(*numPages*)
283
323
284
-
The *setAutoGc()* method sets the *autoGcThresgold* propertyThe default settings is 4. The garbage collector will automatically run when the filesystem has fewer than *autoGcThreshold* pages.
324
+
The *setAutoGc()* method sets the *autoGcThresgold* property. The default *autoGcThresgold* property is 4. The garbage collector will automatically run when the filesystem has fewer than *autoGcThreshold* pages.
285
325
286
326
Setting *numPages* to 0 will turn off automatic garbage collection.
287
327
@@ -297,39 +337,14 @@ The *gc()* method manually starts the garbage collection process. The SPIFlashFi
297
337
298
338
If the *numPages* parameter is specified, the garbage collector will free up to *numPages* pages and return when it completes (this is what happens when the garbage collector runs because the file system needs a page and none are free). If the *numPages* parameter is ommited, the garbage collector will run asynchronously in the background (this is what happens when the garbage collector runs because free pages drops below the value of *autoGcThreshold*).
299
339
300
-
### created(*fileRef*)
301
-
302
-
Gets the creation timestamp for a specified file reference (id or name).
303
-
304
-
```squirrel
305
-
// get creation date by name
306
-
sffs.created("file.txt");
307
-
308
-
// get creation date by id
309
-
sffs.created(5);
310
-
```
311
-
312
-
### dimensions()
313
-
314
-
Returns a table with the dimensions of the File System:
315
340
316
-
```squirrel
317
-
{
318
-
"start": /* First byte of SPIFlash allocated to file system */,
319
-
"size": /* The size of the SPI Flash */,
320
-
"end": /* Last byte of SPIFlash allocated to file system */,
321
-
"len": /* Size of File System */,
322
-
"pages": /* Number of pages available in the File system*/
323
-
}
324
-
```
341
+
## SPIFlashFileSystem.File
325
342
326
-
```squirrel
327
-
local d = sffs.dimensions();
328
-
```
343
+
A *SPIFlashFileSystem.File* object is returned from the SPIFlashFileSystem each time a file is opened. The *SPIFlashFileSystem.File* object acts as a stream, with an internal pointer which can be manipulated with a variety of methods in the *SPIFlashFileSystem.File* class. Typically, you will not need to instantiate SPIFlashFileSystem.File objects yourself.
A *SPIFlashFileSystem.File* object is returned from the SPIFlashFileSystem each time a file is opened. The *SPIFlashFileSystem.File* object acts as a stream, with an internal pointer which can be manipulated with a variety of methods in the *SPIFlashFileSystem.File* class.
347
+
The constructor creates a file record. It takes the current SPIFlashFileSystem instance, the file’s unique integer ID, the index of the file in the internal record of open files, the current file’s filename, and its access mode: read or write, represented as the strings `"r"` and `"w"`, respectively.
Writes a string or blob to the end of a file's data opened with mode `"w"`. If you attempt to write to a file opened with mode `"r"` a `SPIFlashFileSystem.ERR_WRITE_R_FILE` error will be thrown.
390
405
391
-
**Note** The page header is not written to the SPI Flash until the entire page is written, or the [close](#close) method is called.
406
+
**Note:** The page header is not written to the SPI Flash until the entire page is written, or the [close](#close) method is called.
392
407
393
408
In the following example, we download a file in chunks from the agent:
394
409
@@ -403,13 +418,100 @@ file.write("World!");
403
418
file.close();
404
419
```
405
420
421
+
### created()
422
+
423
+
The *created()* method returns the file’s creation timestamp.
424
+
406
425
### close()
407
426
408
427
The *close()* method closes a file, and writes data to the SPI Flash if required. All files that are opened should be closed, regardless of what mode they were opened in.
409
428
410
429
*See [write()](#writedata) for sample usage.*
411
430
412
431
432
+
## SPIFlashFileSystem.FAT
433
+
434
+
A SPIFlashFileSystem.FAT object is automatically generated when the filesystem is initialized. It records the file allocation table (FAT).
The constructor takes a master *SPIFlashFileSystem* object and, optionally, the number of 4KB pages it contains. If no value is passed into the pages parameter, the constructor will scan the filesystem for files and build an FAT from them.
439
+
440
+
## SPIFlashFileSystem.FAT Methods
441
+
442
+
### scan()
443
+
444
+
This method scans the filesystem for files and uses them to construct the file allocation table.
445
+
446
+
### get(*fileReference*)
447
+
448
+
This method retrieves information about a specific file from the file allocation table. The value passed into *fileReference* can be either a file’s name (a string) or its ID (an integer). The method returns a table of information containing the following keys:
449
+
450
+
| Key | Description |
451
+
| ----- | --------------- |
452
+
|*id*| The file’s ID (integer) |
453
+
|*fname*| The file’s filename |
454
+
|*spans*| ??? |
455
+
|*pages*| An array of pages in which the file is stored |
456
+
|*pageCount*| The number of pages in which the file is stored |
457
+
|*sizes*| An array of the size (in bytes) of the file chunks each page contains |
458
+
|*sizeTotal*| The total size of the file |
459
+
|*created*| A timestamp indicating the date and time of the file’s creation |
460
+
461
+
### getFileList(*[orderByDate]*)
462
+
463
+
The *getFileList()* returns an array of file information, one entry per file in the FAT. It takes an optional parameter: *orderByData*, a Boolean value that specifies whether the returned files should be sorted into date order for you. The default value is `false`. The information for each file is a table with the following keys:
464
+
465
+
| Key | Description |
466
+
| ----- | --------------- |
467
+
|*id*| The file’s ID (integer) |
468
+
|*fname*| The file’s filename |
469
+
|*size*| The total size of the file |
470
+
|*created*| A timestamp indicating the date and time of the file’s creation |
471
+
472
+
### getFileId(*filename*)
473
+
This method takes a given file’s name and returns the unique integer by which it is referenced in the file allocation table.
474
+
475
+
### fileExists(*fileReference*)
476
+
This method returns `true` or `false` according to whether or not the specified file exists in the file allocation table. The value passed into fileReference can be either a file’s name (a string) or its ID (an integer).
477
+
478
+
### getFreePage()
479
+
This method returns the address of a random free page in the filesystem. It will return an error, *SPIFlashFileSystem.ERR_NO_FREE_SPACE*, if it is unable to do so because there is no free space left.
480
+
481
+
### markPage(*address, status*)
482
+
This method sets the status of the page at address. Status values can be any of the constants: *SPIFLASHFILESYSTEM_STATUS_FREE, SPIFLASHFILESYSTEM_STATUS_USED*, *SPIFLASHFILESYSTEM_STATUS_ERASED or SPIFLASHFILESYSTEM_STATUS_BAD*.
483
+
484
+
### addPage(*fileId, page*)
485
+
This method adds the specified page to the file allocation table for the specified file.
486
+
487
+
### getPageCount(*fileReference*)
488
+
This method returns the number of pages the files specified by the parameter fileReference comprises. The value passed into fileReference can be either a file’s name (a string) or its ID (an integer).
489
+
490
+
### forEachPage(*fileReference, callback*)
491
+
This method iterates over each page used to record the file specified by the parameter fileReference. For each page, the function passed into the parameter callback is called. This function takes a single parameter: the page it is passed.
492
+
493
+
### pagesOrderedBySpan(*pages*)
494
+
This method returns an array of pages ordered by the span.
495
+
496
+
### addSizeToLastSpan(*fileId, bytes*)
497
+
This method updates the size of the last span in a file.
498
+
499
+
### set(*fileId, file*)
500
+
This method sets the span for the file specified by its ID.
501
+
502
+
### removeFile(*filename*)
503
+
This method removes the named file from the file allocation table.
504
+
505
+
### getSectorMap()
506
+
This method returns the file allocation table’s sector map.
507
+
508
+
### getStats()
509
+
This method returns a table of page usage information, specifically the number of pages in the filesystem in each of the four status categories. This information is returned as a table with the keys *free, used, erased and bad*. Each key’s value is an integer: the number of pages in the filesystem with that status.
510
+
511
+
### describe()
512
+
This method is intended to assist with debugging: it provides a readout of the current number of files in the file allocation table, and lists each file by name, the number of pages it spans and its total size.
513
+
514
+
413
515
## Testing
414
516
415
517
Repository contains [impUnit](https://github.com/electricimp/impUnit) tests and a configuration for [impTest](https://github.com/electricimp/impTest).
0 commit comments