3 Commits

Author SHA1 Message Date
d67dea275e Improved volume control 2023-12-10 17:29:15 +01:00
06da06c519 Updated the Roadmap 2023-12-10 16:03:00 +00:00
763254b603 Added .../playback/<index> endpoint 2023-12-10 17:01:54 +01:00
4 changed files with 46 additions and 54 deletions

View File

@@ -35,12 +35,10 @@ Features, already implemented, or still in progress for the v1.0.0 release!
- [X] /api/v1/playback/pause
- [X] /api/v1/playback/next
- [X] /api/v1/playback/previous
- [ ] /api/v1/playback/&lt;index&gt;
- [X] /api/v1/playback/&lt;index&gt;
- [X] /api/v1/playback/info
- [ ] /api/v1/playback/id3_image
- [X] /api/v1/playlist/get
- [ ] /api/v1/playlist/append
- [ ] /api/v1/playlist/remove
- [X] /api/v1/volume/get
- [X] /api/v1/volume/up
- [X] /api/v1/volume/down

View File

@@ -21,7 +21,7 @@ For more information, please refer to <http://unlicense.org/>
// 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 SD_CS = 5; // 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
// 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)
String currentSongPath; // path to currently playing song
bool audioPlaying = false; // play song or not?

View File

@@ -13,7 +13,7 @@ For more information, please refer to <http://unlicense.org/>
void setupAudio() {
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);
}
@@ -115,11 +115,11 @@ void backwardButtonHandler() {
}
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
currentVolume = newCurrentVolume;
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

@@ -88,7 +88,6 @@ void api_v1_playback_next() {
String content = "\"resource_playlist_index\": ";
content += String(currentPlaylistPosition);
api_server.send(200, "application/json", generate_api_json(true, content));
}
@@ -102,6 +101,33 @@ void api_v1_playback_previous() {
api_server.send(200, "application/json", generate_api_json(true, content));
}
void api_v1_playback_byindex() {
String option = api_server.pathArg(0);
String toIntStr;
Serial.printf("[HTTP] [API] 200 - GET '/api/v1/playback/%s'\n", option);
// convert string to int
for (int i = 0; i < option.length(); i++) {
if (isDigit((char)option[i])) toIntStr += option[i];
else {
api_server.send(200, "application/json", generate_api_json(false));
return;
}
}
int index = toIntStr.toInt();
if (getURLFromPlaylist(currentPlaylist, index) != "") { // if this index exists
currentPlaylistPosition = index - 1;
nextAudio(); // the clean way of playing the next resource
} else {
api_server.send(200, "application/json", generate_api_json(false));
}
String content = "\"resource_playlist_index\": ";
content += String(currentPlaylistPosition);
api_server.send(200, "application/json", generate_api_json(true, content));
}
void api_v1_playback_volume() {
String option = api_server.pathArg(0);
bool success = true;
@@ -119,53 +145,19 @@ void api_v1_playback_volume() {
muted = false;
} else if (option == "get") {
// 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 {
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
String content = "\"volume\": "; // prepare the http response
@@ -254,7 +246,7 @@ void api_v1_playlist_play() {
}
}
if (getURLFromPlaylist(playlistPath, 0) == "") { // if playlist is empty or nonexistent
if (getURLFromPlaylist(playlistPath, 0) == "") { // if playlist is empty or nonexistent
api_server.send(200, "application/json", generate_api_json(false)); // send no success info
} else {
currentPlaylist = playlistPath;
@@ -298,6 +290,7 @@ void setupApiWeb() {
api_server.on("/api/v1/playback/pause", HTTP_GET, api_v1_playback_pause);
api_server.on("/api/v1/playback/next", HTTP_GET, api_v1_playback_next);
api_server.on("/api/v1/playback/previous", HTTP_GET, api_v1_playback_previous);
api_server.on(UriBraces("/api/v1/playback/{}"), HTTP_GET, api_v1_playback_byindex);
api_server.on("/api/v1/playback/info", HTTP_GET, api_v1_playback_info);
api_server.on("/api/v1/playlist/get", HTTP_GET, api_v1_playlist_get);
api_server.on("/api/v1/playlist/create", HTTP_POST, api_v1_playlist_create);