168 lines
3.5 KiB
C
168 lines
3.5 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);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|