diff --git a/.cproject b/.cproject index 2d04633..3fed957 100644 --- a/.cproject +++ b/.cproject @@ -27,6 +27,7 @@ + @@ -38,6 +39,7 @@ + diff --git a/.project b/.project index 7f424b3..4b6f409 100644 --- a/.project +++ b/.project @@ -26,6 +26,11 @@ it.baeyens.arduinonature + + Libraries/EEPROM + 2 + ArduinoHardwareLibPath/EEPROM + Libraries/Metro 2 diff --git a/Config.cpp b/Config.cpp new file mode 100644 index 0000000..509d55f --- /dev/null +++ b/Config.cpp @@ -0,0 +1,70 @@ +/* + * Config.cpp + * + * Created on: 04.03.2014 + * Author: wn + */ + + +#include +#include "Config.h" + + +float Config::getFloat(int pos) { + u_float u; + for (unsigned int i = 0; i < sizeof(float); i++) { + u.e[i] = EEPROM.read(pos + i); + } + return u.f; +} + +void Config::setFloat(int pos, float value) { + u_float u; + u.f = value; + for (unsigned int i = 0; i < sizeof(float); i++) { + EEPROM.write(pos + i, u.e[i]); + } +} + +unsigned int Config::getUInt(int pos) { + u_uint u; + for (unsigned int i = 0; i < sizeof(unsigned int); i++) { + u.e[i] = EEPROM.read(pos + i); + } + return u.i; +} + +void Config::setUInt(int pos, unsigned int value) { + u_uint u; + u.i = value; + for (unsigned int i = 0; i < sizeof(unsigned int); i++) { + EEPROM.write(pos + i, u.e[i]); + } +} + +unsigned long Config::getULong(int pos) { + u_ulong u; + for (unsigned int i = 0; i < sizeof(unsigned long); i++) { + u.e[i] = EEPROM.read(pos + i); + } + return u.l; +} + +void Config::setULong(int pos, unsigned long value) { + u_ulong u; + u.l = value; + for (unsigned int i = 0; i < sizeof(unsigned long); i++) { + EEPROM.write(pos + i, u.e[i]); + } +} + +bool Config::isInitialized() { + unsigned int magic = getUInt(MAGIC); + return magic == MAGIC_TOKEN; +} + +void Config::setMagic() { + setUInt(MAGIC, MAGIC_TOKEN); +} + + diff --git a/Config.h b/Config.h new file mode 100644 index 0000000..b9b6d19 --- /dev/null +++ b/Config.h @@ -0,0 +1,49 @@ +/* + * Config.h + * + * Created on: 04.03.2014 + * Author: wn + */ + +#ifndef CONFIG_H_ +#define CONFIG_H_ + + + +typedef union { + float f; + uint8_t e[sizeof(float)]; +} u_float; + +typedef union { + unsigned int i; + uint8_t e[sizeof(unsigned int)]; +} u_uint; + +typedef union { + unsigned long l; + uint8_t e[sizeof(unsigned long)]; +} u_ulong; + + +namespace Config { + const unsigned int MAGIC_TOKEN = 0xDEADBEEF; + + const int MAGIC = 0; // 4 + const int THERMOMETER_ALPHA = 4; // 4 + const int THERMOMETER_PERIOD = 8; // 8 + const int THERMOMETER_CAL[4] = {12, 16, 20, 24}; // 4 x 4 + + float getFloat(int pos); + void setFloat(int pos, float value); + unsigned int getUInt(int pos); + void setUInt(int pos, unsigned int value); + unsigned long getULong(int pos); + void setULong(int pos, unsigned long value); + + bool isInitialized(); + + void setMagic(); +} + +#endif /* CONFIG_H_ */ diff --git a/thermometer.cpp b/thermometer.cpp index a9dc4e2..fa84c78 100644 --- a/thermometer.cpp +++ b/thermometer.cpp @@ -3,6 +3,7 @@ #include "fatal.h" #include "AD7190.h" #include "Streaming.h" +#include "Config.h" @@ -118,18 +119,33 @@ Thermometer::Thermometer() : m_period(DEFAULT_PERIOD), m_periodMillis(DEFAULT_PE void Thermometer::setPeriodMeasure(unsigned long p) { + Config::setULong(Config::THERMOMETER_PERIOD, p); m_periodMillis = p; m_period = Metro(p); } +unsigned long Thermometer::getPeriodMeasure() { + return m_periodMillis; +} + void Thermometer::setAlpha(float a) { + Config::setFloat(Config::THERMOMETER_ALPHA, a); m_alpha = a; } +float Thermometer::getAlpha() { + return m_alpha; +} + + void Thermometer::setCalibrateFactor(unsigned int i, float c) { + Config::setFloat(Config::THERMOMETER_CAL[i], c); m_calibrateFactor[i] = c; } +float Thermometer::getCalibrateFactor(unsigned int i) { + return m_calibrateFactor[i]; +} float pt1000(float r) { @@ -205,10 +221,28 @@ void Thermometer::begin(CmdServer *cmdServer) { AD7190_Calibrate(AD7190_MODE_CAL_INT_ZERO, AD7190_CH_AIN1P_AINCOM); AD7190_Calibrate(AD7190_MODE_CAL_INT_FULL, AD7190_CH_AIN1P_AINCOM); - setCalibrateFactor(0, 1.002999); - setCalibrateFactor(1, 1.001804); - setCalibrateFactor(2, 1.000794); - setCalibrateFactor(3, 1.001071); + + if (! Config::isInitialized()) { + Serial << "Initializing EEPROM" << endl; + Config::setFloat(Config::THERMOMETER_ALPHA, 1.0); + Config::setULong(Config::THERMOMETER_PERIOD, 1000); + for (int i = 0; i < 4; i++) { + Config::setFloat(Config::THERMOMETER_CAL[i], 1.0); + } + Config::setMagic(); + } + + setAlpha(Config::getFloat(Config::THERMOMETER_ALPHA)); + setPeriodMeasure(Config::getULong(Config::THERMOMETER_PERIOD)); + for (int i = 0; i < 4; i++) { + setCalibrateFactor(i, Config::getFloat(Config::THERMOMETER_CAL[i])); + } + + + //setCalibrateFactor(0, 1.002999); + //setCalibrateFactor(1, 1.001804); + //setCalibrateFactor(2, 1.000794); + //setCalibrateFactor(3, 1.001071); // prepare prepareAdc(); diff --git a/thermometer.h b/thermometer.h index ea8c508..a6048f8 100644 --- a/thermometer.h +++ b/thermometer.h @@ -93,11 +93,11 @@ private: void setTemperature(unsigned int index, float t); void setPeriodMeasure(unsigned long p); - unsigned long getPeriodMeasure() { return m_periodMillis; } + unsigned long getPeriodMeasure(); void setAlpha(float a); - float getAlpha() { return m_alpha; } + float getAlpha(); void setCalibrateFactor(unsigned int i, float c); - float getCalibrateFactor(unsigned int i) { return m_calibrateFactor[i]; } + float getCalibrateFactor(unsigned int i); };