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