Beacon demo for the BLE API using the nRF51822 native mode drivers
Dependencies: BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1
Fork of BLE_iBeacon by
main.cpp@9:438f44012039, 2014-05-22 (annotated)
- Committer:
- Rohit Grover
- Date:
- Thu May 22 09:54:46 2014 +0100
- Revision:
- 9:438f44012039
- Parent:
- 8:d851d92601b7
- Child:
- 10:391c1acf4b9d
renaming LEDs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ktownsend | 0:7613d21e5974 | 1 | /* mbed Microcontroller Library |
ktownsend | 0:7613d21e5974 | 2 | * Copyright (c) 2006-2013 ARM Limited |
ktownsend | 0:7613d21e5974 | 3 | * |
ktownsend | 0:7613d21e5974 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ktownsend | 0:7613d21e5974 | 5 | * you may not use this file except in compliance with the License. |
ktownsend | 0:7613d21e5974 | 6 | * You may obtain a copy of the License at |
ktownsend | 0:7613d21e5974 | 7 | * |
ktownsend | 0:7613d21e5974 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
ktownsend | 0:7613d21e5974 | 9 | * |
ktownsend | 0:7613d21e5974 | 10 | * Unless required by applicable law or agreed to in writing, software |
ktownsend | 0:7613d21e5974 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
ktownsend | 0:7613d21e5974 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ktownsend | 0:7613d21e5974 | 13 | * See the License for the specific language governing permissions and |
ktownsend | 0:7613d21e5974 | 14 | * limitations under the License. |
ktownsend | 0:7613d21e5974 | 15 | */ |
ktownsend | 0:7613d21e5974 | 16 | |
ktownsend | 0:7613d21e5974 | 17 | #include "mbed.h" |
ktownsend | 0:7613d21e5974 | 18 | #include "nRF51822n.h" |
ktownsend | 0:7613d21e5974 | 19 | |
ktownsend | 0:7613d21e5974 | 20 | nRF51822n nrf; /* BLE radio driver */ |
ktownsend | 0:7613d21e5974 | 21 | |
Rohit Grover |
9:438f44012039 | 22 | DigitalOut mainloopLED(LED1); |
Rohit Grover |
9:438f44012039 | 23 | DigitalOut tickerLED(LED2); |
ktownsend | 0:7613d21e5974 | 24 | Ticker flipper; |
ktownsend | 0:7613d21e5974 | 25 | Serial pc(USBTX,USBRX); |
ktownsend | 0:7613d21e5974 | 26 | |
ktownsend | 0:7613d21e5974 | 27 | void tickerCallback(void); |
ktownsend | 0:7613d21e5974 | 28 | |
ktownsend | 0:7613d21e5974 | 29 | /**************************************************************************/ |
ktownsend | 0:7613d21e5974 | 30 | /*! |
ktownsend | 0:7613d21e5974 | 31 | @brief Program entry point |
ktownsend | 0:7613d21e5974 | 32 | */ |
ktownsend | 0:7613d21e5974 | 33 | /**************************************************************************/ |
ktownsend | 0:7613d21e5974 | 34 | int main(void) |
ktownsend | 0:7613d21e5974 | 35 | { |
Rohit Grover |
9:438f44012039 | 36 | /* Setup blinky: mainloopLED is toggled in main, tickerLED is toggled via Ticker */ |
Rohit Grover |
9:438f44012039 | 37 | mainloopLED = 1; |
Rohit Grover |
9:438f44012039 | 38 | tickerLED = 1; |
ktownsend | 0:7613d21e5974 | 39 | flipper.attach(&tickerCallback, 1.0); |
ktownsend | 0:7613d21e5974 | 40 | |
ktownsend | 0:7613d21e5974 | 41 | /* Initialise the nRF51822 */ |
ktownsend | 0:7613d21e5974 | 42 | pc.printf("Initialising the nRF51822\n\r"); |
ktownsend | 0:7613d21e5974 | 43 | nrf.init(); |
rohit.grover |
6:26eab6ee6df4 | 44 | |
rohit.grover |
5:97ce285ff00a | 45 | GapAdvertisingParams advParams ( |
rohit.grover |
5:97ce285ff00a | 46 | GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED ); |
ktownsend | 0:7613d21e5974 | 47 | GapAdvertisingData advData; |
ktownsend | 0:7613d21e5974 | 48 | GapAdvertisingData scanResponse; |
ktownsend | 0:7613d21e5974 | 49 | |
rohit.grover |
7:e2bfd5db6713 | 50 | /* |
rohit.grover |
7:e2bfd5db6713 | 51 | * For this demo application, populate the beacon advertisement payload |
rohit.grover |
7:e2bfd5db6713 | 52 | * with 2 AD structures: FLAG and MSD |
rohit.grover |
7:e2bfd5db6713 | 53 | * |
rohit.grover |
7:e2bfd5db6713 | 54 | * Reference: |
rohit.grover |
7:e2bfd5db6713 | 55 | * Bluetooth Core Specification 4.0 (Vol. 3), Part C, Section 11, 18 |
rohit.grover |
7:e2bfd5db6713 | 56 | */ |
rohit.grover |
7:e2bfd5db6713 | 57 | |
rohit.grover |
7:e2bfd5db6713 | 58 | /* Define an Beacon payload. |
rohit.grover |
7:e2bfd5db6713 | 59 | |
rohit.grover |
7:e2bfd5db6713 | 60 | This is the data part of the MSD AdvertisingData structure to be added to |
rohit.grover |
7:e2bfd5db6713 | 61 | the advertising payload. |
ktownsend | 0:7613d21e5974 | 62 | -------------------------------------------------------------- |
ktownsend | 0:7613d21e5974 | 63 | 128-Bit UUID = E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 |
ktownsend | 0:7613d21e5974 | 64 | Major/Minor = 0000 / 0000 |
ktownsend | 0:7613d21e5974 | 65 | Tx Power = C8 |
ktownsend | 0:7613d21e5974 | 66 | */ |
rohit.grover |
7:e2bfd5db6713 | 67 | uint8_t beaconPayload[] = { |
rohit.grover |
7:e2bfd5db6713 | 68 | 0x4C, 0x00, // Company identifier code (0x004C == Apple) |
rohit.grover |
7:e2bfd5db6713 | 69 | 0x02, // ID |
rohit.grover |
7:e2bfd5db6713 | 70 | 0x15, // length of the remaining payload |
rohit.grover |
7:e2bfd5db6713 | 71 | 0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, // UUID |
rohit.grover |
7:e2bfd5db6713 | 72 | 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61, |
rohit.grover |
7:e2bfd5db6713 | 73 | 0x00, 0x00, // the major value to differenciate a location |
rohit.grover |
7:e2bfd5db6713 | 74 | 0x00, 0x00, // the minor value to differenciate a location |
rohit.grover |
7:e2bfd5db6713 | 75 | 0xC8 // 2's complement of the Tx power (-56dB) |
rohit.grover |
7:e2bfd5db6713 | 76 | }; |
ktownsend | 0:7613d21e5974 | 77 | |
ktownsend | 0:7613d21e5974 | 78 | /* Make sure we get a clean start */ |
ktownsend | 0:7613d21e5974 | 79 | nrf.reset(); |
ktownsend | 0:7613d21e5974 | 80 | |
ktownsend | 0:7613d21e5974 | 81 | advData.addFlags(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
rohit.grover |
5:97ce285ff00a | 82 | advData.addData(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, |
rohit.grover |
5:97ce285ff00a | 83 | beaconPayload, |
rohit.grover |
5:97ce285ff00a | 84 | sizeof(beaconPayload)); |
ktownsend | 0:7613d21e5974 | 85 | |
ktownsend | 0:7613d21e5974 | 86 | /* Start advertising! */ |
ktownsend | 0:7613d21e5974 | 87 | nrf.getGap().setAdvertisingData(advData, scanResponse); |
ktownsend | 0:7613d21e5974 | 88 | nrf.getGap().startAdvertising(advParams); |
ktownsend | 0:7613d21e5974 | 89 | |
Rohit Grover |
9:438f44012039 | 90 | /* Do blinky on mainloopLED while we're waiting for BLE events */ |
rohit.grover |
5:97ce285ff00a | 91 | for (;; ) { |
Rohit Grover |
9:438f44012039 | 92 | mainloopLED = !mainloopLED; |
rohit.grover |
6:26eab6ee6df4 | 93 | wait(1); |
ktownsend | 0:7613d21e5974 | 94 | } |
ktownsend | 0:7613d21e5974 | 95 | } |
ktownsend | 0:7613d21e5974 | 96 | |
ktownsend | 0:7613d21e5974 | 97 | /**************************************************************************/ |
ktownsend | 0:7613d21e5974 | 98 | /*! |
Rohit Grover |
9:438f44012039 | 99 | @brief Ticker callback to switch tickerLED state |
ktownsend | 0:7613d21e5974 | 100 | */ |
ktownsend | 0:7613d21e5974 | 101 | /**************************************************************************/ |
ktownsend | 0:7613d21e5974 | 102 | void tickerCallback(void) |
ktownsend | 0:7613d21e5974 | 103 | { |
Rohit Grover |
9:438f44012039 | 104 | tickerLED = !tickerLED; |
ktownsend | 0:7613d21e5974 | 105 | } |