Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Hector Oliveros committed Jan 6, 2018
1 parent 6bbe3a2 commit 80f949b
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
51 changes: 51 additions & 0 deletions examples/simple_example/simple_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <math.h>
#include <configdb.h>

/** The custom MyConfiguration class **/
class MyConfiguration
{
public:
int intValue;
float floatValue;
};

ConfigDB<MyConfiguration> configDB;

bool printCfg(MyConfiguration cfg)
{
Serial.printf("cfg.intValue:%d\n", cfg.intValue);
Serial.printf("cfg.floatValue:%f\n", cfg.floatValue);
}

void setup()
{
Serial.begin(9600);

// intValue | floatValue
MyConfiguration defaultCfg = {0, 9876.543};

// If the MyConfiguration is not saved then save it with a default value
bool isPreviusConfigStored = configDB.storeDefaultIfNotLoad(defaultCfg);

Serial.printf("\nisPreviusConfigStored=%d\n", isPreviusConfigStored); // False

// Get the Configuration from the eeprom
MyConfiguration newCfg = configDB.load();
printCfg(newCfg);

// Make new config and save
newCfg.intValue = 1234;
newCfg.floatValue = EULER;
configDB.save(newCfg);

// Get the new MyConfiguration recently saved in the eeprom
newCfg = configDB.load();
printCfg(newCfg);

configDB.clear();
}

void loop()
{
// put your main code here, to run repeatedly:
}
Binary file added media/serial_output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
107 changes: 107 additions & 0 deletions src/configdb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#ifndef _CONFIG_DB__
#define _CONFIG_DB__

#include <EEPROM.h>
#include <Arduino.h>

/**
If the first segment of the config is the PI number then a
valid config is stored in the EEPROM
*/
double VALID_CONFIG_ID = (double)PI;

template <class T>
class ConfigDB
{
public:
/**
@param startAddr: address of the EEPROM where the
configuration data is stored.
*/
ConfigDB(int startAddr = 0)
{
this->_startAddr = startAddr;
this->_startConfigAddr = startAddr + sizeof(VALID_CONFIG_ID);
EEPROM.begin(sizeof(T) + sizeof(VALID_CONFIG_ID));
}

/**
If there is no stored configuration then save one using the default
values of the Config class
@return true if save defaults config, false otherwise
*/
bool storeDefaultIfNotLoad(T defaultValue)
{
if (!isValidConfigStored())
{
this->save(defaultValue);
return false;
}
return true;
}

/**
Save config in the EEPROM
*/
void save(T cfg)
{
EEPROM.put(this->_startAddr, VALID_CONFIG_ID);
EEPROM.put(this->_startConfigAddr, cfg);
EEPROM.commit();
}

/**
Load config from the EEPROM
*/
T load()
{
T tmpCfg;
EEPROM.get(this->_startConfigAddr, tmpCfg);
return tmpCfg;
}

/**
clear the configuration (set to zeroes)
*/
void clear()
{
for (unsigned int i = this->_startAddr; i < (sizeof(T) + sizeof(VALID_CONFIG_ID)); i++)
{
EEPROM.write(i, 0);
}
EEPROM.commit();
}

/**
Check if a valid configuration is stores in the segment of EEPROM
*/
bool isValidConfigStored()
{
double firstData;
EEPROM.get(this->_startAddr, firstData);
return firstData == VALID_CONFIG_ID;
}

/**
Get the start address of the config in EEPROM
*/
int getStartAddr()
{
return this->_startAddr;
}

/**
Get the last address of the config in EEPROM.
*/
int getEndAddr()
{
return this->_startConfigAddr + sizeof(T);
}

private:
unsigned int _startAddr;
unsigned int _startConfigAddr;
};

#endif

0 comments on commit 80f949b

Please sign in to comment.