Skip to content

Commit

Permalink
Fix #2 bug with eeprom begin size. Fix #3 missing startbyte on clear.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maksim Kalashnikov authored and Maksim Kalashnikov committed Sep 3, 2020
1 parent cd2be0f commit 7cd5546
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
],
"dependencies": ["ArduinoJson"],
"version": "1.0.3",
"version": "1.0.4",
"frameworks": "arduino",
"platforms": "*"
}
47 changes: 28 additions & 19 deletions src/config2eeprom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,19 @@ config2eeprom::config2eeprom(unsigned int beginByte)
{
_beginByte = beginByte;
}
// Begin on bite number and sets the doc Size
config2eeprom::config2eeprom(unsigned int beginByte, unsigned int docSize)
{
_beginByte = beginByte;
_docSize = docSize;
}

// Save staticDOC json hash to EEPROM
void config2eeprom::save(staticConfigDoc doc)
{
#ifdef DEBUG_EEPROM
Serial.println("Config2EEPRom: Saving data.");
Serial.print("Config2EEPRom: Saving data from begin byte:");
Serial.println(_beginByte);
#endif

// Mount EEPROM partiton
EEPROM.begin(_docSize);
EEPROM.begin(EEPROM_SIZE);

char serialized[_docSize];
char serialized[EEPROM_DOC_SIZE];
serializeMsgPack(doc, serialized);

// Write to EEPROM byte by byte
Expand All @@ -37,20 +32,25 @@ void config2eeprom::save(staticConfigDoc doc)
}

// Commit and unmount
EEPROM.commit();
if (!EEPROM.commit())
{
Serial.print("Config2EEPRom: Error while commit to eeprom.");
}
EEPROM.end();
}

// Load staticDOC json hash from EEPROM. Return false on error or empty.
bool config2eeprom::load(staticConfigDoc &doc)
{
#ifdef DEBUG_EEPROM
Serial.println("Config2EEProm: Loads from eeprom.");
Serial.print("Config2EEPRom: Loading data from begin byte:");
Serial.println(_beginByte);
#endif

// Mount EEPROM partiton
EEPROM.begin(_docSize);
EEPROM.begin(EEPROM_SIZE);

char serialized[_docSize];
char serialized[EEPROM_DOC_SIZE];

// Read from EEPROM byte by byte
for (short n = 0; n < sizeof(serialized); n++) // automatically adjust for chars
Expand All @@ -61,6 +61,11 @@ bool config2eeprom::load(staticConfigDoc &doc)
// Unmount eeprom
EEPROM.end();

#ifdef DEBUG_EEPROM
Serial.println("Config2EEPRom: Got bytes from eeprom:");
Serial.println(serialized);
#endif

// Deserialize from msgPack
DeserializationError error = deserializeMsgPack(doc, serialized);

Expand All @@ -84,7 +89,7 @@ bool config2eeprom::load(staticConfigDoc &doc)

#ifdef DEBUG_EEPROM
Serial.print("Config2EEProm: Deserialized document with size:");
Serial.println(doc.size();
Serial.println(doc.size());
#endif
return true;
}
Expand All @@ -93,14 +98,18 @@ bool config2eeprom::load(staticConfigDoc &doc)
void config2eeprom::clear()
{
#ifdef DEBUG_EEPROM
Serial.println("Config2EEProm: Clearing EEPROM.");
Serial.print("Config2EEPRom: Clearing EEPROM begin byte:");
Serial.println(_beginByte);
#endif

EEPROM.begin(_docSize);
for (int i = 0; i < _docSize; i++)
EEPROM.begin(EEPROM_SIZE);
for (int i = 0; i < EEPROM_DOC_SIZE; i++)
{
EEPROM.write(i + _beginByte, 0);
}
if (!EEPROM.commit())
{
EEPROM.write(i, 0);
Serial.print("Config2EEPRom: Error while commit to eeprom.");
}
EEPROM.commit();
EEPROM.end();
}
6 changes: 5 additions & 1 deletion src/config2eeprom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
#include <EEPROM.h>
#include <ArduinoJson.h>

#define DEBUG_EEPROM

#define EEPROM_SIZE 4000
#define EEPROM_DOC_SIZE 254

// StaticJsonDocument type for EEPROM_DOC_SIZE
typedef StaticJsonDocument<EEPROM_DOC_SIZE> staticConfigDoc;

Expand All @@ -14,7 +19,6 @@ class config2eeprom
public:
config2eeprom();
config2eeprom(unsigned int beginByte);
config2eeprom(unsigned int beginByte, unsigned int docSize);
void save(staticConfigDoc doc);
bool load(staticConfigDoc &doc);
void clear();
Expand Down

0 comments on commit 7cd5546

Please sign in to comment.