Skip to content

Commit

Permalink
change boot sound config to a generic sound config
Browse files Browse the repository at this point in the history
  • Loading branch information
rennancockles committed Sep 9, 2024
1 parent a3c0493 commit 50ac97a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/core/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ extern bool isScreenOff;
extern bool dimmer;
extern int dimmerSet;
extern int devMode;
extern int startupSoundEnabled;
extern int soundEnabled;

void readFGCOLORFromEEPROM();

Expand Down
2 changes: 1 addition & 1 deletion src/core/menu_items/ConfigMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }},
Expand Down
22 changes: 11 additions & 11 deletions src/core/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/*********************************************************************
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -800,7 +800,7 @@ void getConfigs() {
if(setting.containsKey("wigleBasicToken")) { wigleBasicToken = setting["wigleBasicToken"].as<String>(); } else { count++; log_i("Fail"); }

if(setting.containsKey("devMode")) { devMode = setting["devMode"].as<int>(); } else { count++; log_i("Fail"); }
if(setting.containsKey("startupSoundEnabled")) { startupSoundEnabled = setting["startupSoundEnabled"].as<int>(); } else { count++; log_i("Fail"); }
if(setting.containsKey("soundEnabled")) { soundEnabled = setting["soundEnabled"].as<int>(); } else { count++; log_i("Fail"); }

log_i("Brightness: %d", bright);
setBrightness(bright);
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ void saveConfigs();

void runClockLoop();

void setBootSound();
void setSoundConfig();
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
61 changes: 35 additions & 26 deletions src/modules/others/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -92,20 +95,22 @@ bool playAudioRTTTLString(String song) {
delete generator;
delete source;
delete audioout;

return true;
}
// else
return false; // init error
}

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;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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)
Expand Down

0 comments on commit 50ac97a

Please sign in to comment.