pxt-calliope/libs/core/storage.cpp

57 lines
1.5 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 {
2021-11-28 03:54:02 +01:00
/**
* 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
}
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
/**
2022-02-28 22:41:38 +01:00
* Removes a key value pair from the non volatile storage
2021-11-28 00:38:36 +01:00
* @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));
}
}