ble transmitter
Dependencies: mbed BLE_API nRF51822
transmitter_main.cpp@1:6659687d9cb0, 2019-04-17 (annotated)
- Committer:
- jzabins2
- Date:
- Wed Apr 17 20:21:20 2019 +0000
- Revision:
- 1:6659687d9cb0
- Parent:
- 0:37b6e6dc1a2f
- Child:
- 2:f8dfdded83bf
Test commit
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" |
aidanjabb | 0:37b6e6dc1a2f | 3 | //#define LED_RED p21 |
aidanjabb | 0:37b6e6dc1a2f | 4 | //#define LED_GREEN p22 |
aidanjabb | 0:37b6e6dc1a2f | 5 | //#define LED_BLUE p23 |
aidanjabb | 0:37b6e6dc1a2f | 6 | //#define BUTTON_PIN p17 |
aidanjabb | 0:37b6e6dc1a2f | 7 | //#define BATTERY_PIN p1 |
aidanjabb | 0:37b6e6dc1a2f | 8 | #define LED_RED p22 |
aidanjabb | 0:37b6e6dc1a2f | 9 | DigitalOut led1(p21); |
aidanjabb | 0:37b6e6dc1a2f | 10 | |
jzabins2 | 1:6659687d9cb0 | 11 | // sample comment |
aidanjabb | 0:37b6e6dc1a2f | 12 | typedef struct packet { |
aidanjabb | 0:37b6e6dc1a2f | 13 | int increasing_sequence = 23; |
aidanjabb | 0:37b6e6dc1a2f | 14 | const static char DEVICE_NAME[] = "Joe-Aidan"; |
aidanjabb | 0:37b6e6dc1a2f | 15 | } |
aidanjabb | 0:37b6e6dc1a2f | 16 | |
aidanjabb | 0:37b6e6dc1a2f | 17 | static volatile bool triggerSensorPolling = false; |
aidanjabb | 0:37b6e6dc1a2f | 18 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
aidanjabb | 0:37b6e6dc1a2f | 19 | { |
aidanjabb | 0:37b6e6dc1a2f | 20 | BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising |
aidanjabb | 0:37b6e6dc1a2f | 21 | } |
aidanjabb | 0:37b6e6dc1a2f | 22 | void periodicCallback(void) |
aidanjabb | 0:37b6e6dc1a2f | 23 | { |
aidanjabb | 0:37b6e6dc1a2f | 24 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
aidanjabb | 0:37b6e6dc1a2f | 25 | /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
aidanjabb | 0:37b6e6dc1a2f | 26 | * heavy-weight sensor polling from the main thread. */ |
aidanjabb | 0:37b6e6dc1a2f | 27 | triggerSensorPolling = true; |
aidanjabb | 0:37b6e6dc1a2f | 28 | } |
aidanjabb | 0:37b6e6dc1a2f | 29 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
aidanjabb | 0:37b6e6dc1a2f | 30 | { |
aidanjabb | 0:37b6e6dc1a2f | 31 | BLE &ble = params->ble; |
aidanjabb | 0:37b6e6dc1a2f | 32 | ble_error_t error = params->error; |
aidanjabb | 0:37b6e6dc1a2f | 33 | if (error != BLE_ERROR_NONE) { |
aidanjabb | 0:37b6e6dc1a2f | 34 | return; |
aidanjabb | 0:37b6e6dc1a2f | 35 | } |
aidanjabb | 0:37b6e6dc1a2f | 36 | ble.gap().onDisconnection(disconnectionCallback); |
aidanjabb | 0:37b6e6dc1a2f | 37 | /* Setup primary service. */ |
aidanjabb | 0:37b6e6dc1a2f | 38 | // hrService = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER); |
aidanjabb | 0:37b6e6dc1a2f | 39 | /* Setup auxiliary service. */ |
aidanjabb | 0:37b6e6dc1a2f | 40 | // deviceInfo = new DeviceInformationService(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); |
aidanjabb | 0:37b6e6dc1a2f | 41 | /* Setup advertising. */ |
aidanjabb | 0:37b6e6dc1a2f | 42 | // ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
aidanjabb | 0:37b6e6dc1a2f | 43 | // ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
aidanjabb | 0:37b6e6dc1a2f | 44 | // ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); |
aidanjabb | 0:37b6e6dc1a2f | 45 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
aidanjabb | 0:37b6e6dc1a2f | 46 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
aidanjabb | 0:37b6e6dc1a2f | 47 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
aidanjabb | 0:37b6e6dc1a2f | 48 | ble.gap().startAdvertising(); |
aidanjabb | 0:37b6e6dc1a2f | 49 | } |
aidanjabb | 0:37b6e6dc1a2f | 50 | int main(void) |
aidanjabb | 0:37b6e6dc1a2f | 51 | { |
aidanjabb | 0:37b6e6dc1a2f | 52 | led1 = 1; |
aidanjabb | 0:37b6e6dc1a2f | 53 | Ticker ticker; |
aidanjabb | 0:37b6e6dc1a2f | 54 | ticker.attach(periodicCallback, 1); // blink LED every second |
aidanjabb | 0:37b6e6dc1a2f | 55 | BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); |
aidanjabb | 0:37b6e6dc1a2f | 56 | ble.init(bleInitComplete); |
aidanjabb | 0:37b6e6dc1a2f | 57 | |
aidanjabb | 0:37b6e6dc1a2f | 58 | /* SpinWait for initialization to complete. This is necessary because the |
aidanjabb | 0:37b6e6dc1a2f | 59 | * BLE object is used in the main loop below. */ |
aidanjabb | 0:37b6e6dc1a2f | 60 | while (ble.hasInitialized() == false) { /* spin loop */ } |
aidanjabb | 0:37b6e6dc1a2f | 61 | |
aidanjabb | 0:37b6e6dc1a2f | 62 | while (1) { |
aidanjabb | 0:37b6e6dc1a2f | 63 | // check for trigger from periodicCallback() |
aidanjabb | 0:37b6e6dc1a2f | 64 | if (triggerSensorPolling && ble.getGapState().connected) { |
aidanjabb | 0:37b6e6dc1a2f | 65 | triggerSensorPolling = false; |
aidanjabb | 0:37b6e6dc1a2f | 66 | } else { |
aidanjabb | 0:37b6e6dc1a2f | 67 | ble.waitForEvent(); // low power wait for event |
aidanjabb | 0:37b6e6dc1a2f | 68 | } |
aidanjabb | 0:37b6e6dc1a2f | 69 | } |
aidanjabb | 0:37b6e6dc1a2f | 70 | } |