From 1065b42a166986e926e3b8926ef87c62c27fa7a6 Mon Sep 17 00:00:00 2001 From: Lisoveliy <1986developer@gmail.com> Date: Mon, 21 Oct 2024 17:53:33 +0300 Subject: [PATCH] feat: added eeprom service --- program/SVET/.vscode/settings.json | 3 +- program/SVET/lib/SVET/Flash/FlashMapping.h | 12 +++++ program/SVET/lib/SVET/Flash/FlashService.cpp | 43 ++++++++++++++++++ program/SVET/lib/SVET/Flash/FlashService.h | 47 ++++++++++++++++++++ program/SVET/platformio.ini | 3 +- 5 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 program/SVET/lib/SVET/Flash/FlashMapping.h create mode 100644 program/SVET/lib/SVET/Flash/FlashService.cpp create mode 100644 program/SVET/lib/SVET/Flash/FlashService.h diff --git a/program/SVET/.vscode/settings.json b/program/SVET/.vscode/settings.json index 64e220b..ba58895 100644 --- a/program/SVET/.vscode/settings.json +++ b/program/SVET/.vscode/settings.json @@ -51,6 +51,7 @@ "streambuf": "cpp", "cinttypes": "cpp", "typeinfo": "cpp", - "variant": "cpp" + "variant": "cpp", + "hash_map": "cpp" } } \ No newline at end of file diff --git a/program/SVET/lib/SVET/Flash/FlashMapping.h b/program/SVET/lib/SVET/Flash/FlashMapping.h new file mode 100644 index 0000000..6443dec --- /dev/null +++ b/program/SVET/lib/SVET/Flash/FlashMapping.h @@ -0,0 +1,12 @@ +#pragma once + +class FlashMapping{ + public: + char* key; + short size; + + FlashMapping(char* key, short size){ + this->key = key; + this->size = size; + } +}; \ No newline at end of file diff --git a/program/SVET/lib/SVET/Flash/FlashService.cpp b/program/SVET/lib/SVET/Flash/FlashService.cpp new file mode 100644 index 0000000..c7aed15 --- /dev/null +++ b/program/SVET/lib/SVET/Flash/FlashService.cpp @@ -0,0 +1,43 @@ +#include "FlashService.h" +#include + +/* + * 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; + } +} \ No newline at end of file diff --git a/program/SVET/lib/SVET/Flash/FlashService.h b/program/SVET/lib/SVET/Flash/FlashService.h new file mode 100644 index 0000000..68a33c3 --- /dev/null +++ b/program/SVET/lib/SVET/Flash/FlashService.h @@ -0,0 +1,47 @@ +#pragma once +#include "FlashMapping.h" +#include +/* + * Definition of Flash service. + * Writed on 21.10.2024 17:11 + * By Lisoveliy + */ +typedef std::unordered_map 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); +}; \ No newline at end of file diff --git a/program/SVET/platformio.ini b/program/SVET/platformio.ini index f7a3c28..4e96f3f 100644 --- a/program/SVET/platformio.ini +++ b/program/SVET/platformio.ini @@ -14,4 +14,5 @@ board = nodemcuv2 framework = arduino monitor_rts = 0 upload_speed = 378000 -monitor_speed = 19200 \ No newline at end of file +monitor_speed = 19200 +lib_deps = eeprom \ No newline at end of file