Team HUB / Mbed 2 deprecated nRF51822_Fresh

Dependencies:   BLE_API mbed nRF51822

Fork of nRF51822_Updated by Nicholas Hazen

Committer:
jck125
Date:
Mon Jul 27 18:06:26 2015 +0000
Revision:
3:d67fc41e20e2
Parent:
2:4b66b69c7ecb
Child:
4:2f9c7d53a099
SimpleChat with updated libraries

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RedBearLab 1:1c058e553423 1 /*
RedBearLab 0:cffe8ac1bdf0 2
RedBearLab 1:1c058e553423 3 Copyright (c) 2012-2014 RedBearLab
RedBearLab 1:1c058e553423 4
RedBearLab 1:1c058e553423 5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
RedBearLab 1:1c058e553423 6 and associated documentation files (the "Software"), to deal in the Software without restriction,
RedBearLab 1:1c058e553423 7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
RedBearLab 1:1c058e553423 8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
RedBearLab 1:1c058e553423 9 subject to the following conditions:
RedBearLab 1:1c058e553423 10 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
RedBearLab 1:1c058e553423 11
RedBearLab 1:1c058e553423 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
RedBearLab 1:1c058e553423 13 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
RedBearLab 1:1c058e553423 14 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
RedBearLab 1:1c058e553423 15 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
RedBearLab 1:1c058e553423 16 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
RedBearLab 1:1c058e553423 17
RedBearLab 1:1c058e553423 18 */
RedBearLab 1:1c058e553423 19
RedBearLab 1:1c058e553423 20 /*
RedBearLab 1:1c058e553423 21 * The application works with the BLEController iOS/Android App.
RedBearLab 1:1c058e553423 22 * Type something from the Terminal to send
RedBearLab 1:1c058e553423 23 * to the BLEController App or vice verse.
RedBearLab 1:1c058e553423 24 * Characteristics received from App will print on Terminal.
RedBearLab 1:1c058e553423 25 */
RedBearLab 1:1c058e553423 26
RedBearLab 0:cffe8ac1bdf0 27 #include "mbed.h"
RedBearLab 2:4b66b69c7ecb 28 #include "ble/BLE.h"
RedBearLab 0:cffe8ac1bdf0 29
RedBearLab 0:cffe8ac1bdf0 30
RedBearLab 0:cffe8ac1bdf0 31 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
RedBearLab 0:cffe8ac1bdf0 32 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
RedBearLab 0:cffe8ac1bdf0 33 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
RedBearLab 0:cffe8ac1bdf0 34
RedBearLab 0:cffe8ac1bdf0 35 #define TXRX_BUF_LEN 20
RedBearLab 0:cffe8ac1bdf0 36
jck125 3:d67fc41e20e2 37 #define MIN_CONN_INTERVAL 6
jck125 3:d67fc41e20e2 38 #define MAX_CONN_INTERVAL 10
jck125 3:d67fc41e20e2 39 #define SLAVE_LATENCY 4
jck125 3:d67fc41e20e2 40 #define CONN_SUP_TIMEOUT 6000
jck125 3:d67fc41e20e2 41
jck125 3:d67fc41e20e2 42 BLE ble;
RedBearLab 0:cffe8ac1bdf0 43
RedBearLab 0:cffe8ac1bdf0 44 Serial pc(USBTX, USBRX);
RedBearLab 0:cffe8ac1bdf0 45
RedBearLab 0:cffe8ac1bdf0 46
RedBearLab 0:cffe8ac1bdf0 47 // The Nordic UART Service
RedBearLab 0:cffe8ac1bdf0 48 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:cffe8ac1bdf0 49 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:cffe8ac1bdf0 50 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
RedBearLab 0:cffe8ac1bdf0 51 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
RedBearLab 0:cffe8ac1bdf0 52
RedBearLab 0:cffe8ac1bdf0 53
RedBearLab 0:cffe8ac1bdf0 54 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 55 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
RedBearLab 0:cffe8ac1bdf0 56
RedBearLab 0:cffe8ac1bdf0 57 static uint8_t rx_buf[TXRX_BUF_LEN];
RedBearLab 0:cffe8ac1bdf0 58 static uint8_t rx_len=0;
RedBearLab 0:cffe8ac1bdf0 59
jck125 3:d67fc41e20e2 60 uint8_t packet1[] = {0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34};
jck125 3:d67fc41e20e2 61 uint8_t packet2[] = {0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48};
jck125 3:d67fc41e20e2 62 uint8_t packet3[] = {0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c};
jck125 3:d67fc41e20e2 63
jck125 3:d67fc41e20e2 64 Timer t;
RedBearLab 0:cffe8ac1bdf0 65
RedBearLab 0:cffe8ac1bdf0 66 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
jck125 3:d67fc41e20e2 67
RedBearLab 0:cffe8ac1bdf0 68 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
jck125 3:d67fc41e20e2 69
RedBearLab 0:cffe8ac1bdf0 70 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
RedBearLab 0:cffe8ac1bdf0 71
RedBearLab 0:cffe8ac1bdf0 72 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
RedBearLab 0:cffe8ac1bdf0 73
jck125 3:d67fc41e20e2 74 int i = 0;
RedBearLab 0:cffe8ac1bdf0 75
RedBearLab 0:cffe8ac1bdf0 76 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
RedBearLab 0:cffe8ac1bdf0 77 {
RedBearLab 0:cffe8ac1bdf0 78 pc.printf("Disconnected \r\n");
RedBearLab 0:cffe8ac1bdf0 79 pc.printf("Restart advertising \r\n");
RedBearLab 0:cffe8ac1bdf0 80 ble.startAdvertising();
RedBearLab 0:cffe8ac1bdf0 81 }
RedBearLab 0:cffe8ac1bdf0 82
RedBearLab 2:4b66b69c7ecb 83 void WrittenHandler(const GattWriteCallbackParams *Handler)
RedBearLab 0:cffe8ac1bdf0 84 {
RedBearLab 0:cffe8ac1bdf0 85 uint8_t buf[TXRX_BUF_LEN];
RedBearLab 0:cffe8ac1bdf0 86 uint16_t bytesRead, index;
RedBearLab 0:cffe8ac1bdf0 87
RedBearLab 2:4b66b69c7ecb 88 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
RedBearLab 0:cffe8ac1bdf0 89 {
jck125 3:d67fc41e20e2 90 pc.printf("Time before packet received in seconds: %f\n", t.read());
RedBearLab 0:cffe8ac1bdf0 91 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
jck125 3:d67fc41e20e2 92
jck125 3:d67fc41e20e2 93 t.stop();
jck125 3:d67fc41e20e2 94 pc.printf("Time in seconds: %f\n", t.read());
jck125 3:d67fc41e20e2 95
jck125 3:d67fc41e20e2 96 bytesRead = 20;
RedBearLab 0:cffe8ac1bdf0 97 memset(txPayload, 0, TXRX_BUF_LEN);
jck125 3:d67fc41e20e2 98 memcpy(txPayload, buf, TXRX_BUF_LEN);
RedBearLab 0:cffe8ac1bdf0 99 pc.printf("WriteHandler \r\n");
RedBearLab 0:cffe8ac1bdf0 100 pc.printf("Length: ");
RedBearLab 0:cffe8ac1bdf0 101 pc.putc(bytesRead);
RedBearLab 0:cffe8ac1bdf0 102 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 103 pc.printf("Data: ");
jck125 3:d67fc41e20e2 104 for(index=0; index<bytesRead; index++) {
jck125 3:d67fc41e20e2 105 pc.putc((char)txPayload[index]);
RedBearLab 0:cffe8ac1bdf0 106 }
RedBearLab 0:cffe8ac1bdf0 107 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 108 }
RedBearLab 0:cffe8ac1bdf0 109 }
RedBearLab 0:cffe8ac1bdf0 110
RedBearLab 0:cffe8ac1bdf0 111 void uartCB(void)
RedBearLab 0:cffe8ac1bdf0 112 {
RedBearLab 0:cffe8ac1bdf0 113 while(pc.readable())
RedBearLab 0:cffe8ac1bdf0 114 {
RedBearLab 0:cffe8ac1bdf0 115 rx_buf[rx_len++] = pc.getc();
RedBearLab 0:cffe8ac1bdf0 116 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
RedBearLab 0:cffe8ac1bdf0 117 {
RedBearLab 0:cffe8ac1bdf0 118 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
RedBearLab 0:cffe8ac1bdf0 119 pc.printf("RecHandler \r\n");
RedBearLab 0:cffe8ac1bdf0 120 pc.printf("Length: ");
RedBearLab 0:cffe8ac1bdf0 121 pc.putc(rx_len);
RedBearLab 0:cffe8ac1bdf0 122 pc.printf("\r\n");
RedBearLab 0:cffe8ac1bdf0 123 rx_len = 0;
RedBearLab 0:cffe8ac1bdf0 124 break;
RedBearLab 0:cffe8ac1bdf0 125 }
RedBearLab 0:cffe8ac1bdf0 126 }
RedBearLab 0:cffe8ac1bdf0 127 }
RedBearLab 0:cffe8ac1bdf0 128
jck125 3:d67fc41e20e2 129 void writePackets(void)
jck125 3:d67fc41e20e2 130 {
jck125 3:d67fc41e20e2 131 /*
jck125 3:d67fc41e20e2 132 while(pc.readable())
jck125 3:d67fc41e20e2 133 {
jck125 3:d67fc41e20e2 134 rx_buf[rx_len++] = pc.getc();
jck125 3:d67fc41e20e2 135 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
jck125 3:d67fc41e20e2 136 {
jck125 3:d67fc41e20e2 137 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
jck125 3:d67fc41e20e2 138 pc.printf("RecHandler \r\n");
jck125 3:d67fc41e20e2 139 pc.printf("Length: ");
jck125 3:d67fc41e20e2 140 pc.putc(rx_len);
jck125 3:d67fc41e20e2 141 pc.printf("\r\n");
jck125 3:d67fc41e20e2 142 rx_len = 0;
jck125 3:d67fc41e20e2 143 break;
jck125 3:d67fc41e20e2 144 }
jck125 3:d67fc41e20e2 145 }
jck125 3:d67fc41e20e2 146 */
jck125 3:d67fc41e20e2 147
jck125 3:d67fc41e20e2 148 pc.printf("Updating Characteristic... \r\n");
jck125 3:d67fc41e20e2 149
jck125 3:d67fc41e20e2 150 wait(1);
jck125 3:d67fc41e20e2 151
jck125 3:d67fc41e20e2 152 t.start();
jck125 3:d67fc41e20e2 153
jck125 3:d67fc41e20e2 154 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), packet1, 20);
jck125 3:d67fc41e20e2 155 pc.printf("Time after first packet sent in seconds: %f\n", t.read());
jck125 3:d67fc41e20e2 156 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), packet2, 20);
jck125 3:d67fc41e20e2 157 pc.printf("Time after second packet sent in seconds: %f\n", t.read());
jck125 3:d67fc41e20e2 158 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), packet3, 20);
jck125 3:d67fc41e20e2 159 pc.printf("Time after third packet sent in seconds: %f\n", t.read());
jck125 3:d67fc41e20e2 160 }
jck125 3:d67fc41e20e2 161
RedBearLab 0:cffe8ac1bdf0 162 int main(void)
RedBearLab 0:cffe8ac1bdf0 163 {
RedBearLab 0:cffe8ac1bdf0 164 ble.init();
RedBearLab 0:cffe8ac1bdf0 165 ble.onDisconnection(disconnectionCallback);
RedBearLab 0:cffe8ac1bdf0 166 ble.onDataWritten(WrittenHandler);
RedBearLab 0:cffe8ac1bdf0 167
RedBearLab 0:cffe8ac1bdf0 168 pc.baud(9600);
RedBearLab 0:cffe8ac1bdf0 169 pc.printf("SimpleChat Init \r\n");
RedBearLab 0:cffe8ac1bdf0 170
RedBearLab 0:cffe8ac1bdf0 171 pc.attach( uartCB , pc.RxIrq);
RedBearLab 0:cffe8ac1bdf0 172 // setup advertising
RedBearLab 0:cffe8ac1bdf0 173 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
RedBearLab 0:cffe8ac1bdf0 174 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
RedBearLab 0:cffe8ac1bdf0 175 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
jck125 3:d67fc41e20e2 176 (const uint8_t *)"Megatron", sizeof("Megatron") - 1);
RedBearLab 0:cffe8ac1bdf0 177 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
RedBearLab 0:cffe8ac1bdf0 178 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
RedBearLab 0:cffe8ac1bdf0 179 // 100ms; in multiples of 0.625ms.
RedBearLab 0:cffe8ac1bdf0 180 ble.setAdvertisingInterval(160);
RedBearLab 0:cffe8ac1bdf0 181
RedBearLab 0:cffe8ac1bdf0 182 ble.addService(uartService);
RedBearLab 0:cffe8ac1bdf0 183
RedBearLab 0:cffe8ac1bdf0 184 ble.startAdvertising();
RedBearLab 0:cffe8ac1bdf0 185 pc.printf("Advertising Start \r\n");
RedBearLab 0:cffe8ac1bdf0 186
jck125 3:d67fc41e20e2 187 while(1)
RedBearLab 0:cffe8ac1bdf0 188 {
jck125 3:d67fc41e20e2 189 ble.waitForEvent();
RedBearLab 0:cffe8ac1bdf0 190
jck125 3:d67fc41e20e2 191 if(i == 0)
jck125 3:d67fc41e20e2 192 {
jck125 3:d67fc41e20e2 193 writePackets();
jck125 3:d67fc41e20e2 194 i++;
jck125 3:d67fc41e20e2 195 }
jck125 3:d67fc41e20e2 196 }
jck125 3:d67fc41e20e2 197 }