Skip to content

Commit 22433ee

Browse files
committed
bt_app: optimize the "Auto Reconnect" feature
1 parent 475119c commit 22433ee

File tree

5 files changed

+28
-23
lines changed

5 files changed

+28
-23
lines changed

main/inc/core/os.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ typedef enum user_event_group_bits {
2525
VFX_FFT_NULL_BIT = BIT7,
2626

2727
KEY_SCAN_RUN_BIT = BIT8,
28+
KEY_SCAN_CLR_BIT = BIT9,
2829

29-
AUDIO_RENDER_CLR_BIT = BIT9,
30+
AUDIO_RENDER_CLR_BIT = BIT10,
3031

31-
AUDIO_INPUT_RUN_BIT = BIT10,
32-
AUDIO_INPUT_FFT_BIT = BIT11,
32+
AUDIO_INPUT_RUN_BIT = BIT11,
33+
AUDIO_INPUT_FFT_BIT = BIT12,
3334

34-
AUDIO_PLAYER_RUN_BIT = BIT12,
35-
AUDIO_PLAYER_IDLE_BIT = BIT13,
35+
AUDIO_PLAYER_RUN_BIT = BIT13,
36+
AUDIO_PLAYER_IDLE_BIT = BIT14,
3637
} user_event_group_bits_t;
3738

3839
extern EventGroupHandle_t user_event_group;

main/src/user/bt_app.c

+4-15
Original file line numberDiff line numberDiff line change
@@ -105,26 +105,15 @@ void bt_app_init(void)
105105
ESP_LOGI(BT_APP_TAG, "started.");
106106

107107
if (memcmp(last_remote_bda, "\x00\x00\x00\x00\x00\x00", 6) != 0) {
108-
// Reconnection delay
109-
vTaskDelay(1000 / portTICK_RATE_MS);
110-
#ifdef CONFIG_ENABLE_AUDIO_PROMPT
111-
xEventGroupWaitBits(
112-
user_event_group,
113-
AUDIO_PLAYER_IDLE_BIT,
114-
pdFALSE,
115-
pdFALSE,
116-
portMAX_DELAY
117-
);
118-
#endif
108+
vTaskDelay(2000 / portTICK_RATE_MS);
109+
119110
EventBits_t uxBits = xEventGroupGetBits(user_event_group);
120-
if (uxBits & BT_A2DP_IDLE_BIT) {
121-
ESP_LOGI(BT_APP_TAG, "reconnect to [%02x:%02x:%02x:%02x:%02x:%02x]",
111+
if (uxBits & BT_A2DP_IDLE_BIT && !(uxBits & OS_PWR_SLEEP_BIT) && !(uxBits & OS_PWR_RESET_BIT)) {
112+
ESP_LOGW(BT_APP_TAG, "connecting to [%02x:%02x:%02x:%02x:%02x:%02x]",
122113
last_remote_bda[0], last_remote_bda[1], last_remote_bda[2],
123114
last_remote_bda[3], last_remote_bda[4], last_remote_bda[5]);
124115

125116
esp_a2d_sink_connect(last_remote_bda);
126-
} else {
127-
ESP_LOGW(BT_APP_TAG, "reconnecting aborted");
128117
}
129118
}
130119
}

main/src/user/bt_av.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ static void bt_av_hdl_a2d_evt(uint16_t event, void *p_param)
138138

139139
EventBits_t uxBits = xEventGroupGetBits(user_event_group);
140140
if (!(uxBits & OS_PWR_SLEEP_BIT) && !(uxBits & OS_PWR_RESET_BIT)) {
141+
xEventGroupSetBits(user_event_group, KEY_SCAN_RUN_BIT | KEY_SCAN_CLR_BIT);
142+
141143
esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
142144
}
143145

@@ -152,6 +154,11 @@ static void bt_av_hdl_a2d_evt(uint16_t event, void *p_param)
152154
} else if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_CONNECTED) {
153155
xEventGroupClearBits(user_event_group, BT_A2DP_IDLE_BIT);
154156

157+
EventBits_t uxBits = xEventGroupGetBits(user_event_group);
158+
if (!(uxBits & OS_PWR_SLEEP_BIT) && !(uxBits & OS_PWR_RESET_BIT)) {
159+
xEventGroupSetBits(user_event_group, KEY_SCAN_RUN_BIT | KEY_SCAN_CLR_BIT);
160+
}
161+
155162
memcpy(&a2d_remote_bda, a2d->conn_stat.remote_bda, sizeof(esp_bd_addr_t));
156163

157164
if (memcmp(&last_remote_bda, &a2d_remote_bda, sizeof(esp_bd_addr_t)) != 0) {
@@ -168,7 +175,7 @@ static void bt_av_hdl_a2d_evt(uint16_t event, void *p_param)
168175
led_set_mode(2);
169176
#endif
170177
} else {
171-
xEventGroupClearBits(user_event_group, BT_A2DP_IDLE_BIT);
178+
xEventGroupClearBits(user_event_group, BT_A2DP_IDLE_BIT | KEY_SCAN_RUN_BIT);
172179
}
173180
break;
174181
}

main/src/user/key.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* Author: Jack Chen <redchenjs@live.com>
66
*/
77

8+
#include <string.h>
9+
810
#include "esp_log.h"
911

1012
#include "freertos/FreeRTOS.h"
@@ -73,14 +75,20 @@ static void key_task(void *pvParameter)
7375
vTaskDelay(2000 / portTICK_RATE_MS);
7476

7577
while (1) {
76-
xEventGroupWaitBits(
78+
EventBits_t uxBits = xEventGroupWaitBits(
7779
user_event_group,
7880
KEY_SCAN_RUN_BIT,
7981
pdFALSE,
8082
pdFALSE,
8183
portMAX_DELAY
8284
);
8385

86+
if (uxBits & KEY_SCAN_CLR_BIT) {
87+
memset(&count, 0x00, sizeof(count));
88+
89+
xEventGroupClearBits(user_event_group, KEY_SCAN_CLR_BIT);
90+
}
91+
8492
xLastWakeTime = xTaskGetTickCount();
8593

8694
for (int i=0; i<sizeof(gpio_pin); i++) {

main/src/user/ota.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,6 @@ void ota_end(void)
363363
led_set_mode(3);
364364
#endif
365365

366-
xEventGroupSetBits(user_event_group, KEY_SCAN_RUN_BIT);
366+
xEventGroupSetBits(user_event_group, KEY_SCAN_RUN_BIT | KEY_SCAN_CLR_BIT);
367367
}
368368
}

0 commit comments

Comments
 (0)