![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
A cute tiny piece of code implementing an IoT NAND device, demonstrating how to setup and advertise a cute GATT (NAND) service. The code has been tested on a Nordic nRF51822-DK.
Dependencies: BLE_API mbed nRF51822
Fork of BLE_HeartRate_IDB0XA1 by
bricks/init.cpp@26:dce30a5341bb, 2018-05-19 (annotated)
- Committer:
- hux
- Date:
- Sat May 19 14:10:17 2018 +0000
- Revision:
- 26:dce30a5341bb
Published
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hux | 26:dce30a5341bb | 1 | // init.cpp - initialize BLE system |
hux | 26:dce30a5341bb | 2 | |
hux | 26:dce30a5341bb | 3 | #include "bricks/init.h" |
hux | 26:dce30a5341bb | 4 | #include "bricks/blinker.h" |
hux | 26:dce30a5341bb | 5 | #include "bricks/trace.h" |
hux | 26:dce30a5341bb | 6 | |
hux | 26:dce30a5341bb | 7 | static void (*cbSetup)(O&o) = 0; // setup callback |
hux | 26:dce30a5341bb | 8 | static void (*cbError)(O&o) = 0; // error callback |
hux | 26:dce30a5341bb | 9 | |
hux | 26:dce30a5341bb | 10 | static void cbDefaultError(O&o) // default error handler |
hux | 26:dce30a5341bb | 11 | { |
hux | 26:dce30a5341bb | 12 | Blinker blink; |
hux | 26:dce30a5341bb | 13 | blink.error(); // 'error' blink sequence |
hux | 26:dce30a5341bb | 14 | } |
hux | 26:dce30a5341bb | 15 | |
hux | 26:dce30a5341bb | 16 | static void cbDefaultComplete(O&o) // default error handler |
hux | 26:dce30a5341bb | 17 | { |
hux | 26:dce30a5341bb | 18 | // empty |
hux | 26:dce30a5341bb | 19 | } |
hux | 26:dce30a5341bb | 20 | |
hux | 26:dce30a5341bb | 21 | static void initComplete(BLE::InitializationCompleteCallbackContext *params) |
hux | 26:dce30a5341bb | 22 | { |
hux | 26:dce30a5341bb | 23 | Blob o; // setup a blob |
hux | 26:dce30a5341bb | 24 | o.pComplete = params; // store to provide access |
hux | 26:dce30a5341bb | 25 | |
hux | 26:dce30a5341bb | 26 | // params->error = (ble_error_t)((int)BLE_ERROR_NONE+1); // uncomment for debug |
hux | 26:dce30a5341bb | 27 | |
hux | 26:dce30a5341bb | 28 | trace(o,2,"<initialized>\n"); |
hux | 26:dce30a5341bb | 29 | |
hux | 26:dce30a5341bb | 30 | if (params->error != BLE_ERROR_NONE) |
hux | 26:dce30a5341bb | 31 | { |
hux | 26:dce30a5341bb | 32 | trace(o,0,"error\n"); |
hux | 26:dce30a5341bb | 33 | if (cbError) |
hux | 26:dce30a5341bb | 34 | (*cbError)(o); // handle error in follow-up |
hux | 26:dce30a5341bb | 35 | |
hux | 26:dce30a5341bb | 36 | return; |
hux | 26:dce30a5341bb | 37 | } |
hux | 26:dce30a5341bb | 38 | |
hux | 26:dce30a5341bb | 39 | // Ensure that it is the default instance of BLE |
hux | 26:dce30a5341bb | 40 | |
hux | 26:dce30a5341bb | 41 | if(o.getInstanceID() != BLE::DEFAULT_INSTANCE) |
hux | 26:dce30a5341bb | 42 | { |
hux | 26:dce30a5341bb | 43 | return; |
hux | 26:dce30a5341bb | 44 | } |
hux | 26:dce30a5341bb | 45 | |
hux | 26:dce30a5341bb | 46 | (*cbSetup)(o); // followup with setup |
hux | 26:dce30a5341bb | 47 | } |
hux | 26:dce30a5341bb | 48 | |
hux | 26:dce30a5341bb | 49 | |
hux | 26:dce30a5341bb | 50 | void init(O&o, void (*scb)(O&o),void (*ecb)(O&o)) // init BLE system |
hux | 26:dce30a5341bb | 51 | { |
hux | 26:dce30a5341bb | 52 | cbSetup = scb; // store setup callback |
hux | 26:dce30a5341bb | 53 | cbError = ecb; // store error callback |
hux | 26:dce30a5341bb | 54 | o.init(initComplete); |
hux | 26:dce30a5341bb | 55 | } |
hux | 26:dce30a5341bb | 56 | |
hux | 26:dce30a5341bb | 57 | |
hux | 26:dce30a5341bb | 58 | void init(O&o, void (*scb)(O&o)) // initialize BLE system |
hux | 26:dce30a5341bb | 59 | { |
hux | 26:dce30a5341bb | 60 | init(o,scb,cbDefaultError); // continue with default error callback |
hux | 26:dce30a5341bb | 61 | } |
hux | 26:dce30a5341bb | 62 | |
hux | 26:dce30a5341bb | 63 | void init(O&o) // initialize BLE system |
hux | 26:dce30a5341bb | 64 | { |
hux | 26:dce30a5341bb | 65 | init(o,cbDefaultComplete,cbDefaultError); |
hux | 26:dce30a5341bb | 66 | while (!o.hasInitialized()) |
hux | 26:dce30a5341bb | 67 | { |
hux | 26:dce30a5341bb | 68 | trace(o,1,"waiting for initialized BLE!\n"); |
hux | 26:dce30a5341bb | 69 | wait_ms(200); |
hux | 26:dce30a5341bb | 70 | } |
hux | 26:dce30a5341bb | 71 | } |