Felix Rüdiger / Mbed 2 deprecated BLE_Nano_MPU6050Service

Dependencies:   BLE_API mbed nRF51822 MPU6050_lib

Revision:
12:dc46aa2edccd
Parent:
10:b305e261e7d1
diff -r 1412ede83587 -r dc46aa2edccd main.cpp
--- a/main.cpp	Tue Sep 08 13:47:25 2015 +0000
+++ b/main.cpp	Tue Sep 22 10:49:59 2015 +0000
@@ -6,7 +6,6 @@
 #include "MPU6050Service.h"
 #include "PowerService.h"
 #include "services/DeviceInformationService.h"
-#include "MPU6050/helpers.h"
 
 /** Constants */
 
@@ -22,15 +21,25 @@
 static const uint16_t       maximumConnectionInterval       = Gap::MSEC_TO_GAP_DURATION_UNITS(40); // 40ms;
 static const uint16_t       slaveLatency                    = 0;
 
-static const float          ledTimeout             = 0.05f;
-static const uint8_t        connectedBlinkSequ[]            = { BYTE(11111111), BYTE(11111111), BYTE(11111111), BYTE(11111111),
-                                                                BYTE(11111111), BYTE(11111111), BYTE(11111111), BYTE(11111111),
-                                                                BYTE(11111111), BYTE(11111111), BYTE(11111111), BYTE(11000000),
-                                                                BYTE(00000000) };
-static const uint8_t        disconnectedBlinkSequ[]         = { BYTE(11111111), BYTE(11000000), BYTE(00001111), BYTE(11111100),
-                                                                BYTE(00000000), BYTE(11111111), BYTE(11000000), BYTE(00001111),
-                                                                BYTE(11111100), BYTE(00000000), BYTE(00000000) };
-static const uint8_t        batteryCritBlinkSequ[]          = { BYTE(11111100), BYTE(00001111), BYTE(11000000) };
+static const float          ledTimeout                      = 0.05f;
+static const float          batteryCritBlinkSequ[]          = { 0.000f, 0.146f, 0.500f, 0.854f, 1.000f, 0.854f, 0.500f, 0.146f };
+static const float          connectedBlinkSequ[]            = { 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f,
+                                                                1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f,                                                            
+                                                                1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f,                                                           
+                                                                1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 
+                                                                1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f,                                                           
+                                                                1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f,                                                           
+                                                                1.000f, 0.962f, 0.854f, 0.691f, 0.500f, 0.309f, 0.146f, 0.038f,
+                                                                0.000f, 0.038f, 0.146f, 0.309f, 0.500f, 0.691f, 0.854f, 0.962f };
+static const float          disconnectedBlinkSequ[]         = { 0.000f, 0.038f, 0.146f, 0.309f, 0.500f, 0.691f, 0.854f, 0.962f,
+                                                                1.000f, 0.962f, 0.854f, 0.691f, 0.500f, 0.309f, 0.146f, 0.038f,
+                                                                0.000f, 0.038f, 0.146f, 0.309f, 0.500f, 0.691f, 0.854f, 0.962f,
+                                                                1.000f, 0.962f, 0.854f, 0.691f, 0.500f, 0.309f, 0.146f, 0.038f,
+                                                                0.000f, 0.038f, 0.146f, 0.309f, 0.500f, 0.691f, 0.854f, 0.962f,
+                                                                1.000f, 0.962f, 0.854f, 0.691f, 0.500f, 0.309f, 0.146f, 0.038f,
+                                                                0.000f, 0.000f, 0.000f, 0.000f, 0.000f, 0.000f, 0.000f, 0.000f,                                                            
+                                                                0.000f, 0.000f, 0.000f, 0.000f, 0.000f, 0.000f, 0.000f, 0.000f,                                                            
+                                                                0.000f, 0.000f, 0.000f, 0.000f, 0.000f, 0.000f, 0.000f, 0.000f };
 
 /** Power determination stuff:
  *
@@ -89,13 +98,15 @@
 
 MPU6050                     mpu(I2C_SDA0, I2C_SCL0, MPU6050::ADDRESS_0);
 
-DigitalOut                  btLed(P0_28);
-DigitalOut                  pwrLed(P0_29);
+PwmOut                      btLed(P0_28);
+PwmOut                      pwrLed(P0_29);
 DigitalOut                  aliveLed(LED);
 
 AnalogIn                    uRef1(P0_4);
 AnalogIn                    uRef2(P0_5);
 
+InterruptIn                 intIn(P0_9);
+
 Ticker                      powerAccumulationTicker;
 Ticker                      powerServiceUpdateTicker;
 Ticker                      ledTicker;
@@ -133,8 +144,8 @@
     powerServiceUpdateTrigger = false;
     
     powerService->updateVoltage(U0);
-    powerService->updateCurrent(I0 * 1000);
-    powerService->updateDischarge(Q * 0.277777777f);
+    powerService->updateCurrent(I0 * 1000);             // mA
+    powerService->updateDischarge(Q * 0.277777777f);    // mAh
 }
 
 void powerServiceUpdatePeriodicCallback()
@@ -148,25 +159,28 @@
 static bool ledTrigger = false;
 static bool batteryCritical = false;
 
-void ledHandler(Gap &gap, MPU6050Service *mpu)
+void ledHandler(Gap &gap)
 {   
     ledTrigger = false;
     
-    static uint8_t byte = 0;
-    static uint8_t bit  = 0;  
+    static uint32_t     cnt                         = 0;
+    static const int    batteryCritBlinkSequSize    = sizeof(batteryCritBlinkSequ) / sizeof(float);    
+    static const int    connectedBlinkSequSize      = sizeof(connectedBlinkSequ) / sizeof(float);
+    static const int    disconnectedBlinkSequSize   = sizeof(disconnectedBlinkSequ) / sizeof(float);  
     
-    if (mpu->isRunning())
-        aliveLed = !aliveLed;
+    aliveLed = !aliveLed;
     
     if (gap.getState().connected)
-        btLed = (connectedBlinkSequ[byte % sizeof(connectedBlinkSequ)] >> bit) & 0x01;        
+        btLed = connectedBlinkSequ[cnt % connectedBlinkSequSize];        
     else
-        btLed = (disconnectedBlinkSequ[byte % sizeof(connectedBlinkSequ)] >> bit) & 0x01;
+        btLed = disconnectedBlinkSequ[cnt % disconnectedBlinkSequSize];
     
     if (batteryCritical)
-        pwrLed = (batteryCritBlinkSequ[byte % sizeof(batteryCritBlinkSequ)] >> bit) & 0x01;
+        pwrLed = batteryCritBlinkSequ[cnt % batteryCritBlinkSequSize];
     else
         pwrLed = 1;
+        
+    cnt++;
 }
 
 void ledPeriodicCallback()
@@ -238,8 +252,8 @@
 
 int main()
 {   
-    btLed       = 1;
-    pwrLed      = 1;
+    btLed       = 1.0f;
+    pwrLed      = 1.0f;
     aliveLed    = 0;
 
     ble.init();
@@ -271,9 +285,9 @@
     fast.slaveLatency = slaveLatency;
     gap.setPreferredConnectionParams(&fast);
       
-    DeviceInformationService    deviceInformation(ble, &deviceManufacturers[0], deviceModelNumber, deviceSerialNumber, deviceHardwareRev, deviceFirmwareRev, deviceSoftwareRev);    
+    DeviceInformationService    deviceInformation(ble, deviceManufacturers, deviceModelNumber, deviceSerialNumber, deviceHardwareRev, deviceFirmwareRev, deviceSoftwareRev);    
     PowerService                powerService(ble);
-    MPU6050Service              mpu6050(ble, mpu, P0_9);    
+    MPU6050Service              mpu6050(ble, mpu, &intIn);    
     
     gap.startAdvertising();
     
@@ -285,7 +299,7 @@
             powerServiceUpdateHandler(&powerService);
             
         if (ledTrigger)
-            ledHandler(gap, &mpu6050);
+            ledHandler(gap);
             
         if (connectionTrigger)
             connectionHandler(&mpu6050);