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
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 #include "mbed.h" 00021 #include "BLEDevice.h" 00022 00023 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ 00024 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ 00025 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ 00026 00027 #define TXRX_BUF_LEN 20 00028 00029 BLEDevice ble; 00030 DigitalOut myled1(LED1); 00031 DigitalOut light1(P0_15); //green 00032 DigitalOut light2(P0_6); //yellow 00033 DigitalOut light3(P0_7); //red 00034 DigitalOut vib(P0_4); //vibration 00035 DigitalOut sound(P0_5); //sound 00036 00037 Serial pc(USBTX, USBRX); 00038 00039 // The Nordic UART Service 00040 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00041 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00042 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; 00043 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; 00044 00045 00046 uint8_t txPayload[TXRX_BUF_LEN] = {0,}; 00047 uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; 00048 00049 static uint8_t rx_buf[TXRX_BUF_LEN]; 00050 static uint8_t rx_len=0; 00051 00052 00053 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); 00054 00055 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); 00056 00057 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; 00058 00059 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); 00060 00061 00062 00063 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00064 { 00065 pc.printf("Disconnected \r\n"); 00066 pc.printf("Restart advertising \r\n"); 00067 ble.startAdvertising(); 00068 } 00069 00070 void vibrate(){//Function used when audio and tactile output is required 00071 pc.printf("\nVIBRATE\n"); 00072 sound = 0; 00073 light1 = 1; 00074 light2 = 1; 00075 light3 = 1; 00076 for (int i=0;i<250;i++){ 00077 sound = !sound;//sound requires high frequency 00078 if ((i+1)/20 == 0)//vibrate at 250Hz for best result 00079 vib = !vib; 00080 if ((i+1)/50 == 0){//reduce blinking frequency so flicker is visible 00081 light1 = !light1; 00082 light2 = !light2; 00083 light3 = !light3; 00084 } 00085 wait(0.0002); 00086 } 00087 //turn everything off just in case 00088 vib = 0; 00089 sound = 0; 00090 light1 = 0; 00091 light2 = 0; 00092 light3 = 0; 00093 } 00094 00095 00096 void WrittenHandler(const GattCharacteristicWriteCBParams *Handler) 00097 { 00098 uint8_t buf[TXRX_BUF_LEN]; 00099 uint16_t bytesRead, index; 00100 00101 if (Handler->charHandle == txCharacteristic.getValueAttribute().getHandle()) 00102 { 00103 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); 00104 memset(txPayload, 0, TXRX_BUF_LEN); 00105 memcpy(txPayload, buf, TXRX_BUF_LEN); 00106 pc.printf("WriteHandler \r\n"); 00107 pc.printf("Length: "); 00108 pc.putc(bytesRead); 00109 pc.printf("\r\n"); 00110 pc.printf("Data: "); 00111 for(index=0; index<bytesRead; index++) 00112 { 00113 if (txPayload[0] == 'a'){//get the first letter of the message and see if it is "a" 00114 light1 = 1; 00115 light2 = 0; 00116 light3 = 0; 00117 pc.printf("\nGREEN\n"); 00118 } 00119 else if (txPayload[0] == 'b'){ 00120 light1 = 0; 00121 light2 = 1; 00122 light3 = 0; 00123 pc.printf("\nYELLOW\n"); 00124 } 00125 else if (txPayload[0] == 'c'){ 00126 light1 = 0; 00127 light2 = 0; 00128 light3 = 1; 00129 pc.printf("\nRED\n"); 00130 } 00131 else if (txPayload[0] == 'd'){ 00132 vibrate(); 00133 } 00134 else {//turn everything off 00135 light1 = 0; 00136 light2 = 0; 00137 light3 = 0; 00138 vib = 0; 00139 sound = 0; 00140 } 00141 pc.putc(txPayload[index]); 00142 } 00143 pc.printf("\r\n"); 00144 } 00145 } 00146 00147 void uartCB(void) 00148 { 00149 while(pc.readable()) 00150 { 00151 rx_buf[rx_len++] = pc.getc(); 00152 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n') 00153 { 00154 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 00155 pc.printf("RecHandler \r\n"); 00156 pc.printf("Length: "); 00157 pc.putc(rx_len); 00158 pc.printf("\r\n"); 00159 rx_len = 0; 00160 break; 00161 } 00162 } 00163 } 00164 00165 int main(void) 00166 { 00167 ble.init(); 00168 00169 ble.onDisconnection(disconnectionCallback); 00170 ble.onDataWritten(WrittenHandler); 00171 00172 pc.baud(9600); 00173 pc.printf("SimpleChat Init \r\n"); 00174 00175 pc.attach( uartCB , pc.RxIrq); 00176 // setup advertising 00177 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); 00178 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00179 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, 00180 (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1); 00181 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, 00182 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); 00183 // 100ms; in multiples of 0.625ms. 00184 ble.setAdvertisingInterval(160); 00185 00186 ble.addService(uartService); 00187 00188 ble.startAdvertising(); 00189 pc.printf("Advertising Start \r\n"); 00190 //myled1 = 1; 00191 while(1) 00192 { 00193 ble.waitForEvent(); 00194 } 00195 }
Generated on Thu Dec 29 2022 16:54:37 by
1.7.2