test
Dependencies: BLE_API mbed nRF51822
Fork of Connect_Test4 by
main.cpp@18:8454c63488ee, 2015-11-09 (annotated)
- Committer:
- peteratsl
- Date:
- Mon Nov 09 14:34:14 2015 +0000
- Revision:
- 18:8454c63488ee
- Parent:
- 17:dfe796bba97c
- Child:
- 19:da48cacdb4c1
commit
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" |
mbedAustin | 1:94152e7d8b5c | 3 | |
mbedAustin | 1:94152e7d8b5c | 4 | BLEDevice ble; |
mbedAustin | 1:94152e7d8b5c | 5 | DigitalOut led(LED1); |
mbedAustin | 9:b33f42191584 | 6 | uint16_t customServiceUUID = 0xA000; |
mbedAustin | 13:62b1d32745ac | 7 | uint16_t readCharUUID = 0xA001; |
mbedAustin | 9:b33f42191584 | 8 | uint16_t writeCharUUID = 0xA002; |
peteratsl | 18:8454c63488ee | 9 | uint16_t A16 = 0xA005; |
mbedAustin | 1:94152e7d8b5c | 10 | |
peteratsl | 18:8454c63488ee | 11 | |
peteratsl | 18:8454c63488ee | 12 | |
peteratsl | 18:8454c63488ee | 13 | |
peteratsl | 18:8454c63488ee | 14 | uint8_t b1_id = 0x01; |
peteratsl | 18:8454c63488ee | 15 | uint8_t b2_id = 0x02; |
peteratsl | 18:8454c63488ee | 16 | uint8_t b3_id = 0x03; |
peteratsl | 18:8454c63488ee | 17 | uint8_t b4_id = 0x04; |
peteratsl | 18:8454c63488ee | 18 | uint8_t b5_id = 0x05; |
peteratsl | 18:8454c63488ee | 19 | uint8_t b6_id = 0x06; |
peteratsl | 18:8454c63488ee | 20 | uint8_t b7_id = 0x07; |
peteratsl | 18:8454c63488ee | 21 | |
peteratsl | 18:8454c63488ee | 22 | PinName flood_p = P0_4; |
peteratsl | 18:8454c63488ee | 23 | |
peteratsl | 18:8454c63488ee | 24 | PinName b1_p = P0_28; |
peteratsl | 18:8454c63488ee | 25 | PinName b2_p = P0_29; |
peteratsl | 18:8454c63488ee | 26 | PinName b3_p = P0_15; |
peteratsl | 18:8454c63488ee | 27 | PinName b4_p = P0_10; //CTX |
peteratsl | 18:8454c63488ee | 28 | PinName b5_p = P0_9; //TXD |
peteratsl | 18:8454c63488ee | 29 | PinName b6_p = P0_11; //RXD |
peteratsl | 18:8454c63488ee | 30 | PinName b7_p = P0_8; //RTS |
peteratsl | 18:8454c63488ee | 31 | |
peteratsl | 18:8454c63488ee | 32 | |
peteratsl | 18:8454c63488ee | 33 | /* |
peteratsl | 18:8454c63488ee | 34 | PinName b4_p = CTX; //CTX |
peteratsl | 18:8454c63488ee | 35 | PinName b5_p = TXD; //TXD |
peteratsl | 18:8454c63488ee | 36 | PinName b6_p = D0; //RXD |
peteratsl | 18:8454c63488ee | 37 | PinName b7_p = D3; //RTS |
peteratsl | 18:8454c63488ee | 38 | */ |
peteratsl | 18:8454c63488ee | 39 | |
peteratsl | 18:8454c63488ee | 40 | |
peteratsl | 18:8454c63488ee | 41 | const static char DEVICE_NAME[] = "PETE2"; // change this |
peteratsl | 18:8454c63488ee | 42 | static const uint16_t uuid16_list[] = {0xFF02}; //Custom UUID, FFFF is reserved for development |
mbedAustin | 1:94152e7d8b5c | 43 | |
mbedAustin | 10:6acaa9f0ef85 | 44 | // Set Up custom Characteristics |
mbedAustin | 9:b33f42191584 | 45 | static uint8_t readValue[10] = {0}; |
mbedAustin | 12:6d1f77d0cb37 | 46 | ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue); |
mbedAustin | 2:e84c13abc479 | 47 | |
mbedAustin | 9:b33f42191584 | 48 | static uint8_t writeValue[10] = {0}; |
mbedAustin | 12:6d1f77d0cb37 | 49 | WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue); |
mbedAustin | 2:e84c13abc479 | 50 | |
mbedAustin | 2:e84c13abc479 | 51 | // Set up custom service |
mbedAustin | 8:60ede963dfe2 | 52 | GattCharacteristic *characteristics[] = {&readChar, &writeChar}; |
mbedAustin | 9:b33f42191584 | 53 | GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); |
mbedAustin | 2:e84c13abc479 | 54 | |
peteratsl | 18:8454c63488ee | 55 | //setup led |
peteratsl | 18:8454c63488ee | 56 | DigitalOut led1(P0_19); |
peteratsl | 18:8454c63488ee | 57 | |
peteratsl | 18:8454c63488ee | 58 | |
peteratsl | 18:8454c63488ee | 59 | DigitalOut b1(b1_p); |
peteratsl | 18:8454c63488ee | 60 | DigitalOut b2(b2_p); |
peteratsl | 18:8454c63488ee | 61 | DigitalOut b3(b3_p); |
peteratsl | 18:8454c63488ee | 62 | DigitalOut b4(b4_p); |
peteratsl | 18:8454c63488ee | 63 | DigitalOut b5(b5_p); |
peteratsl | 18:8454c63488ee | 64 | DigitalOut b6(b6_p); |
peteratsl | 18:8454c63488ee | 65 | DigitalOut b7(b7_p); |
peteratsl | 18:8454c63488ee | 66 | |
peteratsl | 18:8454c63488ee | 67 | |
peteratsl | 18:8454c63488ee | 68 | DigitalIn enable(flood_p); |
mbedAustin | 2:e84c13abc479 | 69 | |
mbedAustin | 8:60ede963dfe2 | 70 | /* |
mbedAustin | 8:60ede963dfe2 | 71 | * Restart advertising when phone app disconnects |
mbedAustin | 8:60ede963dfe2 | 72 | */ |
mbedAustin | 1:94152e7d8b5c | 73 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
mbedAustin | 1:94152e7d8b5c | 74 | { |
mbedAustin | 8:60ede963dfe2 | 75 | ble.startAdvertising(); |
mbedAustin | 1:94152e7d8b5c | 76 | } |
mbedAustin | 0:cd5b6733aeb1 | 77 | |
mbedAustin | 8:60ede963dfe2 | 78 | /* |
mbedAustin | 8:60ede963dfe2 | 79 | * handle writes to writeCharacteristic |
mbedAustin | 8:60ede963dfe2 | 80 | */ |
mbedAustin | 4:7ef90665b399 | 81 | void writeCharCallback(const GattCharacteristicWriteCBParams *params) |
mbedAustin | 3:0fb60f81f693 | 82 | { |
mbedAustin | 8:60ede963dfe2 | 83 | // check to see what characteristic was written, by handle |
peteratsl | 18:8454c63488ee | 84 | if(params->charHandle == writeChar.getValueHandle()) |
peteratsl | 18:8454c63488ee | 85 | { |
peteratsl | 18:8454c63488ee | 86 | // toggle LED if... |
peteratsl | 18:8454c63488ee | 87 | if(params->data[0] == b1_id) |
peteratsl | 18:8454c63488ee | 88 | { |
peteratsl | 18:8454c63488ee | 89 | b1 = !b1; |
peteratsl | 18:8454c63488ee | 90 | //led1 = 0; |
peteratsl | 18:8454c63488ee | 91 | } |
peteratsl | 18:8454c63488ee | 92 | else if(params->data[0] == b2_id) |
peteratsl | 18:8454c63488ee | 93 | { |
peteratsl | 18:8454c63488ee | 94 | b2 = !b2; |
peteratsl | 18:8454c63488ee | 95 | //led1 = 0; |
peteratsl | 18:8454c63488ee | 96 | } |
peteratsl | 18:8454c63488ee | 97 | else if(params->data[0] == b3_id) |
peteratsl | 18:8454c63488ee | 98 | { |
peteratsl | 18:8454c63488ee | 99 | b3 = !b3; |
peteratsl | 18:8454c63488ee | 100 | //led1 = 0; |
peteratsl | 18:8454c63488ee | 101 | } |
peteratsl | 18:8454c63488ee | 102 | if(params->data[0] == b4_id) |
peteratsl | 18:8454c63488ee | 103 | { |
peteratsl | 18:8454c63488ee | 104 | b4 = !b4; |
peteratsl | 18:8454c63488ee | 105 | } |
peteratsl | 18:8454c63488ee | 106 | else if(params->data[0] == b5_id) |
peteratsl | 18:8454c63488ee | 107 | { |
peteratsl | 18:8454c63488ee | 108 | b5 = !b5; |
peteratsl | 18:8454c63488ee | 109 | //led1 = 0; |
peteratsl | 18:8454c63488ee | 110 | } |
peteratsl | 18:8454c63488ee | 111 | else if(params->data[0] == b6_id) |
peteratsl | 18:8454c63488ee | 112 | { |
peteratsl | 18:8454c63488ee | 113 | b6 = !b6; |
peteratsl | 18:8454c63488ee | 114 | //led1 = 0; |
peteratsl | 18:8454c63488ee | 115 | } |
peteratsl | 18:8454c63488ee | 116 | else if(params->data[0] == b7_id) |
peteratsl | 18:8454c63488ee | 117 | { |
peteratsl | 18:8454c63488ee | 118 | b7 = !b7; |
peteratsl | 18:8454c63488ee | 119 | led1 = 0; |
peteratsl | 18:8454c63488ee | 120 | wait(3); |
peteratsl | 18:8454c63488ee | 121 | led1 = 1; |
mbedAustin | 8:60ede963dfe2 | 122 | } |
mbedAustin | 8:60ede963dfe2 | 123 | // print the data if more than 1 byte is written |
peteratsl | 18:8454c63488ee | 124 | else |
peteratsl | 18:8454c63488ee | 125 | { |
peteratsl | 18:8454c63488ee | 126 | |
mbedAustin | 11:b59226338001 | 127 | printf("\n\r Data received: length = %d, data = 0x",params->len); |
mbedAustin | 8:60ede963dfe2 | 128 | for(int x=0; x < params->len; x++) { |
mbedAustin | 8:60ede963dfe2 | 129 | printf("%x",params->data[x]); |
mbedAustin | 8:60ede963dfe2 | 130 | } |
peteratsl | 18:8454c63488ee | 131 | |
mbedAustin | 8:60ede963dfe2 | 132 | } |
mbedAustin | 8:60ede963dfe2 | 133 | // update the readChar with the value of writeChar |
mbedAustin | 8:60ede963dfe2 | 134 | ble.updateCharacteristicValue(readChar.getValueHandle(),params->data,params->len); |
mbedAustin | 8:60ede963dfe2 | 135 | } |
mbedAustin | 3:0fb60f81f693 | 136 | } |
mbedAustin | 0:cd5b6733aeb1 | 137 | |
mbedAustin | 8:60ede963dfe2 | 138 | /* |
mbedAustin | 8:60ede963dfe2 | 139 | * main loop |
mbedAustin | 8:60ede963dfe2 | 140 | */ |
mbedAustin | 0:cd5b6733aeb1 | 141 | int |
mbedAustin | 0:cd5b6733aeb1 | 142 | main(void) |
mbedAustin | 0:cd5b6733aeb1 | 143 | { |
mbedAustin | 8:60ede963dfe2 | 144 | /* initialize stuff */ |
mbedAustin | 8:60ede963dfe2 | 145 | printf("\n\r********* Starting Main Loop *********\n\r"); |
peteratsl | 18:8454c63488ee | 146 | led1 = 1; |
peteratsl | 18:8454c63488ee | 147 | |
peteratsl | 18:8454c63488ee | 148 | b1 = 1;//b1_p = P0_28; |
peteratsl | 18:8454c63488ee | 149 | b2 = 1;//b2_p = P0_29; |
peteratsl | 18:8454c63488ee | 150 | b3 = 1;//b3_p = P0_9; |
peteratsl | 18:8454c63488ee | 151 | b4 = 1;//b4_p = P0_10; //CTX |
peteratsl | 18:8454c63488ee | 152 | b5 = 1;//b5_p = P0_9; //TXD |
peteratsl | 18:8454c63488ee | 153 | b6 = 1;//b6_p = P0_11; //RXD |
peteratsl | 18:8454c63488ee | 154 | b7 = 1;//b7_p = P0_8; //RTS |
peteratsl | 18:8454c63488ee | 155 | |
peteratsl | 18:8454c63488ee | 156 | |
peteratsl | 18:8454c63488ee | 157 | |
peteratsl | 18:8454c63488ee | 158 | |
mbedAustin | 2:e84c13abc479 | 159 | ble.init(); |
mbedAustin | 2:e84c13abc479 | 160 | ble.onDisconnection(disconnectionCallback); |
peteratsl | 18:8454c63488ee | 161 | |
mbedAustin | 10:6acaa9f0ef85 | 162 | ble.onDataWritten(writeCharCallback); |
mbedAustin | 2:e84c13abc479 | 163 | |
mbedAustin | 2:e84c13abc479 | 164 | /* setup advertising */ |
mbedAustin | 7:f6814152873c | 165 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT |
mbedAustin | 7:f6814152873c | 166 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type |
mbedAustin | 7:f6814152873c | 167 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name |
mbedAustin | 7:f6814152873c | 168 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet |
mbedAustin | 17:dfe796bba97c | 169 | ble.setAdvertisingInterval(100); // 100ms. |
mbedAustin | 2:e84c13abc479 | 170 | |
mbedAustin | 8:60ede963dfe2 | 171 | // add our custom service |
mbedAustin | 13:62b1d32745ac | 172 | ble.addService(customService); |
mbedAustin | 2:e84c13abc479 | 173 | |
mbedAustin | 13:62b1d32745ac | 174 | // start advertising |
mbedAustin | 13:62b1d32745ac | 175 | ble.startAdvertising(); |
mbedAustin | 9:b33f42191584 | 176 | |
mbedAustin | 9:b33f42191584 | 177 | // infinite loop waiting for BLE interrupt events |
mbedAustin | 2:e84c13abc479 | 178 | while (true) { |
mbedAustin | 2:e84c13abc479 | 179 | ble.waitForEvent(); //Save power |
mbedAustin | 2:e84c13abc479 | 180 | } |
mbedAustin | 8:60ede963dfe2 | 181 | } |