Merge branch 'feat_storage' into V4

This commit is contained in:
JW 2021-11-28 03:54:09 +01:00
commit e6b7d55add
7 changed files with 147 additions and 430 deletions

View File

@ -266,9 +266,6 @@
"basic.showAnimation": "Shows a sequence of LED screens as an animation.",
"basic.showAnimation|param|interval": "time in milliseconds between each redraw",
"basic.showAnimation|param|leds": "pattern of LEDs to turn on/off",
"basic.showArrow": "Draws an arrow on the LED screen",
"basic.showArrow|param|direction": "the direction of the arrow",
"basic.showArrow|param|interval": "the amount of time (milliseconds) to show the icon. Default is 600.",
"basic.showCompass": "Draws needle on the screen which always points to north",
"basic.showCompass|param|interval": "the amount of time (milliseconds) to show the needle. Default is 600.",
"basic.showIcon": "Draws the selected icon on the LED screen",
@ -694,15 +691,25 @@
"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.getValueInt": "Reads a key value pair from the non volatile storage",
"storage.getValueInt|param|key": "the key for accesing the value",
"storage.getValue|param|key": "the key for accesing the value",
"storage.putNumber": "Saves a key value pair in the non volatile storage",
"storage.putNumber|param|key": "the key for accesing the value",
"storage.putNumber|param|value": "value to store",
"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.putValueInt": "Saves a key value pair in the non volatile storage",
"storage.putValueInt|param|key": "the key for accesing the value",
"storage.putValueInt|param|value": "value to store",
"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.removeKeyInt": "Deletes the key from the non volatile storage",
"storage.removeKeyInt|param|key": "the key for accesing the value",
"storage.removeKeyStr": "Deletes the key from the non volatile storage",
"storage.removeKeyStr|param|key": "the key for accesing the value",
"storage.remove|param|key": "the key for accesing the value"
}

View File

@ -22,14 +22,6 @@
"Array.shift|block": "get and remove first value from %list",
"Array.unshift|block": "%list| insert %value| at beginning",
"Array|block": "Array",
"ArrowNames.East|block": "East",
"ArrowNames.NorthEast|block": "North East",
"ArrowNames.NorthWest|block": "North West",
"ArrowNames.North|block": "North",
"ArrowNames.SouthEast|block": "South East",
"ArrowNames.SouthWest|block": "South West",
"ArrowNames.South|block": "South",
"ArrowNames.West|block": "West",
"BaudRate.BaudRate115200|block": "115200",
"BaudRate.BaudRate1200|block": "1200",
"BaudRate.BaudRate14400|block": "14400",
@ -117,43 +109,16 @@
"IconNames.ArrowSouth|block": "arrow south",
"IconNames.ArrowWest|block": "arrow west",
"IconNames.Asleep|block": "asleep",
"IconNames.Butterfly|block": "butterfly",
"IconNames.Chessboard|block": "chess board",
"IconNames.Confused|block": "confused",
"IconNames.Cow|block": "cow",
"IconNames.Diamond|block": "diamond",
"IconNames.Duck|block": "duck",
"IconNames.EigthNote|block": "eigth note",
"IconNames.Fabulous|block": "fabulous",
"IconNames.Ghost|block": "ghost",
"IconNames.Giraffe|block": "giraffe",
"IconNames.Happy|block": "happy",
"IconNames.Heart|block": "heart",
"IconNames.House|block": "house",
"IconNames.LeftTriangle|block": "left triangle",
"IconNames.Meh|block": "meh",
"IconNames.No|block": "no",
"IconNames.Pitchfork|block": "pitchfork",
"IconNames.QuarterNote|block": "quarter note",
"IconNames.Rabbit|block": "rabbit",
"IconNames.Rollerskate|block": "roller skate",
"IconNames.Sad|block": "sad",
"IconNames.Scissors|block": "scissors",
"IconNames.Silly|block": "silly",
"IconNames.Skull|block": "skull",
"IconNames.SmallDiamond|block": "small diamond",
"IconNames.SmallHeart|block": "small heart",
"IconNames.SmallSquare|block": "small square",
"IconNames.Snake|block": "snake",
"IconNames.Square|block": "square",
"IconNames.StickFigure|block": "stick figure",
"IconNames.Surprised|block": "surprised",
"IconNames.Sword|block": "sword",
"IconNames.TShirt|block": "t-shirt",
"IconNames.Target|block": "target",
"IconNames.Tortoise|block": "tortoise",
"IconNames.Triangle|block": "triangle",
"IconNames.Umbrella|block": "umbrella",
"IconNames.Yes|block": "yes",
"Image.scrollImage|block": "scroll image %sprite(myImage)|with offset %frameoffset|and interval (ms) %interval",
"Image.showImage|block": "show image %sprite(myImage)|at offset %offset ||and interval (ms) %interval",
@ -279,7 +244,6 @@
"basic.rgbw|block": "red %red|green %green|blue %blue|white %white",
"basic.rgb|block": "red %red|green %green|blue %blue",
"basic.setLedColor|block": "set led to %color=colorNumberPicker",
"basic.showArrow|block": "show arrow %i=device_arrow",
"basic.showCompass|block": "show compass needle for $interval|ms",
"basic.showIcon|block": "show icon %i || for %interval ms",
"basic.showLeds|block": "show leds",
@ -328,8 +292,6 @@
"game.setScore|block": "set score %points",
"game.startCountdown|block": "start countdown|(ms) %duration",
"game|block": "game",
"images.arrowImage|block": "arrow image %i",
"images.arrowNumber|block": "%arrow",
"images.createBigImage|block": "create big image",
"images.createImage|block": "create image",
"images.iconImage|block": "icon image %i",
@ -447,12 +409,16 @@
"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.getNumber|block": "read from number %key",
"storage.getString|block": "read from string %key",
"storage.getValueInt|block": "get number 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.putNumber|block": "Save into number %key a value of %value",
"storage.putString|block": "Save into string %key a value of %value",
"storage.putValueInt|block": "Put into %key a value of %value as Int",
"storage.putValue|block": "Put into %key a value of %value as String",
"storage.removeKeyInt|block": "Clear number %key",
"storage.removeKeyStr|block": "Clear string %key",
"storage.remove|block": "remove %key",
"storage|block": "storage",
"{id:category}AnalogInPin": "AnalogInPin",

View File

@ -65,87 +65,6 @@ enum IconNames {
//% block="meh"
//% jres=icons.meh
Meh,
//% block="t-shirt"
//% jres=icons.tshirt
TShirt,
//% block="roller skate"
//% jres=icons.rollerskate
Rollerskate,
//% block="duck"
//% jres=icons.duck
Duck,
//% block="house"
//% jres=icons.house
House,
//% block="tortoise"
//% jres=icons.tortoise
Tortoise,
//% block="butterfly"
//% jres=icons.butterfly
Butterfly,
//% block="stick figure"
//% jres=icons.stickfigure
StickFigure,
//% block="ghost"
//% jres=icons.ghost
Ghost,
//% block="sword"
//% jres=icons.sword
Sword,
//% block="giraffe"
//% jres=icons.giraffe
Giraffe,
//% block="skull"
//% jres=icons.skull
Skull,
//% block="umbrella"
//% jres=icons.umbrella
Umbrella,
//% block="snake"
//% jres=icons.snake
Snake,
//% block="rabbit"
//% jres=icons.rabbit
Rabbit,
//% block="cow"
//% jres=icons.cow
Cow,
//% block="quarter note"
//% jres=icons.quarternote
QuarterNote,
//% block="eigth note"
//% jres=icons.eigthnote
EigthNote,
//% block="pitchfork"
//% jres=icons.pitchfork
Pitchfork,
//% block="target"
//% jres=icons.target
Target,
//% block="triangle"
//% jres=icons.triangle
Triangle,
//% block="left triangle"
//% jres=icons.lefttriangle
LeftTriangle,
//% block="chess board"
//% jres=icons.chessboard
Chessboard,
//% block="diamond"
//% jres=icons.diamond
Diamond,
//% block="small diamond"
//% jres=icons.smalldiamond
SmallDiamond,
//% block="square"
//% jres=icons.square
Square,
//% block="small square"
//% jres=icons.smallsquare
SmallSquare,
//% block="scissors"
//% jres=icons.scissors
Scissors,
//% block="arrow north"
//% jres=icons.arrownorth
ArrowNorth,
@ -172,25 +91,6 @@ enum IconNames {
ArrowNorthWest
}
enum ArrowNames {
//% blockIdentity=images.arrowNumber block="North"
North = 0,
//% blockIdentity=images.arrowNumber block="North East"
NorthEast,
//% blockIdentity=images.arrowNumber block="East"
East,
//% blockIdentity=images.arrowNumber block="South East"
SouthEast,
//% blockIdentity=images.arrowNumber block="South"
South,
//% blockIdentity=images.arrowNumber block="South West"
SouthWest,
//% blockIdentity=images.arrowNumber block="West"
West,
//% blockIdentity=images.arrowNumber block="North West"
NorthWest,
}
namespace basic {
@ -216,94 +116,11 @@ namespace basic {
res.showImage(0, interval)
}
/**
* Draws an arrow on the LED screen
* @param direction the direction of the arrow
* @param interval the amount of time (milliseconds) to show the icon. Default is 600.
*/
//% weight=50 blockGap=8
//% blockId=basic_show_arrow
//% block="show arrow %i=device_arrow"
//% parts="ledmatrix"
//% help=basic/show-arrow
//% deprecated
//% group="LED matrix"
export function showArrow(direction: number, interval = 600) {
let res = images.arrowImage(direction)
res.showImage(0, interval)
}
}
namespace images {
//% weight=50 blockGap=8
//% help=images/arrow-image
//% blockId=builtin_arrow_image block="arrow image %i"
//% deprecated=true
export function arrowImage(i: ArrowNames): Image {
switch (i) {
// compass directions
case ArrowNames.North: return images.createImage(`
. . # . .
. # # # .
# . # . #
. . # . .
. . # . .`);
case ArrowNames.NorthEast: return images.createImage(`
. . # # #
. . . # #
. . # . #
. # . . .
# . . . .`);
case ArrowNames.East: return images.createImage(`
. . # . .
. . . # .
# # # # #
. . . # .
. . # . .`);
case ArrowNames.SouthEast: return images.createImage(`
# . . . .
. # . . .
. . # . #
. . . # #
. . # # #`);
case ArrowNames.South: return images.createImage(`
. . # . .
. . # . .
# . # . #
. # # # .
. . # . .`);
case ArrowNames.SouthWest: return images.createImage(`
. . . . #
. . . # .
# . # . .
# # . . .
# # # . .`);
case ArrowNames.West: return images.createImage(`
. . # . .
. # . . .
# # # # #
. # . . .
. . # . .`);
case ArrowNames.NorthWest: return images.createImage(`
# # # . .
# # . . .
# . # . .
. . . # .
. . . . #`);
default: return images.createImage(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`);
}
}
//% weight=50 blockGap=8
//% help=images/icon-image
//% blockId=builtin_image block="icon image %i"
@ -393,173 +210,6 @@ namespace images {
. . # . .
. # . # .
# . . . #`);
case IconNames.Triangle: return images.createImage(`
. . . . .
. . # . .
. # . # .
# # # # #
. . . . .`);
case IconNames.LeftTriangle: return images.createImage(`
# . . . .
# # . . .
# . # . .
# . . # .
# # # # #`);
case IconNames.Chessboard: return images.createImage(`
. # . # .
# . # . #
. # . # .
# . # . #
. # . # .`);
case IconNames.Diamond: return images.createImage(`
. . # . .
. # . # .
# . . . #
. # . # .
. . # . .`);
case IconNames.SmallDiamond: return images.createImage(`
. . . . .
. . # . .
. # . # .
. . # . .
. . . . .`);
case IconNames.Square: return images.createImage(`
# # # # #
# . . . #
# . . . #
# . . . #
# # # # #`);
case IconNames.SmallSquare: return images.createImage(`
. . . . .
. # # # .
. # . # .
. # # # .
. . . . .`);
case IconNames.Scissors: return images.createImage(`
# # . . #
# # . # .
. . # . .
# # . # .
# # . . #`);
// The following images were designed by Abbie Brooks.
case IconNames.TShirt: return images.createImage(`
# # . # #
# # # # #
. # # # .
. # # # .
. # # # .`);
case IconNames.Rollerskate: return images.createImage(`
. . . # #
. . . # #
# # # # #
# # # # #
. # . # .`);
case IconNames.Duck: return images.createImage(`
. # # . .
# # # . .
. # # # #
. # # # .
. . . . .`);
case IconNames.House: return images.createImage(`
. . # . .
. # # # .
# # # # #
. # # # .
. # . # .`);
case IconNames.Tortoise: return images.createImage(`
. . . . .
. # # # .
# # # # #
. # . # .
. . . . .`);
case IconNames.Butterfly: return images.createImage(`
# # . # #
# # # # #
. . # . .
# # # # #
# # . # #`);
case IconNames.StickFigure: return images.createImage(`
. . # . .
# # # # #
. . # . .
. # . # .
# . . . #`);
case IconNames.Ghost: return images.createImage(`
. # # # .
# . # . #
# # # # #
# # # # #
# . # . #`);
case IconNames.Sword: return images.createImage(`
. . # . .
. . # . .
. . # . .
. # # # .
. . # . .`);
case IconNames.Giraffe: return images.createImage(`
# # . . .
. # . . .
. # . . .
. # # # .
. # . # .`);
case IconNames.Skull: return images.createImage(`
. # # # .
# . # . #
# # # # #
. # # # .
. # # # .`);
case IconNames.Umbrella: return images.createImage(`
. # # # .
# # # # #
. . # . .
# . # . .
# # # . .`);
case IconNames.Snake: return images.createImage(`
# # . . .
# # . # #
. # . # .
. # # # .
. . . . .`);
// animals
case IconNames.Rabbit: return images.createImage(`
# . # . .
# . # . .
# # # # .
# # . # .
# # # # .`);
case IconNames.Cow: return images.createImage(`
# . . . #
# . . . #
# # # # #
. # # # .
. . # . .`);
// musical notes
case IconNames.QuarterNote: return images.createImage(`
. . # . .
. . # . .
. . # . .
# # # . .
# # # . .`);
case IconNames.EigthNote: return images.createImage(`
. . # . .
. . # # .
. . # . #
# # # . .
# # # . .`);
// other icons
case IconNames.Pitchfork: return images.createImage(`
# . # . #
# . # . #
# # # # #
. . # . .
. . # . .`);
case IconNames.Target: return images.createImage(`
. . # . .
. # # # .
# # . # #
. # # # .
. . # . .`);
// arrows
case IconNames.ArrowNorth: return images.createImage(`
. . # . .
@ -619,12 +269,4 @@ namespace images {
}
}
//% weight=50 blockGap=8
//% help=images/arrow-number
//% blockId=device_arrow block="%arrow"
//% shim=TD_ID
//% deprecated=true
export function arrowNumber(arrow: ArrowNames): number {
return arrow;
}
}

28
libs/core/shims.d.ts vendored
View File

@ -1322,13 +1322,26 @@ declare namespace storage {
* @param value value to store
*/
//% weight=100 blockGap=16
//% block="Put into %key a value of %value as number"
//% blockId=storage_put_value
//% block="Put into %key a value of %value as String"
//% blockId=storage_put_value_str
//%
//% group="Put"
//% blockHidden=true value.defl=0 shim=storage::putValue
function putValue(key: string, value?: string): void;
/**
* 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 Int"
//% blockId=storage_put_value_int
//%
//% group="Put"
//% blockHidden=true value.defl=0 shim=storage::putValueInt
function putValueInt(key: string, value?: int32): void;
/**
* Reads a key value pair from the non volatile storage
* @param key the key for accesing the value
@ -1340,6 +1353,17 @@ declare namespace storage {
//% 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="get number from %key"
//% blockId=storage_get_value_int
//% group="Get"
//% blockHidden=true shim=storage::getValueInt
function getValueInt(key: string): int32;
/**
* Reads a key value pair from the non volatile storage
* @param key the key for accesing the value

View File

@ -10,13 +10,30 @@ namespace storage {
* @param value value to store
*/
//% weight=100 blockGap=16
//% block="Put into %key a value of %value as number"
//% blockId=storage_put_value
//% block="Put into %key a value of %value as String"
//% blockId=storage_put_value_str
//% value.defl=0
//% group="Put"
//% blockHidden=true
void putValue(String key, String value) {
uBit.storage.put(MSTR(key), (uint8_t *)&value, sizeof(int));
ManagedString managedKey = MSTR(key);
uBit.storage.put(managedKey, (uint8_t *)&value, sizeof(int));
}
/**
* 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 Int"
//% blockId=storage_put_value_int
//% value.defl=0
//% group="Put"
//% blockHidden=true
void putValueInt(String key, int value) {
ManagedString managedKey = MSTR(key);
uBit.storage.put(managedKey, (uint8_t *)&value, sizeof(int));
}
/**
@ -40,6 +57,27 @@ namespace storage {
}
}
/**
* 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_int
//% group="Get"
//% blockHidden=true
int getValueInt(String key) {
KeyValuePair* data = uBit.storage.get(MSTR(key));
int stored;
if(data == NULL) {
return 0;
} 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

View File

@ -15,8 +15,8 @@ enum StorageSlots {
s7 = 6,
}
let storages = ['s1', 's2', 's3', 's4', 's5', 's6', 's7', 's8', 's9', 's10', 's11', 's12'];
let storagesStr = ['s1', 's2', 's3', 's4', 's5', 's6', 's7'];
let storagesInt = ['i1', 'i2', 'i3', 'i4', 'i5', 'i6', 'i7'];
/**
* Provides access to persistent storage functionality.
*/
@ -30,12 +30,27 @@ namespace storage {
* @param value value to store
*/
//% weight=100 blockGap=16
//% block="Save into %key a value of %value"
//% block="Save into string %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);
putValue(storagesStr[key], value);
}
/**
* 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 number %key a value of %value"
//% blockId=storage_put_number
//% group="Put"
export function putNumber(key: StorageSlots, value: number) : void {
let managedValue = (value * 1000) | 0;
putValueInt(storagesInt[key], managedValue);
}
/**
@ -43,16 +58,12 @@ namespace storage {
* @param key the key for accesing the value
*/
//% weight=100 blockGap=16
//% block="read from %key as number"
//% block="read from number %key"
//% 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);
}
let value = getValueInt(storagesInt[key]);
return value / 1000;
}
/**
@ -60,11 +71,11 @@ namespace storage {
* @param key the key for accesing the value
*/
//% weight=100 blockGap=16
//% block="read from %key"
//% block="read from string %key"
//% blockId=storage_get_string
//% group="Get"
export function getString(key: StorageSlots) : string {
return getValue(storages[key]);
return getValue(storagesStr[key]);
}
/**
@ -72,11 +83,23 @@ namespace storage {
* @param key the key for accesing the value
*/
//% weight=100 blockGap=16
//% block="Clear %key"
//% blockId=storage_remove_key
//% block="Clear string %key"
//% blockId=storage_remove_key_string
//% group="Remove"
export function removeKey(key: StorageSlots) : void {
remove(storages[key]);
export function removeKeyStr(key: StorageSlots) : void {
remove(storagesStr[key]);
}
/**
* Deletes the key from the non volatile storage
* @param key the key for accesing the value
*/
//% weight=100 blockGap=16
//% block="Clear number %key"
//% blockId=storage_remove_key_int
//% group="Remove"
export function removeKeyInt(key: StorageSlots) : void {
remove(storagesInt[key]);
}

View File

@ -1,10 +1,15 @@
namespace pxsim.storage {
export function putValue(key: string, value: string) : void {
function putValue(key: string, value: string) : void {
sessionStorage.setItem('simulatorValue_'+key, value);
}
function putValueInt(key: string, value: number) : void {
sessionStorage.setItem('simulatorValue_'+key, value+"");
}
export function getValue(key: string) : string {
if(sessionStorage.getItem('simulatorValue_'+key)) {
return sessionStorage.getItem('simulatorValue_'+key);
@ -13,7 +18,19 @@ namespace pxsim.storage {
}
}
export function remove(key: string) : void {
export function getValueInt(key: string) : number {
if(sessionStorage.getItem('simulatorValue_'+key)) {
return parseFloat(sessionStorage.getItem('simulatorValue_'+key));
} else {
return 0;
}
}
export function removeStr(key: string) : void {
sessionStorage.removeItem('simulatorValue_'+key);
}
export function removeInt(key: string) : void {
sessionStorage.removeItem('simulatorValue_'+key);
}