194 lines
3.8 KiB
C++
194 lines
3.8 KiB
C++
#ifndef SensorLcdDef
|
|
#define SensorLcdDef 1
|
|
#include "defines.h"
|
|
#include <LiquidCrystal.h>
|
|
|
|
#define MAX_SENS_PAN 2
|
|
class PantallaLcd
|
|
{
|
|
public:
|
|
PantallaLcd()
|
|
{
|
|
msg[0]=0;
|
|
}
|
|
char msg[MAX_CHAR_PAN_LCD];
|
|
DomoEspSensorReceiver* sen[MAX_SENS_PAN];
|
|
};
|
|
|
|
|
|
//sensor virtual
|
|
class SensorLcd : public DomoEspSensorReceiver
|
|
{
|
|
float val;
|
|
LiquidCrystal *lcd;
|
|
int nchar;
|
|
int nlin;
|
|
int npan;
|
|
int apan;
|
|
Ctimer timer;
|
|
|
|
|
|
PantallaLcd pan[MAX_PAN_LCD];
|
|
public:
|
|
SensorLcd()
|
|
{
|
|
val = 0;
|
|
topic[0] = 0;
|
|
lcd = 0;
|
|
npan=0;
|
|
apan=0;
|
|
|
|
}
|
|
void set(char* topic_id, LiquidCrystal * _lcd, int _nchar, int lineas, int seg)
|
|
{
|
|
val = 0;
|
|
strcpy(topic, topic_id);
|
|
lcd = _lcd;
|
|
timer.set(seg);
|
|
nchar = _nchar;
|
|
nlin = lineas;
|
|
|
|
}
|
|
void AddPan(char* msg, DomoEspSensorReceiver *sen1=0, DomoEspSensorReceiver *sen2=0)
|
|
{
|
|
if(npan<MAX_PAN_LCD)
|
|
{
|
|
strcpy(pan[npan].msg, msg);
|
|
pan[npan].sen[0]=sen1;
|
|
pan[npan].sen[1]=sen2;
|
|
npan++;
|
|
}
|
|
else
|
|
{
|
|
#ifdef DEBUG_PS
|
|
Serial.println("----------------------ERROR NO ENTRAN MAS pantallas en lcd---------------- ");
|
|
#endif
|
|
}
|
|
}
|
|
virtual float getVal()
|
|
{
|
|
return val;
|
|
}
|
|
|
|
virtual void inicia()
|
|
{
|
|
lcd->begin(nchar, nlin);
|
|
timer.inicia();
|
|
}
|
|
|
|
virtual void procesa(IMqttManager* man, int tiempo)
|
|
{
|
|
if(timer.onTimerReset())
|
|
{
|
|
apan=(apan+1)%npan;
|
|
muestraPan();
|
|
}
|
|
}
|
|
|
|
virtual void SubscribeMqtt(IMqttManager* man) {
|
|
//char buffer_t[MAXTOPICVAR];
|
|
sprintf(buffer_t, "%s/set", topic);
|
|
man->MqttSubs(buffer_t);
|
|
}
|
|
virtual void OnMqtt(IMqttManager* man, char* _topic, char* payload, int tipo)
|
|
{
|
|
if (tipo != Topic::SET)
|
|
return;
|
|
if (!strcmp(_topic, topic))
|
|
{
|
|
Muestra(payload);
|
|
timer.inicia();
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
void muestraPan()
|
|
{
|
|
if(npan<=0)
|
|
return;
|
|
#ifdef DEBUG_PS
|
|
Serial.println("SenLcd muestraPan:");
|
|
Serial.println(apan);
|
|
#endif
|
|
char msg[MAX_CHAR_PAN_LCD];
|
|
char val1[6];
|
|
char val2[6];
|
|
|
|
if(pan[apan].sen[0])
|
|
dtostrf(pan[apan].sen[0]->getVal(),3, 2, val1);
|
|
if(pan[apan].sen[0])
|
|
dtostrf(pan[apan].sen[1]->getVal(),3, 2, val2);
|
|
|
|
sprintf(msg, pan[apan].msg, val1,val2);
|
|
Muestra(msg);
|
|
|
|
}
|
|
void Muestra(char * txt)
|
|
{
|
|
#ifdef DEBUG_PS
|
|
Serial.println("SenLcd muestra: ");
|
|
Serial.println(txt);
|
|
#endif
|
|
int linea=0;
|
|
int i=0;
|
|
char* ini=txt;
|
|
bool sigue=true;
|
|
while(sigue && linea<nlin)
|
|
{
|
|
if((ini[i]=='\n') || (ini[i]==0))
|
|
{
|
|
Muestra(ini, i, linea);
|
|
if(*ini==0)
|
|
{
|
|
sigue=false;
|
|
}
|
|
else
|
|
{
|
|
ini=&ini[i+1];
|
|
}
|
|
i=0;
|
|
linea++;
|
|
|
|
|
|
}
|
|
else
|
|
i++;
|
|
|
|
}
|
|
while(linea<nlin)
|
|
{
|
|
Muestra("", 0, linea);
|
|
linea++;
|
|
}
|
|
|
|
|
|
}
|
|
void Muestra(char * txt,int nStr, int linea)
|
|
{
|
|
char buf[MAXTOPICVAR];
|
|
|
|
if (nStr > nchar)
|
|
nStr= nchar;
|
|
|
|
|
|
|
|
for (int i = 0; i < nchar; i++)
|
|
if (i < nStr)
|
|
buf[i] = txt[i];
|
|
else
|
|
buf[i] = ' ';
|
|
|
|
buf[nchar] = 0;
|
|
#ifdef DEBUG_PS
|
|
Serial.println("SenLcd muestraFin: ");
|
|
Serial.println(buf);
|
|
#endif
|
|
lcd->setCursor(0, linea);
|
|
lcd->print(buf);
|
|
}
|
|
};
|
|
|
|
#endif |