GAP based TOF Demo
Dependencies: BLE_API X_NUCLEO_6180XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
main.cpp@29:0f068d70c1a5, 2018-08-20 (annotated)
- Committer:
- hux
- Date:
- Mon Aug 20 16:42:06 2018 +0000
- Revision:
- 29:0f068d70c1a5
- Parent:
- 28:86a594313434
made in Shanghai
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hux | 28:86a594313434 | 1 | // S13_TOF_GAP - ToF Sensor @ GAP based on X-NUCLEO-6180XA1 Board |
hux | 28:86a594313434 | 2 | |
hux | 28:86a594313434 | 3 | #include <mbed.h> |
hux | 28:86a594313434 | 4 | #include "bricks/bricks.h" |
hux | 28:86a594313434 | 5 | #include "bricks/blinker.h" |
hux | 28:86a594313434 | 6 | //#include "bricks/stop.h" |
hux | 28:86a594313434 | 7 | #include "shields/shields.h" |
hux | 28:86a594313434 | 8 | |
hux | 28:86a594313434 | 9 | typedef DisplayShield Dish; // a short hand for DisplayShield |
hux | 24:0f08f68579bd | 10 | |
hux | 28:86a594313434 | 11 | Dish *pShield; // to access our 6180XA1 DIsplay SHield |
hux | 28:86a594313434 | 12 | //StopButton stop(USER_BUTTON); // press blue button for stop |
hux | 28:86a594313434 | 13 | |
hux | 28:86a594313434 | 14 | //============================================================================== |
hux | 28:86a594313434 | 15 | // Data Ready Callback |
hux | 28:86a594313434 | 16 | //============================================================================== |
hux | 28:86a594313434 | 17 | |
hux | 28:86a594313434 | 18 | static void cbReady(void) // called if measurement done, data ready |
hux | 28:86a594313434 | 19 | { |
hux | 28:86a594313434 | 20 | pShield->ready(); // forward IRQ callback to ready() |
hux | 28:86a594313434 | 21 | } |
hux | 24:0f08f68579bd | 22 | |
hux | 22:d467526abc4a | 23 | //============================================================================== |
hux | 28:86a594313434 | 24 | // Simple Loop - Cannot Change Mode |
hux | 22:d467526abc4a | 25 | //============================================================================== |
screamer | 0:eb7f02ad28a7 | 26 | |
hux | 28:86a594313434 | 27 | void loop(Dish &o) |
hux | 28:86a594313434 | 28 | { |
hux | 28:86a594313434 | 29 | int err = o.start(cbReady); // start measuement callback @ cbReady |
hux | 28:86a594313434 | 30 | |
hux | 28:86a594313434 | 31 | if (err == 0) |
hux | 28:86a594313434 | 32 | { |
hux | 28:86a594313434 | 33 | // while ( !stop.request() ) |
hux | 28:86a594313434 | 34 | { |
hux | 28:86a594313434 | 35 | if( o.request() ) // data service requested by 6180XA1 IRQ? |
hux | 28:86a594313434 | 36 | o.handle(); // handle data transfer |
hux | 28:86a594313434 | 37 | |
hux | 28:86a594313434 | 38 | o.refresh(); // refresh display |
hux | 28:86a594313434 | 39 | } |
hux | 28:86a594313434 | 40 | o.stop(); // stop all current measurements |
hux | 28:86a594313434 | 41 | } |
hux | 28:86a594313434 | 42 | // stop.clear(); // clear stop request (of stop button) |
hux | 28:86a594313434 | 43 | } |
hux | 28:86a594313434 | 44 | |
hux | 28:86a594313434 | 45 | //============================================================================== |
hux | 28:86a594313434 | 46 | // Some Callbacks |
hux | 28:86a594313434 | 47 | //============================================================================== |
hux | 28:86a594313434 | 48 | |
hux | 28:86a594313434 | 49 | void cbDisconnect(O&o) // disconnection callback |
hux | 28:86a594313434 | 50 | { |
hux | 28:86a594313434 | 51 | advertise(o); // start advertising |
hux | 28:86a594313434 | 52 | blinkAdvertise(o); // indicate advertising |
hux | 28:86a594313434 | 53 | } |
hux | 28:86a594313434 | 54 | |
hux | 28:86a594313434 | 55 | |
hux | 27:32267cee7cb8 | 56 | void cbError(O&o) // Error Reporting Callback |
hux | 23:677689000369 | 57 | { |
hux | 28:86a594313434 | 58 | blinkError(o); // indicate an error |
hux | 24:0f08f68579bd | 59 | } |
apalmieri | 13:227a0149b677 | 60 | |
apalmieri | 13:227a0149b677 | 61 | |
hux | 28:86a594313434 | 62 | void cbSetup(O&o) // setup calback (after BLE init) |
hux | 22:d467526abc4a | 63 | { |
hux | 28:86a594313434 | 64 | device(o,"S13#1.0_TOF_GAP"); // setup device name |
hux | 28:86a594313434 | 65 | name(o,"TOF Detector"); // add name to device |
hux | 28:86a594313434 | 66 | data(o,"123"); // advertising user data |
hux | 28:86a594313434 | 67 | |
hux | 28:86a594313434 | 68 | onDisconnect(o,cbDisconnect); // setup disconnection callback |
hux | 28:86a594313434 | 69 | |
hux | 28:86a594313434 | 70 | payload(o,"C:ng"); // no BR/EDR, general discoverable (UDSN) |
hux | 28:86a594313434 | 71 | advertise(o,100); // start advertising @ 100 msec interval |
hux | 28:86a594313434 | 72 | blinkAdvertise(o); // indicate advertising mode |
hux | 22:d467526abc4a | 73 | } |
hux | 22:d467526abc4a | 74 | |
hux | 28:86a594313434 | 75 | |
hux | 28:86a594313434 | 76 | int main() |
hux | 24:0f08f68579bd | 77 | { |
hux | 28:86a594313434 | 78 | O o; // Our Blob (BLuetooth OBject) |
hux | 28:86a594313434 | 79 | verbose(o); // verbose talking (all levels) |
hux | 28:86a594313434 | 80 | blinkIdle(o); // idle blinking - everything is OK! |
hux | 28:86a594313434 | 81 | |
hux | 28:86a594313434 | 82 | init(o,cbSetup,cbError); // init BLE base layer, always do first |
hux | 24:0f08f68579bd | 83 | |
hux | 28:86a594313434 | 84 | while (true) // Infinite loop waiting for BLE events |
hux | 28:86a594313434 | 85 | sleep(o); // low power waiting for BLE events |
hux | 28:86a594313434 | 86 | /* |
hux | 28:86a594313434 | 87 | pShield = new Dish; // create a DIsplay SHield class instance |
hux | 28:86a594313434 | 88 | pShield->init("ToF",1000); |
hux | 28:86a594313434 | 89 | |
hux | 28:86a594313434 | 90 | Dish &sh = *pShield; // short hand |
hux | 28:86a594313434 | 91 | |
hux | 28:86a594313434 | 92 | sh.setup(InterruptTOF); // setup fot ToF measurement only |
hux | 28:86a594313434 | 93 | loop(sh); |
hux | 28:86a594313434 | 94 | sh.display("BYE",2000); // display "byE" for 2 seconds |
hux | 28:86a594313434 | 95 | */ |
hux | 22:d467526abc4a | 96 | } |