start integrating MeterBus client
This commit is contained in:
37
Config.cpp
37
Config.cpp
@ -8,6 +8,7 @@
|
||||
|
||||
#include <EEPROM.h>
|
||||
#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;
|
||||
|
11
Config.h
11
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();
|
||||
|
||||
|
@ -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",
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
@ -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();
|
||||
|
97
meterBusClient.cpp
Normal file
97
meterBusClient.cpp
Normal file
@ -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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
46
meterBusClient.h
Normal file
46
meterBusClient.h
Normal file
@ -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_ */
|
@ -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;
|
||||
|
||||
|
Reference in New Issue
Block a user