2023年11月29日 星期三

Control Your Music by ESP32 Bluetooth Speaker

Purpose:
利用上一篇文章ESP32 Bluetooth Speaker再加上Play, Pause, Next and Previous的按鍵去控制Mobile APP的動作並且可以自動連接上一個連接過的裝置!

Use the ESP32 Bluetooth Speaker in the previous article add the Play, Pause, Next and Previous buttons to control the actions of the Mobile APP and automatically connect to a connected device!

Fundamental:
Reference https://en.wikipedia.org/wiki/List_of_Bluetooth_profiles

藍牙規範(Bluetooth profile),藍牙技術聯盟定義了許多Profile。Profile目的是要確保Bluetooth裝置間的互通性(interoperability)。但Bluetooth產品無須實現所有的Bluetooth規範Profile。Bluetooth 版本 1.1 定義13個Profiles。
目前WinCE6.0提供的Profile有DUN GATEWAY、HF/740T、Audio Gateway、LAP、PAN、HID。

This Project 用到的BT規範(A2DP and AVRCP):
藍牙立體聲音訊傳輸規範(A2DP
藍牙立體聲音訊傳輸規範(Advance Audio Distribution Profile),規定了使用藍牙非同步傳輸信道方式,傳輸高品質音樂檔案數據的協定堆棧軟體和使用方法,基於該協定就能通過以藍牙方式傳輸高品質的立體聲音樂。分為1.1版和1.2版,只要連接雙方支援A2DP協定都能以16 bits,44.1 kHz的品質傳輸聲音訊號。假如有一方沒有支援A2DP的話,只能以8 bits,8 kHz的品質的免手持裝置規範(Handsfree Profile)傳輸模式,聲音品質會大打折扣。
音訊/影片遠端控制設定檔(AVRCP
音訊/影片遠端控制設定檔(Audio Video Remote Control Profile,簡稱AVRCP)設計用於提供控制 TV、Hi-Fi 裝置等的標準[1]介面。此設定檔用於許可單個遠端控制裝置。

Reference the previous article link on blog:


YouTube Demo:




ESP32 Code:

#include "BluetoothA2DPSink.h"
#include <ezButton.h>

BluetoothA2DPSink a2dp_sink;
esp_a2d_connection_state_t last_state;
bool is_active = true;
int LEDFlag = 0;
#define PLAY      33
#define STOP      32
#define NEXT      22
#define PREVIOUS  23
int bValue_A = 0; // To store value of the button
int bValue_B = 0; // To store value of the button
int bValue_X = 0; // To store value of the button
int bValue_Y = 0; // To store value of the button
ezButton buttonA(PLAY);
ezButton buttonB(STOP);
ezButton buttonX(NEXT);
ezButton buttonY(PREVIOUS);

void avrc_metadata_callback(uint8_t id, const uint8_t *text)
{
  Serial.printf("==> AVRC metadata rsp: attribute id 0x%x, %s\n", id, text);
}
//---------------------------------------------------------------------------------
#ifndef LED_BUILTIN
#define LED_BUILTIN 2
#endif
TaskHandle_t hled;
void initial()
{
  Serial.println(F("Create Task"));
  //--------------- create task----------------------------------
  xTaskCreatePinnedToCore(
    vLEDTask, "LEDTask" // A name just for humans
    ,
    1024 // This stack size can be checked & adjusted by reading the Stack Highwater
    ,
    NULL, 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
    ,
    &hled //handle
    ,
    0);
  //----------------------------------------
  //----------------------------------------------------------------------
}

void setup()
{
  Serial.begin(9600);
  Serial.println(F("init"));
  initial();
  //-----------------------------------------------------------------
  buttonA.setDebounceTime(50); // set debounce time to 50 milliseconds
  buttonB.setDebounceTime(50); // set debounce time to 50 milliseconds
  buttonX.setDebounceTime(50); // set debounce time to 50 milliseconds
  buttonY.setDebounceTime(50); // set debounce time to 50 milliseconds
  static const i2s_config_t i2s_config = {
        .mode = (i2s_mode_t) (I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN),
        .sample_rate = 44100, // corrected by info from bluetooth
        .bits_per_sample = (i2s_bits_per_sample_t) 16, /* the DAC module will only take the 8bits from MSB */
        .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
        .communication_format = (i2s_comm_format_t)I2S_COMM_FORMAT_STAND_MSB,
        .intr_alloc_flags = 0, // default interrupt priority
        .dma_buf_count = 8,
        .dma_buf_len = 64,
        .use_apll = false
    };
  a2dp_sink.set_i2s_config(i2s_config);
  a2dp_sink.set_auto_reconnect(true);
  a2dp_sink.set_avrc_metadata_callback(avrc_metadata_callback);
  a2dp_sink.start("MyMusic");  
}


void loop()
{
  Serial.print(F("Main at core:"));
  Serial.println(xPortGetCoreID());
  while (1)
  {
    buttonA.loop();
    buttonB.loop();
    buttonX.loop();
    buttonY.loop();
    //----------------------------
    bValue_A = buttonA.getState();
    bValue_B = buttonB.getState();
    bValue_X = buttonX.getState();
    bValue_Y = buttonY.getState();
    if (buttonA.isPressed()) {
      Serial.println("The buttonA is pressed");
      Serial.println("play");
      a2dp_sink.play();
      LEDFlag=2;
    }
    if (buttonA.isReleased()) {
      Serial.println("The buttonA is released");
      // TODO do something here
    }
    if (buttonB.isPressed()) {
      Serial.println("The buttonB is pressed");
      Serial.println("pause");
      a2dp_sink.pause();
      LEDFlag=1;
    }
    if (buttonB.isReleased()) {
      Serial.println("The buttonB is released");
      // TODO do something here
    }
    if (buttonX.isPressed()) {
      Serial.println("The buttonX is pressed");
      Serial.println("next");
      a2dp_sink.next();
      LEDFlag=2;
    }
    if (buttonX.isReleased()) {
      Serial.println("The buttonX is released");
      // TODO do something here
    }
    if (buttonY.isPressed()) {
      Serial.println("The buttonY is pressed");
      Serial.println("previous");
      a2dp_sink.previous();
      LEDFlag=2;
    }
    if (buttonY.isReleased()) {
      Serial.println("The buttonY is released");
      // TODO do something here
    }
    //--------------------------------------------------------
    // check state
    esp_a2d_connection_state_t state = a2dp_sink.get_connection_state();
    bool is_connected = state == ESP_A2D_CONNECTION_STATE_CONNECTED;
    if (last_state != state)
    {
      Serial.println(is_connected ? "Connected" : "Not connected");    
      last_state = state;
      if(is_connected)
      {
        LEDFlag=1;
      }
    }
 
    //------------------------------------------
    if (a2dp_sink.get_audio_state()==ESP_A2D_AUDIO_STATE_STARTED)
    {
      //Serial.println("changing state...");
      is_active = !is_active;
   
      if (is_active)
      {
        //Serial.println("play");
        //a2dp_sink.play();
        LEDFlag=2;
      }
      else
      {
        //Serial.println("pause");
        //a2dp_sink.pause();
        //LEDFlag=1;
      }
    }
    delay(10);
  } //---while(1)
 
}

//-------------------------------------------------------------------------
static void vLEDTask(void *pvParameters)
{
  (void)pvParameters;

  Serial.println(F("LEDTask at core:"));
  Serial.println(xPortGetCoreID());
  pinMode(LED_BUILTIN, OUTPUT);
  for (;;) // A Task shall never return or exit.
  {
    if(LEDFlag == 0)
    {
      digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
      vTaskDelay(800);
    }
    else if(LEDFlag == 1)
    {
      digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
      vTaskDelay(800);
      digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
      vTaskDelay(800);
    }
    else if(LEDFlag == 2)
    {
      digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
      vTaskDelay(300);
      digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
      vTaskDelay(300);
    }
    else
    {
      vTaskDelay(10);
    }
  }
}

沒有留言:

張貼留言