6 Commits

4 changed files with 213 additions and 57 deletions

View File

@@ -35,17 +35,24 @@ 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/<index>
- [X] /api/v1/playback/<index>
- [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
- [X] /api/v1/volume/mute
- [X] /api/v1/volume/<0-20>
- [X] /api/v1/balance/<0-32>
- [X] /api/v1/balance/get
- [X] /api/v1/eq/get
- [X] /api/v1/eq/low/get
- [X] /api/v1/eq/low/<0-46>
- [X] /api/v1/eq/mid/get
- [X] /api/v1/eq/mid/<0-46>
- [X] /api/v1/eq/high/get
- [X] /api/v1/eq/high/<0-46>
- [X] /api/v1/settings/restart/
- [ ] /api/v1/system/name
- [ ] /api/v1/system/info

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,12 @@ 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)
int balanceLevel = 0; // left-right balance between -16 to 16 inclusive (both)
int eqLow = 0; // equalizer low value between -40 and 6dB
int eqMid = 0; // equalizer mid value between -40 and 6dB
int eqHigh = 0; // equalizer high value between -40 and 6dB
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

@@ -32,7 +32,12 @@ String generate_api_json(bool success) { // if you just want the basic json fra
String generate_api_json(bool success, String content) { // when info is to be embedded
return generate_api_json(success, content, false);
}
bool isStringConvertable2Int(String toIntStr) {
for (int i = 0; i < toIntStr.length(); i++) { // check if a non-digit character is in the given string
if (!isDigit((char)toIntStr[i])) return false;
}
return true;
}
void configRoot() {
Serial.println("[HTTP] [Config] 200 - GET '/'");
@@ -88,7 +93,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,7 +106,34 @@ void api_v1_playback_previous() {
api_server.send(200, "application/json", generate_api_json(true, content));
}
void api_v1_playback_volume() {
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_volume() {
String option = api_server.pathArg(0);
bool success = true;
Serial.printf("[HTTP] [API] 200 - GET '/api/v1/volume/%s'\n", option);
@@ -119,53 +150,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
@@ -175,6 +172,146 @@ void api_v1_playback_volume() {
api_server.send(200, "application/json", generate_api_json(success, content)); // generate json and send it
}
void api_v1_balance() {
String option = api_server.pathArg(0);
bool success = true;
Serial.printf("[HTTP] [API] 200 - GET '/api/v1/balance/%s'\n", option);
if (option == "right") {
balanceLevel--;
if (balanceLevel < -16) balanceLevel = -16;
} else if (option == "left") {
balanceLevel++;
if (balanceLevel > 16) balanceLevel = 16;
} else if (option == "get") {
// just here that no 'success: false' is sent
} else {
String toIntStr; // between 0 and 32
// 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() <= 32) balanceLevel = toIntStr.toInt() - 16; // as the input string is between 0 and 32
else success = false;
}
audio.setBalance(balanceLevel); // set volume if not muted and in range
String content = "\"balance\": "; // prepare the http response
content += String(balanceLevel);
api_server.send(200, "application/json", generate_api_json(success, content)); // generate json and send it
}
void api_v1_eq_get() {
Serial.println("[HTTP] [API] 200 - GET '/api/v1/eq/get");
String content = "\"equalizer_low\": "; // prepare the http response
content += String(eqLow);
content += ", \"equalizer_mid\": "; // prepare the http response
content += String(eqMid);
content += ", \"equalizer_high\": "; // prepare the http response
content += String(eqHigh);
api_server.send(200, "application/json", generate_api_json(true, content)); // generate json and send it
}
void api_v1_eq_reset() {
Serial.println("[HTTP] [API] 200 - GET '/api/v1/eq/reset");
eqLow = 0;
eqMid = 0;
eqHigh = 0;
audio.setTone(0, 0, 0);
Serial.printf("[INFO] Set balance to %ddB | %ddB | %ddB!\n", eqLow, eqMid, eqHigh);
String content = "\"equalizer_low\": "; // prepare the http response
content += String(eqLow);
content += ", \"equalizer_mid\": "; // prepare the http response
content += String(eqMid);
content += ", \"equalizer_high\": "; // prepare the http response
content += String(eqHigh);
api_server.send(200, "application/json", generate_api_json(true, content)); // generate json and send it
}
void api_v1_eq_low() {
String option = api_server.pathArg(0);
bool success = true;
Serial.printf("[HTTP] [API] 200 - GET '/api/v1/eq/low/%s'\n", option);
if (option == "get") {
// just here to make /get available
} else if (option == "reset") {
eqLow = 0;
} else if (isStringConvertable2Int(option)) {
int res = option.toInt();
if (res > 46) { // the maximum is 6dB and there will be 40 subtracted one line beneath
success = false;
} else {
eqLow = res - 40;
Serial.printf("[INFO] Set balance to %ddB | %ddB | %ddB!\n", eqLow, eqMid, eqHigh);
}
} else success = false;
audio.setTone(eqLow, eqMid, eqHigh); // set volume if not muted and in range
String content = "\"equalizer_low\": "; // prepare the http response
content += String(eqLow);
api_server.send(200, "application/json", generate_api_json(success, content)); // generate json and send it
}
void api_v1_eq_mid() {
String option = api_server.pathArg(0);
bool success = true;
Serial.printf("[HTTP] [API] 200 - GET '/api/v1/eq/mid/%s'\n", option);
if (option == "get") {
// just here to make /get available
} else if (option == "reset") {
eqMid = 0;
} else if (isStringConvertable2Int(option)) {
int res = option.toInt();
if (res > 46) { // the maximum is 6dB and there will be 40 subtracted one line beneath
success = false;
} else {
eqMid = res - 40;
Serial.printf("[INFO] Set balance to %ddB | %ddB | %ddB!\n", eqLow, eqMid, eqHigh);
}
} else success = false;
audio.setTone(eqLow, eqMid, eqHigh); // set volume if not muted and in range
String content = "\"equalizer_mid\": "; // prepare the http response
content += String(eqMid);
api_server.send(200, "application/json", generate_api_json(success, content)); // generate json and send it
}
void api_v1_eq_high() {
String option = api_server.pathArg(0);
bool success = true;
Serial.printf("[HTTP] [API] 200 - GET '/api/v1/eq/high/%s'\n", option);
if (option == "get") {
// just here to make /get available
} else if (option == "reset") {
eqHigh = 0;
} else if (isStringConvertable2Int(option)) {
int res = option.toInt();
if (res > 46) { // the maximum is 6dB and there will be 40 subtracted one line beneath
success = false;
} else {
eqHigh = res - 40;
Serial.printf("[INFO] Set balance to %dB | %ddB | %ddB!\n", eqLow, eqMid, eqHigh);
}
} else success = false;
audio.setTone(eqLow, eqMid, eqHigh); // set volume if not muted and in range
String content = "\"equalizer_high\": "; // prepare the http response
content += String(eqHigh);
api_server.send(200, "application/json", generate_api_json(success, content)); // generate json and send it
}
void api_v1_playback_info() {
Serial.println("[HTTP] [API] 200 - GET '/api/v1/playback/info'");
String content = "\"resource_path\": \"" + pbInfo.resourcePath + "\", "; // resource's path
@@ -254,7 +391,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;
@@ -299,10 +436,17 @@ void setupApiWeb() {
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("/api/v1/playback/info", HTTP_GET, api_v1_playback_info);
api_server.on(UriBraces("/api/v1/playback/{}"), HTTP_GET, api_v1_playback_byindex);
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);
api_server.on("/api/v1/playlist/play", HTTP_POST, api_v1_playlist_play);
api_server.on(UriBraces("/api/v1/volume/{}"), HTTP_GET, api_v1_playback_volume);
api_server.on(UriBraces("/api/v1/volume/{}"), HTTP_GET, api_v1_volume);
api_server.on(UriBraces("/api/v1/balance/{}"), HTTP_GET, api_v1_balance);
api_server.on(UriBraces("/api/v1/eq/get"), HTTP_GET, api_v1_eq_get);
api_server.on(UriBraces("/api/v1/eq/reset"), HTTP_GET, api_v1_eq_reset);
api_server.on(UriBraces("/api/v1/eq/low/{}"), HTTP_GET, api_v1_eq_low);
api_server.on(UriBraces("/api/v1/eq/mid/{}"), HTTP_GET, api_v1_eq_mid);
api_server.on(UriBraces("/api/v1/eq/high/{}"), HTTP_GET, api_v1_eq_high);
api_server.on("/api/v1/settings/restart", HTTP_GET, api_v1_settings_restart);
Serial.println("[HTTP] [API] Starting API server (http) on port " + String(webport_api));