![](/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
bricks/callback.cpp@27:32267cee7cb8, 2017-01-14 (annotated)
- Committer:
- hux
- Date:
- Sat Jan 14 08:43:14 2017 +0000
- Revision:
- 27:32267cee7cb8
Setup a GATT Detector Service. There is some bug in the GATT setup, resulting in different behavior between Nordic nRF51822-DK and NUCLEO-L476RG, but with the workaround it works also for the NUCLEO board. (using Bricks V1A)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hux | 27:32267cee7cb8 | 1 | // callback.cpp - deal with callbacks |
hux | 27:32267cee7cb8 | 2 | |
hux | 27:32267cee7cb8 | 3 | #include "bricks/blob.h" |
hux | 27:32267cee7cb8 | 4 | #include "bricks/callback.h" |
hux | 27:32267cee7cb8 | 5 | #include "bricks/trace.h" |
hux | 27:32267cee7cb8 | 6 | |
hux | 27:32267cee7cb8 | 7 | //============================================================================== |
hux | 27:32267cee7cb8 | 8 | // Setup Data Written Callback |
hux | 27:32267cee7cb8 | 9 | //============================================================================== |
hux | 27:32267cee7cb8 | 10 | |
hux | 27:32267cee7cb8 | 11 | static void (*cbWritten)(O&o) = 0; // where we have to follow up |
hux | 27:32267cee7cb8 | 12 | |
hux | 27:32267cee7cb8 | 13 | // default onDataWritten callback |
hux | 27:32267cee7cb8 | 14 | |
hux | 27:32267cee7cb8 | 15 | static void dfWritten(const GattWriteCallbackParams *params) |
hux | 27:32267cee7cb8 | 16 | { |
hux | 27:32267cee7cb8 | 17 | O o; // setup a blob |
hux | 27:32267cee7cb8 | 18 | o.pWritten = params; // store to provide access |
hux | 27:32267cee7cb8 | 19 | |
hux | 27:32267cee7cb8 | 20 | trace(o,2,"<data written>\n"); |
hux | 27:32267cee7cb8 | 21 | |
hux | 27:32267cee7cb8 | 22 | if (cbWritten) |
hux | 27:32267cee7cb8 | 23 | (*cbWritten)(o); // user callback follow-up |
hux | 27:32267cee7cb8 | 24 | } |
hux | 27:32267cee7cb8 | 25 | |
hux | 27:32267cee7cb8 | 26 | |
hux | 27:32267cee7cb8 | 27 | void onWritten(O&o, void (*fptr)(O&o)) // setup data written callback |
hux | 27:32267cee7cb8 | 28 | { |
hux | 27:32267cee7cb8 | 29 | cbWritten = fptr; // remember function to follow-up |
hux | 27:32267cee7cb8 | 30 | o.gattServer().onDataWritten(dfWritten);// actual calback setup in GATT |
hux | 27:32267cee7cb8 | 31 | } |
hux | 27:32267cee7cb8 | 32 | |
hux | 27:32267cee7cb8 | 33 | //============================================================================== |
hux | 27:32267cee7cb8 | 34 | // Setup Connection Callback |
hux | 27:32267cee7cb8 | 35 | //============================================================================== |
hux | 27:32267cee7cb8 | 36 | |
hux | 27:32267cee7cb8 | 37 | static void (*cbConnect)(Blob&) = 0;// where we have to follow up |
hux | 27:32267cee7cb8 | 38 | |
hux | 27:32267cee7cb8 | 39 | // default connection callback |
hux | 27:32267cee7cb8 | 40 | |
hux | 27:32267cee7cb8 | 41 | static void dfConnect(const Gap::ConnectionCallbackParams_t *params) |
hux | 27:32267cee7cb8 | 42 | { |
hux | 27:32267cee7cb8 | 43 | O o; // setup a blob object |
hux | 27:32267cee7cb8 | 44 | o.pConnect = params; // store to provide access |
hux | 27:32267cee7cb8 | 45 | |
hux | 27:32267cee7cb8 | 46 | trace(o,2,"<connect>\n"); |
hux | 27:32267cee7cb8 | 47 | |
hux | 27:32267cee7cb8 | 48 | if (cbConnect) |
hux | 27:32267cee7cb8 | 49 | (*cbConnect)(o); // user callback follow-up |
hux | 27:32267cee7cb8 | 50 | } |
hux | 27:32267cee7cb8 | 51 | |
hux | 27:32267cee7cb8 | 52 | void onConnect(O&o,void(*fptr)(O&)) // setup connection callback |
hux | 27:32267cee7cb8 | 53 | { |
hux | 27:32267cee7cb8 | 54 | cbConnect = fptr; // remember function to follow-up |
hux | 27:32267cee7cb8 | 55 | o.gap().onConnection(dfConnect); // actual callback setup in GAP |
hux | 27:32267cee7cb8 | 56 | } |
hux | 27:32267cee7cb8 | 57 | |
hux | 27:32267cee7cb8 | 58 | //============================================================================== |
hux | 27:32267cee7cb8 | 59 | // Setup Disconnection Callback |
hux | 27:32267cee7cb8 | 60 | //============================================================================== |
hux | 27:32267cee7cb8 | 61 | |
hux | 27:32267cee7cb8 | 62 | static void (*cbDisconnect)(O&o) = 0; // where we have to follow up |
hux | 27:32267cee7cb8 | 63 | |
hux | 27:32267cee7cb8 | 64 | // default disconnection callback |
hux | 27:32267cee7cb8 | 65 | |
hux | 27:32267cee7cb8 | 66 | static void dfDisconnect(const Gap::DisconnectionCallbackParams_t *params) |
hux | 27:32267cee7cb8 | 67 | { |
hux | 27:32267cee7cb8 | 68 | Blob o; // setup a blob |
hux | 27:32267cee7cb8 | 69 | o.pDisconnect = params; // store to provide access |
hux | 27:32267cee7cb8 | 70 | |
hux | 27:32267cee7cb8 | 71 | trace(o,2,"<disconnected>\n"); |
hux | 27:32267cee7cb8 | 72 | |
hux | 27:32267cee7cb8 | 73 | if (cbDisconnect) |
hux | 27:32267cee7cb8 | 74 | (*cbDisconnect)(o); // user callback follow-up |
hux | 27:32267cee7cb8 | 75 | } |
hux | 27:32267cee7cb8 | 76 | |
hux | 27:32267cee7cb8 | 77 | void onDisconnect(O&o,void(*fptr)(O&o)) // setup disconnection callback |
hux | 27:32267cee7cb8 | 78 | { |
hux | 27:32267cee7cb8 | 79 | cbDisconnect = fptr; // remember function to follow-up |
hux | 27:32267cee7cb8 | 80 | |
hux | 27:32267cee7cb8 | 81 | o.gap().onDisconnection(dfDisconnect);// actualcallback setup in GAP |
hux | 27:32267cee7cb8 | 82 | } |
hux | 27:32267cee7cb8 | 83 |