Swim-It, is a waterproof counter for arm movement

Dependencies:   Hexi_KW40Z Hexi_OLED_SSD1351

Fork of Hexi_BLE_Example by Hexiwear

Revision:
3:f9d351e32e78
Parent:
1:a0d9eeedb771
--- a/main.cpp	Tue Sep 20 22:56:09 2016 +0000
+++ b/main.cpp	Sun Oct 09 03:19:16 2016 +0000
@@ -1,22 +1,35 @@
+//This file is based on example of Hexi_Accelero_Magnet and Hexi_BLE_Example, logic of swim it is added as core lines for arm stroke counting in 
 #include "mbed.h"
 #include "Hexi_KW40Z.h"
 #include "Hexi_OLED_SSD1351.h"
+#include "./FXOS8700/FXOS8700.h"
 #include "OLED_types.h"
 #include "OpenSans_Font.h"
 #include "string.h"
 
 #define LED_ON      0
 #define LED_OFF     1
+
+#define MAG_THREHOLD        0.5
+#define ACCEL_THREHOLD      0.5
+   
+Timeout swimit_reset;
+static uint16_t swimit_count;
    
 void StartHaptic(void);
 void StopHaptic(void const *n);
 void txTask(void);
+void swimit_reseter(void);
 
 DigitalOut redLed(LED1,1);
 DigitalOut greenLed(LED2,1);
 DigitalOut blueLed(LED3,1);
 DigitalOut haptic(PTB9);
 
+// Pin connections & address for Hexiwear
+FXOS8700 accel(PTC11, PTC10);
+FXOS8700 mag(PTC11, PTC10);
+
 /* Define timer for haptic feedback */
 RtosTimer hapticTimer(StopHaptic, osTimerOnce);
 
@@ -32,6 +45,8 @@
  /* Text Buffer */ 
 char text[20]; 
 
+
+
 /****************************Call Back Functions*******************************/
 void ButtonRight(void)
 {
@@ -62,6 +77,12 @@
 
 int main()
 {    
+    // Configure Accelerometer FXOS8700, Magnetometer FXOS8700
+    accel.accel_config();
+    mag.mag_config();
+
+
+        
     /* Get OLED Class Default Text Properties */
     oled_text_properties_t textProperties = {0};
     oled.GetTextProperties(&textProperties);    
@@ -102,6 +123,7 @@
     {
         blueLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/   
         Thread::wait(50);
+        
     }
 }
 
@@ -110,35 +132,48 @@
 
 /* txTask() transmits the sensor data */
 void txTask(void){
-   
+
+    float accel_data[3];
+    float accel_rms=0.0;
+    float mag_data[3];   
+    float mag_rms=0.0;   
+    uint16_t steps=0;
+    float accel_rms_ave=accel_rms;
+    
    while (true) 
    {
-        
+
+ 
+    
+        /*acquire accel_data and mag_data, running Sensor Tag mode*/
+      accel.acquire_accel_data_g(accel_data);
+      accel_rms = sqrt(((accel_data[0]*accel_data[0])+(accel_data[1]*accel_data[1])+(accel_data[2]*accel_data[2]))/3);
+      Thread::wait(0.01);
+      
+      mag.acquire_mag_data_uT(mag_data);
+      mag_rms = sqrt(((mag_data[0]*mag_data[0])+(mag_data[1]*mag_data[1])+(mag_data[2]*mag_data[2]))/3);
+      Thread::wait(0.01);
+    
+        //rm Strok counting by change of MAG and ACCEL at the same time, 
+      if (accel_rms>ACCEL_THREHOLD ||mag_rms>MAG_THREHOLD)   swimit_count++;
+      steps = swimit_count;
+      accel_rms_ave=accel_rms;
+
+      
+      // Reset the step after 60 second without any movement;
+      if (abs(accel_rms_ave-accel_rms)<0.1)    swimit_reset.attach(&swimit_reseter, 60.0);
+
+
         /*Notify Hexiwear App that it is running Sensor Tag mode*/
         kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG);
                 
-        /*The following is sending dummy data over BLE. Replace with real data*/
+
+              
+        /*The following is sending Mag,Accel,Gyro Data over BLE.*/
     
-        /*Send Battery Level for 20% */ 
-        kw40z_device.SendBatteryLevel(20);
-               
-        /*Send Ambient Light Level at 50% */ 
-        kw40z_device.SendAmbientLight(50);
-        
-        /*Send Humidity at 90% */
-        kw40z_device.SendHumidity(9000);
-        
-        /*Send Temperature at 25 degrees Celsius */
-        kw40z_device.SendTemperature(2500);
-
-        /*Send Pressure at 100kPA */ 
-        kw40z_device.SendPressure(10000);
-        
-        /*Send Mag,Accel,Gyro Data. */
-        kw40z_device.SendGyro(0,0,0);
-        kw40z_device.SendAccel(0,0,0);        
-        kw40z_device.SendMag(0,0,0);        
-        Thread::wait(1000);                 
+        kw40z_device.SendAccel(accel_data[0],accel_data[1],accel_data[2]); 
+        kw40z_device.SendSteps(steps);  
+        Thread::wait(10);                 
     }
 }
 
@@ -152,3 +187,6 @@
     hapticTimer.stop();
 }
 
+void swimit_reseter() {
+swimit_count=0;
+}
\ No newline at end of file