test 4
Dependencies: BLE_API mbed nRF51822
Fork of BLE_EvothingsExample_GATT by
main.cpp@19:da48cacdb4c1, 2015-11-13 (annotated)
- Committer:
- peteratsl
- Date:
- Fri Nov 13 03:33:39 2015 +0000
- Revision:
- 19:da48cacdb4c1
- Parent:
- 18:8454c63488ee
test 4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedAustin | 0:cd5b6733aeb1 | 1 | #include "mbed.h" |
mbedAustin | 0:cd5b6733aeb1 | 2 | #include "BLEDevice.h" |
peteratsl | 19:da48cacdb4c1 | 3 | #include "AltBeaconService.h" |
peteratsl | 19:da48cacdb4c1 | 4 | #include "iBeaconService.h" |
peteratsl | 19:da48cacdb4c1 | 5 | |
mbedAustin | 1:94152e7d8b5c | 6 | |
mbedAustin | 1:94152e7d8b5c | 7 | BLEDevice ble; |
mbedAustin | 1:94152e7d8b5c | 8 | DigitalOut led(LED1); |
peteratsl | 19:da48cacdb4c1 | 9 | |
peteratsl | 19:da48cacdb4c1 | 10 | /*.......................................................................... |
peteratsl | 19:da48cacdb4c1 | 11 | uint8_t beaconID[] = { 0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, |
peteratsl | 19:da48cacdb4c1 | 12 | 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61, |
peteratsl | 19:da48cacdb4c1 | 13 | 0x11, 0x22, 0x33, 0x44 |
peteratsl | 19:da48cacdb4c1 | 14 | }; |
peteratsl | 19:da48cacdb4c1 | 15 | uint16_t manufacturerID = 0x5900; //Nordic SIG ID |
peteratsl | 19:da48cacdb4c1 | 16 | int8_t rssi = -122; |
peteratsl | 19:da48cacdb4c1 | 17 | ..........................................................................*/ |
peteratsl | 19:da48cacdb4c1 | 18 | |
peteratsl | 19:da48cacdb4c1 | 19 | |
peteratsl | 19:da48cacdb4c1 | 20 | uint8_t beaconID[] = { 0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, |
peteratsl | 19:da48cacdb4c1 | 21 | 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61 |
peteratsl | 19:da48cacdb4c1 | 22 | }; |
peteratsl | 19:da48cacdb4c1 | 23 | uint16_t majorNumber = 1122; |
peteratsl | 19:da48cacdb4c1 | 24 | uint16_t minorNumber = 3344; |
peteratsl | 19:da48cacdb4c1 | 25 | uint16_t txPower = 0xC8; |
peteratsl | 19:da48cacdb4c1 | 26 | |
peteratsl | 19:da48cacdb4c1 | 27 | |
mbedAustin | 9:b33f42191584 | 28 | uint16_t customServiceUUID = 0xA000; |
mbedAustin | 13:62b1d32745ac | 29 | uint16_t readCharUUID = 0xA001; |
mbedAustin | 9:b33f42191584 | 30 | uint16_t writeCharUUID = 0xA002; |
peteratsl | 18:8454c63488ee | 31 | uint16_t A16 = 0xA005; |
mbedAustin | 1:94152e7d8b5c | 32 | |
peteratsl | 18:8454c63488ee | 33 | |
peteratsl | 18:8454c63488ee | 34 | |
peteratsl | 18:8454c63488ee | 35 | |
peteratsl | 19:da48cacdb4c1 | 36 | |
peteratsl | 19:da48cacdb4c1 | 37 | const uint8_t b1_id = 0x01; |
peteratsl | 19:da48cacdb4c1 | 38 | const uint8_t b2_id = 0x02; |
peteratsl | 19:da48cacdb4c1 | 39 | const uint8_t b3_id = 0x03; |
peteratsl | 19:da48cacdb4c1 | 40 | const uint8_t b4_id = 0x04; |
peteratsl | 19:da48cacdb4c1 | 41 | const uint8_t b5_id = 0x05; |
peteratsl | 19:da48cacdb4c1 | 42 | const uint8_t b6_id = 0x06; |
peteratsl | 19:da48cacdb4c1 | 43 | const uint8_t b7_id = 0x07; |
peteratsl | 19:da48cacdb4c1 | 44 | const uint8_t b8_id = 0x08; |
peteratsl | 18:8454c63488ee | 45 | |
peteratsl | 19:da48cacdb4c1 | 46 | const uint8_t bA1_id = 0x31; |
peteratsl | 19:da48cacdb4c1 | 47 | const uint8_t bA2_id = 0x32; |
peteratsl | 19:da48cacdb4c1 | 48 | const uint8_t bA3_id = 0x33; |
peteratsl | 19:da48cacdb4c1 | 49 | const uint8_t bA4_id = 0x34; |
peteratsl | 19:da48cacdb4c1 | 50 | const uint8_t bA5_id = 0x35; |
peteratsl | 19:da48cacdb4c1 | 51 | const uint8_t bA6_id = 0x36; |
peteratsl | 19:da48cacdb4c1 | 52 | const uint8_t bA7_id = 0x37; |
peteratsl | 18:8454c63488ee | 53 | |
peteratsl | 19:da48cacdb4c1 | 54 | PinName flood_p = P0_4; |
peteratsl | 19:da48cacdb4c1 | 55 | PinName b1_p = P0_28; |
peteratsl | 19:da48cacdb4c1 | 56 | PinName b2_p = P0_29; |
peteratsl | 19:da48cacdb4c1 | 57 | PinName b3_p = P0_15; |
peteratsl | 19:da48cacdb4c1 | 58 | PinName b4_p = P0_10; //CTX |
peteratsl | 19:da48cacdb4c1 | 59 | PinName b5_p = P0_9; //TXD |
peteratsl | 19:da48cacdb4c1 | 60 | PinName b6_p = P0_11; //RXD |
peteratsl | 19:da48cacdb4c1 | 61 | PinName b7_p = P0_8; //RTS |
peteratsl | 18:8454c63488ee | 62 | |
peteratsl | 18:8454c63488ee | 63 | |
peteratsl | 18:8454c63488ee | 64 | /* |
peteratsl | 18:8454c63488ee | 65 | PinName b4_p = CTX; //CTX |
peteratsl | 18:8454c63488ee | 66 | PinName b5_p = TXD; //TXD |
peteratsl | 18:8454c63488ee | 67 | PinName b6_p = D0; //RXD |
peteratsl | 18:8454c63488ee | 68 | PinName b7_p = D3; //RTS |
peteratsl | 18:8454c63488ee | 69 | */ |
peteratsl | 18:8454c63488ee | 70 | |
peteratsl | 18:8454c63488ee | 71 | |
peteratsl | 18:8454c63488ee | 72 | const static char DEVICE_NAME[] = "PETE2"; // change this |
peteratsl | 18:8454c63488ee | 73 | static const uint16_t uuid16_list[] = {0xFF02}; //Custom UUID, FFFF is reserved for development |
mbedAustin | 1:94152e7d8b5c | 74 | |
peteratsl | 19:da48cacdb4c1 | 75 | |
peteratsl | 19:da48cacdb4c1 | 76 | //setup custom characteristics |
peteratsl | 19:da48cacdb4c1 | 77 | |
peteratsl | 19:da48cacdb4c1 | 78 | //read |
mbedAustin | 9:b33f42191584 | 79 | static uint8_t readValue[10] = {0}; |
mbedAustin | 12:6d1f77d0cb37 | 80 | ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue); |
peteratsl | 19:da48cacdb4c1 | 81 | //write |
mbedAustin | 9:b33f42191584 | 82 | static uint8_t writeValue[10] = {0}; |
mbedAustin | 12:6d1f77d0cb37 | 83 | WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue); |
mbedAustin | 2:e84c13abc479 | 84 | |
peteratsl | 19:da48cacdb4c1 | 85 | |
mbedAustin | 2:e84c13abc479 | 86 | // Set up custom service |
peteratsl | 19:da48cacdb4c1 | 87 | GattCharacteristic *characteristics[] = {&readChar, &writeChar}; |
peteratsl | 19:da48cacdb4c1 | 88 | GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); |
mbedAustin | 2:e84c13abc479 | 89 | |
peteratsl | 18:8454c63488ee | 90 | //setup led |
peteratsl | 18:8454c63488ee | 91 | DigitalOut led1(P0_19); |
peteratsl | 18:8454c63488ee | 92 | |
peteratsl | 19:da48cacdb4c1 | 93 | //setup digital outs |
peteratsl | 18:8454c63488ee | 94 | DigitalOut b1(b1_p); |
peteratsl | 18:8454c63488ee | 95 | DigitalOut b2(b2_p); |
peteratsl | 18:8454c63488ee | 96 | DigitalOut b3(b3_p); |
peteratsl | 18:8454c63488ee | 97 | DigitalOut b4(b4_p); |
peteratsl | 18:8454c63488ee | 98 | DigitalOut b5(b5_p); |
peteratsl | 19:da48cacdb4c1 | 99 | //DigitalOut b6(b6_p); |
peteratsl | 18:8454c63488ee | 100 | DigitalOut b7(b7_p); |
peteratsl | 18:8454c63488ee | 101 | |
peteratsl | 19:da48cacdb4c1 | 102 | DigitalIn enable(flood_p); |
peteratsl | 18:8454c63488ee | 103 | |
peteratsl | 19:da48cacdb4c1 | 104 | |
peteratsl | 19:da48cacdb4c1 | 105 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason); |
peteratsl | 19:da48cacdb4c1 | 106 | void writeCharCallback(const GattCharacteristicWriteCBParams *params); |
peteratsl | 19:da48cacdb4c1 | 107 | void testAll(void); |
peteratsl | 19:da48cacdb4c1 | 108 | void blinky(void); |
peteratsl | 19:da48cacdb4c1 | 109 | int flip(char value); |
peteratsl | 19:da48cacdb4c1 | 110 | |
peteratsl | 19:da48cacdb4c1 | 111 | |
peteratsl | 19:da48cacdb4c1 | 112 | //main loop............................................................................................................ |
peteratsl | 19:da48cacdb4c1 | 113 | int main(void) |
peteratsl | 19:da48cacdb4c1 | 114 | { |
peteratsl | 19:da48cacdb4c1 | 115 | /* initialize stuff */ |
peteratsl | 19:da48cacdb4c1 | 116 | led1 = 1; |
peteratsl | 19:da48cacdb4c1 | 117 | |
peteratsl | 19:da48cacdb4c1 | 118 | b1 = 1;//b1_p = P0_28; |
peteratsl | 19:da48cacdb4c1 | 119 | b2 = 1;//b2_p = P0_29; |
peteratsl | 19:da48cacdb4c1 | 120 | b3 = 1;//b3_p = P0_9; |
peteratsl | 19:da48cacdb4c1 | 121 | b4 = 1;//b4_p = P0_10; //CTX |
peteratsl | 19:da48cacdb4c1 | 122 | b5 = 1;//b5_p = P0_9; //TXD |
peteratsl | 19:da48cacdb4c1 | 123 | //b6 = 1;//b6_p = P0_11; //RXD |
peteratsl | 19:da48cacdb4c1 | 124 | b7 = 1;//b7_p = P0_8; //RTS |
peteratsl | 19:da48cacdb4c1 | 125 | |
peteratsl | 19:da48cacdb4c1 | 126 | |
peteratsl | 19:da48cacdb4c1 | 127 | |
peteratsl | 19:da48cacdb4c1 | 128 | |
peteratsl | 19:da48cacdb4c1 | 129 | ble.init(); |
peteratsl | 19:da48cacdb4c1 | 130 | |
peteratsl | 19:da48cacdb4c1 | 131 | //setup beacon service |
peteratsl | 19:da48cacdb4c1 | 132 | //AltBeaconService altbeacon(ble, manufacturerID, beaconID, rssi); |
peteratsl | 19:da48cacdb4c1 | 133 | iBeaconService ibeacon(ble, beaconID, majorNumber, minorNumber, txPower); |
peteratsl | 19:da48cacdb4c1 | 134 | |
peteratsl | 19:da48cacdb4c1 | 135 | |
peteratsl | 19:da48cacdb4c1 | 136 | ble.onDisconnection(disconnectionCallback); |
peteratsl | 19:da48cacdb4c1 | 137 | ble.onDataWritten(writeCharCallback); |
peteratsl | 19:da48cacdb4c1 | 138 | |
peteratsl | 19:da48cacdb4c1 | 139 | |
peteratsl | 19:da48cacdb4c1 | 140 | /* setup advertising */ |
peteratsl | 19:da48cacdb4c1 | 141 | |
peteratsl | 19:da48cacdb4c1 | 142 | // BLE only, no classic BT |
peteratsl | 19:da48cacdb4c1 | 143 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
peteratsl | 19:da48cacdb4c1 | 144 | // advertising type |
peteratsl | 19:da48cacdb4c1 | 145 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
peteratsl | 19:da48cacdb4c1 | 146 | // add name |
peteratsl | 19:da48cacdb4c1 | 147 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
peteratsl | 19:da48cacdb4c1 | 148 | // UUID's broadcast in advertising packet |
peteratsl | 19:da48cacdb4c1 | 149 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
peteratsl | 19:da48cacdb4c1 | 150 | |
peteratsl | 19:da48cacdb4c1 | 151 | |
peteratsl | 19:da48cacdb4c1 | 152 | // set interval |
peteratsl | 19:da48cacdb4c1 | 153 | ble.setAdvertisingInterval(100); // 100ms. |
peteratsl | 19:da48cacdb4c1 | 154 | |
peteratsl | 19:da48cacdb4c1 | 155 | // add our custom service |
peteratsl | 19:da48cacdb4c1 | 156 | ble.addService(customService); |
peteratsl | 19:da48cacdb4c1 | 157 | |
peteratsl | 19:da48cacdb4c1 | 158 | // start advertising |
peteratsl | 19:da48cacdb4c1 | 159 | ble.startAdvertising(); |
peteratsl | 19:da48cacdb4c1 | 160 | |
peteratsl | 19:da48cacdb4c1 | 161 | // infinite loop waiting for BLE interrupt events |
peteratsl | 19:da48cacdb4c1 | 162 | while (true) { |
peteratsl | 19:da48cacdb4c1 | 163 | ble.waitForEvent(); //Save power |
peteratsl | 19:da48cacdb4c1 | 164 | } |
peteratsl | 19:da48cacdb4c1 | 165 | } |
peteratsl | 19:da48cacdb4c1 | 166 | //.......................end main............................................................................................ |
mbedAustin | 2:e84c13abc479 | 167 | |
mbedAustin | 8:60ede963dfe2 | 168 | /* |
mbedAustin | 8:60ede963dfe2 | 169 | * Restart advertising when phone app disconnects |
mbedAustin | 8:60ede963dfe2 | 170 | */ |
mbedAustin | 1:94152e7d8b5c | 171 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
mbedAustin | 1:94152e7d8b5c | 172 | { |
mbedAustin | 8:60ede963dfe2 | 173 | ble.startAdvertising(); |
mbedAustin | 1:94152e7d8b5c | 174 | } |
mbedAustin | 0:cd5b6733aeb1 | 175 | |
mbedAustin | 8:60ede963dfe2 | 176 | /* |
mbedAustin | 8:60ede963dfe2 | 177 | * handle writes to writeCharacteristic |
mbedAustin | 8:60ede963dfe2 | 178 | */ |
mbedAustin | 4:7ef90665b399 | 179 | void writeCharCallback(const GattCharacteristicWriteCBParams *params) |
mbedAustin | 3:0fb60f81f693 | 180 | { |
mbedAustin | 8:60ede963dfe2 | 181 | // check to see what characteristic was written, by handle |
peteratsl | 18:8454c63488ee | 182 | if(params->charHandle == writeChar.getValueHandle()) |
peteratsl | 18:8454c63488ee | 183 | { |
peteratsl | 19:da48cacdb4c1 | 184 | //testAll(); |
peteratsl | 19:da48cacdb4c1 | 185 | flip(params->data[0]); |
peteratsl | 19:da48cacdb4c1 | 186 | |
mbedAustin | 8:60ede963dfe2 | 187 | // update the readChar with the value of writeChar |
mbedAustin | 8:60ede963dfe2 | 188 | ble.updateCharacteristicValue(readChar.getValueHandle(),params->data,params->len); |
mbedAustin | 8:60ede963dfe2 | 189 | } |
mbedAustin | 3:0fb60f81f693 | 190 | } |
mbedAustin | 0:cd5b6733aeb1 | 191 | |
peteratsl | 19:da48cacdb4c1 | 192 | void testAll(void) |
mbedAustin | 0:cd5b6733aeb1 | 193 | { |
peteratsl | 19:da48cacdb4c1 | 194 | for(int i=0; i<4; i++) |
peteratsl | 19:da48cacdb4c1 | 195 | { |
peteratsl | 19:da48cacdb4c1 | 196 | led1 = !led1; |
peteratsl | 19:da48cacdb4c1 | 197 | b1 = !b1; |
peteratsl | 19:da48cacdb4c1 | 198 | b2 = !b2; |
peteratsl | 19:da48cacdb4c1 | 199 | b3 = !b3; |
peteratsl | 19:da48cacdb4c1 | 200 | b4 = !b4; |
peteratsl | 19:da48cacdb4c1 | 201 | b5 = !b5; |
peteratsl | 19:da48cacdb4c1 | 202 | //b6 = !b6; |
peteratsl | 19:da48cacdb4c1 | 203 | b7 = !b7; |
peteratsl | 19:da48cacdb4c1 | 204 | wait(1); |
peteratsl | 19:da48cacdb4c1 | 205 | } |
peteratsl | 19:da48cacdb4c1 | 206 | } |
peteratsl | 19:da48cacdb4c1 | 207 | void blinky(void) |
peteratsl | 19:da48cacdb4c1 | 208 | { |
peteratsl | 19:da48cacdb4c1 | 209 | for(int i=0; i<20; i++) |
peteratsl | 19:da48cacdb4c1 | 210 | { |
peteratsl | 19:da48cacdb4c1 | 211 | led1 = !led1; |
peteratsl | 19:da48cacdb4c1 | 212 | wait(.3); |
mbedAustin | 2:e84c13abc479 | 213 | } |
mbedAustin | 8:60ede963dfe2 | 214 | } |
peteratsl | 19:da48cacdb4c1 | 215 | int flip(char value) |
peteratsl | 19:da48cacdb4c1 | 216 | { |
peteratsl | 19:da48cacdb4c1 | 217 | int ret = 0; |
peteratsl | 19:da48cacdb4c1 | 218 | |
peteratsl | 19:da48cacdb4c1 | 219 | switch(value) |
peteratsl | 19:da48cacdb4c1 | 220 | { |
peteratsl | 19:da48cacdb4c1 | 221 | case b1_id : |
peteratsl | 19:da48cacdb4c1 | 222 | case bA1_id : led1 = !led1; |
peteratsl | 19:da48cacdb4c1 | 223 | b1 = !b1; |
peteratsl | 19:da48cacdb4c1 | 224 | break; |
peteratsl | 19:da48cacdb4c1 | 225 | |
peteratsl | 19:da48cacdb4c1 | 226 | case b2_id : |
peteratsl | 19:da48cacdb4c1 | 227 | case bA2_id : led1 = !led1; |
peteratsl | 19:da48cacdb4c1 | 228 | b2 = !b2; |
peteratsl | 19:da48cacdb4c1 | 229 | break; |
peteratsl | 19:da48cacdb4c1 | 230 | |
peteratsl | 19:da48cacdb4c1 | 231 | case b3_id : |
peteratsl | 19:da48cacdb4c1 | 232 | case bA3_id : led1 = !led1; |
peteratsl | 19:da48cacdb4c1 | 233 | b3 = !b3; |
peteratsl | 19:da48cacdb4c1 | 234 | break; |
peteratsl | 19:da48cacdb4c1 | 235 | |
peteratsl | 19:da48cacdb4c1 | 236 | case b4_id : |
peteratsl | 19:da48cacdb4c1 | 237 | case bA4_id : led1 = !led1; |
peteratsl | 19:da48cacdb4c1 | 238 | b4 = !b4; |
peteratsl | 19:da48cacdb4c1 | 239 | break; |
peteratsl | 19:da48cacdb4c1 | 240 | |
peteratsl | 19:da48cacdb4c1 | 241 | case b5_id : |
peteratsl | 19:da48cacdb4c1 | 242 | case bA5_id : led1 = !led1; |
peteratsl | 19:da48cacdb4c1 | 243 | b5 = !b5; |
peteratsl | 19:da48cacdb4c1 | 244 | break; |
peteratsl | 19:da48cacdb4c1 | 245 | |
peteratsl | 19:da48cacdb4c1 | 246 | case b7_id : |
peteratsl | 19:da48cacdb4c1 | 247 | case bA7_id : led1 = !led1; |
peteratsl | 19:da48cacdb4c1 | 248 | b7 = !b7; |
peteratsl | 19:da48cacdb4c1 | 249 | break; |
peteratsl | 19:da48cacdb4c1 | 250 | |
peteratsl | 19:da48cacdb4c1 | 251 | default : blinky(); |
peteratsl | 19:da48cacdb4c1 | 252 | ret = 1; |
peteratsl | 19:da48cacdb4c1 | 253 | break; |
peteratsl | 19:da48cacdb4c1 | 254 | } |
peteratsl | 19:da48cacdb4c1 | 255 | return ret; |
peteratsl | 19:da48cacdb4c1 | 256 | } |