From 403c182724a1dd6d70129bbd90168a96ba63c9e6 Mon Sep 17 00:00:00 2001 From: rljonesau Date: Wed, 19 Dec 2018 19:39:07 +1100 Subject: [PATCH] Comprehensive stuff up - editing a test branch - incorporated changes back into proper repo --- Arduino/BTCDieselHeater/BTCDieselHeater.ino | 4 +- Arduino/BTCDieselHeater/src/RTC/Clock.cpp | 3 +- Arduino/BTCDieselHeater/src/RTC/Timers.cpp | 122 ++++++++++++++++++ Arduino/BTCDieselHeater/src/RTC/Timers.h | 30 +++++ .../BTCDieselHeater/src/Utility/BTC_JSON.cpp | 59 ++------- .../BTCDieselHeater/src/WiFi/BTCWebServer.cpp | 4 +- 6 files changed, 168 insertions(+), 54 deletions(-) create mode 100644 Arduino/BTCDieselHeater/src/RTC/Timers.cpp create mode 100644 Arduino/BTCDieselHeater/src/RTC/Timers.h diff --git a/Arduino/BTCDieselHeater/BTCDieselHeater.ino b/Arduino/BTCDieselHeater/BTCDieselHeater.ino index dabbc2b..9854f23 100644 --- a/Arduino/BTCDieselHeater/BTCDieselHeater.ino +++ b/Arduino/BTCDieselHeater/BTCDieselHeater.ino @@ -889,9 +889,9 @@ void checkDebugCommands() void updateJsonBT() { - char jsonStr[512]; + char jsonStr[600]; - if(makeJsonString(BTModerator, jsonStr, 512)) { + if(makeJsonString(BTModerator, jsonStr, sizeof(jsonStr))) { Bluetooth.send( jsonStr ); } } diff --git a/Arduino/BTCDieselHeater/src/RTC/Clock.cpp b/Arduino/BTCDieselHeater/src/RTC/Clock.cpp index 5e70af2..3460d66 100644 --- a/Arduino/BTCDieselHeater/src/RTC/Clock.cpp +++ b/Arduino/BTCDieselHeater/src/RTC/Clock.cpp @@ -173,4 +173,5 @@ void setTime(const char* newTime) DateTime newDateTime(currentDateTime.year(), currentDateTime.month(), currentDateTime.day(), hour, minute, second); Clock.set(newDateTime); } -} \ No newline at end of file +} + diff --git a/Arduino/BTCDieselHeater/src/RTC/Timers.cpp b/Arduino/BTCDieselHeater/src/RTC/Timers.cpp new file mode 100644 index 0000000..dfbc0be --- /dev/null +++ b/Arduino/BTCDieselHeater/src/RTC/Timers.cpp @@ -0,0 +1,122 @@ +/* + * This file is part of the "bluetoothheater" distribution + * (https://gitlab.com/mrjones.id.au/bluetoothheater) + * + * Copyright (C) 2018 Ray Jones + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include +#include "Timers.h" +#include "../Utility/NVStorage.h" +#include "BTCDateTime.h" + + +void decodeTimerDays(int ID, const char* str) +{ + sTimer timer; + NVstore.getTimerInfo(ID, timer); + unsigned char days = 0; + if(strstr(str, "Next")) { + days = 0x80; + } + else { + for(int i=0; i< 7; i++) { + int mask = 0x01 << i; + if(strstr(str, daysOfTheWeek[i])) + days |= mask; + } + } + timer.enabled = days; + NVstore.setTimerInfo(ID, timer); +} + + +void decodeTimerTime(int ID, int stop, const char* str) +{ + sTimer timer; + NVstore.getTimerInfo(ID, timer); + int hour, minute; + if(2 == sscanf(str, "%d:%d", &hour, &minute)) { + if(stop) { + timer.stop.hour = hour; + timer.stop.min = minute; + } + else { + timer.start.hour = hour; + timer.start.min = minute; + } + NVstore.setTimerInfo(ID, timer); + } +} + + +void decodeTimerRepeat(int ID, int state) +{ + sTimer timer; + NVstore.getTimerInfo(ID, timer); + timer.repeat = state; + NVstore.setTimerInfo(ID, timer); +} + + +const char* getTimerStr(int timer, int param) +{ + sTimer timerInfo; + // due to how ArduinoJSON builds the JSON string, we need to create and store each string individually here. + static char StartStr[2][8]; + static char StopStr[2][8]; + static char DayStr[2][32]; + static char RptStr[2][2]; + + NVstore.getTimerInfo(timer, timerInfo); + int i = 0; + int comma = 0; + + switch(param) { + case 0: + sprintf(StartStr[timer], "%02d:%02d", timerInfo.start.hour, timerInfo.start.min); + return StartStr[timer]; + case 1: + sprintf(StopStr[timer], "%02d:%02d", timerInfo.stop.hour, timerInfo.stop.min); + return StopStr[timer]; + case 2: + if(timerInfo.enabled == 0) { + strcpy(DayStr[timer], "None"); + } + else if(timerInfo.enabled & 0x80) { + strcpy(DayStr[timer], "Next"); + } + else { + comma = 0; + DayStr[timer][0] = 0; + for(i=0; i<7; i++) { + if(timerInfo.enabled & (0x01< + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#ifndef __BTC_TIMERS_H__ +#define __BTC_TIMERS_H__ + +const char* getTimerStr(int timer, int param); +void decodeTimerDays(int timerID, const char* str); +void decodeTimerTime(int ID, int stop, const char*); +void decodeTimerRepeat(int ID, int state); + +#endif diff --git a/Arduino/BTCDieselHeater/src/Utility/BTC_JSON.cpp b/Arduino/BTCDieselHeater/src/Utility/BTC_JSON.cpp index 3462138..27f7251 100644 --- a/Arduino/BTCDieselHeater/src/Utility/BTC_JSON.cpp +++ b/Arduino/BTCDieselHeater/src/Utility/BTC_JSON.cpp @@ -24,14 +24,11 @@ #include "../Protocol/helpers.h" #include "NVstorage.h" #include "../RTC/BTCDateTime.h" +#include "../RTC/Timers.h" char defaultJSONstr[64]; -void decodeTimerDays(int timerID, const char* str); -void decodeTimerTime(int ID, int stop, const char*); -void decodeTimerRepeat(int ID, int state); - void interpretJsonCommand(char* pLine) { @@ -128,7 +125,7 @@ void interpretJsonCommand(char* pLine) bool makeJsonString(CModerator& moderator, char* opStr, int len) { - StaticJsonBuffer<512> jsonBuffer; // create a JSON buffer on the stack + StaticJsonBuffer<600> jsonBuffer; // create a JSON buffer on the stack JsonObject& root = jsonBuffer.createObject(); // create object to add JSON commands to bool bSend = false; // reset should send flag @@ -155,6 +152,14 @@ bool makeJsonString(CModerator& moderator, char* opStr, int len) bSend |= moderator.addJson("SystemVoltage", getHeaterInfo().getBattVoltage(), root ); bSend |= moderator.addJson("GlowVoltage", getHeaterInfo().getGlow_Voltage(), root ); bSend |= moderator.addJson("GlowCurrent", getHeaterInfo().getGlow_Current(), root ); + bSend |= moderator.addJson("Timer1Start", getTimerStr(0, 0), root ); + bSend |= moderator.addJson("Timer1Stop", getTimerStr(0, 1), root ); + bSend |= moderator.addJson("Timer1Days", getTimerStr(0, 2), root ); + bSend |= moderator.addJson("Timer1Repeat", getTimerStr(0, 3), root ); + bSend |= moderator.addJson("Timer2Start", getTimerStr(1, 0), root ); + bSend |= moderator.addJson("Timer2Stop", getTimerStr(1, 1), root ); + bSend |= moderator.addJson("Timer2Days", getTimerStr(1, 2), root ); + bSend |= moderator.addJson("Timer2Repeat", getTimerStr(1, 3), root ); if(bSend) { root.printTo(opStr, len); @@ -164,47 +169,3 @@ bool makeJsonString(CModerator& moderator, char* opStr, int len) } -void decodeTimerDays(int ID, const char* str) -{ - sTimer timer; - NVstore.getTimerInfo(ID, timer); - unsigned char days = 0; - if(strstr(str, "Next")) { - days = 0x80; - } - else { - for(int i=0; i< 7; i++) { - int mask = 0x01 << i; - if(strstr(str, daysOfTheWeek[i])) - days |= mask; - } - } - timer.enabled = days; - NVstore.setTimerInfo(ID, timer); -} - -void decodeTimerTime(int ID, int stop, const char* str) -{ - sTimer timer; - NVstore.getTimerInfo(ID, timer); - int hour, minute; - if(2 == sscanf(str, "%d:%d", &hour, &minute)) { - if(stop) { - timer.stop.hour = hour; - timer.stop.min = minute; - } - else { - timer.start.hour = hour; - timer.start.min = minute; - } - NVstore.setTimerInfo(ID, timer); - } -} - -void decodeTimerRepeat(int ID, int state) -{ - sTimer timer; - NVstore.getTimerInfo(ID, timer); - timer.repeat = state; - NVstore.setTimerInfo(ID, timer); -} \ No newline at end of file diff --git a/Arduino/BTCDieselHeater/src/WiFi/BTCWebServer.cpp b/Arduino/BTCDieselHeater/src/WiFi/BTCWebServer.cpp index 1075378..70c8f6b 100644 --- a/Arduino/BTCDieselHeater/src/WiFi/BTCWebServer.cpp +++ b/Arduino/BTCDieselHeater/src/WiFi/BTCWebServer.cpp @@ -98,9 +98,9 @@ bool doWebServer(void) { lastTx = millis() + 100; - char jsonStr[512]; + char jsonStr[600]; - if(makeJsonString(WebModerator, jsonStr, 512)) { + if(makeJsonString(WebModerator, jsonStr, sizeof(jsonStr))) { bTxWebData = true; // OLED tx data animation flag webSocket.broadcastTXT(jsonStr); }