BLE Nano LED control
Dependencies: BLE_API mbed nRF51822
Diff: main.cpp
- Revision:
- 0:355d86799c43
- Child:
- 2:f4709d271f13
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat Dec 24 14:50:03 2016 +0000 @@ -0,0 +1,84 @@ +/* + +Copyright (c) 2012-2014 RedBearLab + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software +and associated documentation files (the "Software"), to deal in the Software without restriction, +including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE +FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +*/ + +#include "mbed.h" +#include "ble/BLE.h" +#include "ble_nanoled_service.h" + +namespace { + const char DEVICE_NAME[] = "BLE Nano"; +} + +namespace { + BLE mBle; + ServiceUart mServiceUart; +} + + +namespace { + inline int calcAdvertisingIntervalMs(int msec) + { + return 1000 * msec / 625; + } +} + + +namespace { + void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) + { + mBle.gap().startAdvertising(); + } + + + void writtenHandler(const GattWriteCallbackParams* params) + { + mServiceUart.writtenHandler(mBle, params); + } + + void readHandler(const GattReadCallbackParams* params) + { + mServiceUart.readHandler(mBle, params); + } +} + + +int main(void) +{ + mBle.init(); + mBle.onDisconnection(disconnectionCallback); + mBle.onDataWritten(writtenHandler); + mBle.onDataRead(readHandler); + + //services + mServiceUart.addService(mBle); + + // setup advertising + mBle.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); + mBle.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); + mBle.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, + (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1); + // 100ms; in multiples of 0.625ms. + mBle.setAdvertisingInterval(calcAdvertisingIntervalMs(100)); + mBle.startAdvertising(); + + while (true) { + mBle.waitForEvent(); + } +} +