1
1
/* mbed Microcontroller Library
2
- * Copyright (c) 2006-2013 ARM Limited
2
+ * Copyright (c) 2006-2014 ARM Limited
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
15
15
*/
16
16
17
17
#include " mbed.h"
18
- #include " nRF51822n.h"
19
18
#include " TMP102.h"
19
+ #include " nRF51822n.h"
20
20
21
- nRF51822n nrf; /* BLE radio driver */
21
+ nRF51822n nrf; /* BLE radio driver */
22
+ TMP102 healthThemometer (p22, p20, 0x90 ); /* The TMP102 connected to our board */
22
23
23
- DigitalOut led1 (LED1);
24
- DigitalOut led2 (LED2);
25
- Serial pc (USBTX,USBRX);
26
- TMP102 healthThemometer (p22, p20, 0x90 );
24
+ /* LEDs for indication: */
25
+ DigitalOut oneSecondLed (LED1); /* LED1 is toggled every second. */
26
+ DigitalOut advertisingStateLed (LED2); /* LED2 is on when we are advertising, otherwise off. */
27
27
28
- /* Device Information service */
29
- uint8_t manufacturerName[ 4 ] = { ' m ' , ' b ' , ' e ' , ' d ' };
30
- GattService deviceInformationService ( GattService::UUID_DEVICE_INFORMATION_SERVICE ) ;
31
- GattCharacteristic deviceManufacturer ( GattCharacteristic::UUID_MANUFACTURER_NAME_STRING_CHAR,
32
- sizeof (manufacturerName), sizeof(manufacturerName) ,
33
- GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ );
28
+
29
+ /* Health Thermometer Service */
30
+ uint8_t thermTempPayload[ 5 ] = { 0 , 0 , 0 , 0 , 0 } ;
31
+ GattService thermService (GattService::UUID_HEALTH_THERMOMETER_SERVICE);
32
+ GattCharacteristic thermTemp (GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR ,
33
+ 5 , 5 , GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE );
34
34
35
35
/* Battery Level Service */
36
36
uint8_t batt = 100 ; /* Battery level */
@@ -41,19 +41,13 @@ GattCharacteristic battLevel ( GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, 1,
41
41
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
42
42
43
43
44
- /* Health Thermometer Service */
45
- uint8_t thermTempPayload[5 ] = { 0 , 0 , 0 , 0 , 0 };
46
- GattService thermService (GattService::UUID_HEALTH_THERMOMETER_SERVICE);
47
- GattCharacteristic thermTemp (GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR,
48
- 5 , 5 , GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE);
49
-
50
44
/* Advertising data and parameters */
51
45
GapAdvertisingData advData;
52
46
GapAdvertisingData scanResponse;
53
47
GapAdvertisingParams advParams ( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED );
54
- uint16_t uuid16_list[] = { GattService::UUID_BATTERY_SERVICE,
55
- GattService::UUID_DEVICE_INFORMATION_SERVICE ,
56
- GattService::UUID_HEALTH_THERMOMETER_SERVICE };
48
+
49
+ uint16_t uuid16_list[] = { GattService::UUID_HEALTH_THERMOMETER_SERVICE ,
50
+ GattService::UUID_BATTERY_SERVICE };
57
51
58
52
uint32_t quick_ieee11073_from_float (float temperature);
59
53
void updateServiceValues (void );
@@ -66,59 +60,18 @@ void updateServiceValues(void);
66
60
/* *************************************************************************/
67
61
class GapEventHandler : public GapEvents
68
62
{
69
- virtual void onTimeout (void )
70
- {
71
- pc.printf (" Advertising Timeout!\n\r " );
72
- // Restart the advertising process with a much slower interval,
73
- // only start advertising again after a button press, etc.
74
- }
75
-
63
+ // virtual void onTimeout(void) {}
64
+
76
65
virtual void onConnected (void )
77
66
{
78
- pc. printf ( " Connected! \n\r " ) ;
67
+ advertisingStateLed = 0 ;
79
68
}
80
69
70
+ /* When a client device disconnects we need to start advertising again. */
81
71
virtual void onDisconnected (void )
82
72
{
83
- pc.printf (" Disconnected!\n\r " );
84
- pc.printf (" Restarting the advertising process\n\r " );
85
73
nrf.getGap ().startAdvertising (advParams);
86
- }
87
- };
88
-
89
- /* *************************************************************************/
90
- /* !
91
- @brief This custom class can be used to override any GattServerEvents
92
- that you are interested in handling on an application level.
93
- */
94
- /* *************************************************************************/
95
- class GattServerEventHandler : public GattServerEvents
96
- {
97
- // virtual void onDataSent(void) {}
98
- // virtual void onDataWritten(void) {}
99
-
100
- virtual void onUpdatesEnabled (uint16_t charHandle)
101
- {
102
- if (charHandle == thermTemp.handle )
103
- {
104
- pc.printf (" Temperature indication enabled\n\r " );
105
- }
106
- }
107
-
108
- virtual void onUpdatesDisabled (uint16_t charHandle)
109
- {
110
- if (charHandle == thermTemp.handle )
111
- {
112
- pc.printf (" Temperature indication disabled\n\r " );
113
- }
114
- }
115
-
116
- virtual void onConfirmationReceived (uint16_t charHandle)
117
- {
118
- if (charHandle == thermTemp.handle )
119
- {
120
- pc.printf (" Temperature indication received\n\r " );
121
- }
74
+ advertisingStateLed = 1 ;
122
75
}
123
76
};
124
77
@@ -129,18 +82,14 @@ class GattServerEventHandler : public GattServerEvents
129
82
/* *************************************************************************/
130
83
int main (void )
131
84
{
132
- pc.baud (115200 );
133
85
134
- /* Setup blinky: led1 is toggled in main, led2 is toggled via Ticker */
135
- led1=1 ;
136
- led2=1 ;
137
-
138
- /* Setup the local GAP/GATT event handlers */
86
+ /* Setup blinky led */
87
+ oneSecondLed=1 ;
88
+
89
+ /* Setup an event handler for GAP events i.e. Client/Server connection events. */
139
90
nrf.getGap ().setEventHandler (new GapEventHandler ());
140
- nrf.getGattServer ().setEventHandler (new GattServerEventHandler ());
141
-
91
+
142
92
/* Initialise the nRF51822 */
143
- pc.printf (" Initialising the nRF51822\n\r " );
144
93
nrf.init ();
145
94
146
95
/* Make sure we get a clean start */
@@ -153,55 +102,46 @@ int main(void)
153
102
advData.addAppearance (GapAdvertisingData::GENERIC_THERMOMETER);
154
103
nrf.getGap ().setAdvertisingData (advData, scanResponse);
155
104
156
- /* Add the Battery Level service */
157
- battService.addCharacteristic (battLevel);
158
- nrf.getGattServer ().addService (battService);
159
-
160
- /* Add the Device Information service */
161
- deviceInformationService.addCharacteristic (deviceManufacturer);
162
- nrf.getGattServer ().addService (deviceInformationService);
163
-
164
-
165
105
/* Health Thermometer Service */
166
106
thermService.addCharacteristic (thermTemp);
167
107
nrf.getGattServer ().addService (thermService);
108
+
109
+ /* Add the Battery Level service */
110
+ battService.addCharacteristic (battLevel);
111
+ nrf.getGattServer ().addService (battService);
168
112
169
113
/* Start advertising (make sure you've added all your data first) */
170
114
nrf.getGap ().startAdvertising (advParams);
171
-
172
- /* Now that we're live, update the battery level characteristic, and */
173
- /* change the device manufacturer characteristic to 'mbed' */
174
- nrf.getGattServer ().updateValue (battLevel.handle , (uint8_t *)&batt, sizeof (batt));
175
- nrf.getGattServer ().updateValue (deviceManufacturer.handle , manufacturerName, sizeof (manufacturerName));
176
- nrf.getGattServer ().updateValue (thermTemp.handle , thermTempPayload, sizeof (thermTempPayload));
177
-
115
+ advertisingStateLed = 1 ;
178
116
179
117
for (;;)
180
118
{
181
- wait ( 1 );
119
+ /* Now that we're live, update the battery level & temperature characteristics */
182
120
updateServiceValues ();
121
+ wait (1 );
183
122
}
184
123
}
185
124
186
125
/* *************************************************************************/
187
126
/* !
188
- @brief Ticker callback to switch led2 state
127
+ @brief Ticker callback to switch advertisingStateLed state
189
128
*/
190
129
/* *************************************************************************/
191
130
void updateServiceValues (void )
192
131
{
193
- /* Toggle the LED */
194
- led1 = !led1 ;
132
+ /* Toggle the one second LEDs */
133
+ oneSecondLed = !oneSecondLed ;
195
134
196
135
/* Update battery level */
197
136
nrf.getGattServer ().updateValue (battLevel.handle , (uint8_t *)&batt, sizeof (batt));
137
+ /* Decrement the battery level. */
138
+ batt <=50 ? batt=100 : batt--;;
198
139
199
- /* Update the temperature */
140
+ /* Update the temperature. Note that we need to convert to an ieee11073 format float. */
200
141
float temperature = healthThemometer.read ();
201
142
uint32_t temp_ieee11073 = quick_ieee11073_from_float (temperature);
202
143
memcpy (thermTempPayload+1 , &temp_ieee11073, 4 );
203
144
nrf.getGattServer ().updateValue (thermTemp.handle , thermTempPayload, sizeof (thermTempPayload));
204
- printf (" Temperature: %f Celsius\r\n " , temperature);
205
145
}
206
146
207
147
/* *
@@ -216,3 +156,4 @@ uint32_t quick_ieee11073_from_float(float temperature)
216
156
217
157
return ( ((uint32_t )exponent) << 24 ) | mantissa;
218
158
}
159
+
0 commit comments