From 4795c58e97caceee3470b0df9a6f8422744268fe Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Sat, 16 Apr 2016 07:45:04 -0700 Subject: [PATCH] initial blocks for storage --- .gitignore | 1 + libs/microbit/enums.d.ts | 2 ++ libs/microbit/pxt.json | 3 ++- libs/microbit/shims.d.ts | 32 +++++++++++++++++++++++++++++ libs/microbit/storage.cpp | 42 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 libs/microbit/storage.cpp diff --git a/.gitignore b/.gitignore index 3f74229a..961ea959 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ temp projects win10/app/bin win10/app/bld +win10/*.opendb *.user *.sw? diff --git a/libs/microbit/enums.d.ts b/libs/microbit/enums.d.ts index 69ff593b..fadecb8f 100644 --- a/libs/microbit/enums.d.ts +++ b/libs/microbit/enums.d.ts @@ -281,5 +281,7 @@ declare namespace serial { Int32BE = 10, // UInt32, } +declare namespace storage { +} // Auto-generated. Do not edit. Really. diff --git a/libs/microbit/pxt.json b/libs/microbit/pxt.json index 081b2e1a..2b62cdc1 100644 --- a/libs/microbit/pxt.json +++ b/libs/microbit/pxt.json @@ -26,7 +26,8 @@ "pins.ts", "serial.cpp", "serial.ts", - "buffer.cpp" + "buffer.cpp", + "storage.cpp" ], "public": true, "dependencies": {}, diff --git a/libs/microbit/shims.d.ts b/libs/microbit/shims.d.ts index 40ebc796..5644ffae 100644 --- a/libs/microbit/shims.d.ts +++ b/libs/microbit/shims.d.ts @@ -585,4 +585,36 @@ declare interface Buffer { 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. diff --git a/libs/microbit/storage.cpp b/libs/microbit/storage.cpp new file mode 100644 index 00000000..0656d578 --- /dev/null +++ b/libs/microbit/storage.cpp @@ -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(); + } +} \ No newline at end of file