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 BLE_EvothingsExample_GATT by
main.cpp
00001 #include "mbed.h" 00002 #include "BLEDevice.h" 00003 #include "AltBeaconService.h" 00004 #include "iBeaconService.h" 00005 00006 00007 BLEDevice ble; 00008 DigitalOut led(LED1); 00009 00010 /*.......................................................................... 00011 uint8_t beaconID[] = { 0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, 00012 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61, 00013 0x11, 0x22, 0x33, 0x44 00014 }; 00015 uint16_t manufacturerID = 0x5900; //Nordic SIG ID 00016 int8_t rssi = -122; 00017 ..........................................................................*/ 00018 00019 00020 uint8_t beaconID[] = { 0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, 00021 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61 00022 }; 00023 uint16_t majorNumber = 1122; 00024 uint16_t minorNumber = 3344; 00025 uint16_t txPower = 0xC8; 00026 00027 00028 uint16_t customServiceUUID = 0xA000; 00029 uint16_t readCharUUID = 0xA001; 00030 uint16_t writeCharUUID = 0xA002; 00031 uint16_t A16 = 0xA005; 00032 00033 00034 00035 00036 00037 const uint8_t b1_id = 0x01; 00038 const uint8_t b2_id = 0x02; 00039 const uint8_t b3_id = 0x03; 00040 const uint8_t b4_id = 0x04; 00041 const uint8_t b5_id = 0x05; 00042 const uint8_t b6_id = 0x06; 00043 const uint8_t b7_id = 0x07; 00044 const uint8_t b8_id = 0x08; 00045 00046 const uint8_t bA1_id = 0x31; 00047 const uint8_t bA2_id = 0x32; 00048 const uint8_t bA3_id = 0x33; 00049 const uint8_t bA4_id = 0x34; 00050 const uint8_t bA5_id = 0x35; 00051 const uint8_t bA6_id = 0x36; 00052 const uint8_t bA7_id = 0x37; 00053 00054 PinName flood_p = P0_4; 00055 PinName b1_p = P0_28; 00056 PinName b2_p = P0_29; 00057 PinName b3_p = P0_15; 00058 PinName b4_p = P0_10; //CTX 00059 PinName b5_p = P0_9; //TXD 00060 PinName b6_p = P0_11; //RXD 00061 PinName b7_p = P0_8; //RTS 00062 00063 00064 /* 00065 PinName b4_p = CTX; //CTX 00066 PinName b5_p = TXD; //TXD 00067 PinName b6_p = D0; //RXD 00068 PinName b7_p = D3; //RTS 00069 */ 00070 00071 00072 const static char DEVICE_NAME[] = "PETE2"; // change this 00073 static const uint16_t uuid16_list[] = {0xFF02}; //Custom UUID, FFFF is reserved for development 00074 00075 00076 //setup custom characteristics 00077 00078 //read 00079 static uint8_t readValue[10] = {0}; 00080 ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue); 00081 //write 00082 static uint8_t writeValue[10] = {0}; 00083 WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue); 00084 00085 00086 // Set up custom service 00087 GattCharacteristic *characteristics[] = {&readChar, &writeChar}; 00088 GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); 00089 00090 //setup led 00091 DigitalOut led1(P0_19); 00092 00093 //setup digital outs 00094 DigitalOut b1(b1_p); 00095 DigitalOut b2(b2_p); 00096 DigitalOut b3(b3_p); 00097 DigitalOut b4(b4_p); 00098 DigitalOut b5(b5_p); 00099 //DigitalOut b6(b6_p); 00100 DigitalOut b7(b7_p); 00101 00102 DigitalIn enable(flood_p); 00103 00104 00105 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason); 00106 void writeCharCallback(const GattCharacteristicWriteCBParams *params); 00107 void testAll(void); 00108 void blinky(void); 00109 int flip(char value); 00110 00111 00112 //main loop............................................................................................................ 00113 int main(void) 00114 { 00115 /* initialize stuff */ 00116 led1 = 1; 00117 00118 b1 = 1;//b1_p = P0_28; 00119 b2 = 1;//b2_p = P0_29; 00120 b3 = 1;//b3_p = P0_9; 00121 b4 = 1;//b4_p = P0_10; //CTX 00122 b5 = 1;//b5_p = P0_9; //TXD 00123 //b6 = 1;//b6_p = P0_11; //RXD 00124 b7 = 1;//b7_p = P0_8; //RTS 00125 00126 00127 00128 00129 ble.init(); 00130 00131 //setup beacon service 00132 //AltBeaconService altbeacon(ble, manufacturerID, beaconID, rssi); 00133 iBeaconService ibeacon(ble, beaconID, majorNumber, minorNumber, txPower); 00134 00135 00136 ble.onDisconnection(disconnectionCallback); 00137 ble.onDataWritten(writeCharCallback); 00138 00139 00140 /* setup advertising */ 00141 00142 // BLE only, no classic BT 00143 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00144 // advertising type 00145 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00146 // add name 00147 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00148 // UUID's broadcast in advertising packet 00149 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00150 00151 00152 // set interval 00153 ble.setAdvertisingInterval(100); // 100ms. 00154 00155 // add our custom service 00156 ble.addService(customService); 00157 00158 // start advertising 00159 ble.startAdvertising(); 00160 00161 // infinite loop waiting for BLE interrupt events 00162 while (true) { 00163 ble.waitForEvent(); //Save power 00164 } 00165 } 00166 //.......................end main............................................................................................ 00167 00168 /* 00169 * Restart advertising when phone app disconnects 00170 */ 00171 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00172 { 00173 ble.startAdvertising(); 00174 } 00175 00176 /* 00177 * handle writes to writeCharacteristic 00178 */ 00179 void writeCharCallback(const GattCharacteristicWriteCBParams *params) 00180 { 00181 // check to see what characteristic was written, by handle 00182 if(params->charHandle == writeChar.getValueHandle()) 00183 { 00184 //testAll(); 00185 flip(params->data[0]); 00186 00187 // update the readChar with the value of writeChar 00188 ble.updateCharacteristicValue(readChar.getValueHandle(),params->data,params->len); 00189 } 00190 } 00191 00192 void testAll(void) 00193 { 00194 for(int i=0; i<4; i++) 00195 { 00196 led1 = !led1; 00197 b1 = !b1; 00198 b2 = !b2; 00199 b3 = !b3; 00200 b4 = !b4; 00201 b5 = !b5; 00202 //b6 = !b6; 00203 b7 = !b7; 00204 wait(1); 00205 } 00206 } 00207 void blinky(void) 00208 { 00209 for(int i=0; i<20; i++) 00210 { 00211 led1 = !led1; 00212 wait(.3); 00213 } 00214 } 00215 int flip(char value) 00216 { 00217 int ret = 0; 00218 00219 switch(value) 00220 { 00221 case b1_id : 00222 case bA1_id : led1 = !led1; 00223 b1 = !b1; 00224 break; 00225 00226 case b2_id : 00227 case bA2_id : led1 = !led1; 00228 b2 = !b2; 00229 break; 00230 00231 case b3_id : 00232 case bA3_id : led1 = !led1; 00233 b3 = !b3; 00234 break; 00235 00236 case b4_id : 00237 case bA4_id : led1 = !led1; 00238 b4 = !b4; 00239 break; 00240 00241 case b5_id : 00242 case bA5_id : led1 = !led1; 00243 b5 = !b5; 00244 break; 00245 00246 case b7_id : 00247 case bA7_id : led1 = !led1; 00248 b7 = !b7; 00249 break; 00250 00251 default : blinky(); 00252 ret = 1; 00253 break; 00254 } 00255 return ret; 00256 }
Generated on Wed Jul 13 2022 01:11:47 by
1.7.2
