mirror of
https://github.com/Crocmagnon/buzzer.git
synced 2024-11-25 01:18:06 +01:00
Allow changing volume from web interface
This commit is contained in:
parent
f24285ad5e
commit
80373ae5fb
3 changed files with 71 additions and 25 deletions
|
@ -14,14 +14,18 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="w3-margin w3-center w3-card w3-padding-24">
|
<div class="w3-margin w3-center w3-card w3-padding-24">
|
||||||
<h3 class="w3-padding">Contrôles</h3>
|
|
||||||
<button onclick="play()" class="w3-button w3-blue w3-xlarge w3-ripple">Jouer le son</button>
|
<button onclick="play()" class="w3-button w3-blue w3-xlarge w3-ripple">Jouer le son</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="w3-margin w3-center w3-card w3-padding-24">
|
<div class="w3-margin w3-center w3-card w3-padding-24">
|
||||||
<h3 class="w3-padding">Fichiers disponibles</h3>
|
|
||||||
<div id="available-files"></div>
|
<div id="available-files"></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="w3-margin w3-center w3-card w3-padding-24">
|
||||||
|
<button id="volume-decrease" onclick="volume(-1)" class="w3-button w3-blue w3-xlarge w3-ripple">Vol-</button>
|
||||||
|
<span id="volume-current"></span>
|
||||||
|
<button id="volume-increase" onclick="volume(1)" class="w3-button w3-blue w3-xlarge w3-ripple">Vol+</button>
|
||||||
|
</div>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
|
@ -1,25 +1,35 @@
|
||||||
|
const GLOBAL_TIMEOUT = 7000;
|
||||||
|
|
||||||
function play() {
|
function play() {
|
||||||
console.log("Play...");
|
console.log("Play...");
|
||||||
fetch("/play", { signal: AbortSignal.timeout(7000) });
|
fetch("/play", { signal: AbortSignal.timeout(GLOBAL_TIMEOUT) });
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadAvailableFiles() {
|
function volume(modifier) {
|
||||||
console.log("Available files...");
|
const body = new FormData();
|
||||||
fetch("/available-files", { signal: AbortSignal.timeout(7000) })
|
body.set("modifier", modifier);
|
||||||
|
fetch(`/change-volume`, { method: "POST", body: body, signal: AbortSignal.timeout(GLOBAL_TIMEOUT) })
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(handleAvailableFiles);
|
.then(handleStatus);
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadStatus() {
|
||||||
|
console.log("Status...");
|
||||||
|
fetch("/status", { signal: AbortSignal.timeout(GLOBAL_TIMEOUT) })
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(handleStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
function selectFile(name) {
|
function selectFile(name) {
|
||||||
console.log("Select file");
|
console.log("Select file");
|
||||||
const body = new FormData();
|
const body = new FormData();
|
||||||
body.set("fileName", name);
|
body.set("fileName", name);
|
||||||
fetch("/select-file", { method: "POST", body: body, signal: AbortSignal.timeout(7000) })
|
fetch("/select-file", { method: "POST", body: body, signal: AbortSignal.timeout(GLOBAL_TIMEOUT) })
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(handleAvailableFiles);
|
.then(handleStatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleAvailableFiles(data) {
|
function handleStatus(data) {
|
||||||
console.log("data", data);
|
console.log("data", data);
|
||||||
let dom = "";
|
let dom = "";
|
||||||
data.files.forEach(element => {
|
data.files.forEach(element => {
|
||||||
|
@ -31,9 +41,12 @@ function handleAvailableFiles(data) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
document.getElementById("available-files").innerHTML = dom;
|
document.getElementById("available-files").innerHTML = dom;
|
||||||
|
document.getElementById("volume-current").innerText = data.volume.current;
|
||||||
|
document.getElementById("volume-increase").disabled = !data.volume.canIncrease;
|
||||||
|
document.getElementById("volume-decrease").disabled = !data.volume.canDecrease;
|
||||||
}
|
}
|
||||||
|
|
||||||
(() => {
|
(() => {
|
||||||
loadAvailableFiles();
|
loadStatus();
|
||||||
setInterval(loadAvailableFiles, 10000);
|
setInterval(loadStatus, 10000);
|
||||||
})();
|
})();
|
||||||
|
|
55
src/main.cpp
55
src/main.cpp
|
@ -37,11 +37,19 @@ AsyncWebServer server(80);
|
||||||
Audio audio;
|
Audio audio;
|
||||||
|
|
||||||
byte buttonLastState = HIGH;
|
byte buttonLastState = HIGH;
|
||||||
|
byte currentVolume = 15;
|
||||||
|
|
||||||
bool fileIsValid(String fileName) {
|
bool fileIsValid(String fileName) {
|
||||||
return !fileName.startsWith(".") && (fileName.endsWith(".mp3") || fileName.endsWith(".aac") || fileName.endsWith(".wav"));
|
return !fileName.startsWith(".") && (fileName.endsWith(".mp3") || fileName.endsWith(".aac") || fileName.endsWith(".wav"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void displayText(String text) {
|
||||||
|
display.clearDisplay();
|
||||||
|
display.setCursor(0, 0);
|
||||||
|
display.println(text);
|
||||||
|
display.display();
|
||||||
|
}
|
||||||
|
|
||||||
void play()
|
void play()
|
||||||
{
|
{
|
||||||
String path = "/" + selectedFile;
|
String path = "/" + selectedFile;
|
||||||
|
@ -55,9 +63,9 @@ void onPlay(AsyncWebServerRequest *request)
|
||||||
request->send(200);
|
request->send(200);
|
||||||
}
|
}
|
||||||
|
|
||||||
void onAvailableFiles(AsyncWebServerRequest *request)
|
void onStatus(AsyncWebServerRequest *request)
|
||||||
{
|
{
|
||||||
Serial.println("Available files");
|
Serial.println("Status");
|
||||||
AsyncResponseStream *response = request->beginResponseStream("application/json");
|
AsyncResponseStream *response = request->beginResponseStream("application/json");
|
||||||
|
|
||||||
DynamicJsonDocument root(256);
|
DynamicJsonDocument root(256);
|
||||||
|
@ -73,6 +81,12 @@ void onAvailableFiles(AsyncWebServerRequest *request)
|
||||||
file.close();
|
file.close();
|
||||||
file = music.openNextFile();
|
file = music.openNextFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JsonObject volume = root.createNestedObject("volume");
|
||||||
|
volume["current"] = currentVolume;
|
||||||
|
volume["canDecrease"] = currentVolume > 0;
|
||||||
|
volume["canIncrease"] = currentVolume < 21;
|
||||||
|
|
||||||
serializeJson(root, *response);
|
serializeJson(root, *response);
|
||||||
|
|
||||||
request->send(response);
|
request->send(response);
|
||||||
|
@ -85,13 +99,30 @@ void onSelectFile(AsyncWebServerRequest *request)
|
||||||
{
|
{
|
||||||
selectedFile = request->getParam("fileName", true)->value();
|
selectedFile = request->getParam("fileName", true)->value();
|
||||||
Serial.print(selectedFile);
|
Serial.print(selectedFile);
|
||||||
display.clearDisplay();
|
displayText("Selectionne : " + selectedFile);
|
||||||
display.setCursor(0, 0);
|
|
||||||
display.println("Selectionne : "+ selectedFile);
|
|
||||||
display.display();
|
|
||||||
}
|
}
|
||||||
Serial.println();
|
Serial.println();
|
||||||
onAvailableFiles(request);
|
onStatus(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
void onChangeVolume(AsyncWebServerRequest *request)
|
||||||
|
{
|
||||||
|
Serial.print("Volume: ");
|
||||||
|
if (request->hasParam("modifier", true))
|
||||||
|
{
|
||||||
|
String s_modifier = request->getParam("modifier", true)->value();
|
||||||
|
int modifier = s_modifier.toInt();
|
||||||
|
currentVolume += modifier;
|
||||||
|
if (currentVolume > 21)
|
||||||
|
currentVolume = 21;
|
||||||
|
else if (currentVolume < 0)
|
||||||
|
currentVolume = 0;
|
||||||
|
audio.setVolume(currentVolume);
|
||||||
|
Serial.print(currentVolume);
|
||||||
|
displayText("Volume : " + currentVolume);
|
||||||
|
}
|
||||||
|
Serial.println();
|
||||||
|
onStatus(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
|
@ -179,22 +210,20 @@ void setup()
|
||||||
|
|
||||||
// Server
|
// Server
|
||||||
server.on("/play", HTTP_GET, onPlay);
|
server.on("/play", HTTP_GET, onPlay);
|
||||||
server.on("/available-files", HTTP_GET, onAvailableFiles);
|
server.on("/status", HTTP_GET, onStatus);
|
||||||
server.on("/select-file", HTTP_POST, onSelectFile);
|
server.on("/select-file", HTTP_POST, onSelectFile);
|
||||||
|
server.on("/change-volume", HTTP_POST, onChangeVolume);
|
||||||
server.onNotFound([](AsyncWebServerRequest *request)
|
server.onNotFound([](AsyncWebServerRequest *request)
|
||||||
{ request->send(404); });
|
{ request->send(404); });
|
||||||
server.serveStatic("/", SPIFFS, "/www/").setDefaultFile("index.html");
|
server.serveStatic("/", SPIFFS, "/www/").setDefaultFile("index.html");
|
||||||
server.begin();
|
server.begin();
|
||||||
|
|
||||||
Serial.println("Server ready!");
|
Serial.println("Server ready!");
|
||||||
display.clearDisplay();
|
displayText("Pret !");
|
||||||
display.setCursor(0, 0);
|
|
||||||
display.println("Pret !");
|
|
||||||
display.display();
|
|
||||||
|
|
||||||
// Audio
|
// Audio
|
||||||
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
|
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
|
||||||
audio.setVolume(15); // Max 21
|
audio.setVolume(currentVolume); // Max 21
|
||||||
|
|
||||||
// Setup is done, light up the LED
|
// Setup is done, light up the LED
|
||||||
digitalWrite(LED, HIGH);
|
digitalWrite(LED, HIGH);
|
||||||
|
|
Loading…
Reference in a new issue