Skip to content

Commit d45d1a3

Browse files
committed
main: code refactoring
1 parent 51f2c49 commit d45d1a3

File tree

16 files changed

+363
-157
lines changed

16 files changed

+363
-157
lines changed

main/inc/core/os.h

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,26 @@ typedef enum user_event_group_bits {
1515
VFX_RELOAD_BIT = BIT0,
1616
VFX_FFT_EXEC_BIT = BIT1,
1717
VFX_FFT_FULL_BIT = BIT2,
18+
1819
KEY_SCAN_RUN_BIT = BIT3,
19-
BT_A2DP_IDLE_BIT = BIT4,
20-
BT_OTA_LOCKED_BIT = BIT5,
21-
BT_OTA_RESTART_BIT = BIT6,
22-
BLE_OTA_LOCKED_BIT = BIT7,
23-
AUDIO_INPUT_RUN_BIT = BIT8,
24-
AUDIO_PLAYER_RUN_BIT = BIT9,
25-
AUDIO_PLAYER_IDLE_BIT = BIT10,
20+
21+
BT_SPP_IDLE_BIT = BIT4,
22+
BT_A2DP_IDLE_BIT = BIT5,
23+
BT_OTA_LOCKED_BIT = BIT6,
24+
BLE_GATTS_IDLE_BIT = BIT7,
25+
26+
OS_PWR_SLEEP_BIT = BIT8,
27+
OS_PWR_RESTART_BIT = BIT9,
28+
29+
AUDIO_INPUT_RUN_BIT = BIT10,
30+
AUDIO_PLAYER_RUN_BIT = BIT11,
31+
AUDIO_PLAYER_IDLE_BIT = BIT12,
2632
} user_event_group_bits_t;
2733

2834
extern EventGroupHandle_t user_event_group;
2935

30-
#if defined(CONFIG_ENABLE_WAKEUP_KEY) || defined(CONFIG_ENABLE_SLEEP_KEY)
31-
extern void os_enter_sleep_mode(void);
32-
#endif
36+
extern void os_power_sleep_wait(EventBits_t bits);
37+
extern void os_power_restart_wait(EventBits_t bits);
3338

3439
extern void os_init(void);
3540

main/inc/user/ble_app_gatts.h

Lines changed: 0 additions & 15 deletions
This file was deleted.

main/inc/user/ble_gatts.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* ble_gatts.h
3+
*
4+
* Created on: 2018-05-12 22:31
5+
* Author: Jack Chen <[email protected]>
6+
*/
7+
8+
#ifndef INC_USER_BLE_GATTS_H_
9+
#define INC_USER_BLE_GATTS_H_
10+
11+
#include "esp_gatts_api.h"
12+
13+
#define PROFILE_NUM 1
14+
#define PROFILE_A_APP_ID 0
15+
16+
typedef struct gatts_profile_inst {
17+
esp_gatts_cb_t gatts_cb;
18+
uint16_t gatts_if;
19+
uint16_t app_id;
20+
uint16_t conn_id;
21+
uint16_t service_handle;
22+
esp_gatt_srvc_id_t service_id;
23+
uint16_t char_handle;
24+
esp_bt_uuid_t char_uuid;
25+
esp_gatt_perm_t perm;
26+
esp_gatt_char_prop_t property;
27+
uint16_t descr_handle;
28+
esp_bt_uuid_t descr_uuid;
29+
} gatts_profile_inst_t;
30+
31+
extern gatts_profile_inst_t gl_profile_tab[];
32+
33+
extern void ble_gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
34+
35+
#endif /* INC_USER_BLE_GATTS_H_ */

main/inc/user/bt_app_av.h renamed to main/inc/user/bt_av.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
2-
* bt_app_av.h
2+
* bt_av.h
33
*
44
* Created on: 2019-04-29 12:31
55
* Author: Jack Chen <[email protected]>
66
*/
77

8-
#ifndef INC_USER_BT_APP_AV_H_
9-
#define INC_USER_BT_APP_AV_H_
8+
#ifndef INC_USER_BT_AV_H_
9+
#define INC_USER_BT_AV_H_
1010

1111
#include <stdint.h>
1212
#include "esp_a2dp_api.h"
@@ -34,4 +34,4 @@ void bt_app_avrc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *par
3434
*/
3535
void bt_app_avrc_tg_cb(esp_avrc_tg_cb_event_t event, esp_avrc_tg_cb_param_t *param);
3636

37-
#endif /* INC_USER_BT_APP_AV_H_*/
37+
#endif /* INC_USER_BT_AV_H_*/
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
/*
2-
* bt_app_spp.h
2+
* bt_spp.h
33
*
44
* Created on: 2019-07-03 15:48
55
* Author: Jack Chen <[email protected]>
66
*/
77

8-
#ifndef INC_USER_BT_APP_SPP_H_
9-
#define INC_USER_BT_APP_SPP_H_
8+
#ifndef INC_USER_BT_SPP_H_
9+
#define INC_USER_BT_SPP_H_
1010

1111
#include "esp_spp_api.h"
1212

13+
extern uint32_t spp_conn_handle;
1314
extern esp_bd_addr_t spp_remote_bda;
1415

1516
extern void bt_app_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param);
1617

17-
#endif /* INC_USER_BT_APP_SPP_H_ */
18+
#endif /* INC_USER_BT_SPP_H_ */

main/src/core/os.c

Lines changed: 111 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,149 @@
1111
#include "freertos/FreeRTOS.h"
1212
#include "freertos/event_groups.h"
1313
#include "freertos/task.h"
14+
#include "driver/rtc_io.h"
1415

1516
#include "core/os.h"
1617
#include "user/audio_player.h"
1718

18-
#define TAG "os"
19+
#define OS_PWR_TAG "os_power"
1920

2021
EventGroupHandle_t user_event_group;
2122

22-
#if defined(CONFIG_ENABLE_WAKEUP_KEY) || defined(CONFIG_ENABLE_SLEEP_KEY)
23-
void os_enter_sleep_mode(void)
23+
#if defined(CONFIG_ENABLE_WAKEUP_KEY) || defined(CONFIG_ENABLE_SLEEP_KEY) || defined(CONFIG_ENABLE_OTA_OVER_SPP)
24+
static EventBits_t sleep_wait_bits = 0;
25+
static EventBits_t restart_wait_bits = 0;
26+
27+
static void os_power_task_handle(void *pvParameters)
2428
{
25-
ESP_LOGI(TAG, "entering sleep mode");
29+
ESP_LOGI(OS_PWR_TAG, "started.");
30+
31+
while (1) {
32+
xEventGroupWaitBits(
33+
user_event_group,
34+
OS_PWR_SLEEP_BIT | OS_PWR_RESTART_BIT,
35+
pdFALSE,
36+
pdFALSE,
37+
portMAX_DELAY
38+
);
39+
40+
EventBits_t uxBits = xEventGroupGetBits(user_event_group);
41+
if (uxBits & OS_PWR_SLEEP_BIT) {
42+
for (int i=3; i>0; i--) {
43+
ESP_LOGW(OS_PWR_TAG, "sleep countdown...%d", i);
44+
vTaskDelay(1000 / portTICK_RATE_MS);
45+
}
46+
47+
xEventGroupWaitBits(
48+
user_event_group,
49+
sleep_wait_bits,
50+
pdFALSE,
51+
pdTRUE,
52+
portMAX_DELAY
53+
);
54+
55+
#ifdef CONFIG_ENABLE_WAKEUP_KEY
56+
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
57+
rtc_gpio_set_direction(CONFIG_WAKEUP_KEY_PIN, RTC_GPIO_MODE_INPUT_ONLY);
58+
#ifdef CONFIG_WAKEUP_KEY_ACTIVE_LOW
59+
rtc_gpio_pulldown_dis(CONFIG_WAKEUP_KEY_PIN);
60+
rtc_gpio_pullup_en(CONFIG_WAKEUP_KEY_PIN);
61+
esp_sleep_enable_ext1_wakeup(1ULL << CONFIG_WAKEUP_KEY_PIN, ESP_EXT1_WAKEUP_ALL_LOW);
62+
#else
63+
rtc_gpio_pullup_dis(CONFIG_WAKEUP_KEY_PIN);
64+
rtc_gpio_pulldown_en(CONFIG_WAKEUP_KEY_PIN);
65+
esp_sleep_enable_ext1_wakeup(1ULL << CONFIG_WAKEUP_KEY_PIN, ESP_EXT1_WAKEUP_ANY_HIGH);
66+
#endif
67+
#endif
2668

69+
ESP_LOGW(OS_PWR_TAG, "sleep now");
70+
esp_deep_sleep_start();
71+
} else if (uxBits & OS_PWR_RESTART_BIT) {
72+
for (int i=3; i>0; i--) {
73+
ESP_LOGW(OS_PWR_TAG, "restart countdown...%d", i);
74+
vTaskDelay(1000 / portTICK_RATE_MS);
75+
}
76+
77+
xEventGroupWaitBits(
78+
user_event_group,
79+
restart_wait_bits,
80+
pdFALSE,
81+
pdTRUE,
82+
portMAX_DELAY
83+
);
84+
85+
ESP_LOGW(OS_PWR_TAG, "restart now");
86+
esp_restart();
87+
}
88+
}
89+
}
90+
91+
void os_power_sleep_wait(EventBits_t bits)
92+
{
93+
if (bits) {
94+
sleep_wait_bits = bits;
95+
xEventGroupSetBits(user_event_group, OS_PWR_SLEEP_BIT);
96+
} else {
2797
#ifdef CONFIG_ENABLE_WAKEUP_KEY
98+
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
99+
rtc_gpio_set_direction(CONFIG_WAKEUP_KEY_PIN, RTC_GPIO_MODE_INPUT_ONLY);
28100
#ifdef CONFIG_WAKEUP_KEY_ACTIVE_LOW
101+
rtc_gpio_pulldown_dis(CONFIG_WAKEUP_KEY_PIN);
102+
rtc_gpio_pullup_en(CONFIG_WAKEUP_KEY_PIN);
29103
esp_sleep_enable_ext1_wakeup(1ULL << CONFIG_WAKEUP_KEY_PIN, ESP_EXT1_WAKEUP_ALL_LOW);
30104
#else
105+
rtc_gpio_pullup_dis(CONFIG_WAKEUP_KEY_PIN);
106+
rtc_gpio_pulldown_en(CONFIG_WAKEUP_KEY_PIN);
31107
esp_sleep_enable_ext1_wakeup(1ULL << CONFIG_WAKEUP_KEY_PIN, ESP_EXT1_WAKEUP_ANY_HIGH);
32-
#endif // #ifdef CONFIG_WAKEUP_KEY_ACTIVE_LOW
33-
#endif // #ifdef CONFIG_ENABLE_WAKEUP_KEY
108+
#endif
109+
#endif
110+
ESP_LOGW(OS_PWR_TAG, "sleep now");
111+
esp_deep_sleep_start();
112+
}
113+
}
34114

35-
esp_deep_sleep_start();
115+
void os_power_restart_wait(EventBits_t bits)
116+
{
117+
if (bits) {
118+
restart_wait_bits = bits;
119+
xEventGroupSetBits(user_event_group, OS_PWR_RESTART_BIT);
120+
} else {
121+
ESP_LOGW(OS_PWR_TAG, "restart now");
122+
esp_restart();
123+
}
36124
}
37-
#endif // #if defined(CONFIG_ENABLE_WAKEUP_KEY) || defined(CONFIG_ENABLE_SLEEP_KEY)
125+
#endif
38126

39127
void os_init(void)
40128
{
129+
user_event_group = xEventGroupCreate();
130+
41131
#ifdef CONFIG_ENABLE_WAKEUP_KEY
42-
ESP_LOGI(TAG, "checking wakeup cause");
132+
ESP_LOGW(OS_PWR_TAG, "checking wakeup cause");
43133
if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_UNDEFINED) {
44134
vTaskDelay(CONFIG_WAKEUP_KEY_EXTRA_HOLD_TIME / portTICK_RATE_MS);
45135

46136
#ifdef CONFIG_WAKEUP_KEY_ACTIVE_LOW
47-
if (gpio_get_level(CONFIG_WAKEUP_KEY_PIN) == 0) {
137+
if (rtc_gpio_get_level(CONFIG_WAKEUP_KEY_PIN) == 0) {
48138
#else
49-
if (gpio_get_level(CONFIG_WAKEUP_KEY_PIN) == 1) {
139+
if (rtc_gpio_get_level(CONFIG_WAKEUP_KEY_PIN) == 1) {
50140
#endif
51-
ESP_LOGI(TAG, "resuming from sleep mode");
141+
ESP_LOGW(OS_PWR_TAG, "resuming from sleep mode");
52142
} else {
53-
ESP_LOGI(TAG, "resume aborted");
143+
ESP_LOGW(OS_PWR_TAG, "resume aborted");
54144

55-
os_enter_sleep_mode();
145+
os_power_sleep_wait(0);
56146
}
57147
} else {
58-
os_enter_sleep_mode();
148+
os_power_sleep_wait(0);
59149
}
60-
#endif // CONFIG_ENABLE_WAKEUP_KEY
61-
62-
user_event_group = xEventGroupCreate();
63150

64-
#ifdef CONFIG_ENABLE_WAKEUP_KEY
151+
#ifdef CONFIG_ENABLE_AUDIO_PROMPT
65152
audio_player_play_file(2);
66153
#endif
154+
#endif // CONFIG_ENABLE_WAKEUP_KEY
155+
156+
#if defined(CONFIG_ENABLE_WAKEUP_KEY) || defined(CONFIG_ENABLE_SLEEP_KEY) || defined(CONFIG_ENABLE_OTA_OVER_SPP)
157+
xTaskCreatePinnedToCore(os_power_task_handle, "OsPowerT", 2048, NULL, 5, NULL, 0);
158+
#endif
67159
}

main/src/user/audio_input.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818

1919
#define BUFF_SIZE (FFT_N * 4)
2020

21-
static uint8_t audio_input_mode = 0;
21+
static uint8_t audio_input_mode = 1;
2222

2323
static void audio_input_task_handle(void *pvParameters)
2424
{
25-
#ifndef CONFIG_AUDIO_INPUT_NONE
2625
size_t bytes_read = 0;
2726
char data[BUFF_SIZE] = {0};
2827

@@ -77,11 +76,11 @@ static void audio_input_task_handle(void *pvParameters)
7776
}
7877
#endif // CONFIG_ENABLE_VFX
7978
}
80-
#endif // CONFIG_AUDIO_INPUT_NONE
8179
}
8280

8381
void audio_input_set_mode(uint8_t idx)
8482
{
83+
#ifndef CONFIG_AUDIO_INPUT_NONE
8584
audio_input_mode = idx;
8685
ESP_LOGI(TAG, "mode %u", audio_input_mode);
8786

@@ -92,6 +91,7 @@ void audio_input_set_mode(uint8_t idx)
9291
}
9392

9493
xEventGroupSetBits(user_event_group, VFX_RELOAD_BIT | VFX_FFT_FULL_BIT);
94+
#endif
9595
}
9696

9797
uint8_t audio_input_get_mode(void)
@@ -101,5 +101,7 @@ uint8_t audio_input_get_mode(void)
101101

102102
void audio_input_init(void)
103103
{
104+
xEventGroupSetBits(user_event_group, AUDIO_INPUT_RUN_BIT);
105+
104106
xTaskCreatePinnedToCore(audio_input_task_handle, "AudioInputT", 2048, NULL, 8, NULL, 1);
105107
}

main/src/user/audio_player.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,7 @@ void audio_player_play_file(uint8_t idx)
114114

115115
void audio_player_init(void)
116116
{
117+
xEventGroupSetBits(user_event_group, AUDIO_PLAYER_IDLE_BIT);
118+
117119
xTaskCreatePinnedToCore(audio_player_task_handle, "AudioPlayerT", 8448, NULL, 8, NULL, 1);
118120
}

main/src/user/ble_app.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
#include "esp_gap_ble_api.h"
1414
#include "esp_gatt_common_api.h"
1515

16-
#include "user/ble_app_gatts.h"
16+
#include "core/os.h"
17+
#include "user/ble_gatts.h"
1718

1819
#define BLE_APP_TAG "ble_app"
1920
#define BLE_GAP_TAG "ble_gap"
@@ -117,7 +118,7 @@ void ble_app_init(void)
117118
ESP_ERROR_CHECK(esp_ble_gatts_register_callback(ble_gatts_event_handler));
118119
ESP_ERROR_CHECK(esp_ble_gap_register_callback(ble_gap_event_handler));
119120

120-
ESP_ERROR_CHECK(esp_ble_gatts_app_register(0));
121+
ESP_ERROR_CHECK(esp_ble_gatts_app_register(PROFILE_A_APP_ID));
121122

122123
ESP_ERROR_CHECK(esp_ble_gatt_set_local_mtu(500));
123124

0 commit comments

Comments
 (0)