A blue button is always a nice toy ...
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
main.cpp@24:0f08f68579bd, 2017-01-06 (annotated)
- Committer:
- hux
- Date:
- Fri Jan 06 20:49:58 2017 +0000
- Revision:
- 24:0f08f68579bd
- Parent:
- 23:677689000369
- Child:
- 25:231d3f382583
Version 1.0 - now satisfying behavior. No known bugs!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hux | 24:0f08f68579bd | 1 | // T07N_Easy_GATT - Easy GATT setup using bricks |
hux | 24:0f08f68579bd | 2 | |
hux | 22:d467526abc4a | 3 | #include "bricks.h" |
screamer | 0:eb7f02ad28a7 | 4 | |
hux | 22:d467526abc4a | 5 | //============================================================================== |
hux | 24:0f08f68579bd | 6 | // Custom Service Setup |
hux | 22:d467526abc4a | 7 | //============================================================================== |
apalmieri | 13:227a0149b677 | 8 | |
hux | 24:0f08f68579bd | 9 | // Detection Service |
hux | 23:677689000369 | 10 | |
hux | 24:0f08f68579bd | 11 | Service detection(0xA010,"Detection"); // Detection Service |
screamer | 0:eb7f02ad28a7 | 12 | |
hux | 24:0f08f68579bd | 13 | Characteristic<Digital> chrPresence(detection,0xA011, "rn", "Presence"); |
hux | 24:0f08f68579bd | 14 | Characteristic<DateTime> chrTimestamp(detection,0xA012, "r", "Timestamp"); |
hux | 23:677689000369 | 15 | |
hux | 24:0f08f68579bd | 16 | // Address Service |
hux | 24:0f08f68579bd | 17 | |
hux | 24:0f08f68579bd | 18 | Service address(0xA020,"Adress"); // Address Service |
hux | 23:677689000369 | 19 | |
hux | 24:0f08f68579bd | 20 | Characteristic<Digital> chrId(address, 0xA021, "ran", "ID"); |
hux | 24:0f08f68579bd | 21 | Characteristic<Digital> chrName(address, 0xA022, "ran", "Name"); |
hux | 24:0f08f68579bd | 22 | Characteristic<Digital> chrLayout(address, 0xA023, "ran", "Layout"); |
hux | 24:0f08f68579bd | 23 | |
hux | 24:0f08f68579bd | 24 | // Debug Service |
hux | 24:0f08f68579bd | 25 | |
hux | 24:0f08f68579bd | 26 | Service debug(0xA030,"Debug"); // Debug Service |
hux | 24:0f08f68579bd | 27 | |
hux | 24:0f08f68579bd | 28 | Characteristic<Digital> chrTest (debug,0xA031, "w", "Test"); |
screamer | 0:eb7f02ad28a7 | 29 | |
hux | 24:0f08f68579bd | 30 | // Configure all supported services |
hux | 24:0f08f68579bd | 31 | |
hux | 24:0f08f68579bd | 32 | void services(Blob &o) // configure all supported services |
hux | 24:0f08f68579bd | 33 | { |
hux | 24:0f08f68579bd | 34 | address.config(o); // configure Address Service |
hux | 24:0f08f68579bd | 35 | detection.config(o); // configure Detection Service |
hux | 24:0f08f68579bd | 36 | debug.config(o); // configure Debug Service |
hux | 24:0f08f68579bd | 37 | } |
hux | 24:0f08f68579bd | 38 | |
hux | 22:d467526abc4a | 39 | //============================================================================== |
hux | 24:0f08f68579bd | 40 | // Callbacks |
hux | 22:d467526abc4a | 41 | //============================================================================== |
screamer | 0:eb7f02ad28a7 | 42 | |
hux | 24:0f08f68579bd | 43 | void onError(Blob &o) // Error Reporting Callback |
hux | 23:677689000369 | 44 | { |
hux | 24:0f08f68579bd | 45 | (void) o; // Avoid compiler warnings |
hux | 24:0f08f68579bd | 46 | blink(FAULT); // indicate advertising |
hux | 24:0f08f68579bd | 47 | } |
apalmieri | 13:227a0149b677 | 48 | |
hux | 22:d467526abc4a | 49 | void onConnect(Blob &blue) // Connection Callback |
hux | 22:d467526abc4a | 50 | { |
hux | 23:677689000369 | 51 | blink("x x x ",CONNECTED); // indicate advertising |
hux | 22:d467526abc4a | 52 | } |
apalmieri | 13:227a0149b677 | 53 | |
hux | 22:d467526abc4a | 54 | void onDisconnect(Blob &blue) // Disconnection Callback |
hux | 22:d467526abc4a | 55 | { |
hux | 22:d467526abc4a | 56 | blue.start(); // start advertising on client disconnect |
hux | 22:d467526abc4a | 57 | blink(ADVERTISE); // indicate advertising |
hux | 22:d467526abc4a | 58 | } |
hux | 22:d467526abc4a | 59 | |
hux | 24:0f08f68579bd | 60 | void onWritten(Blob &o) // Handle writes to writeCharacteristic |
hux | 22:d467526abc4a | 61 | { |
hux | 24:0f08f68579bd | 62 | if (updated(o,chrTest)) // has chrTest been updated? |
hux | 24:0f08f68579bd | 63 | { Digital value; |
hux | 24:0f08f68579bd | 64 | |
hux | 24:0f08f68579bd | 65 | get(o,chrTest,value); // get value of chrTest |
hux | 24:0f08f68579bd | 66 | set(o,chrPresence,value); // and store this value to chrPresence |
hux | 24:0f08f68579bd | 67 | |
hux | 24:0f08f68579bd | 68 | print(o,chrTest,"test"); |
hux | 24:0f08f68579bd | 69 | blink("x x x xxx xxx xxx x ",CONNECTED); |
hux | 24:0f08f68579bd | 70 | } |
hux | 24:0f08f68579bd | 71 | } |
screamer | 0:eb7f02ad28a7 | 72 | |
hux | 24:0f08f68579bd | 73 | void onSetup(Blob &o) // Immediately After Initializing BLE |
hux | 24:0f08f68579bd | 74 | { |
hux | 24:0f08f68579bd | 75 | services(o); // config all services |
hux | 24:0f08f68579bd | 76 | |
hux | 24:0f08f68579bd | 77 | o.onConnect(onConnect); // setup connection callback |
hux | 24:0f08f68579bd | 78 | o.onDisconnect(onDisconnect); // setup disconnection callback |
hux | 24:0f08f68579bd | 79 | o.onWritten(onWritten); // setup data written callback |
hux | 24:0f08f68579bd | 80 | |
hux | 24:0f08f68579bd | 81 | device(o,"nRF51-DK"); // setup device name |
hux | 24:0f08f68579bd | 82 | data(o,"ABCDEF"); // setup advertising data |
hux | 24:0f08f68579bd | 83 | name(o,"T07N#1.0 Easy GATT"); // setup advertising name |
hux | 24:0f08f68579bd | 84 | |
hux | 24:0f08f68579bd | 85 | peripheral(o,"C:ng",100); // start advertising @ 100 msec interval |
hux | 22:d467526abc4a | 86 | blink(ADVERTISE); // show that board is advertising |
hux | 22:d467526abc4a | 87 | } |
hux | 24:0f08f68579bd | 88 | |
hux | 22:d467526abc4a | 89 | //============================================================================== |
hux | 22:d467526abc4a | 90 | // Main Program |
hux | 22:d467526abc4a | 91 | //============================================================================== |
hux | 22:d467526abc4a | 92 | |
hux | 22:d467526abc4a | 93 | int main(void) |
hux | 22:d467526abc4a | 94 | { |
hux | 23:677689000369 | 95 | verbose(10); // print all verbose messages |
hux | 22:d467526abc4a | 96 | blink(IDLE); // idle blinking - just started! |
apalmieri | 13:227a0149b677 | 97 | |
hux | 23:677689000369 | 98 | Blob blue; // declare a blob (BLE OBject) |
hux | 22:d467526abc4a | 99 | blue.init(onSetup,onError); // init BLE base layer, always do first |
hux | 22:d467526abc4a | 100 | |
hux | 22:d467526abc4a | 101 | while (true) // Infinite loop waiting for BLE events |
hux | 22:d467526abc4a | 102 | blue.sleep(); // low power waiting for BLE events |
hux | 24:0f08f68579bd | 103 | } |