![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
TOF based Presence Detector
Dependencies: BLE_API X_NUCLEO_6180XA1 X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
main.cpp@29:eceecbe28088, 2018-08-20 (annotated)
- Committer:
- hux
- Date:
- Mon Aug 20 16:43:10 2018 +0000
- Revision:
- 29:eceecbe28088
- Parent:
- 28:a23b16555909
made in Shanghai
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hux | 28:a23b16555909 | 1 | // S14_TOF_Detector - Easy GATT setup using bricks for DETECTION services |
hux | 24:0f08f68579bd | 2 | |
hux | 26:fd06c8b57d16 | 3 | #include "bricks/bricks.h" |
hux | 27:32267cee7cb8 | 4 | #include "detection.h" |
hux | 28:a23b16555909 | 5 | #include "bricks/stop.h" |
hux | 28:a23b16555909 | 6 | #include "shields/shields.h" |
hux | 28:a23b16555909 | 7 | |
hux | 28:a23b16555909 | 8 | typedef DisplayShield Dish; // a short hand for DisplayShield |
hux | 28:a23b16555909 | 9 | |
hux | 28:a23b16555909 | 10 | Dish *pShield; // to access our 6180XA1 DIsplay SHield |
hux | 28:a23b16555909 | 11 | StopButton stop(USER_BUTTON); // press blue button for stop |
hux | 28:a23b16555909 | 12 | |
hux | 28:a23b16555909 | 13 | //============================================================================== |
hux | 28:a23b16555909 | 14 | // Data Ready Callback |
hux | 28:a23b16555909 | 15 | //============================================================================== |
hux | 28:a23b16555909 | 16 | |
hux | 28:a23b16555909 | 17 | static void cbReady(void) // called if measurement done, data ready |
hux | 28:a23b16555909 | 18 | { |
hux | 28:a23b16555909 | 19 | pShield->ready(); // forward IRQ callback to ready() |
hux | 28:a23b16555909 | 20 | } |
hux | 28:a23b16555909 | 21 | |
hux | 28:a23b16555909 | 22 | //============================================================================== |
hux | 28:a23b16555909 | 23 | // Simple Loop - Cannot Change Mode |
hux | 28:a23b16555909 | 24 | //============================================================================== |
hux | 28:a23b16555909 | 25 | |
hux | 28:a23b16555909 | 26 | void loop(Dish &o) |
hux | 28:a23b16555909 | 27 | { |
hux | 28:a23b16555909 | 28 | int err = o.start(cbReady); // start measuement callback @ cbReady |
hux | 28:a23b16555909 | 29 | |
hux | 28:a23b16555909 | 30 | if (err == 0) |
hux | 28:a23b16555909 | 31 | { |
hux | 28:a23b16555909 | 32 | // while ( !stop.request() ) |
hux | 28:a23b16555909 | 33 | { |
hux | 28:a23b16555909 | 34 | if( o.request() ) // data service requested by 6180XA1 IRQ? |
hux | 28:a23b16555909 | 35 | o.handle(); // handle data transfer |
hux | 28:a23b16555909 | 36 | |
hux | 28:a23b16555909 | 37 | o.refresh(); // refresh display |
hux | 28:a23b16555909 | 38 | } |
hux | 28:a23b16555909 | 39 | o.stop(); // stop all current measurements |
hux | 28:a23b16555909 | 40 | } |
hux | 28:a23b16555909 | 41 | // stop.clear(); // clear stop request (of stop button) |
hux | 28:a23b16555909 | 42 | } |
hux | 28:a23b16555909 | 43 | |
hux | 24:0f08f68579bd | 44 | |
hux | 22:d467526abc4a | 45 | //============================================================================== |
hux | 24:0f08f68579bd | 46 | // Callbacks |
hux | 22:d467526abc4a | 47 | //============================================================================== |
screamer | 0:eb7f02ad28a7 | 48 | |
hux | 27:32267cee7cb8 | 49 | void cbError(O&o) // Error Reporting Callback |
hux | 23:677689000369 | 50 | { |
hux | 27:32267cee7cb8 | 51 | blinkError(o); // 'error' blink sequence |
hux | 24:0f08f68579bd | 52 | } |
apalmieri | 13:227a0149b677 | 53 | |
hux | 27:32267cee7cb8 | 54 | void cbConnect(O&o) // Connection Callback |
hux | 22:d467526abc4a | 55 | { |
hux | 27:32267cee7cb8 | 56 | blinkConnected(o); // 'error' blink sequence |
hux | 22:d467526abc4a | 57 | } |
apalmieri | 13:227a0149b677 | 58 | |
hux | 27:32267cee7cb8 | 59 | void cbDisconnect(O&o) // Disconnection Callback |
hux | 22:d467526abc4a | 60 | { |
hux | 27:32267cee7cb8 | 61 | advertise(o); // start advertising on client disconnect |
hux | 27:32267cee7cb8 | 62 | blinkAdvertise(o); // 'advertise' blink sequence |
hux | 22:d467526abc4a | 63 | } |
hux | 22:d467526abc4a | 64 | |
hux | 27:32267cee7cb8 | 65 | void cbSetup(O&o) // Immediately After Initializing BLE |
hux | 24:0f08f68579bd | 66 | { |
hux | 27:32267cee7cb8 | 67 | services(o); // enroll all services & setup callbacks |
hux | 24:0f08f68579bd | 68 | |
hux | 27:32267cee7cb8 | 69 | onConnect(o,cbConnect); // setup connection callback |
hux | 27:32267cee7cb8 | 70 | onDisconnect(o,cbDisconnect); // setup disconnection callback |
hux | 24:0f08f68579bd | 71 | |
hux | 28:a23b16555909 | 72 | device(o,"S14#0.15 ToF Detector"); // setup device name |
hux | 27:32267cee7cb8 | 73 | name(o,"Detector"); // setup advertising name |
hux | 27:32267cee7cb8 | 74 | data(o,"My Layout"); // setup advertising data |
hux | 24:0f08f68579bd | 75 | |
hux | 27:32267cee7cb8 | 76 | advertise(o,"C:ng",100); // start advertising @ 100 msec interval |
hux | 27:32267cee7cb8 | 77 | blinkAdvertise(o); // 'advertise' blink sequence |
hux | 22:d467526abc4a | 78 | } |
hux | 24:0f08f68579bd | 79 | |
hux | 22:d467526abc4a | 80 | //============================================================================== |
hux | 22:d467526abc4a | 81 | // Main Program |
hux | 22:d467526abc4a | 82 | //============================================================================== |
hux | 22:d467526abc4a | 83 | |
hux | 28:a23b16555909 | 84 | #define VL6180X_I2C_SDA D14 |
hux | 28:a23b16555909 | 85 | #define VL6180X_I2C_SCL D15 |
hux | 28:a23b16555909 | 86 | |
hux | 28:a23b16555909 | 87 | #define RANGE 0 |
hux | 28:a23b16555909 | 88 | #define ALS 1 |
hux | 28:a23b16555909 | 89 | |
hux | 22:d467526abc4a | 90 | int main(void) |
hux | 22:d467526abc4a | 91 | { |
hux | 28:a23b16555909 | 92 | O o; // declare a blob (BLE OBject) |
hux | 28:a23b16555909 | 93 | verbose(o); // enable all trace messages |
hux | 28:a23b16555909 | 94 | blinkIdle(o); // idle blinking - just started! |
hux | 28:a23b16555909 | 95 | |
hux | 28:a23b16555909 | 96 | //pShield = new Dish; // create a DIsplay SHield class instance |
hux | 28:a23b16555909 | 97 | //pShield->init("ToF",1000); |
hux | 28:a23b16555909 | 98 | |
hux | 28:a23b16555909 | 99 | |
hux | 28:a23b16555909 | 100 | DevI2C *pDevice; |
hux | 28:a23b16555909 | 101 | X_NUCLEO_6180XA1 *pBoard; |
apalmieri | 13:227a0149b677 | 102 | |
hux | 28:a23b16555909 | 103 | pDevice = new DevI2C(VL6180X_I2C_SDA, VL6180X_I2C_SCL); |
hux | 28:a23b16555909 | 104 | |
hux | 28:a23b16555909 | 105 | // next we create the 6180XA1 expansion board singleton obj |
hux | 28:a23b16555909 | 106 | // after that we are already able to display the init message |
hux | 22:d467526abc4a | 107 | |
hux | 28:a23b16555909 | 108 | //pBoard = X_NUCLEO_6180XA1::Instance(pDevice, A3, A2, D13, D2); |
hux | 28:a23b16555909 | 109 | |
hux | 28:a23b16555909 | 110 | init(o,cbSetup,cbError); // init BLE base layer, always do first |
hux | 28:a23b16555909 | 111 | sleep(o,5000); |
hux | 28:a23b16555909 | 112 | |
hux | 28:a23b16555909 | 113 | pBoard = X_NUCLEO_6180XA1::Instance(pDevice, A3, A2, D13, D2); |
hux | 28:a23b16555909 | 114 | |
hux | 28:a23b16555909 | 115 | while (true) // Infinite loop waiting for BLE events |
hux | 28:a23b16555909 | 116 | sleep(o); // low power waiting for BLE events |
hux | 24:0f08f68579bd | 117 | } |