bluetooth recoonect

This commit is contained in:
Martijn Scheepers
2022-05-06 15:41:29 +02:00
parent 239c06da15
commit baf4dd4c9a
10 changed files with 115 additions and 50 deletions

View File

@@ -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"
}
}

View File

@@ -3,6 +3,8 @@
#include <stdlib.h>
#include <string.h>
#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();
}

View File

@@ -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);

View File

@@ -1,4 +1,4 @@
//#include <string.h>
#include <string.h>
//#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);

View File

@@ -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);

View File

@@ -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

View File

@@ -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)

View File

@@ -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);

View File

@@ -12,16 +12,16 @@
// 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 <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_system.h"
//#include "esp_log.h"
#include "H2201_i2c.h"
#include "H2201_i2s.h"

View File

@@ -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