ble transmitter

Dependencies:   mbed BLE_API nRF51822

Revision:
0:37b6e6dc1a2f
Child:
1:6659687d9cb0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/transmitter_main.cpp	Wed Apr 17 20:12:27 2019 +0000
@@ -0,0 +1,69 @@
+#include "mbed.h"
+#include "ble/BLE.h"
+//#define LED_RED     p21
+//#define LED_GREEN   p22
+//#define LED_BLUE    p23
+//#define BUTTON_PIN  p17
+//#define BATTERY_PIN p1
+#define LED_RED     p22
+DigitalOut led1(p21);
+
+typedef struct packet {
+    int increasing_sequence = 23;
+    const static char DEVICE_NAME[] = "Joe-Aidan";
+}
+     
+static volatile bool  triggerSensorPolling = false;
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+{
+    BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising
+}
+void periodicCallback(void)
+{
+    led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
+    /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
+     * heavy-weight sensor polling from the main thread. */
+    triggerSensorPolling = true;
+}
+void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
+{
+    BLE &ble          = params->ble;
+    ble_error_t error = params->error;
+    if (error != BLE_ERROR_NONE) {
+        return;
+    }
+    ble.gap().onDisconnection(disconnectionCallback);
+    /* Setup primary service. */
+    // hrService = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
+    /* Setup auxiliary service. */
+    // deviceInfo = new DeviceInformationService(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
+    /* Setup advertising. */
+    // ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
+    // ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
+    // ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
+    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
+    ble.gap().setAdvertisingInterval(1000); /* 1000ms */
+    ble.gap().startAdvertising();
+}
+int main(void)
+{
+    led1 = 1;
+    Ticker ticker;
+    ticker.attach(periodicCallback, 1); // blink LED every second
+    BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
+    ble.init(bleInitComplete);
+    
+    /* SpinWait for initialization to complete. This is necessary because the
+     * BLE object is used in the main loop below. */
+    while (ble.hasInitialized()  == false) { /* spin loop */ }
+    
+    while (1) {
+        // check for trigger from periodicCallback()
+        if (triggerSensorPolling && ble.getGapState().connected) {
+            triggerSensorPolling = false;
+        } else {
+            ble.waitForEvent(); // low power wait for event
+        }
+    }
+}
\ No newline at end of file