DomoEsp_v2.0/DomoEspSensor/SensorBuzzer.h

70 lines
1.5 KiB
C++

#ifndef SensorBuzzerDef
#define SensorBuzzerDef 1
#include "Utiles.h"
//sensor virtual
class SensorBuzzer: public DomoEspSensorReceiver
{
int val;
SonidoBuzzer buz;
int pin;
public:
SensorBuzzer()
{
val=0;
topic[0]=0;
}
void setMelodia(int i, char* melodia)
{
buz.Set(i, melodia);
}
void set(char* topic_id, int _pin)
{
val=0;
strcpy(topic, topic_id);
pin=_pin;
}
virtual float getVal()
{
return (float)val;
}
virtual void inicia()
{
pinMode(pin, OUTPUT);
digitalWrite(pin, 1);
}
virtual void SubscribeMqtt(IMqttManager* man){
//char buffer_t[MAXTOPICVAR];
sprintf(buffer_t, "%s/set",topic);
man->MqttSubs(buffer_t);
sprintf(buffer_t, "%s/pul",topic);
man->MqttSubs(buffer_t);
}
virtual void OnMqtt(IMqttManager * man, char* _topic, char* payload, int tipo)
{
if((tipo!=Topic::SET) && (tipo!=Topic::PUL))
return;
if(!strcmp(_topic, topic))
{
if(tipo==Topic::PUL)
{
//reproduce melodia
val=atoi(payload);
if(val>=0 && val<MAX_MELODIAS)
buz.Toca(pin, val);
digitalWrite(pin, 1);
}
else if(tipo==Topic::SET)
{
//reproduce melodia
char buf[5];
char*mel=buz.getMelSubSt(payload, '~', 5, buf);
val=atoi(buf);
if(val>=0 && val<MAX_MELODIAS)
buz.Set(val, mel);
}
}
}
};
#endif