dfrobot

Dependencies:   BLE_API mbed nRF51822

Dependents:   BLE_UART_LCD_Batt_DevInfo

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Committer:
jimaobian
Date:
Fri Oct 31 07:55:37 2014 +0000
Revision:
9:7b3841a91548
Parent:
6:e0fc9072e853
cup

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 0:e910d9bb040f 1 /* mbed Microcontroller Library
yihui 0:e910d9bb040f 2 * Copyright (c) 2006-2013 ARM Limited
yihui 0:e910d9bb040f 3 *
yihui 0:e910d9bb040f 4 * Licensed under the Apache License, Version 2.0 (the "License");
yihui 0:e910d9bb040f 5 * you may not use this file except in compliance with the License.
yihui 0:e910d9bb040f 6 * You may obtain a copy of the License at
yihui 0:e910d9bb040f 7 *
yihui 0:e910d9bb040f 8 * http://www.apache.org/licenses/LICENSE-2.0
yihui 0:e910d9bb040f 9 *
yihui 0:e910d9bb040f 10 * Unless required by applicable law or agreed to in writing, software
yihui 0:e910d9bb040f 11 * distributed under the License is distributed on an "AS IS" BASIS,
yihui 0:e910d9bb040f 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
yihui 0:e910d9bb040f 13 * See the License for the specific language governing permissions and
yihui 0:e910d9bb040f 14 * limitations under the License.
yihui 0:e910d9bb040f 15 */
yihui 0:e910d9bb040f 16
yihui 0:e910d9bb040f 17 #include "mbed.h"
Rohit Grover 2:e060367b9024 18 #include "BLEDevice.h"
yihui 0:e910d9bb040f 19
rgrover1 6:e0fc9072e853 20 #include "UARTService.h"
yihui 0:e910d9bb040f 21
jimaobian 9:7b3841a91548 22 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
Rohit Grover 2:e060367b9024 23 * it will have an impact on code-size and power consumption. */
yihui 0:e910d9bb040f 24
Rohit Grover 2:e060367b9024 25 #if NEED_CONSOLE_OUTPUT
rgrover1 6:e0fc9072e853 26 #define DEBUG(...) { printf(__VA_ARGS__); }
yihui 0:e910d9bb040f 27 #else
Rohit Grover 2:e060367b9024 28 #define DEBUG(...) /* nothing */
Rohit Grover 2:e060367b9024 29 #endif /* #if NEED_CONSOLE_OUTPUT */
yihui 0:e910d9bb040f 30
Rohit Grover 2:e060367b9024 31 BLEDevice ble;
Rohit Grover 2:e060367b9024 32 DigitalOut led1(LED1);
jimaobian 9:7b3841a91548 33 DigitalOut led2(LED2);
jimaobian 9:7b3841a91548 34 Timer timer;
yihui 0:e910d9bb040f 35
rgrover1 6:e0fc9072e853 36 UARTService *uartServicePtr;
yihui 0:e910d9bb040f 37
rgrover1 5:4bc41267a03a 38 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
yihui 0:e910d9bb040f 39 {
Rohit Grover 2:e060367b9024 40 DEBUG("Disconnected!\n\r");
Rohit Grover 2:e060367b9024 41 DEBUG("Restarting the advertising process\n\r");
Rohit Grover 2:e060367b9024 42 ble.startAdvertising();
Rohit Grover 2:e060367b9024 43 }
yihui 0:e910d9bb040f 44
rgrover1 6:e0fc9072e853 45 void onDataWritten(const GattCharacteristicWriteCBParams *params)
yihui 0:e910d9bb040f 46 {
rgrover1 6:e0fc9072e853 47 if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) {
rgrover1 5:4bc41267a03a 48 uint16_t bytesRead = params->len;
rgrover1 5:4bc41267a03a 49 DEBUG("received %u bytes\n\r", bytesRead);
rgrover1 6:e0fc9072e853 50 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
yihui 0:e910d9bb040f 51 }
Rohit Grover 2:e060367b9024 52 }
yihui 0:e910d9bb040f 53
Rohit Grover 2:e060367b9024 54 void periodicCallback(void)
Rohit Grover 2:e060367b9024 55 {
rgrover1 6:e0fc9072e853 56 led1 = !led1;
Rohit Grover 2:e060367b9024 57 }
yihui 0:e910d9bb040f 58
yihui 0:e910d9bb040f 59 int main(void)
yihui 0:e910d9bb040f 60 {
jimaobian 9:7b3841a91548 61
jimaobian 9:7b3841a91548 62
jimaobian 9:7b3841a91548 63 timer.start();
jimaobian 9:7b3841a91548 64
Rohit Grover 2:e060367b9024 65 led1 = 1;
Rohit Grover 2:e060367b9024 66 Ticker ticker;
Rohit Grover 2:e060367b9024 67 ticker.attach(periodicCallback, 1);
yihui 0:e910d9bb040f 68
Rohit Grover 2:e060367b9024 69 DEBUG("Initialising the nRF51822\n\r");
Rohit Grover 2:e060367b9024 70 ble.init();
Rohit Grover 2:e060367b9024 71 ble.onDisconnection(disconnectionCallback);
Rohit Grover 2:e060367b9024 72 ble.onDataWritten(onDataWritten);
yihui 0:e910d9bb040f 73
Rohit Grover 2:e060367b9024 74 /* setup advertising */
Rohit Grover 2:e060367b9024 75 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
Rohit Grover 2:e060367b9024 76 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Rohit Grover 2:e060367b9024 77 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
rgrover1 6:e0fc9072e853 78 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
Rohit Grover 2:e060367b9024 79 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
rgrover1 6:e0fc9072e853 80 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
yihui 0:e910d9bb040f 81
Rohit Grover 2:e060367b9024 82 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
Rohit Grover 2:e060367b9024 83 ble.startAdvertising();
yihui 0:e910d9bb040f 84
rgrover1 6:e0fc9072e853 85 UARTService uartService(ble);
rgrover1 6:e0fc9072e853 86 uartServicePtr = &uartService;
yihui 0:e910d9bb040f 87
jimaobian 9:7b3841a91548 88
Rohit Grover 2:e060367b9024 89 while (true) {
jimaobian 9:7b3841a91548 90 static unsigned long led2timer=timer.read_ms();
jimaobian 9:7b3841a91548 91
jimaobian 9:7b3841a91548 92 if((unsigned long)timer.read_ms()-led2timer>1000UL)
jimaobian 9:7b3841a91548 93 {
jimaobian 9:7b3841a91548 94 led2timer=timer.read_ms();
jimaobian 9:7b3841a91548 95 led2=!led2;
jimaobian 9:7b3841a91548 96 }
jimaobian 9:7b3841a91548 97
jimaobian 9:7b3841a91548 98
jimaobian 9:7b3841a91548 99
Rohit Grover 2:e060367b9024 100 ble.waitForEvent();
yihui 0:e910d9bb040f 101 }
yihui 0:e910d9bb040f 102 }
jimaobian 9:7b3841a91548 103
jimaobian 9:7b3841a91548 104 //// Count the time to toggle a LED
jimaobian 9:7b3841a91548 105 //
jimaobian 9:7b3841a91548 106 //#include "mbed.h"
jimaobian 9:7b3841a91548 107 //
jimaobian 9:7b3841a91548 108 //Timer timer;
jimaobian 9:7b3841a91548 109 //DigitalOut led(LED1);
jimaobian 9:7b3841a91548 110 //int begin, end;
jimaobian 9:7b3841a91548 111 //
jimaobian 9:7b3841a91548 112 //int main() {
jimaobian 9:7b3841a91548 113 // timer.start();
jimaobian 9:7b3841a91548 114 //
jimaobian 9:7b3841a91548 115 // while(true)
jimaobian 9:7b3841a91548 116 // {
jimaobian 9:7b3841a91548 117 //// begin = timer.read_ms();
jimaobian 9:7b3841a91548 118 // led = !led;
jimaobian 9:7b3841a91548 119 // wait(1);
jimaobian 9:7b3841a91548 120 //// end = timer.read_ms();
jimaobian 9:7b3841a91548 121 //// printf("%d us\r\n", end - begin);
jimaobian 9:7b3841a91548 122 // }
jimaobian 9:7b3841a91548 123 //}