BLE UART example for Nucloe board and Bluetooth LE Nucleo shield.
Dependencies: Nucleo_BLE_API Nucleo_BLE_BlueNRG mbed
Fork of Nucleo_BLE_UART by
main.cpp@1:69e44344edaa, 2014-12-24 (annotated)
- Committer:
- sjallouli
- Date:
- Wed Dec 24 18:05:05 2014 +0000
- Revision:
- 1:69e44344edaa
- Parent:
- 0:3e2ce2bb50b9
Use USER_BUTTON interrupt to send data
; use ble.updateCharacteristicValue() to send data
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sjallouli | 0:3e2ce2bb50b9 | 1 | /* mbed Microcontroller Library |
sjallouli | 0:3e2ce2bb50b9 | 2 | * Copyright (c) 2006-2013 ARM Limited |
sjallouli | 0:3e2ce2bb50b9 | 3 | * |
sjallouli | 0:3e2ce2bb50b9 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sjallouli | 0:3e2ce2bb50b9 | 5 | * you may not use this file except in compliance with the License. |
sjallouli | 0:3e2ce2bb50b9 | 6 | * You may obtain a copy of the License at |
sjallouli | 0:3e2ce2bb50b9 | 7 | * |
sjallouli | 0:3e2ce2bb50b9 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sjallouli | 0:3e2ce2bb50b9 | 9 | * |
sjallouli | 0:3e2ce2bb50b9 | 10 | * Unless required by applicable law or agreed to in writing, software |
sjallouli | 0:3e2ce2bb50b9 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sjallouli | 0:3e2ce2bb50b9 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sjallouli | 0:3e2ce2bb50b9 | 13 | * See the License for the specific language governing permissions and |
sjallouli | 0:3e2ce2bb50b9 | 14 | * limitations under the License. |
sjallouli | 0:3e2ce2bb50b9 | 15 | */ |
sjallouli | 0:3e2ce2bb50b9 | 16 | |
sjallouli | 0:3e2ce2bb50b9 | 17 | #include "mbed.h" |
sjallouli | 0:3e2ce2bb50b9 | 18 | #include "BLEDevice.h" |
sjallouli | 1:69e44344edaa | 19 | #include "DeviceInformationService.h" |
sjallouli | 0:3e2ce2bb50b9 | 20 | #include "UARTService.h" |
sjallouli | 0:3e2ce2bb50b9 | 21 | #include "Utils.h" |
sjallouli | 0:3e2ce2bb50b9 | 22 | |
sjallouli | 1:69e44344edaa | 23 | const static char DEVICE_NAME[] = "BlueNRG_UART"; |
sjallouli | 1:69e44344edaa | 24 | uint8_t c = 'A'; |
sjallouli | 0:3e2ce2bb50b9 | 25 | |
sjallouli | 1:69e44344edaa | 26 | extern bool user_button_pressed; |
sjallouli | 1:69e44344edaa | 27 | bool connected = false; |
sjallouli | 1:69e44344edaa | 28 | bool UpdatedEnabled = false; |
sjallouli | 0:3e2ce2bb50b9 | 29 | |
sjallouli | 0:3e2ce2bb50b9 | 30 | BLEDevice ble; |
sjallouli | 1:69e44344edaa | 31 | UARTService *uartServicePtr; |
sjallouli | 0:3e2ce2bb50b9 | 32 | |
sjallouli | 1:69e44344edaa | 33 | /* Callback called when the device is disconnected */ |
sjallouli | 0:3e2ce2bb50b9 | 34 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
sjallouli | 0:3e2ce2bb50b9 | 35 | { |
sjallouli | 1:69e44344edaa | 36 | DEBUG("Disconnected!\n\r"); |
sjallouli | 1:69e44344edaa | 37 | DEBUG("Restarting the advertising process\n\r"); |
sjallouli | 1:69e44344edaa | 38 | |
sjallouli | 1:69e44344edaa | 39 | ble.startAdvertising(); |
sjallouli | 1:69e44344edaa | 40 | connected = false; |
sjallouli | 0:3e2ce2bb50b9 | 41 | } |
sjallouli | 0:3e2ce2bb50b9 | 42 | |
sjallouli | 1:69e44344edaa | 43 | /* Callback called when the device is connected */ |
sjallouli | 0:3e2ce2bb50b9 | 44 | void connectionCallback(Gap::Handle_t handle, const Gap::ConnectionParams_t *reason) |
sjallouli | 0:3e2ce2bb50b9 | 45 | { |
sjallouli | 0:3e2ce2bb50b9 | 46 | DEBUG("Connected\r\n"); |
sjallouli | 1:69e44344edaa | 47 | |
sjallouli | 0:3e2ce2bb50b9 | 48 | connected = true; |
sjallouli | 0:3e2ce2bb50b9 | 49 | } |
sjallouli | 0:3e2ce2bb50b9 | 50 | |
sjallouli | 1:69e44344edaa | 51 | /* Not working */ |
sjallouli | 1:69e44344edaa | 52 | void onDataSent(unsigned count) |
sjallouli | 1:69e44344edaa | 53 | { |
sjallouli | 1:69e44344edaa | 54 | DEBUG("onDataSent\r\n"); |
sjallouli | 1:69e44344edaa | 55 | } |
sjallouli | 1:69e44344edaa | 56 | |
sjallouli | 1:69e44344edaa | 57 | /* Not working */ |
sjallouli | 0:3e2ce2bb50b9 | 58 | void onDataWritten(const GattCharacteristicWriteCBParams *params) |
sjallouli | 0:3e2ce2bb50b9 | 59 | { |
sjallouli | 1:69e44344edaa | 60 | DEBUG("--onDataWritten\r\n"); |
sjallouli | 1:69e44344edaa | 61 | DEBUG("---charHandle : %d\r\n", params->charHandle); |
sjallouli | 1:69e44344edaa | 62 | DEBUG("---getTXCharacteristicHandle: %d\r\n", uartServicePtr->getTXCharacteristicHandle()); |
sjallouli | 1:69e44344edaa | 63 | DEBUG("---len: %d\r\n", params->len); |
sjallouli | 1:69e44344edaa | 64 | |
sjallouli | 1:69e44344edaa | 65 | if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) |
sjallouli | 1:69e44344edaa | 66 | { |
sjallouli | 1:69e44344edaa | 67 | DEBUG("received %u bytes\n\r", params->len); |
sjallouli | 1:69e44344edaa | 68 | |
sjallouli | 1:69e44344edaa | 69 | if(params->data[0] == 0x00) |
sjallouli | 1:69e44344edaa | 70 | { |
sjallouli | 1:69e44344edaa | 71 | //Do something |
sjallouli | 0:3e2ce2bb50b9 | 72 | } |
sjallouli | 1:69e44344edaa | 73 | else |
sjallouli | 1:69e44344edaa | 74 | { |
sjallouli | 1:69e44344edaa | 75 | //Do something else |
sjallouli | 1:69e44344edaa | 76 | } |
sjallouli | 1:69e44344edaa | 77 | } |
sjallouli | 1:69e44344edaa | 78 | } |
sjallouli | 1:69e44344edaa | 79 | |
sjallouli | 1:69e44344edaa | 80 | /* Callback called when the client enables updates */ |
sjallouli | 1:69e44344edaa | 81 | void onUpdatesEnabled(uint16_t attributeHandle) |
sjallouli | 1:69e44344edaa | 82 | { |
sjallouli | 1:69e44344edaa | 83 | DEBUG("**onUpdatesEnabled**\r\n"); |
sjallouli | 1:69e44344edaa | 84 | UpdatedEnabled = true; |
sjallouli | 1:69e44344edaa | 85 | } |
sjallouli | 1:69e44344edaa | 86 | |
sjallouli | 1:69e44344edaa | 87 | /* Callback called when the client disable updates */ |
sjallouli | 1:69e44344edaa | 88 | void onUpdatesDisabled(uint16_t attributeHandle) |
sjallouli | 1:69e44344edaa | 89 | { |
sjallouli | 1:69e44344edaa | 90 | DEBUG("**onUpdatesDisabled**\r\n"); |
sjallouli | 1:69e44344edaa | 91 | UpdatedEnabled = false; |
sjallouli | 0:3e2ce2bb50b9 | 92 | } |
sjallouli | 0:3e2ce2bb50b9 | 93 | |
sjallouli | 1:69e44344edaa | 94 | /* Main */ |
sjallouli | 0:3e2ce2bb50b9 | 95 | int main(void) |
sjallouli | 0:3e2ce2bb50b9 | 96 | { |
sjallouli | 1:69e44344edaa | 97 | DEBUG("Initialising \n\r"); |
sjallouli | 1:69e44344edaa | 98 | ble.init(); |
sjallouli | 1:69e44344edaa | 99 | #if 1 |
sjallouli | 1:69e44344edaa | 100 | /* Set callback functions */ |
sjallouli | 1:69e44344edaa | 101 | ble.onDisconnection(disconnectionCallback); |
sjallouli | 1:69e44344edaa | 102 | ble.onConnection(connectionCallback); |
sjallouli | 1:69e44344edaa | 103 | ble.onDataWritten(onDataWritten); |
sjallouli | 1:69e44344edaa | 104 | ble.onDataSent(onDataSent); |
sjallouli | 1:69e44344edaa | 105 | ble.onUpdatesEnabled(onUpdatesEnabled); |
sjallouli | 1:69e44344edaa | 106 | ble.onUpdatesDisabled(onUpdatesDisabled); |
sjallouli | 0:3e2ce2bb50b9 | 107 | |
sjallouli | 1:69e44344edaa | 108 | DeviceInformationService deviceInfo(ble, "ST", "Nucleo", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); |
sjallouli | 1:69e44344edaa | 109 | /* setup advertising */ |
sjallouli | 1:69e44344edaa | 110 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
sjallouli | 1:69e44344edaa | 111 | ble.setAdvertisingType (GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
sjallouli | 1:69e44344edaa | 112 | |
sjallouli | 1:69e44344edaa | 113 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME , (const uint8_t *)"BlueNRG_UART" , sizeof("BlueNRG_UART") - 1); |
sjallouli | 1:69e44344edaa | 114 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
sjallouli | 1:69e44344edaa | 115 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME , (uint8_t *)DEVICE_NAME , sizeof(DEVICE_NAME)); |
sjallouli | 1:69e44344edaa | 116 | |
sjallouli | 1:69e44344edaa | 117 | /* Start advertising */ |
sjallouli | 1:69e44344edaa | 118 | ble.setAdvertisingInterval(160); |
sjallouli | 1:69e44344edaa | 119 | ble.startAdvertising(); |
sjallouli | 0:3e2ce2bb50b9 | 120 | |
sjallouli | 1:69e44344edaa | 121 | UARTService uartService(ble); |
sjallouli | 1:69e44344edaa | 122 | uartServicePtr = &uartService; |
sjallouli | 1:69e44344edaa | 123 | |
sjallouli | 1:69e44344edaa | 124 | |
sjallouli | 1:69e44344edaa | 125 | while (true) |
sjallouli | 1:69e44344edaa | 126 | { |
sjallouli | 1:69e44344edaa | 127 | ble.waitForEvent(); |
sjallouli | 1:69e44344edaa | 128 | |
sjallouli | 1:69e44344edaa | 129 | if(connected == true) |
sjallouli | 0:3e2ce2bb50b9 | 130 | { |
sjallouli | 1:69e44344edaa | 131 | if ((user_button_pressed == true) && (UpdatedEnabled == true)) |
sjallouli | 1:69e44344edaa | 132 | { |
sjallouli | 1:69e44344edaa | 133 | user_button_pressed = false; |
sjallouli | 1:69e44344edaa | 134 | DEBUG("Current Char: %c\r\n",c); |
sjallouli | 1:69e44344edaa | 135 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), &c, 1); |
sjallouli | 1:69e44344edaa | 136 | |
sjallouli | 1:69e44344edaa | 137 | c++; |
sjallouli | 1:69e44344edaa | 138 | |
sjallouli | 1:69e44344edaa | 139 | if(c == ('Z'+1)) |
sjallouli | 0:3e2ce2bb50b9 | 140 | { |
sjallouli | 1:69e44344edaa | 141 | c = 'A'; |
sjallouli | 0:3e2ce2bb50b9 | 142 | } |
sjallouli | 1:69e44344edaa | 143 | } |
sjallouli | 0:3e2ce2bb50b9 | 144 | } |
sjallouli | 1:69e44344edaa | 145 | } |
sjallouli | 1:69e44344edaa | 146 | #else |
sjallouli | 1:69e44344edaa | 147 | while (true) |
sjallouli | 1:69e44344edaa | 148 | { |
sjallouli | 1:69e44344edaa | 149 | if (user_button_pressed == true) |
sjallouli | 1:69e44344edaa | 150 | { |
sjallouli | 1:69e44344edaa | 151 | user_button_pressed = false; |
sjallouli | 1:69e44344edaa | 152 | DEBUG("Current Char: %c\r\n",c); |
sjallouli | 1:69e44344edaa | 153 | |
sjallouli | 1:69e44344edaa | 154 | c++; |
sjallouli | 1:69e44344edaa | 155 | |
sjallouli | 1:69e44344edaa | 156 | if(c == ('Z'+1)) |
sjallouli | 1:69e44344edaa | 157 | { |
sjallouli | 1:69e44344edaa | 158 | c = 'A'; |
sjallouli | 1:69e44344edaa | 159 | } |
sjallouli | 1:69e44344edaa | 160 | } |
sjallouli | 1:69e44344edaa | 161 | } |
sjallouli | 1:69e44344edaa | 162 | #endif |
sjallouli | 0:3e2ce2bb50b9 | 163 | } |