Page 1 of 1

Send other data during audio streaming

Posted: Mon Mar 16, 2020 5:02 am
by JayceCao
Dear all,

I am using the pipelink_a2dp_sink_stream example to streaming music from my phone.
During the audio streaming, I want to send a small data, cmd ... to control the on/off of LEDs.
I would like to ask about the method or any sample to send data/command during the audio streaming task.

Best regard!

Re: Send other data during audio streaming

Posted: Mon Mar 16, 2020 9:32 am
by Jakobsen
Hi Cao
I have successfully setup a SPP link between my phone an ESP32.
On this link I do my normal control communication stuff. Volume, UV and amp power mode feedback, DSP control.
It is a simple mixup of the A2DP and SPP examples from the Bluetooth classics repo. I have coded my stuff in ESP-IDF not ADF.

/J

Code: Select all

// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "esp_system.h"
#include "esp_log.h"

#include "esp_bt.h"
#include "bt_app_core.h"
#include "bt_app_av.h"
#include "esp_bt_main.h"
#include "esp_bt_device.h"
#include "esp_gap_bt_api.h"
#include "esp_spp_api.h"
#include "esp_a2dp_api.h"
#include "esp_avrc_api.h"
#include "driver/i2s.h"
#include <driver/adc.h>
#include "dsps_biquad_gen.h"

#include "MerusAudio.h"
#include "ma120x0_all.h"

uint32_t spp_handle = 0; 

static const esp_spp_mode_t esp_spp_mode = ESP_SPP_MODE_CB;

typedef struct ptype { 
  int filtertype;
  float freq;
  float q;
  float coeffs[5];
  float w[2];  
} ptype_t;

typedef struct pnode {
    ptype_t process;
    struct pnode *next; 
} pnode_t;


/* event for handler "bt_av_hdl_stack_up */
enum {
    BT_APP_EVT_STACK_UP = 0,
};

/* handler for bluetooth stack enabled events */
static void bt_av_hdl_stack_evt(uint16_t event, void *p_param);
static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param);

uint8_t muteCH[2] = {0,0}; 
uint8_t DSPmode = 1;

ptype_t bq[4];
float freq = 2000;
void app_main()
{
    /* Initialize NVS — it is used to store PHY calibration data */
    esp_err_t err = nvs_flash_init();
    if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        err = nvs_flash_init();
    }
    ESP_ERROR_CHECK(err);
    // pull enable and mute


    gpio_config_t io_conf;

    io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
  	io_conf.mode = GPIO_MODE_OUTPUT;
  	io_conf.pin_bit_mask = (1ULL<<15 | 1ULL<<13);
	io_conf.pull_down_en = 0;
    io_conf.pull_up_en = 0;

    gpio_config(&io_conf);
    gpio_set_level(13, 0);
	  gpio_set_level(15, 1);
	  printf("Disable device \n");

	  vTaskDelay(1000 / portTICK_RATE_MS);

	  gpio_set_level(15, 0);
	  printf("Enable device \n");
    vTaskDelay(1000 / portTICK_RATE_MS);

    i2c_master_init();
    printf("Hardware tick Freertos %d\n",portTICK_RATE_MS);

   	uint8_t res = ma_read_byte(MA_hw_version__a);
	  printf("Hardware version: 0x%02x\n",res);

	  ma_write_byte(MA_i2s_format__a,8);          // Set i2s left justified, set audio_proc_enable
      ma_write_byte(MA_vol_db_master__a,0x40);    // Set vol_db_master low

	  res = ma_read_byte(MA_error__a);
	  printf("Errors : 0x%02x\n",res);

	  res = ma_read_byte(116);
	  printf("Audio in mode : 0x%02x\n",res);

  	  ma_write_byte(45,0x34);
	  ma_write_byte(45,0x30);

  	  printf("Init done\n");
      gpio_set_level(13, 1);
	  printf("Unmute \n");

    // Setup audio process flow 
     float f = freq/44100/2; 
     
     bq[0] = (ptype_t) { 1, f, 0.707 , {0,0,0,0,0}, {0, 0} } ;
     bq[1] = (ptype_t) { 1, f, 0.707 , {0,0,0,0,0}, {0, 0} } ;
     bq[2] = (ptype_t) { 2, f, 0.707 , {0,0,0,0,0}, {0, 0} } ;
     bq[3] = (ptype_t) { 2, f, 0.707 , {0,0,0,0,0}, {0, 0} } ;
     
     pnode_t * aflow = NULL;
     aflow = malloc(sizeof(pnode_t));
     if (aflow == NULL) 
     { printf("Could not create node"); 
     }   
    
     for (uint8_t n=0; n<=3; n++)
     { switch (bq[n].filtertype) {
         case 1: err = dsps_biquad_gen_lpf_f32( bq[n].coeffs, bq[n].freq, bq[n].q );
                 break;
         case 2: err = dsps_biquad_gen_hpf_f32( bq[n].coeffs, bq[n].freq, bq[n].q );
                 break;
         default : break;
       }  
       for (uint8_t i = 0;i <=4 ;i++ )
       {  printf("%.6f ",bq[n].coeffs[i]);
       }  
       printf("\n");     
     } 


    i2s_config_t i2s_config = {
        .mode = I2S_MODE_MASTER | I2S_MODE_TX,                                  // Only TX
        .sample_rate = 48000,
        .bits_per_sample = 32,
        .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,                           //2-channels
        .communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
        .dma_buf_count = 6,
        .dma_buf_len = 60,                                                      //
        .intr_alloc_flags = 0,
        .use_apll = true,
        .tx_desc_auto_clear = true,
        .fixed_mclk = 0
    };


    i2s_driver_install(0, &i2s_config, 0, NULL);

    i2s_pin_config_t pin_config = {
        .bck_io_num = CONFIG_I2S_BCK_PIN,
        .ws_io_num = CONFIG_I2S_LRCK_PIN,
        .data_out_num = CONFIG_I2S_DATA_PIN,
        .data_in_num = -1                                                       //Not used
    };

    i2s_set_pin(0, &pin_config);


    ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_BLE));

    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
    if ((err = esp_bt_controller_init(&bt_cfg)) != ESP_OK) {
        ESP_LOGE(BT_AV_TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(err));
        return;
    }

    if ((err = esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT)) != ESP_OK) {
        ESP_LOGE(BT_AV_TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(err));
        return;
    }

    if ((err = esp_bluedroid_init()) != ESP_OK) {
        ESP_LOGE(BT_AV_TAG, "%s initialize bluedroid failed: %s\n", __func__, esp_err_to_name(err));
        return;
    }

    if ((err = esp_bluedroid_enable()) != ESP_OK) {
        ESP_LOGE(BT_AV_TAG, "%s enable bluedroid failed: %s\n", __func__, esp_err_to_name(err));
        return;
    }
    // Enable SPP
    if ((err = esp_spp_register_callback(esp_spp_cb)) != ESP_OK) {
        ESP_LOGE("SPP_TAG", "%s spp register failed: %s\n", __func__, esp_err_to_name(err));
        return;
    }
    if ((err = esp_spp_init(esp_spp_mode)) != ESP_OK) {
        ESP_LOGE("SPP_TAG", "%s spp init failed: %s\n", __func__, esp_err_to_name(err));
        return;
    }

    /* create application task */
    bt_app_task_start_up();

    /* Bluetooth device name, connection mode and profile set up */
    bt_app_work_dispatch(bt_av_hdl_stack_evt, BT_APP_EVT_STACK_UP, NULL, 0, NULL);

    #if (CONFIG_BT_SSP_ENABLED == true)
      /* Set default parameters for Secure Simple Pairing */
      esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
      esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_IO;
      esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t));
    #endif

     /*
     * Use fixed pin code
     * Set default parameters for Legacy Pairing
     */
    esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED;
    esp_bt_pin_code_t pin_code;
    pin_code[0] = '1';
    pin_code[1] = '2';
    pin_code[2] = '3';
    pin_code[3] = '4';
    esp_bt_gap_set_pin(pin_type, 4, pin_code);
    adc1_config_width(ADC_WIDTH_BIT_12);
    adc1_config_channel_atten(ADC1_CHANNEL_4,ADC_ATTEN_DB_0);

    if (0)
    { for (;;)
      { int sum = 0;
        int val = 0;
        for(int i=0;i<32;i++)
        { val = adc1_get_raw(ADC1_CHANNEL_0);
          sum = sum + val;
        }
        val = sum/32;
        printf("%d : %2.2f \n",val,(float) val/4096*3.3*(99300+11800)/11800*1.08 );
        vTaskDelay(10000 / portTICK_RATE_MS);
      }
    } 

    if (1) 
    { uint8_t amp_state[8];
      for (;;)   // read back 96 to 102 (7 bytes)
      { 
        ma_read(MA_dcu_mon0__PM_mon__a, amp_state, 7); 
        if (spp_handle != 0)
        {   //printf("BT tx\n");
            esp_spp_write(spp_handle, 7, amp_state);
        }
        vTaskDelay(100/portTICK_RATE_MS);    
      }
    }
}

void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
{
    switch (event) {
    case ESP_BT_GAP_AUTH_CMPL_EVT: {
        if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) {
            ESP_LOGI(BT_AV_TAG, "authentication success: %s", param->auth_cmpl.device_name);
            esp_log_buffer_hex(BT_AV_TAG, param->auth_cmpl.bda, ESP_BD_ADDR_LEN);
        } else {
            ESP_LOGE(BT_AV_TAG, "authentication failed, status:%d", param->auth_cmpl.stat);
        }
        break;
    }

#if (CONFIG_BT_SSP_ENABLED == true)
    case ESP_BT_GAP_CFM_REQ_EVT:
        ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_CFM_REQ_EVT Please compare the numeric value: %d", param->cfm_req.num_val);
        esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true);
        break;
    case ESP_BT_GAP_KEY_NOTIF_EVT:
        ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_KEY_NOTIF_EVT passkey:%d", param->key_notif.passkey);
        break;
    case ESP_BT_GAP_KEY_REQ_EVT:
        ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_KEY_REQ_EVT Please enter passkey!");
        break;
#endif

    default: {
        ESP_LOGI(BT_AV_TAG, "event: %d", event);
        break;
    }
    }
    return;
}
#define SPP_TAG "SPP_TAG"
static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
{   esp_err_t err;
    switch (event) {
    case ESP_SPP_INIT_EVT:
        ESP_LOGI(SPP_TAG, "ESP_SPP_INIT_EVT");
        esp_bt_dev_set_device_name("EXCAMPLE_DEVICE_NAME");
        esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
        esp_spp_start_srv(ESP_SPP_SEC_AUTHENTICATE,ESP_SPP_ROLE_SLAVE, 0, "SPP_SERVER");
        break;
    case ESP_SPP_DISCOVERY_COMP_EVT:
        ESP_LOGI(SPP_TAG, "ESP_SPP_DISCOVERY_COMP_EVT");
        break;
    case ESP_SPP_OPEN_EVT:
        ESP_LOGI(SPP_TAG, "ESP_SPP_OPEN_EVT");
        break;
    case ESP_SPP_CLOSE_EVT:
        ESP_LOGI(SPP_TAG, "ESP_SPP_CLOSE_EVT");
        break;
    case ESP_SPP_START_EVT:
        ESP_LOGI(SPP_TAG, "ESP_SPP_START_EVT");
        break;
    case ESP_SPP_CL_INIT_EVT:
        ESP_LOGI(SPP_TAG, "ESP_SPP_CL_INIT_EVT");
        break;
    case ESP_SPP_DATA_IND_EVT:
        ESP_LOGI(SPP_TAG, "ESP_SPP_DATA_IND_EVT len=%d handle=%d",
                 param->data_ind.len, param->data_ind.handle);
        if (param->data_ind.data[0] == 1) {
            if (param->data_ind.data[1] == 1) {
                ma_write_byte(param->data_ind.data[2],param->data_ind.data[3]);  
            }
        }
        if (param->data_ind.data[0] == 5) {
            if (param->data_ind.data[1] == 1) {
                freq = param->data_ind.data[3]*256+param->data_ind.data[4]; 
                printf("%f\n",freq);
                float f = freq/44100/2; 
                for ( int8_t n=0; n<=3; n++)
                { bq[n].freq = f ;
                  
                  switch (bq[n].filtertype) {
                    case 1: for (uint8_t i = 0;i <=4 ;i++ )
                            {  printf("%.6f ",bq[n].coeffs[i]);  }  
                               printf("\n");     
                              
                            err = dsps_biquad_gen_lpf_f32( bq[n].coeffs, bq[n].freq, bq[n].q );
                            if (err != ESP_OK) {
                              ESP_LOGE(SPP_TAG, "%s dsps errir: %s\n", __func__, esp_err_to_name(err));
                            }
                             for (uint8_t i = 0;i <=4 ;i++ )
                            {  printf("%.6f ",bq[n].coeffs[i]);  }  
                            printf("%f \n",bq[n].freq);     
                             
                            
                            break;
                    case 2: err = dsps_biquad_gen_hpf_f32( bq[n].coeffs, bq[n].freq, bq[n].q );
                            if (err != ESP_OK) {
                              ESP_LOGE(SPP_TAG, "%s dsps errir: %s\n", __func__, esp_err_to_name(err));
                            }
                            break;
                    default : break;
                  }  
                }  
            } 
            if (param->data_ind.data[1] == 2) {  // Mute CH0 or CH1  
                muteCH[param->data_ind.data[2]] = param->data_ind.data[3];  
            }     
            if (param->data_ind.data[1] == 3) {  // Set DSPmode   
                DSPmode = param->data_ind.data[2];  
                printf("DSP mode : %d \n",DSPmode);    
            }     
            if (param->data_ind.data[1] == 4) {  // Sync UI elements   
              uint8_t UI_sync_vector[15];
              uint8_t UI_sync_vol[4];   
              ma_read( MA_vol_db_master__a, UI_sync_vol, 4 );
              UI_sync_vector[0] = 5;
              UI_sync_vector[1] = 4;
              UI_sync_vector[2] = 0;
              UI_sync_vector[3] = 9;
              UI_sync_vector[4] = UI_sync_vol[0];
              UI_sync_vector[5] = UI_sync_vol[2];
              UI_sync_vector[6] = UI_sync_vol[3];
              UI_sync_vector[7] = muteCH[0];
              UI_sync_vector[8] = muteCH[1];
              UI_sync_vector[9] = 0;
              UI_sync_vector[10] = DSPmode;
              UI_sync_vector[11] = (uint16_t)freq / 256; 
              UI_sync_vector[12] = (uint16_t)freq % 256;  
               
              if (spp_handle != 0)
              {   //printf("BT tx\n");
                   esp_spp_write(spp_handle, 13, UI_sync_vector);
              }
            }
        }
        esp_log_buffer_hex("",param->data_ind.data,param->data_ind.len);
        break;
    case ESP_SPP_CONG_EVT:
        ESP_LOGI(SPP_TAG, "ESP_SPP_CONG_EVT");
        break;
    case ESP_SPP_WRITE_EVT:
        //ESP_LOGI(SPP_TAG, "ESP_SPP_WRITE_EVT");
        break;
    case ESP_SPP_SRV_OPEN_EVT:
         spp_handle = param->open.handle;
        printf("%d spp handle \n",spp_handle); 
       ESP_LOGI(SPP_TAG, "ESP_SPP_SRV_OPEN_EVT");
        break;
    default:
        break;
    }
}
static void bt_av_hdl_stack_evt(uint16_t event, void *p_param)
{
    ESP_LOGD(BT_AV_TAG, "%s evt %d", __func__, event);
    switch (event) {
    case BT_APP_EVT_STACK_UP: {
        /* set up device name */
        char *dev_name = "MerusAmp";
        esp_bt_dev_set_device_name(dev_name);

        esp_bt_gap_register_callback(bt_app_gap_cb);

        /* initialize AVRCP controller */
        esp_avrc_ct_init();
        esp_avrc_ct_register_callback(bt_app_rc_ct_cb);
        /* initialize AVRCP target */
        assert (esp_avrc_tg_init() == ESP_OK);
        esp_avrc_tg_register_callback(bt_app_rc_tg_cb);

        esp_avrc_rn_evt_cap_mask_t evt_set = {0};
        esp_avrc_rn_evt_bit_mask_operation(ESP_AVRC_BIT_MASK_OP_SET, &evt_set, ESP_AVRC_RN_VOLUME_CHANGE);
        assert(esp_avrc_tg_set_rn_evt_cap(&evt_set) == ESP_OK);

        /* initialize A2DP sink */ 
        // was bt_i2s_task_handler 
        esp_a2d_register_callback(&bt_app_a2d_cb);
        esp_a2d_sink_register_data_callback(bt_app_a2d_data_cb);
        esp_a2d_sink_init();

        /* set discoverable and connectable mode, wait to be connected */
        esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
        break;
    }
    default:
        ESP_LOGE(BT_AV_TAG, "%s unhandled evt %d", __func__, event);
        break;
    }
}