Rotate the Cube Puck to invoke actions on your smartphone. Built on the Puck IOT platform.

Dependencies:   Puck MPU6050 mbed

The Cube Puck is an innovative bluetooth-enabled remote control device. It is a six-sided cube that can be rotated to any of its sides to invoke actions linked to that side. The cube puck is completely customizable and therefore also quite versatile.

A tutorial for the Cube Puck is available on GitHub.

Tutorials and in-depth documentation for the Puck platform is available at the project's GitHub page

Files at this revision

API Documentation at this revision

Comitter:
sigveseb
Date:
Thu Jul 24 14:28:09 2014 +0000
Parent:
2:b9b42ff80e9a
Child:
4:6a2b306b6b41
Commit message:
Update to use new puck lib

Changed in this revision

BLE_API.lib Show diff for this revision Revisions of this file
Puck.lib Show annotated file Show diff for this revision Revisions of this file
gatt_service.cpp Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
nRF51822.lib Show diff for this revision Revisions of this file
--- a/BLE_API.lib	Wed Jul 09 15:02:59 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#6f4c8e545d38
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Puck.lib	Thu Jul 24 14:28:09 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/teams/Nordic-Pucks/code/Puck/#49ffd38fb401
--- a/gatt_service.cpp	Wed Jul 09 15:02:59 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,23 +0,0 @@
-#include "BLEDevice.h"
-
-uint8_t uuid_array_service[16]   = {'b', 'f', 't', 'j', ' ', 'c', 'u', 'b', 'e', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
-uint8_t uuid_array_direction[16] = {'b', 'f', 't', 'j', ' ', 'c', 'u', 'b', 'e', ' ', 'd', 'i', 'r', 'c', 't', 'n' };
-
-const UUID uuid_service = UUID(uuid_array_service);
-const UUID uuid_direction = UUID(uuid_array_direction);
-
-uint8_t direction_data[1] = {6};
-
-GattCharacteristic directionCharacteristic(
-        uuid_direction,
-        direction_data,
-        sizeof(direction_data),
-        sizeof(direction_data),
-        GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ
-        | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
-        
-GattCharacteristic *characteristics[] = {&directionCharacteristic};
-GattService cube_service(uuid_service,
-        characteristics,
-        sizeof(characteristics) / sizeof(GattCharacteristic *));
-
--- a/main.cpp	Wed Jul 09 15:02:59 2014 +0000
+++ b/main.cpp	Thu Jul 24 14:28:09 2014 +0000
@@ -1,10 +1,13 @@
-#include "mbed.h"
-#include "BLEDevice.h"
-#include "nRF51822n.h"
 #include "MPU6050.h"
 #include <math.h>
 
-#define DEBUG 1
+#define LOG_LEVEL_INFO
+#include "Puck.h"
+
+Puck* puck = &Puck::getPuck();
+
+const UUID CUBE_SERVICE_UUID = UUID(stringToUUID("bftj cube       "));
+const UUID DIRECTION_UUID = UUID(stringToUUID("bftj cube dirctn"));
 
 enum Direction {
     UP,
@@ -16,25 +19,7 @@
     UNDEFINED
 };
 
-Serial pc(USBTX, USBRX);
-
-BLEDevice ble;
-
 const static int16_t ACCELERATION_EXITATION_THRESHOLD = 15000;
-const static uint8_t beaconPayload[] = {
-    0x00, 0x4C, // Company identifier code (0x004C == Apple)
-    0x02,       // ID
-    0x15,       // length of the remaining payload
-    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
-    0xC0, 0xBE, // the minor value to differenciate a location
-    0xC8        // 2's complement of the Tx power (-56dB)
-};
-
-extern GattService cube_service;
-extern GattCharacteristic directionCharacteristic;
-extern uint8_t direction_data[1];
 
 MPU6050 mpu;
 
@@ -43,63 +28,29 @@
 
 Direction direction = UNDEFINED;
 
-void log_direction(void)
-{
+void log_direction(Direction 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;
+        case UP: LOG_INFO("Direction UP\n"); break;
+        case DOWN: LOG_INFO("Direction DOWN\n"); break;
+        case LEFT: LOG_INFO("Direction LEFT\n"); break;
+        case RIGHT: LOG_INFO("Direction RIGHT\n"); break;
+        case BACK: LOG_INFO("Direction BACK\n"); break;
+        case FRONT: LOG_INFO("Direction FRONT\n"); break;
+        default: LOG_INFO("Direction UNSET\n"); break;
     }
 }
 
-int16_t 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) {
+    }
+    if (acceleration < -ACCELERATION_EXITATION_THRESHOLD) {
         return -1;
-    } else {
-        return 0;
     }
+    return 0;
 }
 
-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)
-{
+void updateCubeDirection(void) {
     mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
 
     int16_t x = direction_if_exited(ax);
@@ -111,7 +62,7 @@
         return;
     }
 
-    Direction new_direction;
+    Direction new_direction = UNDEFINED;
     if (z == 1) {
         new_direction = UP;
     } else if (z == -1) {
@@ -132,62 +83,35 @@
 
     direction = new_direction;
 
-#if DEBUG
-    log_direction();
-#endif
-    update_direction_characteristic();
-}
-
-void disconnectionCallback(void)
-{
-    pc.printf("Disconnected!\n");
-    pc.printf("Restarting the advertising process\n");
-    ble.startAdvertising();
-}
-
-void periodicCallback(void)
-{
-    update_cube_direction();
+    log_direction(direction);
+    uint8_t directionAsInteger = direction;
+    puck->updateCharacteristicValue(DIRECTION_UUID, &directionAsInteger, 1);
 }
 
-void setup_ble(void)
-{
-    ble.init();
-    ble.onDisconnection(disconnectionCallback);
-
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,
-                                     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() {
+    
+    Ticker ticker;
+    ticker.attach(updateCubeDirection, 1);
 
-int main()
-{
-    setup_ble();
-
-    Ticker ticker;
-    ticker.attach(periodicCallback, 1);
-
-    pc.printf("MPU6050 test startup:\n");
+    LOG_VERBOSE("MPU6050 test startup:\n");
 
     mpu.initialize();
-    pc.printf("TestConnection\n");
+    LOG_VERBOSE("TestConnection\n");
 
     if (mpu.testConnection()) {
-        pc.printf("MPU success\n");
+        LOG_INFO("MPU initialized.\n");
     } else {
-        pc.printf("MPU error\n");
+        LOG_ERROR("MPU not properly initialized!\n");
     }
 
-    while(1) {
-        ble.waitForEvent();
-    }
+    puck->addCharacteristic(
+        CUBE_SERVICE_UUID,
+        DIRECTION_UUID,
+        1,
+        GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
+    
+    puck->init(0xC0BE);
+
+    while(puck->drive());
 }
\ No newline at end of file
--- a/mbed.bld	Wed Jul 09 15:02:59 2014 +0000
+++ b/mbed.bld	Thu Jul 24 14:28:09 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/024bf7f99721
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/04dd9b1680ae
\ No newline at end of file
--- a/nRF51822.lib	Wed Jul 09 15:02:59 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#7174913c9d67