当前位置:网站首页>nrf52832--官方例程ble_app_uart添加led特性,实现电脑uart和手机app控制开发板led开和关
nrf52832--官方例程ble_app_uart添加led特性,实现电脑uart和手机app控制开发板led开和关
2022-06-12 05:44:00 【m0_7788】
硬件:nrf52832开发板
软件:编译环境:keil, SDK版本:nRF5_SDK_17.1.0_ddde560, 协议栈版本:s132_nrf52_7.2.0_softdevice
实现功能:在官方串口例程ble_app_uart的基础上添加一个特性,用于控制led。
1.通过nRF Connect官方APP的RX Characteristic写1,控制led2亮,写0控制led灭
2.win电脑串口助手自定义通讯协议,TX发16进制aa01控制led2亮,16进制aa00控制led灭
在nodic官方ble_app_uart中无需添加其他驱动组件库,按以下位置添加即可实现以上功能(粗体字为新增部分)。
#define BLE_UART_LED
services_init注册NUS,nus_init.data_handler = nus_data_handler;
static void nus_data_handler(ble_nus_evt_t * p_evt)
{
if (p_evt->type == BLE_NUS_EVT_RX_DATA)
{
uint32_t err_code;
NRF_LOG_DEBUG("nus_data_handler:Received data from BLE NUS. Writing data on UART.");
NRF_LOG_HEXDUMP_DEBUG(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);
****#ifdef BLE_UART_LED
if (p_evt->params.rx_data.p_data[0] == '1') //自定义通信协议开灯
{
bsp_board_led_on(LED_2);
NRF_LOG_INFO("led2 is open");
}
else if(p_evt->params.rx_data.p_data[0] == '0') //自定义通信协议关灯
{
bsp_board_led_off(LED_2);
NRF_LOG_INFO("led2 is close");
}
#endif //BLE_UART_LED****
for (uint32_t i = 0; i < p_evt->params.rx_data.length; i++)
{
do
{
err_code = app_uart_put(p_evt->params.rx_data.p_data[i]);
if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY))
{
NRF_LOG_ERROR("Failed receiving NUS message. Error 0x%x. ", err_code);
APP_ERROR_CHECK(err_code);
}
} while (err_code == NRF_ERROR_BUSY);
}
if (p_evt->params.rx_data.p_data[p_evt->params.rx_data.length - 1] == '\r')
{
while (app_uart_put('\n') == NRF_ERROR_BUSY);
}
}
}
uart_init的APP_UART_FIFO_INIT调用uart_event_handle,
void uart_event_handle(app_uart_evt_t * p_event)
{
static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
static uint8_t index = 0;
uint32_t err_code;
switch (p_event->evt_type)
{
case APP_UART_DATA_READY:
//data_array[index]取得电脑串口发过来的数据
UNUSED_VARIABLE(app_uart_get(&data_array[index]));
index++;
if ((data_array[index - 1] == '\n') ||
(data_array[index - 1] == '\r') ||
(index >= m_ble_nus_max_data_len))
{
if (index > 1)
{
NRF_LOG_DEBUG("uart_event_handle:Ready to send data over BLE NUS");
NRF_LOG_HEXDUMP_DEBUG(data_array, index);
****#ifdef BLE_UART_LED
if (data_array[0] == 0xaa && data_array[1] == 0x01) //自定义通信协议开灯
{
pte_led_on(LED_3);
}
else if(data_array[0] == 0xaa && data_array[1] == 0x00) //自定义通信协议关灯
{
pte_led_off(LED_3);
}
#endif //BLE_UART_LED****
do
{
uint16_t length = (uint16_t)index;
err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
if ((err_code != NRF_ERROR_INVALID_STATE) &&
(err_code != NRF_ERROR_RESOURCES) &&
(err_code != NRF_ERROR_NOT_FOUND))
{
APP_ERROR_CHECK(err_code);
}
} while (err_code == NRF_ERROR_RESOURCES);
}
index = 0; //发送了数据后清零数组下标。以继续缓存后续的串口数据。
}
break;
case APP_UART_COMMUNICATION_ERROR:
APP_ERROR_HANDLER(p_event->data.error_communication);
break;
case APP_UART_FIFO_ERROR:
APP_ERROR_HANDLER(p_event->data.error_code);
break;
default:
break;
}
}
边栏推荐
猜你喜欢

Is the individual industrial and commercial door a legal person enterprise

Redis memory obsolescence strategy

Conversion of Halcon 3D depth map to 3D image

Identification of campus green plants based on tensorflow

Halcon 3D 1 Reading 3D data

Nature | 给全球的新冠伤亡算一笔账

Codis 3. X expansion and contraction

Win10 desktop unlimited refresh

Redis transaction

TCP and UDP introduction
随机推荐
Automated testing - Po mode / log /allure/ continuous integration
Greenplum [question 05] Greenplum streaming server custom client problem handling (increasing)
【js小知识】轻松了解js防抖与节流
Filter的注解配置
Performance test - GTI application service performance monitoring platform
Go interface implementation principle [advanced level]
arp timer and arptables
登录验证过滤器
项目开发流程简单介绍
Test work summary - performance test related issues
Chapter 7 - pointer learning
What is the project advance payment
What is the lszrz protocol used at ordinary times? Talk about xmodem/ymodem/zmodem
Golang idea configures the agent to improve the speed of packages downloaded by go get
国企为什么要上市
How does WiFi 802.11 correspond to 802.3
Halcon uses points to fit a plane
The application could not be installed: INSTALL_FAILED_TEST_ONLY
Introduction to thread pool: ThreadPoolExecutor
merge sort