3K04 Project / Mbed 2 deprecated Pacemaker

Dependencies:   mbed-rtos mbed

Revision:
2:4fb5a8d15f9c
Parent:
1:8f545f45d899
--- a/motion.cpp	Fri Nov 25 03:44:54 2016 +0000
+++ b/motion.cpp	Fri Dec 02 19:15:25 2016 +0000
@@ -1,6 +1,7 @@
 #include "rtos.h"
 #include "pinmap.h"
 #include "FXOS8700Q.h"
+#include "motion.h"
 
 
 FXOS8700Q_acc acc(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1);
@@ -8,7 +9,8 @@
 /* Constants and Declares */
 int numCount;
 int const MAX_NUM_COUNTS = 3;
-int const TIMING_PERIOD = 20; // Sensor polling interval
+int const TIMING_PERIOD = 1; // Sensor polling interval
+int events [30];
 
 uint8_t motion_exceeded_threshold = 0;
 
@@ -43,7 +45,7 @@
  */ 
 void a_count(void) {
     /* step 1 increment the counter */
-    numCount++;
+    numCount = sumArray();
     
 
     if (numCount >= MAX_NUM_COUNTS) {
@@ -56,7 +58,6 @@
 
 void motion_thread () {
     while(true) {
-
         float xAcc, yAcc, zAcc;
         acc.getX(&xAcc);
         acc.getY(&yAcc);
@@ -64,9 +65,27 @@
         float magtd = xAcc*xAcc + yAcc*yAcc + zAcc*zAcc;
         
         if (magtd > 3.0f) { // Greater than (1.5G of Accel.)^2
+            addEvent(1);
             a_count();      // increment acceleration event counter
+        }else{
+            addEvent(0);
         }
 
         Thread::wait(TIMING_PERIOD); 
     }   
+}
+
+void addEvent(int input){
+    for(int i=29; i >= 1;i--){
+        events[i]=events[i-1];
+    }
+    events[0]=input;    
+}
+
+int sumArray(){
+    int sum=0;
+    for(int i=0;i<=29;i++){
+        sum += events[i];        
+    }    
+    return sum;
 }
\ No newline at end of file