From baf4dd4c9abf03555f9141e5ab29314c097be745 Mon Sep 17 00:00:00 2001 From: Martijn Scheepers Date: Fri, 6 May 2022 15:41:29 +0200 Subject: [PATCH] bluetooth recoonect --- ESP32/.vscode/settings.json | 9 +++- ESP32/main/H2201_a2dp.c | 99 +++++++++++++++++++++++++++++------- ESP32/main/H2201_a2dp.h | 2 +- ESP32/main/H2201_bluetooth.c | 13 ++++- ESP32/main/H2201_buttons.c | 2 +- ESP32/main/H2201_buttons.h | 4 -- ESP32/main/H2201_i2s.c | 12 +---- ESP32/main/H2201_i2s.h | 2 +- ESP32/main/main.c | 16 +++--- ESP32/sdkconfig.old | 6 +-- 10 files changed, 115 insertions(+), 50 deletions(-) diff --git a/ESP32/.vscode/settings.json b/ESP32/.vscode/settings.json index 097782b..bedd46c 100644 --- a/ESP32/.vscode/settings.json +++ b/ESP32/.vscode/settings.json @@ -20,6 +20,13 @@ "h2201_i2c.h": "c", "adc.h": "c", "*.ipp": "c", - "*.inc": "c" + "*.inc": "c", + "array": "c", + "bitset": "c", + "string_view": "c", + "initializer_list": "c", + "regex": "c", + "utility": "c", + "bt_types.h": "c" } } diff --git a/ESP32/main/H2201_a2dp.c b/ESP32/main/H2201_a2dp.c index 9442f15..87d9fd1 100644 --- a/ESP32/main/H2201_a2dp.c +++ b/ESP32/main/H2201_a2dp.c @@ -3,6 +3,8 @@ #include #include #include "esp_log.h" +#include "nvs.h" +#include "nvs_flash.h" #include "esp_gap_bt_api.h" #include "esp_a2dp_api.h" @@ -11,15 +13,70 @@ #include "H2201_a2dp.h" #include "H2201_bluetooth.h" -// static uint32_t s_pkt_cnt = 0; -// static esp_a2d_audio_state_t s_audio_state = ESP_A2D_AUDIO_STATE_STOPPED; static const char *h2201_a2dp_conn_state_str[] = {"Disconnected", "Connecting", "Connected", "Disconnecting"}; static const char *h2201_a2dp_audio_state_str[] = {"Suspended", "Stopped", "Started"}; +size_t bda_size = sizeof(esp_bd_addr_t); +uint8_t last_connection[sizeof(esp_bd_addr_t)]; + +void h2201_a2dp_get_connection() +{ + nvs_handle my_handle; + if (nvs_open("connected_bda", NVS_READONLY, &my_handle) != ESP_OK) + { + ESP_LOGE(H2201_A2DP_TAG, "NVS OPEN ERROR"); + } + if (nvs_get_blob(my_handle, "last_bda", last_connection, &bda_size) != ESP_OK) + { + ESP_LOGE(H2201_A2DP_TAG, "NVS read ERROR"); + } + nvs_close(my_handle); + + ESP_LOGI(H2201_A2DP_TAG, "get last connection: [%02x:%02x:%02x:%02x:%02x:%02x]", last_connection[0], last_connection[1], last_connection[2], last_connection[3], last_connection[4], last_connection[5]); +} + +void h2201_a2dp_store_connection(uint8_t bda[]) +{ + ESP_LOGI(H2201_A2DP_TAG, "set connection: [%02x:%02x:%02x:%02x:%02x:%02x]", bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]); + + // same value, nothing to store + if (memcmp(bda, last_connection, bda_size) == 0) + { + ESP_LOGD(H2201_A2DP_TAG, "bda not changed!"); + return; + } + + nvs_handle my_handle; + if (nvs_open("connected_bda", NVS_READWRITE, &my_handle) != ESP_OK) + { + ESP_LOGE(H2201_A2DP_TAG, "NVS OPEN ERROR"); + } + if (nvs_set_blob(my_handle, "last_bda", bda, bda_size) != ESP_OK) + { + ESP_LOGE(H2201_A2DP_TAG, "NVS WRITE ERROR"); + } + else + { + if (nvs_commit(my_handle) != ESP_OK) + { + ESP_LOGE(H2201_A2DP_TAG, "NVS COMMIT ERROR"); + } + } + nvs_close(my_handle); + memcpy(last_connection, bda, bda_size); +} + +void h2201_a2dp_remove_connection() +{ + ESP_LOGI(H2201_A2DP_TAG, "remove connection"); + uint8_t bda[] = {0, 0, 0, 0, 0, 0, 0, 0}; + h2201_a2dp_store_connection(bda); +} + /* callback for A2DP sink */ void h2201_a2dp_callback(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *a2d) { - ESP_LOGD(H2201_A2DP_TAG, "%s evt %d", __func__, event); + ESP_LOGI(H2201_A2DP_TAG, "%s event %d", __func__, event); switch (event) { @@ -31,24 +88,32 @@ void h2201_a2dp_callback(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *a2d) { esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE); h2201_i2s_task_shut_down(); + + if (a2d->conn_stat.disc_rsn == ESP_A2D_DISC_RSN_NORMAL) + { + h2201_a2dp_remove_connection(); + } + else + { + esp_a2d_sink_connect(last_connection); + } } else if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_CONNECTED) { esp_bt_gap_set_scan_mode(ESP_BT_NON_CONNECTABLE, ESP_BT_NON_DISCOVERABLE); h2201_i2s_task_start_up(); + + h2201_a2dp_store_connection(a2d->conn_stat.remote_bda); } break; } + case ESP_A2D_AUDIO_STATE_EVT: // audio stream transmission state changed event { ESP_LOGI(H2201_A2DP_TAG, "A2DP audio state: %s", h2201_a2dp_audio_state_str[a2d->audio_stat.state]); - // s_audio_state = a2d->audio_stat.state; - // if (ESP_A2D_AUDIO_STATE_STARTED == a2d->audio_stat.state) - //{ - // s_pkt_cnt = 0; - // } break; } + case ESP_A2D_AUDIO_CFG_EVT: // audio codec is configured { ESP_LOGI(H2201_A2DP_TAG, "A2DP audio stream configuration, codec type %d", a2d->audio_cfg.mcc.type); @@ -80,35 +145,31 @@ void h2201_a2dp_callback(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *a2d) } break; } + case ESP_A2D_PROF_STATE_EVT: // indicate a2dp init&deinit complete { if (ESP_A2D_INIT_SUCCESS == a2d->a2d_prof_stat.init_state) { - ESP_LOGI(H2201_A2DP_TAG, "A2DP PROF STATE: Init Compl\n"); + ESP_LOGI(H2201_A2DP_TAG, "A2DP PROF STATE: Init Complete"); + h2201_a2dp_get_connection(); + //esp_a2d_sink_connect(last_connection); } else { - ESP_LOGI(H2201_A2DP_TAG, "A2DP PROF STATE: Deinit Compl\n"); + ESP_LOGI(H2201_A2DP_TAG, "A2DP PROF STATE: Deinit Complete"); } break; } + default: ESP_LOGE(H2201_A2DP_TAG, "%s unhandled evt %d", __func__, event); break; } } -void h2201_a2dp_data_callback(const uint8_t *data, uint32_t len) -{ - h2201_i2s_write_ringbuf(data, len); - // if (++s_pkt_cnt % 100 == 0) { - // ESP_LOGI(H2201_A2DP_TAG, "Audio packet count %u", s_pkt_cnt); - // } -} - void h2201_a2dp_create_sink(void) { esp_a2d_register_callback(&h2201_a2dp_callback); - esp_a2d_sink_register_data_callback(h2201_a2dp_data_callback); + esp_a2d_sink_register_data_callback(h2201_i2s_write_ringbuf); esp_a2d_sink_init(); } \ No newline at end of file diff --git a/ESP32/main/H2201_a2dp.h b/ESP32/main/H2201_a2dp.h index 8700dcc..b1f1324 100644 --- a/ESP32/main/H2201_a2dp.h +++ b/ESP32/main/H2201_a2dp.h @@ -1,7 +1,7 @@ #ifndef __H2201_A2DP_H__ #define __H2201_A2DP_H__ -#define H2201_A2DP_TAG "A2DP" +#define H2201_A2DP_TAG "H2201_A2DP" void h2201_a2dp_create_sink(void); diff --git a/ESP32/main/H2201_bluetooth.c b/ESP32/main/H2201_bluetooth.c index a85cf4f..03582f8 100644 --- a/ESP32/main/H2201_bluetooth.c +++ b/ESP32/main/H2201_bluetooth.c @@ -1,4 +1,4 @@ -//#include +#include //#include "esp_system.h" #include "esp_log.h" @@ -10,8 +10,12 @@ #include "H2201_bluetooth.h" #include "H2201_a2dp.h" + + void h2201_bluetooth_gap_register_callback(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param) { + // ESP_LOGI(H2201_BLUETOOTH_TAG, "gap register event: %d", event); + switch (event) { case ESP_BT_GAP_AUTH_CMPL_EVT: // Authentication complete event @@ -45,6 +49,10 @@ void h2201_bluetooth_gap_register_callback(esp_bt_gap_cb_event_t event, esp_bt_g ESP_LOGI(H2201_BLUETOOTH_TAG, "ESP_BT_GAP_MODE_CHG_EVT mode:%d", param->mode_chg.mode); break; + case ESP_BT_GAP_CONFIG_EIR_DATA_EVT: // Config EIR data event + ESP_LOGI(H2201_BLUETOOTH_TAG, "ESP_BT_GAP_CONFIG_EIR_DATA_EVT"); + break; + default: { ESP_LOGI(H2201_BLUETOOTH_TAG, "event: %d", event); @@ -56,6 +64,7 @@ void h2201_bluetooth_gap_register_callback(esp_bt_gap_cb_event_t event, esp_bt_g void h2201_bluetooth_init() { + // release not used BLE memory ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_BLE)); esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); @@ -89,7 +98,7 @@ void h2201_bluetooth_init() esp_bt_gap_register_callback(h2201_bluetooth_gap_register_callback); h2201_a2dp_create_sink(); - + /* set discoverable and connectable mode, wait to be connected */ esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE); diff --git a/ESP32/main/H2201_buttons.c b/ESP32/main/H2201_buttons.c index f032146..813af28 100644 --- a/ESP32/main/H2201_buttons.c +++ b/ESP32/main/H2201_buttons.c @@ -117,7 +117,7 @@ void H2201_buttons_init(void) // but will remove the glitches on GPIO36 and GPIO39. adc_power_acquire(); - H2201_button_event_queue = xQueueCreate(10, sizeof(uint32_t)); + H2201_button_event_queue = xQueueCreate(20, sizeof(uint32_t)); xTaskCreate(H2201_button_task, "H2201_button_task", 4096, NULL, 10, NULL); gpio_install_isr_service(ESP_INTR_FLAG_EDGE); diff --git a/ESP32/main/H2201_buttons.h b/ESP32/main/H2201_buttons.h index 1d88872..0673aab 100644 --- a/ESP32/main/H2201_buttons.h +++ b/ESP32/main/H2201_buttons.h @@ -8,10 +8,6 @@ #define ROTARY_A_GPIO 27 #define ROTARY_B_GPIO 32 -//#define ROTARY_STEP_SIZE 100000 -//#define ROTARY_MAX 8388608 -//#define ROTARY_MAX 1000 -//#define ROTARY_MIN 0 #define ROTARY_GLITCH_US 10 #define SHIFT_CLOCK 19 diff --git a/ESP32/main/H2201_i2s.c b/ESP32/main/H2201_i2s.c index 0b6f644..70e21ac 100644 --- a/ESP32/main/H2201_i2s.c +++ b/ESP32/main/H2201_i2s.c @@ -58,17 +58,9 @@ void h2201_i2s_task_shut_down(void) } } -size_t h2201_i2s_write_ringbuf(const uint8_t *data, size_t size) +void h2201_i2s_write_ringbuf(const uint8_t *data, uint32_t size) { - BaseType_t done = xRingbufferSend(h2201_i2s_ringbuffer_handle, (void *)data, size, (portTickType)portMAX_DELAY); - if (done) - { - return size; - } - else - { - return 0; - } + xRingbufferSend(h2201_i2s_ringbuffer_handle, (void *)data, size, (portTickType)portMAX_DELAY); } void h2201_i2s_set_samplerate(int sample_rate) diff --git a/ESP32/main/H2201_i2s.h b/ESP32/main/H2201_i2s.h index d38112e..1b115b9 100644 --- a/ESP32/main/H2201_i2s.h +++ b/ESP32/main/H2201_i2s.h @@ -4,7 +4,7 @@ void h2201_i2s_task_start_up(void); void h2201_i2s_task_shut_down(void); -size_t h2201_i2s_write_ringbuf(const uint8_t *data, size_t size); +void h2201_i2s_write_ringbuf(const uint8_t *data, uint32_t size); void h2201_i2s_set_samplerate(int sample_rate); diff --git a/ESP32/main/main.c b/ESP32/main/main.c index f9f159f..91c59ae 100644 --- a/ESP32/main/main.c +++ b/ESP32/main/main.c @@ -12,16 +12,16 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include -#include -#include -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" +//#include +//#include +//#include +//#include +//#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_system.h" +//#include "esp_log.h" #include "H2201_i2c.h" #include "H2201_i2s.h" diff --git a/ESP32/sdkconfig.old b/ESP32/sdkconfig.old index 29a354a..fd62740 100644 --- a/ESP32/sdkconfig.old +++ b/ESP32/sdkconfig.old @@ -228,7 +228,7 @@ CONFIG_BT_BLUEDROID_ENABLED=y # # Bluedroid Options # -CONFIG_BT_BTC_TASK_STACK_SIZE=3072 +CONFIG_BT_BTC_TASK_STACK_SIZE=4096 CONFIG_BT_BLUEDROID_PINNED_TO_CORE_0=y # CONFIG_BT_BLUEDROID_PINNED_TO_CORE_1 is not set CONFIG_BT_BLUEDROID_PINNED_TO_CORE=0 @@ -417,7 +417,7 @@ CONFIG_BT_LOG_BLUFI_TRACE_LEVEL=2 # end of BT DEBUG LOG LEVEL CONFIG_BT_ACL_CONNECTIONS=4 -CONFIG_BT_MULTI_CONNECTION_ENBALE=y +# CONFIG_BT_MULTI_CONNECTION_ENBALE is not set # CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST is not set # CONFIG_BT_BLE_DYNAMIC_ENV_MEMORY is not set # CONFIG_BT_BLE_HOST_QUEUE_CONG_CHECK is not set @@ -1532,7 +1532,7 @@ CONFIG_BTDM_CONTROLLER_HCI_MODE_VHCI=y CONFIG_BTDM_CONTROLLER_MODEM_SLEEP=y CONFIG_BLUEDROID_ENABLED=y # CONFIG_NIMBLE_ENABLED is not set -CONFIG_BTC_TASK_STACK_SIZE=3072 +CONFIG_BTC_TASK_STACK_SIZE=4096 CONFIG_BLUEDROID_PINNED_TO_CORE_0=y # CONFIG_BLUEDROID_PINNED_TO_CORE_1 is not set CONFIG_BLUEDROID_PINNED_TO_CORE=0