本帖最后由 donatello1996 于 2024-1-16 23:27 编辑
功能解析与代码片段解读(STM32H747开发板部分):
LVGL界面初始化,需要初始化三个文本框,两个按钮,三个文本框中的最顶部框为状态指示框,分别显示语音识别流程,从录音->录音语音识别->访问文心一言获取回答->将回答进行语音合成->播放五个环节,环节循环进行;
第二个文本框为录音问题提问框,将录音识别而成的文字显示在此框;
第三个文本框位回答显示框,将文心一言的回答文字显示在此框;
【开始流程】按钮按下后录音开始,整个流程也开始;
【清除问题和回答】框将问题文本框和回答文本框的内容全部清除;
void lv_mainstart(void)
{
uint8_t utf8_chinese[200];
dropdown_font = &myFont;
//dropdown_font = &lv_font_montserrat_22;
dropdown_width = 150;
ta_state = lv_textarea_create(lv_scr_act());
lv_obj_align(ta_state , LV_ALIGN_TOP_LEFT , 10 , 10);
lv_obj_set_style_text_font(ta_state , dropdown_font, LV_STATE_DEFAULT);
//lv_obj_add_event_cb(ta,textarea_Show1_event_cb,LV_EVENT_READY,ta);
lv_obj_set_size(ta_state , 700 , 50);
lv_obj_add_state(ta_state , LV_STATE_FOCUSED);
ta_question = lv_textarea_create(lv_scr_act());
lv_obj_align(ta_question , LV_ALIGN_TOP_LEFT , 10 , 60);
lv_obj_set_style_text_font(ta_question , dropdown_font, LV_STATE_DEFAULT);
//lv_obj_add_event_cb(ta,textarea_Show1_event_cb,LV_EVENT_READY,ta);
lv_obj_set_size(ta_question , 700 , 99);
lv_obj_add_state(ta_question , LV_STATE_FOCUSED);
//lv_obj_add_state(ta1 , LV_STATE_DEFAULT);
btn_start = lv_btn_create(lv_scr_act());
lv_obj_set_size(btn_start, 200 , 50);
lv_obj_align(btn_start, LV_ALIGN_TOP_LEFT, 10 , 160);
lv_obj_add_event_cb(btn_start, btn_event_cb, LV_EVENT_CLICKED, NULL);
lv_obj_t* label_btn_start = lv_label_create(btn_start);
lv_obj_set_style_text_font(label_btn_start , dropdown_font, LV_PART_MAIN);
lv_obj_set_align(label_btn_start , LV_ALIGN_CENTER);
ANSI_to_UTF8("开始流程" , utf8_chinese);
lv_label_set_text(label_btn_start , utf8_chinese);
btn_clean = lv_btn_create(lv_scr_act());
lv_obj_set_size(btn_clean , 300 , 50);
lv_obj_align(btn_clean , LV_ALIGN_TOP_LEFT, 220 , 160);
lv_obj_add_event_cb(btn_clean , btn_event_cb, LV_EVENT_CLICKED, NULL);
lv_obj_t* label_btn_clean = lv_label_create(btn_clean);
lv_obj_set_style_text_font(label_btn_clean , dropdown_font, LV_PART_MAIN);
lv_obj_set_align(label_btn_clean , LV_ALIGN_CENTER);
ANSI_to_UTF8("清除问题和回答" , utf8_chinese);
lv_label_set_text(label_btn_clean , utf8_chinese);
ta_answer = lv_textarea_create(lv_scr_act());
lv_obj_align(ta_answer , LV_ALIGN_TOP_LEFT , 10 , 211);
lv_obj_set_style_text_font(ta_answer , dropdown_font, LV_STATE_DEFAULT);
//lv_obj_add_event_cb(ta,textarea_Show1_event_cb,LV_EVENT_READY,ta);
lv_obj_set_size(ta_answer , 700 , 268);
lv_obj_add_state(ta_answer , LV_STATE_DEFAULT);
}
点击按钮控制GPIO发送上升沿信号,GPIO初始化状态为低电平,状态文本框会在不同阶段实时改变:
static void btn_event_cb(lv_event_t* e)
{
lv_obj_t *target = lv_event_get_target(e);
if(target == btn_start)
{
printf("btn_start.\n");
HAL_GPIO_WritePin(GPIOJ , GPIO_PIN_0 , GPIO_PIN_SET);
}
else if(target == btn_clean)
{
printf("btn_clean.\n");
lv_textarea_set_text(ta_question , "");
lv_textarea_set_text(ta_answer , "");
}
}
void GPIOJ0_Init()
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOJ_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOJ , &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOJ , GPIO_PIN_0 , GPIO_PIN_SET);
}
lv_init();
lv_port_disp_init();
lv_port_indev_init();
lv_mainstart();
while(1)
{
count ++;
if(count > 100)
{
count = 0;
HAL_GPIO_WritePin(GPIOJ , GPIO_PIN_0 , GPIO_PIN_RESET);
}
lv_task_handler();
lv_tick_inc(1);
if(uart8_recv_end_flag)
{
uart8_recv_end_flag = 0;
printf("rx8_buf[0] == 0x%x && rx8_buf[1] == 0x%x.\n" , rx8_buf[0] , rx8_buf[1]);
if(rx8_buf[0] == 0x11 && rx8_buf[1] == 0x11)
{
ANSI_to_UTF8(rx8_buf + 2 , utf8_global);
lv_textarea_set_text(ta_state , utf8_global);
}
else if(rx8_buf[0] == 0x11 && rx8_buf[1] == 0x12)
{
ANSI_to_UTF8(rx8_buf + 2 , utf8_global);
lv_textarea_set_text(ta_question , utf8_global);
}
else if(rx8_buf[0] == 0x11 && rx8_buf[1] == 0x13)
{
ANSI_to_UTF8(rx8_buf + 2 , utf8_global);
lv_textarea_set_text(ta_answer , utf8_global);
}
else if(rx8_buf[0] == 0x11 && rx8_buf[1] == 0x14)
{
lv_textarea_set_text(ta_question , rx8_buf + 2);
}
else if(rx8_buf[0] == 0x11 && rx8_buf[1] == 0x15)
{
lv_textarea_set_text(ta_answer , rx8_buf + 2);
}
else if(rx8_buf[0] == 0x11 && rx8_buf[1] == 0x16 && rx8_buf[2] == 1)
{
BSP_LED_Off(LED_RED);
BSP_LED_Off(LED_ORANGE);
BSP_LED_Off(LED_BLUE);
BSP_LED_Off(LED_GREEN);
}
else if(rx8_buf[0] == 0x11 && rx8_buf[1] == 0x16 && rx8_buf[2] == 2)
{
BSP_LED_On(LED_RED);
BSP_LED_Off(LED_ORANGE);
BSP_LED_Off(LED_BLUE);
BSP_LED_Off(LED_GREEN);
}
else if(rx8_buf[0] == 0x11 && rx8_buf[1] == 0x16 && rx8_buf[2] == 3)
{
BSP_LED_Off(LED_RED);
BSP_LED_On(LED_ORANGE);
BSP_LED_Off(LED_BLUE);
BSP_LED_Off(LED_GREEN);
}
else if(rx8_buf[0] == 0x11 && rx8_buf[1] == 0x16 && rx8_buf[2] == 4)
{
BSP_LED_Off(LED_RED);
BSP_LED_Off(LED_ORANGE);
BSP_LED_On(LED_BLUE);
BSP_LED_Off(LED_GREEN);
}
else if(rx8_buf[0] == 0x11 && rx8_buf[1] == 0x16 && rx8_buf[2] == 5)
{
BSP_LED_Off(LED_RED);
BSP_LED_Off(LED_ORANGE);
BSP_LED_Off(LED_BLUE);
BSP_LED_On(LED_GREEN);
}
else if(rx8_buf[0] == 0x11 && rx8_buf[1] == 0x16 && rx8_buf[2] == 6)
{
BSP_LED_On(LED_RED);
BSP_LED_On(LED_ORANGE);
BSP_LED_On(LED_BLUE);
BSP_LED_On(LED_GREEN);
}
}
}
|