BLE project for PILLAR
Dependencies: BLE_API mbed simpleBLEControl
Fork of BLE_API_DEMO by
main.cpp@50:4c02add9abba, 2014-10-22 (annotated)
- Committer:
- nebgnahz
- Date:
- Wed Oct 22 17:08:57 2014 +0000
- Revision:
- 50:4c02add9abba
- Parent:
- 49:14b2df099dfc
- Child:
- 51:0bf1116bca52
use random generated UUID from http://www.itu.int/ITU-T/asn1/uuid.html
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ktownsend | 0:87a7fc231fae | 1 | #include "mbed.h" |
Rohit Grover |
10:2436164b692e | 2 | #include "BLEDevice.h" |
ktownsend | 0:87a7fc231fae | 3 | |
Rohit Grover |
10:2436164b692e | 4 | BLEDevice ble; |
rgrover1 | 47:430545f41113 | 5 | DigitalOut led1(LED1); |
ktownsend | 0:87a7fc231fae | 6 | |
nebgnahz | 50:4c02add9abba | 7 | const uint8_t LED1_UUID[LENGTH_OF_LONG_UUID] = { |
nebgnahz | 50:4c02add9abba | 8 | 0xfb, 0x71, 0xbc, 0xc0, 0x5a, 0x0c, 0x11, 0xe4, |
nebgnahz | 50:4c02add9abba | 9 | 0x91, 0xae, 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b |
nebgnahz | 50:4c02add9abba | 10 | }; |
nebgnahz | 50:4c02add9abba | 11 | const uint8_t BUTTON_UUID[LENGTH_OF_LONG_UUID] = { |
nebgnahz | 50:4c02add9abba | 12 | 0x7a, 0x77, 0xbe, 0x20, 0x5a, 0x0d, 0x11, 0xe4, |
nebgnahz | 50:4c02add9abba | 13 | 0xa9, 0x5e, 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b |
nebgnahz | 50:4c02add9abba | 14 | }; |
nebgnahz | 50:4c02add9abba | 15 | const uint8_t TEST_SERVICE_UUID[LENGTH_OF_LONG_UUID] = { |
nebgnahz | 50:4c02add9abba | 16 | 0xb0, 0xbb, 0x58, 0x20, 0x5a, 0x0d, 0x11, 0xe4, |
nebgnahz | 50:4c02add9abba | 17 | 0x93, 0xee, 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b |
nebgnahz | 50:4c02add9abba | 18 | }; |
nebgnahz | 50:4c02add9abba | 19 | |
nebgnahz | 50:4c02add9abba | 20 | const static char DEVICE_NAME[] = "Nordic"; |
nebgnahz | 48:908b4ec086ba | 21 | static volatile bool is_button_pressed = false; |
nebgnahz | 48:908b4ec086ba | 22 | static volatile uint16_t led1_handler; |
nebgnahz | 48:908b4ec086ba | 23 | |
nebgnahz | 48:908b4ec086ba | 24 | uint8_t led_state, button_state; |
Rohit Grover |
36:ea2a1b4f51c1 | 25 | |
rgrover1 | 41:9cef0129da5f | 26 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
ktownsend | 0:87a7fc231fae | 27 | { |
rgrover1 | 46:ee7c55907f36 | 28 | ble.startAdvertising(); // restart advertising |
rgrover1 | 7:daab8ba5139e | 29 | } |
Rohit Grover |
3:24e2b056d229 | 30 | |
nebgnahz | 48:908b4ec086ba | 31 | void changeLED(const GattCharacteristicWriteCBParams *eventDataP) { |
nebgnahz | 49:14b2df099dfc | 32 | // eventDataP->charHandle is just uint16_t |
nebgnahz | 49:14b2df099dfc | 33 | // it's used to dispatch the callbacks |
nebgnahz | 48:908b4ec086ba | 34 | if (eventDataP->charHandle == led1_handler) { |
nebgnahz | 48:908b4ec086ba | 35 | led1 = eventDataP->data[0] % 2; |
nebgnahz | 48:908b4ec086ba | 36 | } |
nebgnahz | 48:908b4ec086ba | 37 | } |
rgrover1 | 47:430545f41113 | 38 | |
nebgnahz | 48:908b4ec086ba | 39 | void button1Pressed() { |
nebgnahz | 48:908b4ec086ba | 40 | button_state = 1; |
nebgnahz | 48:908b4ec086ba | 41 | is_button_pressed = true; |
nebgnahz | 48:908b4ec086ba | 42 | } |
nebgnahz | 48:908b4ec086ba | 43 | void button2Pressed() { |
nebgnahz | 48:908b4ec086ba | 44 | button_state = 2; |
nebgnahz | 48:908b4ec086ba | 45 | is_button_pressed = true; |
Rohit Grover |
11:1d9aafee4984 | 46 | } |
Rohit Grover |
11:1d9aafee4984 | 47 | |
ktownsend | 0:87a7fc231fae | 48 | int main(void) |
ktownsend | 0:87a7fc231fae | 49 | { |
nebgnahz | 48:908b4ec086ba | 50 | // button initialization |
nebgnahz | 48:908b4ec086ba | 51 | InterruptIn button1(BUTTON1); |
nebgnahz | 48:908b4ec086ba | 52 | InterruptIn button2(BUTTON2); |
nebgnahz | 48:908b4ec086ba | 53 | button1.mode(PullUp); |
nebgnahz | 48:908b4ec086ba | 54 | button2.mode(PullUp); |
nebgnahz | 48:908b4ec086ba | 55 | button1.rise(&button1Pressed); |
nebgnahz | 48:908b4ec086ba | 56 | button2.rise(&button2Pressed); |
nebgnahz | 48:908b4ec086ba | 57 | led1 = 0; |
ktownsend | 0:87a7fc231fae | 58 | |
nebgnahz | 48:908b4ec086ba | 59 | // just a simple service example |
nebgnahz | 48:908b4ec086ba | 60 | // o led1 characteristics, you can write from the phone to control led1 |
nebgnahz | 48:908b4ec086ba | 61 | // o button characteristics, you can read and get notified |
nebgnahz | 48:908b4ec086ba | 62 | GattCharacteristic led1_characteristics( |
nebgnahz | 50:4c02add9abba | 63 | LED1_UUID, &led_state, sizeof(led_state), sizeof(led_state), |
nebgnahz | 48:908b4ec086ba | 64 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
nebgnahz | 48:908b4ec086ba | 65 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); |
nebgnahz | 48:908b4ec086ba | 66 | led1_handler = led1_characteristics.getValueAttribute().getHandle(); |
rgrover1 | 45:98c5a34b07a4 | 67 | |
nebgnahz | 48:908b4ec086ba | 68 | GattCharacteristic button_characteristics( |
nebgnahz | 50:4c02add9abba | 69 | BUTTON_UUID, &button_state, sizeof(button_state), sizeof(button_state), |
nebgnahz | 48:908b4ec086ba | 70 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | |
nebgnahz | 48:908b4ec086ba | 71 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
nebgnahz | 48:908b4ec086ba | 72 | |
nebgnahz | 50:4c02add9abba | 73 | const uint8_t TEST_SERVICE_UUID[LENGTH_OF_LONG_UUID] = { |
nebgnahz | 50:4c02add9abba | 74 | 0xb0, 0xbb, 0x58, 0x20, 0x5a, 0x0d, 0x11, 0xe4, |
nebgnahz | 50:4c02add9abba | 75 | 0x93, 0xee, 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}; |
nebgnahz | 48:908b4ec086ba | 76 | GattCharacteristic *charTable[] = {&led1_characteristics, &button_characteristics}; |
nebgnahz | 50:4c02add9abba | 77 | GattService testService(TEST_SERVICE_UUID, charTable, |
nebgnahz | 49:14b2df099dfc | 78 | sizeof(charTable) / sizeof(GattCharacteristic *)); |
nebgnahz | 50:4c02add9abba | 79 | |
nebgnahz | 48:908b4ec086ba | 80 | // BLE setup, mainly we add service and callbacks |
nebgnahz | 48:908b4ec086ba | 81 | ble.init(); |
nebgnahz | 48:908b4ec086ba | 82 | ble.addService(testService); |
nebgnahz | 48:908b4ec086ba | 83 | ble.onDataWritten(&changeLED); |
nebgnahz | 48:908b4ec086ba | 84 | ble.onDisconnection(disconnectionCallback); |
nebgnahz | 48:908b4ec086ba | 85 | |
nebgnahz | 48:908b4ec086ba | 86 | // setup advertising |
nebgnahz | 49:14b2df099dfc | 87 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | |
nebgnahz | 50:4c02add9abba | 88 | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
nebgnahz | 49:14b2df099dfc | 89 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, |
nebgnahz | 49:14b2df099dfc | 90 | (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
rgrover1 | 7:daab8ba5139e | 91 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
rgrover1 | 40:e73130c6f2bb | 92 | ble.setAdvertisingInterval(1600); /* 1000ms; in multiples of 0.625ms. */ |
rgrover1 | 7:daab8ba5139e | 93 | ble.startAdvertising(); |
Rohit Grover |
3:24e2b056d229 | 94 | |
Rohit Grover |
11:1d9aafee4984 | 95 | while (true) { |
nebgnahz | 48:908b4ec086ba | 96 | if (is_button_pressed) { |
nebgnahz | 49:14b2df099dfc | 97 | // if button pressed, we update the characteristics |
nebgnahz | 48:908b4ec086ba | 98 | is_button_pressed = false; |
nebgnahz | 48:908b4ec086ba | 99 | ble.updateCharacteristicValue(button_characteristics.getValueAttribute().getHandle(), |
nebgnahz | 48:908b4ec086ba | 100 | &button_state, sizeof(button_state)); |
Rohit Grover |
36:ea2a1b4f51c1 | 101 | } else { |
Rohit Grover |
36:ea2a1b4f51c1 | 102 | ble.waitForEvent(); |
Rohit Grover |
36:ea2a1b4f51c1 | 103 | } |
ktownsend | 0:87a7fc231fae | 104 | } |
nebgnahz | 50:4c02add9abba | 105 | } |