add storage functions
This commit is contained in:
parent
04e44183a2
commit
447ef6ac65
1
.vscode/settings.json
vendored
1
.vscode/settings.json
vendored
@ -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,
|
||||
|
@ -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"
|
||||
}
|
@ -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",
|
||||
|
2
libs/core/enums.d.ts
vendored
2
libs/core/enums.d.ts
vendored
@ -564,5 +564,7 @@ declare namespace motors {
|
||||
}
|
||||
declare namespace serial {
|
||||
}
|
||||
declare namespace storage {
|
||||
}
|
||||
|
||||
// Auto-generated. Do not edit. Really.
|
||||
|
@ -64,6 +64,8 @@
|
||||
"sendbuffer.s",
|
||||
"sendbuffernrf52.s",
|
||||
"sendbufferbrightness.s",
|
||||
"storage.cpp",
|
||||
"storage.ts",
|
||||
"light.cpp",
|
||||
"compass.ts",
|
||||
"parts/speaker.svg",
|
||||
|
43
libs/core/shims.d.ts
vendored
43
libs/core/shims.d.ts
vendored
@ -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 {
|
||||
|
||||
/**
|
||||
|
56
libs/core/storage.cpp
Normal file
56
libs/core/storage.cpp
Normal file
@ -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));
|
||||
}
|
||||
|
||||
}
|
83
libs/core/storage.ts
Normal file
83
libs/core/storage.ts
Normal file
@ -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]);
|
||||
}
|
||||
|
||||
|
||||
}
|
20
sim/state/storage.ts
Normal file
20
sim/state/storage.ts
Normal file
@ -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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user