From 0b2a0c702ad08bed820a7dbfaa248f0a892495a5 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Mon, 2 Jan 2023 15:42:39 +0100 Subject: [PATCH] Trigger play when button press --- src/main.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index dcc48bb..b63e968 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,17 +12,25 @@ #define I2S_LRC 26 #define LED 2 +#define BUTTON 18 String selectedFile = ""; AsyncWebServer server(80); Audio audio; -void onPlay(AsyncWebServerRequest *request) +byte buttonLastState = HIGH; + +void play() { String path = "/music/" + selectedFile; Serial.println("Playing file: " + path); audio.connecttoFS(SPIFFS, path.c_str()); +} + +void onPlay(AsyncWebServerRequest *request) +{ + play(); request->send(200); } @@ -68,6 +76,8 @@ void setup() pinMode(LED, OUTPUT); digitalWrite(LED, LOW); + pinMode(BUTTON, INPUT_PULLUP); + // Setup SPIFFS if (!SPIFFS.begin()) { @@ -128,4 +138,8 @@ void setup() void loop() { + byte buttonCurrentState = digitalRead(BUTTON); + if (buttonCurrentState == LOW && buttonLastState == HIGH) + play(); + buttonLastState = buttonCurrentState; }