141 lines
4.4 KiB
C
141 lines
4.4 KiB
C
#include "K210_USART.h"
|
|
#include "Debug.h" // 添加调试支持
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
// 引用来自 main.c 的全局变量
|
|
extern volatile char g_k210_rx_buffer[];
|
|
extern volatile uint16_t g_k210_rx_counter;
|
|
extern volatile uint8_t g_k210_rx_flag;
|
|
|
|
// 全局变量:存储解析后的人脸识别结果
|
|
FaceRecognitionResult K210_FaceResult;
|
|
|
|
/* 函数作用:初始化与 K210 通信的 USART3 串口和接收中断。 */
|
|
void K210_USART_Init(void)
|
|
{
|
|
// 根据连接图.md: K210使用USART3 (PB10/PB11)
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
USART_InitTypeDef USART_InitStructure;
|
|
NVIC_InitTypeDef NVIC_InitStructure;
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
|
|
|
|
// PB10: USART3_TX
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
|
|
// PB11: USART3_RX
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
|
|
USART_InitStructure.USART_BaudRate = 115200;
|
|
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
|
USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
|
USART_InitStructure.USART_Parity = USART_Parity_No;
|
|
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
|
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
|
USART_Init(USART3, &USART_InitStructure);
|
|
|
|
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
|
|
USART_Cmd(USART3, ENABLE);
|
|
}
|
|
|
|
/**
|
|
* @brief 解析K210发送的人脸识别数据
|
|
* @param buffer 接收到的数据缓冲区
|
|
* @param result 解析结果存储结构体
|
|
* @retval 1=解析成功, 0=解析失败
|
|
*
|
|
* K210数据格式示例: "ID:2021001,NAME:Zhang_San,CONF:0.95"
|
|
* 或者: "UNKNOWN" (未识别到人脸)
|
|
*/
|
|
/* 函数作用:解析 K210 串口发送的人脸识别字符串并填充结果结构体。 */
|
|
uint8_t K210_ParseFaceData(const char* buffer, FaceRecognitionResult* result)
|
|
{
|
|
DEBUG_PRINT("[K210] Raw data: %s\r\n", buffer);
|
|
|
|
// 清空结果结构体
|
|
memset(result, 0, sizeof(FaceRecognitionResult));
|
|
|
|
// 检查是否为未识别
|
|
if (strstr(buffer, "UNKNOWN") != NULL)
|
|
{
|
|
DEBUG_INFO("[K210] Unknown face detected");
|
|
result->success = 0;
|
|
strcpy(result->student_name, "Unknown");
|
|
return 1;
|
|
}
|
|
|
|
// 尝试解析完整数据
|
|
char id[20] = {0};
|
|
char name[32] = {0};
|
|
float conf = 0.0f;
|
|
|
|
// 解析格式: ID:2021001,NAME:Zhang_San,CONF:0.95
|
|
int parsed = sscanf(buffer, "ID:%19[^,],NAME:%31[^,],CONF:%f", id, name, &conf);
|
|
|
|
if (parsed == 3)
|
|
{
|
|
DEBUG_PRINT("[K210] Parsed: ID=%s, Name=%s, Conf=%.2f\r\n", id, name, conf);
|
|
|
|
result->success = 1;
|
|
strncpy(result->student_id, id, 19);
|
|
result->student_id[19] = '\0';
|
|
|
|
// 将下划线替换为空格(方便显示)
|
|
strncpy(result->student_name, name, 31);
|
|
result->student_name[31] = '\0';
|
|
for (int i = 0; result->student_name[i] != '\0'; i++)
|
|
{
|
|
if (result->student_name[i] == '_')
|
|
result->student_name[i] = ' ';
|
|
}
|
|
|
|
result->confidence = conf;
|
|
return 1;
|
|
}
|
|
|
|
DEBUG_ERROR("[K210] Parse failed!");
|
|
return 0; // 解析失败
|
|
}
|
|
|
|
/* 函数作用:检查 K210 接收完成标志,并在有新数据时解析识别结果。 */
|
|
uint8_t K210_GetRxFlag(void)
|
|
{
|
|
if (g_k210_rx_flag)
|
|
{
|
|
DEBUG_INFO("[K210] Data received, parsing...");
|
|
|
|
// 解析K210发送的人脸识别数据
|
|
K210_ParseFaceData((const char*)g_k210_rx_buffer, &K210_FaceResult);
|
|
|
|
g_k210_rx_flag = 0; // 清除标志位
|
|
g_k210_rx_counter = 0; // 重置计数器
|
|
|
|
// 清空缓冲区,防止数据粘连
|
|
memset((void*)g_k210_rx_buffer, 0, K210_RX_BUFFER_SIZE);
|
|
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* 函数作用:清除 K210 串口接收完成标志和计数器。 */
|
|
void K210_ClearRxFlag(void)
|
|
{
|
|
g_k210_rx_flag = 0;
|
|
g_k210_rx_counter = 0;
|
|
}
|