for camera
Dependencies: BLE_API mbed nRF51822
Fork of BLE_GATT_Example by
main.cpp@29:48287117fa46, 2017-06-10 (annotated)
- Committer:
- andreydung
- Date:
- Sat Jun 10 03:05:02 2017 +0000
- Revision:
- 29:48287117fa46
- Parent:
- 28:382c630bae02
- Child:
- 30:27466f74458c
worked!!!
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" |
mbedAustin | 1:94152e7d8b5c | 3 | |
andreydung | 27:ed42554cf870 | 4 | DigitalOut led(P0_19, 1); |
mbedAustin | 9:b33f42191584 | 5 | uint16_t customServiceUUID = 0xA000; |
mbedAustin | 13:62b1d32745ac | 6 | uint16_t readCharUUID = 0xA001; |
mbedAustin | 9:b33f42191584 | 7 | uint16_t writeCharUUID = 0xA002; |
mbedAustin | 1:94152e7d8b5c | 8 | |
andreydung | 28:382c630bae02 | 9 | const static char DEVICE_NAME[] = "timesync1"; // change this |
andreydung | 27:ed42554cf870 | 10 | static const uint16_t uuid16_list[] = {0xFFFF}; //Custom UUID, FFFF is reserved for development |
mbedAustin | 1:94152e7d8b5c | 11 | |
mbedAustin | 9:b33f42191584 | 12 | static uint8_t writeValue[10] = {0}; |
mbedAustin | 12:6d1f77d0cb37 | 13 | WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue); |
mbedAustin | 2:e84c13abc479 | 14 | |
andresag | 22:406127954d1f | 15 | /* Set up custom service */ |
andreydung | 28:382c630bae02 | 16 | GattCharacteristic *characteristics[] = {&writeChar}; |
mbedAustin | 9:b33f42191584 | 17 | GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); |
mbedAustin | 2:e84c13abc479 | 18 | |
andreydung | 27:ed42554cf870 | 19 | /* |
andreydung | 27:ed42554cf870 | 20 | * Restart advertising when phone app disconnects |
andreydung | 27:ed42554cf870 | 21 | */ |
andresag | 19:477567297aac | 22 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) |
mbedAustin | 1:94152e7d8b5c | 23 | { |
andreydung | 27:ed42554cf870 | 24 | led = 1; // just in case |
andresag | 22:406127954d1f | 25 | BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); |
mbedAustin | 1:94152e7d8b5c | 26 | } |
mbedAustin | 0:cd5b6733aeb1 | 27 | |
andreydung | 27:ed42554cf870 | 28 | /* |
andreydung | 27:ed42554cf870 | 29 | * Handle writes to writeCharacteristic |
andreydung | 27:ed42554cf870 | 30 | */ |
melmon | 18:7d89c4fba362 | 31 | void writeCharCallback(const GattWriteCallbackParams *params) |
mbedAustin | 3:0fb60f81f693 | 32 | { |
andresag | 22:406127954d1f | 33 | /* Check to see what characteristic was written, by handle */ |
melmon | 18:7d89c4fba362 | 34 | if(params->handle == writeChar.getValueHandle()) { |
andreydung | 27:ed42554cf870 | 35 | // since LED is pullup mode |
andreydung | 27:ed42554cf870 | 36 | led = 0; |
andreydung | 27:ed42554cf870 | 37 | wait(1.0); |
andreydung | 27:ed42554cf870 | 38 | led = 1; |
mbedAustin | 8:60ede963dfe2 | 39 | } |
mbedAustin | 3:0fb60f81f693 | 40 | } |
andreydung | 28:382c630bae02 | 41 | |
andreydung | 28:382c630bae02 | 42 | |
andresag | 22:406127954d1f | 43 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
andresag | 22:406127954d1f | 44 | { |
andresag | 22:406127954d1f | 45 | BLE &ble = params->ble; |
andresag | 22:406127954d1f | 46 | ble_error_t error = params->error; |
andresag | 22:406127954d1f | 47 | |
andresag | 22:406127954d1f | 48 | if (error != BLE_ERROR_NONE) { |
andresag | 22:406127954d1f | 49 | return; |
andresag | 22:406127954d1f | 50 | } |
mbedAustin | 0:cd5b6733aeb1 | 51 | |
andresag | 19:477567297aac | 52 | ble.gap().onDisconnection(disconnectionCallback); |
andresag | 19:477567297aac | 53 | ble.gattServer().onDataWritten(writeCharCallback); |
mbedAustin | 2:e84c13abc479 | 54 | |
andresag | 22:406127954d1f | 55 | /* Setup advertising */ |
andresag | 19:477567297aac | 56 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT |
andresag | 19:477567297aac | 57 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type |
andresag | 19:477567297aac | 58 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name |
andresag | 19:477567297aac | 59 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet |
andreydung | 29:48287117fa46 | 60 | ble.gap().setAdvertisingInterval(500); // 500ms. |
mbedAustin | 2:e84c13abc479 | 61 | |
andresag | 22:406127954d1f | 62 | /* Add our custom service */ |
mbedAustin | 13:62b1d32745ac | 63 | ble.addService(customService); |
mbedAustin | 2:e84c13abc479 | 64 | |
andresag | 22:406127954d1f | 65 | /* Start advertising */ |
andresag | 19:477567297aac | 66 | ble.gap().startAdvertising(); |
andresag | 22:406127954d1f | 67 | } |
andresag | 19:477567297aac | 68 | |
andresag | 22:406127954d1f | 69 | /* |
andresag | 22:406127954d1f | 70 | * Main loop |
andresag | 22:406127954d1f | 71 | */ |
andresag | 22:406127954d1f | 72 | int main(void) |
andreydung | 29:48287117fa46 | 73 | { |
andreydung | 29:48287117fa46 | 74 | NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos; |
andreydung | 29:48287117fa46 | 75 | NRF_POWER->TASKS_LOWPWR = 1; |
andreydung | 29:48287117fa46 | 76 | NRF_ADC->ENABLE = (ADC_ENABLE_ENABLE_Disabled << ADC_ENABLE_ENABLE_Pos); |
andreydung | 29:48287117fa46 | 77 | NRF_POWER->DCDCEN = 0x00000001; |
andreydung | 29:48287117fa46 | 78 | |
andresag | 22:406127954d1f | 79 | BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); |
andresag | 22:406127954d1f | 80 | ble.init(bleInitComplete); |
andreydung | 29:48287117fa46 | 81 | ble.setTxPower(-20); |
andresag | 22:406127954d1f | 82 | |
andresag | 22:406127954d1f | 83 | /* SpinWait for initialization to complete. This is necessary because the |
andresag | 22:406127954d1f | 84 | * BLE object is used in the main loop below. */ |
andresag | 22:406127954d1f | 85 | while (ble.hasInitialized() == false) { /* spin loop */ } |
andresag | 22:406127954d1f | 86 | |
andresag | 22:406127954d1f | 87 | /* Infinite loop waiting for BLE interrupt events */ |
mbedAustin | 2:e84c13abc479 | 88 | while (true) { |
andresag | 22:406127954d1f | 89 | ble.waitForEvent(); /* Save power */ |
mbedAustin | 2:e84c13abc479 | 90 | } |
andresag | 20:fcc752d401ec | 91 | } |