This project allows IDB0XA1 shields to open up a serial communication channel with nRF BLE phone apps.
Dependencies: BLE_API_modified_UARTService_for_IDB0XA1_nRF_UART BLE_Observer_Nucleo X_NUCLEO_IDB0XA1 mbed
Fork of BLE_Observer_Nucleo by
main.cpp@1:2d5632f16714, 2018-07-31 (annotated)
- Committer:
- TianyouTong
- Date:
- Tue Jul 31 11:14:25 2018 +0000
- Revision:
- 1:2d5632f16714
- Parent:
- 0:39b311448c9e
Published new library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vcoubard | 0:39b311448c9e | 1 | /* mbed Microcontroller Library |
TianyouTong | 1:2d5632f16714 | 2 | * Copyright (c) 2006-2013 ARM Limited |
vcoubard | 0:39b311448c9e | 3 | * |
vcoubard | 0:39b311448c9e | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
vcoubard | 0:39b311448c9e | 5 | * you may not use this file except in compliance with the License. |
vcoubard | 0:39b311448c9e | 6 | * You may obtain a copy of the License at |
vcoubard | 0:39b311448c9e | 7 | * |
vcoubard | 0:39b311448c9e | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
vcoubard | 0:39b311448c9e | 9 | * |
vcoubard | 0:39b311448c9e | 10 | * Unless required by applicable law or agreed to in writing, software |
vcoubard | 0:39b311448c9e | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
vcoubard | 0:39b311448c9e | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
vcoubard | 0:39b311448c9e | 13 | * See the License for the specific language governing permissions and |
vcoubard | 0:39b311448c9e | 14 | * limitations under the License. |
vcoubard | 0:39b311448c9e | 15 | */ |
TianyouTong | 1:2d5632f16714 | 16 | |
TianyouTong | 1:2d5632f16714 | 17 | #include <string.h> |
vcoubard | 0:39b311448c9e | 18 | #include "mbed.h" |
vcoubard | 0:39b311448c9e | 19 | #include "BLE.h" |
TianyouTong | 1:2d5632f16714 | 20 | |
TianyouTong | 1:2d5632f16714 | 21 | #include "UARTService.h" |
TianyouTong | 1:2d5632f16714 | 22 | #include "BatteryService.h" |
TianyouTong | 1:2d5632f16714 | 23 | #include "DeviceInformationService.h" |
TianyouTong | 1:2d5632f16714 | 24 | static const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE, |
TianyouTong | 1:2d5632f16714 | 25 | GattService::UUID_DEVICE_INFORMATION_SERVICE}; |
vcoubard | 0:39b311448c9e | 26 | |
TianyouTong | 1:2d5632f16714 | 27 | Serial pc(USBTX, USBRX); |
TianyouTong | 1:2d5632f16714 | 28 | BLEDevice ble; |
TianyouTong | 1:2d5632f16714 | 29 | |
TianyouTong | 1:2d5632f16714 | 30 | //Used to access the defined in main UARTService object globally. |
TianyouTong | 1:2d5632f16714 | 31 | UARTService *uart; |
vcoubard | 0:39b311448c9e | 32 | |
TianyouTong | 1:2d5632f16714 | 33 | //Keeping a receive buffer to alter the data received. |
TianyouTong | 1:2d5632f16714 | 34 | uint8_t DatatoSend[1000]; |
TianyouTong | 1:2d5632f16714 | 35 | |
TianyouTong | 1:2d5632f16714 | 36 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
TianyouTong | 1:2d5632f16714 | 37 | { |
TianyouTong | 1:2d5632f16714 | 38 | pc.printf("\n Disconnected.!!\n"); |
TianyouTong | 1:2d5632f16714 | 39 | pc.printf("\n Restarting the advertising process.\n"); |
TianyouTong | 1:2d5632f16714 | 40 | ble.startAdvertising(); |
TianyouTong | 1:2d5632f16714 | 41 | } |
TianyouTong | 1:2d5632f16714 | 42 | |
vcoubard | 0:39b311448c9e | 43 | void periodicCallback(void) |
vcoubard | 0:39b311448c9e | 44 | { |
vcoubard | 0:39b311448c9e | 45 | } |
vcoubard | 0:39b311448c9e | 46 | |
TianyouTong | 1:2d5632f16714 | 47 | void onDataWritten(const GattWriteCallbackParams *params) |
TianyouTong | 1:2d5632f16714 | 48 | { |
TianyouTong | 1:2d5632f16714 | 49 | /*if received something, print it out on PC screen*/ |
TianyouTong | 1:2d5632f16714 | 50 | if ((uart != NULL) && (params->handle == uart->getTXCharacteristicHandle())) { |
TianyouTong | 1:2d5632f16714 | 51 | uint16_t bytesRead = params->len; |
TianyouTong | 1:2d5632f16714 | 52 | |
TianyouTong | 1:2d5632f16714 | 53 | pc.printf("Data Received\n"); |
TianyouTong | 1:2d5632f16714 | 54 | /*for all received data, send a copy back to the BLE central(in this case the phone app)*/ |
TianyouTong | 1:2d5632f16714 | 55 | for(int j=0;j<bytesRead;j++) |
TianyouTong | 1:2d5632f16714 | 56 | { |
TianyouTong | 1:2d5632f16714 | 57 | pc.printf(" %x\n",*((params->data)+j)); |
TianyouTong | 1:2d5632f16714 | 58 | DatatoSend[j]=(*((params->data)+j)); |
TianyouTong | 1:2d5632f16714 | 59 | } |
TianyouTong | 1:2d5632f16714 | 60 | |
TianyouTong | 1:2d5632f16714 | 61 | wait(1); |
TianyouTong | 1:2d5632f16714 | 62 | ble.updateCharacteristicValue(uart->getRXCharacteristicHandle(), DatatoSend, bytesRead); |
TianyouTong | 1:2d5632f16714 | 63 | //Use the below statement for loopback. |
TianyouTong | 1:2d5632f16714 | 64 | //ble.updateCharacteristicValue(uart->getRXCharacteristicHandle(), params->data, bytesRead); |
TianyouTong | 1:2d5632f16714 | 65 | |
TianyouTong | 1:2d5632f16714 | 66 | /*print out what have just been sent*/ |
TianyouTong | 1:2d5632f16714 | 67 | pc.printf("Data Sent\n"); |
TianyouTong | 1:2d5632f16714 | 68 | for(int j=0;j<bytesRead;j++) |
TianyouTong | 1:2d5632f16714 | 69 | { |
TianyouTong | 1:2d5632f16714 | 70 | pc.printf(" %x\n",DatatoSend[j]); |
TianyouTong | 1:2d5632f16714 | 71 | } |
TianyouTong | 1:2d5632f16714 | 72 | wait(1); |
vcoubard | 0:39b311448c9e | 73 | } |
vcoubard | 0:39b311448c9e | 74 | } |
vcoubard | 0:39b311448c9e | 75 | |
vcoubard | 0:39b311448c9e | 76 | int main(void) |
vcoubard | 0:39b311448c9e | 77 | { |
TianyouTong | 1:2d5632f16714 | 78 | |
TianyouTong | 1:2d5632f16714 | 79 | pc.baud(9600); |
TianyouTong | 1:2d5632f16714 | 80 | pc.printf("Hello, starting BlueNRG Serial Console Demo!\n"); |
vcoubard | 0:39b311448c9e | 81 | Ticker ticker; |
vcoubard | 0:39b311448c9e | 82 | ticker.attach(periodicCallback, 1); |
TianyouTong | 1:2d5632f16714 | 83 | |
TianyouTong | 1:2d5632f16714 | 84 | pc.printf("Initialising the module\n\r"); |
vcoubard | 0:39b311448c9e | 85 | ble.init(); |
TianyouTong | 1:2d5632f16714 | 86 | ble.onDisconnection(disconnectionCallback); |
TianyouTong | 1:2d5632f16714 | 87 | ble.onDataWritten(onDataWritten); |
TianyouTong | 1:2d5632f16714 | 88 | |
TianyouTong | 1:2d5632f16714 | 89 | UARTService uartService(ble); |
TianyouTong | 1:2d5632f16714 | 90 | uart = &uartService; |
TianyouTong | 1:2d5632f16714 | 91 | |
TianyouTong | 1:2d5632f16714 | 92 | BatteryService battery(ble); |
TianyouTong | 1:2d5632f16714 | 93 | DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); |
TianyouTong | 1:2d5632f16714 | 94 | |
TianyouTong | 1:2d5632f16714 | 95 | /* setup advertising */ |
TianyouTong | 1:2d5632f16714 | 96 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
TianyouTong | 1:2d5632f16714 | 97 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
TianyouTong | 1:2d5632f16714 | 98 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
TianyouTong | 1:2d5632f16714 | 99 | (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); |
TianyouTong | 1:2d5632f16714 | 100 | ble.accumulateAdvertisingPayload(GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS, |
TianyouTong | 1:2d5632f16714 | 101 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
TianyouTong | 1:2d5632f16714 | 102 | ble.accumulateAdvertisingPayload(GapAdvertisingData::INCOMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
TianyouTong | 1:2d5632f16714 | 103 | ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
TianyouTong | 1:2d5632f16714 | 104 | ble.startAdvertising(); |
TianyouTong | 1:2d5632f16714 | 105 | pc.printf("Start Advertising.\n"); |
TianyouTong | 1:2d5632f16714 | 106 | |
vcoubard | 0:39b311448c9e | 107 | while (true) { |
vcoubard | 0:39b311448c9e | 108 | ble.waitForEvent(); |
vcoubard | 0:39b311448c9e | 109 | } |
vcoubard | 0:39b311448c9e | 110 | } |