an observer for temperature demo

Dependencies:   BLE_API mbed nRF51822 TMP_nrf51

Fork of BLE_Observer by Bluetooth Low Energy

Committer:
sunsmile2015
Date:
Mon Jul 20 09:00:24 2015 +0000
Revision:
7:91324daa3bfa
Parent:
6:850f44146c9f
Child:
8:649bd171929e
1. change the coding style; 2. add struct for adv data and manufacturer data

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 0:332983584a9c 1 /* mbed Microcontroller Library
rgrover1 0:332983584a9c 2 * Copyright (c) 2006-2015 ARM Limited
rgrover1 0:332983584a9c 3 *
rgrover1 0:332983584a9c 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 0:332983584a9c 5 * you may not use this file except in compliance with the License.
rgrover1 0:332983584a9c 6 * You may obtain a copy of the License at
rgrover1 0:332983584a9c 7 *
rgrover1 0:332983584a9c 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 0:332983584a9c 9 *
rgrover1 0:332983584a9c 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 0:332983584a9c 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 0:332983584a9c 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 0:332983584a9c 13 * See the License for the specific language governing permissions and
rgrover1 0:332983584a9c 14 * limitations under the License.
rgrover1 0:332983584a9c 15 */
rgrover1 0:332983584a9c 16
rgrover1 0:332983584a9c 17 #include "mbed.h"
rgrover1 4:dd8231564124 18 #include "BLE.h"
sunsmile2015 7:91324daa3bfa 19 #include "TMP_nrf51/TMP_nrf51.h"
sunsmile2015 7:91324daa3bfa 20
sunsmile2015 7:91324daa3bfa 21 #define COMP_ID_TEST 0xFEFE
sunsmile2015 7:91324daa3bfa 22
sunsmile2015 7:91324daa3bfa 23 #pragma pack(1)
sunsmile2015 7:91324daa3bfa 24 /* Advertising data */
sunsmile2015 7:91324daa3bfa 25 typedef struct advertisingData {
sunsmile2015 7:91324daa3bfa 26 uint8_t length; /* doesn't include itself*/
sunsmile2015 7:91324daa3bfa 27 GapAdvertisingData::DataType dataType;
sunsmile2015 7:91324daa3bfa 28 uint8_t data[1];
sunsmile2015 7:91324daa3bfa 29 } advertisingData_t;
sunsmile2015 7:91324daa3bfa 30
sunsmile2015 7:91324daa3bfa 31 typedef struct manufacturerData {
sunsmile2015 7:91324daa3bfa 32 uint16_t companyId;
sunsmile2015 7:91324daa3bfa 33 /* User defined manufacture data */
sunsmile2015 7:91324daa3bfa 34 TMP_nrf51::tmpSensorValue_t tmpSensorValue;
sunsmile2015 7:91324daa3bfa 35 } manufacturerData_t;
sunsmile2015 7:91324daa3bfa 36 #pragma pack()
rgrover1 0:332983584a9c 37
rgrover1 5:103717ce54e5 38 BLE ble;
rgrover1 0:332983584a9c 39 DigitalOut led1(LED1);
rgrover1 0:332983584a9c 40
rgrover1 0:332983584a9c 41 void periodicCallback(void)
rgrover1 0:332983584a9c 42 {
rgrover1 0:332983584a9c 43 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
rgrover1 0:332983584a9c 44 }
rgrover1 0:332983584a9c 45
sunsmile2015 6:850f44146c9f 46 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
sunsmile2015 6:850f44146c9f 47 {
sunsmile2015 7:91324daa3bfa 48 advertisingData_t *pAdvData = NULL;
sunsmile2015 7:91324daa3bfa 49 uint8_t len = 0;
sunsmile2015 6:850f44146c9f 50
sunsmile2015 7:91324daa3bfa 51 /* Search for the manufacturer data */
sunsmile2015 7:91324daa3bfa 52 while(len < params->advertisingDataLen) {
sunsmile2015 7:91324daa3bfa 53 pAdvData = (advertisingData_t *)&params->advertisingData[len];
sunsmile2015 7:91324daa3bfa 54 if(pAdvData->dataType == GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA) {
sunsmile2015 7:91324daa3bfa 55 manufacturerData_t *pManuData = (manufacturerData_t *)pAdvData->data;
sunsmile2015 7:91324daa3bfa 56 if(pManuData->companyId == COMP_ID_TEST) {
sunsmile2015 7:91324daa3bfa 57 printf("Recv temp is %f\r\n", (float)pManuData->tmpSensorValue);
sunsmile2015 7:91324daa3bfa 58 break;
sunsmile2015 7:91324daa3bfa 59 }
sunsmile2015 6:850f44146c9f 60 }
sunsmile2015 7:91324daa3bfa 61 len += (pAdvData->length + 1);
rgrover1 0:332983584a9c 62 }
rgrover1 0:332983584a9c 63 }
rgrover1 0:332983584a9c 64
rgrover1 0:332983584a9c 65 int main(void)
rgrover1 0:332983584a9c 66 {
rgrover1 0:332983584a9c 67 led1 = 1;
rgrover1 0:332983584a9c 68 Ticker ticker;
rgrover1 0:332983584a9c 69 ticker.attach(periodicCallback, 1);
rgrover1 0:332983584a9c 70
rgrover1 0:332983584a9c 71 ble.init();
sunsmile2015 6:850f44146c9f 72 ble.gap().setScanParams(1800 /* scan interval */, 1500 /* scan window */);
rgrover1 5:103717ce54e5 73 ble.gap().startScan(advertisementCallback);
rgrover1 0:332983584a9c 74
rgrover1 0:332983584a9c 75 while (true) {
rgrover1 0:332983584a9c 76 ble.waitForEvent();
rgrover1 0:332983584a9c 77 }
rgrover1 0:332983584a9c 78 }