Pedometer to BT

Dependencies:   FXOS8700 Hexi_KW40Z Hexi_OLED_SSD1351

Fork of Hexi_BLE_Example_Modified by Xi Han

Files at this revision

API Documentation at this revision

Comitter:
catchvibes95
Date:
Tue Jun 05 01:58:28 2018 +0000
Parent:
4:20d4eebfa986
Commit message:
Ped_Blue;

Changed in this revision

FXOS8700.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FXOS8700.lib	Tue Jun 05 01:58:28 2018 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/AswinSivakumar/code/FXOS8700/#98ea52282575
--- a/main.cpp	Fri May 04 08:56:11 2018 +0000
+++ b/main.cpp	Tue Jun 05 01:58:28 2018 +0000
@@ -4,6 +4,7 @@
 #include "OLED_types.h"
 #include "OpenSans_Font.h"
 #include "string.h"
+#include "FXOS8700.h"
 
 #define LED_ON      0
 #define LED_OFF     1
@@ -13,7 +14,9 @@
 void StopHaptic(void const *n);
 void txTask(void);
 
-Serial pc(USBTX, USBRX);
+DigitalOut led1(LED_GREEN); // RGB LED
+Serial pc(USBTX, USBRX); // Serial interface
+FXOS8700 accel(PTC11, PTC10);
 
 DigitalOut redLed(LED1,1);
 DigitalOut greenLed(LED2,1);
@@ -32,6 +35,9 @@
 /*Create a Thread to handle sending BLE Sensor Data */ 
 Thread txThread;
 
+Ticker flipper;
+
+
  /* Text Buffer */ 
 char text[20]; 
 
@@ -44,7 +50,43 @@
 uint16_t y = 5000;
 uint16_t z = 10000;
 
+// Variables
+float accel_data[3]; // Storage for the data from the sensor
+float accel_rms=0.0; // RMS value from the sensor
+float ax, ay, az; // Integer value from the sensor to be displayed
+const uint8_t *image1; // Pointer for the image1 to be displayed
+char text1[20]; // Text Buffer for dynamic value displayed
+char text2[20]; // Text Buffer for dynamic value displayed
+char text3[20]; // Text Buffer for dynamic value displayed
+float dot;
+float old_acc=0;
+float new_acc=0;
+float old_accx, old_accy, old_accz, old_dot=0.0;
+uint8_t StepNum = 0, StepNumber = 0;
+
+float filter_buf[75];
+
+
+
+
+
 /****************************Call Back Functions*******************************/
+
+float Filter(int s) 
+{
+    accel.acquire_accel_data_g(accel_data);
+    float filter_sum = 0.0;
+    //printf("%d\n\r",s);
+    for(int i = 0; i < 75; i++) 
+    {
+    filter_buf[i] = accel_data[s];
+    //printf("%4.2f\n\r",filter_buf[i]);
+    filter_sum += filter_buf[i];
+    }
+    return (float)(filter_sum / 75);
+}
+
+
 void ButtonRight(void)
 {
     StartHaptic();
@@ -57,6 +99,53 @@
     kw40z_device.ToggleAdvertisementMode();
 }
 
+void ButtonUp(void)
+{
+    StartHaptic();
+    oled.FillScreen(COLOR_BLACK);
+
+    /* Get OLED Class Default Text Properties */
+    oled_text_properties_t textProperties = {0};
+    oled.GetTextProperties(&textProperties);    
+        
+    /* Change font color to Blue */ 
+    textProperties.fontColor   = COLOR_BLUE;
+    oled.SetTextProperties(&textProperties);
+    strcpy((char *) text1,"Steps: ");
+    oled.Label((uint8_t *)text1,3,45);      
+    sprintf(text1,"%d",StepNumber);
+      /* Display time reading in 35px by 15px textbox at(x=55, y=40) */
+    oled.TextBox((uint8_t *)text1,70,45,20,15); //Increase textbox for more digits
+    
+    
+}
+void ButtonDown(void)
+{
+    oled.FillScreen(COLOR_BLACK);
+
+    /* Get OLED Class Default Text Properties */
+    oled_text_properties_t textProperties = {0};
+    oled.GetTextProperties(&textProperties);    
+        
+    /* Change font color to Blue */ 
+    textProperties.fontColor   = COLOR_BLUE;
+    oled.SetTextProperties(&textProperties);
+    
+    /* Display Bluetooth Label at x=17,y=65 */ 
+    strcpy((char *) text,"BLUETOOTH");
+    oled.Label((uint8_t *)text,17,65);
+    
+    /* Change font color to white */ 
+    textProperties.fontColor   = COLOR_WHITE;
+    textProperties.alignParam = OLED_TEXT_ALIGN_CENTER;
+    oled.SetTextProperties(&textProperties);
+    
+    /* Display Label at x=22,y=80 */ 
+    strcpy((char *) text,"Tap Below");
+    oled.Label((uint8_t *)text,22,80);
+    
+    
+}
 void PassKey(void)
 {
     StartHaptic();
@@ -98,9 +187,13 @@
 
 int main()
 {    
+
+     accel.accel_config();
     /* Register callbacks to application functions */
     kw40z_device.attach_buttonLeft(&ButtonLeft);
     kw40z_device.attach_buttonRight(&ButtonRight);
+    kw40z_device.attach_buttonDown(&ButtonDown);
+    kw40z_device.attach_buttonUp(&ButtonUp);
     kw40z_device.attach_passkey(&PassKey);
     kw40z_device.attach_alert(&AlertReceived);
 
@@ -110,28 +203,7 @@
     oled.DimScreenON();
     
     /* Fills the screen with solid black */         
-    oled.FillScreen(COLOR_BLACK);
-
-    /* Get OLED Class Default Text Properties */
-    oled_text_properties_t textProperties = {0};
-    oled.GetTextProperties(&textProperties);    
-        
-    /* Change font color to Blue */ 
-    textProperties.fontColor   = COLOR_BLUE;
-    oled.SetTextProperties(&textProperties);
     
-    /* Display Bluetooth Label at x=17,y=65 */ 
-    strcpy((char *) text,"BLUETOOTH");
-    oled.Label((uint8_t *)text,17,65);
-    
-    /* Change font color to white */ 
-    textProperties.fontColor   = COLOR_WHITE;
-    textProperties.alignParam = OLED_TEXT_ALIGN_CENTER;
-    oled.SetTextProperties(&textProperties);
-    
-    /* Display Label at x=22,y=80 */ 
-    strcpy((char *) text,"Tap Below");
-    oled.Label((uint8_t *)text,22,80);
          
     uint8_t prevLinkState = 0; 
     uint8_t currLinkState = 0;
@@ -161,49 +233,72 @@
         /*The following is sending dummy data over BLE. Replace with real data*/
     
         /*Send Battery Level for 20% */ 
-        kw40z_device.SendBatteryLevel(battery);
+        kw40z_device.SendBatteryLevel(StepNumber);
                
         /*Send Ambient Light Level at 50% */ 
-        kw40z_device.SendAmbientLight(light);
+        //kw40z_device.SendAmbientLight(light);
         
         /*Send Humidity at 90% */
-        kw40z_device.SendHumidity(humidity);
+        //kw40z_device.SendHumidity(humidity);
         
         /*Send Temperature at 25 degrees Celsius */
-        kw40z_device.SendTemperature(temperature);
+        //kw40z_device.SendTemperature(temperature);
 
         /*Send Pressure at 100kPA */ 
-        kw40z_device.SendPressure(pressure);
+        //kw40z_device.SendPressure(pressure);
         
         /*Send Mag,Accel,Gyro Data. */
-        kw40z_device.SendGyro(x,y,z);
-        kw40z_device.SendAccel(z,x,y);
-        kw40z_device.SendMag(y,z,x);
+       // kw40z_device.SendGyro(x,y,z);
+       // kw40z_device.SendAccel(z,x,y);
+       // kw40z_device.SendMag(y,z,x);
 
-        Thread::wait(1000);                 
+        Thread::wait(10);                 
     }
 }
 
 void UpdateSensorData(void)
 {    
-    battery -= 5;
-    if(battery < 5) battery = 100;
-    
-    light += 20;
-    if(light > 100) light = 0;
-    
-    humidity += 500;
-    if(humidity > 8000) humidity = 2000;
-    
-    temperature -= 200;
-    if(temperature < 200) temperature = 4200;
-    
-    pressure += 300;
-    if(pressure > 10300) pressure = 7500;
-    
-    x += 1400;
-    y -= 2300;
-    z += 1700;
+        accel.acquire_accel_data_g(accel_data);
+      //printf("Accelerometer \tX-Axis %4.2f \tY-Axis %4.2f \tZ-Axis %4.2f \tRMS %4.2f\n\r",accel_data[0],accel_data[1],accel_data[2],accel_rms);
+      ax = Filter(0);
+      ay = Filter(1);
+      az = Filter(2);  
+      wait(0.02);           
+      accel_rms = sqrt((ax*ax)+(ay*ay)+(az*az)/3);
+      //printf("Accelerometer \tX-Axis %4.2f \tY-Axis %4.2f \tZ-Axis %4.2f \tRMS %4.2f\n\r",ax,ay,az,accel_rms);
+      dot = (old_accx * ax)+(old_accy * ay)+(old_accz * az);
+      old_acc = abs(sqrt(old_accx*old_accx+old_accy*old_accy+old_accz*old_accz));
+      new_acc = abs(sqrt(ax*ax+ay*ay+az*az));
+      //printf("\nOld Acceleration: %4.2f\n\r",old_acc);
+      //printf("New Acceleration: %4.2f\n\r",new_acc);
+      dot /= (old_acc * new_acc);
+      //printf("\nDot: %4.2f\n\r",dot);
+      //printf("Old Dot: %4.2f\n\r",old_dot);
+      
+       oled_text_properties_t textProperties = {0};
+      oled.GetTextProperties(&textProperties); 
+      
+      /* Set text properties to white and right aligned for the dynamic text */
+      textProperties.fontColor = COLOR_BLUE;
+      textProperties.alignParam = OLED_TEXT_ALIGN_LEFT;
+      oled.SetTextProperties(&textProperties);  
+      
+      /* Display Legends */
+      //strcpy((char *) text1,"Steps: ");
+      //oled.Label((uint8_t *)text1,3,45);      
+      StepNum = StepNumber;
+      if(abs(dot - old_dot) >= 0.05 && abs(dot - old_dot) <= 0.10) 
+      {
+            StepNumber += 1;
+        
+      }
+      //printf("%4.2f\n\r",dot);
+      old_accx = ax;
+      old_accy = ay;
+      old_accz = az;
+      old_dot = dot;
+      
+        Thread::wait(250);
 }
 
 void StartHaptic(void)  {