Improve siren suport and silent sound

This commit is contained in:
Guillermo Marcel 2025-04-14 11:19:55 -03:00
parent 605fa823c6
commit c40e22c81c
3 changed files with 3606 additions and 77 deletions

View File

@ -1,78 +1,96 @@
#include <XT_DAC_Audio.h> #include <XT_DAC_Audio.h>
#include "alarm.h" #include "alarm.h"
#include "Arduino.h"
#include "sound.h"
#include "soc/rtc_io_reg.h" #include "soc/rtc_io_reg.h"
#include "alarmSound.h" #include "alarmSound.h"
#include "silentSound.h"
// XT_Wav_Class SirenSound(sampleSound);
XT_Wav_Class SirenSound(alarmSound); XT_Wav_Class SirenSound(alarmSound);
XT_Wav_Class SilentSound(silentSound);
XT_DAC_Audio_Class DacAudio(25, 0); XT_DAC_Audio_Class DacAudio(25, 0);
uint32_t DemoCounter=0;
class Siren{ class Siren
{
private: private:
int sirenPin = 25;
AlarmStatus* status; AlarmStatus* status;
float sinVal;
int toneVal;
void playSound(){ void playSound(){
int max = 180; DacAudio.FillBuffer(); // Fill the sound buffer with data
Serial.println("Sound on " + String(sirenPin)); if (SirenSound.Playing == false) // if not playing,
DacAudio.Play(&SirenSound); // play it, this will cause it to repeat and repeat...
if(status->silent)
max = 2;
for(int x = 0; x < max ; x++){
//convert angle of sinusoidal to radian measure
sinVal = (sin(x*(3.1412/180)));
//generate sound of different frequencies by sinusoidal value
toneVal = 2000+(int(sinVal*1000));
//Set a frequency for Pin-out 8
// ledcAttach(sirenPin, toneVal, 3);
tone(sirenPin, toneVal, 3);
// buzzer->tone(toneVal, 3);
// TimerFreeTone(TONE_PIN, toneVal, 2, 5);
delay(2);
}
noTone(sirenPin);
// buzzer->noTone();
// ledcWrite(sirenPin,0); // No sound
// ledcDetach(sirenPin);
} }
void playSilentSound(){
DacAudio.FillBuffer(); // Fill the sound buffer with data
if (SilentSound.Playing == false) // if not playing,
DacAudio.Play(&SilentSound); // play it, this will cause it to repeat and repeat...
}
public: public:
Siren(AlarmStatus* statusRef){ Siren(AlarmStatus *statusRef)
status=statusRef; {
status = statusRef;
} }
Siren(AlarmStatus* statusRef, int pin){ /**
status=statusRef; * Only use DAC enabled pinout (25 or 26)
sirenPin=pin; */
Siren(AlarmStatus *statusRef, int pin)
{
status = statusRef;
DacAudio = XT_DAC_Audio_Class(pin, 0);
} }
void Init()
{
void Init(){
// pinMode(sirenPin, OUTPUT);
// DacAudio = XT_DAC_Audio_Class(sirenPin, 0);
} }
void SoundSiren()
{
void SoundSiren(){ if (status->muted)
if(status->muted)
return; return;
// playSound(); if(status->silent){
playSilentSound();
return;
}
playSound();
// delay(500); // delay(500);
DacAudio.FillBuffer(); // Fill the sound buffer with data
if(SirenSound.Playing==false) // if not playing,
DacAudio.Play(&SirenSound); // play it, this will cause it to repeat and repeat...
Serial.println(DemoCounter++);
} }
}; };
// float sinVal;
// int toneVal;
// void playSound()
// {
// int max = 180;
// Serial.println("Sound on " + String(sirenPin));
// if (status->silent)
// max = 2;
// for (int x = 0; x < max; x++)
// {
// // convert angle of sinusoidal to radian measure
// sinVal = (sin(x * (3.1412 / 180)));
// // generate sound of different frequencies by sinusoidal value
// toneVal = 2000 + (int(sinVal * 1000));
// // Set a frequency for Pin-out 8
// // ledcAttach(sirenPin, toneVal, 3);
// tone(sirenPin, toneVal, 3);
// // buzzer->tone(toneVal, 3);
// // TimerFreeTone(TONE_PIN, toneVal, 2, 5);
// delay(2);
// }
// noTone(sirenPin);
// // buzzer->noTone();
// // ledcWrite(sirenPin,0); // No sound
// // ledcDetach(sirenPin);
// }

View File

@ -10,67 +10,72 @@ DoorSensor doorSensor(&s);
LedController led(&s); LedController led(&s);
Siren siren(&s); Siren siren(&s);
void printStatus(){ void printStatus()
{
String msg = String("Door: ") + String(doorSensor.IsDoorOpen() ? "Open" : "Closed"); String msg = String("Door: ") + String(doorSensor.IsDoorOpen() ? "Open" : "Closed");
msg = msg + " | Alarm: " + String(s.isArmed ? "Armed": "Disarmed"); msg = msg + " | Alarm: " + String(s.isArmed ? "Armed" : "Disarmed");
msg = msg + " | Is Fired: " + String(s.isFired ? "Fired": "Not Fired"); msg = msg + " | Is Fired: " + String(s.isFired ? "Fired" : "Not Fired");
msg = msg + " | Led: " + String(s.ledOn ? "On": "Off"); msg = msg + " | Led: " + String(s.ledOn ? "On" : "Off");
msg = msg + " | Silent: " + String(s.silent ? "Silent": "Loud"); msg = msg + " | Silent: " + String(s.silent ? "Silent" : "Loud");
msg = msg + " | Muted: " + String(s.muted ? "Muted": "Not Muted"); msg = msg + " | Muted: " + String(s.muted ? "Muted" : "Not Muted");
Serial.println(msg); Serial.println(msg);
} }
void setup() {
void setup()
{
Serial.begin(115200); Serial.begin(115200);
printStatus();
doorSensor.Init(); doorSensor.Init();
led.Init(); led.Init();
siren.Init(); siren.Init();
s.silent = true;
s.isArmed = doorSensor.IsDoorClosed(); s.isArmed = doorSensor.IsDoorClosed();
// Serial.println("Alarm is " + String(s.isArmed ? "Armed" : "Disarmed"));
// WiFi.mode(WIFI_STA); // WiFi.mode(WIFI_STA);
// WiFi.disconnect(); // WiFi.disconnect();
// delay(100); // delay(100);
printStatus(); printStatus();
} }
void loop() { void loop()
{
doorSensor.HandleDoor(); doorSensor.HandleDoor();
if(s.doorChanged){ if (s.doorChanged)
{
DoorEvent(); DoorEvent();
} }
s.doorChanged=false; s.doorChanged = false;
if (s.isFired)
if(s.isFired){ {
s.silent = !s.silent;
siren.SoundSiren(); siren.SoundSiren();
} }
led.Update(); led.Update();
// delay(1000); // delay(1000);
} }
void DoorEvent(){ void DoorEvent()
if(doorSensor.IsDoorOpen()){ {
if (doorSensor.IsDoorOpen())
{
Serial.println("The door-opening"); Serial.println("The door-opening");
if(!s.isArmed) if (!s.isArmed)
{ {
Serial.println("Alarm is dissarmed, not fireing"); Serial.println("Alarm is dissarmed, not fireing");
return; return;
} }
Serial.println("Alarm is armed, firing"); Serial.println("Alarm is armed, firing");
s.isFired=true; s.isFired = true;
}else{ }
else
{
// delay(500); // delay(500);
Serial.println("The door-closing"); Serial.println("The door-closing");
s.isArmed=true; s.isArmed = true;
} }
} }

3506
esp32/silentSound.h Normal file

File diff suppressed because it is too large Load Diff