Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
2ada607813 | |||
ebc835c794 | |||
45bdbe0a57 | |||
0f16b2c3a9 | |||
84f06c66e8 | |||
db5cb1829a | |||
26b47b1159 | |||
ec6b7c0647 | |||
13d19d4e15 | |||
854d2f202a | |||
b333be0cd9 |
1
.hgtags
1
.hgtags
@ -1 +1,2 @@
|
|||||||
55e33ac01375df1e3e295262fcc03a568ddf48e3 before_switch_to_arm
|
55e33ac01375df1e3e295262fcc03a568ddf48e3 before_switch_to_arm
|
||||||
|
4c0a748c4e0681d22d1d3e1faf4e09266a64dc8c MBC_WORKING_FIRST_FRAME_ANSWERED
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
<configuration id="it.baeyens.arduino.core.toolChain.release.1909314976" name="Release">
|
<configuration id="it.baeyens.arduino.core.toolChain.release.1909314976" name="Release">
|
||||||
<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
|
<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
|
||||||
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
|
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
|
||||||
<provider class="it.baeyens.arduino.toolchain.ArduinoLanguageProvider" console="false" env-hash="-2011256820" id="it.baeyens.arduino.languageSettingsProvider" keep-relative-paths="false" name="Arduino Compiler Settings" parameter="${COMMAND} -E -P -v -dD ${INPUTS}" prefer-non-shared="true">
|
<provider class="it.baeyens.arduino.toolchain.ArduinoLanguageProvider" console="false" env-hash="-1881042430836" id="it.baeyens.arduino.languageSettingsProvider" keep-relative-paths="false" name="Arduino Compiler Settings" parameter="${COMMAND} -E -P -v -dD ${INPUTS}" prefer-non-shared="true">
|
||||||
<language-scope id="org.eclipse.cdt.core.gcc"/>
|
<language-scope id="org.eclipse.cdt.core.gcc"/>
|
||||||
<language-scope id="org.eclipse.cdt.core.g++"/>
|
<language-scope id="org.eclipse.cdt.core.g++"/>
|
||||||
</provider>
|
</provider>
|
||||||
|
@ -171,7 +171,6 @@ unsigned char AD7190_Init(void)
|
|||||||
// TIME_DelayMs(1);
|
// TIME_DelayMs(1);
|
||||||
delay(1); // arduino function
|
delay(1); // arduino function
|
||||||
regVal = AD7190_GetRegisterValue(AD7190_REG_ID, 1, 1);
|
regVal = AD7190_GetRegisterValue(AD7190_REG_ID, 1, 1);
|
||||||
Serial.print(String("regVal: ") + regVal + "\n");
|
|
||||||
if( (regVal & AD7190_ID_MASK) != ID_AD7190)
|
if( (regVal & AD7190_ID_MASK) != ID_AD7190)
|
||||||
{
|
{
|
||||||
status = 0;
|
status = 0;
|
||||||
|
18
Config.cpp
18
Config.cpp
@ -102,20 +102,22 @@ void Config::setUChar(int pos, unsigned char value) {
|
|||||||
|
|
||||||
|
|
||||||
void Config::initialize() {
|
void Config::initialize() {
|
||||||
if (! Config::isInitialized()) {
|
if (! isInitialized()) {
|
||||||
Serial.println(getResource(CONFIG_INIT_KEY));
|
Serial.println(getResource(CONFIG_INIT_KEY));
|
||||||
Config::setFloat(Config::THERMOMETER_ALPHA, 1.0);
|
setFloat(THERMOMETER_ALPHA, 1.0);
|
||||||
Config::setULong(Config::THERMOMETER_PERIOD, 1000);
|
setULong(THERMOMETER_PERIOD, 1000);
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
Config::setFloat(Config::THERMOMETER_CAL[i], 1.0);
|
setFloat(THERMOMETER_CAL[i], 1.0);
|
||||||
}
|
}
|
||||||
Config::setBool(Config::THERMOMETER_DEBUG, true);
|
setBool(THERMOMETER_DEBUG, false);
|
||||||
Config::setBool(Config::THERMOMETER_INFO, true);
|
setBool(THERMOMETER_INFO, false);
|
||||||
|
|
||||||
|
|
||||||
Config::setUChar(Config::METERBUSCLIENT_ADDRESS, 0);
|
setUChar(METERBUSCLIENT_ADDRESS, 0);
|
||||||
|
setBool(METERBUSCLIENT_DEBUG, false);
|
||||||
|
setBool(METERBUSCLIENT_INFO, false);
|
||||||
|
|
||||||
Config::setMagic();
|
setMagic();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
3
Config.h
3
Config.h
@ -55,6 +55,9 @@ namespace Config {
|
|||||||
const int THERMOMETER_DEBUG = 28; // 1
|
const int THERMOMETER_DEBUG = 28; // 1
|
||||||
const int THERMOMETER_INFO = 29; // 1
|
const int THERMOMETER_INFO = 29; // 1
|
||||||
const int METERBUSCLIENT_ADDRESS = 30; // 1
|
const int METERBUSCLIENT_ADDRESS = 30; // 1
|
||||||
|
const int METERBUSCLIENT_DEBUG = 31; // 1
|
||||||
|
const int METERBUSCLIENT_INFO = 32; // 1
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool getBool(int pos);
|
bool getBool(int pos);
|
||||||
|
@ -9,13 +9,95 @@
|
|||||||
#include "Resources.h"
|
#include "Resources.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define USE_REAL_TEXTS 1
|
||||||
|
|
||||||
//String TEXT;
|
|
||||||
|
#ifndef USE_REAL_TEXTS
|
||||||
|
String TEXT;
|
||||||
|
#else
|
||||||
|
const String TEXT_RESOURCES[] = {
|
||||||
|
"Text1",
|
||||||
|
"PeriodMeasure (ms): ",
|
||||||
|
"Alpha: ",
|
||||||
|
"Calibration: ",
|
||||||
|
"Info: ",
|
||||||
|
"Debug: ",
|
||||||
|
"COMPILE_TIME_DEBUG: ",
|
||||||
|
"TimeOuts: ",
|
||||||
|
"Cycles: ",
|
||||||
|
"Period (ms): ",
|
||||||
|
"Alpha: ",
|
||||||
|
"index: ",
|
||||||
|
"cal: ",
|
||||||
|
"t: ",
|
||||||
|
"ts: ",
|
||||||
|
", ",
|
||||||
|
" enable : enable the calibration mode",
|
||||||
|
" disable : disable the calibration mode",
|
||||||
|
" show : show parameters for calibration process",
|
||||||
|
" r : set value of calibration resistor",
|
||||||
|
" start <0..3>: start calibration on channel",
|
||||||
|
" stop <0..3>: stop calibration on channel",
|
||||||
|
" enabled : ",
|
||||||
|
" r_cal : ",
|
||||||
|
"setTemperature: i=",
|
||||||
|
"t=",
|
||||||
|
"t_smoothed=",
|
||||||
|
"CONF: ",
|
||||||
|
"MODE: ",
|
||||||
|
"Initializing EEPROM",
|
||||||
|
"State 0",
|
||||||
|
"Switching to State 1",
|
||||||
|
"Switching to State 10",
|
||||||
|
"State 9",
|
||||||
|
"Timeout: ",
|
||||||
|
"State 10",
|
||||||
|
"No, no, we are in calibration high mode, so directly switch to state 20",
|
||||||
|
"Switching to state 11",
|
||||||
|
"r_avg on channel ",
|
||||||
|
"calibration factor: ",
|
||||||
|
"Calibration stopped",
|
||||||
|
"Save calibration factor for channel ",
|
||||||
|
": ",
|
||||||
|
"HELP List this help for all commands",
|
||||||
|
"command not found",
|
||||||
|
"Thermometer configuration operations",
|
||||||
|
"Show thermometer measurement values",
|
||||||
|
"Thermometer calibration operations",
|
||||||
|
"No, no, we are in calibration zero mode, so directly switch to state 20",
|
||||||
|
"MeterBus Client Configuration",
|
||||||
|
"Invalid frame",
|
||||||
|
"No valid frame available",
|
||||||
|
"Invalid checksum",
|
||||||
|
"Not for me but for ",
|
||||||
|
"Unhandled frame",
|
||||||
|
"s: ",
|
||||||
|
"l: ",
|
||||||
|
"c: ",
|
||||||
|
"a: ",
|
||||||
|
"ci: ",
|
||||||
|
"u: ",
|
||||||
|
" ",
|
||||||
|
"frames: ",
|
||||||
|
"my frames: ",
|
||||||
|
"invalid frames: ",
|
||||||
|
"invalid checksum: ",
|
||||||
|
"mbus address: ",
|
||||||
|
"collisions: ",
|
||||||
|
"r: ",
|
||||||
|
"setR: i=",
|
||||||
|
"r=",
|
||||||
|
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
const String& getResource(uint8_t key) {
|
const String& getResource(uint8_t key) {
|
||||||
//TEXT = String("T") + key + String(": ");
|
#ifndef USE_REAL_TEXTS
|
||||||
//return TEXT;
|
TEXT = String(" T") + key + String(": ");
|
||||||
|
return TEXT;
|
||||||
|
#else
|
||||||
return TEXT_RESOURCES[key];
|
return TEXT_RESOURCES[key];
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
77
Resources.h
77
Resources.h
@ -62,62 +62,27 @@ const uint8_t THERMVALUES_HELP_KEY = 46;
|
|||||||
const uint8_t THERMCALIBRATE_HELP_KEY = 47;
|
const uint8_t THERMCALIBRATE_HELP_KEY = 47;
|
||||||
const uint8_t CALIBRATION_ZEOR_MODE_HINT_KEY = 48;
|
const uint8_t CALIBRATION_ZEOR_MODE_HINT_KEY = 48;
|
||||||
const uint8_t MBC_CONFIG_HELP_KEY = 49;
|
const uint8_t MBC_CONFIG_HELP_KEY = 49;
|
||||||
|
const uint8_t MBC_INVALID_FRAME_KEY = 50;
|
||||||
|
const uint8_t MBC_NO_VALID_FRAME_KEY = 51;
|
||||||
const String TEXT_RESOURCES[] = {
|
const uint8_t MBC_INVALID_CHECKSUM_KEY = 52;
|
||||||
"Text1",
|
const uint8_t MBC_NOT_FOR_ME_KEY = 53;
|
||||||
"PeriodMeasure (ms): ",
|
const uint8_t MBC_UNHANDLED_FRAME_KEY = 54;
|
||||||
"Alpha: ",
|
const uint8_t MBC_START_DELIMITER_KEY = 55;
|
||||||
"Calibration: ",
|
const uint8_t MBC_LENGTH_KEY = 56;
|
||||||
"Info: ",
|
const uint8_t MBC_CFIELD_KEY = 57;
|
||||||
"Debug: ",
|
const uint8_t MBC_ADDRESS_KEY = 58;
|
||||||
"COMPILE_TIME_DEBUG: ",
|
const uint8_t MBC_CIFIELD_KEY = 59;
|
||||||
"TimeOuts: ",
|
const uint8_t MBC_USERDATA_KEY = 60;
|
||||||
"Cycles: ",
|
const uint8_t SPACE_KEY = 61;
|
||||||
"Period (ms): ",
|
const uint8_t MBC_FRAMES_KEY = 62;
|
||||||
"Alpha: ",
|
const uint8_t MBC_MYFRAMES_KEY = 63;
|
||||||
"index: ",
|
const uint8_t MBC_INVALID_FRAMES_KEY = 64;
|
||||||
"cal: ",
|
const uint8_t MBC_INVALID_CHECKSUM_CNT_KEY = 65;
|
||||||
"t: ",
|
const uint8_t MBC_ADDRESS_LONG_KEY = 66;
|
||||||
"ts: ",
|
const uint8_t MBC_COLLISION_CNT_KEY = 67;
|
||||||
", ",
|
const uint8_t R_KEY = 68;
|
||||||
" enable : enable the calibration mode",
|
const uint8_t SET_R_KEY = 69;
|
||||||
" disable : disable the calibration mode",
|
const uint8_t R_EQUALS_KEY = 70;
|
||||||
" show : show parameters for calibration process",
|
|
||||||
" r : set value of calibration resistor",
|
|
||||||
" start <0..3>: start calibration on channel",
|
|
||||||
" stop <0..3>: stop calibration on channel",
|
|
||||||
" enabled : ",
|
|
||||||
" r_cal : ",
|
|
||||||
"setTemperature: i=",
|
|
||||||
"t=",
|
|
||||||
"t_smoothed=",
|
|
||||||
"CONF: ",
|
|
||||||
"MODE: ",
|
|
||||||
"Initializing EEPROM",
|
|
||||||
"State 0",
|
|
||||||
"Switching to State 1",
|
|
||||||
"Switching to State 10",
|
|
||||||
"State 9",
|
|
||||||
"Timeout: ",
|
|
||||||
"State 10",
|
|
||||||
"No, no, we are in calibration high mode, so directly switch to state 20",
|
|
||||||
"Switching to state 11",
|
|
||||||
"r_avg on channel ",
|
|
||||||
"calibration factor: ",
|
|
||||||
"Calibration stopped",
|
|
||||||
"Save calibration factor for channel ",
|
|
||||||
": ",
|
|
||||||
"HELP List this help for all commands",
|
|
||||||
"command not found",
|
|
||||||
"Thermometer configuration operations",
|
|
||||||
"Show thermometer measurement values",
|
|
||||||
"Thermometer calibration operations",
|
|
||||||
"No, no, we are in calibration zero mode, so directly switch to state 20",
|
|
||||||
"MeterBus Client Configuration",
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const String& getResource(uint8_t key);
|
const String& getResource(uint8_t key);
|
||||||
|
@ -5,33 +5,37 @@
|
|||||||
#include "thermometer.h"
|
#include "thermometer.h"
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
#include "spi.h"
|
#include "spi.h"
|
||||||
|
#include "meterBusClient.h"
|
||||||
|
|
||||||
|
|
||||||
#define ENABLE_CONFIGURATION_INVALID_CMD 1
|
const unsigned char OK_LED_PIN = 2;
|
||||||
|
|
||||||
|
|
||||||
static CmdServer cmdServer(&Serial);
|
static CmdServer cmdServer(&Serial);
|
||||||
#ifdef ENABLE_CONFIGURATION_INVALID_CMD
|
static ConfigInvalidateCmd configInvalidateCmd;
|
||||||
static ConfigInvalidateCmd configInvalidateCmd;
|
|
||||||
#endif
|
|
||||||
static Uptime uptime;
|
static Uptime uptime;
|
||||||
static Thermometer thermometer;
|
static Thermometer thermometer;
|
||||||
|
static MeterBusClient meterBusClient;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
pinMode(OK_LED_PIN, OUTPUT);
|
||||||
|
digitalWrite(OK_LED_PIN, LOW);
|
||||||
|
|
||||||
|
// delay(5000);
|
||||||
|
|
||||||
|
Serial.begin(115200);
|
||||||
|
|
||||||
Config::initialize();
|
Config::initialize();
|
||||||
|
|
||||||
|
|
||||||
spiInit();
|
spiInit();
|
||||||
|
|
||||||
cmdServer.begin();
|
cmdServer.begin();
|
||||||
#ifdef ENABLE_CONFIGURATION_INVALID_CMD
|
|
||||||
configInvalidateCmd.registerYourself(&cmdServer);
|
configInvalidateCmd.registerYourself(&cmdServer);
|
||||||
#endif
|
|
||||||
uptime.begin(&cmdServer);
|
uptime.begin(&cmdServer);
|
||||||
thermometer.begin(&cmdServer);
|
thermometer.begin(&cmdServer);
|
||||||
|
meterBusClient.begin(&cmdServer, &thermometer, &uptime);
|
||||||
|
|
||||||
|
digitalWrite(OK_LED_PIN, HIGH);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -39,4 +43,5 @@ void loop() {
|
|||||||
cmdServer.exec();
|
cmdServer.exec();
|
||||||
uptime.exec();
|
uptime.exec();
|
||||||
thermometer.exec();
|
thermometer.exec();
|
||||||
|
meterBusClient.exec();
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
#include <avr/interrupt.h>
|
|
||||||
#include"fatal.h"
|
#include"fatal.h"
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
void fatal(uint8_t code) {
|
void fatal(uint8_t code) {
|
||||||
// cli();
|
// cli();
|
||||||
// do something with the code and somehow show that there is a problem
|
// do something with the code and somehow show that there is a problem
|
||||||
|
Serial.print("Fatal: "); Serial.println(code);
|
||||||
while (1);
|
while (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,20 +5,76 @@
|
|||||||
* Author: wn
|
* Author: wn
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#include "meterBusClient.h"
|
#include "meterBusClient.h"
|
||||||
|
|
||||||
|
|
||||||
MeterBusClient::MeterBusClient() : m_address(0) {
|
const bool MBC_COMPILE_TIME_DEBUG = false;
|
||||||
|
|
||||||
|
|
||||||
|
String MeterBusClientConfig::exec(String params) {
|
||||||
|
String res = "done";
|
||||||
|
|
||||||
|
int space = params.indexOf(' ');
|
||||||
|
String p1 = "";
|
||||||
|
char pb1[128];
|
||||||
|
if (space != -1) {
|
||||||
|
params.toCharArray(pb1, 128, space+1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (params.startsWith("a ") && (space != -1)) {
|
||||||
|
unsigned int a = atoi(pb1);
|
||||||
|
m_meterBusClient->setAddress(a);
|
||||||
|
Serial.print("Address is now "); Serial.println(a);
|
||||||
|
Serial.print("MBC is at "); Serial.println((long) m_meterBusClient, 16);
|
||||||
|
} else if (params.startsWith("debug ") && (space != -1)) {
|
||||||
|
bool b = (strcmp(pb1, "on") == 0);
|
||||||
|
m_meterBusClient->setDebug(b);
|
||||||
|
} else if (params.startsWith("info ") && (space != -1)) {
|
||||||
|
bool b = (strcmp(pb1, "on") == 0);
|
||||||
|
m_meterBusClient->setInfo(b);
|
||||||
|
} else if (params.startsWith("show")) {
|
||||||
|
Serial.print(getResource(MBC_FRAMES_KEY)); Serial.print(m_meterBusClient->m_frameCnt); Serial.println();
|
||||||
|
Serial.print(getResource(MBC_MYFRAMES_KEY)); Serial.print(m_meterBusClient->m_myFrameCnt); Serial.println();
|
||||||
|
Serial.print(getResource(MBC_INVALID_FRAMES_KEY)); Serial.print(m_meterBusClient->m_invalidFrameCnt); Serial.println();
|
||||||
|
Serial.print(getResource(MBC_INVALID_CHECKSUM_CNT_KEY)); Serial.print(m_meterBusClient->m_invalidChecksum); Serial.println();
|
||||||
|
Serial.print(getResource(MBC_COLLISION_CNT_KEY)); Serial.print(m_meterBusClient->m_collisionCnt); Serial.println();
|
||||||
|
Serial.print(getResource(MBC_ADDRESS_LONG_KEY)); Serial.print(m_meterBusClient->getAddress()); Serial.println();
|
||||||
|
} else {
|
||||||
|
res = "subcommand not found";
|
||||||
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void MeterBusClient::begin(CmdServer *cmdServer) {
|
|
||||||
Serial3.begin(1200);
|
|
||||||
|
MeterBusClient::MeterBusClient() : m_meterBusClientConfig(this), m_address(0),
|
||||||
|
m_frameCnt(0), m_accessCnt(0), m_myFrameCnt(0), m_invalidFrameCnt(0), m_invalidChecksum(0),
|
||||||
|
m_collisionCnt(0) {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void MeterBusClient::begin(CmdServer *cmdServer, Thermometer *thermometer, Uptime *uptime) {
|
||||||
|
Serial.print("I'm at "); Serial.println((long)this, 16);
|
||||||
|
|
||||||
|
|
||||||
|
m_meterBusClientConfig.registerYourself(cmdServer);
|
||||||
|
m_thermometer = thermometer;
|
||||||
|
m_uptime = uptime;
|
||||||
|
|
||||||
|
//Serial3.begin(1200);
|
||||||
|
//Serial3.begin(2400, SERIAL_8E1);
|
||||||
|
Serial3.begin(2400);
|
||||||
|
UART2_C1 |= UART_C1_PE | UART_C1_M;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
setAddress(Config::getUChar(Config::METERBUSCLIENT_ADDRESS));
|
setAddress(Config::getUChar(Config::METERBUSCLIENT_ADDRESS));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MeterBusClient::setAddress(unsigned char a) {
|
void MeterBusClient::setAddress(unsigned char a) {
|
||||||
@ -30,6 +86,31 @@ unsigned char MeterBusClient::getAddress() {
|
|||||||
return m_address;
|
return m_address;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MeterBusClient::setDebug(bool b) {
|
||||||
|
Config::setBool(Config::METERBUSCLIENT_DEBUG, b);
|
||||||
|
m_debug = b;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MeterBusClient::getDebug() {
|
||||||
|
return m_debug;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MeterBusClient::setInfo(bool b) {
|
||||||
|
Config::setBool(Config::METERBUSCLIENT_INFO, b);
|
||||||
|
m_info = b;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MeterBusClient::getInfo() {
|
||||||
|
return m_info;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char MeterBusClient::getStatus() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char MeterBusClient::getAccessCnt() {
|
||||||
|
return m_accessCnt;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -44,7 +125,7 @@ unsigned char MeterBusClient::getAddress() {
|
|||||||
*
|
*
|
||||||
* Control Frame:
|
* Control Frame:
|
||||||
* Start 68h
|
* Start 68h
|
||||||
* Length
|
* Length = 3
|
||||||
* Length
|
* Length
|
||||||
* Start 68h
|
* Start 68h
|
||||||
* C-Field
|
* C-Field
|
||||||
@ -74,24 +155,239 @@ unsigned char MeterBusClient::getAddress() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
void MeterBusClient::exec() {
|
bool isChecksumValid(MeterBusFrame frame) {
|
||||||
static uint8_t state = 0;
|
unsigned char ctrlsum = frame.aField + frame.cField + frame.ciField;
|
||||||
bool done = false;
|
for (int i = 0; i < (frame.length - 3); i++) {
|
||||||
|
ctrlsum += frame.userData[i];
|
||||||
if (Serial3.available()) {
|
}
|
||||||
int chi;
|
return (ctrlsum == frame.checksum);
|
||||||
while ((chi = Serial3.read()) != -1) {
|
}
|
||||||
char ch = (char) chi;
|
|
||||||
switch (state) {
|
|
||||||
case 0:
|
|
||||||
break;
|
bool MeterBusClient::handleFrame() {
|
||||||
}
|
bool res = false;
|
||||||
|
m_frameCnt++;
|
||||||
if (done) {
|
if (! isChecksumValid(m_frame)) {
|
||||||
}
|
Serial.println(getResource(MBC_INVALID_CHECKSUM_KEY));
|
||||||
}
|
m_invalidChecksum++;
|
||||||
|
} else if (m_frame.aField != getAddress()) {
|
||||||
|
if (MBC_COMPILE_TIME_DEBUG && getDebug()) { Serial.print(getResource(MBC_NOT_FOR_ME_KEY)); Serial.print((int)m_frame.aField, 16); Serial.println();}
|
||||||
|
} else {
|
||||||
|
m_myFrameCnt++;
|
||||||
|
// handle the frame
|
||||||
|
if (MBC_COMPILE_TIME_DEBUG && getDebug()) { Serial.print(getResource(MBC_START_DELIMITER_KEY)); Serial.println(m_frame.startDelimiter, 16);}
|
||||||
|
if (MBC_COMPILE_TIME_DEBUG && getDebug()) { Serial.print(getResource(MBC_LENGTH_KEY)); Serial.println(m_frame.length, 16);}
|
||||||
|
if (MBC_COMPILE_TIME_DEBUG && getDebug()) { Serial.print(getResource(MBC_CFIELD_KEY)); Serial.println(m_frame.cField, 16);}
|
||||||
|
if (MBC_COMPILE_TIME_DEBUG && getDebug()) { Serial.print(getResource(MBC_ADDRESS_KEY)); Serial.println(m_frame.aField, 16);}
|
||||||
|
if (MBC_COMPILE_TIME_DEBUG && getDebug()) { Serial.print(getResource(MBC_CIFIELD_KEY)); Serial.println(m_frame.ciField, 16);}
|
||||||
|
|
||||||
|
if (m_frame.length > 3) {
|
||||||
|
if (MBC_COMPILE_TIME_DEBUG && getDebug()) { Serial.print(getResource(MBC_USERDATA_KEY));}
|
||||||
|
for (unsigned char i = 0; i < (m_frame.length - 3); i++) {
|
||||||
|
if (MBC_COMPILE_TIME_DEBUG && getDebug()) { Serial.print(m_frame.userData[i], 16);}
|
||||||
|
if (MBC_COMPILE_TIME_DEBUG && getDebug()) { Serial.print(getResource(SPACE_KEY));}
|
||||||
|
}
|
||||||
|
if (MBC_COMPILE_TIME_DEBUG && getDebug()) { Serial.println();}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (m_frame.startDelimiter == 0x10) {
|
||||||
|
if (m_frame.cField == 0x40) {
|
||||||
|
SND_NKE();
|
||||||
|
res = true;
|
||||||
|
} else if (m_frame.cField == 0x5b) {
|
||||||
|
m_accessCnt++;
|
||||||
|
REQ_UD2();
|
||||||
|
res = true;
|
||||||
|
} else {
|
||||||
|
Serial.println(getResource(MBC_UNHANDLED_FRAME_KEY));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
STATE_START, STATE_IDLE, STATE_SHORT_FRAME, STATE_LONG_CTRL_FRAME, STATE_HANDLE, STATE_INVALID, STATE_DONE,
|
||||||
|
STATE_DELAYED_RESPONSE, STATE_DELAY, STATE_RESPONSE, STATE_RECEIVE_ECHO
|
||||||
|
} e_meterBusClientState;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
SUBSTATE_LENGTH, SUBSTATE_LENGTH_REPEAT, SUBSTATE_START_REPEAT, SUBSTATE_C_FIELD, SUBSTATE_A_FIELD, SUBSTATE_CI_FIELD,
|
||||||
|
SUBSTATE_USERDATA, SUBSTATE_CHECKSUM, SUBSTATE_STOP
|
||||||
|
} e_meterBusClientSubState;
|
||||||
|
|
||||||
|
|
||||||
|
void MeterBusClient::exec() {
|
||||||
|
static e_meterBusClientState state = STATE_IDLE;
|
||||||
|
static e_meterBusClientSubState subState = SUBSTATE_LENGTH;
|
||||||
|
static unsigned char userDataCnt = 0;
|
||||||
|
static unsigned int echoReceiveCnt = 0;
|
||||||
|
static unsigned long receivedDoneTimestamp;
|
||||||
|
|
||||||
|
int chi = Serial3.read();
|
||||||
|
//if (chi != -1) {
|
||||||
|
// Serial.print("Received: "); Serial.println(chi, 16);
|
||||||
|
//}
|
||||||
|
char ch = (char) chi;
|
||||||
|
|
||||||
|
switch (state) {
|
||||||
|
case STATE_START:
|
||||||
|
m_frame.startDelimiter = 0;
|
||||||
|
m_frame.length = 0;
|
||||||
|
m_frame.cField = 0;
|
||||||
|
m_frame.aField = 0;
|
||||||
|
m_frame.ciField = 0;
|
||||||
|
//memset(m_frame.userData, 0, sizeof(m_frame.userData));
|
||||||
|
m_frame.valid = false;
|
||||||
|
state = STATE_IDLE;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case STATE_IDLE:
|
||||||
|
if (chi != -1) {
|
||||||
|
if (ch == 0x10) {
|
||||||
|
m_frame.startDelimiter = 0x10;
|
||||||
|
state = STATE_SHORT_FRAME;
|
||||||
|
subState = SUBSTATE_C_FIELD;
|
||||||
|
} else if (ch == 0x68) {
|
||||||
|
m_frame.startDelimiter = 0x68;
|
||||||
|
state = STATE_LONG_CTRL_FRAME;
|
||||||
|
subState = SUBSTATE_LENGTH;
|
||||||
|
} else {
|
||||||
|
state = STATE_INVALID;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case STATE_SHORT_FRAME:
|
||||||
|
if (chi != -1) {
|
||||||
|
if (subState == SUBSTATE_C_FIELD) {
|
||||||
|
m_frame.cField = ch;
|
||||||
|
subState = SUBSTATE_A_FIELD;
|
||||||
|
} else if (subState == SUBSTATE_A_FIELD) {
|
||||||
|
m_frame.aField = ch;
|
||||||
|
subState = SUBSTATE_CHECKSUM;
|
||||||
|
} else if (subState == SUBSTATE_CHECKSUM) {
|
||||||
|
m_frame.checksum = ch;
|
||||||
|
subState = SUBSTATE_STOP;
|
||||||
|
} else if (subState == SUBSTATE_STOP) {
|
||||||
|
if (ch == 0x16) {
|
||||||
|
m_frame.valid = true;
|
||||||
|
state = STATE_HANDLE;
|
||||||
|
} else {
|
||||||
|
state = STATE_INVALID;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case STATE_LONG_CTRL_FRAME:
|
||||||
|
if (chi != -1) {
|
||||||
|
if (subState == SUBSTATE_LENGTH) {
|
||||||
|
m_frame.length = ch;
|
||||||
|
if ((m_frame.length < 3) || (m_frame.length > 252)) {
|
||||||
|
state = STATE_INVALID;
|
||||||
|
} else {
|
||||||
|
subState = SUBSTATE_LENGTH_REPEAT;
|
||||||
|
}
|
||||||
|
} else if (subState == SUBSTATE_LENGTH_REPEAT) {
|
||||||
|
if (ch == m_frame.length) {
|
||||||
|
subState = SUBSTATE_START_REPEAT;
|
||||||
|
} else {
|
||||||
|
state = STATE_INVALID;
|
||||||
|
}
|
||||||
|
} else if (subState == SUBSTATE_START_REPEAT) {
|
||||||
|
if (ch == 0x68) {
|
||||||
|
subState = SUBSTATE_C_FIELD;
|
||||||
|
} else {
|
||||||
|
state = STATE_INVALID;
|
||||||
|
}
|
||||||
|
} else if (subState == SUBSTATE_C_FIELD) {
|
||||||
|
m_frame.cField = ch;
|
||||||
|
subState = SUBSTATE_A_FIELD;
|
||||||
|
} else if (subState == SUBSTATE_A_FIELD) {
|
||||||
|
m_frame.aField = ch;
|
||||||
|
subState = SUBSTATE_CI_FIELD;
|
||||||
|
} else if (subState == SUBSTATE_CI_FIELD) {
|
||||||
|
m_frame.ciField = ch;
|
||||||
|
if (m_frame.length == 3) {
|
||||||
|
subState = SUBSTATE_CHECKSUM;
|
||||||
|
} else {
|
||||||
|
subState = SUBSTATE_USERDATA;
|
||||||
|
userDataCnt = 0;
|
||||||
|
}
|
||||||
|
} else if (subState == SUBSTATE_USERDATA) {
|
||||||
|
m_frame.userData[userDataCnt] = ch;
|
||||||
|
userDataCnt++;
|
||||||
|
if (userDataCnt >= (m_frame.length - 3)) {
|
||||||
|
subState = SUBSTATE_CHECKSUM;
|
||||||
|
}
|
||||||
|
} else if (subState == SUBSTATE_CHECKSUM) {
|
||||||
|
m_frame.checksum = ch;
|
||||||
|
subState = SUBSTATE_STOP;
|
||||||
|
} else if (subState == SUBSTATE_STOP) {
|
||||||
|
if (ch == 0x16) {
|
||||||
|
m_frame.valid = true;
|
||||||
|
state = STATE_HANDLE;
|
||||||
|
} else {
|
||||||
|
state = STATE_INVALID;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case STATE_INVALID:
|
||||||
|
Serial.println("INVALID FRAME");
|
||||||
|
m_invalidFrameCnt++;
|
||||||
|
state = STATE_START;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case STATE_HANDLE:
|
||||||
|
state = STATE_DONE;
|
||||||
|
if (m_frame.valid) {
|
||||||
|
receivedDoneTimestamp = millis();
|
||||||
|
if (handleFrame()) {
|
||||||
|
state = STATE_DELAY;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case STATE_DELAY:
|
||||||
|
if ((receivedDoneTimestamp + RESPONSE_DELAY) < millis()) {
|
||||||
|
state = STATE_DELAYED_RESPONSE;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case STATE_DELAYED_RESPONSE:
|
||||||
|
for (unsigned int i = 0; i < m_sendBufferLen; i++) {
|
||||||
|
Serial3.write(m_sendBuffer[i]);
|
||||||
|
}
|
||||||
|
echoReceiveCnt = 0;
|
||||||
|
state = STATE_RECEIVE_ECHO;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case STATE_RECEIVE_ECHO:
|
||||||
|
if (chi != -1) {
|
||||||
|
//Serial.println("echo character");
|
||||||
|
if (ch == m_sendBuffer[echoReceiveCnt]) {
|
||||||
|
echoReceiveCnt++;
|
||||||
|
if (echoReceiveCnt == m_sendBufferLen) {
|
||||||
|
state = STATE_DONE;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
m_collisionCnt++;
|
||||||
|
state = STATE_DONE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case STATE_DONE:
|
||||||
|
// may be another useful message
|
||||||
|
state = STATE_START;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -9,9 +9,16 @@
|
|||||||
#define METERBUSCLIENT_H_
|
#define METERBUSCLIENT_H_
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#include "cmd.h"
|
#include "cmd.h"
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
#include "Resources.h"
|
#include "Resources.h"
|
||||||
|
#include "Thermometer.h"
|
||||||
|
#include "uptime.h"
|
||||||
|
|
||||||
|
|
||||||
|
const unsigned long RESPONSE_DELAY = 50;
|
||||||
|
|
||||||
|
|
||||||
class MeterBusClient;
|
class MeterBusClient;
|
||||||
|
|
||||||
@ -19,7 +26,7 @@ class MeterBusClient;
|
|||||||
|
|
||||||
class MeterBusClientConfig : public Cmd {
|
class MeterBusClientConfig : public Cmd {
|
||||||
public:
|
public:
|
||||||
MeterBusClientConfig(MeterBusClient *meterBusClient) : m_meterBusClient(meterBusClient) {};
|
MeterBusClientConfig(MeterBusClient *meterBusClient) : m_meterBusClient(meterBusClient) { };
|
||||||
virtual String getCmdName() { return "MBCC"; }
|
virtual String getCmdName() { return "MBCC"; }
|
||||||
virtual String getHelp() { return getResource(MBC_CONFIG_HELP_KEY); }
|
virtual String getHelp() { return getResource(MBC_CONFIG_HELP_KEY); }
|
||||||
virtual String exec(String params);
|
virtual String exec(String params);
|
||||||
@ -29,18 +36,85 @@ private:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
struct MeterBusFrame {
|
||||||
|
/*
|
||||||
|
* Short Frame:
|
||||||
|
* Start 10h
|
||||||
|
* C-Field
|
||||||
|
* A-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
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
unsigned char startDelimiter;
|
||||||
|
unsigned char length;
|
||||||
|
unsigned char cField;
|
||||||
|
unsigned char aField;
|
||||||
|
unsigned char ciField;
|
||||||
|
unsigned char userData[255];
|
||||||
|
unsigned char checksum;
|
||||||
|
bool valid;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
class MeterBusClient {
|
class MeterBusClient {
|
||||||
public:
|
public:
|
||||||
MeterBusClient();
|
MeterBusClient();
|
||||||
void begin(CmdServer *cmdServer);
|
void begin(CmdServer *cmdServer, Thermometer *thermometer, Uptime *uptime);
|
||||||
void exec();
|
void exec();
|
||||||
friend class MeterBusClientConfig;
|
friend class MeterBusClientConfig;
|
||||||
private:
|
private:
|
||||||
|
MeterBusClientConfig m_meterBusClientConfig;
|
||||||
|
Thermometer *m_thermometer;
|
||||||
|
Uptime *m_uptime;
|
||||||
unsigned char m_address;
|
unsigned char m_address;
|
||||||
void setAddress(unsigned char address);
|
void setAddress(unsigned char address);
|
||||||
unsigned char getAddress();
|
unsigned char getAddress();
|
||||||
|
bool handleFrame();
|
||||||
|
MeterBusFrame m_frame;
|
||||||
|
|
||||||
|
unsigned char m_sendBuffer[265];
|
||||||
|
unsigned int m_sendBufferLen;
|
||||||
|
void aSB(unsigned char v); // append to send buffer
|
||||||
|
void aSB(unsigned int v);
|
||||||
|
void aSB(unsigned long v);
|
||||||
|
void aSB(float v);
|
||||||
|
unsigned char calcSendChecksum();
|
||||||
|
void calcAndSetFrameLength();
|
||||||
|
|
||||||
|
unsigned long m_frameCnt;
|
||||||
|
unsigned char m_accessCnt;
|
||||||
|
unsigned long m_myFrameCnt;
|
||||||
|
unsigned long m_invalidFrameCnt;
|
||||||
|
unsigned long m_invalidChecksum;
|
||||||
|
unsigned long m_collisionCnt;
|
||||||
|
|
||||||
|
unsigned char getAccessCnt();
|
||||||
|
unsigned char getStatus();
|
||||||
|
|
||||||
|
bool m_debug;
|
||||||
|
bool getDebug();
|
||||||
|
void setDebug(bool b);
|
||||||
|
bool m_info;
|
||||||
|
bool getInfo();
|
||||||
|
void setInfo(bool b);
|
||||||
|
|
||||||
|
void SND_NKE();
|
||||||
|
void REQ_UD2();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* METERBUSCLIENT_H_ */
|
#endif /* METERBUSCLIENT_H_ */
|
||||||
|
190
meterBusClientFrames.cpp
Normal file
190
meterBusClientFrames.cpp
Normal file
@ -0,0 +1,190 @@
|
|||||||
|
/*
|
||||||
|
* meterBusClientFrames.cpp
|
||||||
|
*
|
||||||
|
* Created on: 09.03.2014
|
||||||
|
* Author: wn
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "meterBusClient.h"
|
||||||
|
|
||||||
|
void MeterBusClient::aSB(unsigned char v) {
|
||||||
|
m_sendBuffer[m_sendBufferLen] = v;
|
||||||
|
m_sendBufferLen++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MeterBusClient::aSB(unsigned int v) {
|
||||||
|
// 16 bit
|
||||||
|
union {
|
||||||
|
unsigned int i;
|
||||||
|
unsigned char c[2];
|
||||||
|
} u;
|
||||||
|
u.i = v;
|
||||||
|
m_sendBuffer[m_sendBufferLen] = u.c[0];
|
||||||
|
m_sendBufferLen++;
|
||||||
|
m_sendBuffer[m_sendBufferLen] = u.c[1];
|
||||||
|
m_sendBufferLen++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MeterBusClient::aSB(unsigned long v) {
|
||||||
|
// 32 bit
|
||||||
|
union{
|
||||||
|
unsigned long l;
|
||||||
|
unsigned char c[4];
|
||||||
|
} u;
|
||||||
|
u.l = v;
|
||||||
|
m_sendBuffer[m_sendBufferLen] = u.c[0];
|
||||||
|
m_sendBufferLen++;
|
||||||
|
m_sendBuffer[m_sendBufferLen] = u.c[1];
|
||||||
|
m_sendBufferLen++;
|
||||||
|
m_sendBuffer[m_sendBufferLen] = u.c[2];
|
||||||
|
m_sendBufferLen++;
|
||||||
|
m_sendBuffer[m_sendBufferLen] = u.c[3];
|
||||||
|
m_sendBufferLen++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MeterBusClient::aSB(float v) {
|
||||||
|
// 32 bit
|
||||||
|
union{
|
||||||
|
float f;
|
||||||
|
unsigned char c[4];
|
||||||
|
} u;
|
||||||
|
u.f = v;
|
||||||
|
m_sendBuffer[m_sendBufferLen] = u.c[0];
|
||||||
|
m_sendBufferLen++;
|
||||||
|
m_sendBuffer[m_sendBufferLen] = u.c[1];
|
||||||
|
m_sendBufferLen++;
|
||||||
|
m_sendBuffer[m_sendBufferLen] = u.c[2];
|
||||||
|
m_sendBufferLen++;
|
||||||
|
m_sendBuffer[m_sendBufferLen] = u.c[3];
|
||||||
|
m_sendBufferLen++;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char MeterBusClient::calcSendChecksum() {
|
||||||
|
unsigned char checksum = 0;
|
||||||
|
for (unsigned int i = 4; i < m_sendBufferLen; i++) {
|
||||||
|
checksum += m_sendBuffer[i];
|
||||||
|
}
|
||||||
|
return checksum;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MeterBusClient::calcAndSetFrameLength() {
|
||||||
|
unsigned char frameLength = m_sendBufferLen - 6;
|
||||||
|
m_sendBuffer[1] = frameLength;
|
||||||
|
m_sendBuffer[2] = frameLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MeterBusClient::SND_NKE() {
|
||||||
|
m_sendBufferLen = 0;
|
||||||
|
aSB((unsigned char)0xE5);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MeterBusClient::REQ_UD2() {
|
||||||
|
m_sendBufferLen = 0;
|
||||||
|
// frame header
|
||||||
|
aSB((unsigned char)0x68);
|
||||||
|
aSB((unsigned char)0x00);
|
||||||
|
aSB((unsigned char)0x00);
|
||||||
|
aSB((unsigned char)0x68);
|
||||||
|
|
||||||
|
// C
|
||||||
|
aSB((unsigned char)0x08);
|
||||||
|
|
||||||
|
// A
|
||||||
|
aSB(getAddress());
|
||||||
|
|
||||||
|
// CI
|
||||||
|
aSB((unsigned char)0x72);
|
||||||
|
|
||||||
|
// Header
|
||||||
|
aSB((unsigned long)0); // Ident
|
||||||
|
aSB((unsigned int)0); // Manu.
|
||||||
|
aSB((unsigned char)1); // Version
|
||||||
|
aSB((unsigned char)0); // Medium: other
|
||||||
|
aSB(getAccessCnt()); // Access
|
||||||
|
aSB(getStatus()); // Status
|
||||||
|
aSB((unsigned int)0); // Signatur
|
||||||
|
|
||||||
|
// Uptime
|
||||||
|
// DIF
|
||||||
|
aSB((unsigned char)0x01);
|
||||||
|
// VIF
|
||||||
|
aSB((unsigned char)0x24);
|
||||||
|
aSB(m_uptime->getSeconds());
|
||||||
|
|
||||||
|
// DIF
|
||||||
|
aSB((unsigned char)0x01);
|
||||||
|
// VIF
|
||||||
|
aSB((unsigned char)0x25);
|
||||||
|
aSB(m_uptime->getMinutes());
|
||||||
|
|
||||||
|
// DIF
|
||||||
|
aSB((unsigned char)0x01);
|
||||||
|
// VIF
|
||||||
|
aSB((unsigned char)0x26);
|
||||||
|
aSB(m_uptime->getHours());
|
||||||
|
|
||||||
|
// DIF
|
||||||
|
aSB((unsigned char)0x02);
|
||||||
|
// VIF
|
||||||
|
aSB((unsigned char)0x27);
|
||||||
|
aSB((unsigned int)m_uptime->getDays());
|
||||||
|
|
||||||
|
|
||||||
|
// Temperatur 1
|
||||||
|
// DIF
|
||||||
|
aSB((unsigned char)0x05);
|
||||||
|
// VIF
|
||||||
|
aSB((unsigned char)0x67);
|
||||||
|
// value
|
||||||
|
aSB(m_thermometer->getTemperature(0));
|
||||||
|
|
||||||
|
// Temperatur 2
|
||||||
|
// DIF
|
||||||
|
aSB((unsigned char)0x05);
|
||||||
|
// VIF
|
||||||
|
aSB((unsigned char)0x67);
|
||||||
|
// value
|
||||||
|
aSB(m_thermometer->getTemperature(1));
|
||||||
|
|
||||||
|
// Temperatur 3
|
||||||
|
// DIF
|
||||||
|
aSB((unsigned char)0x05);
|
||||||
|
// VIF
|
||||||
|
aSB((unsigned char)0x67);
|
||||||
|
// value
|
||||||
|
aSB(m_thermometer->getTemperature(2));
|
||||||
|
|
||||||
|
// Temperatur 4
|
||||||
|
// DIF
|
||||||
|
aSB((unsigned char)0x05);
|
||||||
|
// VIF
|
||||||
|
aSB((unsigned char)0x67);
|
||||||
|
// value
|
||||||
|
aSB(m_thermometer->getTemperature(3));
|
||||||
|
|
||||||
|
// Structured Manufacturer Data
|
||||||
|
// DIF
|
||||||
|
aSB((unsigned char)0x0f);
|
||||||
|
aSB(m_frameCnt);
|
||||||
|
aSB(m_myFrameCnt);
|
||||||
|
aSB(m_invalidFrameCnt);
|
||||||
|
aSB(m_invalidChecksum);
|
||||||
|
aSB(m_collisionCnt);
|
||||||
|
aSB(m_thermometer->getCalibrateFactor(0));
|
||||||
|
aSB(m_thermometer->getCalibrateFactor(1));
|
||||||
|
aSB(m_thermometer->getCalibrateFactor(2));
|
||||||
|
aSB(m_thermometer->getCalibrateFactor(3));
|
||||||
|
aSB(m_thermometer->getAlpha());
|
||||||
|
aSB(m_thermometer->getPeriodMeasure());
|
||||||
|
|
||||||
|
|
||||||
|
aSB(calcSendChecksum());
|
||||||
|
aSB((unsigned char)0x16);
|
||||||
|
calcAndSetFrameLength();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -63,8 +63,9 @@ String ThermValues::exec(String params) {
|
|||||||
for (unsigned int i = 0; i < NUM_OF_CHANNELS; i++) {
|
for (unsigned int i = 0; i < NUM_OF_CHANNELS; i++) {
|
||||||
m_stream->print(getResource(INDEX_KEY)); m_stream->print(i); m_stream->print(getResource(COMMA_SPACE_KEY));
|
m_stream->print(getResource(INDEX_KEY)); m_stream->print(i); m_stream->print(getResource(COMMA_SPACE_KEY));
|
||||||
m_stream->print(getResource(CAL_KEY)); m_stream->print(m_thermometer->getCalibrateFactor(i), 6); m_stream->print(getResource(COMMA_SPACE_KEY));
|
m_stream->print(getResource(CAL_KEY)); m_stream->print(m_thermometer->getCalibrateFactor(i), 6); m_stream->print(getResource(COMMA_SPACE_KEY));
|
||||||
m_stream->print(getResource(T_KEY)); m_stream->print(m_thermometer->m_temperature[i], 6); m_stream->print(getResource(COMMA_SPACE_KEY));
|
m_stream->print(getResource(R_KEY)); m_stream->print(m_thermometer->m_r[i], 2); m_stream->print(getResource(COMMA_SPACE_KEY));
|
||||||
m_stream->print(getResource(TS_KEY)); m_stream->print(m_thermometer->m_smoothedTemperature[i], 6);
|
m_stream->print(getResource(T_KEY)); m_stream->print(m_thermometer->m_temperature[i], 2); m_stream->print(getResource(COMMA_SPACE_KEY));
|
||||||
|
m_stream->print(getResource(TS_KEY)); m_stream->print(m_thermometer->m_smoothedTemperature[i], 2);
|
||||||
m_stream->println();
|
m_stream->println();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,13 +98,8 @@ String ThermCalibrate::exec(String params) {
|
|||||||
m_stream->println(getResource(CALI_HELP_4_KEY));
|
m_stream->println(getResource(CALI_HELP_4_KEY));
|
||||||
m_stream->println(getResource(CALI_HELP_5_KEY));
|
m_stream->println(getResource(CALI_HELP_5_KEY));
|
||||||
m_stream->println(getResource(CALI_HELP_6_KEY));
|
m_stream->println(getResource(CALI_HELP_6_KEY));
|
||||||
} else if (params.startsWith("start ") && (space != -1) && m_enabled) {
|
} else if (params.startsWith("start") && m_enabled) {
|
||||||
m_channel = atoi(pb1);
|
m_start_highCalibration = true;
|
||||||
if ((m_channel + 1) > NUM_OF_CHANNELS) {
|
|
||||||
res = "Invalid channel number";
|
|
||||||
} else {
|
|
||||||
m_start_highCalibration = true;
|
|
||||||
}
|
|
||||||
} else if (params.startsWith("stop")) {
|
} else if (params.startsWith("stop")) {
|
||||||
m_start_highCalibration = false;
|
m_start_highCalibration = false;
|
||||||
} else if (params.startsWith("r ") && (space != -1)) {
|
} else if (params.startsWith("r ") && (space != -1)) {
|
||||||
@ -181,6 +177,14 @@ float pt1000(float r) {
|
|||||||
return (r / PT1000_R0 - 1) / PT1000_Coeff;
|
return (r / PT1000_R0 - 1) / PT1000_Coeff;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Thermometer::setR(unsigned int index, float r) {
|
||||||
|
m_r[index] = r;
|
||||||
|
if (getDebug() || getInfo()) {
|
||||||
|
Serial.print(getResource(SET_R_KEY)); Serial.print(index); Serial.print(getResource(COMMA_SPACE_KEY));
|
||||||
|
Serial.print(getResource(R_EQUALS_KEY)); Serial.print(r, 2); Serial.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Thermometer::setTemperature(unsigned int index, float t) {
|
void Thermometer::setTemperature(unsigned int index, float t) {
|
||||||
m_temperature[index] = t;
|
m_temperature[index] = t;
|
||||||
|
|
||||||
@ -198,6 +202,10 @@ void Thermometer::setTemperature(unsigned int index, float t) {
|
|||||||
m_lastSmoothedTemperature[index] = m_smoothedTemperature[index];
|
m_lastSmoothedTemperature[index] = m_smoothedTemperature[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float Thermometer::getTemperature(unsigned int index) {
|
||||||
|
return m_temperature[index];
|
||||||
|
}
|
||||||
|
|
||||||
void Thermometer::prepareAdc() {
|
void Thermometer::prepareAdc() {
|
||||||
unsigned long oldRegValue = AD7190_GetRegisterValue(AD7190_REG_CONF, 3, 1);
|
unsigned long oldRegValue = AD7190_GetRegisterValue(AD7190_REG_CONF, 3, 1);
|
||||||
oldRegValue &= ~(AD7190_CONF_CHAN(0xFF));
|
oldRegValue &= ~(AD7190_CONF_CHAN(0xFF));
|
||||||
@ -341,7 +349,7 @@ void Thermometer::exec() {
|
|||||||
SPI_Disable(AD7190_SLAVE_ID);
|
SPI_Disable(AD7190_SLAVE_ID);
|
||||||
|
|
||||||
if (m_calibrationHighMode || thermCalibrate.m_start_highCalibration) {
|
if (m_calibrationHighMode || thermCalibrate.m_start_highCalibration) {
|
||||||
Serial.println(getResource(CALIBRATION_MODE_HINT_KEY));
|
if (getDebug()) { Serial.println(getResource(CALIBRATION_MODE_HINT_KEY)); }
|
||||||
state = 20;
|
state = 20;
|
||||||
} else if (m_calibrationZeroMode || thermCalibrate.m_start_zeroCalibration) {
|
} else if (m_calibrationZeroMode || thermCalibrate.m_start_zeroCalibration) {
|
||||||
Serial.println(getResource(CALIBRATION_ZEOR_MODE_HINT_KEY));
|
Serial.println(getResource(CALIBRATION_ZEOR_MODE_HINT_KEY));
|
||||||
@ -354,6 +362,7 @@ void Thermometer::exec() {
|
|||||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("calibrateFactor="); Serial.print(getCalibrateFactor(i), 6); Serial.print(getResource(COMMA_SPACE_KEY)); }
|
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("calibrateFactor="); Serial.print(getCalibrateFactor(i), 6); Serial.print(getResource(COMMA_SPACE_KEY)); }
|
||||||
float r = (((float)(m_n[i] - ((i == 3) ? 0 : m_n[i + 1]))) / ((float)N_MAX)) * R_REF * getCalibrateFactor(i);
|
float r = (((float)(m_n[i] - ((i == 3) ? 0 : m_n[i + 1]))) / ((float)N_MAX)) * R_REF * getCalibrateFactor(i);
|
||||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("r="); Serial.print(r); Serial.print(getResource(COMMA_SPACE_KEY)); }
|
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("r="); Serial.print(r); Serial.print(getResource(COMMA_SPACE_KEY)); }
|
||||||
|
setR(i, r);
|
||||||
float t = pt1000(r);
|
float t = pt1000(r);
|
||||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("t="); Serial.print(t); Serial.print(getResource(COMMA_SPACE_KEY)); }
|
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("t="); Serial.print(t); Serial.print(getResource(COMMA_SPACE_KEY)); }
|
||||||
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.println(); }
|
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.println(); }
|
||||||
@ -374,7 +383,6 @@ void Thermometer::exec() {
|
|||||||
if (currentMillis >= (lastMillis + getPeriodMeasure())) {
|
if (currentMillis >= (lastMillis + getPeriodMeasure())) {
|
||||||
lastMillis = currentMillis;
|
lastMillis = currentMillis;
|
||||||
state = 0;
|
state = 0;
|
||||||
Serial3.println("Tick");
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -382,25 +390,29 @@ void Thermometer::exec() {
|
|||||||
if (thermCalibrate.m_start_highCalibration) {
|
if (thermCalibrate.m_start_highCalibration) {
|
||||||
if (m_calibrationHighMode) {
|
if (m_calibrationHighMode) {
|
||||||
// do the calibration stuff
|
// do the calibration stuff
|
||||||
|
(thermCalibrate.m_turn)++;
|
||||||
for (unsigned int i = 0; i < NUM_OF_CHANNELS; i++) {
|
for (unsigned int i = 0; i < NUM_OF_CHANNELS; i++) {
|
||||||
float r = (((float)(m_n[i] - ((i == 3) ? 0 : m_n[i + 1]))) / ((float)N_MAX)) * R_REF;
|
float r = (((float)(m_n[i] - ((i == 3) ? 0 : m_n[i + 1]))) / ((float)N_MAX)) * R_REF;
|
||||||
Serial.print("t="); Serial.print(thermCalibrate.m_turn); Serial.print(", c="); Serial.print(i);
|
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("t="); Serial.print(thermCalibrate.m_turn); Serial.print(", c="); Serial.print(i); }
|
||||||
Serial.print(", r="); Serial.print(r, 6); Serial.print(", n="); Serial.print(m_n[i]);
|
if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print(", r="); Serial.print(r, 6); Serial.print(", n="); Serial.print(m_n[i]); }
|
||||||
if (i == thermCalibrate.m_channel) {
|
thermCalibrate.m_r_sum[i] += r;
|
||||||
thermCalibrate.m_r_sum += r;
|
float r_avg = thermCalibrate.m_r_sum[i] / thermCalibrate.m_turn;
|
||||||
}
|
if (getDebug() || getInfo()) { Serial.print(thermCalibrate.m_turn); Serial.print(getResource(COMMA_SPACE_KEY)); Serial.print(getResource(STATE_20_MSG_1_KEY)); Serial.print(i); Serial.print(getResource(COLON_SPACE_KEY)); Serial.print(r_avg, 6); }
|
||||||
|
float calFact = thermCalibrate.m_r_cal / r_avg;
|
||||||
|
if (getDebug() || getInfo()) { Serial.print(getResource(COMMA_SPACE_KEY)); Serial.print(getResource(STATE_20_MSG_2_KEY)); Serial.print(calFact, 6); Serial.println(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((thermCalibrate.m_turn) >= NUM_OF_CALIBRATION_CYCLES) {
|
||||||
|
thermCalibrate.m_start_highCalibration = false;
|
||||||
}
|
}
|
||||||
(thermCalibrate.m_turn)++;
|
|
||||||
float r_avg = thermCalibrate.m_r_sum / thermCalibrate.m_turn;
|
|
||||||
Serial.print(getResource(STATE_20_MSG_1_KEY)); Serial.print(thermCalibrate.m_channel); Serial.print(getResource(COLON_SPACE_KEY)); Serial.print(r_avg, 6);
|
|
||||||
float calFact = thermCalibrate.m_r_cal / r_avg;
|
|
||||||
Serial.print(getResource(STATE_20_MSG_2_KEY)); Serial.print(calFact, 6); Serial.println();
|
|
||||||
} else {
|
} else {
|
||||||
thermCalibrate.m_preserved_alpha = m_alpha;
|
thermCalibrate.m_preserved_alpha = m_alpha;
|
||||||
m_alpha = 1;
|
m_alpha = 1;
|
||||||
thermCalibrate.m_preserved_period = m_periodMillis;
|
thermCalibrate.m_preserved_period = m_periodMillis;
|
||||||
m_periodMillis = 500;
|
m_periodMillis = CALIBRATION_CYCLE_TIME;
|
||||||
thermCalibrate.m_r_sum = 0.0;
|
for (unsigned int i = 0; i < NUM_OF_CHANNELS; i++) {
|
||||||
|
thermCalibrate.m_r_sum[i] = 0.0;
|
||||||
|
}
|
||||||
thermCalibrate.m_turn = 0;
|
thermCalibrate.m_turn = 0;
|
||||||
m_calibrationHighMode = true;
|
m_calibrationHighMode = true;
|
||||||
}
|
}
|
||||||
@ -409,12 +421,15 @@ void Thermometer::exec() {
|
|||||||
m_calibrationHighMode = false;
|
m_calibrationHighMode = false;
|
||||||
m_alpha = thermCalibrate.m_preserved_alpha;
|
m_alpha = thermCalibrate.m_preserved_alpha;
|
||||||
m_periodMillis = thermCalibrate.m_preserved_period;
|
m_periodMillis = thermCalibrate.m_preserved_period;
|
||||||
Serial.println(getResource(STATE_20_MSG_3_KEY));
|
for (unsigned int i = 0; i < NUM_OF_CHANNELS; i++) {
|
||||||
float r_avg = thermCalibrate.m_r_sum / thermCalibrate.m_turn;
|
Serial.println(getResource(STATE_20_MSG_3_KEY));
|
||||||
float calFact = thermCalibrate.m_r_cal / r_avg;
|
float r_avg = thermCalibrate.m_r_sum[i] / thermCalibrate.m_turn;
|
||||||
Serial.print(getResource(STATE_20_MSG_4_KEY)); Serial.print(thermCalibrate.m_channel);
|
float calFact = thermCalibrate.m_r_cal / r_avg;
|
||||||
Serial.print(getResource(COLON_SPACE_KEY)); Serial.print(calFact, 6); Serial.println();
|
|
||||||
setCalibrateFactor(thermCalibrate.m_channel, calFact);
|
Serial.print(getResource(STATE_20_MSG_4_KEY)); Serial.print(i);
|
||||||
|
Serial.print(getResource(COLON_SPACE_KEY)); Serial.print(calFact, 6); Serial.println();
|
||||||
|
setCalibrateFactor(i, calFact);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
state = 11;
|
state = 11;
|
||||||
|
@ -21,6 +21,8 @@ const float R_REF = 6200.0;
|
|||||||
const float PT1000_R0 = 1000.0;
|
const float PT1000_R0 = 1000.0;
|
||||||
const float PT1000_Coeff = 3.85e-3;
|
const float PT1000_Coeff = 3.85e-3;
|
||||||
const unsigned long CONV_TIMEOUT = 0xfffff;
|
const unsigned long CONV_TIMEOUT = 0xfffff;
|
||||||
|
const unsigned long NUM_OF_CALIBRATION_CYCLES = 250;
|
||||||
|
const unsigned long CALIBRATION_CYCLE_TIME = 10; // ms
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -61,9 +63,8 @@ private:
|
|||||||
float m_r_cal;
|
float m_r_cal;
|
||||||
float m_preserved_alpha;
|
float m_preserved_alpha;
|
||||||
unsigned long m_preserved_period;
|
unsigned long m_preserved_period;
|
||||||
unsigned int m_channel;
|
|
||||||
unsigned int m_turn;
|
unsigned int m_turn;
|
||||||
float m_r_sum;
|
float m_r_sum[NUM_OF_CHANNELS];
|
||||||
};
|
};
|
||||||
|
|
||||||
class Thermometer {
|
class Thermometer {
|
||||||
@ -73,6 +74,10 @@ public:
|
|||||||
void exec();
|
void exec();
|
||||||
friend class ThermConfig;
|
friend class ThermConfig;
|
||||||
friend class ThermValues;
|
friend class ThermValues;
|
||||||
|
float getTemperature(unsigned int index);
|
||||||
|
float getCalibrateFactor(unsigned int i);
|
||||||
|
unsigned long getPeriodMeasure();
|
||||||
|
float getAlpha();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned long m_periodMillis;
|
unsigned long m_periodMillis;
|
||||||
@ -85,6 +90,7 @@ private:
|
|||||||
|
|
||||||
unsigned long m_n[NUM_OF_CHANNELS];
|
unsigned long m_n[NUM_OF_CHANNELS];
|
||||||
float m_calibrateFactor[NUM_OF_CHANNELS];
|
float m_calibrateFactor[NUM_OF_CHANNELS];
|
||||||
|
float m_r[NUM_OF_CHANNELS];
|
||||||
float m_temperature[NUM_OF_CHANNELS];
|
float m_temperature[NUM_OF_CHANNELS];
|
||||||
float m_lastSmoothedTemperature[NUM_OF_CHANNELS];
|
float m_lastSmoothedTemperature[NUM_OF_CHANNELS];
|
||||||
float m_smoothedTemperature[NUM_OF_CHANNELS];
|
float m_smoothedTemperature[NUM_OF_CHANNELS];
|
||||||
@ -100,12 +106,10 @@ private:
|
|||||||
void prepareAdc();
|
void prepareAdc();
|
||||||
|
|
||||||
void setTemperature(unsigned int index, float t);
|
void setTemperature(unsigned int index, float t);
|
||||||
|
void setR(unsigned int index, float r);
|
||||||
void setPeriodMeasure(unsigned long p);
|
void setPeriodMeasure(unsigned long p);
|
||||||
unsigned long getPeriodMeasure();
|
|
||||||
void setAlpha(float a);
|
void setAlpha(float a);
|
||||||
float getAlpha();
|
|
||||||
void setCalibrateFactor(unsigned int i, float c);
|
void setCalibrateFactor(unsigned int i, float c);
|
||||||
float getCalibrateFactor(unsigned int i);
|
|
||||||
void setDebug(bool b);
|
void setDebug(bool b);
|
||||||
bool getDebug();
|
bool getDebug();
|
||||||
void setInfo(bool b);
|
void setInfo(bool b);
|
||||||
|
2
uptime.h
2
uptime.h
@ -26,7 +26,7 @@ public:
|
|||||||
uint8_t getSeconds() { return m_seconds; };
|
uint8_t getSeconds() { return m_seconds; };
|
||||||
uint8_t getMinutes() { return m_minutes; };
|
uint8_t getMinutes() { return m_minutes; };
|
||||||
uint8_t getHours() { return m_hours; };
|
uint8_t getHours() { return m_hours; };
|
||||||
uint8_t getDays() { return m_days; };
|
uint16_t getDays() { return m_days; };
|
||||||
private:
|
private:
|
||||||
UptimeCmd m_uptimeCmd;
|
UptimeCmd m_uptimeCmd;
|
||||||
uint8_t m_seconds;
|
uint8_t m_seconds;
|
||||||
|
Reference in New Issue
Block a user