Tidy up of state machine in JSON world
Hit <SPC> to get debug console menu Toggle reporting of CHeaterPackage to debug console - using <B> on debug console Toggle reporting of JSON to debug console - using <J> on debug console Only allow temperature and heating mode changes when not using an OEM controller
This commit is contained in:
parent
f2732ba1ac
commit
5268d0704c
|
@ -131,7 +131,6 @@ void initBlueWireSerial();
|
|||
bool validateFrame(const CProtocol& frame, const char* name);
|
||||
void checkDisplayUpdate();
|
||||
void checkDebugCommands();
|
||||
void updateJSONclients();
|
||||
|
||||
// DS18B20 temperature sensor support
|
||||
OneWire ds(DS18B20_Pin); // on pin 5 (a 4.7K resistor is necessary)
|
||||
|
@ -152,12 +151,13 @@ CSmartError SmartError;
|
|||
CKeyPad KeyPad;
|
||||
CScreenManager ScreenManager;
|
||||
TelnetSpy DebugPort;
|
||||
CModerator JSONmoderator;
|
||||
|
||||
sRxLine PCline;
|
||||
long lastRxTime; // used to observe inter character delays
|
||||
bool hasOEMController = false;
|
||||
bool hasHtrData = false;
|
||||
bool bReportBlueWireData = REPORT_RAW_DATA;
|
||||
bool bReportJSONData = REPORT_JSON_TRANSMIT;
|
||||
|
||||
CProtocolPackage HeaterData;
|
||||
|
||||
|
@ -379,18 +379,20 @@ void loop()
|
|||
DebugPort.print("ms - ");
|
||||
if(CommState.is(CommStates::OEMCtrlRx)) {
|
||||
DebugPort.println("Timeout collecting OEM controller data, returning to Idle State");
|
||||
hasOEMController = false;
|
||||
}
|
||||
else if(CommState.is(CommStates::HeaterRx1)) {
|
||||
DebugPort.println("Timeout collecting OEM heater response data, returning to Idle State");
|
||||
hasHtrData = false;
|
||||
}
|
||||
else {
|
||||
DebugPort.println("Timeout collecting BTC heater response data, returning to Idle State");
|
||||
hasHtrData = false;
|
||||
}
|
||||
}
|
||||
|
||||
DebugPort.println("Recycling blue wire serial interface");
|
||||
initBlueWireSerial();
|
||||
// CommState.set(CommStates::Idle); // revert to idle mode, after passing thru temperature mode
|
||||
CommState.set(CommStates::TemperatureRead); // revert to idle mode, after passing thru temperature mode
|
||||
}
|
||||
}
|
||||
|
@ -473,18 +475,8 @@ void loop()
|
|||
break;
|
||||
}
|
||||
|
||||
// filled OEM controller frame, report
|
||||
// echo received OEM controller frame over Bluetooth, using [OEM] header
|
||||
// note that Rotary Knob and LED OEM controllers can flood the Bluetooth
|
||||
// handling at the client side, moderate OEM Bluetooth delivery
|
||||
if(OEMCtrlFrame.elapsedTime() > OEM_TO_BLUETOOTH_MODERATION_TIME) {
|
||||
OEMCtrlFrame.setTime();
|
||||
}
|
||||
else {
|
||||
#if REPORT_SUPPRESSED_OEM_DATA_FRAMES != 0
|
||||
DebugPort.println("Suppressed delivery of OEM frame");
|
||||
#endif
|
||||
}
|
||||
// filled OEM controller frame
|
||||
OEMCtrlFrame.setTime();
|
||||
CommState.set(CommStates::HeaterRx1);
|
||||
break;
|
||||
|
||||
|
@ -518,22 +510,12 @@ void loop()
|
|||
|
||||
// do some monitoring of the heater state variable
|
||||
// if abnormal transitions, introduce a smart error!
|
||||
// This will also cancel ON/OFF requests if runstate in startup/shutdown
|
||||
// This routine also cancels ON/OFF requests if runstate in startup/shutdown periods
|
||||
SmartError.monitor(HeaterFrame1);
|
||||
|
||||
// echo heater reponse data to Bluetooth client
|
||||
// note that Rotary Knob and LED OEM controllers can flood the Bluetooth
|
||||
// handling at the client side, moderate OEM Bluetooth delivery
|
||||
if(HeaterFrame1.elapsedTime() > OEM_TO_BLUETOOTH_MODERATION_TIME) {
|
||||
HeaterFrame1.setTime();
|
||||
}
|
||||
else {
|
||||
#if REPORT_SUPPRESSED_OEM_DATA_FRAMES != 0
|
||||
DebugPort.println("Suppressed delivery of OEM heater response frame");
|
||||
#endif
|
||||
}
|
||||
HeaterFrame1.setTime();
|
||||
|
||||
if(digitalRead(ListenOnlyPin)) {
|
||||
if(digitalRead(ListenOnlyPin)) { // pin open, pulled high (STANDARD OPERATION)
|
||||
bool isBTCmaster = false;
|
||||
while(BlueWireSerial.available()) {
|
||||
DebugPort.println("DUMPED ROGUE RX DATA");
|
||||
|
@ -544,7 +526,7 @@ void loop()
|
|||
TxManage.Start(timenow);
|
||||
CommState.set(CommStates::BTC_Tx);
|
||||
}
|
||||
else {
|
||||
else { // pin shorted to ground
|
||||
HeaterData.set(HeaterFrame1, OEMCtrlFrame);
|
||||
CommState.set(CommStates::TemperatureRead); // "Listen Only" input is held low, don't send out Tx
|
||||
}
|
||||
|
@ -555,11 +537,6 @@ void loop()
|
|||
// Handle time interval where we send data to the blue wire
|
||||
lastRxTime = timenow; // *we* are pumping onto blue wire, track this activity!
|
||||
if(TxManage.CheckTx(timenow) ) { // monitor progress of our data delivery
|
||||
/* if(!hasOEMController) {
|
||||
// only convey this frames to Bluetooth when NOT using an OEM controller!
|
||||
// Bluetooth.sendFrame("[BTC]", TxManage.getFrame(), TERMINATE_BTC_LINE); // BTC => Bluetooth Controller :-)
|
||||
// Bluetooth.send( createJSON("RunState", 1.50 ) );
|
||||
}*/
|
||||
CommState.set(CommStates::HeaterRx2); // then await heater repsonse
|
||||
}
|
||||
break;
|
||||
|
@ -596,7 +573,7 @@ void loop()
|
|||
digitalWrite(LED_Pin, LOW);
|
||||
#endif
|
||||
|
||||
// test for valid CRC, abort and restarts Serial1 if invalid
|
||||
// test for valid CRC, abort and restart Serial1 if invalid
|
||||
if(!validateFrame(HeaterFrame2, "RX2")) {
|
||||
hasHtrData = false;
|
||||
break;
|
||||
|
@ -609,11 +586,6 @@ void loop()
|
|||
// if abnormal transitions, introduce a smart error!
|
||||
SmartError.monitor(HeaterFrame2);
|
||||
|
||||
/* delay(5);
|
||||
if(!hasOEMController) {
|
||||
// only convey these frames to Bluetooth when NOT using an OEM controller!
|
||||
// Bluetooth.sendFrame("[HTR]", HeaterFrame2, true); // pin not grounded, suppress duplicate to BT
|
||||
}*/
|
||||
CommState.set(CommStates::TemperatureRead);
|
||||
HeaterData.set(HeaterFrame2, TxManage.getFrame());
|
||||
break;
|
||||
|
@ -636,7 +608,9 @@ void loop()
|
|||
ScreenManager.reqUpdate();
|
||||
}
|
||||
CommState.set(CommStates::Idle);
|
||||
updateJSONclients();
|
||||
updateJSONclients(bReportJSONData);
|
||||
if(bReportBlueWireData)
|
||||
HeaterData.reportFrames(hasOEMController);
|
||||
break;
|
||||
} // switch(CommState)
|
||||
|
||||
|
@ -680,9 +654,7 @@ bool validateFrame(const CProtocol& frame, const char* name)
|
|||
DebugPort.print("\007Bad CRC detected for ");
|
||||
DebugPort.print(name);
|
||||
DebugPort.println(" frame - restarting blue wire's serial port");
|
||||
char header[16];
|
||||
sprintf(header, "[CRC_%s]", name);
|
||||
DebugReportFrame(header, frame, "\r\n");
|
||||
DebugReportFrame("BAD CRC:", frame, "\r\n");
|
||||
initBlueWireSerial();
|
||||
CommState.set(CommStates::Idle);
|
||||
return false;
|
||||
|
@ -695,14 +667,14 @@ void requestOn()
|
|||
{
|
||||
TxManage.queueOnRequest();
|
||||
SmartError.reset();
|
||||
Bluetooth.setRefTime();
|
||||
HeaterData.setRefTime();
|
||||
}
|
||||
|
||||
void requestOff()
|
||||
{
|
||||
TxManage.queueOffRequest();
|
||||
SmartError.inhibit();
|
||||
Bluetooth.setRefTime();
|
||||
HeaterData.setRefTime();
|
||||
}
|
||||
|
||||
void ToggleOnOff()
|
||||
|
@ -718,8 +690,11 @@ void ToggleOnOff()
|
|||
}
|
||||
|
||||
|
||||
void reqTemp(unsigned char newTemp)
|
||||
bool reqTemp(unsigned char newTemp)
|
||||
{
|
||||
if(hasOEMController)
|
||||
return false;
|
||||
|
||||
unsigned char max = DefaultBTCParams.getTemperature_Max();
|
||||
unsigned char min = DefaultBTCParams.getTemperature_Min();
|
||||
if(newTemp >= max)
|
||||
|
@ -730,13 +705,14 @@ void reqTemp(unsigned char newTemp)
|
|||
NVstore.setDesiredTemperature(newTemp);
|
||||
|
||||
ScreenManager.reqUpdate();
|
||||
return true;
|
||||
}
|
||||
|
||||
void reqTempDelta(int delta)
|
||||
bool reqTempDelta(int delta)
|
||||
{
|
||||
unsigned char newTemp = getSetTemp() + delta;
|
||||
|
||||
reqTemp(newTemp);
|
||||
return reqTemp(newTemp);
|
||||
}
|
||||
|
||||
int getSetTemp()
|
||||
|
@ -744,14 +720,18 @@ int getSetTemp()
|
|||
return NVstore.getDesiredTemperature();
|
||||
}
|
||||
|
||||
void reqThermoToggle()
|
||||
bool reqThermoToggle()
|
||||
{
|
||||
setThermostatMode(getThermostatMode() ? 0 : 1);
|
||||
return setThermostatMode(getThermostatMode() ? 0 : 1);
|
||||
}
|
||||
|
||||
void setThermostatMode(unsigned char val)
|
||||
bool setThermostatMode(unsigned char val)
|
||||
{
|
||||
if(hasOEMController)
|
||||
return false;
|
||||
|
||||
NVstore.setThermostatMode(val);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
@ -832,49 +812,83 @@ void checkDebugCommands()
|
|||
|
||||
char rxVal = DebugPort.read();
|
||||
|
||||
rxVal = toLowerCase(rxVal);
|
||||
|
||||
#ifdef PROTOCOL_INVESTIGATION
|
||||
bool bSendVal = false;
|
||||
#endif
|
||||
if(isControl(rxVal)) { // "End of Line"
|
||||
#ifdef PROTOCOL_INVESTIGATION
|
||||
String convert(PCline.Line);
|
||||
val = convert.toInt();
|
||||
bSendVal = true;
|
||||
PCline.clear();
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
if(isDigit(rxVal)) {
|
||||
if(rxVal == ' ') { // SPACE to bring up menu
|
||||
DebugPort.print("\014");
|
||||
DebugPort.println("MENU options");
|
||||
DebugPort.println("<+> - request heater turns ON");
|
||||
DebugPort.println("<-> - request heater turns OFF");
|
||||
DebugPort.println("<B> - toggle raw blue wire data reporting");
|
||||
DebugPort.println("<J> - toggle output JSON reporting");
|
||||
DebugPort.println("<R> - restart the ESP");
|
||||
DebugPort.println("");
|
||||
DebugPort.println("");
|
||||
DebugPort.println("");
|
||||
DebugPort.println("");
|
||||
DebugPort.println("");
|
||||
DebugPort.println("");
|
||||
DebugPort.println("");
|
||||
DebugPort.println("");
|
||||
}
|
||||
#ifdef PROTOCOL_INVESTIGATION
|
||||
else if(isDigit(rxVal)) {
|
||||
PCline.append(rxVal);
|
||||
}
|
||||
else if((rxVal == 'p') || (rxVal == 'P')) {
|
||||
else if(rxVal == 'p') {
|
||||
DebugPort.println("Test Priming Byte... ");
|
||||
mode = 1;
|
||||
}
|
||||
else if((rxVal == 'g') || (rxVal == 'G')) {
|
||||
else if(rxVal == 'g') {
|
||||
DebugPort.println("Test glow power byte... ");
|
||||
mode = 2;
|
||||
}
|
||||
else if((rxVal == 'i') || (rxVal == 'I')) {
|
||||
else if(rxVal == 'i') {
|
||||
DebugPort.println("Test fan bytes");
|
||||
mode = 3;
|
||||
}
|
||||
else if(rxVal == '+') {
|
||||
TxManage.queueOnRequest();
|
||||
Bluetooth.setRefTime(); // reset time reference "run time"
|
||||
}
|
||||
else if(rxVal == '-') {
|
||||
TxManage.queueOffRequest();
|
||||
Bluetooth.setRefTime();
|
||||
}
|
||||
else if(rxVal == ']') {
|
||||
val++;
|
||||
bSendVal = true;
|
||||
}
|
||||
else if(rxVal == 'r') {
|
||||
ESP.restart(); // reset the esp
|
||||
}
|
||||
else if(rxVal == '[') {
|
||||
val--;
|
||||
bSendVal = true;
|
||||
}
|
||||
#endif
|
||||
else if(rxVal == 'b') {
|
||||
DebugPort.println("Toggling raw blue wire data reporting");
|
||||
bReportBlueWireData = !bReportBlueWireData;
|
||||
}
|
||||
else if(rxVal == 'j') {
|
||||
DebugPort.println("Toggling JSON data reporting");
|
||||
bReportJSONData = !bReportJSONData;
|
||||
}
|
||||
else if(rxVal == '+') {
|
||||
TxManage.queueOnRequest();
|
||||
HeaterData.setRefTime();
|
||||
}
|
||||
else if(rxVal == '-') {
|
||||
TxManage.queueOffRequest();
|
||||
HeaterData.setRefTime();
|
||||
}
|
||||
else if(rxVal == 'r') {
|
||||
ESP.restart(); // reset the esp
|
||||
}
|
||||
}
|
||||
#ifdef PROTOCOL_INVESTIGATION
|
||||
if(bSendVal) {
|
||||
switch(mode) {
|
||||
case 1:
|
||||
|
@ -889,25 +903,10 @@ void checkDebugCommands()
|
|||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
void updateJSONclients()
|
||||
{
|
||||
char jsonStr[600];
|
||||
|
||||
if(makeJsonString(JSONmoderator, jsonStr, sizeof(jsonStr))) {
|
||||
DebugPort.print("JSON send: "); DebugPort.println(jsonStr);
|
||||
Bluetooth.send( jsonStr );
|
||||
sendWebServerString( jsonStr );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void resetJSONmoderator()
|
||||
{
|
||||
JSONmoderator.reset();
|
||||
}
|
||||
|
||||
|
||||
const char* getControllerStat()
|
||||
|
|
|
@ -32,18 +32,10 @@ class CProtocol;
|
|||
class CBluetoothAbstract {
|
||||
protected:
|
||||
sRxLine _rxLine;
|
||||
CContextTimeStamp _timeStamp;
|
||||
virtual void foldbackDesiredTemp() {};
|
||||
public:
|
||||
virtual void begin() {};
|
||||
virtual void setRefTime() {
|
||||
_timeStamp.setRefTime();
|
||||
};
|
||||
virtual void send(const char* Str) {};
|
||||
virtual void sendFrame(const char* pHdr, const CProtocol& Frame, bool lineterm=true) {
|
||||
_timeStamp.report(pHdr);
|
||||
DebugReportFrame(pHdr, Frame, lineterm ? "\r\n" : " ");
|
||||
};
|
||||
virtual void check() {};
|
||||
virtual void collectRxData(char rxVal) {
|
||||
// provide common behviour for bytes received from a bluetooth client
|
||||
|
|
|
@ -87,32 +87,15 @@ CBluetoothESP32Classic::check()
|
|||
}
|
||||
|
||||
void
|
||||
CBluetoothESP32Classic::sendFrame(const char* pHdr, const CProtocol& Frame, bool lineterm)
|
||||
CBluetoothESP32Classic::send(const char* Str)
|
||||
{
|
||||
char fullMsg[32];
|
||||
|
||||
// report to debug port
|
||||
CBluetoothAbstract::sendFrame(pHdr, Frame, lineterm);
|
||||
|
||||
delay(40);
|
||||
if(isConnected()) {
|
||||
|
||||
if(Frame.verifyCRC()) {
|
||||
#if BT_LED == 1
|
||||
digitalWrite(LED_Pin, !digitalRead(LED_Pin)); // toggle LED
|
||||
digitalWrite(LED_Pin, !digitalRead(LED_Pin)); // toggle LED
|
||||
#endif
|
||||
int len = strlen(pHdr);
|
||||
if(len < 8) {
|
||||
strcpy(fullMsg, pHdr);
|
||||
memcpy(&fullMsg[len], Frame.Data, 24);
|
||||
|
||||
SerialBT.write((uint8_t*)fullMsg, 24+len);
|
||||
}
|
||||
delay(10);
|
||||
}
|
||||
else {
|
||||
DebugPort.println("Data not sent to Bluetooth, CRC error!");
|
||||
}
|
||||
SerialBT.write((uint8_t*)Str, strlen(Str));
|
||||
delay(10);
|
||||
}
|
||||
else {
|
||||
DebugPort.println("No Bluetooth client");
|
||||
|
@ -244,7 +227,7 @@ CBluetoothESP32BLE::begin()
|
|||
DebugPort.println("Awaiting a client to notify...");
|
||||
}
|
||||
|
||||
void
|
||||
/*void
|
||||
CBluetoothESP32BLE::sendFrame(const char* pHdr, const CProtocol& Frame, bool lineterm)
|
||||
{
|
||||
char fullMsg[32];
|
||||
|
@ -276,6 +259,29 @@ CBluetoothESP32BLE::sendFrame(const char* pHdr, const CProtocol& Frame, bool lin
|
|||
#endif
|
||||
}
|
||||
}
|
||||
*/
|
||||
void
|
||||
CBluetoothESP32BLE::send(const char* Str)
|
||||
{
|
||||
char fullMsg[32];
|
||||
|
||||
if(isConnected()) {
|
||||
|
||||
#if BT_LED == 1
|
||||
digitalWrite(LED_Pin, !digitalRead(LED_Pin)); // toggle LED
|
||||
#endif
|
||||
std::string txData = Str;
|
||||
|
||||
BLE_Send(txData);
|
||||
delay(10);
|
||||
}
|
||||
else {
|
||||
DebugPort.println("No Bluetooth client");
|
||||
#if BT_LED == 1
|
||||
digitalWrite(LED_Pin, 0);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
CBluetoothESP32BLE::isConnected()
|
||||
|
|
|
@ -35,7 +35,7 @@ class CBluetoothESP32Classic : public CBluetoothAbstract {
|
|||
BluetoothSerial SerialBT;
|
||||
public:
|
||||
virtual void begin();
|
||||
virtual void sendFrame(const char* pHdr, const CProtocol& Frame, bool lineterm=true);
|
||||
virtual void send(const char* Str);
|
||||
virtual void check();
|
||||
virtual bool isConnected();
|
||||
};
|
||||
|
@ -58,7 +58,7 @@ public:
|
|||
CBluetoothESP32BLE();
|
||||
virtual ~CBluetoothESP32BLE();
|
||||
virtual void begin();
|
||||
virtual void sendFrame(const char* pHdr, const CProtocol& Frame, bool lineterm=true);
|
||||
virtual void send(const char* Str);
|
||||
virtual void check();
|
||||
virtual bool isConnected();
|
||||
|
||||
|
|
|
@ -151,9 +151,14 @@ CBluetoothHC05::isConnected()
|
|||
void
|
||||
CBluetoothHC05::send(const char* Str)
|
||||
{
|
||||
HC05_SerialPort.print(Str);
|
||||
if(isConnected()) {
|
||||
HC05_SerialPort.print(Str);
|
||||
}
|
||||
else {
|
||||
// DebugPort.print("No Bluetooth client");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
void
|
||||
CBluetoothHC05::sendFrame(const char* pHdr, const CProtocol& Frame, bool lineterm)
|
||||
{
|
||||
|
@ -186,6 +191,7 @@ CBluetoothHC05::sendFrame(const char* pHdr, const CProtocol& Frame, bool lineter
|
|||
if(lineterm)
|
||||
DebugPort.println("");
|
||||
}
|
||||
*/
|
||||
|
||||
void
|
||||
CBluetoothHC05::openSerial(int baudrate)
|
||||
|
|
|
@ -44,7 +44,6 @@ public:
|
|||
CBluetoothHC05(int keyPin, int sensePin);
|
||||
void begin();
|
||||
void send(const char* Str);
|
||||
void sendFrame(const char* pHdr, const CProtocol& Frame, bool lineterm=true);
|
||||
void check();
|
||||
virtual bool isConnected();
|
||||
protected:
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#include "Protocol.h"
|
||||
#include "../Utility/DebugPort.h"
|
||||
#include "helpers.h"
|
||||
#include "../cfg/BTCConfig.h"
|
||||
|
||||
|
||||
unsigned short
|
||||
|
@ -393,3 +394,22 @@ CProtocolPackage::getErrStateStrEx() const
|
|||
UPPERLIMIT(errstate, 13);
|
||||
return ErrstatesEx[errstate];
|
||||
}
|
||||
|
||||
void
|
||||
CProtocolPackage::setRefTime()
|
||||
{
|
||||
_timeStamp.setRefTime();
|
||||
}
|
||||
|
||||
void
|
||||
CProtocolPackage::reportFrames(bool isOEM)
|
||||
{
|
||||
_timeStamp.report(); // absolute time
|
||||
if(isOEM) {
|
||||
DebugReportFrame("OEM:", Controller, TERMINATE_OEM_LINE ? "\r\n" : " ");
|
||||
}
|
||||
else {
|
||||
DebugReportFrame("BTC:", Controller, TERMINATE_BTC_LINE ? "\r\n" : " ");
|
||||
}
|
||||
DebugReportFrame("HTR:", Heater, "\r\n");
|
||||
}
|
|
@ -23,6 +23,7 @@
|
|||
#define _CPROTOCOL_H_
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "../Utility/UtilClasses.h"
|
||||
|
||||
class CProtocol {
|
||||
public:
|
||||
|
@ -204,9 +205,11 @@ public:
|
|||
long elapsedTime() { return millis() - lastTime; };
|
||||
};
|
||||
|
||||
|
||||
class CProtocolPackage {
|
||||
CProtocol Heater;
|
||||
CProtocol Controller;
|
||||
CContextTimeStamp _timeStamp;
|
||||
public:
|
||||
void set(const CProtocol& htr, const CProtocol& ctl) { Heater = htr; Controller = ctl; };
|
||||
int getRunState() const { return Heater.getRunState(); };
|
||||
|
@ -231,6 +234,9 @@ public:
|
|||
float getGlowPlug_Power() const { return Heater.getGlowPlug_Current() * Heater.getGlowPlug_Voltage(); };
|
||||
float getGlow_Voltage() const { return Heater.getGlowPlug_Voltage(); };
|
||||
float getGlow_Current() const { return Heater.getGlowPlug_Current(); };
|
||||
|
||||
void setRefTime();
|
||||
void reportFrames(bool isOEM);
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -26,10 +26,10 @@
|
|||
extern void ToggleOnOff();
|
||||
extern void requestOn();
|
||||
extern void requestOff();
|
||||
extern void reqTempDelta(int delta);
|
||||
extern void reqTemp(unsigned char newTemp);
|
||||
extern void reqThermoToggle();
|
||||
extern void setThermostatMode(unsigned char);
|
||||
extern bool reqTempDelta(int delta);
|
||||
extern bool reqTemp(unsigned char newTemp);
|
||||
extern bool reqThermoToggle();
|
||||
extern bool setThermostatMode(unsigned char);
|
||||
extern void reqPumpPrime(bool on);
|
||||
extern float getActualTemperature();
|
||||
extern int getSetTemp();
|
||||
|
|
|
@ -25,9 +25,13 @@
|
|||
#include "NVstorage.h"
|
||||
#include "../RTC/BTCDateTime.h"
|
||||
#include "../RTC/Timers.h"
|
||||
#include "../Bluetooth/BluetoothAbstract.h"
|
||||
#include "../WiFi/BTCWebServer.h"
|
||||
#include "../cfg/BTCConfig.h"
|
||||
|
||||
|
||||
char defaultJSONstr[64];
|
||||
CModerator JSONmoderator;
|
||||
|
||||
|
||||
void interpretJsonCommand(char* pLine)
|
||||
|
@ -49,7 +53,9 @@ void interpretJsonCommand(char* pLine)
|
|||
for(it = obj.begin(); it != obj.end(); ++it) {
|
||||
|
||||
if(strcmp("TempDesired", it->key) == 0) {
|
||||
reqTemp(it->value.as<unsigned char>());
|
||||
if(!reqTemp(it->value.as<unsigned char>())) { // this request is blocked if OEM controller active
|
||||
JSONmoderator.reset("TempDesired");
|
||||
}
|
||||
}
|
||||
else if(strcmp("RunState", it->key) == 0) {
|
||||
if(it->value.as<unsigned char>()) {
|
||||
|
@ -72,7 +78,9 @@ void interpretJsonCommand(char* pLine)
|
|||
setFanMax(it->value.as<short>());
|
||||
}
|
||||
else if(strcmp("Thermostat", it->key) == 0) {
|
||||
setThermostatMode(it->value.as<unsigned char>());
|
||||
if(!setThermostatMode(it->value.as<unsigned char>())) { // this request is blocked if OEM controller active
|
||||
JSONmoderator.reset("ThermoStat");
|
||||
}
|
||||
}
|
||||
else if(strcmp("NVsave", it->key) == 0) {
|
||||
if(it->value.as<int>() == 8861)
|
||||
|
@ -169,3 +177,24 @@ bool makeJsonString(CModerator& moderator, char* opStr, int len)
|
|||
}
|
||||
|
||||
|
||||
void updateJSONclients(bool report)
|
||||
{
|
||||
char jsonStr[600];
|
||||
|
||||
if(makeJsonString(JSONmoderator, jsonStr, sizeof(jsonStr))) {
|
||||
if (report) {
|
||||
DebugPort.print("JSON send: "); DebugPort.println(jsonStr);
|
||||
}
|
||||
getBluetoothClient().send( jsonStr );
|
||||
sendWebServerString( jsonStr );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void resetJSONmoderator()
|
||||
{
|
||||
JSONmoderator.reset();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
extern char defaultJSONstr[64];
|
||||
|
||||
bool makeJsonString(CModerator& moderator, char* opStr, int len);
|
||||
void updateJSONclients(bool report);
|
||||
|
||||
template<class T>
|
||||
const char* createJSON(const char* name, T value)
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
|
||||
#include <map>
|
||||
#include <ArduinoJSON.h>
|
||||
#include "DebugPort.h"
|
||||
|
||||
template <class T>
|
||||
class TModerator {
|
||||
|
@ -32,6 +33,7 @@ public:
|
|||
bool shouldSend(const char* name, T value);
|
||||
bool addJson(const char* name, T value, JsonObject& root);
|
||||
void reset();
|
||||
void reset(const char* name);
|
||||
};
|
||||
|
||||
template<class T>
|
||||
|
@ -72,6 +74,21 @@ void TModerator<T>::reset()
|
|||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void TModerator<T>::reset(const char* name)
|
||||
{
|
||||
auto it = Memory.find(name);
|
||||
if(it != Memory.end()) {
|
||||
DebugPort.print("Resetting moderator: \""); DebugPort.print(name); DebugPort.println("\"");
|
||||
if(std::is_pointer<T>::value) {
|
||||
it->second = NULL;
|
||||
}
|
||||
else {
|
||||
it->second = it->second+100;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class CModerator {
|
||||
TModerator<int> iModerator;
|
||||
TModerator<float> fModerator;
|
||||
|
@ -113,6 +130,12 @@ public:
|
|||
ucModerator.reset();
|
||||
szModerator.reset();
|
||||
};
|
||||
void reset(const char* name) {
|
||||
iModerator.reset(name);
|
||||
fModerator.reset(name);
|
||||
ucModerator.reset(name);
|
||||
szModerator.reset(name);
|
||||
};
|
||||
};
|
||||
|
||||
#endif // __BTC_MODERATOR_H__
|
||||
|
|
|
@ -24,9 +24,9 @@
|
|||
|
||||
#include <Arduino.h>
|
||||
#include <string.h>
|
||||
#include "../Protocol/Protocol.h"
|
||||
#include "DebugPort.h"
|
||||
|
||||
class CProtocol;
|
||||
|
||||
// a class to track the blue wire receive / transmit states
|
||||
class CommStates {
|
||||
|
@ -36,47 +36,26 @@ public:
|
|||
Idle, OEMCtrlRx, OEMCtrlReport, HeaterRx1, HeaterReport1, BTC_Tx, HeaterRx2, HeaterReport2, TemperatureRead
|
||||
};
|
||||
|
||||
private:
|
||||
eCS m_State;
|
||||
int m_Count;
|
||||
|
||||
public:
|
||||
CommStates() {
|
||||
m_State = Idle;
|
||||
m_Count = 0;
|
||||
}
|
||||
void set(eCS eState) {
|
||||
m_State = eState;
|
||||
m_Count = 0;
|
||||
#if SHOW_STATE_MACHINE_TRANSITIONS == 1
|
||||
DebugPort.print("State");DebugPort.println(m_State);
|
||||
#endif
|
||||
}
|
||||
void set(eCS eState);
|
||||
eCS get() {
|
||||
return m_State;
|
||||
}
|
||||
bool is(eCS eState) {
|
||||
return m_State == eState;
|
||||
}
|
||||
bool collectData(CProtocol& Frame, unsigned char val, int limit = 24) { // returns true when buffer filled
|
||||
Frame.Data[m_Count++] = val;
|
||||
return m_Count >= limit;
|
||||
}
|
||||
bool collectDataEx(CProtocol& Frame, unsigned char val, int limit = 24) { // returns true when buffer filled
|
||||
// guarding against rogue rx kernel buffer stutters....
|
||||
if((m_Count == 0) && (val != 0x76)) {
|
||||
DebugPort.println("First heater byte not 0x76 - SKIPPING");
|
||||
return false;
|
||||
}
|
||||
Frame.Data[m_Count++] = val;
|
||||
return m_Count >= limit;
|
||||
}
|
||||
bool checkValidStart(unsigned char val)
|
||||
{
|
||||
if(m_Count)
|
||||
return true;
|
||||
else
|
||||
return val == 0x76;
|
||||
}
|
||||
bool collectData(CProtocol& Frame, unsigned char val, int limit = 24);
|
||||
bool collectDataEx(CProtocol& Frame, unsigned char val, int limit = 24);
|
||||
bool checkValidStart(unsigned char val);
|
||||
|
||||
private:
|
||||
eCS m_State;
|
||||
int m_Count;
|
||||
};
|
||||
|
||||
|
||||
|
@ -137,9 +116,9 @@ public:
|
|||
void setRefTime() {
|
||||
refTime = millis();
|
||||
};
|
||||
void report(const char* pHdr) {
|
||||
void report(bool isDelta) {
|
||||
char msg[32];
|
||||
if(strncmp(pHdr, "[HTR]", 5) == 0) {
|
||||
if(isDelta) {
|
||||
long delta = millis() - prevTime;
|
||||
sprintf(msg, "%+8ldms ", delta);
|
||||
}
|
||||
|
@ -149,6 +128,12 @@ public:
|
|||
}
|
||||
DebugPort.print(msg);
|
||||
};
|
||||
void report() {
|
||||
char msg[32];
|
||||
prevTime = millis();
|
||||
sprintf(msg, "%8dms ", prevTime - refTime);
|
||||
DebugPort.print(msg);
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -44,13 +44,6 @@
|
|||
#define USE_WEBSERVER 1
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// limit rate of Bluetooth delivery from enthusiastic OEM controllers
|
||||
//
|
||||
#define OEM_TO_BLUETOOTH_MODERATION_TIME 700
|
||||
// show when we did moderate data frames to bluetooth
|
||||
#define REPORT_SUPPRESSED_OEM_DATA_FRAMES 0
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// debug reporting options
|
||||
|
@ -58,8 +51,11 @@
|
|||
// true: each frame of data is reported on a new lines
|
||||
// false: controller, then heater response frmaes are reported on a single line (excel CSV worthy!)
|
||||
//
|
||||
#define TERMINATE_OEM_LINE false /* when an OEM controller exists */
|
||||
#define TERMINATE_BTC_LINE false /* when an OEM controller does not exist */
|
||||
#define REPORT_RAW_DATA 0 /* can toggle using <B> from debug console */
|
||||
#define TERMINATE_OEM_LINE 0 /* when an OEM controller exists */
|
||||
#define TERMINATE_BTC_LINE 0 /* when an OEM controller does not exist */
|
||||
#define REPORT_OEM_RESYNC 0 /* report idle time if OEM controller detected */
|
||||
#define SHOW_STATE_MACHINE_TRANSITIONS 0 /* state machine changes reported to DebugPort */
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// LED monitoring
|
||||
|
@ -91,5 +87,9 @@
|
|||
// Blue wire handling
|
||||
//
|
||||
#define SUPPORT_OEM_CONTROLLER 1 /* 0=we send without regard to an OEM's data, 1=co-exist with OEM controller */
|
||||
#define REPORT_OEM_RESYNC 0 /* report idle time if OEM controller detected */
|
||||
#define SHOW_STATE_MACHINE_TRANSITIONS 0 /* state machine changes reported to DebugPort */
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Communications reporting
|
||||
//
|
||||
#define REPORT_JSON_TRANSMIT 1
|
||||
|
||||
|
|
|
@ -38,6 +38,7 @@ Working so far:
|
|||
To be implemented
|
||||
--------------------------
|
||||
* MQTT pub/sub
|
||||
* "fuel gauge" - Integrate pump frequency, assuming a repeatable dose of fuel per pump cycle...
|
||||
* Expand hardware compatability with different MCU setups. IE Arduino Due/Mega/Pro ESP8266 & ESP32
|
||||
* Documentation
|
||||
* list under construction.....
|
||||
|
|
Loading…
Reference in a new issue