BLE Nano LED control
Dependencies: BLE_API mbed nRF51822
main.cpp@2:f4709d271f13, 2016-12-24 (annotated)
- Committer:
- hiro99ma
- Date:
- Sat Dec 24 15:18:47 2016 +0000
- Revision:
- 2:f4709d271f13
- Parent:
- 0:355d86799c43
delete File Header
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hiro99ma | 0:355d86799c43 | 1 | #include "mbed.h" |
hiro99ma | 0:355d86799c43 | 2 | #include "ble/BLE.h" |
hiro99ma | 0:355d86799c43 | 3 | #include "ble_nanoled_service.h" |
hiro99ma | 0:355d86799c43 | 4 | |
hiro99ma | 0:355d86799c43 | 5 | namespace { |
hiro99ma | 0:355d86799c43 | 6 | const char DEVICE_NAME[] = "BLE Nano"; |
hiro99ma | 0:355d86799c43 | 7 | } |
hiro99ma | 0:355d86799c43 | 8 | |
hiro99ma | 0:355d86799c43 | 9 | namespace { |
hiro99ma | 0:355d86799c43 | 10 | BLE mBle; |
hiro99ma | 0:355d86799c43 | 11 | ServiceUart mServiceUart; |
hiro99ma | 0:355d86799c43 | 12 | } |
hiro99ma | 0:355d86799c43 | 13 | |
hiro99ma | 0:355d86799c43 | 14 | |
hiro99ma | 0:355d86799c43 | 15 | namespace { |
hiro99ma | 0:355d86799c43 | 16 | inline int calcAdvertisingIntervalMs(int msec) |
hiro99ma | 0:355d86799c43 | 17 | { |
hiro99ma | 0:355d86799c43 | 18 | return 1000 * msec / 625; |
hiro99ma | 0:355d86799c43 | 19 | } |
hiro99ma | 0:355d86799c43 | 20 | } |
hiro99ma | 0:355d86799c43 | 21 | |
hiro99ma | 0:355d86799c43 | 22 | |
hiro99ma | 0:355d86799c43 | 23 | namespace { |
hiro99ma | 0:355d86799c43 | 24 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
hiro99ma | 0:355d86799c43 | 25 | { |
hiro99ma | 0:355d86799c43 | 26 | mBle.gap().startAdvertising(); |
hiro99ma | 0:355d86799c43 | 27 | } |
hiro99ma | 0:355d86799c43 | 28 | |
hiro99ma | 0:355d86799c43 | 29 | |
hiro99ma | 0:355d86799c43 | 30 | void writtenHandler(const GattWriteCallbackParams* params) |
hiro99ma | 0:355d86799c43 | 31 | { |
hiro99ma | 0:355d86799c43 | 32 | mServiceUart.writtenHandler(mBle, params); |
hiro99ma | 0:355d86799c43 | 33 | } |
hiro99ma | 0:355d86799c43 | 34 | |
hiro99ma | 0:355d86799c43 | 35 | void readHandler(const GattReadCallbackParams* params) |
hiro99ma | 0:355d86799c43 | 36 | { |
hiro99ma | 0:355d86799c43 | 37 | mServiceUart.readHandler(mBle, params); |
hiro99ma | 0:355d86799c43 | 38 | } |
hiro99ma | 0:355d86799c43 | 39 | } |
hiro99ma | 0:355d86799c43 | 40 | |
hiro99ma | 0:355d86799c43 | 41 | |
hiro99ma | 0:355d86799c43 | 42 | int main(void) |
hiro99ma | 0:355d86799c43 | 43 | { |
hiro99ma | 0:355d86799c43 | 44 | mBle.init(); |
hiro99ma | 0:355d86799c43 | 45 | mBle.onDisconnection(disconnectionCallback); |
hiro99ma | 0:355d86799c43 | 46 | mBle.onDataWritten(writtenHandler); |
hiro99ma | 0:355d86799c43 | 47 | mBle.onDataRead(readHandler); |
hiro99ma | 0:355d86799c43 | 48 | |
hiro99ma | 0:355d86799c43 | 49 | //services |
hiro99ma | 0:355d86799c43 | 50 | mServiceUart.addService(mBle); |
hiro99ma | 0:355d86799c43 | 51 | |
hiro99ma | 0:355d86799c43 | 52 | // setup advertising |
hiro99ma | 0:355d86799c43 | 53 | mBle.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
hiro99ma | 0:355d86799c43 | 54 | mBle.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
hiro99ma | 0:355d86799c43 | 55 | mBle.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
hiro99ma | 0:355d86799c43 | 56 | (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1); |
hiro99ma | 0:355d86799c43 | 57 | // 100ms; in multiples of 0.625ms. |
hiro99ma | 0:355d86799c43 | 58 | mBle.setAdvertisingInterval(calcAdvertisingIntervalMs(100)); |
hiro99ma | 0:355d86799c43 | 59 | mBle.startAdvertising(); |
hiro99ma | 0:355d86799c43 | 60 | |
hiro99ma | 0:355d86799c43 | 61 | while (true) { |
hiro99ma | 0:355d86799c43 | 62 | mBle.waitForEvent(); |
hiro99ma | 0:355d86799c43 | 63 | } |
hiro99ma | 0:355d86799c43 | 64 | } |
hiro99ma | 0:355d86799c43 | 65 |