Broadcasts raw accelerometer values

Dependencies:   BLE_API MMA8452_tag_private mbed nRF51822

Fork of tag_final by Luis Bañuelos Chacon

Revision:
2:eb47002f16b5
Parent:
1:1c14c1d3ce09
--- a/main.cpp	Tue Feb 09 19:45:55 2016 +0000
+++ b/main.cpp	Tue Jun 14 00:42:00 2016 +0000
@@ -3,17 +3,9 @@
 #include "MMA8452.h"
 
 // Configuration
-#define BLE_ADV_INTERVAL    500         // Interval between each advertising packet (in MS)
-#define ACC_RATE            10          // Accelerometer sampling rate (in Hz)
-#define ACC_BUFFER_SIZE     10          // Accelerometer history size
-
-// Algorithm
-#define ALG_PERIOD          1           // Algorithm period
-#define ALG_ACC_AXIS        0           // Which accelerometer axis to use
-#define ALG_UP_THRES        0.3         // Up State Threshold
-#define ALG_UP_TTCKS        10          // Up State Trigger Time
-#define ALG_DOWN_THRES      0.3         // Down State Threshold
-#define ALG_DOWN_TICKS      4           // Down State Trigger Time
+#define BLE_ADV_INTERVAL    1000        // Interval between each advertising packet (in MS)
+#define ACC_RATE            8          // Accelerometer sampling rate (in Hz)
+#define ACC_BUFFER_SIZE     8          // Accelerometer history size (DON'T CHANGE)
 
 // Objects
 BLEDevice   ble;                        // BLE
@@ -27,14 +19,10 @@
 // Data
 struct acc_ring {
     double data[3][ACC_BUFFER_SIZE];
+    int8_t data_int[3][ACC_BUFFER_SIZE];
     int head;
 } acc_ring;
 
-enum AlgState {
-    UP,
-    DOWN
-};
-
 // Prototypes
 void onError();
 void onAccTick();
@@ -55,14 +43,22 @@
 }
 
 void onAccTick() {
-    // Increment head
-    acc_ring.head++;
-    if (acc_ring.head >= ACC_BUFFER_SIZE) acc_ring.head = 0;
-    
     // Read accelerometer
     acc.readXGravity(&acc_ring.data[0][acc_ring.head]);
     acc.readYGravity(&acc_ring.data[1][acc_ring.head]);
     acc.readZGravity(&acc_ring.data[2][acc_ring.head]);
+    
+    // Convert to integer
+    acc_ring.data_int[0][acc_ring.head] = (int8_t)(acc_ring.data[0][acc_ring.head] * 100);
+    acc_ring.data_int[1][acc_ring.head] = (int8_t)(acc_ring.data[1][acc_ring.head] * 100);
+    acc_ring.data_int[2][acc_ring.head] = (int8_t)(acc_ring.data[2][acc_ring.head] * 100);
+    
+    // Increment head
+    acc_ring.head++;
+    if (acc_ring.head >= ACC_BUFFER_SIZE) {
+        acc_ring.head = 0;
+        setPayload((uint8_t*)acc_ring.data_int, sizeof(acc_ring.data_int));
+    }
 }
 
 void onButton() {
@@ -80,46 +76,6 @@
         led_tick.detach();
 }
 
-void onAlgTick() {
-    static AlgState state = UP;
-    static AlgState last = DOWN;
-    static int timer;
-    
-    double pos = getAccMean(ALG_ACC_AXIS);
-    
-    switch (state) {
-        case UP:
-            if (pos < ALG_DOWN_THRES) {
-                timer = 0;
-            } else {
-                ledBlink(1, 1);
-            }
-            if (timer > ALG_DOWN_TICKS) {
-                state = DOWN;
-            }
-            break;
-            
-        case DOWN:
-            if (pos > ALG_UP_THRES) {
-                timer = 0;
-            } else {
-                ledBlink(1, 1);
-            }
-            if (timer > 10) {
-                state = UP;
-            }
-            break;
-    }
-    
-    if (state != last) {
-        last = state;
-        setPayload((uint8_t*)&state, 1);
-        ledBlink(2, 0.25);
-    }
-    
-    timer++;
-}
-
 // Functions
 void setPayload(uint8_t * data, uint8_t size) {
     ble.clearScanResponse();
@@ -138,10 +94,6 @@
     led_tick.detach();
 }
 
-double getAccLast(int axis) {
-    return acc_ring.data[axis][acc_ring.head];
-}
-
 double getAccMean(int axis) {
     double mean = 0;
     for (int i = 0; i < ACC_BUFFER_SIZE; i++) {
@@ -186,9 +138,6 @@
     acc.activate();                                         // Activate accelerometer
     acc_tick.attach(&onAccTick, (1.0 / (float)ACC_RATE));   // Setup periodic reads
     
-    // Setup algorithm
-    alg_tick.attach(&onAlgTick, ALG_PERIOD);
-    
     while (1) {
         ble.waitForEvent();
     }