KT25-1014_AC695x_BTE_SDK310/apps/kaotings/kt.c

602 lines
13 KiB
C
Raw Normal View History

#include "kt.h"
#include "kt_dbg.h"
#include "bt.h"
#include "bt_emitter.h"
#include "asm/mcpwm.h"
#include "avctp_user.h"
#include "key_event_deal.h"
2025-10-19 14:43:56 +00:00
#include "audio_config.h"
#include "app_task.h"
#include "app_main.h"
#include "bt_emitter.h"
2025-10-19 14:43:56 +00:00
// #include "task_manager/linein/linein.h"
#include "bt.h"
2025-10-19 14:43:56 +00:00
extern int linein_volume_set(u8 vol);
extern void mcpwm_init(struct pwm_platform_data *arg);
typedef struct _kt_var_
{
// u16 tid;
struct kt
{
u16 tid;
int led_cnt;
} tim;
struct
{
u8 curr_mode;
u8 last_mode;
u8 next_mode;
u8 user_role;
int cnt;
} mode;
// u8 curr_task_mode;
// u8 mode;
} _kt_var;
static _kt_var kt_var;
#define __this (&kt_var)
static char *mode_name[] = {
"MODE_AUX_LP",
"MODE_BT_EMITTER",
"MODE_BT_RECEIVER",
};
u8 kt_get_mode(void)
{
u32 adc_value = 0;
// int cnt = 0;
// u8 temp_mode = 0;
adc_value = adc_get_value(CFG_MODE_AD_CH);
2025-10-19 14:43:56 +00:00
// printf("----------->>>>>>>>>>>>>>> adc_value = %d <<<<<<<<<<<----------\n", adc_value);
if (adc_value > 80 && adc_value < 100)
{
2025-10-19 14:43:56 +00:00
// printf("----------->>>>>>>>>>>>>>> temp_mode = MODE_AUX_LP <<<<<<<<<<<----------\n");
return MODE_AUX_LP;
}
else if (adc_value > 330 && adc_value < 370)
{
2025-10-19 14:43:56 +00:00
// printf("----------->>>>>>>>>>>>>>> temp_mode = MODE_BT_RECEIVER <<<<<<<<<<<----------\n");
return MODE_BT_RECEIVER;
}
else
{
2025-10-19 14:43:56 +00:00
// printf("----------->>>>>>>>>>>>>>> temp_mode = MODE_BT_EMITTER <<<<<<<<<<<----------\n");
return MODE_BT_EMITTER;
}
#if 0
while (1)
{
adc_value = adc_get_value(CFG_MODE_AD_CH);
if (adc_value > 80 && adc_value < 100)
{
if (temp_mode != MODE_AUX_LP)
{
temp_mode = MODE_AUX_LP;
cnt = 0;
}
else
{
cnt++;
if (cnt >= 3)
{
break;
}
}
}
else if (adc_value > 330 && adc_value < 370)
{
if (temp_mode != MODE_BT_RECEIVER)
{
temp_mode = MODE_BT_RECEIVER;
cnt = 0;
}
else
{
cnt++;
if (cnt >= 3)
{
break;
}
}
}
else
{
if (temp_mode != MODE_BT_EMITTER)
{
temp_mode = MODE_BT_EMITTER;
cnt = 0;
}
else
{
cnt++;
if (cnt >= 3)
{
break;
}
}
}
clr_wdt();
os_time_dly(1);
}
printf("----------->>>>>>>>>>>>>>> temp_mode = %s <<<<<<<<<<<----------\n", mode_name[temp_mode]);
return temp_mode;
#endif
}
void kt_set_curr_mode(u8 mode)
{
__this->mode.curr_mode = mode;
}
u8 kt_get_curr_mode(void)
{
return __this->mode.curr_mode;
}
void kt_boot_init(void)
{
log_i("kt_boot_init");
// mute pin
gpio_set_pull_down(CFG_MUTE_PIN, 0);
gpio_set_pull_up(CFG_MUTE_PIN, 0);
gpio_set_direction(CFG_MUTE_PIN, 0);
PA_MUTE();
/* 初始化Kaotings调试系统默认INFO级别 */
kt_dbg_set_level(_DBG_LEVEL_);
KT_LOGI("KT", "dbg ready @level=%d", kt_dbg_get_level());
2025-10-19 14:43:56 +00:00
// volume pin adc ch
gpio_set_die(CFG_VOLUME_PIN, 0);
gpio_set_direction(CFG_VOLUME_PIN, 1);
gpio_set_pull_down(CFG_VOLUME_PIN, 0);
gpio_set_pull_up(CFG_VOLUME_PIN, 1);
adc_add_sample_ch(CFG_VOLUME_AD_CH);
// mode pin adc ch
gpio_set_die(CFG_MODE_PIN, 0);
gpio_set_direction(CFG_MODE_PIN, 1);
gpio_set_pull_down(CFG_MODE_PIN, 0);
gpio_set_pull_up(CFG_MODE_PIN, 1);
adc_add_sample_ch(CFG_MODE_AD_CH);
// lp mot pin
gpio_set_pull_down(CFG_LP_MOT_PIN, 0);
gpio_set_pull_up(CFG_LP_MOT_PIN, 0);
gpio_set_direction(CFG_LP_MOT_PIN, 0);
LP_MOT_OFF();
// pa pwr pin
gpio_set_pull_down(CFG_PA_PWR_PIN, 0);
gpio_set_pull_up(CFG_PA_PWR_PIN, 0);
gpio_set_direction(CFG_PA_PWR_PIN, 0);
PA_PWR_OFF();
// led red pin
gpio_set_pull_down(CFG_LED_RED_PIN, 0);
gpio_set_pull_up(CFG_LED_RED_PIN, 0);
gpio_set_direction(CFG_LED_RED_PIN, 0);
LED_RED_OFF();
// led blue pin
gpio_set_pull_down(CFG_LED_BLUE_PIN, 0);
gpio_set_pull_up(CFG_LED_BLUE_PIN, 0);
gpio_set_direction(CFG_LED_BLUE_PIN, 0);
LED_BLUE_OFF();
gpio_set_pull_down(IO_PORTC_03, 0);
gpio_set_pull_up(IO_PORTC_03, 0);
gpio_set_direction(IO_PORTC_03, 0);
gpio_set_output_value(IO_PORTC_03, 1);
struct pwm_platform_data pwm_p_data;
pwm_p_data.pwm_aligned_mode = pwm_edge_aligned; // 边沿对齐
pwm_p_data.frequency = 20000; // 1KHz
// pwm_p_data.frequency = 50000; //1KHz
pwm_p_data.pwm_ch_num = pwm_ch5; // 通道0
pwm_p_data.pwm_timer_num = pwm_timer5; // 时基选择通道0
pwm_p_data.duty = 5000; // 占空比50%
pwm_p_data.h_pin = IO_PORTC_04; // 没有则填 -1。h_pin_output_ch_num无效可不配置
pwm_p_data.l_pin = -1; // 硬件引脚l_pin_output_ch_num无效可不配置
pwm_p_data.complementary_en = 1; // 两个引脚的波形同步
mcpwm_init(&pwm_p_data);
}
static void mode_led_flash(void)
{
static int cnt = 0;
2025-10-19 14:43:56 +00:00
// printf("---------------> mode_led_flash %s <---------------\n", mode_name[__this->mode.curr_mode]);
if (__this->mode.curr_mode == MODE_BT_RECEIVER)
{
LED_RED_OFF();
2025-10-19 14:43:56 +00:00
// printf("1-%d\n", get_bt_connect_status());
if (get_bt_connect_status() < BT_STATUS_CONNECTING)
{
__this->tim.led_cnt++;
if (__this->tim.led_cnt < 10)
{
LED_BLUE_ON();
}
else if (__this->tim.led_cnt < 20)
{
LED_BLUE_OFF();
}
else
{
__this->tim.led_cnt = 0;
LED_BLUE_ON();
}
}
else
{
LED_BLUE_ON();
}
}
else if (__this->mode.curr_mode == MODE_BT_EMITTER)
{
// LED_RED_OFF();
2025-10-19 14:43:56 +00:00
// printf("2-%d\n", get_bt_connect_status());
/* if(cnt++ > 40)
{
cnt = 0;
printf("2-%d\n", get_bt_connect_status());
} */
2025-10-19 14:43:56 +00:00
// printf("---------------> get_bt_connect_status %d <---------------\n", get_bt_connect_status());
if (get_total_connect_dev() == 0)
{
__this->tim.led_cnt++;
if (__this->tim.led_cnt < 4)
{
LED_RED_ON();
LED_BLUE_ON();
}
else if (__this->tim.led_cnt < 8)
{
LED_RED_OFF();
LED_BLUE_OFF();
}
else
{
__this->tim.led_cnt = 0;
LED_RED_ON();
LED_BLUE_ON();
}
}
else
{
LED_RED_ON();
LED_BLUE_ON();
}
}
else
{
LED_BLUE_OFF();
LED_RED_ON();
}
}
static void mode_check(void)
{
u8 mode = kt_get_mode();
if (mode != __this->mode.next_mode)
{
__this->mode.next_mode = mode;
__this->mode.cnt = 0;
}
else
{
__this->mode.cnt++;
if (__this->mode.cnt >= 3)
{
if (mode == __this->mode.curr_mode)
{
__this->mode.cnt = 0;
return;
}
else
{
__this->mode.cnt = 0;
app_task_put_key_msg(KEY_MSG_MODE_CHANGE, 0);
}
}
}
}
2025-10-19 14:43:56 +00:00
static void volume_check(void)
{
static u32 last_vol_value = 0;
u32 temp = adc_get_value(CFG_VOLUME_AD_CH);
if (temp > last_vol_value + 3)
{
last_vol_value = temp;
app_task_put_key_msg(KEY_MSG_VOLUME_CHANGE, temp);
}
else if (temp < last_vol_value - 3)
{
last_vol_value = temp;
app_task_put_key_msg(KEY_MSG_VOLUME_CHANGE, temp);
}
else
{
// last_vol_value = temp;
}
}
//int test = 0;
static void timer_callback(void *priv)
{
2025-10-19 14:43:56 +00:00
volume_check();
mode_check();
mode_led_flash();
2025-10-19 14:43:56 +00:00
/* test++;
if (test > 100)
{
test = 0;
app_task_put_key_msg(KEY_MSG_EQ_TEST_CHANGE, 0);
} */
}
static void start_timer(void)
{
if (__this->tim.tid != 0)
{
sys_timer_del(__this->tim.tid);
}
__this->tim.led_cnt = 0;
__this->tim.tid = sys_timer_add(NULL, timer_callback, 50);
}
void kt_init(void)
{
PA_PWR_ON();
start_timer();
}
u8 kt_get_user_role(void)
{
return __this->mode.user_role;
}
static u8 kt_mode_change(u8 mode)
{
printf("----------->>>>>>>>>>>>>>> curr_mode[%s] to [%s] <<<<<<<<<<<----------\n", mode_name[__this->mode.curr_mode], mode_name[mode]);
if (mode == MODE_BT_RECEIVER)
{
printf("switch to receiver mode\n");
__this->mode.user_role = MODE_BT_RECEIVER;
2025-10-19 14:43:56 +00:00
LP_MOT_OFF();
// 判断当前是否在蓝牙模式
if (app_get_curr_task() != APP_BT_TASK)
{
// 如果当前不在蓝牙模式,还需要判断当前是否是发射模式,直接切换回发生蓝牙冲入错误,造成死机
printf("current task is not bt task, need check emitter role and switch to bt task\n");
2025-10-19 14:43:56 +00:00
if (__this->mode.curr_mode == MODE_BT_EMITTER)
{
printf("current is emitter, need switch to receiver\n");
emitter_or_receiver_switch(BT_RECEIVER_EN);
}
else
{
2025-10-19 14:43:56 +00:00
// 当前仅仅是Linein模式直接切换到蓝牙模式
printf("current is linein, need switch to bt task\n");
app_task_switch_to(APP_BT_TASK);
}
}
else
{
2025-10-19 14:43:56 +00:00
// 当前已经是蓝牙模式,再判断当前角色是否是发射器
if (bt_emitter_role_get() == BT_EMITTER_EN)
{
printf("current is emitter, need switch to receiver\n");
emitter_or_receiver_switch(BT_RECEIVER_EN);
}
else
{
printf("current is receiver, no need switch\n");
2025-10-19 14:43:56 +00:00
// emitter_or_receiver_switch(BT_RECEIVER_EN);
}
}
return MODE_BT_RECEIVER;
}
else if (mode == MODE_BT_EMITTER)
{
__this->mode.user_role = MODE_BT_EMITTER;
2025-10-19 14:43:56 +00:00
LP_MOT_ON();
// printf("----------->>>>>>>>>>>>>>> switch to APP_LINEIN_TASK <<<<<<<<<<<----------\n");
// 需要切换到发射模式,首先判断当前是否在蓝牙模式
if (app_get_curr_task() == APP_BT_TASK)
{
printf("current task is bt task\n");
// 当前在蓝牙模式 需要判断当前角色是否是接收器
if (bt_emitter_role_get() == BT_RECEIVER_EN)
{
// 当前是接收器 需要切换到发射器
printf("current is receiver, need switch to emitter\n");
emitter_or_receiver_switch(BT_EMITTER_EN);
}
else
{
// 当前已经是发射器 不需要切换
printf("current is emitter, no need switch\n");
2025-10-19 14:43:56 +00:00
// emitter_or_receiver_switch(BT_RECEIVER_EN);
}
}
else
{
// 当前不在蓝牙模式 需要切换到蓝牙模式,
printf("current task is not bt task, need switch to bt task\n");
2025-10-19 14:43:56 +00:00
// 在蓝牙连接成功后会自动切换到LINEIN模式
printf("mode switch in connnected after bt task\n");
app_task_switch_to(APP_BT_TASK);
}
return MODE_BT_EMITTER;
}
else
{
printf("switch to aux lp mode\n");
__this->mode.user_role = MODE_AUX_LP;
2025-10-19 14:43:56 +00:00
LP_MOT_ON();
// 当前模式是否在发射模式,如果是在发射模式,需要先关闭连接和搜索
2025-10-19 14:43:56 +00:00
if (__this->mode.curr_mode == MODE_BT_EMITTER)
{
if (get_total_connect_dev() > 0)
{
user_send_cmd_prepare(USER_CTRL_POWER_OFF, 0, NULL);
}
2025-10-19 14:43:56 +00:00
printf("current mode is emitter, need close connection and search\n");
emitter_or_receiver_switch(BT_RECEIVER_EN);
bt_emitter_stop_search_device();
}
// 判断当前是否在LINEIN模式
if (app_get_curr_task() == APP_LINEIN_TASK)
{
2025-10-19 14:43:56 +00:00
// 当前在蓝牙模式 直接切换到LINEIN模式
printf("current task is linein task, no need switch\n");
app_task_switch_to(APP_LINEIN_TASK);
}
else
{
2025-10-19 14:43:56 +00:00
// 当前不在Linein模式 需要切换到Linein模式,
printf("current task is not linein task, need switch to linein task\n");
app_task_switch_to(APP_LINEIN_TASK);
}
}
#if 0
if (app_get_curr_task() != APP_LINEIN_TASK)
{
printf("----------->>>>>>>>>>>>>>> %s %d <<<<<<<<<<<----------\n", __func__, __LINE__);
app_task_switch_to(APP_LINEIN_TASK);
}
else
{
printf("----------->>>>>>>>>>>>>>> %s %d <<<<<<<<<<<----------\n", __func__, __LINE__);
if (bt_emitter_role_get() == BT_RECEIVER_EN)
{
printf("----------->>>>>>>>>>>>>>> %s %d <<<<<<<<<<<----------\n", __func__, __LINE__);
emitter_or_receiver_switch(BT_EMITTER_EN);
}
}
return MODE_BT_EMITTER;
}
else
{
//printf("----------->>>>>>>>>>>>>>> %s %d <<<<<<<<<<<----------\n", __func__, __LINE__);
if (app_get_curr_task() != APP_LINEIN_TASK)
{
printf("----------->>>>>>>>>>>>>>> %s %d <<<<<<<<<<<----------\n", __func__, __LINE__);
app_task_switch_to(APP_LINEIN_TASK);
}
else
{
printf("----------->>>>>>>>>>>>>>> %s %d <<<<<<<<<<<----------\n", __func__, __LINE__);
if (bt_emitter_role_get() == BT_EMITTER_EN)
{
printf("----------->>>>>>>>>>>>>>> %s %d <<<<<<<<<<<----------\n", __func__, __LINE__);
emitter_or_receiver_switch(BT_RECEIVER_EN);
}
}
return MODE_AUX_LP;
}
#endif
}
2025-10-19 14:43:56 +00:00
extern void eff_file_switch(u8 *path);
void kt_eq_sw_bt(void)
{
printf("--------> kt_eq_sw_bt \n");
eff_file_switch(EQ_FILE_BT);
}
void kt_eq_sw_lp(void)
{
printf("--------> kt_eq_sw_lp \n");
eff_file_switch(EQ_FILE_LP);
}
u8 eq = 0;
u8 kt_key_event_filter_after(int key_event, int key_value)
{
u8 ret = false;
switch (key_event)
{
2025-10-19 14:43:56 +00:00
/* case KEY_MSG_EQ_TEST_CHANGE:
{
if (eq == 0)
{
eq = 1;
kt_eq_sw_lp();
}
else
{
eq = 0;
kt_eq_sw_bt();
}
ret = true;
}
break; */
case KEY_MSG_VOLUME_CHANGE:
{
if (key_value > 1023)
{
key_value = 1023;
}
else if (key_value < 0)
{
key_value = 0;
}
u8 vol = (u8)(key_value / 1023.0 * app_audio_get_max_volume());
u8 temp = app_audio_get_volume(APP_AUDIO_STATE_MUSIC);
if (vol != temp)
{
if (app_get_curr_task() == APP_LINEIN_TASK)
{
linein_volume_set(vol);
}
else
{
app_audio_set_volume(APP_AUDIO_STATE_MUSIC, vol, 1);
}
}
ret = true;
}
break;
case KEY_MSG_MODE_CHANGE:
2025-10-19 14:43:56 +00:00
{
if (__this->mode.curr_mode != __this->mode.next_mode)
{
u8 mode = kt_get_mode();
if (mode == __this->mode.next_mode)
{
__this->mode.curr_mode = kt_mode_change(mode);
}
else
{
__this->mode.next_mode = mode;
__this->mode.cnt = 0;
}
}
__this->mode.cnt = 0;
ret = true;
2025-10-19 14:43:56 +00:00
}
break;
default:
{
ret = false;
}
break;
}
return ret;
}