/****************************************************************************** 1
2
********************
3
Filename: heartrate.c
4
Revised: $Date $
5
Revision: $Revision $
6
7
Description: This file contains the heart rate sample application
8
for use with the CC2540 Bluetooth Low Energy Protocol Stack.
9
10
Copyright 2011 Texas Instruments Incorporated. All rights reserved.
11
IMPORTANT: Your use of this Software is limited to those specific rights
12
13
granted under the terms of a software license agreement between the user
14
who downloaded the software, his/her employer (which must be your employer)
and Texas Instruments Incorporated (the "License"). You may not use this
15
16
Software unless you agree to abide by the terms of the License. The License
17
limits your use, and you acknowledge, that the Software may not be modified,
18
copied or distributed unless embedded on a Texas Instruments microcontroller
19
or used solely and exclusively in conjunction with a Texas Instruments radio
20
frequency transceiver, which is integrated into your product. Other than for
the foregoing purpose, you may not use, reproduce, copy, prepare derivative
21
22
works of, modify, distribute, perform, display or sell this Software and/or
23
its documentation for any purpose.
24
25
YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE 26
PROVIDED 揂S IS?WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
27
28
NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
29
TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
30
NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
31
LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
32
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
33
34
OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
35
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
36
37
Should you have any questions regarding your right to use this Software,
38
contact Texas Instruments Incorporated at www.TI.com.
39
******************************************************************************* 40
*******************/
41
42
/*********************************************************************
43
* INCLUDES
44
*/
45
//此段相当于安卓中的important,导入头文件
46
#include "bcomdef.h"
47
#include "OSAL.h"
48
#include "OnBoard.h"
49
#include "hal_led.h"
50
#include "hal_lcd.h"
#include "hal_key.h"
51
52
#include "linkdb.h"
53
#include "gatt.h"
54
#include "gapgattserver.h"
55
#include "gattservapp.h"
56
#include "heartrateservice.h"
#include "devinfoservice.h"
57
58
#include "battservice.h"
59
#include "peripheral.h"
#include "gapbondmgr.h"
60
61
#include "heartrate.h"
62
#include "myheartrate.h"
63
64
//important到此为止
65
extern bool QS;// 当BPM 确定时,这个值是True
extern int16 BPM; // 脉率值,即上文所说的BPM
66
67
void HeartRateloop();
68
/*********************************************************************
69
* MACROS
70
*/
71
//下面的是一些宏定义,也就是相当于安卓中Int HEARTRATE_BPM_TO_RR=123; 给这个变量赋值一个固定的数值id
72
73
// Convert BPM to RR-Interval for data simulation purposes
74
#define HEARTRATE_BPM_TO_RR(bpm) ((uint16) 60 * 1024 / (uint16) (bpm))
75
76
/*********************************************************************
77
* CONSTANTS
*/
78
79
80
// Fast advertising interval in 625us units
81
#define DEFAULT_FAST_ADV_INTERVAL 32
82
83
// Duration of fast advertising duration in ms
84
#define DEFAULT_FAST_ADV_DURATION 30000
85
86
// Slow advertising interval in 625us units
87
#define DEFAULT_SLOW_ADV_INTERVAL 1600
88
89
// Duration of slow advertising duration in ms (set to 0 for continuous advertising)
90
#define DEFAULT_SLOW_ADV_DURATION 0
91
92
// How often to perform heart rate periodic event
93
#define DEFAULT_HEARTRATE_PERIOD 1000
94
// Whether to enable automatic parameter update request when a connection is formed
95
96
#define DEFAULT_ENABLE_UPDATE_REQUEST FALSE
97
98
// Minimum connection interval (units of 1.25ms) if automatic parameter update request is enabled
99
100
#define DEFAULT_DESIRED_MIN_CONN_INTERVAL 200
// Maximum connection interval (units of 1.25ms) if automatic parameter update request is 102
103
enabled
104
#define DEFAULT_DESIRED_MAX_CONN_INTERVAL 1600
105
106
// Slave latency to use if automatic parameter update request is enabled
107
#define DEFAULT_DESIRED_SLAVE_LATENCY 1
108
109
// Supervision timeout value (units of 10ms) if automatic parameter update request is enabled 110
#define DEFAULT_DESIRED_CONN_TIMEOUT 1000
111
112
// Battery level is critical when it is less than this %
113
#define DEFAULT_BATT_CRITICAL_LEVEL 6
114
// Battery measurement period in ms
115
116
#define DEFAULT_BATT_PERIOD 15000
117
// Some values used to simulate measurements
118
119
#define BPM_DEFAULT 73
120
#define BPM_MAX 80
121
#define ENERGY_INCREMENT 10
122
#define FLAGS_IDX_MAX 7
123
//宏定义到此为止
//下面是是真正变量的定义
124
125
/*********************************************************************
126
* TYPEDEFS
*/
127
128
129
/*********************************************************************
130
* GLOBAL VARIABLES
131
*/
132
/*********************************************************************
133
134
* EXTERNAL VARIABLES
135
*/
136
137
/*********************************************************************
138
* EXTERNAL FUNCTIONS
139
*/
140
141
/*********************************************************************
142
* LOCAL VARIABLES
143
*/
144
static uint8 heartRate_TaskID; // Task ID for internal task/event processing
145
146
static gaprole_States_t gapProfileState = GAPROLE_INIT;
147
148
// GAP Profile - Name attribute for SCAN RSP data
static uint8 scanData[] = // 被搜索到之后设备会把这个8位发送出去,蓝牙设备接受到之149
150
后会将这个数组转化为自己要的类似于这样的数组就是蓝牙传输过程中的流
0x12, // length of this data
152
153
GAP_ADTYPE_LOCAL_NAME_COMPLETE,
154
'H',
'e',
155
156
'a',
157
'r',
158
't',
159
' ',
160
'R',
'a',
161
162
't',
163
'e',
' ',
164
165
'S',
166
'e',
167
'n',
168
's',
169
'o',
'r'
170
171
};
172
static uint8 advertData[] = // 这个也是将要通过蓝牙传输给其他蓝牙设备(如手机)的数据, 173
174
貌似定义了一些UUID (uuid 做蓝牙的人都懂得)
175
{
176
// flags
177
0x02,
178
GAP_ADTYPE_FLAGS,
GAP_ADTYPE_FLAGS_GENERAL | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,
179
180
// service UUIDs
181
0x05,
GAP_ADTYPE_16BIT_MORE,
182
183
LO_UINT16(HEARTRATE_SERV_UUID),
184
HI_UINT16(HEARTRATE_SERV_UUID),
185
LO_UINT16(BATT_SERVICE_UUID),
186
HI_UINT16(BATT_SERVICE_UUID)
187
};
188
189
// Device name attribute value接下来定义的都是一些变量
190
static uint8 attDeviceName[GAP_DEVICE_NAME_LEN] = "Heart Rate Sensor";
191
192
// GAP connection handle
193
static uint16 gapConnHandle;
194
// Heart rate measurement value stored in this structure
195
196
static attHandleValueNoti_t heartRateMeas;
197
198
// Components of heart rate measurement structure
static uint8 heartRateBpm = BPM_DEFAULT;
199
200
static uint16 heartRateEnergy = 0;
static uint16 heartRateRrInterval1 = HEARTRATE_BPM_TO_RR(BPM_DEFAULT);
201
202
static uint16 heartRateRrInterval2 = HEARTRATE_BPM_TO_RR(BPM_DEFAULT);
203
204
// flags for simulated measurements 一组数据结构,定义了设备的一些标志,里面的字母是宏定义中定义好的,他的实质是一个数字用的时候直接用名字数字不用管
205
206
static const uint8 heartRateFlags[FLAGS_IDX_MAX] =
207
{
HEARTRATE_FLAGS_CONTACT_NOT_SUP,
208
209
HEARTRATE_FLAGS_CONTACT_NOT_DET,
210
HEARTRATE_FLAGS_CONTACT_DET | HEARTRATE_FLAGS_ENERGY_EXP,
211
HEARTRATE_FLAGS_CONTACT_DET | HEARTRATE_FLAGS_RR,
212
HEARTRATE_FLAGS_CONTACT_DET | HEARTRATE_FLAGS_ENERGY_EXP | 213
HEARTRATE_FLAGS_RR,
HEARTRATE_FLAGS_FORMAT_UINT16 | HEARTRATE_FLAGS_CONTACT_DET | 214
215
HEARTRATE_FLAGS_ENERGY_EXP | HEARTRATE_FLAGS_RR,
216
0x00
};
217
218
219
static uint8 heartRateFlagsIdx = 0;
220
221
// Advertising user-cancelled state
222
static bool heartRateAdvCancelled = FALSE;
223
224
/*********************************************************************
225
* LOCAL FUNCTIONS 本地的一些方法都在这里了
226
*/
static void heartRate_ProcessOSALMsg( osal_event_hdr_t *pMsg );
227
228
static void HeartRateGapStateCB( gaprole_States_t newState );
229
static void heartRatePeriodicTask( void );
230
static void heartRateBattPeriodicTask( void );
231
static void heartRate_HandleKeys( uint8 shift, uint8 keys );
232
static void heartRateMeasNotify(void);
static void heartRateCB(uint8 event);
233
234
static void heartRateBattCB(uint8 event);
235
/*********************************************************************
236
237
* PROFILE CALLBACKS
238
*/
239
240
// GAP Role Callbacks这是一个回调
函数
excel方差函数excelsd函数已知函数 2 f x m x mx m 2 1 4 2拉格朗日函数pdf函数公式下载
当状态改变时会调用GAP 我也不知道是什么241
static gapRolesCBs_t heartRatePeripheralCB =
{
242
243
HeartRateGapStateCB, // Profile State Change Callbacks
244
NULL // When a valid RSSI is read from controller
245
};
246
247
// Bond Manager Callbacks这也是一个回调函数应该是gap绑定CB 的回调函数
248
static const gapBondCBs_t heartRateBondCB =
249
{
250
NULL, // Passcode callback