forked from bsiever/microbit-pxt-flashstorage
/
flashstorage.cpp
54 lines (47 loc) · 1.24 KB
/
flashstorage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Bill Siever
* 2021-10-02 Initial Version
*
* Development environment specifics:
* Written in Microsoft PXT
*
* This code is released under the [MIT License](http://opensource.org/licenses/MIT).
* Please review the LICENSE.md file included with this example. If you have any questions
* or concerns with licensing, please contact techsupport@sparkfun.com.
* Distributed as-is; no warranty is given.
*/
#include "pxt.h"
#include "MicroBit.h"
using namespace pxt;
namespace flashstorage {
//%
String get(String key) {
if(!key) return PSTR("");
KeyValuePair *pair = uBit.storage.get(key->getUTF8Data());
if (pair == NULL) {
return PSTR("");
} else{
String s = PSTR((char*)pair->value);
delete pair;
return s;
}
}
//%
void remove(String key) {
if(!key) return;
uBit.storage.remove(key->getUTF8Data());
}
//%
int size() {
return uBit.storage.size();
}
//%
void put(String key, String value) {
if(!key) return;
if(!value) {
remove(key);
return;
}
uBit.storage.put(key->getUTF8Data(), (uint8_t*)value->getUTF8Data(), value->getUTF8Size());
}
}