diff --git a/.cproject b/.cproject index ee01d06..128a3a9 100644 --- a/.cproject +++ b/.cproject @@ -18,19 +18,27 @@ - + + + + + @@ -43,7 +51,7 @@ - + diff --git a/.project b/.project index cd38515..b01e851 100644 --- a/.project +++ b/.project @@ -26,6 +26,11 @@ it.baeyens.arduinonature + + Libraries/EEPROM + 2 + ArduinoHardwareLibPath/EEPROM + Libraries/SPI 2 diff --git a/AD7190.cpp b/AD7190.cpp index b42b138..8d3c06e 100644 --- a/AD7190.cpp +++ b/AD7190.cpp @@ -305,7 +305,7 @@ void AD7190_RangeSetup(unsigned char polarity, unsigned char range) oldRegValue = AD7190_GetRegisterValue(AD7190_REG_CONF,3, 1); oldRegValue &= ~(AD7190_CONF_UNIPOLAR | AD7190_CONF_GAIN(0x7)); - newRegValue = oldRegValue | + newRegValue = oldRegValue | (polarity * AD7190_CONF_UNIPOLAR) | AD7190_CONF_GAIN(range); AD7190_SetRegisterValue(AD7190_REG_CONF, newRegValue, 3, 1); @@ -323,7 +323,8 @@ unsigned long AD7190_SingleConversion(void) command = AD7190_MODE_SEL(AD7190_MODE_SINGLE) | AD7190_MODE_CLKSRC(AD7190_CLK_INT) | - AD7190_MODE_RATE(0x060); + AD7190_MODE_REJ60 | + AD7190_MODE_RATE(0x360); //ADI_PART_CS_LOW; SPI_Enable(AD7190_SLAVE_ID); diff --git a/ThermometerPro.cpp b/ThermometerPro.cpp index 8db6eec..84cf590 100644 --- a/ThermometerPro.cpp +++ b/ThermometerPro.cpp @@ -1,8 +1,25 @@ #include "ThermometerPro.h" + #include "AD7190.h" +#include "cmd.h" +#include "test.h" +#include "uptime.h" + + +static CmdServer cmdServer(&Serial); +static TestCmd testCmd; +static Uptime uptime; + + void setup() { + cmdServer.begin(); + testCmd.registerYourself(&cmdServer); + uptime.begin(&cmdServer); + + + Serial.begin(9600); delay(5000); @@ -11,6 +28,7 @@ void setup() { unsigned char ad7190Status = AD7190_Init(); Serial.print(String("ad7190Status: ") + ad7190Status + "\n"); + AD7190_RangeSetup(1, AD7190_CONF_GAIN_1); AD7190_Calibrate(AD7190_MODE_CAL_INT_ZERO, AD7190_CH_AIN4P_AINCOM); AD7190_Calibrate(AD7190_MODE_CAL_INT_FULL, AD7190_CH_AIN4P_AINCOM); @@ -24,10 +42,11 @@ void setup() { AD7190_Calibrate(AD7190_MODE_CAL_INT_ZERO, AD7190_CH_AIN1P_AINCOM); AD7190_Calibrate(AD7190_MODE_CAL_INT_FULL, AD7190_CH_AIN1P_AINCOM); - AD7190_RangeSetup(1, AD7190_CONF_GAIN_1); } + + unsigned long T() { AD7190_ChannelSelect(AD7190_CH_AIN4P_AINCOM); unsigned long t = AD7190_SingleConversion(); @@ -43,41 +62,66 @@ float pt1000(float r) { void loop() { + cmdServer.exec(); + uptime.exec(); + + + Serial.print("Tick\n"); AD7190_ChannelSelect(AD7190_CH_AIN4P_AINCOM); unsigned long n4 = AD7190_SingleConversion(); + n4 &= 0xffff00; AD7190_ChannelSelect(AD7190_CH_AIN3P_AINCOM); unsigned long n3x = AD7190_SingleConversion(); + n3x &= 0xffff00; AD7190_ChannelSelect(AD7190_CH_AIN2P_AINCOM); unsigned long n2x = AD7190_SingleConversion(); + n2x &= 0xffff00; AD7190_ChannelSelect(AD7190_CH_AIN1P_AINCOM); unsigned long n1x = AD7190_SingleConversion(); - - + n1x &= 0xffff00; unsigned long n_max = 0xffffff; float r_ref = 6000.0; - float r_m4 = (((float)n4) / ((float)n_max)) * r_ref; + + float r_m4 = (((float)n4) / ((float)n_max)) * r_ref * 1.0010705; float t4 = pt1000(r_m4); - Serial.print(n4); Serial.print(", "); Serial.print(r_m4, 4); Serial.print(", "); Serial.println(t4, 2); + Serial.print(n4); Serial.print(", "); Serial.print(r_m4, 4); Serial.print(", "); Serial.print(t4, 2); + Serial.println(); unsigned long n3 = n3x - n4; - float r_m3 = (((float)n3) / ((float)n_max)) * r_ref; + float r_m3 = (((float)n3) / ((float)n_max)) * r_ref * 1.000794; float t3 = pt1000(r_m3); - Serial.print(n3x); Serial.print(", "); Serial.print(n3); Serial.print(", "); Serial.print(r_m3, 4); Serial.print(", "); Serial.println(t3, 2); + Serial.print(n3x); Serial.print(", "); Serial.print(n3); Serial.print(", "); Serial.print(r_m3, 4); Serial.print(", "); Serial.print(t3, 2); +// float d = r_m4 - r_m3; +// Serial.print(", "); Serial.print(d, 2); +// unsigned long dd = n4 - n3; +// Serial.print(", "); Serial.print(dd); + Serial.println(); unsigned long n2 = n2x - n3x; - float r_m2 = (((float)n2) / ((float)n_max)) * r_ref; + float r_m2 = (((float)n2) / ((float)n_max)) * r_ref * 1.001804; float t2 = pt1000(r_m2); - Serial.print(n2x); Serial.print(", "); Serial.print(n2); Serial.print(", "); Serial.print(r_m2, 4); Serial.print(", "); Serial.println(t2, 2); + Serial.print(n2x); Serial.print(", "); Serial.print(n2); Serial.print(", "); Serial.print(r_m2, 4); Serial.print(", "); Serial.print(t2, 2); +// d = r_m3 - r_m2; +// Serial.print(", "); Serial.print(d, 2); +// dd = n3 - n2; +// Serial.print(", "); Serial.print(dd); + Serial.println(); unsigned long n1 = n1x - n2x; - float r_m1 = (((float)n1) / ((float)n_max)) * r_ref; + float r_m1 = (((float)n1) / ((float)n_max)) * r_ref * 1.002999; float t1 = pt1000(r_m1); - Serial.print(n1x); Serial.print(", "); Serial.print(n1); Serial.print(", "); Serial.print(r_m1, 4); Serial.print(", "); Serial.println(t1, 2); + Serial.print(n1x); Serial.print(", "); Serial.print(n1); Serial.print(", "); Serial.print(r_m1, 4); Serial.print(", "); Serial.print(t1, 2); +// d = r_m2 - r_m1; +// Serial.print(", "); Serial.print(d, 2); +// dd = n2 - n1; +// Serial.print(", "); Serial.print(dd); + Serial.println(); + Serial.println(); delay(1000); diff --git a/cmd.cpp b/cmd.cpp new file mode 100644 index 0000000..fbacf1c --- /dev/null +++ b/cmd.cpp @@ -0,0 +1,97 @@ +#include + +#include "cmd.h" +#include "fatal.h" + + +void Cmd::registerYourself(CmdServer *cmdServer) { + cmdServer->registerCmd(this); +} + + +CmdServer::CmdServer(Stream *p_stream) : + m_stream(p_stream), cmd(""), params(""), cmdListIdx(0) +{ +} + +void CmdServer::begin() { + ((Serial_*)m_stream)->begin(9600); +} + +void CmdServer::exec() { + static uint8_t state = 0; + bool done = false; + + if (m_stream->available()) { + char ch; + while ((ch = m_stream->read()) != -1) { + switch (state) { + case 0: + if ((ch != ' ') && (ch != '\n')) { + cmd += ch; + } else if (ch == '\n') { + done = true; + } else { + state = 1; + } + break; + case 1: + if (ch != '\n') { + params += ch; + } else { + done = true; + state = 0; + } + break; + } + + if (done) { + m_stream->println("Cmd: " + cmd); + parseCommand(); + cmd = ""; + params = ""; + } + } + } +} + +void CmdServer::registerCmd(Cmd *cmdObj) { + if (cmdListIdx < NUM_OF_COMMANDS) { + cmdList[cmdListIdx] = cmdObj; + cmdListIdx++; + } else { + fatal(FATAL_NOT_ENOUGH_CMD_SLOTS); + } +} + +void CmdServer::parseCommand() { + //m_client.println("cmd: " + cmd); + //m_client.println("params: " + params); + + if (cmd.equalsIgnoreCase("help")) { + for (uint8_t i = 0; i < cmdListIdx; i++) { + Cmd *c = cmdList[i]; + m_stream->println(c->getCmdName() + " " + c->getHelp()); + } + m_stream->println("HELP List this help for all commands"); + } else { + bool found = false; + for (uint8_t i = 0; i < cmdListIdx; i++) { + // m_client.println("Check: " + cmdList[i]->getCmdName()); + if (cmdList[i]->getCmdName().equalsIgnoreCase(cmd)) { + found = true; + // m_client.println("cmd found"); + cmdList[i]->setClient(m_stream); + String res = cmdList[i]->exec(params); + m_stream->println(res); + break; + } + } + if (! found) + m_stream->println("command not found"); + } +} + + + + diff --git a/cmd.h b/cmd.h new file mode 100644 index 0000000..92869a9 --- /dev/null +++ b/cmd.h @@ -0,0 +1,46 @@ +#ifndef CMD_H_ +#define CMD_H_ + +#include +#include +#include +#include +#include + +static const uint8_t NUM_OF_COMMANDS = 10; + +class CmdServer; + +class Cmd { +public: + virtual ~Cmd() {}; + void setClient(Stream *p_stream) { m_stream = p_stream; }; + virtual String exec(String params) =0; + virtual String getCmdName() =0; + virtual String getHelp() =0; + virtual void registerYourself(CmdServer *cmdServer); +protected: + Stream *m_stream; +}; + + + +class CmdServer { +public: + CmdServer(Stream *p_stream); + void begin(); + void exec(); + void registerCmd(Cmd *cmdObj); +private: + void parseCommand(); + Stream *m_stream; + String cmd; + String params; + uint8_t cmdListIdx; + Cmd *cmdList[NUM_OF_COMMANDS]; +}; + + + + +#endif /* CMD_H_ */ diff --git a/fatal.cpp b/fatal.cpp new file mode 100644 index 0000000..aac91d9 --- /dev/null +++ b/fatal.cpp @@ -0,0 +1,11 @@ +#include +#include"fatal.h" + + +void fatal(uint8_t code) { + cli(); + // do something with the code and somehow show that there is a problem + while (1); +} + + diff --git a/fatal.h b/fatal.h new file mode 100644 index 0000000..a6c6581 --- /dev/null +++ b/fatal.h @@ -0,0 +1,15 @@ +#ifndef FATAL_H_ +#define FATAL_H_ + +#include + +const uint8_t FATAL_UNKNOWN = 1; +const uint8_t FATAL_NOT_ENOUGH_CMD_SLOTS = 2; + +const uint8_t FATAL_BUFFER_OVERFLOW = 10; + + +void fatal(uint8_t code); + + +#endif /* FATAL_H_ */ diff --git a/test.cpp b/test.cpp new file mode 100644 index 0000000..7667d38 --- /dev/null +++ b/test.cpp @@ -0,0 +1,7 @@ +#include +#include "test.h" + +String TestCmd::exec(String params) { + return "Result of testCmd"; +} + diff --git a/test.h b/test.h new file mode 100644 index 0000000..5dc257c --- /dev/null +++ b/test.h @@ -0,0 +1,14 @@ +#ifndef TEST_H_ +#define TEST_H_ + +#include "cmd.h" + +class TestCmd : public Cmd { +public: + virtual String getCmdName() { return "TEST"; } + virtual String getHelp() { return "Just a test command"; } + virtual String exec(String params); +}; + + +#endif /* TEST_H_ */ diff --git a/uptime.cpp b/uptime.cpp new file mode 100644 index 0000000..a02d82c --- /dev/null +++ b/uptime.cpp @@ -0,0 +1,45 @@ +#include +#include "uptime.h" + + +UptimeCmd::UptimeCmd(Uptime *uptime) : m_uptime(uptime) { + +} + +String UptimeCmd::exec(String params) { + return m_uptime->getDays() + String(" ") + + m_uptime->getHours() + String(":") + m_uptime->getMinutes() + String(":") + m_uptime->getSeconds(); +} + +Uptime::Uptime() : m_uptimeCmd(this), m_seconds(0), m_minutes(0), m_hours(0), m_days(0) { + +} + + +void Uptime::begin(CmdServer *cmdServer) { + m_uptimeCmd.registerYourself(cmdServer); +} + + +void Uptime::exec() { + static unsigned long lastMillis = 0; + + unsigned long currentMillis = millis(); + if (currentMillis >= (lastMillis + 1000)) { + m_seconds += ((currentMillis - lastMillis) / 1000); + if (m_seconds >= 60) { + m_seconds -= 60; + m_minutes++; + if (m_minutes >= 60) { + m_minutes -= 60; + m_hours++; + if (m_hours >= 24) { + m_hours -= 24; + m_days++; + } + } + } + + lastMillis = currentMillis; + } +} diff --git a/uptime.h b/uptime.h new file mode 100644 index 0000000..4608761 --- /dev/null +++ b/uptime.h @@ -0,0 +1,39 @@ +#ifndef UPTIME_H_ +#define UPTIME_H_ + +#include "cmd.h" + + +class Uptime; + + +class UptimeCmd : public Cmd { +public: + UptimeCmd(Uptime *uptime); + virtual String getCmdName() { return "UP"; } + virtual String getHelp() { return "Uptime"; } + virtual String exec(String params); +private: + Uptime *m_uptime; +}; + + +class Uptime { +public: + Uptime(); + void begin(CmdServer *cmdServer); + void exec(); + uint8_t getSeconds() { return m_seconds; }; + uint8_t getMinutes() { return m_minutes; }; + uint8_t getHours() { return m_hours; }; + uint8_t getDays() { return m_days; }; +private: + UptimeCmd m_uptimeCmd; + uint8_t m_seconds; + uint8_t m_minutes; + uint8_t m_hours; + uint16_t m_days; +}; + + +#endif /* TEST_H_ */