2016-11-04 03:08:30 -04:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
|
2016-12-02 01:39:03 -05:00
|
|
|
#include "esp_system.h"
|
|
|
|
#include "EspAudio.h"
|
|
|
|
#include "EspAudioCom.h"
|
|
|
|
|
2016-11-04 03:08:30 -04:00
|
|
|
#include "bt_app_common.h"
|
2016-12-26 04:26:54 -05:00
|
|
|
#include "esp_bt_stack_manager.h"
|
2016-11-26 00:09:55 -05:00
|
|
|
#include "esp_gap_bt_api.h"
|
2016-11-04 03:08:30 -04:00
|
|
|
#include "bta_api.h"
|
2016-12-01 01:57:44 -05:00
|
|
|
#include "esp_a2dp_api.h"
|
2016-11-15 01:32:22 -05:00
|
|
|
|
2016-11-04 03:08:30 -04:00
|
|
|
typedef enum {
|
2016-12-02 01:39:03 -05:00
|
|
|
BT_APP_EVT_STACK_ON = 0xa0,
|
|
|
|
BT_APP_EVT_MAX
|
2016-12-02 08:50:32 -05:00
|
|
|
} bt_app_evt_t;
|
2016-11-04 03:08:30 -04:00
|
|
|
|
|
|
|
typedef union {
|
2016-12-02 01:39:03 -05:00
|
|
|
esp_a2d_cb_param_t a2d;
|
2016-12-02 08:50:32 -05:00
|
|
|
} bt_app_evt_arg;
|
2016-11-04 03:08:30 -04:00
|
|
|
|
2016-12-02 08:50:32 -05:00
|
|
|
static void bt_app_handle_evt(UINT16 event, void *p_param);
|
2016-11-04 03:08:30 -04:00
|
|
|
|
2016-12-02 01:39:03 -05:00
|
|
|
static void bt_app_a2d_cb(uint32_t event, void *param)
|
2016-11-04 03:08:30 -04:00
|
|
|
{
|
2016-12-01 08:13:27 -05:00
|
|
|
switch (event) {
|
|
|
|
case ESP_A2D_CONNECTION_STATE_EVT:
|
|
|
|
case ESP_A2D_AUDIO_STATE_EVT:
|
|
|
|
case ESP_A2D_AUDIO_CFG_EVT:
|
2016-12-02 01:39:03 -05:00
|
|
|
{
|
2016-12-02 08:50:32 -05:00
|
|
|
bt_app_transfer_context(bt_app_handle_evt, event, param, sizeof(bt_app_evt_arg), NULL);
|
2016-12-01 08:13:27 -05:00
|
|
|
break;
|
2016-12-02 01:39:03 -05:00
|
|
|
}
|
2016-12-01 08:13:27 -05:00
|
|
|
default:
|
2016-12-02 08:50:32 -05:00
|
|
|
BT_APP_TRACE_ERROR("===a2dp invalid cb event: %d\n", event);
|
2016-12-02 01:39:03 -05:00
|
|
|
break;
|
2016-12-01 08:13:27 -05:00
|
|
|
}
|
2016-11-15 01:32:22 -05:00
|
|
|
}
|
|
|
|
|
2016-12-02 01:39:03 -05:00
|
|
|
static void bt_app_a2d_data_cb(uint8_t *data, uint32_t len)
|
2016-11-04 03:08:30 -04:00
|
|
|
{
|
2016-12-26 04:26:54 -05:00
|
|
|
EspAudioPlayerStreamWrite(data, len, 10);
|
2016-11-15 01:32:22 -05:00
|
|
|
}
|
2016-12-02 01:39:03 -05:00
|
|
|
|
2016-12-02 08:50:32 -05:00
|
|
|
static void bt_app_handle_evt(UINT16 event, void *p_param)
|
2016-11-04 03:08:30 -04:00
|
|
|
{
|
2016-12-02 08:50:32 -05:00
|
|
|
BT_APP_TRACE_EVENT("bt_app_handle_evt 0x%x\n", event);
|
2016-12-02 01:39:03 -05:00
|
|
|
esp_a2d_cb_param_t *a2d = NULL;
|
2016-11-04 03:08:30 -04:00
|
|
|
switch (event) {
|
|
|
|
case BT_APP_EVT_STACK_ON: {
|
2016-12-02 08:50:32 -05:00
|
|
|
char *dev_name = "ESP_SPEAKER";
|
2016-12-26 04:26:54 -05:00
|
|
|
// BTM_SetTraceLevel(BT_TRACE_LEVEL_WARNING);
|
|
|
|
esp_bt_gap_set_device_name(dev_name);
|
2016-11-26 00:09:55 -05:00
|
|
|
|
2016-12-02 01:39:03 -05:00
|
|
|
esp_a2d_register_callback(bt_app_a2d_cb);
|
2016-12-02 08:50:32 -05:00
|
|
|
esp_a2d_register_data_callback(bt_app_a2d_data_cb);
|
2016-12-02 01:39:03 -05:00
|
|
|
|
|
|
|
esp_a2d_sink_init();
|
|
|
|
esp_bt_gap_set_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ESP_A2D_CONNECTION_STATE_EVT: {
|
|
|
|
a2d = (esp_a2d_cb_param_t *)(p_param);
|
2016-12-02 08:50:32 -05:00
|
|
|
BT_APP_TRACE_ERROR("===a2dp conn_state_cb %d ===\n", a2d->conn_stat.state);
|
2016-12-02 01:39:03 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ESP_A2D_AUDIO_STATE_EVT: {
|
|
|
|
a2d = (esp_a2d_cb_param_t *)(p_param);
|
2016-12-02 08:50:32 -05:00
|
|
|
BT_APP_TRACE_ERROR("===a2dp audio_state_cb %d ===\n", a2d->audio_stat.state);
|
2016-12-02 01:39:03 -05:00
|
|
|
break;
|
2016-11-04 03:08:30 -04:00
|
|
|
}
|
2016-12-02 01:39:03 -05:00
|
|
|
case ESP_A2D_AUDIO_CFG_EVT: {
|
|
|
|
a2d = (esp_a2d_cb_param_t *)(p_param);
|
2016-12-02 08:50:32 -05:00
|
|
|
BT_APP_TRACE_ERROR("===a2dp audio_cfg_cb type %d ===\n", a2d->audio_cfg.mcc.type);
|
2016-12-02 01:39:03 -05:00
|
|
|
if (a2d->audio_cfg.mcc.type == ESP_A2D_MCT_SBC) {
|
|
|
|
// temporarily hardcoded the PCM configuaration
|
2016-12-02 08:50:32 -05:00
|
|
|
BT_APP_TRACE_ERROR("configure audio player\n");
|
2016-12-02 01:39:03 -05:00
|
|
|
EspAudioPlayerStreamCfg(StreamSampleRate_44k, 2, StreamBitLen_16BIT);
|
|
|
|
EspAudio_SetupStream("stream.pcm", InputSrcType_Stream);
|
|
|
|
EspAudio_SetVolume(99);
|
|
|
|
}
|
|
|
|
break;
|
2016-11-04 03:08:30 -04:00
|
|
|
}
|
|
|
|
default:
|
2016-12-02 08:50:32 -05:00
|
|
|
BT_APP_TRACE_ERROR("===application invalid event: %d\n", event);
|
2016-11-04 03:08:30 -04:00
|
|
|
break;
|
|
|
|
}
|
2016-12-02 08:50:32 -05:00
|
|
|
|
2016-11-04 03:08:30 -04:00
|
|
|
}
|
|
|
|
|
2016-12-01 08:46:37 -05:00
|
|
|
void app_main_entry(void)
|
2016-11-04 03:08:30 -04:00
|
|
|
{
|
2016-12-01 08:46:37 -05:00
|
|
|
bt_status_t init, enable;
|
2016-12-26 04:26:54 -05:00
|
|
|
init = esp_bt_init_stack();
|
|
|
|
if (init != ESP_OK) {
|
2016-12-01 08:46:37 -05:00
|
|
|
return;
|
2016-11-04 03:08:30 -04:00
|
|
|
}
|
|
|
|
|
2016-12-26 04:26:54 -05:00
|
|
|
enable = esp_bt_enable_stack();
|
|
|
|
if (enable != ESP_OK) {
|
2016-12-01 08:46:37 -05:00
|
|
|
return;
|
2016-11-04 03:08:30 -04:00
|
|
|
}
|
|
|
|
|
2016-12-02 08:50:32 -05:00
|
|
|
bt_app_transfer_context(bt_app_handle_evt, BT_APP_EVT_STACK_ON, NULL, 0, NULL);
|
|
|
|
}
|