Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed nRF51822
Fork of nRF51822_Updated by
main.cpp
00001 /* 00002 00003 Copyright (c) 2012-2014 RedBearLab 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00006 and associated documentation files (the "Software"), to deal in the Software without restriction, 00007 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 00008 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 00009 subject to the following conditions: 00010 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 00011 00012 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 00013 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 00014 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 00015 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00016 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 00018 */ 00019 00020 /* 00021 * The application works with the BLEController iOS/Android App. 00022 * Type something from the Terminal to send 00023 * to the BLEController App or vice verse. 00024 * Characteristics received from App will print on Terminal. 00025 */ 00026 00027 #include "mbed.h" 00028 #include "ble/BLE.h" 00029 00030 00031 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ 00032 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ 00033 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ 00034 00035 #define TXRX_BUF_LEN 20 //maximum payload: 20 bytes per packet 00036 00037 #define MIN_CONN_INTERVAL 6 //lowest possible connection interval (6 * 1.25 = 7.5ms) 00038 #define MAX_CONN_INTERVAL 6 00039 #define SLAVE_LATENCY 0 //lowest possible 00040 #define CONN_SUP_TIMEOUT 6000 //default; changing this value has not been shown to make a difference in data transfer rate 00041 00042 BLE ble; 00043 00044 Serial pc(USBTX, USBRX); 00045 00046 00047 // The Nordic UART Service 00048 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00049 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00050 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00051 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; 00052 00053 00054 uint8_t txPayload[TXRX_BUF_LEN] = {0,}; 00055 uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; 00056 00057 static uint8_t rx_buf[TXRX_BUF_LEN]; 00058 static uint8_t rx_len=0; 00059 00060 uint8_t packet1[] = {0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34}; 00061 uint8_t packet2[] = {0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48}; 00062 uint8_t packet3[] = {0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c}; 00063 00064 int i = 0; 00065 00066 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);//GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | 00067 00068 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); 00069 00070 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; 00071 00072 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); 00073 00074 00075 void writePackets(void); 00076 00077 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00078 { 00079 ble.startAdvertising(); 00080 } 00081 00082 void WrittenHandler(const GattWriteCallbackParams *Handler) 00083 { 00084 uint8_t buf[TXRX_BUF_LEN]; 00085 uint16_t bytesRead, index;//unnecessary, but if you take it out you must take out lots of other stuff and I don't want to mess with it right now 00086 writePackets(); 00087 00088 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) 00089 { 00090 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); 00091 00092 bytesRead = 20; 00093 memset(txPayload, 0, TXRX_BUF_LEN); 00094 memcpy(txPayload, buf, TXRX_BUF_LEN); 00095 } 00096 } 00097 00098 void uartCB(void) 00099 { 00100 while(pc.readable()) 00101 { 00102 rx_buf[rx_len++] = pc.getc(); 00103 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n') 00104 { 00105 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 00106 00107 pc.putc(rx_len); 00108 00109 rx_len = 0; 00110 break; 00111 } 00112 } 00113 } 00114 00115 void writePackets(void) 00116 { 00117 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), packet1, 20); 00118 00119 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), packet2, 20); 00120 00121 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), packet3, 20); 00122 } 00123 00124 int main(void) 00125 { 00126 ble.init(); 00127 ble.onDisconnection(disconnectionCallback); 00128 ble.onDataWritten(WrittenHandler); 00129 00130 pc.attach( uartCB , pc.RxIrq); 00131 // setup advertising 00132 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00133 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00134 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, 00135 (const uint8_t *)"Megatron", sizeof("Megatron") - 1); 00136 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, 00137 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); 00138 // 100ms; in multiples of 0.625ms. 00139 ble.setAdvertisingInterval(160); 00140 00141 ble.addService(uartService); 00142 00143 ble.startAdvertising(); 00144 00145 while(1) 00146 { 00147 ble.waitForEvent(); 00148 00149 if(i == 0) 00150 { 00151 writePackets(); 00152 i++; 00153 } 00154 } 00155 }
Generated on Thu Jul 14 2022 23:51:08 by
1.7.2
