initial blocks for storage

This commit is contained in:
Peli de Halleux 2016-04-16 07:45:04 -07:00
parent 8fb96934ca
commit 4795c58e97
5 changed files with 79 additions and 1 deletions

1
.gitignore vendored
View File

@ -6,6 +6,7 @@ temp
projects projects
win10/app/bin win10/app/bin
win10/app/bld win10/app/bld
win10/*.opendb
*.user *.user
*.sw? *.sw?

View File

@ -281,5 +281,7 @@ declare namespace serial {
Int32BE = 10, Int32BE = 10,
// UInt32, // UInt32,
} }
declare namespace storage {
}
// Auto-generated. Do not edit. Really. // Auto-generated. Do not edit. Really.

View File

@ -26,7 +26,8 @@
"pins.ts", "pins.ts",
"serial.cpp", "serial.cpp",
"serial.ts", "serial.ts",
"buffer.cpp" "buffer.cpp",
"storage.cpp"
], ],
"public": true, "public": true,
"dependencies": {}, "dependencies": {},

View File

@ -585,4 +585,36 @@ declare interface Buffer {
write(dstOffset: number, src: Buffer): void; write(dstOffset: number, src: Buffer): void;
} }
/**
* Allows to save and read values in the flash storage
*/
//% weight=10 color=#cc6600
declare namespace storage {
/**
* Writes the key and buffer pair into flash.
*/
//% blockId="storage_put_buffer" block="storage put buffer %key|with %buffer" weight=50 shim=storage::putBuffer
function putBuffer(key: string, buffer: Buffer): void;
/**
* Gets the buffer at the given key if any. If no key is available, empty buffer is returned.
*/
//% blockId="storage_get_buffer" block="storage get buffer %key" weight=49 shim=storage::getBuffer
function getBuffer(key: string): Buffer;
/**
* Removes an entry identified by the key.
*/
//% blockId="storage_remove" block="storage remove %key" weight=20 shim=storage::remove
function remove(key: string): void;
/**
* The number of entries in the key value store
*/
//% blockId="storage_size" block="storage size" weight=10 shim=storage::size
function size(): number;
}
// Auto-generated. Do not edit. Really. // Auto-generated. Do not edit. Really.

42
libs/microbit/storage.cpp Normal file
View File

@ -0,0 +1,42 @@
#include "ksbit.h"
/**
* Allows to save and read values in the flash storage
*/
//% weight=10 color=#cc6600
namespace storage {
/**
* Writes the key and buffer pair into flash.
*/
//% blockId="storage_put_buffer" block="storage put buffer %key|with %buffer" weight=50
void putBuffer(StringData* key, Buffer buffer) {
uBit.storage.put(ManagedString(key), ManagedBuffer(buffer).getBytes());
}
/**
* Gets the buffer at the given key if any. If no key is available, empty buffer is returned.
*/
//% blockId="storage_get_buffer" block="storage get buffer %key" weight=49
Buffer getBuffer(StringData* key) {
KeyValuePair* pv = uBit.storage.get(ManagedString(key));
if (pv == NULL) return ManagedBuffer().leakData();
return ManagedBuffer(pv->value, sizeof(pv->value)).leakData();
}
/**
* Removes an entry identified by the key.
*/
//% blockId="storage_remove" block="storage remove %key" weight=20
void remove(StringData * key) {
uBit.storage.remove(ManagedString(key));
}
/**
* The number of entries in the key value store
*/
//% blockId="storage_size" block="storage size" weight=10
int size() {
return uBit.storage.size();
}
}