A nice BLE demo program which allows remote switch of an LED via GATT interface.
Dependencies: BLE_API mbed nRF51822
Fork of BLE_Button by
bricks/init.cpp@13:0563f1aa6a75, 2017-10-21 (annotated)
- Committer:
- hux
- Date:
- Sat Oct 21 19:56:15 2017 +0000
- Revision:
- 13:0563f1aa6a75
- Parent:
- 12:0d0ca44397dd
Switch LED via BLE GATT
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hux | 12:0d0ca44397dd | 1 | // init.cpp - initialize BLE system |
hux | 12:0d0ca44397dd | 2 | |
hux | 12:0d0ca44397dd | 3 | #include "bricks/init.h" |
hux | 12:0d0ca44397dd | 4 | #include "bricks/blink.h" |
hux | 12:0d0ca44397dd | 5 | #include "bricks/trace.h" |
hux | 12:0d0ca44397dd | 6 | |
hux | 12:0d0ca44397dd | 7 | static void (*cbSetup)(O&o) = 0; // setup callback |
hux | 12:0d0ca44397dd | 8 | static void (*cbError)(O&o) = 0; // error callback |
hux | 12:0d0ca44397dd | 9 | |
hux | 12:0d0ca44397dd | 10 | static void cbDefaultError(O&o) // default error handler |
hux | 12:0d0ca44397dd | 11 | { |
hux | 12:0d0ca44397dd | 12 | blinkError(o); // 'error' blink sequence |
hux | 12:0d0ca44397dd | 13 | } |
hux | 12:0d0ca44397dd | 14 | |
hux | 12:0d0ca44397dd | 15 | static void initComplete(BLE::InitializationCompleteCallbackContext *params) |
hux | 12:0d0ca44397dd | 16 | { |
hux | 12:0d0ca44397dd | 17 | Blob o; // setup a blob |
hux | 12:0d0ca44397dd | 18 | o.pComplete = params; // store to provide access |
hux | 12:0d0ca44397dd | 19 | |
hux | 12:0d0ca44397dd | 20 | // params->error = (ble_error_t)((int)BLE_ERROR_NONE+1); // uncomment for debug |
hux | 12:0d0ca44397dd | 21 | |
hux | 12:0d0ca44397dd | 22 | trace(o,2,"<initialized>\n"); |
hux | 12:0d0ca44397dd | 23 | |
hux | 12:0d0ca44397dd | 24 | if (params->error != BLE_ERROR_NONE) |
hux | 12:0d0ca44397dd | 25 | { |
hux | 12:0d0ca44397dd | 26 | trace(o,0,"error\n"); |
hux | 12:0d0ca44397dd | 27 | if (cbError) |
hux | 12:0d0ca44397dd | 28 | (*cbError)(o); // handle error in follow-up |
hux | 12:0d0ca44397dd | 29 | |
hux | 12:0d0ca44397dd | 30 | return; |
hux | 12:0d0ca44397dd | 31 | } |
hux | 12:0d0ca44397dd | 32 | |
hux | 12:0d0ca44397dd | 33 | // Ensure that it is the default instance of BLE |
hux | 12:0d0ca44397dd | 34 | |
hux | 12:0d0ca44397dd | 35 | if(o.getInstanceID() != BLE::DEFAULT_INSTANCE) |
hux | 12:0d0ca44397dd | 36 | { |
hux | 12:0d0ca44397dd | 37 | return; |
hux | 12:0d0ca44397dd | 38 | } |
hux | 12:0d0ca44397dd | 39 | |
hux | 12:0d0ca44397dd | 40 | (*cbSetup)(o); // followup with setup |
hux | 12:0d0ca44397dd | 41 | } |
hux | 12:0d0ca44397dd | 42 | |
hux | 12:0d0ca44397dd | 43 | |
hux | 12:0d0ca44397dd | 44 | void init(O&o, void (*scb)(O&o),void (*ecb)(O&o)) // init BLE system |
hux | 12:0d0ca44397dd | 45 | { |
hux | 12:0d0ca44397dd | 46 | cbSetup = scb; // store setup callback |
hux | 12:0d0ca44397dd | 47 | cbError = ecb; // store error callback |
hux | 12:0d0ca44397dd | 48 | o.init(initComplete); |
hux | 12:0d0ca44397dd | 49 | } |
hux | 12:0d0ca44397dd | 50 | |
hux | 12:0d0ca44397dd | 51 | |
hux | 12:0d0ca44397dd | 52 | void init(O&o, void (*scb)(O&o)) // initialize BLE system |
hux | 12:0d0ca44397dd | 53 | { |
hux | 12:0d0ca44397dd | 54 | init(o,scb,cbDefaultError); // continue with default error callback |
hux | 12:0d0ca44397dd | 55 | } |