for camera

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_GATT_Example by Bluetooth Low Energy

Committer:
andreydung
Date:
Tue Jun 06 00:48:09 2017 +0000
Revision:
25:56d5b129dfd6
Parent:
24:64e7d23021ba
Child:
26:b3e90d7672f8
wait done. Need board to test!!

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
andresag 19:477567297aac 4 DigitalOut led(LED1, 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 24:64e7d23021ba 9 #define LEN_SEQUENCE 5
andreydung 25:56d5b129dfd6 10 #define NUM_CASES 6
andreydung 24:64e7d23021ba 11 const uint8_t lookUpTable[NUM_CASES][LEN_SEQUENCE] = { {1,0,0,0,1},
andreydung 23:8cd0bcd06533 12 {1,0,0,1,1},
andreydung 23:8cd0bcd06533 13 {1,1,0,0,1},
andreydung 23:8cd0bcd06533 14 {1,0,1,0,1},
andreydung 24:64e7d23021ba 15 {1,1,0,1,1},
andreydung 23:8cd0bcd06533 16 {1,1,0,1,1}};
andreydung 23:8cd0bcd06533 17
andreydung 23:8cd0bcd06533 18 const static char DEVICE_NAME[] = "timesync1"; // change this
mbedAustin 8:60ede963dfe2 19 static const uint16_t uuid16_list[] = {0xFFFF}; //Custom UUID, FFFF is reserved for development
mbedAustin 1:94152e7d8b5c 20
andresag 22:406127954d1f 21 /* Set Up custom Characteristics */
mbedAustin 9:b33f42191584 22 static uint8_t readValue[10] = {0};
mbedAustin 12:6d1f77d0cb37 23 ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue);
mbedAustin 2:e84c13abc479 24
mbedAustin 9:b33f42191584 25 static uint8_t writeValue[10] = {0};
mbedAustin 12:6d1f77d0cb37 26 WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue);
mbedAustin 2:e84c13abc479 27
andresag 22:406127954d1f 28 /* Set up custom service */
mbedAustin 8:60ede963dfe2 29 GattCharacteristic *characteristics[] = {&readChar, &writeChar};
mbedAustin 9:b33f42191584 30 GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
mbedAustin 2:e84c13abc479 31
mbedAustin 2:e84c13abc479 32
mbedAustin 8:60ede963dfe2 33 /*
mbedAustin 8:60ede963dfe2 34 * Restart advertising when phone app disconnects
andresag 19:477567297aac 35 */
andresag 19:477567297aac 36 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
mbedAustin 1:94152e7d8b5c 37 {
andresag 22:406127954d1f 38 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
mbedAustin 1:94152e7d8b5c 39 }
mbedAustin 0:cd5b6733aeb1 40
andresag 19:477567297aac 41 /*
andresag 22:406127954d1f 42 * Handle writes to writeCharacteristic
mbedAustin 8:60ede963dfe2 43 */
melmon 18:7d89c4fba362 44 void writeCharCallback(const GattWriteCallbackParams *params)
mbedAustin 3:0fb60f81f693 45 {
andresag 22:406127954d1f 46 /* Check to see what characteristic was written, by handle */
melmon 18:7d89c4fba362 47 if(params->handle == writeChar.getValueHandle()) {
andresag 22:406127954d1f 48 /* toggle LED if only 1 byte is written */
andreydung 24:64e7d23021ba 49 // if(params->len == 1) {
andreydung 24:64e7d23021ba 50 // led = params->data[0];
andreydung 24:64e7d23021ba 51 // (params->data[0] == 0x00) ? printf("led on\n\r") : printf("led off\n\r"); // print led toggle
andreydung 24:64e7d23021ba 52 // }
andreydung 24:64e7d23021ba 53 // /* Print the data if more than 1 byte is written */
andreydung 24:64e7d23021ba 54 // else {
andreydung 24:64e7d23021ba 55 // printf("Data received: length = %d, data = 0x",params->len);
andreydung 24:64e7d23021ba 56 // for(int x=0; x < params->len; x++) {
andreydung 24:64e7d23021ba 57 // printf("%x", params->data[x]);
andreydung 24:64e7d23021ba 58 // }
andreydung 24:64e7d23021ba 59 // printf("\n\r");
andreydung 24:64e7d23021ba 60 // }
andreydung 24:64e7d23021ba 61 // lookup table
andreydung 24:64e7d23021ba 62 if (params->len == 1) {
andreydung 24:64e7d23021ba 63 int instruction = params->data[0];
andreydung 24:64e7d23021ba 64 if ((instruction >= 0) && (instruction < NUM_CASES)) {
andreydung 24:64e7d23021ba 65 for (int i = 0; i < LEN_SEQUENCE; i++) {
andreydung 24:64e7d23021ba 66 led = lookUpTable[instruction][i];
andreydung 25:56d5b129dfd6 67 wait(0.5);
andreydung 24:64e7d23021ba 68 }
andreydung 24:64e7d23021ba 69 }
mbedAustin 8:60ede963dfe2 70 }
andreydung 24:64e7d23021ba 71
andresag 22:406127954d1f 72 /* Update the readChar with the value of writeChar */
andresag 22:406127954d1f 73 BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(readChar.getValueHandle(), params->data, params->len);
mbedAustin 8:60ede963dfe2 74 }
mbedAustin 3:0fb60f81f693 75 }
andresag 22:406127954d1f 76 /*
andresag 22:406127954d1f 77 * Initialization callback
andresag 22:406127954d1f 78 */
andresag 22:406127954d1f 79 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
andresag 22:406127954d1f 80 {
andresag 22:406127954d1f 81 BLE &ble = params->ble;
andresag 22:406127954d1f 82 ble_error_t error = params->error;
andresag 22:406127954d1f 83
andresag 22:406127954d1f 84 if (error != BLE_ERROR_NONE) {
andresag 22:406127954d1f 85 return;
andresag 22:406127954d1f 86 }
mbedAustin 0:cd5b6733aeb1 87
andresag 19:477567297aac 88 ble.gap().onDisconnection(disconnectionCallback);
andresag 19:477567297aac 89 ble.gattServer().onDataWritten(writeCharCallback);
mbedAustin 2:e84c13abc479 90
andresag 22:406127954d1f 91 /* Setup advertising */
andresag 19:477567297aac 92 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT
andresag 19:477567297aac 93 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type
andresag 19:477567297aac 94 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name
andresag 19:477567297aac 95 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 96 ble.gap().setAdvertisingInterval(100); // 100ms.
mbedAustin 2:e84c13abc479 97
andresag 22:406127954d1f 98 /* Add our custom service */
mbedAustin 13:62b1d32745ac 99 ble.addService(customService);
mbedAustin 2:e84c13abc479 100
andresag 22:406127954d1f 101 /* Start advertising */
andresag 19:477567297aac 102 ble.gap().startAdvertising();
andresag 22:406127954d1f 103 }
andresag 19:477567297aac 104
andresag 22:406127954d1f 105 /*
andresag 22:406127954d1f 106 * Main loop
andresag 22:406127954d1f 107 */
andresag 22:406127954d1f 108 int main(void)
andresag 22:406127954d1f 109 {
andresag 22:406127954d1f 110 /* initialize stuff */
andresag 22:406127954d1f 111 printf("\n\r********* Starting Main Loop *********\n\r");
andresag 22:406127954d1f 112
andresag 22:406127954d1f 113 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
andresag 22:406127954d1f 114 ble.init(bleInitComplete);
andresag 22:406127954d1f 115
andresag 22:406127954d1f 116 /* SpinWait for initialization to complete. This is necessary because the
andresag 22:406127954d1f 117 * BLE object is used in the main loop below. */
andresag 22:406127954d1f 118 while (ble.hasInitialized() == false) { /* spin loop */ }
andresag 22:406127954d1f 119
andresag 22:406127954d1f 120 /* Infinite loop waiting for BLE interrupt events */
mbedAustin 2:e84c13abc479 121 while (true) {
andresag 22:406127954d1f 122 ble.waitForEvent(); /* Save power */
mbedAustin 2:e84c13abc479 123 }
andresag 20:fcc752d401ec 124 }