ble transmitter
Dependencies: mbed BLE_API nRF51822
transmitter_main.cpp@3:76b8337374e7, 2019-04-17 (annotated)
- Committer:
- jzabins2
- Date:
- Wed Apr 17 21:05:11 2019 +0000
- Revision:
- 3:76b8337374e7
- Parent:
- 2:f8dfdded83bf
- Child:
- 4:c3123e9e7d39
Added update payload
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aidanjabb | 0:37b6e6dc1a2f | 1 | #include "mbed.h" |
aidanjabb | 0:37b6e6dc1a2f | 2 | #include "ble/BLE.h" |
jzabins2 | 2:f8dfdded83bf | 3 | |
aidanjabb | 0:37b6e6dc1a2f | 4 | #define LED_RED p22 |
jzabins2 | 2:f8dfdded83bf | 5 | #define LED_GREEN p21 |
jzabins2 | 2:f8dfdded83bf | 6 | #define LED_BLUE p23 |
jzabins2 | 2:f8dfdded83bf | 7 | #define BUTTON_PIN p17 |
jzabins2 | 2:f8dfdded83bf | 8 | #define BATTERY_PIN p1 |
jzabins2 | 2:f8dfdded83bf | 9 | |
aidanjabb | 0:37b6e6dc1a2f | 10 | DigitalOut led1(p21); |
aidanjabb | 0:37b6e6dc1a2f | 11 | |
jzabins2 | 2:f8dfdded83bf | 12 | struct Packet { |
jzabins2 | 3:76b8337374e7 | 13 | uint32_t seqNum; |
jzabins2 | 2:f8dfdded83bf | 14 | }; |
jzabins2 | 2:f8dfdded83bf | 15 | |
jzabins2 | 3:76b8337374e7 | 16 | BLE ble; |
jzabins2 | 3:76b8337374e7 | 17 | const static char DEVICE_NAME[] = "JOEY"; |
aidanjabb | 0:37b6e6dc1a2f | 18 | static volatile bool triggerSensorPolling = false; |
jzabins2 | 3:76b8337374e7 | 19 | |
aidanjabb | 0:37b6e6dc1a2f | 20 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
aidanjabb | 0:37b6e6dc1a2f | 21 | { |
aidanjabb | 0:37b6e6dc1a2f | 22 | BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising |
aidanjabb | 0:37b6e6dc1a2f | 23 | } |
jzabins2 | 3:76b8337374e7 | 24 | |
jzabins2 | 3:76b8337374e7 | 25 | void updatePayload(void) |
jzabins2 | 3:76b8337374e7 | 26 | { |
jzabins2 | 3:76b8337374e7 | 27 | static Packet p; |
jzabins2 | 3:76b8337374e7 | 28 | ble.gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&p, sizeof(p)); |
jzabins2 | 3:76b8337374e7 | 29 | (p.seqNum)++; |
jzabins2 | 3:76b8337374e7 | 30 | } |
jzabins2 | 3:76b8337374e7 | 31 | |
aidanjabb | 0:37b6e6dc1a2f | 32 | void periodicCallback(void) |
aidanjabb | 0:37b6e6dc1a2f | 33 | { |
aidanjabb | 0:37b6e6dc1a2f | 34 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
jzabins2 | 3:76b8337374e7 | 35 | updatePayload(); |
jzabins2 | 3:76b8337374e7 | 36 | |
aidanjabb | 0:37b6e6dc1a2f | 37 | /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
aidanjabb | 0:37b6e6dc1a2f | 38 | * heavy-weight sensor polling from the main thread. */ |
aidanjabb | 0:37b6e6dc1a2f | 39 | triggerSensorPolling = true; |
aidanjabb | 0:37b6e6dc1a2f | 40 | } |
jzabins2 | 3:76b8337374e7 | 41 | |
aidanjabb | 0:37b6e6dc1a2f | 42 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
aidanjabb | 0:37b6e6dc1a2f | 43 | { |
jzabins2 | 3:76b8337374e7 | 44 | BLE &localble = params->ble; |
aidanjabb | 0:37b6e6dc1a2f | 45 | ble_error_t error = params->error; |
jzabins2 | 3:76b8337374e7 | 46 | |
aidanjabb | 0:37b6e6dc1a2f | 47 | if (error != BLE_ERROR_NONE) { |
aidanjabb | 0:37b6e6dc1a2f | 48 | return; |
aidanjabb | 0:37b6e6dc1a2f | 49 | } |
jzabins2 | 3:76b8337374e7 | 50 | |
jzabins2 | 3:76b8337374e7 | 51 | localble.gap().onDisconnection(disconnectionCallback); |
jzabins2 | 3:76b8337374e7 | 52 | |
jzabins2 | 3:76b8337374e7 | 53 | localble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
jzabins2 | 3:76b8337374e7 | 54 | updatePayload(); |
jzabins2 | 3:76b8337374e7 | 55 | |
jzabins2 | 3:76b8337374e7 | 56 | localble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
jzabins2 | 3:76b8337374e7 | 57 | localble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
jzabins2 | 3:76b8337374e7 | 58 | localble.gap().startAdvertising(); |
aidanjabb | 0:37b6e6dc1a2f | 59 | } |
jzabins2 | 3:76b8337374e7 | 60 | |
aidanjabb | 0:37b6e6dc1a2f | 61 | int main(void) |
aidanjabb | 0:37b6e6dc1a2f | 62 | { |
aidanjabb | 0:37b6e6dc1a2f | 63 | led1 = 1; |
jzabins2 | 3:76b8337374e7 | 64 | |
aidanjabb | 0:37b6e6dc1a2f | 65 | Ticker ticker; |
jzabins2 | 3:76b8337374e7 | 66 | ticker.attach(periodicCallback, 1); |
jzabins2 | 3:76b8337374e7 | 67 | |
aidanjabb | 0:37b6e6dc1a2f | 68 | ble.init(bleInitComplete); |
aidanjabb | 0:37b6e6dc1a2f | 69 | |
aidanjabb | 0:37b6e6dc1a2f | 70 | /* SpinWait for initialization to complete. This is necessary because the |
aidanjabb | 0:37b6e6dc1a2f | 71 | * BLE object is used in the main loop below. */ |
aidanjabb | 0:37b6e6dc1a2f | 72 | while (ble.hasInitialized() == false) { /* spin loop */ } |
aidanjabb | 0:37b6e6dc1a2f | 73 | |
aidanjabb | 0:37b6e6dc1a2f | 74 | while (1) { |
aidanjabb | 0:37b6e6dc1a2f | 75 | // check for trigger from periodicCallback() |
aidanjabb | 0:37b6e6dc1a2f | 76 | if (triggerSensorPolling && ble.getGapState().connected) { |
aidanjabb | 0:37b6e6dc1a2f | 77 | triggerSensorPolling = false; |
aidanjabb | 0:37b6e6dc1a2f | 78 | } else { |
aidanjabb | 0:37b6e6dc1a2f | 79 | ble.waitForEvent(); // low power wait for event |
aidanjabb | 0:37b6e6dc1a2f | 80 | } |
aidanjabb | 0:37b6e6dc1a2f | 81 | } |
aidanjabb | 0:37b6e6dc1a2f | 82 | } |