From 07a0f53eb8976f57dac9a1c9c638da65102fab3c Mon Sep 17 00:00:00 2001 From: Gerardo Date: Tue, 1 Oct 2024 09:41:27 +0200 Subject: [PATCH] =?UTF-8?q?a=C3=B1adida=20clase=20timer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DomoEspSensor/SensorTimer.h | 15 +++++++-------- DomoEspSensor/Utiles.cpp | 36 ++++++++++++++++++++++++++++++++++++ DomoEspSensor/Utiles.h | 14 ++++++++++++++ 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/DomoEspSensor/SensorTimer.h b/DomoEspSensor/SensorTimer.h index 21883e5..13293d6 100644 --- a/DomoEspSensor/SensorTimer.h +++ b/DomoEspSensor/SensorTimer.h @@ -6,8 +6,7 @@ class SensorTimer: public DomoEspSensorReceiver { bool val; bool valDef; - unsigned long tiempo_sens; - unsigned long incre_tsens; + Ctimer t; public: SensorTimer() @@ -17,8 +16,7 @@ class SensorTimer: public DomoEspSensorReceiver } void set(char* topic_id, bool _valdef, int segTime) { - incre_tsens=1000*segTime; - + t.set(segTime); valDef=_valdef; val=valDef; strcpy(topic, topic_id); @@ -31,7 +29,7 @@ class SensorTimer: public DomoEspSensorReceiver { if(val!=valDef) { - if(MqttUtiles::pasa_incre(&tiempo_sens, incre_tsens)) + if(t.onTimer()) { val=valDef; tiempo=2; @@ -70,8 +68,8 @@ class SensorTimer: public DomoEspSensorReceiver val=1; else val=0; - - MqttUtiles::reinicia_incre(&tiempo_sens, incre_tsens); + t.inicia(); + //MqttUtiles::reinicia_incre(&tiempo_sens, incre_tsens); } else if(tipo==Topic::SET) { @@ -80,7 +78,8 @@ class SensorTimer: public DomoEspSensorReceiver else val=atof(payload); if(val!=valDef) - MqttUtiles::reinicia_incre(&tiempo_sens, incre_tsens); + t.inicia(); + //MqttUtiles::reinicia_incre(&tiempo_sens, incre_tsens); } if(valOld!=val) diff --git a/DomoEspSensor/Utiles.cpp b/DomoEspSensor/Utiles.cpp index eedd064..559b673 100644 --- a/DomoEspSensor/Utiles.cpp +++ b/DomoEspSensor/Utiles.cpp @@ -47,7 +47,43 @@ void MqttUtiles::resetFunc() ESP.wdtEnable(1); while(1){}; } +//************************************************************************************************************************************************** + Ctimer::Ctimer() + { + t=0; + } + void Ctimer::set(int incremento_seg) + { + incre=1000*incremento_seg; + } + void Ctimer::inicia() + { + t=millis(); + } + + bool Ctimer::onTimer() + { + unsigned long ta=millis(); + if(ta=ta) + return false; + return true; + } + bool Ctimer::onTimerReset() + { + unsigned long ta=millis(); + if(ta=ta) + return false; + t=ta; + return true; + } //************************************************************************************************************************************************** WifiManager::WifiManager() { diff --git a/DomoEspSensor/Utiles.h b/DomoEspSensor/Utiles.h index adb38be..02a6c39 100644 --- a/DomoEspSensor/Utiles.h +++ b/DomoEspSensor/Utiles.h @@ -13,6 +13,20 @@ class MqttUtiles static void resetFunc(); }; +class Ctimer +{ + unsigned long t; + unsigned long incre; + public: + Ctimer(); + + void set(int incremento_seg); + + void inicia();//inicia contador de incremento + bool onTimer();//devuelve true si ha pasado el tiempo + bool onTimerReset();//pasa incremento y si ha pasado se reinicia +}; + class MqttOnVarChangeListenner { public: