Improved volume control

This commit is contained in:
2023-12-10 17:29:15 +01:00
parent 06da06c519
commit d67dea275e
3 changed files with 17 additions and 51 deletions

View File

@@ -21,7 +21,7 @@ For more information, please refer to <http://unlicense.org/>
// define all constants // define all constants
const String version = "NetSpeaker v0.2.0-dev"; // version string used e.g. for access point ssid when wifi couldn't connect const String version = "NetSpeaker v0.2.2-dev"; // version string used e.g. for access point ssid when wifi couldn't connect
const int operation_mode = 0; // 0: interconnected (no buttons, but api and wifi); 1: standalone (no wifi; no api) const int operation_mode = 0; // 0: interconnected (no buttons, but api and wifi); 1: standalone (no wifi; no api)
const int SD_CS = 5; // BOARD SPECIFIC const int SD_CS = 5; // BOARD SPECIFIC
const int SPI_MISO = 19; // BOARD SPECIFIC const int SPI_MISO = 19; // BOARD SPECIFIC
@@ -48,7 +48,8 @@ const String apSSID = version; // ssid of access point open
const String apPSK = "aA16161Aa"; // pre-shared-key of access point opened when not able to connect to wifi const String apPSK = "aA16161Aa"; // pre-shared-key of access point opened when not able to connect to wifi
// create all needed variables // create all needed variables
int currentVolume = 20; // variable where current volume (0...20) is stored int currentVolume = 50; // variable where current volume (0...20) is stored
int maxVolume = 100; // defines the volume steps (max. 255)
bool muted = false; // currently muted (does not affect currentVolume) bool muted = false; // currently muted (does not affect currentVolume)
String currentSongPath; // path to currently playing song String currentSongPath; // path to currently playing song
bool audioPlaying = false; // play song or not? bool audioPlaying = false; // play song or not?

View File

@@ -13,7 +13,7 @@ For more information, please refer to <http://unlicense.org/>
void setupAudio() { void setupAudio() {
audio.setPinout(I2S_BLCK, I2S_LRC, I2S_DOUT); // tell the audio library what output pins to use audio.setPinout(I2S_BLCK, I2S_LRC, I2S_DOUT); // tell the audio library what output pins to use
audio.setVolumeSteps(20); audio.setVolumeSteps(maxVolume);
Serial.printf("[SETUP] Set up audio card successfully (Pins: BLCK %d | LRC %d | DOUT %d)\n", I2S_BLCK, I2S_LRC, I2S_DOUT); Serial.printf("[SETUP] Set up audio card successfully (Pins: BLCK %d | LRC %d | DOUT %d)\n", I2S_BLCK, I2S_LRC, I2S_DOUT);
} }
@@ -115,11 +115,11 @@ void backwardButtonHandler() {
} }
void setAudioVolume() { void setAudioVolume() {
int newCurrentVolume = analogRead(audioVolumePin) / 204.75; // read voltage from audioVolumePin and divide by 204.75 (min. volume is 0, max. 20; 20 fits 204.75 times into 4095 (maximum input)) int newCurrentVolume = analogRead(audioVolumePin) / (4095/maxVolume); // read voltage from audioVolumePin and divide by the calculated steps (4095 is the maximum input)
if (currentVolume != newCurrentVolume) { // just do it if the volume changed if (currentVolume != newCurrentVolume) { // just do it if the volume changed
currentVolume = newCurrentVolume; currentVolume = newCurrentVolume;
audio.setVolume(currentVolume); // set volume audio.setVolume(currentVolume); // set volume
Serial.printf("[INFO] Set volume to %d/20!\n", currentVolume); Serial.printf("[INFO] Set volume to %d/%d!\n", currentVolume, maxVolume);
} }
} }

View File

@@ -114,7 +114,6 @@ void api_v1_playback_byindex() {
return; return;
} }
} }
int index = toIntStr.toInt(); int index = toIntStr.toInt();
if (getURLFromPlaylist(currentPlaylist, index) != "") { // if this index exists if (getURLFromPlaylist(currentPlaylist, index) != "") { // if this index exists
@@ -146,53 +145,19 @@ void api_v1_playback_volume() {
muted = false; muted = false;
} else if (option == "get") { } else if (option == "get") {
// just here that no 'success: false' is sent // just here that no 'success: false' is sent
} else if (option == "0") {
currentVolume = 0;
} else if (option == "1") {
currentVolume = 1;
} else if (option == "2") {
currentVolume = 2;
} else if (option == "3") {
currentVolume = 3;
} else if (option == "4") {
currentVolume = 4;
} else if (option == "5") {
currentVolume = 5;
} else if (option == "6") {
currentVolume = 6;
} else if (option == "7") {
currentVolume = 7;
} else if (option == "8") {
currentVolume = 8;
} else if (option == "9") {
currentVolume = 9;
} else if (option == "10") {
currentVolume = 10;
} else if (option == "11") {
currentVolume = 11;
} else if (option == "12") {
currentVolume = 12;
} else if (option == "13") {
currentVolume = 13;
} else if (option == "14") {
currentVolume = 14;
} else if (option == "15") {
currentVolume = 15;
} else if (option == "16") {
currentVolume = 16;
} else if (option == "17") {
currentVolume = 17;
} else if (option == "18") {
currentVolume = 18;
} else if (option == "19") {
currentVolume = 19;
} else if (option == "20") {
currentVolume = 20;
} else { } else {
success = false; String toIntStr;
int volumeLevel;
// convert string to int
for (int i = 0; i < option.length(); i++) {
if (isDigit((char)option[i])) toIntStr += option[i];
else success = false;
}
if (success && toIntStr.toInt() <= maxVolume) currentVolume = toIntStr.toInt();
else success = false;
} }
if (!muted) audio.setVolume(currentVolume); // set volume if not muted if (!muted) audio.setVolume(currentVolume); // set volume if not muted and in range
else audio.setVolume(0); // if muted, set volume to 0 else audio.setVolume(0); // if muted, set volume to 0
String content = "\"volume\": "; // prepare the http response String content = "\"volume\": "; // prepare the http response