diff --git a/Config.cpp b/Config.cpp index c0f39ed..8d90f35 100644 --- a/Config.cpp +++ b/Config.cpp @@ -8,6 +8,7 @@ #include #include "Config.h" +#include "Resources.h" @@ -83,6 +84,42 @@ void Config::setULong(int pos, unsigned long value) { } } +unsigned char Config::getUChar(int pos) { + u_uchar u; + for (unsigned int i = 0; i < sizeof(unsigned char); i++) { + u.e[i] = EEPROM.read(pos + i); + } + return u.c; +} + +void Config::setUChar(int pos, unsigned char value) { + u_uchar u; + u.c = value; + for (unsigned int i = 0; i < sizeof(unsigned char); i++) { + EEPROM.write(pos + i, u.e[i]); + } +} + + +void Config::initialize() { + if (! Config::isInitialized()) { + Serial.println(getResource(CONFIG_INIT_KEY)); + 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::setBool(Config::THERMOMETER_DEBUG, true); + Config::setBool(Config::THERMOMETER_INFO, true); + + + Config::setUChar(Config::METERBUSCLIENT_ADDRESS, 0); + + Config::setMagic(); + } +} + + bool Config::isInitialized() { unsigned int magic = getUInt(MAGIC); return magic == MAGIC_TOKEN; diff --git a/Config.h b/Config.h index 9355e6b..50a6344 100644 --- a/Config.h +++ b/Config.h @@ -39,6 +39,11 @@ typedef union { uint8_t e[sizeof(bool)]; } u_bool; +typedef union { + unsigned char c; + uint8_t e[sizeof(unsigned char)]; +} u_uchar; + namespace Config { const unsigned int MAGIC_TOKEN = 0xDEADBEEF; @@ -49,6 +54,8 @@ namespace Config { const int THERMOMETER_CAL[4] = {12, 16, 20, 24}; // 4 x 4 const int THERMOMETER_DEBUG = 28; // 1 const int THERMOMETER_INFO = 29; // 1 + const int METERBUSCLIENT_ADDRESS = 30; // 1 + bool getBool(int pos); void setBool(int pos, bool value); @@ -58,6 +65,10 @@ namespace Config { void setUInt(int pos, unsigned int value); unsigned long getULong(int pos); void setULong(int pos, unsigned long value); + unsigned char getUChar(int pos); + void setUChar(int pos, unsigned char value); + + void initialize(); bool isInitialized(); diff --git a/Resources.h b/Resources.h index 03de30a..696c23d 100644 --- a/Resources.h +++ b/Resources.h @@ -41,7 +41,7 @@ const uint8_t SET_TEMPERATURE_DEBUG_2_KEY = 25; const uint8_t SET_TEMPERATURE_DEBUG_3_KEY = 26; const uint8_t CONF_COLON_KEY = 27; const uint8_t MODE_COLON_KEY = 28; -const uint8_t THERMOMETER_BEGIN_1_KEY = 29; +const uint8_t CONFIG_INIT_KEY = 29; const uint8_t STATE_0_KEY = 30; const uint8_t TO_STATE_1_KEY = 31; const uint8_t TO_STATE_10_KEY = 32; @@ -61,6 +61,7 @@ const uint8_t THERMCONFIG_HELP_KEY = 45; const uint8_t THERMVALUES_HELP_KEY = 46; const uint8_t THERMCALIBRATE_HELP_KEY = 47; const uint8_t CALIBRATION_ZEOR_MODE_HINT_KEY = 48; +const uint8_t MBC_CONFIG_HELP_KEY = 49; const String TEXT_RESOURCES[] = { @@ -113,6 +114,8 @@ const String TEXT_RESOURCES[] = { "Show thermometer measurement values", "Thermometer calibration operations", "No, no, we are in calibration zero mode, so directly switch to state 20", + "MeterBus Client Configuration", + }; diff --git a/ThermometerPro.cpp b/ThermometerPro.cpp index c52b64d..6c2a384 100644 --- a/ThermometerPro.cpp +++ b/ThermometerPro.cpp @@ -7,7 +7,7 @@ #include "spi.h" -// #define ENABLE_CONFIGURATION_INVALID_CMD 1 +#define ENABLE_CONFIGURATION_INVALID_CMD 1 static CmdServer cmdServer(&Serial); @@ -21,6 +21,9 @@ static Thermometer thermometer; void setup() { Serial.begin(9600); + + Config::initialize(); + spiInit(); cmdServer.begin(); diff --git a/meterBusClient.cpp b/meterBusClient.cpp new file mode 100644 index 0000000..c8530ff --- /dev/null +++ b/meterBusClient.cpp @@ -0,0 +1,97 @@ +/* + * meterBusClient.cpp + * + * Created on: 08.03.2014 + * Author: wn + */ + +#include "meterBusClient.h" + + +MeterBusClient::MeterBusClient() : m_address(0) { + +} + + + +void MeterBusClient::begin(CmdServer *cmdServer) { + Serial3.begin(1200); + + + setAddress(Config::getUChar(Config::METERBUSCLIENT_ADDRESS)); +} + +void MeterBusClient::setAddress(unsigned char a) { + Config::setUChar(Config::METERBUSCLIENT_ADDRESS, a); + m_address = a; +} + +unsigned char MeterBusClient::getAddress() { + return m_address; +} + + + +/* + * Single Character: E5h + * + * Short Frame: + * Start 10h + * C-Field + * A-Field + * Check-Sum + * Stop 16h + * + * Control Frame: + * Start 68h + * Length + * Length + * Start 68h + * C-Field + * A-Field + * CI-Field + * Check-Sum + * Stop 16h + * + * Long Frame: + * Start 68h + * Length + * Length + * Start 68h + * C-Field + * A-Field + * CI-Field + * User-Data + * Check-Sum + * Stop 16h + * + * Frames: + * SND_NKE + * Short Frame, C: 40h + * + * REQ_UD2 + * Short Frame, C: 5Bh + */ + + +void MeterBusClient::exec() { + static uint8_t state = 0; + bool done = false; + + if (Serial3.available()) { + int chi; + while ((chi = Serial3.read()) != -1) { + char ch = (char) chi; + switch (state) { + case 0: + break; + } + + if (done) { + } + } + } + +} + + diff --git a/meterBusClient.h b/meterBusClient.h new file mode 100644 index 0000000..18209fa --- /dev/null +++ b/meterBusClient.h @@ -0,0 +1,46 @@ +/* + * meterBusClient.h + * + * Created on: 08.03.2014 + * Author: wn + */ + +#ifndef METERBUSCLIENT_H_ +#define METERBUSCLIENT_H_ + + +#include "cmd.h" +#include "Config.h" +#include "Resources.h" + +class MeterBusClient; + + + +class MeterBusClientConfig : public Cmd { +public: + MeterBusClientConfig(MeterBusClient *meterBusClient) : m_meterBusClient(meterBusClient) {}; + virtual String getCmdName() { return "MBCC"; } + virtual String getHelp() { return getResource(MBC_CONFIG_HELP_KEY); } + virtual String exec(String params); +private: + MeterBusClient *m_meterBusClient; +}; + + + + +class MeterBusClient { +public: + MeterBusClient(); + void begin(CmdServer *cmdServer); + void exec(); + friend class MeterBusClientConfig; +private: + unsigned char m_address; + void setAddress(unsigned char address); + unsigned char getAddress(); +}; + + +#endif /* METERBUSCLIENT_H_ */ diff --git a/thermometer.cpp b/thermometer.cpp index e90dd1c..aeb1007 100644 --- a/thermometer.cpp +++ b/thermometer.cpp @@ -253,17 +253,6 @@ void Thermometer::begin(CmdServer *cmdServer) { AD7190_Calibrate(AD7190_MODE_CAL_INT_FULL, AD7190_CH_AIN1P_AINCOM); - if (! Config::isInitialized()) { - Serial.println(getResource(THERMOMETER_BEGIN_1_KEY)); - 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::setBool(Config::THERMOMETER_DEBUG, true); - Config::setBool(Config::THERMOMETER_INFO, true); - Config::setMagic(); - } setAlpha(Config::getFloat(Config::THERMOMETER_ALPHA)); setPeriodMeasure(Config::getULong(Config::THERMOMETER_PERIOD)); @@ -273,12 +262,7 @@ void Thermometer::begin(CmdServer *cmdServer) { setDebug(Config::getBool(Config::THERMOMETER_DEBUG)); setInfo(Config::getBool(Config::THERMOMETER_INFO)); - //setCalibrateFactor(0, 1.002999); - //setCalibrateFactor(1, 1.001804); - //setCalibrateFactor(2, 1.000794); - //setCalibrateFactor(3, 1.001071); - // prepare prepareAdc(); } @@ -390,6 +374,7 @@ void Thermometer::exec() { if (currentMillis >= (lastMillis + getPeriodMeasure())) { lastMillis = currentMillis; state = 0; + Serial3.println("Tick"); } break;