建立双管道时候,按键外设的问题

ChiShaoJun
Posts: 26
Joined: Mon Sep 17, 2018 3:24 am

建立双管道时候,按键外设的问题

Postby ChiShaoJun » Wed Mar 13, 2019 7:56 am

我这边做了一个简单的双管道demo,想通过按键的事件进行切换。管道1是连接bt播放音乐,管道2是连接AUX_IN播放音乐,硬件为ESP32_LyraT v4.3。但是使用管道2时却触发不到按键的事件,是想请问下我代码的哪个步骤出了问题。
  1. /* Play music from Bluetooth device
  2.  
  3.    This example code is in the Public Domain (or CC0 licensed, at your option.)
  4.  
  5.    Unless required by applicable law or agreed to in writing, this
  6.    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  7.    CONDITIONS OF ANY KIND, either express or implied.
  8. */
  9. #include <string.h>
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "esp_log.h"
  13. #include "nvs_flash.h"
  14. #include "audio_element.h"
  15. #include "audio_pipeline.h"
  16. #include "audio_event_iface.h"
  17. #include "audio_common.h"
  18. #include "i2s_stream.h"
  19. #include "esp_peripherals.h"
  20. #include "periph_touch.h"
  21. #include "audio_hal.h"
  22. #include "board.h"
  23. #include "bluetooth_service.h"
  24.  
  25. static const char *TAG = "BLUETOOTH_EXAMPLE";
  26.  
  27. #define LYRAT_TOUCH_SET     TOUCH_PAD_NUM9
  28. #define LYRAT_TOUCH_PLAY    TOUCH_PAD_NUM8
  29. #define LYRAT_TOUCH_VOLUP   TOUCH_PAD_NUM7
  30. #define LYRAT_TOUCH_VOLDWN  TOUCH_PAD_NUM4
  31.  
  32. //状态切换
  33. volatile uint8_t ex_audio_status = 1;
  34.  
  35. void app_main(void)
  36. {
  37.     audio_pipeline_handle_t pipeline1, pipeline2;
  38.     audio_element_handle_t bt_stream_reader ,i2s_stream_reader, i2s_stream_writer1,i2s_stream_writer2;
  39.  
  40.     //nvs flash初始化
  41.     esp_err_t err = nvs_flash_init();
  42.     if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
  43.         // NVS partition was truncated and needs to be erased
  44.         // Retry nvs_flash_init
  45.         ESP_ERROR_CHECK(nvs_flash_erase());
  46.         err = nvs_flash_init();
  47.     }
  48.  
  49.     //日志级别打印
  50.     esp_log_level_set("*", ESP_LOG_INFO);
  51.     esp_log_level_set(TAG, ESP_LOG_DEBUG);
  52.  
  53.     //1.创建蓝牙服务
  54.     ESP_LOGI(TAG, "[ 1 ] Create Bluetooth service");
  55.     bluetooth_service_cfg_t bt_cfg = {
  56.         .device_name = "ESP-ADF-SPEAKER",
  57.         .mode = BLUETOOTH_A2DP_SINK,
  58.     };
  59.     bluetooth_service_start(&bt_cfg);
  60.  
  61.     //2.启动编解码器芯片
  62.     ESP_LOGI(TAG, "[ 2 ] Start codec chip");
  63.     audio_hal_codec_config_t audio_hal_codec_cfg =  AUDIO_HAL_ES8388_DEFAULT();
  64.     audio_hal_codec_cfg.i2s_iface.samples = AUDIO_HAL_44K_SAMPLES;
  65.     //音频输入设置把麦克风改成外部音频输入
  66.     audio_hal_codec_cfg.adc_input = AUDIO_HAL_ADC_INPUT_LINE2;
  67.     audio_hal_handle_t hal = audio_hal_init(&audio_hal_codec_cfg, 0);
  68.     audio_hal_ctrl_codec(hal, AUDIO_HAL_CODEC_MODE_DECODE, AUDIO_HAL_CTRL_START);
  69.  
  70.     //3.创建音频管道以进行播放
  71.     ESP_LOGI(TAG, "[ 3 ] Create audio pipeline for playback");
  72.     audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
  73.     pipeline1 = audio_pipeline_init(&pipeline_cfg);
  74.     pipeline2 = audio_pipeline_init(&pipeline_cfg);
  75.  
  76.     //3.1.创建i2s流以将数据写入编解码器芯片
  77.     ESP_LOGI(TAG, "[3.1] Create i2s stream to read audio data from codec chip");
  78.     i2s_stream_cfg_t i2s_r_cfg = I2S_STREAM_CFG_DEFAULT();
  79.     i2s_r_cfg.type = AUDIO_STREAM_READER;
  80.     i2s_stream_reader = i2s_stream_init(&i2s_r_cfg);
  81.  
  82.     //3.2.创建i2s流以将数据写入编解码器芯片
  83.     ESP_LOGI(TAG, "[3.2] Create i2s stream to write data to codec chip");
  84.     i2s_stream_cfg_t i2s_w_cfg1 = I2S_STREAM_CFG_DEFAULT();
  85.     i2s_w_cfg1.type = AUDIO_STREAM_WRITER;
  86.     i2s_w_cfg1.i2s_config.sample_rate = 48000;
  87.     i2s_stream_writer1 = i2s_stream_init(&i2s_w_cfg1);
  88.  
  89.     //3.2.1.创建i2s流以将数据写入编解码器芯片
  90.     ESP_LOGI(TAG, "[3.2] Create i2s stream to write data to codec chip");
  91.     i2s_stream_cfg_t i2s_w_cfg2 = I2S_STREAM_CFG_DEFAULT();
  92.     i2s_w_cfg2.type = AUDIO_STREAM_WRITER;
  93.     i2s_w_cfg2.i2s_config.sample_rate = 48000;
  94.     i2s_stream_writer2 = i2s_stream_init(&i2s_w_cfg2);
  95.  
  96.     //3.3.获取蓝牙流
  97.     ESP_LOGI(TAG, "[3.3] Get Bluetooth stream");
  98.     bt_stream_reader = bluetooth_service_create_stream();
  99.  
  100.     //3.4.初始化外围设备
  101.     ESP_LOGI(TAG, "[ 3.4 ] Register all elements to audio pipeline");
  102.     audio_pipeline_register(pipeline1, bt_stream_reader, "bt");
  103.     audio_pipeline_register(pipeline1, i2s_stream_writer1, "i2sw1");
  104.  
  105.     audio_pipeline_register(pipeline2, i2s_stream_reader, "i2sr");
  106.     audio_pipeline_register(pipeline2, i2s_stream_writer2, "i2sw2");
  107.  
  108.     //3.5.把它链接在一起
  109.     ESP_LOGI(TAG, "[ 3.5 ] Link elements together");
  110.     audio_pipeline_link(pipeline1, (const char *[]) {"bt", "i2sw1"}, 2);
  111.     audio_pipeline_link(pipeline2, (const char *[]) {"i2sr", "i2sw2"}, 2);
  112.  
  113.     //4.初始化外围设备
  114.     ESP_LOGI(TAG, "[ 4 ] Initialize peripherals");
  115.     esp_periph_config_t periph_cfg = { 0 };
  116.     esp_periph_init(&periph_cfg);
  117.  
  118.     //4.1.初始化Touch外围设备
  119.     ESP_LOGI(TAG, "[4.1] Initialize Touch peripheral");
  120.     periph_touch_cfg_t touch_cfg = {
  121.         .touch_mask = TOUCH_SEL_SET | TOUCH_SEL_PLAY | TOUCH_SEL_VOLUP | TOUCH_SEL_VOLDWN,
  122.         .tap_threshold_percent = 70,
  123.     };
  124.     esp_periph_handle_t touch_periph = periph_touch_init(&touch_cfg);
  125.  
  126.     //4.2.创建蓝牙外设
  127.     ESP_LOGI(TAG, "[4.2] Create Bluetooth peripheral");
  128.     esp_periph_handle_t bt_periph = bluetooth_service_create_periph();
  129.  
  130.     //4.2.启动所有外围设备
  131.     ESP_LOGI(TAG, "[4.2] Start all peripherals");
  132.     esp_periph_start(touch_periph);
  133.     esp_periph_start(bt_periph);
  134.  
  135.     //5.设置事件监听器
  136.     ESP_LOGI(TAG, "[ 5 ] Setup event listener");
  137.     audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
  138.     audio_event_iface_handle_t evt1 = audio_event_iface_init(&evt_cfg);
  139.     audio_event_iface_handle_t evt2 = audio_event_iface_init(&evt_cfg);
  140.  
  141.     ESP_LOGI(TAG, "[5.1] Listening event from all elements of pipeline");
  142.     //为此音频管道设置事件监听器,来自此管道的任何事件都可以通过`evt`监听
  143.     audio_pipeline_set_listener(pipeline1, evt1);
  144.     audio_pipeline_set_listener(pipeline2, evt2);
  145.  
  146.     //5.2.外围设备的监听事件
  147.     ESP_LOGI(TAG, "[5.2] Listening event from peripherals");
  148.     audio_event_iface_set_listener(esp_periph_get_event_iface(), evt1);
  149.     audio_event_iface_set_listener(esp_periph_get_event_iface(), evt2);
  150.  
  151.     while(1)
  152.     {
  153.         switch(ex_audio_status)
  154.         {
  155.         case 0:
  156.             ESP_LOGI(TAG, "case 0");
  157.             //6.启动audio_pipeline
  158.             ESP_LOGI(TAG, "[ 6 ] Start audio_pipeline");
  159.             audio_pipeline_run(pipeline1);
  160.  
  161.             //7.监听所有管道事件
  162.             ESP_LOGI(TAG, "[ 7 ] Listen for all pipeline events");
  163.             while (1)
  164.             {
  165.                 audio_event_iface_msg_t msg;
  166.                 esp_err_t ret1 = audio_event_iface_listen(evt1, &msg, portMAX_DELAY);
  167.                 if (ret1 != ESP_OK) {
  168.                     ESP_LOGE(TAG, "[ * ] Event interface error : %d", ret1);
  169.                     continue;
  170.                 }
  171.  
  172.                 if (msg.cmd == AEL_MSG_CMD_ERROR) {
  173.                     ESP_LOGE(TAG, "[ * ] Action command error: src_type:%d, source:%p cmd:%d, data:%p, data_len:%d",
  174.                              msg.source_type, msg.source, msg.cmd, msg.data, msg.data_len);
  175.                 }
  176.  
  177.                 if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) bt_stream_reader
  178.                     && msg.cmd == AEL_MSG_CMD_REPORT_MUSIC_INFO) {
  179.                     audio_element_info_t music_info = {0};
  180.                     audio_element_getinfo(bt_stream_reader, &music_info);
  181.  
  182.                     ESP_LOGI(TAG, "[ * ] Receive music info from Bluetooth, sample_rates=%d, bits=%d, ch=%d",
  183.                              music_info.sample_rates, music_info.bits, music_info.channels);
  184.  
  185.                     audio_element_setinfo(i2s_stream_writer1, &music_info);
  186.                     i2s_stream_set_clk(i2s_stream_writer1, music_info.sample_rates, music_info.bits, music_info.channels);
  187.                     continue;
  188.                 }
  189.  
  190.                 if (msg.source_type == PERIPH_ID_TOUCH
  191.                     && msg.cmd == PERIPH_TOUCH_TAP
  192.                     && msg.source == (void *)touch_periph) {
  193.  
  194.                     if ((int) msg.data == TOUCH_PLAY) {
  195.                         ESP_LOGI(TAG, "[ * ] [Play] touch tap event");
  196.                         periph_bluetooth_play(bt_periph);
  197.                     } else if ((int) msg.data == TOUCH_SET) {
  198.                         ESP_LOGI(TAG, "[ * ] [Set] touch tap event");
  199.                         periph_bluetooth_pause(bt_periph);
  200.                     } else if ((int) msg.data == TOUCH_VOLUP) {
  201.                         if(audio_pipeline_pause(pipeline2) != ESP_OK)
  202.                         {
  203.                             ESP_LOGI(TAG, "[ * ] audio_pipeline_pause err");
  204.                         }
  205.                         if(audio_pipeline_run(pipeline1) != ESP_OK)
  206.                         {
  207.                             ESP_LOGI(TAG, "[ * ] audio_pipeline_run err");
  208.                         }
  209.                     } else if ((int) msg.data == TOUCH_VOLDWN) {
  210.                         ESP_LOGI(TAG, "[ * ] [Vol-] touch tap event");
  211.                         ex_audio_status = 1;
  212.                         if(audio_pipeline_pause(pipeline1) != ESP_OK)
  213.                         {
  214.                             ESP_LOGI(TAG, "[ * ] audio_pipeline_pause err");
  215.                         }
  216.                         break;
  217.                     }
  218.                 }
  219.  
  220.                 /* Stop when the Bluetooth is disconnected or suspended */
  221.                 if (msg.source_type == PERIPH_ID_BLUETOOTH
  222.                     && msg.source == (void *)bt_periph) {
  223.                     if (msg.cmd == PERIPH_BLUETOOTH_DISCONNECTED) {
  224.                         ESP_LOGW(TAG, "[ * ] Bluetooth disconnected");
  225.                         break;
  226.                     }
  227.                 }
  228.                 /* Stop when the last pipeline element (i2s_stream_writer in this case) receives stop event */
  229.                 if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) i2s_stream_writer1
  230.                     && msg.cmd == AEL_MSG_CMD_REPORT_STATUS && (int) msg.data == AEL_STATUS_STATE_STOPPED) {
  231.                     ESP_LOGW(TAG, "[ * ] Stop event received");
  232.                     break;
  233.                 }
  234.             }
  235.         break;
  236.         case 1:
  237.             audio_pipeline_run(pipeline2);
  238.             while (1)
  239.             {
  240.                 ESP_LOGI(TAG, "case 1");
  241.                 audio_event_iface_msg_t msg2;
  242.                 esp_err_t ret2 = audio_event_iface_listen(evt2, &msg2, portMAX_DELAY);
  243.                 if (ret2 != ESP_OK) {
  244.                     ESP_LOGE(TAG, "[ * ] Event interface error : %d", ret2);
  245.                     continue;
  246.                 }
  247.  
  248.                 if (msg2.cmd == AEL_MSG_CMD_ERROR) {
  249.                     ESP_LOGE(TAG, "[ * ] Action command error: src_type:%d, source:%p cmd:%d, data:%p, data_len:%d",
  250.                              msg2.source_type, msg2.source, msg2.cmd, msg2.data, msg2.data_len);
  251.                 }
  252.  
  253.                 if (msg2.source_type == PERIPH_ID_TOUCH
  254.                     && msg2.cmd == PERIPH_TOUCH_TAP
  255.                     && msg2.source == (void *)touch_periph) {
  256.  
  257.                     if ((int) msg2.data == TOUCH_PLAY) {
  258.                         ESP_LOGI(TAG, "[ * ] [Play] touch tap event");
  259.                         periph_bluetooth_play(bt_periph);
  260.                     } else if ((int) msg2.data == TOUCH_SET) {
  261.                         ESP_LOGI(TAG, "[ * ] [Set] touch tap event");
  262.                         periph_bluetooth_pause(bt_periph);
  263.                     } else if ((int) msg2.data == TOUCH_VOLUP) {
  264.                         if(audio_pipeline_pause(pipeline2) != ESP_OK)
  265.                         {
  266.                             ESP_LOGI(TAG, "[ * ] audio_pipeline_pause err");
  267.                         }
  268.                     } else if ((int) msg2.data == TOUCH_VOLDWN) {
  269.                         ESP_LOGI(TAG, "[ * ] [Vol-] touch tap event");
  270.                         ex_audio_status = 0;
  271.                         if(audio_pipeline_pause(pipeline2) != ESP_OK)
  272.                         {
  273.                             ESP_LOGI(TAG, "[ * ] audio_pipeline_pause err");
  274.                         }
  275.                         break;
  276.                     }
  277.                 }
  278.  
  279.                 /* Stop when the last pipeline element (i2s_stream_writer in this case) receives stop event */
  280.                 if (msg2.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg2.source == (void *) i2s_stream_writer2
  281.                     && msg2.cmd == AEL_MSG_CMD_REPORT_STATUS && (int) msg2.data == AEL_STATUS_STATE_STOPPED) {
  282.                     ESP_LOGW(TAG, "[ * ] Stop event received");
  283.                     break;
  284.                 }
  285.             }
  286.             break;
  287.         }
  288.     }
  289. }

Who is online

Users browsing this forum: arunbm123 and 2 guests