ble transmitter

Dependencies:   mbed BLE_API nRF51822

Committer:
khyein8154
Date:
Wed Apr 17 21:16:55 2019 +0000
Revision:
6:f0984541d764
Parent:
5:c7bbaa34373a
Child:
7:1e93ff891703
printing sequence number

Who changed what in which revision?

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