ble transmitter

Dependencies:   mbed BLE_API nRF51822

Committer:
aidanjabb
Date:
Wed Apr 17 20:12:27 2019 +0000
Revision:
0:37b6e6dc1a2f
Child:
1:6659687d9cb0
first commit

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