Bluetoe 开源项目使用教程
1. 项目的目录结构及介绍
Bluetoe 项目的目录结构如下:
bluetoe/
├── examples/
│ ├── blinky_server/
│ ├── ...
├── include/
│ ├── bluetoe/
│ │ ├── server.hpp
│ │ ├── device.hpp
│ │ ├── ...
├── src/
│ ├── link_layer/
│ ├── ...
├── tests/
│ ├── test_cases/
│ ├── ...
├── CMakeLists.txt
├── README.md
├── LICENSE
├── ...
目录结构介绍
- examples/: 包含多个示例项目,展示了如何使用 Bluetoe 框架构建蓝牙 LE 服务器。
- include/bluetoe/: 包含 Bluetoe 框架的核心头文件,定义了服务器、设备、特性等关键组件。
- src/: 包含 Bluetoe 框架的源代码,特别是链接层(link_layer)的实现。
- tests/: 包含测试用例,用于验证 Bluetoe 框架的正确性和稳定性。
- CMakeLists.txt: CMake 构建文件,用于配置和构建项目。
- README.md: 项目的基本介绍和使用说明。
- LICENSE: 项目的许可证文件,采用 MIT 许可证。
2. 项目的启动文件介绍
Bluetoe 项目的启动文件通常位于 examples/
目录下。以 blinky_server
为例,启动文件为 main.cpp
。
启动文件 main.cpp
介绍
#include <bluetoe/server.hpp>
#include <bluetoe/device.hpp>
#include <nrf.h>
using namespace bluetoe;
// LED1 on a nRF52 eval board
static constexpr int io_pin = 17;
static std::uint8_t io_pin_write_handler(bool state) {
// On an nRF52 eval board the pin is connected to the LED's cathode
// This inverts the logic
NRF_GPIO->OUT = state ? NRF_GPIO->OUT & ~(1 << io_pin) : NRF_GPIO->OUT | (1 << io_pin);
}
using blinky_server = server<
service<
service_uuid<0xC11169E1, 0x6252, 0x4450, 0x931C, 0x1B43A318783B>,
characteristic<
requires_encryption,
free_write_handler<bool, io_pin_write_handler>
>
>
>;
blinky_server gatt_device;
int main() {
// Init GPIO pin
NRF_GPIO->PIN_CNF[io_pin] = (GPIO_PIN_CNF_DRIVE_S0H1 << GPIO_PIN_CNF_DRIVE_Pos) |
(GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
gatt_device.run();
}
启动文件功能介绍
main.cpp
: 这是项目的入口文件,包含了 Bluetoe 服务器的定义和初始化代码。blinky_server
: 定义了一个蓝牙 LE 服务器,包含一个服务和一个特性,允许客户端控制一个 IO 引脚。io_pin_write_handler
: 处理客户端写入操作的回调函数,用于控制 IO 引脚的状态。main()
: 初始化 GPIO 引脚,并启动 Bluetoe 服务器。
3. 项目的配置文件介绍
Bluetoe 项目的配置文件主要是 CMakeLists.txt
,用于配置项目的构建过程。
配置文件 CMakeLists.txt
介绍
cmake_minimum_required(VERSION 3.10)
project(bluetoe)
set(CMAKE_CXX_STANDARD 11)
include_directories(include)
add_subdirectory(src)
add_subdirectory(tests)
add_subdirectory(examples)
# 其他配置项...
配置文件功能介绍
cmake_minimum_required(VERSION 3.10)
: 指定 CMake 的最低版本要求。project(bluetoe)
: 定义项目名称。set(CMAKE_CXX_STANDARD 11)
: 设置 C++ 标准为 C++11。include_directories(include)
: 包含头文件目录。add_subdirectory(src)
: 添加源代码目录。add_subdirectory(tests)
: 添加测试代码目录。add_subdirectory(examples)
: 添加示例代码目录。
通过这些配置,可以方便地构建和测试 Bluetoe 项目。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考