for camera

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_GATT_Example by Bluetooth Low Energy

Committer:
andreydung
Date:
Wed Jun 14 21:35:52 2017 +0000
Revision:
34:0a5b9e4e7680
Parent:
33:65b7bedb0877
4 TX power

Who changed what in which revision?

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