
srdfg
Dependencies: BLE_API mbed nRF51822
main.cpp@0:47106f7ef0c2, 2017-01-09 (annotated)
- Committer:
- muratdemirtas
- Date:
- Mon Jan 09 14:35:18 2017 +0000
- Revision:
- 0:47106f7ef0c2
kk;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
muratdemirtas | 0:47106f7ef0c2 | 1 | /* mbed Microcontroller Library |
muratdemirtas | 0:47106f7ef0c2 | 2 | * Copyright (c) 2006-2013 ARM Limited |
muratdemirtas | 0:47106f7ef0c2 | 3 | * |
muratdemirtas | 0:47106f7ef0c2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
muratdemirtas | 0:47106f7ef0c2 | 5 | * you may not use this file except in compliance with the License. |
muratdemirtas | 0:47106f7ef0c2 | 6 | * You may obtain a copy of the License at |
muratdemirtas | 0:47106f7ef0c2 | 7 | * |
muratdemirtas | 0:47106f7ef0c2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
muratdemirtas | 0:47106f7ef0c2 | 9 | * |
muratdemirtas | 0:47106f7ef0c2 | 10 | * Unless required by applicable law or agreed to in writing, software |
muratdemirtas | 0:47106f7ef0c2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
muratdemirtas | 0:47106f7ef0c2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
muratdemirtas | 0:47106f7ef0c2 | 13 | * See the License for the specific language governing permissions and |
muratdemirtas | 0:47106f7ef0c2 | 14 | * limitations under the License. |
muratdemirtas | 0:47106f7ef0c2 | 15 | */ |
muratdemirtas | 0:47106f7ef0c2 | 16 | |
muratdemirtas | 0:47106f7ef0c2 | 17 | #include "mbed.h" |
muratdemirtas | 0:47106f7ef0c2 | 18 | #include "ble/BLE.h" |
muratdemirtas | 0:47106f7ef0c2 | 19 | |
muratdemirtas | 0:47106f7ef0c2 | 20 | #include "ble/services/UARTService.h" |
muratdemirtas | 0:47106f7ef0c2 | 21 | |
muratdemirtas | 0:47106f7ef0c2 | 22 | #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; |
muratdemirtas | 0:47106f7ef0c2 | 23 | * it will have an impact on code-size and power consumption. */ |
muratdemirtas | 0:47106f7ef0c2 | 24 | |
muratdemirtas | 0:47106f7ef0c2 | 25 | #if NEED_CONSOLE_OUTPUT |
muratdemirtas | 0:47106f7ef0c2 | 26 | #define DEBUG(...) { printf(__VA_ARGS__); } |
muratdemirtas | 0:47106f7ef0c2 | 27 | #else |
muratdemirtas | 0:47106f7ef0c2 | 28 | #define DEBUG(...) /* nothing */ |
muratdemirtas | 0:47106f7ef0c2 | 29 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
muratdemirtas | 0:47106f7ef0c2 | 30 | |
muratdemirtas | 0:47106f7ef0c2 | 31 | BLEDevice ble; |
muratdemirtas | 0:47106f7ef0c2 | 32 | DigitalOut led1(LED1); |
muratdemirtas | 0:47106f7ef0c2 | 33 | |
muratdemirtas | 0:47106f7ef0c2 | 34 | UARTService *uartServicePtr; |
muratdemirtas | 0:47106f7ef0c2 | 35 | |
muratdemirtas | 0:47106f7ef0c2 | 36 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
muratdemirtas | 0:47106f7ef0c2 | 37 | { |
muratdemirtas | 0:47106f7ef0c2 | 38 | DEBUG("Disconnected!\n\r"); |
muratdemirtas | 0:47106f7ef0c2 | 39 | DEBUG("Restarting the advertising process\n\r"); |
muratdemirtas | 0:47106f7ef0c2 | 40 | ble.startAdvertising(); |
muratdemirtas | 0:47106f7ef0c2 | 41 | } |
muratdemirtas | 0:47106f7ef0c2 | 42 | |
muratdemirtas | 0:47106f7ef0c2 | 43 | void onDataWritten(const GattWriteCallbackParams *params) |
muratdemirtas | 0:47106f7ef0c2 | 44 | { |
muratdemirtas | 0:47106f7ef0c2 | 45 | if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) { |
muratdemirtas | 0:47106f7ef0c2 | 46 | uint16_t bytesRead = params->len; |
muratdemirtas | 0:47106f7ef0c2 | 47 | DEBUG("received %u bytes\n\r", bytesRead); |
muratdemirtas | 0:47106f7ef0c2 | 48 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); |
muratdemirtas | 0:47106f7ef0c2 | 49 | } |
muratdemirtas | 0:47106f7ef0c2 | 50 | } |
muratdemirtas | 0:47106f7ef0c2 | 51 | |
muratdemirtas | 0:47106f7ef0c2 | 52 | void periodicCallback(void) |
muratdemirtas | 0:47106f7ef0c2 | 53 | { |
muratdemirtas | 0:47106f7ef0c2 | 54 | led1 = !led1; |
muratdemirtas | 0:47106f7ef0c2 | 55 | } |
muratdemirtas | 0:47106f7ef0c2 | 56 | |
muratdemirtas | 0:47106f7ef0c2 | 57 | int main(void) |
muratdemirtas | 0:47106f7ef0c2 | 58 | { |
muratdemirtas | 0:47106f7ef0c2 | 59 | led1 = 1; |
muratdemirtas | 0:47106f7ef0c2 | 60 | Ticker ticker; |
muratdemirtas | 0:47106f7ef0c2 | 61 | ticker.attach(periodicCallback, 1); |
muratdemirtas | 0:47106f7ef0c2 | 62 | |
muratdemirtas | 0:47106f7ef0c2 | 63 | DEBUG("Initialising the nRF51822\n\r"); |
muratdemirtas | 0:47106f7ef0c2 | 64 | ble.init(); |
muratdemirtas | 0:47106f7ef0c2 | 65 | ble.onDisconnection(disconnectionCallback); |
muratdemirtas | 0:47106f7ef0c2 | 66 | ble.onDataWritten(onDataWritten); |
muratdemirtas | 0:47106f7ef0c2 | 67 | |
muratdemirtas | 0:47106f7ef0c2 | 68 | /* setup advertising */ |
muratdemirtas | 0:47106f7ef0c2 | 69 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
muratdemirtas | 0:47106f7ef0c2 | 70 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
muratdemirtas | 0:47106f7ef0c2 | 71 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
muratdemirtas | 0:47106f7ef0c2 | 72 | (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); |
muratdemirtas | 0:47106f7ef0c2 | 73 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
muratdemirtas | 0:47106f7ef0c2 | 74 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
muratdemirtas | 0:47106f7ef0c2 | 75 | |
muratdemirtas | 0:47106f7ef0c2 | 76 | ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */ |
muratdemirtas | 0:47106f7ef0c2 | 77 | ble.startAdvertising(); |
muratdemirtas | 0:47106f7ef0c2 | 78 | |
muratdemirtas | 0:47106f7ef0c2 | 79 | UARTService uartService(ble); |
muratdemirtas | 0:47106f7ef0c2 | 80 | uartServicePtr = &uartService; |
muratdemirtas | 0:47106f7ef0c2 | 81 | |
muratdemirtas | 0:47106f7ef0c2 | 82 | while (true) { |
muratdemirtas | 0:47106f7ef0c2 | 83 | ble.waitForEvent(); |
muratdemirtas | 0:47106f7ef0c2 | 84 | } |
muratdemirtas | 0:47106f7ef0c2 | 85 | } |