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