feat: added eeprom service

This commit is contained in:
Pavel-Savely Savianok 2024-10-21 17:53:33 +03:00
parent 0c06d2a56e
commit 1065b42a16
5 changed files with 106 additions and 2 deletions

View File

@ -51,6 +51,7 @@
"streambuf": "cpp", "streambuf": "cpp",
"cinttypes": "cpp", "cinttypes": "cpp",
"typeinfo": "cpp", "typeinfo": "cpp",
"variant": "cpp" "variant": "cpp",
"hash_map": "cpp"
} }
} }

View File

@ -0,0 +1,12 @@
#pragma once
class FlashMapping{
public:
char* key;
short size;
FlashMapping(char* key, short size){
this->key = key;
this->size = size;
}
};

View File

@ -0,0 +1,43 @@
#include "FlashService.h"
#include <EEPROM.h>
/*
* Implementation of Flash service. Docs on header file.
* Writed on 21.10.2024 17:11
* By Lisoveliy
*/
FlashService::FlashService(int allocSize)
{
EEPROM.begin(allocSize);
}
bool FlashService::WriteData(char *key, void *object, size_t size)
{
try
{
flashMap.insert(flashHashMap::value_type(FlashMapping(key, size), this->addressPointer));
EEPROM.put(addressPointer, object);
addressPointer += size;
return true;
}
catch (...)
{
return false;
}
}
bool FlashService::WriteData(char *key, char data)
{
try
{
flashMap.insert(flashHashMap::value_type(FlashMapping(key, 1), this->addressPointer));
EEPROM.write(addressPointer, data);
addressPointer++;
return true;
}
catch (...)
{
return false;
}
}

View File

@ -0,0 +1,47 @@
#pragma once
#include "FlashMapping.h"
#include <unordered_map>
/*
* Definition of Flash service.
* Writed on 21.10.2024 17:11
* By Lisoveliy
*/
typedef std::unordered_map<FlashMapping, int> flashHashMap;
class FlashService{
private:
flashHashMap flashMap;
int addressPointer = 0;
public:
/// @brief Flash Service constructor
/// @param allocSize Size of Flash Allocation (in bytes) (default - 128kb)
FlashService(int allocSize = 0x20000);
/// @brief Write data object by key
/// @param key string key for hash
/// @param object any type of object casted to void*
/// @return true on success, false on failure (IDKWhat kind of failure)
bool WriteData(char* key, void* object, size_t size);
/// @brief Write byte data by key
/// @param key string key for hash
/// @param data byte of data
/// @return
bool WriteData(char* key, char data);
/// @brief Update data object by key
/// @param key
/// @param object
/// @return true on success, false on failure (for example not same size of object)
bool UpdateData(char* key, void* object);
/// @brief Remove data object by key
/// @param key string key for hash
/// @return true on success, false on failure (IDKWhat kind of failure)
bool RemoveData(char* key);
/// @brief Read data object by key
/// @param key string key for hash
/// @return data object
void* ReadData(char* key);
};

View File

@ -14,4 +14,5 @@ board = nodemcuv2
framework = arduino framework = arduino
monitor_rts = 0 monitor_rts = 0
upload_speed = 378000 upload_speed = 378000
monitor_speed = 19200 monitor_speed = 19200
lib_deps = eeprom