Skip to content

Commit 232bd99

Browse files
committed
Update Health_Thermometer example to latest revision
1 parent 5f2aec8 commit 232bd99

File tree

1 file changed

+40
-99
lines changed
  • libraries/tests/ble/Health_Thermometer

1 file changed

+40
-99
lines changed
Lines changed: 40 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2006-2013 ARM Limited
2+
* Copyright (c) 2006-2014 ARM Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,22 +15,22 @@
1515
*/
1616

1717
#include "mbed.h"
18-
#include "nRF51822n.h"
1918
#include "TMP102.h"
19+
#include "nRF51822n.h"
2020

21-
nRF51822n nrf; /* BLE radio driver */
21+
nRF51822n nrf; /* BLE radio driver */
22+
TMP102 healthThemometer(p22, p20, 0x90); /* The TMP102 connected to our board */
2223

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. */
2727

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);
3434

3535
/* Battery Level Service */
3636
uint8_t batt = 100; /* Battery level */
@@ -41,19 +41,13 @@ GattCharacteristic battLevel ( GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, 1,
4141
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
4242

4343

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-
5044
/* Advertising data and parameters */
5145
GapAdvertisingData advData;
5246
GapAdvertisingData scanResponse;
5347
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};
5751

5852
uint32_t quick_ieee11073_from_float(float temperature);
5953
void updateServiceValues(void);
@@ -66,59 +60,18 @@ void updateServiceValues(void);
6660
/**************************************************************************/
6761
class GapEventHandler : public GapEvents
6862
{
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+
7665
virtual void onConnected(void)
7766
{
78-
pc.printf("Connected!\n\r");
67+
advertisingStateLed = 0;
7968
}
8069

70+
/* When a client device disconnects we need to start advertising again. */
8171
virtual void onDisconnected(void)
8272
{
83-
pc.printf("Disconnected!\n\r");
84-
pc.printf("Restarting the advertising process\n\r");
8573
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;
12275
}
12376
};
12477

@@ -129,18 +82,14 @@ class GattServerEventHandler : public GattServerEvents
12982
/**************************************************************************/
13083
int main(void)
13184
{
132-
pc.baud(115200);
13385

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. */
13990
nrf.getGap().setEventHandler(new GapEventHandler());
140-
nrf.getGattServer().setEventHandler(new GattServerEventHandler());
141-
91+
14292
/* Initialise the nRF51822 */
143-
pc.printf("Initialising the nRF51822\n\r");
14493
nrf.init();
14594

14695
/* Make sure we get a clean start */
@@ -153,55 +102,46 @@ int main(void)
153102
advData.addAppearance(GapAdvertisingData::GENERIC_THERMOMETER);
154103
nrf.getGap().setAdvertisingData(advData, scanResponse);
155104

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-
165105
/* Health Thermometer Service */
166106
thermService.addCharacteristic(thermTemp);
167107
nrf.getGattServer().addService(thermService);
108+
109+
/* Add the Battery Level service */
110+
battService.addCharacteristic(battLevel);
111+
nrf.getGattServer().addService(battService);
168112

169113
/* Start advertising (make sure you've added all your data first) */
170114
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;
178116

179117
for (;;)
180118
{
181-
wait(1);
119+
/* Now that we're live, update the battery level & temperature characteristics */
182120
updateServiceValues();
121+
wait(1);
183122
}
184123
}
185124

186125
/**************************************************************************/
187126
/*!
188-
@brief Ticker callback to switch led2 state
127+
@brief Ticker callback to switch advertisingStateLed state
189128
*/
190129
/**************************************************************************/
191130
void updateServiceValues(void)
192131
{
193-
/* Toggle the LED */
194-
led1 = !led1;
132+
/* Toggle the one second LEDs */
133+
oneSecondLed = !oneSecondLed;
195134

196135
/* Update battery level */
197136
nrf.getGattServer().updateValue(battLevel.handle, (uint8_t*)&batt, sizeof(batt));
137+
/* Decrement the battery level. */
138+
batt <=50 ? batt=100 : batt--;;
198139

199-
/* Update the temperature */
140+
/* Update the temperature. Note that we need to convert to an ieee11073 format float. */
200141
float temperature = healthThemometer.read();
201142
uint32_t temp_ieee11073 = quick_ieee11073_from_float(temperature);
202143
memcpy(thermTempPayload+1, &temp_ieee11073, 4);
203144
nrf.getGattServer().updateValue(thermTemp.handle, thermTempPayload, sizeof(thermTempPayload));
204-
printf("Temperature: %f Celsius\r\n", temperature);
205145
}
206146

207147
/**
@@ -216,3 +156,4 @@ uint32_t quick_ieee11073_from_float(float temperature)
216156

217157
return ( ((uint32_t)exponent) << 24) | mantissa;
218158
}
159+

0 commit comments

Comments
 (0)