From 447ef6ac6506c1cffa591d4c29f341be03b603d5 Mon Sep 17 00:00:00 2001 From: JW Date: Sun, 28 Nov 2021 00:38:36 +0100 Subject: [PATCH] add storage functions --- .vscode/settings.json | 1 - libs/core/_locales/core-jsdoc-strings.json | 19 ++++- libs/core/_locales/core-strings.json | 19 +++++ libs/core/enums.d.ts | 2 + libs/core/pxt.json | 2 + libs/core/shims.d.ts | 43 +++++++++++ libs/core/storage.cpp | 56 +++++++++++++++ libs/core/storage.ts | 83 ++++++++++++++++++++++ sim/state/storage.ts | 20 ++++++ 9 files changed, 243 insertions(+), 2 deletions(-) create mode 100644 libs/core/storage.cpp create mode 100644 libs/core/storage.ts create mode 100644 sim/state/storage.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index fc770a23..e0fa1cb2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,5 @@ // Place your settings in this file to overwrite default and user settings. { - "files.autoSave": "afterDelay", "files.watcherExclude": { "**/.git/objects/**": true, "**/built/**": true, diff --git a/libs/core/_locales/core-jsdoc-strings.json b/libs/core/_locales/core-jsdoc-strings.json index 9337e930..0a8e4b8f 100644 --- a/libs/core/_locales/core-jsdoc-strings.json +++ b/libs/core/_locales/core-jsdoc-strings.json @@ -687,5 +687,22 @@ "serial.writeString": "Send a piece of text through the serial connection.", "serial.writeValue": "Write a name:value pair as a line to the serial port.", "serial.writeValue|param|name": "name of the value stream, eg: x", - "serial.writeValue|param|value": "to write" + "serial.writeValue|param|value": "to write", + "storage": "Provides access to persistent storage functionality.\n\r\nProvides access to persistent storage functionality.", + "storage.getNumber": "Reads a key value pair from the non volatile storage as a number", + "storage.getNumber|param|key": "the key for accesing the value", + "storage.getString": "Reads a key value pair from the non volatile storage as a string", + "storage.getString|param|key": "the key for accesing the value", + "storage.getValue": "Reads a key value pair from the non volatile storage", + "storage.getValue|param|key": "the key for accesing the value", + "storage.putString": "Saves a key value pair in the non volatile storage", + "storage.putString|param|key": "the key for accesing the value", + "storage.putString|param|value": "value to store", + "storage.putValue": "Saves a key value pair in the non volatile storage", + "storage.putValue|param|key": "the key for accesing the value", + "storage.putValue|param|value": "value to store", + "storage.remove": "Reads a key value pair from the non volatile storage", + "storage.removeKey": "Deletes the key from the non volatile storage", + "storage.removeKey|param|key": "the key for accesing the value", + "storage.remove|param|key": "the key for accesing the value" } \ No newline at end of file diff --git a/libs/core/_locales/core-strings.json b/libs/core/_locales/core-strings.json index d094650b..f92eb5bd 100644 --- a/libs/core/_locales/core-strings.json +++ b/libs/core/_locales/core-strings.json @@ -254,6 +254,13 @@ "PulseValue.Low|block": "low", "Rotation.Pitch|block": "pitch", "Rotation.Roll|block": "roll", + "StorageSlots.s1|block": "Slot 1", + "StorageSlots.s2|block": "Slot 2", + "StorageSlots.s3|block": "Slot 3", + "StorageSlots.s4|block": "Slot 4", + "StorageSlots.s5|block": "Slot 5", + "StorageSlots.s6|block": "Slot 6", + "StorageSlots.s7|block": "Slot 7", "String.charAt|block": "char from %this=text|at %pos", "String.compare|block": "compare %this=text| to %that", "String.fromCharCode|block": "text from char code %code", @@ -440,6 +447,14 @@ "serial.writeString|block": "serial|write string %text", "serial.writeValue|block": "serial|write value %name|= %value", "serial|block": "serial", + "storage.getNumber|block": "read from %key as number", + "storage.getString|block": "read from %key", + "storage.getValue|block": "get number from %key", + "storage.putString|block": "Save into %key a value of %value", + "storage.putValue|block": "Put into %key a value of %value as number", + "storage.removeKey|block": "Clear %key", + "storage.remove|block": "remove %key", + "storage|block": "storage", "{id:category}AnalogInPin": "AnalogInPin", "{id:category}AnalogOutPin": "AnalogOutPin", "{id:category}Array": "Array", @@ -471,19 +486,23 @@ "{id:category}Pins": "Pins", "{id:category}PwmOnlyPin": "PwmOnlyPin", "{id:category}Serial": "Serial", + "{id:category}Storage": "Storage", "{id:category}String": "String", "{id:category}Text": "Text", "{id:category}_py": "_py", "{id:group}Configuration": "Configuration", "{id:group}Control": "Control", "{id:group}Events": "Events", + "{id:group}Get": "Get", "{id:group}LED matrix": "LED matrix", "{id:group}Melody": "Melody", "{id:group}Melody Advanced": "Melody Advanced", "{id:group}Modify": "Modify", "{id:group}Operations": "Operations", + "{id:group}Put": "Put", "{id:group}RGB LED": "RGB LED", "{id:group}Read": "Read", + "{id:group}Remove": "Remove", "{id:group}Sensors": "Sensors", "{id:group}Silence": "Silence", "{id:group}States": "States", diff --git a/libs/core/enums.d.ts b/libs/core/enums.d.ts index 3de6035e..6abb2ecb 100644 --- a/libs/core/enums.d.ts +++ b/libs/core/enums.d.ts @@ -564,5 +564,7 @@ declare namespace motors { } declare namespace serial { } +declare namespace storage { +} // Auto-generated. Do not edit. Really. diff --git a/libs/core/pxt.json b/libs/core/pxt.json index 27486cab..a173cacc 100644 --- a/libs/core/pxt.json +++ b/libs/core/pxt.json @@ -64,6 +64,8 @@ "sendbuffer.s", "sendbuffernrf52.s", "sendbufferbrightness.s", + "storage.cpp", + "storage.ts", "light.cpp", "compass.ts", "parts/speaker.svg", diff --git a/libs/core/shims.d.ts b/libs/core/shims.d.ts index f820968d..ff4a0af4 100644 --- a/libs/core/shims.d.ts +++ b/libs/core/shims.d.ts @@ -1308,6 +1308,49 @@ declare namespace control { //% deprecated=1 shim=control::createBufferFromUTF8 function createBufferFromUTF8(str: string): Buffer; } + + + /** + * Provides access to persistent storage functionality. + */ + +declare namespace storage { + + /** + * Saves a key value pair in the non volatile storage + * @param key the key for accesing the value + * @param value value to store + */ + //% weight=100 blockGap=16 + //% block="Put into %key a value of %value as number" + //% blockId=storage_put_value + //% + //% group="Put" + //% blockHidden=true value.defl=0 shim=storage::putValue + function putValue(key: string, value?: string): void; + + /** + * Reads a key value pair from the non volatile storage + * @param key the key for accesing the value + */ + //% weight=100 blockGap=16 + //% block="get number from %key" + //% blockId=storage_get_value + //% group="Get" + //% blockHidden=true shim=storage::getValue + function getValue(key: string): string; + + /** + * Reads a key value pair from the non volatile storage + * @param key the key for accesing the value + */ + //% weight=100 blockGap=16 + //% block="remove %key" + //% blockId=storage_remove + //% group="Remove" + //% blockHidden=true shim=storage::remove + function remove(key: string): void; +} declare namespace light { /** diff --git a/libs/core/storage.cpp b/libs/core/storage.cpp new file mode 100644 index 00000000..a1cb7112 --- /dev/null +++ b/libs/core/storage.cpp @@ -0,0 +1,56 @@ +#include "pxt.h" + +/** + * Provides access to persistent storage functionality. + */ +namespace storage { + /** + * Saves a key value pair in the non volatile storage + * @param key the key for accesing the value + * @param value value to store + */ + //% weight=100 blockGap=16 + //% block="Put into %key a value of %value as number" + //% blockId=storage_put_value + //% value.defl=0 + //% group="Put" + //% blockHidden=true + void putValue(String key, String value) { + uBit.storage.put(MSTR(key), (uint8_t *)&value, sizeof(int)); + } + + /** + * Reads a key value pair from the non volatile storage + * @param key the key for accesing the value + */ + //% weight=100 blockGap=16 + //% block="get number from %key" + //% blockId=storage_get_value + //% group="Get" + //% blockHidden=true + String getValue(String key) { + KeyValuePair* data = uBit.storage.get(MSTR(key)); + String stored; + if(data == NULL) { + return mkString("", -1); + } else { + memcpy(&stored, data->value, sizeof(int)); + delete data; + return stored; + } + } + + /** + * Reads a key value pair from the non volatile storage + * @param key the key for accesing the value + */ + //% weight=100 blockGap=16 + //% block="remove %key" + //% blockId=storage_remove + //% group="Remove" + //% blockHidden=true + void remove(String key) { + uBit.storage.remove(MSTR(key)); + } + +} \ No newline at end of file diff --git a/libs/core/storage.ts b/libs/core/storage.ts new file mode 100644 index 00000000..e85c4c9c --- /dev/null +++ b/libs/core/storage.ts @@ -0,0 +1,83 @@ +enum StorageSlots { + //% block="Slot 1" + s1 = 0, + //% block="Slot 2" + s2 = 1, + //% block="Slot 3" + s3 = 2, + //% block="Slot 4" + s4 = 3, + //% block="Slot 5" + s5 = 4, + //% block="Slot 6" + s6 = 5, + //% block="Slot 7" + s7 = 6, +} + +let storages = ['s1', 's2', 's3', 's4', 's5', 's6', 's7', 's8', 's9', 's10', 's11', 's12']; + +/** + * Provides access to persistent storage functionality. + */ +//% color=#FFBB00 weight=10 icon="\uf187" +//% advanced=true +namespace storage { + + /** + * Saves a key value pair in the non volatile storage + * @param key the key for accesing the value + * @param value value to store + */ + //% weight=100 blockGap=16 + //% block="Save into %key a value of %value" + //% blockId=storage_put_string + //% value.shadowOptions.toString=true + //% group="Put" + export function putString(key: StorageSlots, value: string) : void { + putValue(storages[key], value); + } + + /** + * Reads a key value pair from the non volatile storage as a number + * @param key the key for accesing the value + */ + //% weight=100 blockGap=16 + //% block="read from %key as number" + //% blockId=storage_get_number + //% group="Get" + export function getNumber(key: StorageSlots) : number { + let value = getValue(storages[key]); + if(value === '') { + return 0; + } else { + return parseFloat(value); + } + } + + /** + * Reads a key value pair from the non volatile storage as a string + * @param key the key for accesing the value + */ + //% weight=100 blockGap=16 + //% block="read from %key" + //% blockId=storage_get_string + //% group="Get" + export function getString(key: StorageSlots) : string { + return getValue(storages[key]); + } + + /** + * Deletes the key from the non volatile storage + * @param key the key for accesing the value + */ + //% weight=100 blockGap=16 + //% block="Clear %key" + //% blockId=storage_remove_key + //% group="Remove" + export function removeKey(key: StorageSlots) : void { + remove(storages[key]); + } + + +} \ No newline at end of file diff --git a/sim/state/storage.ts b/sim/state/storage.ts new file mode 100644 index 00000000..52cc5d63 --- /dev/null +++ b/sim/state/storage.ts @@ -0,0 +1,20 @@ + +namespace pxsim.storage { + + export function putValue(key: string, value: string) : void { + sessionStorage.setItem('simulatorValue_'+key, value); + } + + export function getValue(key: string) : string { + if(sessionStorage.getItem('simulatorValue_'+key)) { + return sessionStorage.getItem('simulatorValue_'+key); + } else { + return ""; + } + } + + export function remove(key: string) : void { + sessionStorage.removeItem('simulatorValue_'+key); + } + +} \ No newline at end of file