实现蓝牙初始化,以及串口发送蓝牙接收、蓝牙发送串口接收
This commit is contained in:
@@ -34,7 +34,12 @@ uint8_t device_init(device_t *port)
|
||||
{
|
||||
SYS_LOG_ERR("embedded device choice error");
|
||||
}
|
||||
|
||||
//默认蓝牙 val
|
||||
res = pc_device_choice(port, DATA_PORT_TYPE_BLE_VAL);
|
||||
if(res == DEVICE_PC_ERROR)
|
||||
{
|
||||
SYS_LOG_ERR("pc device choice error");
|
||||
}
|
||||
return DEVICE_OK;
|
||||
}
|
||||
uint8_t uart_init(init_device_t *port)
|
||||
@@ -55,8 +60,42 @@ uint8_t uart_init(init_device_t *port)
|
||||
return DEVICE_OK;
|
||||
}
|
||||
|
||||
static void _sb_manufacturer_encode(uint8_t manufacturer_data[20])
|
||||
{
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
manufacturer_data[i] = i + 1;
|
||||
}
|
||||
}
|
||||
uint8_t ble_init(init_device_t *port)
|
||||
{
|
||||
ble_server_init_t ble_init_param = {
|
||||
.device_name = "Sertorf", // 设备名
|
||||
.manufacturer_data = _sb_manufacturer_encode, // 回调函数,生成 manufacturer[20]
|
||||
.connect_cb = NULL, // 回调函数, NULL 值可用
|
||||
};
|
||||
//初始化参数
|
||||
sb_ble_server_port_init(&ble_init_param);
|
||||
|
||||
port->ble_spp_server_cmd = ble_server_port_bind_cmd(0x2000, NULL);
|
||||
port->ble_spp_server_val = ble_server_port_bind_val(0x400, NULL);
|
||||
|
||||
if(port->ble_spp_server_cmd == NULL || port->ble_spp_server_val == NULL)
|
||||
{
|
||||
SYS_LOG_WRN("ble init error");
|
||||
return 1;
|
||||
}
|
||||
if(sb_data_port_start(port->ble_spp_server_cmd))
|
||||
{
|
||||
SYS_LOG_WRN("ble cmd start error");
|
||||
return 1;
|
||||
}
|
||||
if(sb_data_port_start(port->ble_spp_server_val))
|
||||
{
|
||||
SYS_LOG_WRN("ble val start error");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return DEVICE_OK;
|
||||
}
|
||||
|
||||
@@ -114,4 +153,55 @@ int embedded_device_write(device_t *port, void *buffer, uint32_t length)
|
||||
uint32_t embedded_device_get_rx_length(device_t *port)
|
||||
{
|
||||
return sb_data_port_get_rx_length(port->embedded_device);
|
||||
}
|
||||
|
||||
uint8_t pc_device_choice(device_t *port, uint8_t type)
|
||||
{
|
||||
//端口与embedded一致
|
||||
if(port->embedded_device_type == type)
|
||||
{
|
||||
SYS_LOG_WRN("embedded Port consistency");
|
||||
return DEVICE_WRN_PC_TYPE;
|
||||
}
|
||||
|
||||
port->pc_device_type = type;
|
||||
switch (port->pc_device_type)
|
||||
{
|
||||
case DATA_PORT_TYPE_UART:
|
||||
port->pc_device = port->init_device.uart_port;
|
||||
break;
|
||||
case DATA_PORT_TYPE_WIFI:
|
||||
port->pc_device = port->init_device.wifi;
|
||||
break;
|
||||
case DATA_PORT_TYPE_BLE_CMD:
|
||||
port->pc_device = port->init_device.ble_spp_server_cmd;
|
||||
break;
|
||||
case DATA_PORT_TYPE_BLE_VAL:
|
||||
port->pc_device = port->init_device.ble_spp_server_val;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if(port->pc_device == NULL)
|
||||
{
|
||||
SYS_LOG_WRN("embedded device choice error");
|
||||
return DEVICE_PC_ERROR;
|
||||
}
|
||||
|
||||
return DEVICE_OK;
|
||||
}
|
||||
|
||||
int pc_device_read(device_t *port, void *buffer, uint32_t length)
|
||||
{
|
||||
return sb_data_port_read(port->pc_device, buffer, length, 0);
|
||||
}
|
||||
|
||||
int pc_device_write(device_t *port, void *buffer, uint32_t length)
|
||||
{
|
||||
return sb_data_port_write(port->pc_device, buffer, length, 0);
|
||||
}
|
||||
uint32_t pc_device_get_rx_length(device_t *port)
|
||||
{
|
||||
return sb_data_port_get_rx_length(port->pc_device);
|
||||
}
|
||||
@@ -9,7 +9,7 @@
|
||||
#include "sys_log.h"
|
||||
|
||||
#define UART_ENABLE 1
|
||||
#define BLE_ENABLE 0
|
||||
#define BLE_ENABLE 1
|
||||
#define WIFI_ENABLE 0
|
||||
|
||||
enum DEVICE_ERROR
|
||||
@@ -128,4 +128,33 @@ int embedded_device_write(device_t *port, void *buffer, uint32_t length);
|
||||
* @param size 写入数据长度
|
||||
* @retval 缓存中可读取的字节数
|
||||
*/
|
||||
uint32_t embedded_device_get_rx_length(device_t *port);
|
||||
uint32_t embedded_device_get_rx_length(device_t *port);
|
||||
/**
|
||||
* @brief PC OR 手机数据接口读操作
|
||||
*
|
||||
* @param port 由对应的驱动提供的绑定接口获得的句柄
|
||||
* @param buffer 读取数据缓存
|
||||
* @param length 读取数据长度
|
||||
* @retval < 0 操作失败或在指定的时间内未满足指定的操作长度
|
||||
* @retval >= 0 实际已缓存的长度
|
||||
*/
|
||||
int pc_device_read(device_t *port, void *buffer, uint32_t length);
|
||||
/**
|
||||
* @brief PC OR 手机数据接口写操作
|
||||
*
|
||||
* @param port 由对应的驱动提供的绑定接口获得的句柄
|
||||
* @param data 写入数据
|
||||
* @param size 写入数据长度
|
||||
* @retval < 0 操作失败或在指定的时间内未满足指定的操作长度
|
||||
* @retval >= 0 实际已缓存的长度
|
||||
*/
|
||||
int pc_device_write(device_t *port, void *buffer, uint32_t length);
|
||||
/**
|
||||
* @brief PC OR 手机数据接口写操作
|
||||
*
|
||||
* @param port 由对应的驱动提供的绑定接口获得的句柄
|
||||
* @param data 写入数据
|
||||
* @param size 写入数据长度
|
||||
* @retval 缓存中可读取的字节数
|
||||
*/
|
||||
uint32_t pc_device_get_rx_length(device_t *port);
|
||||
@@ -33,6 +33,14 @@ void embedded_thread(void* arg)
|
||||
uint8_t data[embedded_size];
|
||||
embedded_device_read(&sertrf.device, data, embedded_size);
|
||||
SYS_LOG_INF("data : %s", data);
|
||||
pc_device_write(&sertrf.device, data, embedded_size);
|
||||
}
|
||||
embedded_size = pc_device_get_rx_length(&sertrf.device);
|
||||
if(embedded_size > 0)
|
||||
{
|
||||
uint8_t data[embedded_size];
|
||||
pc_device_read(&sertrf.device, data, embedded_size);
|
||||
SYS_LOG_INF("data : %s", data);
|
||||
embedded_device_write(&sertrf.device, data, embedded_size);
|
||||
}
|
||||
//需要添加一些延时,否则会卡死,导致看门狗复位
|
||||
|
||||
Reference in New Issue
Block a user