pxt-calliope/libs/core/storage.cpp

94 lines
2.6 KiB
C++
Raw Normal View History

2021-11-28 00:38:36 +01:00
#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
2021-11-28 03:54:02 +01:00
//% block="Put into %key a value of %value as String"
//% blockId=storage_put_value_str
2021-11-28 00:38:36 +01:00
//% value.defl=0
//% group="Put"
//% blockHidden=true
void putValue(String key, String value) {
2021-11-28 03:54:02 +01:00
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));
2021-11-28 00:38:36 +01:00
}
/**
* 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;
}
}
2021-11-28 03:54:02 +01:00
/**
* 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;
}
}
2021-11-28 00:38:36 +01:00
/**
* 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));
}
}