From 50ac97a1802185d31be026791de664fcd9a6fd2a Mon Sep 17 00:00:00 2001 From: Rennan Cockles Date: Mon, 9 Sep 2024 18:17:20 -0300 Subject: [PATCH] change boot sound config to a generic sound config --- src/core/globals.h | 2 +- src/core/menu_items/ConfigMenu.cpp | 2 +- src/core/settings.cpp | 22 +++++------ src/core/settings.h | 2 +- src/main.cpp | 6 +-- src/modules/others/audio.cpp | 61 +++++++++++++++++------------- 6 files changed, 52 insertions(+), 43 deletions(-) diff --git a/src/core/globals.h b/src/core/globals.h index 9b43ba2..4960650 100644 --- a/src/core/globals.h +++ b/src/core/globals.h @@ -134,7 +134,7 @@ extern bool isScreenOff; extern bool dimmer; extern int dimmerSet; extern int devMode; -extern int startupSoundEnabled; +extern int soundEnabled; void readFGCOLORFromEEPROM(); diff --git a/src/core/menu_items/ConfigMenu.cpp b/src/core/menu_items/ConfigMenu.cpp index 90813d4..dc36609 100644 --- a/src/core/menu_items/ConfigMenu.cpp +++ b/src/core/menu_items/ConfigMenu.cpp @@ -9,7 +9,7 @@ void ConfigMenu::optionsMenu() { {"Dim Time", [=]() { setDimmerTimeMenu(); saveConfigs();}}, {"Orientation", [=]() { gsetRotation(true); saveConfigs();}}, {"UI Color", [=]() { setUIColor(); saveConfigs();}}, - {"Boot Sound", [=]() { setBootSound(); saveConfigs();}}, + {"Sound On/Off", [=]() { setSoundConfig(); saveConfigs();}}, {"Clock", [=]() { setClock(); }}, {"Sleep", [=]() { setSleepMode(); }}, {"Restart", [=]() { ESP.restart(); }}, diff --git a/src/core/settings.cpp b/src/core/settings.cpp index 40b1142..700985c 100644 --- a/src/core/settings.cpp +++ b/src/core/settings.cpp @@ -276,21 +276,21 @@ void setUIColor(){ } /********************************************************************* -** Function: setBootSound -** Enable or disable boot sound +** Function: setSoundConfig +** Enable or disable sound **********************************************************************/ -void setBootSound() { +void setSoundConfig() { int result = 0; options = { - {"Sound off", [&]() { result = 0; }, startupSoundEnabled == 0}, - {"Sound on", [&]() { result = 1; }, startupSoundEnabled == 1}, + {"Sound off", [&]() { result = 0; }, soundEnabled == 0}, + {"Sound on", [&]() { result = 1; }, soundEnabled == 1}, }; delay(200); - loopOptions(options, startupSoundEnabled); + loopOptions(options, soundEnabled); delay(200); - startupSoundEnabled=result; + soundEnabled=result; } /********************************************************************* @@ -757,9 +757,9 @@ void getConfigs() { if(file) { // init with default settings #if ROTATION >1 - file.print("[{\"rot\":3,\"dimmerSet\":10,\"bright\":100,\"wui_usr\":\"admin\",\"wui_pwd\":\"bruce\",\"Bruce_FGCOLOR\":43023,\"IrTx\":"+String(LED)+",\"IrRx\":"+String(GROVE_SCL)+",\"RfTx\":"+String(GROVE_SDA)+",\"RfRx\":"+String(GROVE_SCL)+",\"tmz\":3,\"RfModule\":0,\"RfFreq\":433.92,\"RfidModule\":"+String(RfidModule)+",\"wifi\":[{\"ssid\":\"myNetSSID\",\"pwd\":\"myNetPassword\"}],\"wigleBasicToken\":\"\",\"devMode\":0,\"startupSoundEnabled\":1}]"); + file.print("[{\"rot\":3,\"dimmerSet\":10,\"bright\":100,\"wui_usr\":\"admin\",\"wui_pwd\":\"bruce\",\"Bruce_FGCOLOR\":43023,\"IrTx\":"+String(LED)+",\"IrRx\":"+String(GROVE_SCL)+",\"RfTx\":"+String(GROVE_SDA)+",\"RfRx\":"+String(GROVE_SCL)+",\"tmz\":3,\"RfModule\":0,\"RfFreq\":433.92,\"RfidModule\":"+String(RfidModule)+",\"wifi\":[{\"ssid\":\"myNetSSID\",\"pwd\":\"myNetPassword\"}],\"wigleBasicToken\":\"\",\"devMode\":0,\"soundEnabled\":1}]"); #else - file.print("[{\"rot\":1,\"dimmerSet\":10,\"bright\":100,\"wui_usr\":\"admin\",\"wui_pwd\":\"bruce\",\"Bruce_FGCOLOR\":43023,\"IrTx\":"+String(LED)+",\"IrRx\":"+String(GROVE_SCL)+",\"RfTx\":"+String(GROVE_SDA)+",\"RfRx\":"+String(GROVE_SCL)+",\"tmz\":3,\"RfModule\":0,\"RfFreq\":433.92,\"RfidModule\":"+String(RfidModule)+",\"wifi\":[{\"ssid\":\"myNetSSID\",\"pwd\":\"myNetPassword\"}],\"wigleBasicToken\":\"\",\"devMode\":0,\"startupSoundEnabled\":1}]"); + file.print("[{\"rot\":1,\"dimmerSet\":10,\"bright\":100,\"wui_usr\":\"admin\",\"wui_pwd\":\"bruce\",\"Bruce_FGCOLOR\":43023,\"IrTx\":"+String(LED)+",\"IrRx\":"+String(GROVE_SCL)+",\"RfTx\":"+String(GROVE_SDA)+",\"RfRx\":"+String(GROVE_SCL)+",\"tmz\":3,\"RfModule\":0,\"RfFreq\":433.92,\"RfidModule\":"+String(RfidModule)+",\"wifi\":[{\"ssid\":\"myNetSSID\",\"pwd\":\"myNetPassword\"}],\"wigleBasicToken\":\"\",\"devMode\":0,\"soundEnabled\":1}]"); #endif } file.close(); @@ -800,7 +800,7 @@ void getConfigs() { if(setting.containsKey("wigleBasicToken")) { wigleBasicToken = setting["wigleBasicToken"].as(); } else { count++; log_i("Fail"); } if(setting.containsKey("devMode")) { devMode = setting["devMode"].as(); } else { count++; log_i("Fail"); } - if(setting.containsKey("startupSoundEnabled")) { startupSoundEnabled = setting["startupSoundEnabled"].as(); } else { count++; log_i("Fail"); } + if(setting.containsKey("soundEnabled")) { soundEnabled = setting["soundEnabled"].as(); } else { count++; log_i("Fail"); } log_i("Brightness: %d", bright); setBrightness(bright); @@ -875,7 +875,7 @@ void saveConfigs() { } setting["wigleBasicToken"] = wigleBasicToken; setting["devMode"] = devMode; - setting["startupSoundEnabled"] = startupSoundEnabled; + setting["soundEnabled"] = soundEnabled; // Open file for writing File file = fs->open(CONFIG_FILE, FILE_WRITE); if (!file) { diff --git a/src/core/settings.h b/src/core/settings.h index c3f8d02..41fd98c 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -44,4 +44,4 @@ void saveConfigs(); void runClockLoop(); -void setBootSound(); +void setSoundConfig(); diff --git a/src/main.cpp b/src/main.cpp index 260a172..06ff79d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,7 +32,7 @@ int dimmerSet; int bright=100; int tmz=3; int devMode=0; -int startupSoundEnabled=1; +int soundEnabled=1; bool interpreter_start = false; bool sdcardMounted = false; bool gpsConnected = false; @@ -127,7 +127,7 @@ void setup_gpio() { #if defined(BACKLIGHT) pinMode(BACKLIGHT, OUTPUT); #endif - //if(RfModule==1) + //if(RfModule==1) initCC1101once(&sdcardSPI); // Sets GPIO in the CC1101 lib } @@ -338,7 +338,7 @@ void setup() { setupSdCard(); getConfigs(); - if (startupSoundEnabled) startup_sound(); + startup_sound(); #if ! defined(HAS_SCREEN) // start a task to handle serial commands while the webui is running diff --git a/src/modules/others/audio.cpp b/src/modules/others/audio.cpp index 716b061..42a96f1 100644 --- a/src/modules/others/audio.cpp +++ b/src/modules/others/audio.cpp @@ -11,10 +11,11 @@ #if defined(HAS_NS4168_SPKR) bool playAudioFile(FS* fs, String filepath) { - + if (!soundEnabled) return false; + AudioFileSource* source = new AudioFileSourceFS(*fs, filepath.c_str()); if(!source) return false; - + AudioOutputI2S* audioout = new AudioOutputI2S(); // https://github.com/earlephilhower/ESP8266Audio/blob/master/src/AudioOutputI2S.cpp#L32 audioout->SetPinout(BCLK, WCLK, DOUT); @@ -24,17 +25,17 @@ bool playAudioFile(FS* fs, String filepath) { filepath.toLowerCase(); // case-insensitive match if (filepath.endsWith(".txt") || filepath.endsWith(".rtttl")) generator = new AudioGeneratorRTTTL(); - if (filepath.endsWith(".wav")) + if (filepath.endsWith(".wav")) generator = new AudioGeneratorWAV(); - if (filepath.endsWith(".mod")) + if (filepath.endsWith(".mod")) generator = new AudioGeneratorMOD(); - if (filepath.endsWith(".opus")) + if (filepath.endsWith(".opus")) generator = new AudioGeneratorOpus(); if (filepath.endsWith(".mp3")) { generator = new AudioGeneratorMP3(); source = new AudioFileSourceID3(source); } - /* 2FIX: compilation issues + /* 2FIX: compilation issues if(filepath.endsWith(".mid")) { // need to load a soundfont AudioFileSource* sf2 = NULL; @@ -45,7 +46,7 @@ bool playAudioFile(FS* fs, String filepath) { midi->SetSoundfont(sf2); generator = midi; } */ - + if (generator && source && audioout) { Serial.println("Start audio"); generator->begin(source, audioout); @@ -59,26 +60,28 @@ bool playAudioFile(FS* fs, String filepath) { delete generator; delete source; delete audioout; - + return true; } - // else + // else return false; // init error } bool playAudioRTTTLString(String song) { + if (!soundEnabled) return false; + // derived from https://github.com/earlephilhower/ESP8266Audio/blob/master/examples/PlayRTTTLToI2SDAC/PlayRTTTLToI2SDAC.ino - + song.trim(); if(song=="") return false; - + AudioOutputI2S* audioout = new AudioOutputI2S(); audioout->SetPinout(BCLK, WCLK, DOUT); - + AudioGenerator* generator = new AudioGeneratorRTTTL(); - + AudioFileSource* source = new AudioFileSourcePROGMEM( song.c_str(), song.length() ); - + if (generator && source && audioout) { Serial.println("Start audio"); generator->begin(source, audioout); @@ -92,7 +95,7 @@ bool playAudioRTTTLString(String song) { delete generator; delete source; delete audioout; - + return true; } // else @@ -100,12 +103,14 @@ bool playAudioRTTTLString(String song) { } bool tts(String text){ + if (!soundEnabled) return false; + text.trim(); if(text=="") return false; - + AudioOutputI2S* audioout = new AudioOutputI2S(); audioout->SetPinout(BCLK, WCLK, DOUT); - + // https://github.com/earlephilhower/ESP8266SAM/blob/master/examples/Speak/Speak.ino audioout->begin(); ESP8266SAM *sam = new ESP8266SAM; @@ -116,25 +121,27 @@ bool tts(String text){ bool isAudioFile(String filepath) { - - return filepath.endsWith(".opus") || filepath.endsWith(".rtttl") || + + return filepath.endsWith(".opus") || filepath.endsWith(".rtttl") || filepath.endsWith(".wav") || filepath.endsWith(".mod") || filepath.endsWith(".mp3") ; } void playTone(unsigned int frequency, unsigned long duration, short waveType) { + if (!soundEnabled) return; + // derived from https://github.com/earlephilhower/ESP8266Audio/blob/master/examples/PlayWAVFromFunction/PlayWAVFromFunction.ino - + if(frequency==0 || duration==0) return; - + float hz = frequency; AudioGeneratorWAV* wav; AudioFileSourceFunction* file; AudioOutputI2S* out = new AudioOutputI2S(); out->SetPinout(BCLK, WCLK, DOUT); - + file = new AudioFileSourceFunction( duration/1000.0); // , 1, 44100 // // you can set (sec, channels, hz, bit/sample) but you should care about @@ -146,14 +153,14 @@ void playTone(unsigned int frequency, unsigned long duration, short waveType) // bit/sample : default = 16 (8, 16, 32) // ===== set your sound function ===== - + if(waveType==0) { // square file->addAudioGenerators([&](const float time) { float v = ( sin(hz * time) >= 0 ) ? 1.0f : -1.0f;; // generate square wave v *= 0.1; // scale return v; }); - } + } else if(waveType==1) { // sine file->addAudioGenerators([&](const float time) { float v = sin(TWO_PI * hz * time); // generate sine wave @@ -170,11 +177,11 @@ void playTone(unsigned int frequency, unsigned long duration, short waveType) wav = new AudioGeneratorWAV(); wav->begin(file, out); - + while (wav->isRunning()) { if (!wav->loop() || checkAnyKeyPress()) wav->stop(); } - + delete file; delete wav; delete out; @@ -184,6 +191,8 @@ void playTone(unsigned int frequency, unsigned long duration, short waveType) void _tone(unsigned int frequency, unsigned long duration) { + if (!soundEnabled) return; + #if defined(BUZZ_PIN) tone(BUZZ_PIN, frequency, duration); #elif defined(HAS_NS4168_SPKR)