BLE Nano LED control

Dependencies:   BLE_API mbed nRF51822

Committer:
hiro99ma
Date:
Sat Dec 24 14:50:03 2016 +0000
Revision:
0:355d86799c43
Child:
2:f4709d271f13
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hiro99ma 0:355d86799c43 1 /*
hiro99ma 0:355d86799c43 2
hiro99ma 0:355d86799c43 3 Copyright (c) 2012-2014 RedBearLab
hiro99ma 0:355d86799c43 4
hiro99ma 0:355d86799c43 5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
hiro99ma 0:355d86799c43 6 and associated documentation files (the "Software"), to deal in the Software without restriction,
hiro99ma 0:355d86799c43 7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
hiro99ma 0:355d86799c43 8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
hiro99ma 0:355d86799c43 9 subject to the following conditions:
hiro99ma 0:355d86799c43 10 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
hiro99ma 0:355d86799c43 11
hiro99ma 0:355d86799c43 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
hiro99ma 0:355d86799c43 13 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
hiro99ma 0:355d86799c43 14 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
hiro99ma 0:355d86799c43 15 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
hiro99ma 0:355d86799c43 16 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
hiro99ma 0:355d86799c43 17
hiro99ma 0:355d86799c43 18 */
hiro99ma 0:355d86799c43 19
hiro99ma 0:355d86799c43 20 #include "mbed.h"
hiro99ma 0:355d86799c43 21 #include "ble/BLE.h"
hiro99ma 0:355d86799c43 22 #include "ble_nanoled_service.h"
hiro99ma 0:355d86799c43 23
hiro99ma 0:355d86799c43 24 namespace {
hiro99ma 0:355d86799c43 25 const char DEVICE_NAME[] = "BLE Nano";
hiro99ma 0:355d86799c43 26 }
hiro99ma 0:355d86799c43 27
hiro99ma 0:355d86799c43 28 namespace {
hiro99ma 0:355d86799c43 29 BLE mBle;
hiro99ma 0:355d86799c43 30 ServiceUart mServiceUart;
hiro99ma 0:355d86799c43 31 }
hiro99ma 0:355d86799c43 32
hiro99ma 0:355d86799c43 33
hiro99ma 0:355d86799c43 34 namespace {
hiro99ma 0:355d86799c43 35 inline int calcAdvertisingIntervalMs(int msec)
hiro99ma 0:355d86799c43 36 {
hiro99ma 0:355d86799c43 37 return 1000 * msec / 625;
hiro99ma 0:355d86799c43 38 }
hiro99ma 0:355d86799c43 39 }
hiro99ma 0:355d86799c43 40
hiro99ma 0:355d86799c43 41
hiro99ma 0:355d86799c43 42 namespace {
hiro99ma 0:355d86799c43 43 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
hiro99ma 0:355d86799c43 44 {
hiro99ma 0:355d86799c43 45 mBle.gap().startAdvertising();
hiro99ma 0:355d86799c43 46 }
hiro99ma 0:355d86799c43 47
hiro99ma 0:355d86799c43 48
hiro99ma 0:355d86799c43 49 void writtenHandler(const GattWriteCallbackParams* params)
hiro99ma 0:355d86799c43 50 {
hiro99ma 0:355d86799c43 51 mServiceUart.writtenHandler(mBle, params);
hiro99ma 0:355d86799c43 52 }
hiro99ma 0:355d86799c43 53
hiro99ma 0:355d86799c43 54 void readHandler(const GattReadCallbackParams* params)
hiro99ma 0:355d86799c43 55 {
hiro99ma 0:355d86799c43 56 mServiceUart.readHandler(mBle, params);
hiro99ma 0:355d86799c43 57 }
hiro99ma 0:355d86799c43 58 }
hiro99ma 0:355d86799c43 59
hiro99ma 0:355d86799c43 60
hiro99ma 0:355d86799c43 61 int main(void)
hiro99ma 0:355d86799c43 62 {
hiro99ma 0:355d86799c43 63 mBle.init();
hiro99ma 0:355d86799c43 64 mBle.onDisconnection(disconnectionCallback);
hiro99ma 0:355d86799c43 65 mBle.onDataWritten(writtenHandler);
hiro99ma 0:355d86799c43 66 mBle.onDataRead(readHandler);
hiro99ma 0:355d86799c43 67
hiro99ma 0:355d86799c43 68 //services
hiro99ma 0:355d86799c43 69 mServiceUart.addService(mBle);
hiro99ma 0:355d86799c43 70
hiro99ma 0:355d86799c43 71 // setup advertising
hiro99ma 0:355d86799c43 72 mBle.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
hiro99ma 0:355d86799c43 73 mBle.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
hiro99ma 0:355d86799c43 74 mBle.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
hiro99ma 0:355d86799c43 75 (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1);
hiro99ma 0:355d86799c43 76 // 100ms; in multiples of 0.625ms.
hiro99ma 0:355d86799c43 77 mBle.setAdvertisingInterval(calcAdvertisingIntervalMs(100));
hiro99ma 0:355d86799c43 78 mBle.startAdvertising();
hiro99ma 0:355d86799c43 79
hiro99ma 0:355d86799c43 80 while (true) {
hiro99ma 0:355d86799c43 81 mBle.waitForEvent();
hiro99ma 0:355d86799c43 82 }
hiro99ma 0:355d86799c43 83 }
hiro99ma 0:355d86799c43 84