Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User EEPROM Compatibility #17

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ EspSaveCrash SaveCrash(0x0010, 0x0200);

```cpp
//Offset, Size, Persistent EEPROM
EspSaveCrash SaveCrash(0x0010, 0x0200, true);
EspSaveCrash SaveCrash(0x0010, 0x0200, YOUR_APP_EEPROM_SIZE);
```

Picture below shows relationship between both configuration parameters and total available EEPROM space.
Expand Down
28 changes: 18 additions & 10 deletions src/EspSaveCrash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
*/
uint16_t EspSaveCrash::_offset = 0x0010;
uint16_t EspSaveCrash::_size = 0x0200;
bool EspSaveCrash::_persistEEPROM = false;
uint16_t EspSaveCrash::_EEPROM_size = 0;

/**
* Save crash information in EEPROM
Expand All @@ -48,7 +48,9 @@ extern "C" void custom_crash_callback(struct rst_info * rst_info, uint32_t stack
{
// Note that 'EEPROM.begin' method is reserving a RAM buffer
// The buffer size is SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_SPACE_SIZE
EEPROM.begin(EspSaveCrash::_offset + EspSaveCrash::_size);
if(EEPROM.length() == 0){
EEPROM.begin(EspSaveCrash::_EEPROM_size);
}

byte crashCounter = EEPROM.read(EspSaveCrash::_offset + SAVE_CRASH_COUNTER);
int16_t writeFrom;
Expand Down Expand Up @@ -115,11 +117,11 @@ extern "C" void custom_crash_callback(struct rst_info * rst_info, uint32_t stack
/**
* The class constructor
*/
EspSaveCrash::EspSaveCrash(uint16_t off, uint16_t size, bool persistEEPROM)
EspSaveCrash::EspSaveCrash(uint16_t off, uint16_t size, uint16_t EEPROM_size)
{
_offset = off;
_size = size;
_persistEEPROM = persistEEPROM;
_EEPROM_size = EEPROM_size == 0 ? (_offset + _size) : EEPROM_size;
}

/**
Expand All @@ -131,10 +133,12 @@ void EspSaveCrash::clear(void)
{
// Note that 'EEPROM.begin' method is reserving a RAM buffer
// The buffer size is SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_SPACE_SIZE
EEPROM.begin(_offset + _size);
if(EEPROM.length() == 0){
EEPROM.begin(_EEPROM_size);
}
// clear the crash counter
EEPROM.write(_offset + SAVE_CRASH_COUNTER, 0);
if(!_persistEEPROM){
if(!_EEPROM_size){
EEPROM.end();
}
else{
Expand All @@ -151,7 +155,9 @@ void EspSaveCrash::print(Print& outputDev)
{
// Note that 'EEPROM.begin' method is reserving a RAM buffer
// The buffer size is SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_SPACE_SIZE
EEPROM.begin(_offset + _size);
if(EEPROM.length() == 0){
EEPROM.begin(_EEPROM_size);
}
byte crashCounter = EEPROM.read(_offset + SAVE_CRASH_COUNTER);
if (crashCounter == 0)
{
Expand Down Expand Up @@ -207,7 +213,7 @@ void EspSaveCrash::print(Print& outputDev)
}
int16_t writeFrom;
EEPROM.get(_offset + SAVE_CRASH_WRITE_FROM, writeFrom);
if(!_persistEEPROM){
if(!_EEPROM_size){
EEPROM.end();
}
else{
Expand Down Expand Up @@ -272,9 +278,11 @@ void EspSaveCrash::crashToBuffer(char* userBuffer)
*/
int EspSaveCrash::count()
{
EEPROM.begin(_offset + _size);
if(EEPROM.length() == 0){
EEPROM.begin(_EEPROM_size);
}
int crashCounter = EEPROM.read(_offset + SAVE_CRASH_COUNTER);
if(!_persistEEPROM){
if(!_EEPROM_size){
EEPROM.end();
}
else{
Expand Down
4 changes: 2 additions & 2 deletions src/EspSaveCrash.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
class EspSaveCrash
{
public:
EspSaveCrash(uint16_t = 0x0010, uint16_t = 0x0200, bool = false);
EspSaveCrash(uint16_t = 0x0010, uint16_t = 0x0200, uint16_t = 0);
void print(Print& outDevice = Serial);
size_t print(char* userBuffer, size_t size);

Expand All @@ -96,7 +96,7 @@ class EspSaveCrash
//These have to be public in order to be accessed by callback
static uint16_t _offset;
static uint16_t _size;
static bool _persistEEPROM;
static uint16_t _EEPROM_size;
private:
// none
};
Expand Down