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
SPIFlashLogger operates on 4KB sectors and 256-byte chunks. Objects need not be aligned with chunks or sectors. Some necessary overhead is added to the beginning of each sector, as well as each serialized object (assuming you are using the standard [Serializer library](https://developer.electricimp.com/libraries/utilities/serializer)). The overhead includes:
14
16
@@ -21,7 +23,7 @@ SPIFlashLogger operates on 4KB sectors and 256-byte chunks. Objects need not be
SPIFlashLogger’s constructor takes four parameters, all of which are optional:
26
+
#### Parameters ####
25
27
26
28
| Parameter | Default Value | Description |
27
29
| --- | --- | --- |
@@ -30,6 +32,8 @@ SPIFlashLogger’s constructor takes four parameters, all of which are optional:
30
32
|*spiflash*|**hardware.spiflash**|**hardware.spiflash**, or an object with an equivalent interface such as the [SPIFlash](https://developer.electricimp.com/libraries/hardware/spiflash) library |
31
33
|*serializer*| Serializer class | The static [Serializer library](https://developer.electricimp.com/libraries/hardware/spiflash), or an object with an equivalent interface |
32
34
35
+
#### Example ####
36
+
33
37
```squirrel
34
38
// Initializing a SPIFlashLogger on an imp003+
35
39
#require "Serializer.class.nut:1.0.0"
@@ -78,21 +82,25 @@ This method writes any serializable object to the memory allocated for SPIFlashL
78
82
79
83
If the provided object can not be serialized, the exception is thrown by the underlying serializer class.
This method reads objects from the logger asynchronously. This mechanism is intended for the asynchronous processing of each log object, such as sending data to the agent and waiting for an acknowledgement.
95
101
102
+
#### Parameters ####
103
+
96
104
| Parameter | Data Type | Required? | Description |
97
105
| --- | --- | --- | --- |
98
106
|*onData*| Function | Yes | A callback which provides the object which has been read from the logger (see below) |
@@ -120,23 +128,27 @@ As a potential use case, one might log two versions of each message: a short, co
120
128
121
129
**Note** The logger does not erase objects when they are read, but each object can be erased in the *onData* callback by passing *address* to the *erase()* method.
122
130
131
+
#### Example ####
132
+
123
133
```squirrel
124
134
logger.read(
125
-
// For each object in the logs (onData)
126
-
function(dataPoint, address, next) {
127
-
// Send the dataPoint to the agent
128
-
server.log(format("Found object at SPI flash address %d", address))
129
-
agent.send("data", dataPoint);
130
-
// Erase it from the logger
131
-
logger.erase(address);
132
-
// Wait a little while for it to arrive
133
-
imp.wakeup(0.5, next);
134
-
},
135
-
136
-
// All finished (onFinish)(
137
-
function() {
138
-
server.log("Finished sending and all entries are erased")
139
-
}
135
+
// For each object in the logs (onData)
136
+
function(dataPoint, address, next) {
137
+
// Send the dataPoint to the agent
138
+
server.log(format("Found object at SPI flash address %d", address))
139
+
agent.send("data", dataPoint);
140
+
141
+
// Erase it from the logger
142
+
logger.erase(address);
143
+
144
+
// Wait a little while for it to arrive
145
+
imp.wakeup(0.5, next);
146
+
},
147
+
148
+
// All finished (onFinish)
149
+
function() {
150
+
server.log("Finished sending and all entries are erased")
151
+
}
140
152
);
141
153
```
142
154
@@ -152,12 +164,14 @@ This method reads objects from the logger synchronously, returning a single log
152
164
153
165
*readSync*() starts from the current logger position, which is equal to the current write position. Therefore `readSync(0)` could not contain any object, and `readSync(-1)` is equal to ‘step back to read the last written object’. If the value of *index* is greater than zero, the logger is looking for an object in a first populated sector right after the current logger position, or it will read the beginning of the sector at the current position if there are no more sectors with objects.
This method synchronously returns the first object written to the log that hasn’t yet been erased (ie. the oldest entry in flash). If there are no logs in the flash, it returns *default*, or `null` if no argument is passed into *default*.
181
195
196
+
#### Example ####
197
+
182
198
```squirrel
183
199
logger.write("This is the oldest");
184
200
logger.write("This is the newest");
@@ -189,6 +205,8 @@ assert(logger.first() == "This is the oldest");
189
205
190
206
This method synchronously returns the last object written to the log that hasn’t yet been erased (ie. the newest entry in flash). If there are no logs in the flash, it returns *default*, or `null` if no argument is passed into *default*.
@@ -214,18 +232,20 @@ See *setPosition()* for sample usage.
214
232
215
233
This method sets the current SPI flash pointer, ie. where SPIFlashLogger will perform the next read/write task. Setting the pointer can help optimize SPI flash memory usage between deep sleeps, as it allows SPIFlashLogger to be precise to one byte rather 256 bytes (the size of a chunk).
216
234
235
+
#### Example ####
236
+
217
237
```squirrel
218
238
// Create the logger object
219
239
logger <- SPIFlashLogger();
220
240
221
241
// Check if we have position information in the nv table:
222
242
if ("nv" in getroottable() && "position" in nv) {
223
-
// If we do, update the position pointers in the logger object
224
-
logger.setPosition(nv.position);
243
+
// If we do, update the position pointers in the logger object
244
+
logger.setPosition(nv.position);
225
245
} else {
226
-
// If we don't, grab the position points and set nv
227
-
local position = logger.getPosition();
228
-
nv <- { "position": position };
246
+
// If we don't, grab the position points and set nv
247
+
local position = logger.getPosition();
248
+
nv <- { "position": position };
229
249
}
230
250
231
251
// Get some data and log it
@@ -234,51 +254,55 @@ logger.write(data);
234
254
235
255
// Increment a counter
236
256
if (!("count" in nv)) {
237
-
nv.count <- 1;
257
+
nv.count <- 1;
238
258
} else {
239
-
nv.count++;
259
+
nv.count++;
240
260
}
241
261
242
262
// If we have more than 100 samples
243
263
if (nv.count > 100) {
244
-
// Send the samples to the agent
245
-
logger.read(
246
-
function(dataPoint, addr, next) {
247
-
// Send the dataPoint to the agent
248
-
agent.send("data", dataPoint);
249
-
// Erase it from the logger
250
-
logger.erase(addr);
251
-
// Wait a little while for it to arrive
252
-
imp.wakeup(0.5, next);
253
-
},
254
-
function() {
255
-
server.log("Finished sending and all entries are erased");
256
-
// Reset counter
257
-
nv.count <- 1;
258
-
// Go to sleep when done
259
-
imp.onidle(function() {
260
-
// Get and store position pointers for next run
261
-
local position = logger.getPosition();
262
-
nv.position <- position;
263
-
264
-
// Sleep for 1 minute
265
-
imp.deepsleepfor(60);
266
-
});
267
-
}
268
-
);
269
-
} else {
270
-
// Go to sleep
271
-
imp.onidle(function() {
264
+
// Send the samples to the agent
265
+
logger.read(
266
+
function(dataPoint, addr, next) {
267
+
// Send the dataPoint to the agent
268
+
agent.send("data", dataPoint);
269
+
270
+
// Erase it from the logger
271
+
logger.erase(addr);
272
+
273
+
// Wait a little while for it to arrive
274
+
imp.wakeup(0.5, next);
275
+
},
276
+
function() {
277
+
server.log("Finished sending and all entries are erased");
278
+
279
+
// Reset counter
280
+
nv.count <- 1;
281
+
282
+
// Go to sleep when done
283
+
imp.onidle(function() {
272
284
// Get and store position pointers for next run
273
285
local position = logger.getPosition();
274
286
nv.position <- position;
275
287
276
288
// Sleep for 1 minute
277
289
imp.deepsleepfor(60);
278
-
});
290
+
});
291
+
}
292
+
);
293
+
} else {
294
+
// Go to sleep
295
+
imp.onidle(function() {
296
+
// Get and store position pointers for next run
297
+
local position = logger.getPosition();
298
+
nv.position <- position;
299
+
300
+
// Sleep for 1 minute
301
+
imp.deepsleepfor(60);
302
+
});
279
303
}
280
304
```
281
305
282
-
# License
306
+
##License ##
283
307
284
308
The SPIFlashLogger library is licensed under [MIT License](https://github.com/electricimp/spiflashlogger/tree/master/LICENSE).
0 commit comments