Skip to content

Commit 5aed492

Browse files
committed
main: code refactoring
1 parent 58f75ef commit 5aed492

File tree

9 files changed

+59
-61
lines changed

9 files changed

+59
-61
lines changed

main/inc/core/os.h

+14-14
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@
1212
#include "freertos/event_groups.h"
1313

1414
typedef enum user_event_group_bits {
15-
OS_PWR_DUMMY_BIT = 0x00,
16-
OS_PWR_RESET_BIT = BIT0,
17-
OS_PWR_SLEEP_BIT = BIT1,
15+
OS_PWR_DUMMY_BIT = 0x00,
16+
OS_PWR_RESET_BIT = BIT0,
17+
OS_PWR_SLEEP_BIT = BIT1,
1818

19-
BT_A2DP_IDLE_BIT = BIT2,
20-
BT_A2DP_DATA_BIT = BIT3,
19+
BT_A2DP_IDLE_BIT = BIT2,
20+
BT_A2DP_DATA_BIT = BIT3,
2121

22-
BLE_GATTS_IDLE_BIT = BIT4,
23-
BLE_GATTS_LOCK_BIT = BIT5,
22+
VFX_RLD_MODE_BIT = BIT4,
23+
VFX_FFT_NULL_BIT = BIT5,
2424

25-
VFX_RLD_MODE_BIT = BIT6,
26-
VFX_FFT_NULL_BIT = BIT7,
25+
KEY_SCAN_RUN_BIT = BIT6,
26+
KEY_SCAN_CLR_BIT = BIT7,
2727

28-
KEY_SCAN_RUN_BIT = BIT8,
29-
KEY_SCAN_CLR_BIT = BIT9,
28+
BLE_GATTS_IDLE_BIT = BIT8,
29+
BLE_GATTS_LOCK_BIT = BIT9,
3030

31-
AUDIO_RENDER_CLR_BIT = BIT10,
31+
AUDIO_INPUT_RUN_BIT = BIT10,
32+
AUDIO_INPUT_FFT_BIT = BIT11,
3233

33-
AUDIO_INPUT_RUN_BIT = BIT11,
34-
AUDIO_INPUT_FFT_BIT = BIT12,
34+
AUDIO_RENDER_CLR_BIT = BIT12,
3535

3636
AUDIO_PLAYER_RUN_BIT = BIT13,
3737
AUDIO_PLAYER_IDLE_BIT = BIT14,

main/inc/user/bt_app_core.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ typedef void (*bt_app_cb_t)(uint16_t event, void *param);
1919

2020
/* message to be sent */
2121
typedef struct {
22-
uint16_t sig; /*!< signal to bt_app_task */
23-
uint16_t event; /*!< message event id */
24-
bt_app_cb_t cb; /*!< context switch callback */
25-
void *param; /*!< parameter area needs to be last */
22+
uint16_t sig; /*!< signal to bt_app_task */
23+
uint16_t event; /*!< message event id */
24+
bt_app_cb_t cb; /*!< context switch callback */
25+
void *param; /*!< parameter area needs to be last */
2626
} bt_app_msg_t;
2727

2828
/**

main/inc/user/vfx.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ typedef struct {
6161
uint8_t backlight;
6262
} vfx_config_t;
6363

64-
#define FFT_N 128
64+
#define FFT_N (128)
65+
#define FFT_BLOCK_SIZE (FFT_N * 4)
6566

6667
#if defined(CONFIG_VFX_OUTPUT_ST7735) || defined(CONFIG_VFX_OUTPUT_ST7789)
6768
#define DEFAULT_VFX_MODE VFX_MODE_IDX_SPECTRUM_M_N

main/src/user/ain.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static ain_mode_t ain_mode = DEFAULT_AIN_MODE;
2424

2525
static void ain_task(void *pvParameters)
2626
{
27-
char data[FFT_N * 4] = {0};
27+
char data[FFT_BLOCK_SIZE] = {0};
2828

2929
ESP_LOGI(TAG, "started.");
3030

@@ -38,7 +38,7 @@ static void ain_task(void *pvParameters)
3838
);
3939

4040
size_t bytes_read = 0;
41-
i2s_read(CONFIG_AUDIO_INPUT_I2S_NUM, data, FFT_N * 4, &bytes_read, portMAX_DELAY);
41+
i2s_read(CONFIG_AUDIO_INPUT_I2S_NUM, data, FFT_BLOCK_SIZE, &bytes_read, portMAX_DELAY);
4242

4343
#ifdef CONFIG_ENABLE_VFX
4444
// Copy data to FFT input buffer

main/src/user/audio_render.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222

2323
#define TAG "audio_render"
2424

25-
static uint8_t buff_data[1024 * 10] = {0};
26-
static StaticRingbuffer_t buff_struct = {0};
27-
2825
RingbufHandle_t audio_buff = NULL;
2926

27+
static StaticRingbuffer_t buff_struct = {0};
28+
static uint8_t buff_data[FFT_BLOCK_SIZE * 20] = {0};
29+
3030
/* render callback for the libmad synth */
3131
void render_sample_block(short *sample_buff_ch0, short *sample_buff_ch1, int num_samples, unsigned int num_channels)
3232
{
@@ -108,10 +108,10 @@ static void audio_render_task(void *pvParameter)
108108
if (start) {
109109
remain = sizeof(buff_data) - xRingbufferGetCurFreeSize(audio_buff);
110110

111-
if (remain >= 512) {
111+
if (remain >= FFT_BLOCK_SIZE) {
112112
count = 0;
113113

114-
data = (uint8_t *)xRingbufferReceiveUpTo(audio_buff, &size, 16 / portTICK_RATE_MS, 512);
114+
data = (uint8_t *)xRingbufferReceiveUpTo(audio_buff, &size, 16 / portTICK_RATE_MS, FFT_BLOCK_SIZE);
115115
} else if (remain > 0 && !(remain % 4)) {
116116
count = 0;
117117

@@ -135,7 +135,7 @@ static void audio_render_task(void *pvParameter)
135135
continue;
136136
}
137137
} else {
138-
if (xRingbufferGetCurFreeSize(audio_buff) < 512) {
138+
if (xRingbufferGetCurFreeSize(audio_buff) < FFT_BLOCK_SIZE) {
139139
start = true;
140140

141141
xEventGroupClearBits(user_event_group, AUDIO_RENDER_CLR_BIT);
@@ -168,7 +168,7 @@ static void audio_render_task(void *pvParameter)
168168

169169
#ifdef CONFIG_ENABLE_VFX
170170
uxBits = xEventGroupGetBits(user_event_group);
171-
if ((size != 512) || !(uxBits & VFX_FFT_NULL_BIT)) {
171+
if ((size != FFT_BLOCK_SIZE) || !(uxBits & VFX_FFT_NULL_BIT)) {
172172
vRingbufferReturnItem(audio_buff, (void *)data);
173173
continue;
174174
}

main/src/user/ble_app.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
#define BLE_GAP_TAG "ble_gap"
2121

2222
esp_ble_adv_params_t adv_params = {
23-
.adv_int_min = 0x20,
24-
.adv_int_max = 0x40,
25-
.adv_type = ADV_TYPE_IND,
26-
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
27-
.channel_map = ADV_CHNL_ALL,
28-
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
23+
.adv_int_min = 0x20,
24+
.adv_int_max = 0x40,
25+
.adv_type = ADV_TYPE_IND,
26+
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
27+
.channel_map = ADV_CHNL_ALL,
28+
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY
2929
};
3030

3131
static void ble_gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)

main/src/user/ble_gatts.c

+11-18
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
#define GATTS_OTA_TAG "gatts_ota"
2727
#define GATTS_VFX_TAG "gatts_vfx"
2828

29-
#define GATTS_SRV_UUID_OTA 0xFF52
30-
#define GATTS_CHAR_UUID_OTA 0x5201
31-
#define GATTS_NUM_HANDLE_OTA 4
29+
#define GATTS_SRV_UUID_OTA 0xFF52
30+
#define GATTS_CHAR_UUID_OTA 0x5201
31+
#define GATTS_NUM_HANDLE_OTA 4
3232

33-
#define GATTS_SRV_UUID_VFX 0xFF53
34-
#define GATTS_CHAR_UUID_VFX 0x5301
35-
#define GATTS_NUM_HANDLE_VFX 4
33+
#define GATTS_SRV_UUID_VFX 0xFF53
34+
#define GATTS_CHAR_UUID_VFX 0x5301
35+
#define GATTS_NUM_HANDLE_VFX 4
3636

3737
static uint16_t desc_val_ota = 0x0000;
3838
static uint16_t desc_val_vfx = 0x0000;
@@ -85,7 +85,7 @@ static void profile_ota_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t
8585
esp_ble_gatts_send_response(gatts_if, param->read.conn_id, param->read.trans_id, ESP_GATT_OK, &rsp);
8686
break;
8787
}
88-
case ESP_GATTS_WRITE_EVT: {
88+
case ESP_GATTS_WRITE_EVT:
8989
if (!param->write.is_prep) {
9090
if (param->write.handle == gatts_profile_tbl[PROFILE_IDX_OTA].descr_handle) {
9191
desc_val_ota = param->write.value[1] << 8 | param->write.value[0];
@@ -98,7 +98,6 @@ static void profile_ota_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t
9898
esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, NULL);
9999
}
100100
break;
101-
}
102101
case ESP_GATTS_EXEC_WRITE_EVT:
103102
break;
104103
case ESP_GATTS_MTU_EVT:
@@ -250,21 +249,19 @@ static void profile_vfx_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t
250249
esp_ble_gatts_send_response(gatts_if, param->read.conn_id, param->read.trans_id, ESP_GATT_OK, &rsp);
251250
break;
252251
}
253-
case ESP_GATTS_WRITE_EVT: {
252+
case ESP_GATTS_WRITE_EVT:
254253
if (!param->write.is_prep) {
255254
if (param->write.handle == gatts_profile_tbl[PROFILE_IDX_VFX].descr_handle) {
256255
desc_val_vfx = param->write.value[1] << 8 | param->write.value[0];
257-
} else {
258256
#ifdef CONFIG_ENABLE_VFX
257+
} else {
259258
vfx_config_t *vfx = vfx_get_conf();
260259
#ifndef CONFIG_AUDIO_INPUT_NONE
261260
ain_mode_t ain_mode = ain_get_mode();
262261
#endif
263-
#endif
264262
switch (param->write.value[0]) {
265-
case 0xEF: {
263+
case 0xEF:
266264
if (param->write.len == 1) { // restore default configuration
267-
#ifdef CONFIG_ENABLE_VFX
268265
vfx->mode = DEFAULT_VFX_MODE;
269266
vfx->scale_factor = DEFAULT_VFX_SCALE_FACTOR;
270267
vfx->lightness = DEFAULT_VFX_LIGHTNESS;
@@ -276,9 +273,7 @@ static void profile_vfx_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t
276273
ain_set_mode(ain_mode);
277274
app_setenv("AIN_INIT_CFG", &ain_mode, sizeof(ain_mode_t));
278275
#endif
279-
#endif
280276
} else if (param->write.len == 8) { // apply new configuration
281-
#ifdef CONFIG_ENABLE_VFX
282277
vfx->mode = param->write.value[1];
283278
vfx->scale_factor = param->write.value[2] << 8 | param->write.value[3];
284279
vfx->lightness = (param->write.value[4] << 8 | param->write.value[5]) % 0x0200;
@@ -290,24 +285,22 @@ static void profile_vfx_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t
290285
ain_set_mode(ain_mode);
291286
app_setenv("AIN_INIT_CFG", &ain_mode, sizeof(ain_mode_t));
292287
#endif
293-
#endif
294288
} else {
295289
ESP_LOGE(GATTS_VFX_TAG, "command 0x%02X error", param->write.value[0]);
296290
}
297291
break;
298-
}
299292
default:
300293
ESP_LOGW(GATTS_VFX_TAG, "unknown command: 0x%02X", param->write.value[0]);
301294
break;
302295
}
296+
#endif
303297
}
304298
}
305299

306300
if (param->write.need_rsp) {
307301
esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, NULL);
308302
}
309303
break;
310-
}
311304
case ESP_GATTS_EXEC_WRITE_EVT:
312305
break;
313306
case ESP_GATTS_MTU_EVT:

main/src/user/bt_av.c

+8-7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "user/led.h"
2121
#include "user/key.h"
22+
#include "user/vfx.h"
2223
#include "user/bt_app.h"
2324
#include "user/bt_app_core.h"
2425
#include "user/audio_player.h"
@@ -35,6 +36,9 @@
3536
#define APP_RC_CT_TL_RN_PLAYBACK_CHANGE (3)
3637
#define APP_RC_CT_TL_RN_PLAY_POS_CHANGE (4)
3738

39+
int a2d_sample_rate = 16000;
40+
esp_bd_addr_t a2d_remote_bda = {0};
41+
3842
/* a2dp event handler */
3943
static void bt_av_hdl_a2d_evt(uint16_t event, void *p_param);
4044
/* avrc CT event handler */
@@ -45,9 +49,6 @@ static const char *s_a2d_audio_state_str[] = {"suspended", "stopped", "started"}
4549

4650
static esp_avrc_rn_evt_cap_mask_t s_avrc_peer_rn_cap;
4751

48-
int a2d_sample_rate = 16000;
49-
esp_bd_addr_t a2d_remote_bda = {0};
50-
5152
/* callback for A2DP sink */
5253
void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param)
5354
{
@@ -76,14 +77,14 @@ void bt_app_a2d_data_cb(const uint8_t *data, uint32_t len)
7677
if (audio_buff) {
7778
uint32_t pkt = 0, remain = 0;
7879

79-
for (pkt = 0; pkt < len / 512; pkt++) {
80-
xRingbufferSend(audio_buff, (void *)(data + pkt * 512), 512, portMAX_DELAY);
80+
for (pkt = 0; pkt < len / FFT_BLOCK_SIZE; pkt++) {
81+
xRingbufferSend(audio_buff, (void *)(data + pkt * FFT_BLOCK_SIZE), FFT_BLOCK_SIZE, portMAX_DELAY);
8182
taskYIELD();
8283
}
8384

84-
remain = len - pkt * 512;
85+
remain = len - pkt * FFT_BLOCK_SIZE;
8586
if (remain != 0) {
86-
xRingbufferSend(audio_buff, (void *)(data + pkt * 512), remain, portMAX_DELAY);
87+
xRingbufferSend(audio_buff, (void *)(data + pkt * FFT_BLOCK_SIZE), remain, portMAX_DELAY);
8788
taskYIELD();
8889
}
8990
}

main/src/user/ota.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ enum rsp_idx {
6565
RSP_IDX_OK = 0x0,
6666
RSP_IDX_FAIL = 0x1,
6767
RSP_IDX_DONE = 0x2,
68-
RSP_IDX_ERROR = 0x3,
68+
RSP_IDX_ERROR = 0x3
6969
};
7070

7171
static const char rsp_str[][32] = {
@@ -89,8 +89,8 @@ static uint32_t data_length = 0;
8989

9090
static RingbufHandle_t ota_buff = NULL;
9191

92-
static const esp_partition_t *update_partition = NULL;
9392
static esp_ota_handle_t update_handle = 0;
93+
static const esp_partition_t *update_partition = NULL;
9494

9595
static int ota_parse_command(const char *data)
9696
{
@@ -282,6 +282,9 @@ void ota_exec(const char *data, uint32_t len)
282282
app_setenv("LAST_REMOTE_BDA", &last_remote_bda, sizeof(esp_bd_addr_t));
283283

284284
if (!update_handle) {
285+
#ifdef CONFIG_ENABLE_SLEEP_KEY
286+
key_set_scan_mode(KEY_SCAN_MODE_IDX_OFF);
287+
#endif
285288
#ifdef CONFIG_ENABLE_VFX
286289
vfx->mode = VFX_MODE_IDX_OFF;
287290
vfx_set_conf(vfx);

0 commit comments

Comments
 (0)