A blue button is always a nice toy ...
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
main.cpp@22:d467526abc4a, 2016-12-18 (annotated)
- Committer:
- hux
- Date:
- Sun Dec 18 05:33:58 2016 +0000
- Revision:
- 22:d467526abc4a
- Parent:
- 21:0e7c08f5386f
- Child:
- 23:677689000369
some new stuff added - works now
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hux | 22:d467526abc4a | 1 | #include <mbed.h> |
hux | 22:d467526abc4a | 2 | #include "ble/BLE.h" |
hux | 22:d467526abc4a | 3 | #include "bricks.h" |
screamer | 0:eb7f02ad28a7 | 4 | |
hux | 22:d467526abc4a | 5 | //============================================================================== |
hux | 22:d467526abc4a | 6 | // Custom Service Definition |
hux | 22:d467526abc4a | 7 | //============================================================================== |
apalmieri | 13:227a0149b677 | 8 | |
hux | 22:d467526abc4a | 9 | typedef union Buffer |
hux | 22:d467526abc4a | 10 | { |
hux | 22:d467526abc4a | 11 | uint8_t ubuf[10]; |
hux | 22:d467526abc4a | 12 | char cbuf[10]; |
hux | 22:d467526abc4a | 13 | uint8_t byte; |
hux | 22:d467526abc4a | 14 | uint16_t word16; |
hux | 22:d467526abc4a | 15 | uint32_t word32; |
hux | 22:d467526abc4a | 16 | int i; |
hux | 22:d467526abc4a | 17 | double d; |
hux | 22:d467526abc4a | 18 | float f; |
hux | 22:d467526abc4a | 19 | } Buffer; |
hux | 22:d467526abc4a | 20 | |
hux | 22:d467526abc4a | 21 | // Service svc(0xA000); // setup GATT service, UUID 0xA000 |
hux | 22:d467526abc4a | 22 | // Characteristic a(svc,0xA001,"r"); // characteristic A with UUID 0xA001 |
hux | 22:d467526abc4a | 23 | // Characteristic x(svc,0xA002,"w"); // characteristic X with UUID 0xA002 |
hux | 22:d467526abc4a | 24 | // Characteristic y(svc,0xA003,"w"); // characteristic Y with UUID 0xA003 |
hux | 22:d467526abc4a | 25 | |
hux | 22:d467526abc4a | 26 | static Buffer adata[1]; |
hux | 22:d467526abc4a | 27 | ReadOnlyArrayGattCharacteristic<Buffer,1> a(0xA001, adata); |
screamer | 0:eb7f02ad28a7 | 28 | |
hux | 22:d467526abc4a | 29 | static Buffer xdata[1]; |
hux | 22:d467526abc4a | 30 | WriteOnlyArrayGattCharacteristic<Buffer,1> x(0xA002, xdata); |
screamer | 0:eb7f02ad28a7 | 31 | |
hux | 22:d467526abc4a | 32 | static Buffer ydata[1]; |
hux | 22:d467526abc4a | 33 | WriteOnlyArrayGattCharacteristic<Buffer,1> y(0xA002, ydata); |
hux | 22:d467526abc4a | 34 | |
hux | 22:d467526abc4a | 35 | GattCharacteristic *characteristics[] = {&a,&x,&y}; |
hux | 22:d467526abc4a | 36 | GattService service(0xA000, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); |
hux | 22:d467526abc4a | 37 | |
hux | 22:d467526abc4a | 38 | //============================================================================== |
hux | 22:d467526abc4a | 39 | // Some Callbacks |
hux | 22:d467526abc4a | 40 | //============================================================================== |
screamer | 0:eb7f02ad28a7 | 41 | |
hux | 22:d467526abc4a | 42 | void onWritten(Blob &blue) // Handle writes to writeCharacteristic |
hux | 22:d467526abc4a | 43 | { |
hux | 22:d467526abc4a | 44 | const GattWriteCallbackParams *p = blue.pWritten; |
hux | 22:d467526abc4a | 45 | |
hux | 22:d467526abc4a | 46 | // Check to see what characteristic was written, by handle |
hux | 22:d467526abc4a | 47 | |
hux | 22:d467526abc4a | 48 | if(p->handle == x.getValueHandle()) |
hux | 22:d467526abc4a | 49 | blink("x x x xxx x ",CONNECTED); |
hux | 22:d467526abc4a | 50 | else if(p->handle == y.getValueHandle()) |
hux | 22:d467526abc4a | 51 | blink("x x x xxx xxx x ",CONNECTED); |
apalmieri | 13:227a0149b677 | 52 | |
hux | 22:d467526abc4a | 53 | // Update the readChar with the value of writeChar |
screamer | 0:eb7f02ad28a7 | 54 | |
hux | 22:d467526abc4a | 55 | //blue.pble->gattServer().write(a.getValueHandle(), p->data, p->len); |
hux | 22:d467526abc4a | 56 | blue.gatt().write(a.getValueHandle(), p->data, p->len); |
hux | 22:d467526abc4a | 57 | } |
hux | 22:d467526abc4a | 58 | |
apalmieri | 13:227a0149b677 | 59 | |
hux | 22:d467526abc4a | 60 | void onConnect(Blob &blue) // Connection Callback |
hux | 22:d467526abc4a | 61 | { |
hux | 22:d467526abc4a | 62 | blink("x x x ",CONNECTED); // indicate advertising |
hux | 22:d467526abc4a | 63 | } |
apalmieri | 13:227a0149b677 | 64 | |
screamer | 0:eb7f02ad28a7 | 65 | |
hux | 22:d467526abc4a | 66 | void onDisconnect(Blob &blue) // Disconnection Callback |
hux | 22:d467526abc4a | 67 | { |
hux | 22:d467526abc4a | 68 | blue.start(); // start advertising on client disconnect |
hux | 22:d467526abc4a | 69 | blink(ADVERTISE); // indicate advertising |
hux | 22:d467526abc4a | 70 | } |
hux | 22:d467526abc4a | 71 | |
screamer | 0:eb7f02ad28a7 | 72 | |
hux | 22:d467526abc4a | 73 | void onError(Blob &blue) // Error Reporting Callback |
hux | 22:d467526abc4a | 74 | { |
hux | 22:d467526abc4a | 75 | (void) blue; // Avoid compiler warnings |
hux | 22:d467526abc4a | 76 | blink(FAULT); // indicate advertising |
hux | 22:d467526abc4a | 77 | } |
hux | 22:d467526abc4a | 78 | |
screamer | 0:eb7f02ad28a7 | 79 | |
hux | 22:d467526abc4a | 80 | void onSetup(Blob &blue) // Immediately After Initializing BLE |
hux | 22:d467526abc4a | 81 | { |
hux | 22:d467526abc4a | 82 | blue.device("nRF51-DK"); // setup device name |
hux | 22:d467526abc4a | 83 | blue.data("ABCDEF"); // setup advertising data |
hux | 22:d467526abc4a | 84 | blue.name("T06.0 Custom GATT"); // setup advertising name |
screamer | 0:eb7f02ad28a7 | 85 | |
hux | 22:d467526abc4a | 86 | blue.onConnect(onConnect); // setup connection callback |
hux | 22:d467526abc4a | 87 | blue.onDisconnect(onDisconnect); // setup disconnection callback |
hux | 22:d467526abc4a | 88 | blue.onWritten(onWritten); // setup data written callback |
hux | 22:d467526abc4a | 89 | |
hux | 22:d467526abc4a | 90 | //blue.service(svc); // setup custom service |
hux | 22:d467526abc4a | 91 | blue.service(service); // setup custom service |
hux | 22:d467526abc4a | 92 | |
hux | 22:d467526abc4a | 93 | blue.advertise("C:ng",100); // start advertising @ 100 msec interval |
hux | 22:d467526abc4a | 94 | blink(ADVERTISE); // show that board is advertising |
hux | 22:d467526abc4a | 95 | } |
apalmieri | 13:227a0149b677 | 96 | |
hux | 22:d467526abc4a | 97 | //============================================================================== |
hux | 22:d467526abc4a | 98 | // Main Program |
hux | 22:d467526abc4a | 99 | //============================================================================== |
hux | 22:d467526abc4a | 100 | |
hux | 22:d467526abc4a | 101 | int main(void) |
hux | 22:d467526abc4a | 102 | { |
hux | 22:d467526abc4a | 103 | blink(IDLE); // idle blinking - just started! |
apalmieri | 13:227a0149b677 | 104 | |
hux | 22:d467526abc4a | 105 | Blob blue; // declare a blob (BLe OBject) |
hux | 22:d467526abc4a | 106 | blue.init(onSetup,onError); // init BLE base layer, always do first |
hux | 22:d467526abc4a | 107 | |
hux | 22:d467526abc4a | 108 | while (true) // Infinite loop waiting for BLE events |
hux | 22:d467526abc4a | 109 | blue.sleep(); // low power waiting for BLE events |
hux | 22:d467526abc4a | 110 | } |
apalmieri | 14:f715c13eb84f | 111 |