201 lines
4.4 KiB
C
201 lines
4.4 KiB
C
/*
|
||
* @Author: DaMingSY zxm5337@163.com
|
||
* @Date: 2024-09-02 08:54:40
|
||
* @LastEditors: DaMingSY zxm5337@163.com
|
||
* @LastEditTime: 2024-09-09 11:11:11
|
||
* @FilePath: \signal_generator\App\BLE\ble.c
|
||
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
|
||
*/
|
||
|
||
#include "ble.h"
|
||
|
||
void ble_send(UART_HandleTypeDef *huart, uint8_t *data)
|
||
{
|
||
usart_printf(huart, (char *)data);
|
||
}
|
||
|
||
void transparent_bluetooth(st_scom *scom)
|
||
{
|
||
if (scom != &scom6_ble) return;
|
||
|
||
//来自蓝牙设备的数据是否接收完成
|
||
if (scom->rx_flag == TRUE)
|
||
{
|
||
scom->rx_flag = FALSE;
|
||
|
||
//将接收到的数据存入485的tx,准备发送至上位机
|
||
memcpy(scom2_rs485.tx_buff, scom->rx_buff, sizeof(scom->rx_buff));
|
||
scom2_rs485.tx_len = scom->rx_len;
|
||
scom2_rs485.tx_flag = TRUE;
|
||
|
||
//清空缓存区,等待新的数据
|
||
memset(scom->rx_buff, 0, sizeof(scom->rx_buff));
|
||
scom->rx_len = 0;
|
||
}
|
||
|
||
//来自上位机的数据是否准备完毕
|
||
if (scom->tx_flag == TRUE)
|
||
{
|
||
scom->tx_flag = FALSE;
|
||
|
||
//将tx中的数据发送至蓝牙设备
|
||
ble_send(&huart6, scom6_ble.tx_buff);
|
||
|
||
//清空缓存区,等待新的数据
|
||
memset(scom->tx_buff, 0, sizeof(scom->tx_buff));
|
||
scom->tx_len = 0;
|
||
}
|
||
}
|
||
|
||
int8_t write_ble_id(char *id)
|
||
{
|
||
if(!id) return -1;
|
||
|
||
char str_id[50] = {0};
|
||
|
||
sprintf(str_id,"AT+NAME=%s\r\n",id);
|
||
usart_printf(&huart6, str_id);
|
||
return 1;
|
||
}
|
||
|
||
void write_ble_cmd(char *cmd)
|
||
{
|
||
if(!cmd) return;
|
||
|
||
usart_printf(&huart6, cmd);
|
||
}
|
||
|
||
uint8_t ble_init_step = 0;
|
||
uint8_t ble_cnt = 0;
|
||
uint8_t ble_init(void)
|
||
{
|
||
uint8_t result = 0;
|
||
|
||
switch (ble_init_step)
|
||
{
|
||
case 0:
|
||
{
|
||
if(ble_cnt == 0)
|
||
{
|
||
char ble_id[9];
|
||
strcpy (ble_id, "00000001");
|
||
|
||
write_ble_id(ble_id); //修改蓝牙名称
|
||
ble_cnt++;
|
||
}
|
||
else
|
||
{
|
||
ble_cnt++;
|
||
if( (ble_cnt * BLE_TASK_PERIOD) > 500)
|
||
{
|
||
ble_cnt = 0;
|
||
ble_init_step++;
|
||
}
|
||
}
|
||
}
|
||
break;
|
||
|
||
case 1:
|
||
{
|
||
if(ble_cnt == 0)
|
||
{
|
||
write_ble_cmd("AT+UUIDS=FFF0\r\n"); //主服务通道
|
||
|
||
ble_cnt++;
|
||
}
|
||
else
|
||
{
|
||
ble_cnt++;
|
||
if( (ble_cnt * BLE_TASK_PERIOD) > 500)
|
||
{
|
||
ble_cnt = 0;
|
||
ble_init_step++;
|
||
}
|
||
}
|
||
}
|
||
break;
|
||
|
||
case 2:
|
||
{
|
||
if(ble_cnt == 0)
|
||
{
|
||
write_ble_cmd("AT+UUIDN=FFF1\r\n"); //读通道FFF1
|
||
|
||
ble_cnt++;
|
||
}
|
||
else
|
||
{
|
||
ble_cnt++;
|
||
if( (ble_cnt * BLE_TASK_PERIOD) > 500)
|
||
{
|
||
ble_cnt = 0;
|
||
ble_init_step++;
|
||
}
|
||
}
|
||
}
|
||
break;
|
||
|
||
case 3:
|
||
{
|
||
if(ble_cnt == 0)
|
||
{
|
||
write_ble_cmd("AT+UUIDW=FFF2\r\n"); //写通道FFF2
|
||
|
||
ble_cnt++;
|
||
}
|
||
else
|
||
{
|
||
ble_cnt++;
|
||
if( (ble_cnt * BLE_TASK_PERIOD) > 500)
|
||
{
|
||
ble_cnt = 0;
|
||
ble_init_step++;
|
||
}
|
||
}
|
||
}
|
||
break;
|
||
|
||
case 4:
|
||
{
|
||
if(ble_cnt == 0)
|
||
{
|
||
write_ble_cmd("AT+REBOOT=1\r\n"); //重启
|
||
|
||
ble_cnt++;
|
||
}
|
||
else
|
||
{
|
||
ble_cnt++;
|
||
if( (ble_cnt * BLE_TASK_PERIOD) > 1000)
|
||
{
|
||
ble_cnt = 0;
|
||
ble_init_step++;
|
||
}
|
||
}
|
||
}
|
||
break;
|
||
|
||
case 5:
|
||
{
|
||
//清空缓存区(蓝牙配置时收获的响应)
|
||
memset(scom6_ble.rx_buff, 0, sizeof(scom6_ble.rx_buff));
|
||
scom6_ble.rx_len = 0;
|
||
|
||
ble_init_step++;
|
||
}
|
||
break;
|
||
|
||
case 6:
|
||
{
|
||
result = 1;
|
||
}
|
||
break;
|
||
|
||
default:
|
||
break;
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|