From e963db4213333b0dafd35698b8c560160219e433 Mon Sep 17 00:00:00 2001 From: Gerardo Date: Mon, 30 Sep 2024 12:09:35 +0200 Subject: [PATCH] =?UTF-8?q?se=20a=C3=B1ade=20sensor=20buzzer=20y=20sensor?= =?UTF-8?q?=20splitter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DomoEspSensor/.theia/launch.json | 2 +- DomoEspSensor/Actuador.h | 2 +- DomoEspSensor/Automatismos.h | 4 +- DomoEspSensor/SensorBuzzer.h | 70 +++++++++++++++++ DomoEspSensor/SensorSplitter.h | 96 +++++++++++++++++++++++ DomoEspSensor/Utiles.cpp | 114 ++++++++++++++++++++++++++++ DomoEspSensor/Utiles.h | 26 ++++++- DomoEspSensor/configuracionActual.h | 8 +- DomoEspSensor/defines.h | 9 ++- 9 files changed, 322 insertions(+), 9 deletions(-) create mode 100644 DomoEspSensor/SensorBuzzer.h create mode 100644 DomoEspSensor/SensorSplitter.h diff --git a/DomoEspSensor/.theia/launch.json b/DomoEspSensor/.theia/launch.json index 9a49ac9..7e4253b 100644 --- a/DomoEspSensor/.theia/launch.json +++ b/DomoEspSensor/.theia/launch.json @@ -3,6 +3,6 @@ // Hover to view descriptions of existing attributes. "version": "0.2.0", "configurations": [ - + ] } diff --git a/DomoEspSensor/Actuador.h b/DomoEspSensor/Actuador.h index 5e2576d..af21dcb 100644 --- a/DomoEspSensor/Actuador.h +++ b/DomoEspSensor/Actuador.h @@ -17,7 +17,7 @@ class Actuador: public DomoEspSensorReceiver { float val; char topicOut[MAXTOPICVAR]; - char valSend[3]; + char valSend[MAX_CHAR_ENVIO]; Cactivador activador[MAX_ACTIVADORES]; public: diff --git a/DomoEspSensor/Automatismos.h b/DomoEspSensor/Automatismos.h index 2a54948..9a7ce4b 100644 --- a/DomoEspSensor/Automatismos.h +++ b/DomoEspSensor/Automatismos.h @@ -7,10 +7,12 @@ #include "SensorPulsante.h" #include "SensorAin.h" #include "SensorVF.h" -#include "SensorRF.h" #include "SensorTimer.h" #include "SensorLcd.h" +#include "SensorBuzzer.h" +#include "SensorRF.h" +#include "SensorSplitter.h" #include "Actuador.h" //---------------------------------------------------------------------------------------------------------------------------------------------- diff --git a/DomoEspSensor/SensorBuzzer.h b/DomoEspSensor/SensorBuzzer.h new file mode 100644 index 0000000..b4817f4 --- /dev/null +++ b/DomoEspSensor/SensorBuzzer.h @@ -0,0 +1,70 @@ +#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=0 && valMqttSubs(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)) + return; + + if(tipo==Topic::SET ) + { + if(payload[0]=='X') + { + if(val==0) + val=1; + else + val=0; } + else + val=atof(payload); + for(int i=0; itopic); + man->MqttSend(buffer_t, payload); + } + } + } + else if(tipo==Topic::PUL ) + { + for(int i=0; itopic); + man->MqttSend(buffer_t, payload); + } + } + } + } +}; + +#endif \ No newline at end of file diff --git a/DomoEspSensor/Utiles.cpp b/DomoEspSensor/Utiles.cpp index 08e9712..eedd064 100644 --- a/DomoEspSensor/Utiles.cpp +++ b/DomoEspSensor/Utiles.cpp @@ -47,6 +47,7 @@ void MqttUtiles::resetFunc() ESP.wdtEnable(1); while(1){}; } + //************************************************************************************************************************************************** WifiManager::WifiManager() { @@ -228,3 +229,116 @@ void MqttManager::OnMqtt(char* topic, byte* payload, unsigned int length) } //************************************************************************************************************************************************** +MelodiaBuzzer::MelodiaBuzzer() +{ + melodia[0]=0; +} +int MelodiaBuzzer::st2nota(char st) +{ + switch(st) + { + case 'd': + return 262; + case 'r': + return 294; + case 'm': + return 330; + case 'f': + return 349; + case 's': + return 392; + case 'l': + return 440; + case 'i': + return 494; + case 'D': + return 277; + case 'R': + return 311; + case 'M': + return 345; + case 'F': + return 350; + case 'S': + return 415; + case 'L': + return 466; + case 'I': + return 554,36; + default: + return 0; + } +} +int MelodiaBuzzer::str2t(char* st, int *t) +{ + char buf[8]; + int n=0; + int i; + while(st[i]<='9' && st[i]>='0') + { + buf[n++]=st[i++]; + } + buf[n]=0; + (*t)=atoi(buf); + return i; +} +void MelodiaBuzzer::Toca(int pin) +{ + int i=0; + int h=0, t=0; + + while (melodia[i]) + { + h=st2nota(melodia[i]); + i++; + t=0; + i+=str2t(&melodia[i], &t); + if(h>0) + tone(pin, h); + else + noTone(pin); + delay(t); + } +} +//************************************************* +char* SonidoBuzzer::getMelSubSt(char* orig, char caracter, int lim, char* bufOut) +{ + char* res; + for(int i=0; i=n) + return; + mel[imel].Toca(pin); +} + +void SonidoBuzzer::Set(int i, char* melo) +{ + if(iAdd(&sv); man->Add(&st); man->Add(&actTimer); + man->Add(&buz); + @@ -149,6 +155,6 @@ class Confdespacho: public DomoEspConfig }; -Confdespacho ConfiguracionActual; +ConfActual ConfiguracionActual; #endif \ No newline at end of file diff --git a/DomoEspSensor/defines.h b/DomoEspSensor/defines.h index e721573..be5eb4f 100644 --- a/DomoEspSensor/defines.h +++ b/DomoEspSensor/defines.h @@ -11,12 +11,13 @@ //sens-------------------- #define MAXSENS 16 //maximo de sensores totales +//actuador #define MAX_ACTIVADORES 5//maximo de activadores por actuador +#define MAX_CHAR_ENVIO 16 - - - -#define MAXINTERRUP 8 +//MELODIA---------------------- +#define MAX_CHAR_MELODIA 64 +#define MAX_MELODIAS 4