c Copy code
#include <stdbool.h>
#include <stdint.h>
#include "nrf_gpio.h"
#include "nrf_delay.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "nrf_pwr_mgmt.h"
#include "nrf_dfu_ble_svc.h"
#include "nrf_sdh.h"
#include "nrf_sdh_ble.h"
#include "app_button.h"
#include "app_timer.h"
#include "ble_advdata.h"
#include "ble_advertising.h"
#include "ble_conn_params.h"
#include "ble_db_discovery.h"
#include "ble_nus.h"
// 定义按键和LED引脚
#define BUTTON_PIN 17
#define LED_PIN 18
// 定义模式
typedef enum {
MODE_BLE_COMM,
MODE_DFU
} app_mode_t;
static app_mode_t current_mode = MODE_BLE_COMM;
// 蓝牙服务和特征值
BLE_NUS_DEF(m_nus, NRF_SDH_BLE_TOTAL_LINK_COUNT);
// 按键处理函数
static void button_event_handler(uint8_t pin_no, uint8_t button_action) {
if (pin_no == BUTTON_PIN && button_action == APP_BUTTON_PUSH) {
// 模拟长按检测
nrf_delay_ms(2000); // 等待2秒
if (nrf_gpio_pin_read(BUTTON_PIN) == APP_BUTTON_PUSH) {
// 切换模式
if (current_mode == MODE_BLE_COMM) {
current_mode = MODE_DFU;
NRF_LOG_INFO("Switching to DFU mode");
// 启动DFU流程
nrf_dfu_ble_svc_init();
} else {
current_mode = MODE_BLE_COMM;
NRF_LOG_INFO("Switching to BLE communication mode");
// 停止DFU流程
nrf_dfu_ble_svc_cleanup();
}
}
}
}
// 初始化按键
static void buttons_init(void) {
app_button_cfg_t button_cfg = {
.pin_no = BUTTON_PIN,
.active_state = APP_BUTTON_ACTIVE_LOW,
.pull_cfg = NRF_GPIO_PIN_PULLUP,
.button_handler = button_event_handler
};
app_button_init(&button_cfg, 1, APP_TIMER_TICKS(50));
app_button_enable();
}
// 初始化LED
static void leds_init(void) {
nrf_gpio_cfg_output(LED_PIN);
}
// 蓝牙事件处理
static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context) {
switch (p_ble_evt->header.evt_id) {
case BLE_GAP_EVT_CONNECTED:
NRF_LOG_INFO("Connected");
break;
case BLE_GAP_EVT_DISCONNECTED:
NRF_LOG_INFO("Disconnected");
break;
case BLE_GATTS_EVT_WRITE:
// 处理写事件
break;
default:
// 其他事件
break;
}
}
// 初始化蓝牙
static void ble_init(void) {
ret_code_t err_code;
// 初始化蓝牙堆栈
err_code = nrf_sdh_enable_request();
APP_ERROR_CHECK(err_code);
// 配置蓝牙堆栈
uint32_t ram_start = 0;
err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
APP_ERROR_CHECK(err_code);
// 启用蓝牙堆栈
err_code = nrf_sdh_ble_enable(&ram_start);
APP_ERROR_CHECK(err_code);
// 设置蓝牙事件处理程序
NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
// 初始化NUS服务
ble_nus_init_t nus_init = {0};
nus_init.data_handler = nus_data_handler;
err_code = ble_nus_init(&m_nus, &nus_init);
APP_ERROR_CHECK(err_code);
}
// NUS数据处理函数
static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length) {
// 处理接收到的数据
NRF_LOG_INFO("Received data: %.*s", length, p_data);
}
// 主函数
int main(void) {
// 初始化日志
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
NRF_LOG_DEFAULT_BACKENDS_INIT();
// 初始化电源管理
APP_ERROR_CHECK(nrf_pwr_mgmt_init());
// 初始化按键和LED
buttons_init();
leds_init();
// 初始化蓝牙
ble_init();
// 主循环
while (1) {
if (current_mode == MODE_BLE_COMM) {
// 处理蓝牙通信
// 例如:接收和发送数据
} else if (current_mode == MODE_DFU) {
// 处理DFU流程
}
// 处理日志和电源管理
NRF_LOG_PROCESS();
nrf_pwr_mgmt_run();
}
}