2 Commits

3 changed files with 135 additions and 5 deletions

View File

@@ -45,6 +45,14 @@ Features, already implemented, or still in progress for the v1.0.0 release!
- [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

@@ -51,6 +51,9 @@ const String apPSK = "aA16161Aa"; // pre-shared-key of access
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

@@ -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 '/'");
@@ -128,7 +133,7 @@ void api_v1_playback_byindex() {
api_server.send(200, "application/json", generate_api_json(true, content));
}
void api_v1_playback_volume() {
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);
@@ -167,7 +172,7 @@ void api_v1_playback_volume() {
api_server.send(200, "application/json", generate_api_json(success, content)); // generate json and send it
}
void api_v1_playback_balance() {
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);
@@ -198,6 +203,115 @@ void api_v1_playback_balance() {
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
@@ -326,8 +440,13 @@ void setupApiWeb() {
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/balance/{}"), HTTP_GET, api_v1_playback_balance);
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));