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 "alarm.h"
#include "Arduino.h"
#include "sound.h"
#include "soc/rtc_io_reg.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);
uint32_t DemoCounter=0;
class Siren{
class Siren
{
private:
int sirenPin = 25;
AlarmStatus* status;
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);
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...
}
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:
Siren(AlarmStatus* statusRef){
status=statusRef;
}
Siren(AlarmStatus* statusRef, int pin){
status=statusRef;
sirenPin=pin;
Siren(AlarmStatus *statusRef)
{
status = statusRef;
}
void Init(){
// pinMode(sirenPin, OUTPUT);
// DacAudio = XT_DAC_Audio_Class(sirenPin, 0);
/**
* Only use DAC enabled pinout (25 or 26)
*/
Siren(AlarmStatus *statusRef, int pin)
{
status = statusRef;
DacAudio = XT_DAC_Audio_Class(pin, 0);
}
void SoundSiren(){
if(status->muted)
void Init()
{
}
void SoundSiren()
{
if (status->muted)
return;
// playSound();
if(status->silent){
playSilentSound();
return;
}
playSound();
// 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);
Siren siren(&s);
void printStatus(){
void printStatus()
{
String msg = String("Door: ") + String(doorSensor.IsDoorOpen() ? "Open" : "Closed");
msg = msg + " | Alarm: " + String(s.isArmed ? "Armed": "Disarmed");
msg = msg + " | Is Fired: " + String(s.isFired ? "Fired": "Not Fired");
msg = msg + " | Led: " + String(s.ledOn ? "On": "Off");
msg = msg + " | Silent: " + String(s.silent ? "Silent": "Loud");
msg = msg + " | Muted: " + String(s.muted ? "Muted": "Not Muted");
msg = msg + " | Alarm: " + String(s.isArmed ? "Armed" : "Disarmed");
msg = msg + " | Is Fired: " + String(s.isFired ? "Fired" : "Not Fired");
msg = msg + " | Led: " + String(s.ledOn ? "On" : "Off");
msg = msg + " | Silent: " + String(s.silent ? "Silent" : "Loud");
msg = msg + " | Muted: " + String(s.muted ? "Muted" : "Not Muted");
Serial.println(msg);
}
void setup() {
void setup()
{
Serial.begin(115200);
printStatus();
doorSensor.Init();
led.Init();
siren.Init();
s.silent = true;
s.isArmed = doorSensor.IsDoorClosed();
// Serial.println("Alarm is " + String(s.isArmed ? "Armed" : "Disarmed"));
// WiFi.mode(WIFI_STA);
// WiFi.disconnect();
// delay(100);
printStatus();
}
void loop() {
void loop()
{
doorSensor.HandleDoor();
if(s.doorChanged){
if (s.doorChanged)
{
DoorEvent();
}
s.doorChanged=false;
s.doorChanged = false;
if(s.isFired){
s.silent = !s.silent;
if (s.isFired)
{
siren.SoundSiren();
}
led.Update();
// delay(1000);
}
void DoorEvent(){
if(doorSensor.IsDoorOpen()){
void DoorEvent()
{
if (doorSensor.IsDoorOpen())
{
Serial.println("The door-opening");
if(!s.isArmed)
if (!s.isArmed)
{
Serial.println("Alarm is dissarmed, not fireing");
return;
}
Serial.println("Alarm is armed, firing");
s.isFired=true;
}else{
s.isFired = true;
}
else
{
// delay(500);
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