I2C to BLE nano with notifications to pi using original mbed os code

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_notifications by Nicholas Kosarek

Revision:
11:fd1cf9dbf3a4
Parent:
10:7943b5c1117a
Child:
12:e38bb61023ed
--- a/main.cpp	Wed Dec 30 09:54:06 2015 +0000
+++ b/main.cpp	Tue Apr 18 17:05:04 2017 +0000
@@ -17,44 +17,33 @@
 #include "mbed.h"
 #include "ble/BLE.h"
 #include "ButtonService.h"
+#include <time.h>
 
-DigitalOut  led1(LED1);
-InterruptIn button(BUTTON1);
-
-const static char     DEVICE_NAME[] = "Button";
+const static char     DEVICE_NAME[] = "LUMBERJACK_NANO";
 static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID};
 
-enum {
-    RELEASED = 0,
-    PRESSED,
-    IDLE
-};
-static uint8_t buttonState = IDLE;
+Serial pc(USBTX, USBRX);
+
 
 static ButtonService *buttonServicePtr;
+bool isThereAConnection = false;
 
-void buttonPressedCallback(void)
+void sleep(unsigned int mseconds)
 {
-    /* Note that the buttonPressedCallback() executes in interrupt context, so it is safer to access
-     * BLE device API from the main thread. */
-    buttonState = PRESSED;
-}
-
-void buttonReleasedCallback(void)
-{
-    /* Note that the buttonReleasedCallback() executes in interrupt context, so it is safer to access
-     * BLE device API from the main thread. */
-    buttonState = RELEASED;
+    clock_t goal = mseconds + clock();
+    while (goal > clock());
 }
 
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
     BLE::Instance().gap().startAdvertising();
+    isThereAConnection = false;
 }
 
-void periodicCallback(void)
+void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
 {
-    led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
+    pc.printf("Connection recieved!\r\n");
+    isThereAConnection = true;
 }
 
 /**
@@ -85,6 +74,7 @@
     }
 
     ble.gap().onDisconnection(disconnectionCallback);
+    ble.gap().onConnection(connectionCallback);
 
     /* Setup primary service */
     buttonServicePtr = new ButtonService(ble, false /* initial value for button pressed */);
@@ -95,31 +85,35 @@
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
+    pc.printf("start advertising now \r\n");
     ble.gap().startAdvertising();
 
 }
 
 int main(void)
 {
-    led1 = 1;
-    Ticker ticker;
-    ticker.attach(periodicCallback, 1);
-    button.fall(buttonPressedCallback);
-    button.rise(buttonReleasedCallback);
+    pc.printf("entering main!! \r\n");
+    uint8_t counter = 0;
 
     BLE &ble = BLE::Instance();
     ble.init(bleInitComplete);
     
+    pc.printf("entereing spin loop\r\n");
     /* 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 */ }
+    pc.printf("leaving spin loop\r\n");
     
     while (true) {
-        if (buttonState != IDLE) {
-            buttonServicePtr->updateButtonState(buttonState);
-            buttonState = IDLE;
+        pc.printf("loop!\r\n");
+        if(isThereAConnection) {
+            pc.printf("sending Notification\r\n");
+            buttonServicePtr->updateButtonState(counter);
+            counter++;
+            //sleep(1000);
+        } else {
+            //
         }
-
         ble.waitForEvent();
     }
 }