microbit_BLE_RCBController_mbed
Dependencies: BLE_API mbed nRF51822
Fork of BLE_GATT_Example by
main.cpp@23:53883bb55dc3, 2017-10-10 (annotated)
- Committer:
- robo8080
- Date:
- Tue Oct 10 12:46:14 2017 +0000
- Revision:
- 23:53883bb55dc3
- Parent:
- 22:406127954d1f
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedAustin | 0:cd5b6733aeb1 | 1 | #include "mbed.h" |
andresag | 19:477567297aac | 2 | #include "ble/BLE.h" |
robo8080 | 23:53883bb55dc3 | 3 | #include "RCBController.h" |
mbedAustin | 1:94152e7d8b5c | 4 | |
robo8080 | 23:53883bb55dc3 | 5 | Serial pc(USBTX, USBRX); |
robo8080 | 23:53883bb55dc3 | 6 | |
robo8080 | 23:53883bb55dc3 | 7 | uint16_t RCBController_service_uuid = 0xFFF0; |
robo8080 | 23:53883bb55dc3 | 8 | uint16_t RCBController_Characteristic_uuid = 0xFFF1; |
mbedAustin | 1:94152e7d8b5c | 9 | |
robo8080 | 23:53883bb55dc3 | 10 | RCBController controller; |
robo8080 | 23:53883bb55dc3 | 11 | |
robo8080 | 23:53883bb55dc3 | 12 | const static char DEVICE_NAME[] = "micro:bit"; // change this |
mbedAustin | 8:60ede963dfe2 | 13 | static const uint16_t uuid16_list[] = {0xFFFF}; //Custom UUID, FFFF is reserved for development |
robo8080 | 23:53883bb55dc3 | 14 | //static const uint16_t uuid16_list[] = {0xFFF0}; //Custom UUID, FFFF is reserved for development |
mbedAustin | 1:94152e7d8b5c | 15 | |
andresag | 22:406127954d1f | 16 | /* Set Up custom Characteristics */ |
mbedAustin | 9:b33f42191584 | 17 | static uint8_t writeValue[10] = {0}; |
robo8080 | 23:53883bb55dc3 | 18 | WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> Controller(RCBController_Characteristic_uuid, writeValue, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
mbedAustin | 2:e84c13abc479 | 19 | |
andresag | 22:406127954d1f | 20 | /* Set up custom service */ |
robo8080 | 23:53883bb55dc3 | 21 | GattCharacteristic *characteristics[] = {&Controller}; |
robo8080 | 23:53883bb55dc3 | 22 | GattService customService(RCBController_service_uuid, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); |
mbedAustin | 2:e84c13abc479 | 23 | |
robo8080 | 23:53883bb55dc3 | 24 | void connectionCallback(const Gap::ConnectionCallbackParams_t *) |
robo8080 | 23:53883bb55dc3 | 25 | { |
robo8080 | 23:53883bb55dc3 | 26 | printf("connectionCallback\n\r"); |
robo8080 | 23:53883bb55dc3 | 27 | } |
mbedAustin | 2:e84c13abc479 | 28 | |
mbedAustin | 8:60ede963dfe2 | 29 | /* |
mbedAustin | 8:60ede963dfe2 | 30 | * Restart advertising when phone app disconnects |
andresag | 19:477567297aac | 31 | */ |
andresag | 19:477567297aac | 32 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) |
mbedAustin | 1:94152e7d8b5c | 33 | { |
robo8080 | 23:53883bb55dc3 | 34 | printf("disconnectionCallback\n\r"); |
andresag | 22:406127954d1f | 35 | BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); |
mbedAustin | 1:94152e7d8b5c | 36 | } |
mbedAustin | 0:cd5b6733aeb1 | 37 | |
andresag | 19:477567297aac | 38 | /* |
andresag | 22:406127954d1f | 39 | * Handle writes to writeCharacteristic |
mbedAustin | 8:60ede963dfe2 | 40 | */ |
melmon | 18:7d89c4fba362 | 41 | void writeCharCallback(const GattWriteCallbackParams *params) |
mbedAustin | 3:0fb60f81f693 | 42 | { |
robo8080 | 23:53883bb55dc3 | 43 | printf("writeCharCallback\n\r"); |
andresag | 22:406127954d1f | 44 | /* Check to see what characteristic was written, by handle */ |
robo8080 | 23:53883bb55dc3 | 45 | if(params->handle == Controller.getValueHandle()) { |
andresag | 22:406127954d1f | 46 | /* toggle LED if only 1 byte is written */ |
robo8080 | 23:53883bb55dc3 | 47 | if(params->len == 10) { |
andresag | 19:477567297aac | 48 | printf("Data received: length = %d, data = 0x",params->len); |
mbedAustin | 8:60ede963dfe2 | 49 | for(int x=0; x < params->len; x++) { |
andresag | 19:477567297aac | 50 | printf("%x", params->data[x]); |
mbedAustin | 8:60ede963dfe2 | 51 | } |
andresag | 19:477567297aac | 52 | printf("\n\r"); |
mbedAustin | 8:60ede963dfe2 | 53 | } |
mbedAustin | 8:60ede963dfe2 | 54 | } |
mbedAustin | 3:0fb60f81f693 | 55 | } |
andresag | 22:406127954d1f | 56 | /* |
andresag | 22:406127954d1f | 57 | * Initialization callback |
andresag | 22:406127954d1f | 58 | */ |
andresag | 22:406127954d1f | 59 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
andresag | 22:406127954d1f | 60 | { |
andresag | 22:406127954d1f | 61 | BLE &ble = params->ble; |
andresag | 22:406127954d1f | 62 | ble_error_t error = params->error; |
andresag | 22:406127954d1f | 63 | |
andresag | 22:406127954d1f | 64 | if (error != BLE_ERROR_NONE) { |
andresag | 22:406127954d1f | 65 | return; |
andresag | 22:406127954d1f | 66 | } |
mbedAustin | 0:cd5b6733aeb1 | 67 | |
robo8080 | 23:53883bb55dc3 | 68 | ble.gap().onConnection(connectionCallback); |
andresag | 19:477567297aac | 69 | ble.gap().onDisconnection(disconnectionCallback); |
andresag | 19:477567297aac | 70 | ble.gattServer().onDataWritten(writeCharCallback); |
mbedAustin | 2:e84c13abc479 | 71 | |
andresag | 22:406127954d1f | 72 | /* Setup advertising */ |
andresag | 19:477567297aac | 73 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT |
andresag | 19:477567297aac | 74 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type |
andresag | 19:477567297aac | 75 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name |
andresag | 19:477567297aac | 76 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet |
andresag | 19:477567297aac | 77 | ble.gap().setAdvertisingInterval(100); // 100ms. |
mbedAustin | 2:e84c13abc479 | 78 | |
andresag | 22:406127954d1f | 79 | /* Add our custom service */ |
mbedAustin | 13:62b1d32745ac | 80 | ble.addService(customService); |
mbedAustin | 2:e84c13abc479 | 81 | |
andresag | 22:406127954d1f | 82 | /* Start advertising */ |
andresag | 19:477567297aac | 83 | ble.gap().startAdvertising(); |
andresag | 22:406127954d1f | 84 | } |
andresag | 19:477567297aac | 85 | |
andresag | 22:406127954d1f | 86 | /* |
andresag | 22:406127954d1f | 87 | * Main loop |
andresag | 22:406127954d1f | 88 | */ |
andresag | 22:406127954d1f | 89 | int main(void) |
andresag | 22:406127954d1f | 90 | { |
robo8080 | 23:53883bb55dc3 | 91 | pc.baud(115200); |
andresag | 22:406127954d1f | 92 | /* initialize stuff */ |
andresag | 22:406127954d1f | 93 | printf("\n\r********* Starting Main Loop *********\n\r"); |
andresag | 22:406127954d1f | 94 | |
andresag | 22:406127954d1f | 95 | BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); |
andresag | 22:406127954d1f | 96 | ble.init(bleInitComplete); |
andresag | 22:406127954d1f | 97 | |
andresag | 22:406127954d1f | 98 | /* SpinWait for initialization to complete. This is necessary because the |
andresag | 22:406127954d1f | 99 | * BLE object is used in the main loop below. */ |
andresag | 22:406127954d1f | 100 | while (ble.hasInitialized() == false) { /* spin loop */ } |
andresag | 22:406127954d1f | 101 | |
andresag | 22:406127954d1f | 102 | /* Infinite loop waiting for BLE interrupt events */ |
mbedAustin | 2:e84c13abc479 | 103 | while (true) { |
andresag | 22:406127954d1f | 104 | ble.waitForEvent(); /* Save power */ |
mbedAustin | 2:e84c13abc479 | 105 | } |
andresag | 20:fcc752d401ec | 106 | } |