/*******************************************************************************
|
* File Name : BT.c
|
* Description :
|
* Created on : 2018Äê7ÔÂ23ÈÕ
|
* Author : ¶Å¼ü
|
*******************************************************************************/
|
|
/*******************************************************************************
|
* Include Files *
|
*******************************************************************************/
|
#include "stdio.h"
|
#include "stdarg.h"
|
#include "string.h"
|
#include "AppConfig.h"
|
#include "HIDO_VLQueue.h"
|
#include "HIDO_Input.h"
|
#include "HIDO_Timer.h"
|
#include "HIDO_Util.h"
|
#include "bluetooth.h"
|
#include "DBG.h"
|
#include "mainex.h"
|
#include "Uart.h"
|
|
/*******************************************************************************
|
* Macro *
|
*******************************************************************************/
|
// È«¾Ö״̬±êÖ¾
|
|
|
static HIDO_UINT8 l_au8BTUartRxBuf[BT_UART_RX_BUF_SIZE];
|
static HIDO_UINT8 l_au8BTUartTxBuf[BT_UART_TX_BUF_SIZE];
|
HIDO_UINT8 uart6_dma_rxbuf[UART6_DMA_RX_BUF_SIZE] = {0};
|
HIDO_UINT8 uart6_dma_recv_end_flag = 0;
|
HIDO_UINT8 uart6_dma_recv_len = 0;
|
|
/*******************************************************************************
|
* Local Variable *
|
*******************************************************************************/
|
|
|
/*******************************************************************************
|
* Function Name : DBG_Init
|
* Description : µ÷ÊÔ´òÓ¡³õʼ»¯
|
* Input : None
|
* Output : None
|
* Return : None
|
* Author : ¶Å¼ü
|
* Modified Date: : 2018Äê7ÔÂ23ÈÕ
|
*******************************************************************************/
|
HIDO_VOID BT_Init(void)
|
{
|
ST_UartInit stInit;
|
|
memset(&stInit, 0, sizeof(stInit));
|
stInit.m_eRxMode = UART_RX_MODE_DMA;
|
stInit.m_eTxMode = UART_TX_MODE_DMA;
|
stInit.m_pu8RxBuf = l_au8BTUartRxBuf;
|
stInit.m_u32RxBufSize = BT_UART_RX_BUF_SIZE;
|
stInit.m_pu8TxBuf = l_au8BTUartTxBuf;
|
stInit.m_u32TxBufSize = BT_UART_TX_BUF_SIZE;
|
stInit.m_u32TxQueueMemberCnt = BT_UART_TX_QUEUE_MEMBER_CNT;
|
Uart_Init(UART_ID_BT, &stInit);
|
|
|
}
|
|
/**
|
* @brief Æô¶¯ UART6 ½ÓÊÕ£¨ÆôÓà IDLE ÖÐ¶Ï + DMA£©
|
*/
|
void UART6_StartReceive(void)
|
{
|
// Çå³ý±ê־룬·ÀÖ¹ÉϵçÎó´¥·¢
|
__HAL_UART_CLEAR_IDLEFLAG(&huart6);
|
|
// ʹÄÜ IDLE ÖжÏ
|
__HAL_UART_ENABLE_IT(&huart6, UART_IT_IDLE);
|
|
// Æô¶¯ DMA ½ÓÊÕ£¨Ñ»·Ä£Ê½£©
|
HAL_UART_Receive_DMA(&huart6, uart6_dma_rxbuf, UART6_DMA_RX_BUF_SIZE);
|
}
|
|
/**
|
* @brief ·¢ËÍ×Ö·û´®
|
*/
|
void UART6_SendString(const char *str)
|
{
|
HAL_UART_Transmit_DMA(&huart6, (uint8_t *)str, strlen(str));
|
}
|
|
/**
|
* @brief ·¢ËÍÊý¾ÝÊý×é
|
*/
|
void UART6_SendArray(uint8_t *data, uint16_t len)
|
{
|
HAL_UART_Transmit_DMA(&huart6, data, len);
|
}
|
|
void Joystick_Process(Joystick_t *joy)
|
{
|
// ÌáÈ¡ y1 ¿ØÖÆÇ°½ø/ºóÍË
|
int16_t motor_val = joy->y1; // -100 ~ +100
|
Set_Motor_PWM(motor_val);
|
|
// ÌáÈ¡ x2 ¿ØÖÆ×ªÏò
|
int16_t steering_val = joy->x2; // -100 ~ +100
|
Set_Steering_PWM(steering_val);
|
|
}
|
|
// ʾÀýÊäÈë: "[joystick,-22,-2,0,0]"
|
void Parse_Joystick_Data(char *data)
|
{
|
Joystick_t joy = {0};
|
|
if (strstr(data, "joystick") == NULL) return;
|
|
// Ìø¹ý "[joystick,"
|
char *ptr = strstr(data, "joystick");
|
|
if (!ptr) return;
|
|
ptr += 10; // Ìø¹ý "joystick,"
|
|
// ½âÎöËĸöÕûÊý
|
char *end;
|
joy.x1 = strtol(ptr, &end, 10);
|
|
if (end == ptr) return;
|
|
ptr = end + 1;
|
|
joy.y1 = strtol(ptr, &end, 10);
|
|
if (end == ptr) return;
|
|
ptr = end + 1;
|
|
joy.x2 = strtol(ptr, &end, 10);
|
|
if (end == ptr) return;
|
|
ptr = end + 1;
|
|
joy.y2 = strtol(ptr, &end, 10);
|
|
// ´¦ÀíÊý¾Ý
|
Joystick_Process(&joy);
|
}
|
|
|
/*******************************************************************************
|
* Function Name : BT_Init
|
* Description : µ÷ÊÔ´òÓ¡ÂÖѯ
|
* Input : None
|
* Output : None
|
* Return : None
|
* Author : ¶Å¼ü
|
* Modified Date: : 2018Äê7ÔÂ23ÈÕ
|
*******************************************************************************/
|
HIDO_VOID BT_Poll(void)
|
{
|
static HIDO_UINT8 l_uart6_dma_rxbuf[100];
|
if (uart6_dma_recv_len > 0) //HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_13) == GPIO_PIN_RESET
|
{
|
if (uart6_dma_recv_end_flag == 1) //½ÓÊÕÍê³É±êÖ¾
|
{
|
HIDO_UtilSnprintf((HIDO_CHAR *)l_uart6_dma_rxbuf, sizeof(l_uart6_dma_rxbuf), "buff=%s\r\n", uart6_dma_rxbuf);
|
Uart_Send(UART_ID_DBG, (HIDO_UINT8 *)l_uart6_dma_rxbuf, strlen(l_uart6_dma_rxbuf));
|
// ½âÎöÊý¾Ý
|
Parse_Joystick_Data(uart6_dma_rxbuf);
|
}
|
|
uart6_dma_recv_len = 0;//Çå³ý¼ÆÊý
|
uart6_dma_recv_end_flag = 0;//Çå³ý½ÓÊÕ½áÊø±ê־λ
|
memset(uart6_dma_rxbuf, 0, UART6_DMA_RX_BUF_SIZE);
|
__HAL_UART_CLEAR_IDLEFLAG(&huart6);
|
HAL_UART_Receive_DMA(&huart6, uart6_dma_rxbuf, UART6_DMA_RX_BUF_SIZE); //ÖØÐ´ò¿ªDMA½ÓÊÕ
|
}
|
|
}
|