an observer for temperature demo

Dependencies:   BLE_API mbed nRF51822 TMP_nrf51

Fork of BLE_Observer by Bluetooth Low Energy

main.cpp

Committer:
sunsmile2015
Date:
2015-07-20
Revision:
7:91324daa3bfa
Parent:
6:850f44146c9f
Child:
8:649bd171929e

File content as of revision 7:91324daa3bfa:

/* mbed Microcontroller Library
 * Copyright (c) 2006-2015 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mbed.h"
#include "BLE.h"
#include "TMP_nrf51/TMP_nrf51.h"

#define COMP_ID_TEST 0xFEFE

#pragma pack(1)
/* Advertising data  */
typedef struct advertisingData {
    uint8_t length; /* doesn't include itself*/
    GapAdvertisingData::DataType dataType;
    uint8_t data[1];
} advertisingData_t;

typedef struct manufacturerData {
    uint16_t companyId;
    /* User defined manufacture data */
    TMP_nrf51::tmpSensorValue_t tmpSensorValue;
} manufacturerData_t;
#pragma pack()

BLE        ble;
DigitalOut led1(LED1);

void periodicCallback(void)
{
    led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
}

void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
{
    advertisingData_t *pAdvData = NULL;
    uint8_t len = 0;
    
    /* Search for the manufacturer data */
    while(len < params->advertisingDataLen) {
        pAdvData = (advertisingData_t *)&params->advertisingData[len];
        if(pAdvData->dataType == GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA) {
            manufacturerData_t *pManuData = (manufacturerData_t *)pAdvData->data;
            if(pManuData->companyId == COMP_ID_TEST) {
                printf("Recv temp is %f\r\n", (float)pManuData->tmpSensorValue);
                break;
            }
        }
        len += (pAdvData->length + 1);
    }
}

int main(void)
{
    led1 = 1;
    Ticker ticker;
    ticker.attach(periodicCallback, 1);

    ble.init();
    ble.gap().setScanParams(1800 /* scan interval */, 1500 /* scan window */);
    ble.gap().startScan(advertisementCallback);

    while (true) {
        ble.waitForEvent();
    }
}