From 9c14e86b633b1c1a8a377a190c0f5ed018712dd1 Mon Sep 17 00:00:00 2001 From: grch101 Date: Tue, 9 Feb 2021 20:03:20 +0100 Subject: [PATCH] Feature: Bluetooth --- html/management.html | 1 + platformio.ini | 2 +- src/HTMLmanagement.h | 1 + src/main.cpp | 213 +++++++++++++++++++++++++++++++------------ src/settings.h | 2 +- 5 files changed, 157 insertions(+), 62 deletions(-) diff --git a/html/management.html b/html/management.html index e665d70..296ef23 100644 --- a/html/management.html +++ b/html/management.html @@ -227,6 +227,7 @@ +
diff --git a/platformio.ini b/platformio.ini index b9f1384..f2895ca 100644 --- a/platformio.ini +++ b/platformio.ini @@ -21,7 +21,7 @@ lib_deps_external = ESP Async WebServer https://github.com/me-no-dev/AsyncTCP https://github.com/bblanchon/ArduinoJson.git -; https://github.com/pschatzmann/ESP32-A2DP.git + https://github.com/pschatzmann/ESP32-A2DP.git [env:common] platform = espressif32 diff --git a/src/HTMLmanagement.h b/src/HTMLmanagement.h index b6e44cb..f7bc14b 100644 --- a/src/HTMLmanagement.h +++ b/src/HTMLmanagement.h @@ -227,6 +227,7 @@ static const char management_HTML[] PROGMEM = "\ \ \ \ + \ \ \
\ diff --git a/src/main.cpp b/src/main.cpp index 541710f..962eb4c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,6 +23,7 @@ #include "ESP32FtpServer.h" #endif #ifdef BLUETOOTH_ENABLE + #include "esp_bt.h" #include "BluetoothA2DPSink.h" #endif #include "Audio.h" @@ -88,7 +89,7 @@ char *logBuf = (char*) calloc(serialLoglength, sizeof(char)); // Buffer for all #endif #ifdef BLUETOOTH_ENABLE - BluetoothA2DPSink a2dp_sink; + BluetoothA2DPSink *a2dp_sink; #endif #ifdef MEASURE_BATTERY_VOLTAGE @@ -103,6 +104,10 @@ char *logBuf = (char*) calloc(serialLoglength, sizeof(char)); // Buffer for all bool recoverLastRfid = true; #endif +// Operation Mode +#define OPMODE_NORMAL 0 // Normal mode +#define OPMODE_BLUETOOTH 1 // Bluetooth mode. WiFi is deactivated. Music from SD can't be played. + // Track-Control #define STOP 1 // Stop play #define PLAY 2 // Start play (currently not used) @@ -138,6 +143,7 @@ char *logBuf = (char*) calloc(serialLoglength, sizeof(char)); // Buffer for all #define REPEAT_TRACK 111 // Changes active playmode to endless-loop (for a single track) #define DIMM_LEDS_NIGHTMODE 120 // Changes LED-brightness #define TOGGLE_WIFI_STATUS 130 // Toggles WiFi-status +#define TOGGLE_BLUETOOTH_MODE 140 // Toggles Normal/Bluetooth Mode // Repeat-Modes #define NO_REPEAT 0 // No repeat @@ -170,6 +176,9 @@ typedef struct { char nvsEntry[275]; } nvs_t; +// Operation Mode +volatile uint8_t operationMode; + // Configuration of initial values (for the first start) goes here.... // There's no need to change them here as they can be configured via webinterface // Neopixel @@ -408,6 +417,9 @@ void volumeHandler(const int32_t _minVolume, const int32_t _maxVolume); void volumeToQueueSender(const int32_t _newVolume); wl_status_t wifiManager(void); bool writeWifiStatusToNVS(bool wifiStatus); +void bluetoothHandler(void); +uint8_t readOperationModeFromNVS(void); +bool setOperationMode(uint8_t operationMode); /* Wrapper-function for serial-logging (with newline) @@ -1849,7 +1861,7 @@ void showLed(void *parameter) { static unsigned long lastSwitchTimestamp = 0; static bool redrawProgress = false; static uint8_t lastLedBrightness = ledBrightness; - + static CRGB::HTMLColorCode idleColor; static CRGB leds[NUM_LEDS]; FastLED.addLeds(leds, NUM_LEDS).setCorrection( TypicalSMD5050 ); FastLED.setBrightness(ledBrightness); @@ -2075,19 +2087,32 @@ void showLed(void *parameter) { switch (playProperties.playMode) { case NO_PLAYLIST: // If no playlist is active (idle) + #ifdef BLUETOOTH_ENABLE + if(operationMode == OPMODE_BLUETOOTH ) { + idleColor = CRGB::Blue; + } else { + #endif + if(wifiManager() == WL_CONNECTED) { + idleColor = CRGB::White; + } else { + idleColor = CRGB::Green; + } + #ifdef BLUETOOTH_ENABLE + } + #endif if (hlastVolume == currentVolume && lastLedBrightness == ledBrightness) { for (uint8_t i=0; iset_pin_config(pin_config); + a2dp_sink->start("ESPuino"); + } else { + esp_bt_mem_release(ESP_BT_MODE_BTDM); + #endif + xTaskCreatePinnedToCore( + playAudio, /* Function to implement the task */ + "mp3play", /* Name of the task */ + 11000, /* Stack size in words */ + NULL, /* Task input parameter */ + 2 | portPRIVILEGE_BIT, /* Priority of the task */ + &mp3Play, /* Task handle. */ + 1 /* Core where the task should run */ + ); + wifiManager(); + #ifdef BLUETOOTH_ENABLE + } + #endif lastTimeActiveTimestamp = millis(); // initial set after boot @@ -4498,49 +4569,71 @@ void setup() { Serial.printf("PSRAM: %u bytes\n", ESP.getPsramSize()); } +#ifdef BLUETOOTH_ENABLE +void bluetoothHandler(void) { + esp_a2d_audio_state_t state = a2dp_sink->get_audio_state(); + // Reset Sleep Timer when audio is playing + if(state == ESP_A2D_AUDIO_STATE_STARTED) { + lastTimeActiveTimestamp = millis(); + } +} +#endif void loop() { - webserverStart(); - #ifdef FTP_ENABLE - ftpManager(); + + #ifdef BLUETOOTH_ENABLE + if(operationMode == OPMODE_BLUETOOTH) { + bluetoothHandler(); + } else { #endif + webserverStart(); + #ifdef FTP_ENABLE + ftpManager(); + #endif + volumeHandler(minVolume, maxVolume); + if (wifiManager() == WL_CONNECTED) { + #ifdef MQTT_ENABLE + if (enableMqtt) { + reconnect(); + MQTTclient.loop(); + postHeartbeatViaMqtt(); + } + #endif + #ifdef FTP_ENABLE + if (ftpEnableLastStatus && ftpEnableCurrentStatus) { + ftpSrv->handleFTP(); + } + #endif + } + #ifdef FTP_ENABLE + if (ftpEnableLastStatus && ftpEnableCurrentStatus) { + if (ftpSrv->isConnected()) { + lastTimeActiveTimestamp = millis(); // Re-adjust timer while client is connected to avoid ESP falling asleep + } + } + #endif + ws.cleanupClients(); + #ifdef BLUETOOTH_ENABLE + } + #endif + + #ifdef HEADPHONE_ADJUST_ENABLE headphoneVolumeManager(); #endif #ifdef MEASURE_BATTERY_VOLTAGE batteryVoltageTester(); #endif - volumeHandler(minVolume, maxVolume); buttonHandler(); doButtonActions(); sleepHandler(); deepSleepManager(); rfidPreferenceLookupHandler(); - if (wifiManager() == WL_CONNECTED) { - #ifdef MQTT_ENABLE - if (enableMqtt) { - reconnect(); - MQTTclient.loop(); - postHeartbeatViaMqtt(); - } - #endif - #ifdef FTP_ENABLE - if (ftpEnableLastStatus && ftpEnableCurrentStatus) { - ftpSrv->handleFTP(); - } - #endif - } - #ifdef FTP_ENABLE - if (ftpEnableLastStatus && ftpEnableCurrentStatus) { - if (ftpSrv->isConnected()) { - lastTimeActiveTimestamp = millis(); // Re-adjust timer while client is connected to avoid ESP falling asleep - } - } - #endif + #ifdef PLAY_LAST_RFID_AFTER_REBOOT recoverLastRfidPlayed(); #endif - ws.cleanupClients(); + } diff --git a/src/settings.h b/src/settings.h index b1e57ce..a79fad8 100644 --- a/src/settings.h +++ b/src/settings.h @@ -29,7 +29,7 @@ //#define PLAY_LAST_RFID_AFTER_REBOOT // When restarting ESPuino, the last RFID that was active before, is recalled and played //#define USE_LAST_VOLUME_AFTER_REBOOT // Remembers the volume used at last shutdown after reboot -//#define BLUETOOTH_ENABLE // Doesn't work currently (so don't enable) as there's not enough DRAM available +#define BLUETOOTH_ENABLE // Doesn't work currently (so don't enable) as there's not enough DRAM available //################## select SD card mode #############################