From ceb516ddbe5ddacab695d7364d6d6264fc62aad1 Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <49808844+DmitryBorisenko33@users.noreply.github.com> Date: Wed, 18 Nov 2020 03:25:05 +0300 Subject: [PATCH] add count down timer --- data/config.json | 4 +-- data/items/count-down.txt | 1 + data/set.device.json | 3 +- include/BufferExecute.h | 2 +- include/Global.h | 3 ++ include/items/vCountDown.h | 27 +++++++++++++++ include/items/vImpulsOut.h | 2 +- src/BufferExecute.cpp | 5 +++ src/Global.cpp | 3 ++ src/Init.cpp | 7 ++++ src/items/vCountDown.cpp | 71 ++++++++++++++++++++++++++++++++++++++ src/items/vImpulsOut.cpp | 3 +- src/main.cpp | 8 +++-- 13 files changed, 130 insertions(+), 9 deletions(-) create mode 100644 data/items/count-down.txt create mode 100644 include/items/vCountDown.h create mode 100644 src/items/vCountDown.cpp diff --git a/data/config.json b/data/config.json index 2e45f37b..d060d7cc 100644 --- a/data/config.json +++ b/data/config.json @@ -3,8 +3,8 @@ "chipID": "", "apssid": "IoTmanager", "appass": "", - "routerssid": "VOLODYA", - "routerpass": "BELCHENKO", + "routerssid": "rise", + "routerpass": "hostel3333", "timezone": 1, "ntp": "pool.ntp.org", "mqttServer": "wqtt.ru", diff --git a/data/items/count-down.txt b/data/items/count-down.txt new file mode 100644 index 00000000..fa38b94b --- /dev/null +++ b/data/items/count-down.txt @@ -0,0 +1 @@ +0;count-down;id;na;na;na;order \ No newline at end of file diff --git a/data/set.device.json b/data/set.device.json index 7c02e45e..a5aefdac 100644 --- a/data/set.device.json +++ b/data/set.device.json @@ -91,7 +91,8 @@ "/set?addItem=bmp280-temp": "18.Датчик температуры bmp280", "/set?addItem=bmp280-press": "19.Датчик давления bmp280", "/set?addItem=impuls-out": "20.Создать импульсы через заданный промежуток времени (управление шд)", - "/set?addItem=modbus": "21.Прочитать регистр modbus устройства", + "/set?addItem=count-down": "21.Таймер обратного отчета", + "/set?addItem=modbus": "22.Прочитать регистр modbus устройства", "/set?addItem=logging": "a.Логгирование и вывод в график любой величины", "/set?addItem=uptime": "b.Отобразить время работы устройства" } diff --git a/include/BufferExecute.h b/include/BufferExecute.h index 99a1ab26..a23ba5c5 100644 --- a/include/BufferExecute.h +++ b/include/BufferExecute.h @@ -44,4 +44,4 @@ extern void bmp280ReadingPress(); extern void sysUptime(); extern void uptimeReading(); -extern void impuls(); + diff --git a/include/Global.h b/include/Global.h index b44cb8d7..86bc8f3a 100644 --- a/include/Global.h +++ b/include/Global.h @@ -81,6 +81,9 @@ extern int inOutput_EnterCounter; extern String pwmOut_KeyList; extern int pwmOut_EnterCounter; //========================================= +extern String countDown_KeyList; +extern int countDown_EnterCounter; +//========================================= // Sensors extern String sensorReadingMap10sec; diff --git a/include/items/vCountDown.h b/include/items/vCountDown.h new file mode 100644 index 00000000..1c5e9f14 --- /dev/null +++ b/include/items/vCountDown.h @@ -0,0 +1,27 @@ +#pragma once +#include +#include "Global.h" + +class CountDownClass; + +typedef std::vector MyCountDownVector; + +class CountDownClass { +public: + CountDownClass(String key); + ~CountDownClass(); + + void loop(); + void execute(unsigned int countDownPeriod); + +private: + unsigned long _countDownPeriod = 0; + String _key; + bool _start = false; + +}; + +extern MyCountDownVector* myCountDown; + +extern void countDown(); +extern void countDownExecute(); \ No newline at end of file diff --git a/include/items/vImpulsOut.h b/include/items/vImpulsOut.h index 1d9f9fa6..79316073 100644 --- a/include/items/vImpulsOut.h +++ b/include/items/vImpulsOut.h @@ -1,6 +1,5 @@ #pragma once #include - #include "Global.h" class ImpulsOutClass; @@ -28,4 +27,5 @@ class ImpulsOutClass { extern MyImpulsOutVector* myImpulsOut; +extern void impuls(); extern void impulsExecute(); diff --git a/src/BufferExecute.cpp b/src/BufferExecute.cpp index 893119b2..7b3d20b0 100644 --- a/src/BufferExecute.cpp +++ b/src/BufferExecute.cpp @@ -6,6 +6,8 @@ #include "items/vPwmOut.h" #include "items/vInOutput.h" #include "items/vLogging.h" +#include "items/vImpulsOut.h" +#include "items/vCountDown.h" void loopCmdAdd(const String& cmdStr) { orderBuf += cmdStr; @@ -89,6 +91,9 @@ void csvCmdExecute(String& cmdStr) { else if (order == F("impuls-out")) { sCmd.addCommand(order.c_str(), impuls); } + else if (order == F("count-down")) { + sCmd.addCommand(order.c_str(), countDown); + } sCmd.readStr(buf); } diff --git a/src/Global.cpp b/src/Global.cpp index 96f9c7de..b7df47ec 100644 --- a/src/Global.cpp +++ b/src/Global.cpp @@ -52,6 +52,9 @@ int inOutput_EnterCounter = -1; String pwmOut_KeyList = ""; int pwmOut_EnterCounter = -1; //========================================= +String countDown_KeyList = ""; +int countDown_EnterCounter = -1; +//========================================= // Sensors String sensorReadingMap10sec; diff --git a/src/Init.cpp b/src/Init.cpp index 7972db73..9e57f92a 100644 --- a/src/Init.cpp +++ b/src/Init.cpp @@ -8,6 +8,7 @@ #include "items/vSensorDallas.h" #include "items/vInOutput.h" #include "items/vPwmOut.h" +#include "items/vCountDown.h" void loadConfig() { configSetupJson = readFile("config.json", 4096); @@ -73,6 +74,12 @@ void Device_init() { pwmOut_KeyList = ""; pwmOut_EnterCounter = -1; //=================================== + if (myCountDown != nullptr) { + myCountDown->clear(); + } + countDown_KeyList = ""; + countDown_EnterCounter = -1; + //=================================== #ifdef LAYOUT_IN_RAM diff --git a/src/items/vCountDown.cpp b/src/items/vCountDown.cpp new file mode 100644 index 00000000..2215ca9f --- /dev/null +++ b/src/items/vCountDown.cpp @@ -0,0 +1,71 @@ +#include "items/vCountDown.h" +#include "BufferExecute.h" +#include "Class/LineParsing.h" +#include "Global.h" +#include "BufferExecute.h" +#include + +CountDownClass::CountDownClass(String key) { + _key = key; +} + +CountDownClass::~CountDownClass() {} + +void CountDownClass::execute(unsigned int countDownPeriod) { + _countDownPeriod = countDownPeriod * 1000; + _start = true; +} + +void CountDownClass::loop() { + static unsigned long prevMillis1; + static unsigned long prevMillis2; + static int sec; + if (_countDownPeriod > 0 && _start) { + prevMillis1 = millis(); + _start = false; + sec = (_countDownPeriod / 1000) + 1; + } + unsigned long currentMillis = millis(); + unsigned long difference1 = currentMillis - prevMillis1; + unsigned long difference2 = currentMillis - prevMillis2; + if (difference1 > _countDownPeriod && _countDownPeriod > 0) { + _countDownPeriod = 0; + eventGen2(_key, "0"); + } + if (difference2 > 1000 && _countDownPeriod > 0) { + prevMillis2 = millis(); + Serial.println(sec--); + publishStatus(_key, String(sec)); + } +} + +MyCountDownVector* myCountDown = nullptr; + +void countDown() { + myLineParsing.update(); + String key = myLineParsing.gkey(); + myLineParsing.clear(); + + countDown_EnterCounter++; + addKey(key, countDown_KeyList, countDown_EnterCounter); + + static bool firstTime = true; + if (firstTime) myCountDown = new MyCountDownVector(); + firstTime = false; + myCountDown->push_back(CountDownClass(key)); + + sCmd.addCommand(key.c_str(), countDownExecute); +} + +void countDownExecute() { + String key = sCmd.order(); + String countDownPeriod = sCmd.next(); + + int number = getKeyNum(key, countDown_KeyList); + + if (myCountDown != nullptr) { + if (number != -1) { + myCountDown->at(number).execute(countDownPeriod.toInt()); + } + } +} \ No newline at end of file diff --git a/src/items/vImpulsOut.cpp b/src/items/vImpulsOut.cpp index 911fdd22..9654cd69 100644 --- a/src/items/vImpulsOut.cpp +++ b/src/items/vImpulsOut.cpp @@ -1,10 +1,9 @@ #include "items/vImpulsOut.h" - -#include #include "BufferExecute.h" #include "Class/LineParsing.h" #include "Global.h" #include "BufferExecute.h" +#include ImpulsOutClass::ImpulsOutClass(unsigned int impulsPin) { _impulsPin = impulsPin; diff --git a/src/main.cpp b/src/main.cpp index 7b4e9b07..4337dc23 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,7 @@ #include "items/vLogging.h" #include "items/vImpulsOut.h" #include "items/vSensorDallas.h" +#include "items/vCountDown.h" #include "Telegram.h" void not_async_actions(); @@ -156,16 +157,19 @@ void loop() { myLogging->at(i).loop(); } } - if (myImpulsOut != nullptr) { for (unsigned int i = 0; i < myImpulsOut->size(); i++) { myImpulsOut->at(i).loop(); } } - if (mySensorDallas2 != nullptr) { for (unsigned int i = 0; i < mySensorDallas2->size(); i++) { mySensorDallas2->at(i).loop(); } } + if (myCountDown != nullptr) { + for (unsigned int i = 0; i < myCountDown->size(); i++) { + myCountDown->at(i).loop(); + } + } } \ No newline at end of file