X-NUCLEO-IKS01A1 Environmental/Motion sensors data transmitted via X-NUCLEO-IDB04A1 BLE board. Compatible with iOS/Android ST BlueMS V2.1 application.

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 X_NUCLEO_IKS01A1 mbed

Fork of Bluemicrosystem1 by ST Expansion SW Team

BlueMicrosystem application

This application is the mbed equivalent of ST BlueMicrosystem1 and provides an example of motion and environmental data exported via Bluetooth Low Energy to an Android or IOS device.
It runs on a ST NUCLEO-F401RE board connected with a X-NUCLEO-IKS01A1 and a X-NUCLEO-IDB04A1 expansion boards and is compatible with Android and iOS ST BlueMS smartphone applications (based on Android and iOS BlueST SDKs).
By default the application is not providing sensor fusion and activity recognition features. However sensor fusion can be enabled following the steps below:

  • Download and install osxMotionFX library on your PC.
  • Obtain the free license for your board following the instructions
  • Copy the correct license into Middlewares/ST/STM32_OSX_MotionFX_Library/osx_license.h of your mbed program folder
  • Copy Middlewares/ST/STM32_OSX_MotionFX_Library/Inc/osx_motion_fx.h file
  • Rename the provided .lib Keil binary library giving it a .ar extension, then copy it into Middlewares/ST/STM32_OSX_MotionFX_Library/Lib of your mbed program folder
  • Enable USE_SENSOR_FUSION_LIB macro into MotionFX_Manager.h file and recompile.
Revision:
8:b042fe719f51
Parent:
7:34014895dda8
Child:
9:2693f9ef8ff7
--- a/main.cpp	Thu Dec 17 15:58:35 2015 +0000
+++ b/main.cpp	Mon Dec 21 13:05:16 2015 +0000
@@ -1,40 +1,7 @@
-/**
- ******************************************************************************
- * @file    main.cpp
- * @author  AST / EST
- * @version V0.0.1
- * @date    16-Dec-2015
- * @brief   main of Bluemicrosystem1 application
- ******************************************************************************
- * @attention
- *
- * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- *   1. Redistributions of source code must retain the above copyright notice,
- *      this list of conditions and the following disclaimer.
- *   2. Redistributions in binary form must reproduce the above copyright notice,
- *      this list of conditions and the following disclaimer in the documentation
- *      and/or other materials provided with the distribution.
- *   3. Neither the name of STMicroelectronics nor the names of its contributors
- *      may be used to endorse or promote products derived from this software
- *      without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
 #include "mbed.h"
+
+/*L0_BlueNRG_xx version main*/
+
 #include <cmath>
 #include <math.h>
 #include "mbed.h"
@@ -58,10 +25,6 @@
     int32_t AXIS_Z;
 } AxesRaw_TypeDef;
 
-#ifdef CUST_BLEERR_MNGT
-#include "CustomBleErrManagement.h"
-#endif
-
 #ifdef CUST_CONS_SERV
 #include "CustomConsoleService.h"
 #endif
@@ -97,6 +60,8 @@
 static uint32_t         timeoutMems  =0;
 static BLE              * p_BLEdev = NULL;
 static int32_t          CounterAGM  =0;
+static uint32_t         bleTxErrCnt =0;    
+    
 #ifdef CUST_CONS_SERV
 static CustomConsoleService    * p_customconsoleservice = NULL;
 #endif
@@ -116,9 +81,6 @@
 static CustomConfigService     * p_customconfigservice     = NULL;
 #endif
 
-#ifdef CUST_BLEERR_MNGT
-static CustomBleErrManagement  * p_customBleErrManagement = NULL;
-#endif
 
 /***************************    Calibration functions    **************************/
 
@@ -384,10 +346,12 @@
     if (p_customsensorservice)
         p_customsensorservice->enNotify (handle);
 #endif
+
 #ifdef CUST_SW_SERV
     if (p_customsoftwareservice)
         p_customsoftwareservice->enNotify (handle);
 #endif
+
 #ifdef CUST_CONFIG_SERV
     if (p_customconfigservice) {
         p_customconfigservice->enNotify (handle);
@@ -399,11 +363,7 @@
     if (p_customconsoleservice)
         p_customconsoleservice->enNotify (handle);
 #endif
-#ifdef CUST_BLEERR_MNGT
-    if (p_customBleErrManagement)
-        p_customBleErrManagement->clearTxErr();
-#endif
-   /* TODO inform other obj implementing BLE services if any other service is added */
+    /* TODO inform other obj implementing BLE services */
 }
 
 static void onUpdatesDisabledCallback(Gap::Handle_t handle)
@@ -413,10 +373,12 @@
     if (p_customsensorservice)
         p_customsensorservice->disNotify (handle);
 #endif
+
 #ifdef CUST_SW_SERV
     if (p_customsoftwareservice)
         p_customsoftwareservice->disNotify (handle);
 #endif
+
 #ifdef CUST_CONFIG_SERV
     if (p_customconfigservice)
         p_customconfigservice->disNotify (handle);
@@ -425,11 +387,7 @@
     if (p_customconsoleservice)
         p_customconsoleservice->disNotify (handle);
 #endif
-#ifdef CUST_BLEERR_MNGT
-    if (p_customBleErrManagement)
-        p_customBleErrManagement->clearTxErr();
-#endif
-    /* TODO inform other obj implementing BLE services if any other service is added */
+    /* TODO inform other obj implementing BLE services */
 }
 
 //static void onDisconnectionCallback(const Gap::Handle_t handle, const Gap::DisconnectionReason_t disConnectionReason)
@@ -454,14 +412,10 @@
 #ifdef CUST_CONS_SERV
     if (p_customconsoleservice)
         p_customconsoleservice->updateConnectionStatus(DISCONNECTED);
-#endif    
-#ifdef CUST_BLEERR_MNGT
-    if (p_customBleErrManagement)
-        p_customBleErrManagement->updateConnectionStatus(DISCONNECTED);
 #endif
-   /* TODO inform other obj implementing BLE services if any other service is added */
     if (p_BLEdev)
         p_BLEdev->startAdvertising();
+    bleTxErrCnt =0;    
 }
 
 static void onConnectionCallback(const Gap::ConnectionCallbackParams_t * connectionParams)
@@ -488,11 +442,7 @@
     if (p_customconsoleservice)
         p_customconsoleservice->updateConnectionStatus(CONNECTED);
 #endif
-#ifdef CUST_BLEERR_MNGT
-    if (p_customBleErrManagement)
-        p_customBleErrManagement->updateConnectionStatus(CONNECTED);
-#endif
-   /* TODO inform other obj implementing BLE services if any other service is added */
+    bleTxErrCnt =0;
 }
 
 static void onDataReadCallback(const GattReadCallbackParams *eventDataP)
@@ -678,7 +628,7 @@
 static void Ticker_Mems(void)
 {
     TimeStamp++;
-      CounterAGM++;
+    CounterAGM++;
     timeoutMems = 1;
 }
 
@@ -697,16 +647,15 @@
     AxesRaw_TypeDef Gyro;
     bool isgyro_lsm6ds0Present=false;
     bool isgyro_lsm6ds3Present=false;    
-  
+#ifdef CUST_CONS_SERV   
+//    static uint8_t          BufferToWrite[256];
+//    static uint8_t          BytesToWrite;           
+#endif    
     static bool SensorFusionOK = false;
 
     DevI2C *i2c = new DevI2C(I2C_SDA, I2C_SCL);
     i2c->frequency(NUCLEO_I2C_SHIELDS_SPEED);
 
-#ifdef CUST_BLEERR_MNGT
-    p_customBleErrManagement = CustomBleErrManagement::Instance();
-#endif
-
 #ifdef CUST_SENS_SERV
     p_mems_expansion_board = X_NUCLEO_IKS01A1::Instance(i2c);
     if (p_mems_expansion_board->gyro_lsm6ds0) isgyro_lsm6ds0Present=true;
@@ -760,7 +709,7 @@
     p_BLEdev->gattServer().onConfirmationReceived(onConfirmationReceivedCallback);    
     p_BLEdev->gattServer().onDataSent(onDataSentCallback);
     p_BLEdev->gattServer().onDataRead(onDataReadCallback);
-    p_BLEdev->gattServer().onDataWritten(myonDataWriteCallback);
+        p_BLEdev->gattServer().onDataWritten(myonDataWriteCallback);
     p_BLEdev->gap().onConnection(onConnectionCallback);
     p_BLEdev->gap().onDisconnection(onDisconnectionCallback);
     p_BLEdev->gap().onTimeout(onTimeoutCallback);
@@ -778,7 +727,7 @@
     }   
         
 #ifdef CUST_CONFIG_SERV
-    p_customconfigservice = new CustomConfigService(*p_BLEdev, p_customBleErrManagement);
+    p_customconfigservice = new CustomConfigService(*p_BLEdev);
     if (!p_customconfigservice) {
         printf("SW      Service W2ST calibration add FAILED!\n\r");
         return 0;  
@@ -786,7 +735,7 @@
     printf("SW      Service W2ST calibration added successfully\r\n");
 #endif
 #ifdef CUST_CONS_SERV
-    p_customconsoleservice = new CustomConsoleService(*p_BLEdev, p_customBleErrManagement);
+    p_customconsoleservice = new CustomConsoleService(*p_BLEdev);
     if (!p_customconsoleservice) {
         printf("\n\rHW      Service W2ST console add FAILED!\n\r");
         return 0;
@@ -794,7 +743,7 @@
     printf("\rHW      Service W2ST console added successfully\r\n");
 #endif
 #ifdef CUST_SENS_SERV
-    p_customsensorservice = new CustomSensorService(*p_BLEdev, p_customBleErrManagement);
+    p_customsensorservice = new CustomSensorService(*p_BLEdev);
     if (!p_customsensorservice) {
         printf("\n\rHW      Service W2ST sensors add FAILED!\n\r");
         return 0;
@@ -805,7 +754,7 @@
         AxesRaw_TypeDef quat_axes[SEND_N_QUATERNIONS];
         float QuatFloat[SEND_N_QUATERNIONS];        
         if (SensorFusionOK) {
-            p_customsoftwareservice = new CustomSoftwareService(*p_BLEdev, p_customBleErrManagement);
+            p_customsoftwareservice = new CustomSoftwareService(*p_BLEdev);
             if (!p_customsoftwareservice) {
                     printf("SW      Service W2ST quaternions add FAILED!\n\r");
                     return 0;
@@ -817,9 +766,7 @@
         const static char     DEVICE_NAME[]        = BLE_DEV_NAME;
     /* Setup advertising. */
     p_BLEdev->gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
-    p_BLEdev->gap().accumulateAdvertisingPayloadTxPower(8);
-    p_BLEdev->gap().setTxPower(8);    
-    
+//    p_BLEdev->gap().accumulateAdvertisingPayloadTxPower(txPower); 
 #ifdef USE_SENSOR_FUSION_LIB        
     uint8_t dat[]= {0x01,0x80,0x00,0xFC,0x01,0x80};
 #else
@@ -901,12 +848,61 @@
                             }    
             if(CounterAGM >= ACC_GYRO_MAG_UPDATE_MUL_10MS) {
                 CounterAGM=0;
-                p_customsensorservice->updateEnvAccelerometer (&Acc, TimeStamp);
-                p_customsensorservice->updateEnvGyroscope(&Gyro, TimeStamp);
-                p_customsensorservice->updateEnvMagnetometer(&Magn, TimeStamp, magOffset);
-                p_customsensorservice->updateEnvAccGyroMag (&Acc, &Gyro, &Magn, TimeStamp, magOffset);
+                uint32_t bleerr;
+#ifdef BLE_TX_ERR_MNG 
+                if (bleTxErrCnt == 0) {
+#endif                                 
+                    bleerr = p_customsensorservice->updateEnvAccelerometer (&Acc, TimeStamp);
+#ifdef BLE_TX_ERR_MNG 
+                    if (bleerr) {
+                        bleTxErrCnt = BLE_TX_SKIP_NUM;   
+                        printf ("===> BLE_TX_ERR %d\n\r", err);
+                    }                            
+                } else {
+                    bleTxErrCnt--;
+                }
+#endif                     
+#ifdef BLE_TX_ERR_MNG 
+                if (bleTxErrCnt == 0) {
+#endif                                 
+                    bleerr = p_customsensorservice->updateEnvGyroscope(&Gyro, TimeStamp);
+#ifdef BLE_TX_ERR_MNG 
+                    if (bleerr) {
+                        bleTxErrCnt = BLE_TX_SKIP_NUM;   
+                        printf ("===> BLE_TX_ERR %d\n\r", err);
+                    }                            
+                } else {
+                    bleTxErrCnt--;
+                }
+#endif                
+#ifdef BLE_TX_ERR_MNG 
+                if (bleTxErrCnt == 0) {
+#endif                                                 
+                    bleerr = p_customsensorservice->updateEnvMagnetometer(&Magn, TimeStamp, magOffset);
+#ifdef BLE_TX_ERR_MNG 
+                    if (bleerr) {
+                        bleTxErrCnt = BLE_TX_SKIP_NUM;   
+                        printf ("===> BLE_TX_ERR %d\n\r", err);
+                    }                            
+                } else {
+                    bleTxErrCnt--;
+                }
+#endif                
+#ifdef BLE_TX_ERR_MNG 
+                if (bleTxErrCnt == 0) {
+#endif                                                                     
+                    bleerr = p_customsensorservice->updateEnvAccGyroMag (&Acc, &Gyro, &Magn, TimeStamp, magOffset);
+#ifdef BLE_TX_ERR_MNG 
+                    if (bleerr) {
+                        bleTxErrCnt = BLE_TX_SKIP_NUM;   
+                        printf ("===> BLE_TX_ERR %d\n\r", err);
+                    }                            
+                } else {
+                    bleTxErrCnt--;
+                }                    
             }
 #endif
+#endif
 
 #ifdef USE_SENSOR_FUSION_LIB
             if (SensorFusionOK) {
@@ -927,8 +923,21 @@
                     quat_axes[QuaternionNumber].AXIS_Z = (int)(p_MotionFX_Engine_Out->quaternion_9X[2] * 10000);
                 }
 
+                uint32_t bleerr;
                 if (QuaternionNumber == SEND_N_QUATERNIONS-1) {
-                    p_customsoftwareservice->updateQuaternions(quat_axes, TimeStamp);
+#ifdef BLE_TX_ERR_MNG 
+                    if (bleTxErrCnt == 0) {
+#endif                                       
+                        bleerr = p_customsoftwareservice->updateQuaternions(quat_axes, TimeStamp);                    
+#ifdef BLE_TX_ERR_MNG 
+                        if (bleerr) {
+                            bleTxErrCnt = BLE_TX_SKIP_NUM;   
+                            printf ("===> BLE_TX_ERR %d\n\r", bleerr);
+                        }                            
+                    } else {
+                        bleTxErrCnt--;
+                    }
+#endif              
                     QuaternionNumber =0;
                 } else {
                     QuaternionNumber++;
@@ -946,7 +955,20 @@
                         QuatFloat[1] =   p_MotionFX_Engine_Out->quaternion_9X[1];
                         QuatFloat[2] =   p_MotionFX_Engine_Out->quaternion_9X[2];
                     }
-                    p_customsoftwareservice->updateFloatQuaternions(QuatFloat, TimeStamp);
+                    uint32_t bleerr;
+#ifdef BLE_TX_ERR_MNG 
+                    if (bleTxErrCnt == 0) {
+#endif                         
+                    bleerr = p_customsoftwareservice->updateFloatQuaternions(QuatFloat, TimeStamp);
+#ifdef BLE_TX_ERR_MNG 
+                        if (bleerr) {
+                            bleTxErrCnt = BLE_TX_SKIP_NUM;   
+                            printf ("===> BLE_TX_ERR %d\n\r", bleerr);
+                        }                            
+                    } else {
+                        bleTxErrCnt--;
+                    }
+#endif                         
                     CounterFloat=0;
                 } else  {
                     CounterFloat++;
@@ -995,34 +1017,73 @@
 #ifdef CUST_SENS_SERV
                     if (p_customsensorservice->isTempNotificationEn()) {
                         err = p_mems_expansion_board->ht_sensor->GetTemperature(&temp);
-            if ( err != HUM_TEMP_OK) {
+                        if ( err != HUM_TEMP_OK) {
                             printf ("= * ERROR %d GetTemperature\n\r", err);
                         } else {
                             floatToInt(temp, &intPart, &decPart, 1);
                             TempToSend = intPart*10+decPart;
-                            p_customsensorservice->updateEnvTemperature(TempToSend, TimeStamp);
+                            uint32_t bleerr;
+#ifdef BLE_TX_ERR_MNG 
+                            if (bleTxErrCnt == 0) {
+#endif                               
+                                bleerr = p_customsensorservice->updateEnvTemperature(TempToSend, TimeStamp);
+#ifdef BLE_TX_ERR_MNG 
+                                if (bleerr) {
+                                    bleTxErrCnt = BLE_TX_SKIP_NUM;   
+                                    printf ("===> BLE_TX_ERR %d\n\r", bleerr);
+                                }                            
+                            } else {
+                                bleTxErrCnt--;
+                            }
+#endif                                 
                         }
                     }
 
                     if (p_customsensorservice->isHumNotificationEn()) {
                         err = p_mems_expansion_board->ht_sensor->GetHumidity(&hum);
-            if ( err != HUM_TEMP_OK) {
+                        if ( err != HUM_TEMP_OK) {
                             printf ("= * ERROR %d GetHumidity\n\r", err);                           
                         } else {
                             floatToInt(hum, &intPart, &decPart, 1);
                             HumToSend = intPart*10+decPart;
-                            p_customsensorservice->updateEnvHumidity(HumToSend, TimeStamp);
+uint32_t bleerr;
+#ifdef BLE_TX_ERR_MNG 
+                            if (bleTxErrCnt == 0) {
+#endif                             
+                                bleerr = p_customsensorservice->updateEnvHumidity(HumToSend, TimeStamp);
+#ifdef BLE_TX_ERR_MNG 
+                                if (bleerr) {
+                                    bleTxErrCnt = BLE_TX_SKIP_NUM;   
+                                    printf ("===> BLE_TX_ERR %d\n\r", err);
+                                }                            
+                            } else {
+                                bleTxErrCnt--;
+                            }
+#endif                                 
                         }
                     }
 
                     if (p_customsensorservice->isPresNotificationEn()) {
                         err = p_mems_expansion_board->pt_sensor->GetPressure(&pres);
-            if ( err != PRESSURE_OK) {
+                        if ( err != PRESSURE_OK) {
                             printf ("= * ERROR GetPressure\n\r");                           
                         } else {
                             floatToInt(pres, &intPart, &decPart, 1);
                             PresToSend = intPart*100+decPart;
-                            p_customsensorservice->updateEnvPressure(PresToSend, TimeStamp);
+uint32_t bleerr;
+#ifdef BLE_TX_ERR_MNG 
+                            if (bleTxErrCnt == 0) {
+#endif                               
+                                bleerr = p_customsensorservice->updateEnvPressure(PresToSend, TimeStamp);
+#ifdef BLE_TX_ERR_MNG 
+                               if (bleerr) {
+                                    bleTxErrCnt = BLE_TX_SKIP_NUM;   
+                                    printf ("===> BLE_TX_ERR %d\n\r", err);
+                                }                            
+                            } else {
+                                bleTxErrCnt--;
+                            }
+#endif                                   
                         }
                     }
 #endif