Car/Peripheral/Src/bluetooth.c

88 lines
2.4 KiB
C

#include "bluetooth.h"
#include "usart.h"
#include "control.h"
#include <stdio.h>
#include <string.h>
#include "syscalls.h"
#define BUFFER_SIZE 100
#define MESSAGE_SIZE 19
#define CMD_NUM 9
static int8_t rxBuffer[BUFFER_SIZE], txBuffer[10000];
static uint16_t txLen;
static uint8_t cmdIndex;
void BLUETOOTH_Init(void)
{
HAL_UARTEx_ReceiveToIdle_DMA(&huart2, rxBuffer, 100);
// HAL_UART_Receive_IT(&huart2, txBuffer + txLen, 1);
}
// void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
// {
// if (huart == &huart2)
// {
// ++txLen;
// if (txLen == 10000)
// txLen = 0;
// }
// }
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
if (huart == &huart2 && rxBuffer[0] == '$' && rxBuffer[Size - 1] == '#')
{
rxBuffer[Size] = '\0';
// 通用协议
if (mode == MODE_REMOTE && rxBuffer[2] == ',')
{
cmdIndex = 0;
for (uint8_t i = 1; i < MESSAGE_SIZE; i += 2)
if (rxBuffer[i] == '0')
++cmdIndex;
else
break;
if (cmdIndex == CMD_NUM)
cmdIndex = 0;
CONTROL_CommonCmd(cmdIndex, rxBuffer[cmdIndex * 2 + 1] - '0');
}
else
{
static uint16_t num[6];
// 模式选择
if (rxBuffer[5] == 'M')
{
sscanf(rxBuffer + 5, "%*4s%c%c", num, num + 1);
CONTROL_Mode(num[0] - '0', num[1] - '0');
}
else if (mode == MODE_REMOTE)
switch (rxBuffer[5])
{
// 机械手
case 'S':
break;
// 舵机云台
case 'P':
break;
// 七彩灯
case 'C':
sscanf(rxBuffer + 5, "%*3s%hu,%*3s%hu,%*3s%hu", num, num + 1, num + 2);
CONTROL_RGB(num[0], num[1], num[2]);
break;
}
}
// my_printf(&huart2, rxBuffer);
// strcpy(txBuffer + txLen, rxBuffer);
// if (txLen + Size >= 9999)
// txLen = 0;
// else
// txLen += Size;
// txBuffer[txLen] = '\0';
// if (strcmp(rxBuffer, "$send#") == 0)
// my_printf(&huart2, txBuffer);
BLUETOOTH_Init();
}
}