The cube puck updates its gatt attributes on rotation. Listen in!

Dependencies:   BLE_API MPU6050 mbed nRF51822

Fork of cube-puck by Nordic Pucks

Revision:
2:b9b42ff80e9a
Parent:
0:449ee9595cf6
--- a/main.cpp	Thu Jul 03 11:26:44 2014 +0000
+++ b/main.cpp	Wed Jul 09 15:02:59 2014 +0000
@@ -2,7 +2,7 @@
 #include "BLEDevice.h"
 #include "nRF51822n.h"
 #include "MPU6050.h"
-#include <math.h> 
+#include <math.h>
 
 #define DEBUG 1
 
@@ -28,7 +28,7 @@
     0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, // UUID
     0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61,
     0x13, 0x37, // the major value to differenciate a location
-    0xFA, 0xCE, // the minor value to differenciate a location
+    0xC0, 0xBE, // the minor value to differenciate a location
     0xC8        // 2's complement of the Tx power (-56dB)
 };
 
@@ -45,39 +45,39 @@
 
 void log_direction(void)
 {
-    switch(direction)
-    {
+    switch(direction) {
         case UP:
             pc.printf("Direction UP\n");
             break;
-            
+
         case DOWN:
             pc.printf("Direction DOWN\n");
             break;
-            
+
         case LEFT:
             pc.printf("Direction LEFT\n");
             break;
-            
+
         case RIGHT:
             pc.printf("Direction RIGHT\n");
             break;
-            
+
         case BACK:
             pc.printf("Direction BACK\n");
             break;
-            
+
         case FRONT:
             pc.printf("Direction FRONT\n");
             break;
-            
+
         default:
             pc.printf("Direction UNSET\n");
             break;
     }
 }
 
-int16_t inline direction_if_exited(int16_t acceleration) {
+int16_t direction_if_exited(int16_t acceleration)
+{
     if (acceleration > ACCELERATION_EXITATION_THRESHOLD) {
         return 1;
     } else if (acceleration < -ACCELERATION_EXITATION_THRESHOLD) {
@@ -87,47 +87,55 @@
     }
 }
 
+void update_direction_characteristic(void)
+{
+    direction_data[0] = direction;
+    ble.updateCharacteristicValue(directionCharacteristic.getHandle(),
+                                  direction_data,
+                                  sizeof(direction_data));
+#if DEBUG
+    //pc.printf("Updated gatt characteristic\n");
+#endif
+}
+
 void update_cube_direction(void)
 {
     mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
-    
+
     int16_t x = direction_if_exited(ax);
     int16_t y = direction_if_exited(ay);
     int16_t z = direction_if_exited(az);
-    
+
     int16_t sum = abs(x) + abs(y) + abs(z);
-    if (sum == 0 || sum != 1) {
+    if (sum != 1) {
         return;
     }
-    
+
+    Direction new_direction;
     if (z == 1) {
-        direction = UP;
+        new_direction = UP;
     } else if (z == -1) {
-        direction = DOWN;
+        new_direction = DOWN;
     } else if (y == 1) {
-        direction = LEFT;
+        new_direction = LEFT;
     } else if (y == -1) {
-        direction = RIGHT;
+        new_direction = RIGHT;
     } else if (x == 1) {
-        direction = BACK;
+        new_direction = BACK;
     } else if (x == -1) {
-        direction = FRONT;
+        new_direction = FRONT;
     }
-    
+
+    if (direction == new_direction) {
+        return;
+    }
+
+    direction = new_direction;
+
 #if DEBUG
     log_direction();
 #endif
-}
-
-void update_direction_characteristic(void)
-{    
-    direction_data[0] = direction;
-    ble.updateCharacteristicValue(directionCharacteristic.getHandle(),
-            direction_data,
-            sizeof(direction_data));
-#if DEBUG
-    pc.printf("Updated gatt characteristic\n");
-#endif
+    update_direction_characteristic();
 }
 
 void disconnectionCallback(void)
@@ -137,6 +145,11 @@
     ble.startAdvertising();
 }
 
+void periodicCallback(void)
+{
+    update_cube_direction();
+}
+
 void setup_ble(void)
 {
     ble.init();
@@ -144,39 +157,37 @@
 
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,
-                    beaconPayload, sizeof(beaconPayload));
-                    
+                                     beaconPayload, sizeof(beaconPayload));
+
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.setAdvertisingInterval(160);
 
     ble.startAdvertising();
-    
+
     ble.addService(cube_service);
-    
+
     pc.printf("BLE set up and running\n");
 }
 
 int main()
 {
     setup_ble();
+
+    Ticker ticker;
+    ticker.attach(periodicCallback, 1);
+
     pc.printf("MPU6050 test startup:\n");
 
     mpu.initialize();
     pc.printf("TestConnection\n");
-    
-    if (mpu.testConnection())
-    {
+
+    if (mpu.testConnection()) {
         pc.printf("MPU success\n");
-    }
-    else
-    {
+    } else {
         pc.printf("MPU error\n");
     }
 
-    while(1)
-    {
+    while(1) {
         ble.waitForEvent();
-        update_cube_direction();
-        update_direction_characteristic();
     }
 }
\ No newline at end of file