From b640bce9970e3a654ff29d64979b59593d07ef99 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Mon, 2 Jan 2023 22:47:30 +0100 Subject: [PATCH] Always display wifi info --- .gitignore | 6 +++-- .vscode/settings.json | 4 ++- src/creds.dist.h | 2 +- src/main.cpp | 62 ++++++++++++++++++++++++++++++++----------- 4 files changed, 54 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 6519c69..dd5e2e9 100644 --- a/.gitignore +++ b/.gitignore @@ -36,7 +36,8 @@ .LSOverride # Icon must end with two \r -Icon +Icon + # Thumbnails ._* @@ -58,4 +59,5 @@ Temporary Items .apdisk # End of https://www.toptal.com/developers/gitignore/api/osx -creds.h +creds*.h +!creds.dist.h diff --git a/.vscode/settings.json b/.vscode/settings.json index d4f336c..4865f9c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -8,6 +8,8 @@ "vector": "cpp", "string_view": "cpp", "initializer_list": "cpp", - "regex": "cpp" + "regex": "cpp", + "*.tcc": "cpp", + "system_error": "cpp" } } \ No newline at end of file diff --git a/src/creds.dist.h b/src/creds.dist.h index 8bb4dd3..62fb27c 100644 --- a/src/creds.dist.h +++ b/src/creds.dist.h @@ -1,3 +1,3 @@ -// Copy to creds.h and edit +// Copy to creds.h or creds_ap.h and edit const char *ssid = "buzzer"; const char *password = "123456789"; diff --git a/src/main.cpp b/src/main.cpp index f68a710..e3e9039 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,9 +7,16 @@ #include #include #include -#include "creds.h" -// #define B_WIFI_AP +// Toggle on to switch to AP mode. +// Leave commented for wifi station mode. +//#define B_WIFI_AP + +#ifdef B_WIFI_AP +#include "creds_ap.h" +#else +#include "creds.h" +#endif #define I2S_DOUT 32 #define I2S_BCLK 25 @@ -29,6 +36,12 @@ #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C +#define SCREEN_MSG_X 0 +#define SCREEN_MSG_Y 24 + +#define VOLUME_MIN 0 +#define VOLUME_MAX 21 + Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); String selectedFile = ""; @@ -37,17 +50,22 @@ AsyncWebServer server(80); Audio audio; byte buttonLastState = HIGH; -byte currentVolume = 15; +byte currentVolume = 2; bool fileIsValid(String fileName) { return !fileName.startsWith(".") && (fileName.endsWith(".mp3") || fileName.endsWith(".aac") || fileName.endsWith(".wav")); } +void clearMessageArea() { + display.drawRect(SCREEN_MSG_X, SCREEN_MSG_Y, SCREEN_WIDTH - SCREEN_MSG_X, SCREEN_HEIGHT - SCREEN_MSG_Y, BLACK); + display.display(); + display.setCursor(SCREEN_MSG_X, SCREEN_MSG_Y); +} + void displayText(String text) { - display.clearDisplay(); - display.setCursor(0, 0); - display.println(text); - display.display(); + clearMessageArea(); + display.println(text); + display.display(); } void play() @@ -113,13 +131,16 @@ void onChangeVolume(AsyncWebServerRequest *request) 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; + if (currentVolume > VOLUME_MAX) + currentVolume = VOLUME_MAX; + else if (currentVolume < VOLUME_MIN) + currentVolume = VOLUME_MIN; audio.setVolume(currentVolume); Serial.print(currentVolume); - displayText("Volume : " + currentVolume); + clearMessageArea(); + display.print("Volume : "); + display.println(currentVolume); + display.display(); } Serial.println(); onStatus(request); @@ -140,7 +161,7 @@ void setup() } display.clearDisplay(); display.setTextSize(1); - display.setTextColor(WHITE); + display.setTextColor(WHITE, BLACK); display.setCursor(0, 0); display.println("Chargement..."); display.display(); @@ -185,12 +206,18 @@ void setup() file = root.openNextFile(); } + display.clearDisplay(); + display.setCursor(0, 0); // Wifi #ifdef B_WIFI_AP Serial.println("Setting up AP..."); WiFi.softAP(ssid, password); String wifiIP = WiFi.softAPIP().toString(); String wifiMode = "AP"; + display.print("Wifi: "); + display.println(ssid); + display.print("Pass: "); + display.println(password); #else Serial.print("Connecting to wifi..."); WiFi.begin(ssid, password); @@ -205,7 +232,8 @@ void setup() #endif String wifiMessage = wifiMode + " IP: " + wifiIP; Serial.println(wifiMessage); - display.println(wifiMessage); + display.print("IP: "); + display.println(wifiIP); display.display(); // Server @@ -219,11 +247,13 @@ void setup() server.begin(); Serial.println("Server ready!"); - displayText("Pret !"); + clearMessageArea(); + display.println("Pret !"); + display.display(); // Audio audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT); - audio.setVolume(currentVolume); // Max 21 + audio.setVolume(currentVolume); // Setup is done, light up the LED digitalWrite(LED, HIGH);