软件界面

STM32蓝牙串口通信代码
以下是为STM32血压计设计的蓝牙串口通信代码,包含接收和发送功能,并带有详细注释。
1. 头文件定义 (ble_comm.h)
#ifndef __BLE_COMM_H
#define __BLE_COMM_H
#include "main.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
// 血压数据结构体
typedef struct {
uint16_t systolic; // 收缩压 (高压)
uint16_t diastolic; // 舒张压 (低压)
uint16_t pulse; // 脉搏频率
uint8_t battery_level; // 电池电量 (0-100%)
uint32_t timestamp; // 时间戳
} BloodPressureData_t;
// 测量状态枚举
typedef enum {
MEASURE_IDLE, // 空闲状态
MEASURE_STARTING, // 测量开始
MEASURE_INFLATING, // 充气中
MEASURE_DEFLATING, // 放气中
MEASURE_ANALYZING, // 数据分析中
MEASURE_COMPLETE // 测量完成
} MeasureState_t;
// 系统配置结构体
typedef struct {
uint8_t auto_power_off; // 自动关机时间 (分钟)
uint8_t sound_enable; // 声音开关
uint8_t backlight_level; // 背光等级
uint8_t data_format; // 数据格式: 0=文本, 1=JSON
} SystemConfig_t;
// 函数声明
void BLE_Init(UART_HandleTypeDef *huart);
void BLE_Process(void);
void BLE_SendData(const char *data);
void BLE_SendBloodPressure(BloodPressureData_t *data);
void BLE_SendStatus(void);
void BLE_ParseCommand(const char *cmd);
void BLE_StartMeasurement(void);
void BLE_StopMeasurement(void);
uint8_t BLE_IsConnected(void);
// 外部变量声明
extern BloodPressureData_t bp_data;
extern SystemConfig_t sys_config;
extern MeasureState_t measure_state;
#endif /* __BLE_COMM_H */
2. 主通信文件 (ble_comm.c)
#include "ble_comm.h"
#include "cmsis_os.h" // 如果使用RTOS
#include <ctype.h>
// 全局变量定义
BloodPressureData_t bp_data = {
0};
SystemConfig_t sys_config = {
.auto_power_off = 5, // 默认5分钟后自动关机
.sound_enable = 1, // 声音开启
.backlight_level = 3, // 背光等级3
.data_format = 0 // 默认文本格式
};
MeasureState_t measure_state = MEASURE_IDLE;
UART_HandleTypeDef *ble_huart = NULL;
// 接收缓冲区
#define RX_BUFFER_SIZE 256
static uint8_t rx_buffer[RX_BUFFER_SIZE];
static uint16_t rx_index = 0;
static uint8_t ble_connected = 0;
static uint32_t last_activity_time = 0;
// 命令定义
#define MAX_CMD_LENGTH 32
typedef struct {
const char *cmd;
void (*handler)(const char *params);
} Command_t;
// 命令处理函数声明
static void handle_start(const char *params);
static void handle_stop(const char *params);
static void handle_get_status(const char *params);
static void handle_get_battery(const char *params);
static void handle_get_version(const char *params);
static void handle_calibrate(const char *params);
static void handle_set_config(const char *params);
static void handle_get_config(const char *params);
static void handle_unknown(const char *params);
// 命令表
static const Command_t command_table[] = {
{
"START", handle_start},
{
"STOP", handle_stop},
{
"GET_STATUS", handle_get_status},
{
"GET_BATTERY", handle_get_battery},
{
"GET_VERSION", handle_get_version},
{
"CALIBRATE", handle_calibrate},
{
"SET_CONFIG", handle_set_config},


1334

被折叠的 条评论
为什么被折叠?



