From 32b9e4e8e579913cfecbf78705bdf193ff899d45 Mon Sep 17 00:00:00 2001 From: Gerardo Date: Tue, 1 Oct 2024 22:36:32 +0200 Subject: [PATCH] usando onTimer --- DomoEspSensor/SensorAin.h | 9 ++++---- DomoEspSensor/SensorLcd.h | 11 +++++----- DomoEspSensor/SensorPulsante.h | 11 +++++----- DomoEspSensor/Utiles.cpp | 38 ++++------------------------------ DomoEspSensor/Utiles.h | 5 ++--- 5 files changed, 20 insertions(+), 54 deletions(-) diff --git a/DomoEspSensor/SensorAin.h b/DomoEspSensor/SensorAin.h index 89a9285..39cb51e 100644 --- a/DomoEspSensor/SensorAin.h +++ b/DomoEspSensor/SensorAin.h @@ -8,8 +8,7 @@ class SensorAin: public DomoEspSensorReceiver bool negado; uint8_t pin; float dif; - unsigned long t; - unsigned long incre; + Ctimer timer; public: virtual float getVal() { @@ -18,8 +17,8 @@ class SensorAin: public DomoEspSensorReceiver SensorAin(){} void set(uint8_t _pin, char* topic_id, bool _negado, float _dif) { - t=0; - incre=1000; + timer.inicia(); + timer.set(1); dif=_dif; negado=_negado; pin=_pin; @@ -29,7 +28,7 @@ class SensorAin: public DomoEspSensorReceiver virtual void procesa(IMqttManager * man, int tiempo) { //mirar para configurar por interrupcion - if((dif!=0 && MqttUtiles::pasa_incre(&t, incre)) || tiempo==2) + if((dif!=0 && timer.onTimerReset()) || tiempo==2) { float val_act; if(negado) diff --git a/DomoEspSensor/SensorLcd.h b/DomoEspSensor/SensorLcd.h index bc4a235..c2bb35b 100644 --- a/DomoEspSensor/SensorLcd.h +++ b/DomoEspSensor/SensorLcd.h @@ -25,8 +25,7 @@ class SensorLcd : public DomoEspSensorReceiver int nlin; int npan; int apan; - unsigned long tiempo_sens; - unsigned long incre_tsens; + Ctimer timer; PantallaLcd pan[MAX_PAN_LCD]; @@ -45,7 +44,7 @@ public: val = 0; strcpy(topic, topic_id); lcd = _lcd; - incre_tsens = 1000 * seg; + timer.set(seg); nchar = _nchar; nlin = lineas; @@ -74,12 +73,12 @@ public: virtual void inicia() { lcd->begin(nchar, nlin); - MqttUtiles::reinicia_incre(&tiempo_sens, incre_tsens); + timer.inicia(); } virtual void procesa(IMqttManager* man, int tiempo) { - if(MqttUtiles::pasa_incre(&tiempo_sens, incre_tsens)) + if(timer.onTimerReset()) { apan=(apan+1)%npan; muestraPan(); @@ -98,7 +97,7 @@ public: if (!strcmp(_topic, topic)) { Muestra(payload); - MqttUtiles::reinicia_incre(&tiempo_sens, incre_tsens); + timer.inicia(); } } diff --git a/DomoEspSensor/SensorPulsante.h b/DomoEspSensor/SensorPulsante.h index a78c39a..4b1b895 100644 --- a/DomoEspSensor/SensorPulsante.h +++ b/DomoEspSensor/SensorPulsante.h @@ -7,8 +7,7 @@ class SensorPulsante: public DomoEspSensorReceiver bool val; bool valDef; uint8_t pin; - unsigned long tiempo_sens; - unsigned long incre_tsens; + Ctimer timer; bool espera; public: virtual float getVal() @@ -18,7 +17,7 @@ class SensorPulsante: public DomoEspSensorReceiver SensorPulsante() { espera=false; - incre_tsens=500; + timer.setmilis(500); } void set(uint8_t _pin, char* topic_id, bool _valDef) { @@ -29,14 +28,14 @@ class SensorPulsante: public DomoEspSensorReceiver } void setIncremento(int milis) { - incre_tsens=milis; + timer.setmilis(milis); } virtual void procesa(IMqttManager * man, int tiempo) { //mirar para configurar por interrupcion int val_ac=val; - if(!espera || MqttUtiles::pasa_incre(&tiempo_sens, incre_tsens)) + if(!espera || timer.onTimer()) { val_ac=digitalRead(pin); espera=false; @@ -53,7 +52,7 @@ class SensorPulsante: public DomoEspSensorReceiver sprintf(buffer_t, "%s/pul",topic); man->MqttSend(buffer_t, buffer_p); //delay(100); - MqttUtiles::reinicia_incre(&tiempo_sens, incre_tsens); + timer.inicia(); espera=true; } diff --git a/DomoEspSensor/Utiles.cpp b/DomoEspSensor/Utiles.cpp index 559b673..6c0c503 100644 --- a/DomoEspSensor/Utiles.cpp +++ b/DomoEspSensor/Utiles.cpp @@ -9,39 +9,6 @@ #include "Utiles.h" //************************************************************************************************************************************************** - -void MqttUtiles::reinicia_incre( unsigned long *tt, unsigned long incre) -{ - *tt=millis(); -} - -bool MqttUtiles::pasa_incre( unsigned long *tt, unsigned long incre) -{ - unsigned long t=millis(); - if(t<*tt) - { - *tt=t; - return true; - } - if((*tt+incre)>=t) - return false; - *tt=t; - return true; -} - -bool MqttUtiles::pasa_incre( volatile unsigned long *tt, unsigned long incre) -{ - unsigned long t=millis(); - if(t<*tt) - { - *tt=t; - return true; - } - if((*tt+incre)>=t) - return false; - *tt=t; - return true; -} void MqttUtiles::resetFunc() { ESP.wdtEnable(1); @@ -56,7 +23,10 @@ void MqttUtiles::resetFunc() { incre=1000*incremento_seg; } - + void Ctimer::setmilis(int milis) + { + incre=milis; + } void Ctimer::inicia() { t=millis(); diff --git a/DomoEspSensor/Utiles.h b/DomoEspSensor/Utiles.h index 02a6c39..786f27c 100644 --- a/DomoEspSensor/Utiles.h +++ b/DomoEspSensor/Utiles.h @@ -7,9 +7,6 @@ class WiFiClient; class MqttUtiles { public: - static bool pasa_incre( unsigned long *tt, unsigned long incre); - static void reinicia_incre( unsigned long *tt, unsigned long incre); - static bool pasa_incre( volatile unsigned long *tt, unsigned long incre); static void resetFunc(); }; @@ -21,6 +18,8 @@ class Ctimer Ctimer(); void set(int incremento_seg); + void setmilis(int milis); + void inicia();//inicia contador de incremento bool onTimer();//devuelve true si ha pasado el tiempo