add count down timer

This commit is contained in:
Dmitry Borisenko
2020-11-18 03:25:05 +03:00
parent 06df1f17b6
commit ceb516ddbe
13 changed files with 130 additions and 9 deletions

View File

@@ -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",

View File

@@ -0,0 +1 @@
0;count-down;id;na;na;na;order

View File

@@ -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.Отобразить время работы устройства"
}

View File

@@ -44,4 +44,4 @@ extern void bmp280ReadingPress();
extern void sysUptime();
extern void uptimeReading();
extern void impuls();

View File

@@ -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;

View File

@@ -0,0 +1,27 @@
#pragma once
#include <Arduino.h>
#include "Global.h"
class CountDownClass;
typedef std::vector<CountDownClass> 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();

View File

@@ -1,6 +1,5 @@
#pragma once
#include <Arduino.h>
#include "Global.h"
class ImpulsOutClass;
@@ -28,4 +27,5 @@ class ImpulsOutClass {
extern MyImpulsOutVector* myImpulsOut;
extern void impuls();
extern void impulsExecute();

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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

71
src/items/vCountDown.cpp Normal file
View File

@@ -0,0 +1,71 @@
#include "items/vCountDown.h"
#include "BufferExecute.h"
#include "Class/LineParsing.h"
#include "Global.h"
#include "BufferExecute.h"
#include <Arduino.h>
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());
}
}
}

View File

@@ -1,10 +1,9 @@
#include "items/vImpulsOut.h"
#include <Arduino.h>
#include "BufferExecute.h"
#include "Class/LineParsing.h"
#include "Global.h"
#include "BufferExecute.h"
#include <Arduino.h>
ImpulsOutClass::ImpulsOutClass(unsigned int impulsPin) {
_impulsPin = impulsPin;

View File

@@ -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();
}
}
}