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

Committer:
cristea
Date:
Tue Aug 05 08:53:18 2014 +0000
Revision:
6:4f2aaa06ff44
Parent:
4:6a2b306b6b41
Child:
7:4244572015c6
Add license

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cristea 6:4f2aaa06ff44 1 /**
cristea 6:4f2aaa06ff44 2 * Copyright 2014 Nordic Semiconductor
cristea 6:4f2aaa06ff44 3 *
cristea 6:4f2aaa06ff44 4 * Licensed under the Apache License, Version 2.0 (the "License");
cristea 6:4f2aaa06ff44 5 * you may not use this file except in compliance with the License.
cristea 6:4f2aaa06ff44 6 * You may obtain a copy of the License at
cristea 6:4f2aaa06ff44 7 *
cristea 6:4f2aaa06ff44 8 * http://www.apache.org/licenses/LICENSE-2.0
cristea 6:4f2aaa06ff44 9 *
cristea 6:4f2aaa06ff44 10 * Unless required by applicable law or agreed to in writing, software
cristea 6:4f2aaa06ff44 11 * distributed under the License is distributed on an "AS IS" BASIS,
cristea 6:4f2aaa06ff44 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
cristea 6:4f2aaa06ff44 13 * See the License for the specific language governing permissions and
cristea 6:4f2aaa06ff44 14 * limitations under the License
cristea 6:4f2aaa06ff44 15 */
cristea 6:4f2aaa06ff44 16
aleksanb 0:449ee9595cf6 17 #include "MPU6050.h"
aleksanb 2:b9b42ff80e9a 18 #include <math.h>
aleksanb 0:449ee9595cf6 19
sigveseb 3:6a7310ea51f7 20 #include "Puck.h"
sigveseb 3:6a7310ea51f7 21
sigveseb 3:6a7310ea51f7 22 Puck* puck = &Puck::getPuck();
sigveseb 3:6a7310ea51f7 23
sigveseb 3:6a7310ea51f7 24 const UUID CUBE_SERVICE_UUID = UUID(stringToUUID("bftj cube "));
sigveseb 3:6a7310ea51f7 25 const UUID DIRECTION_UUID = UUID(stringToUUID("bftj cube dirctn"));
aleksanb 0:449ee9595cf6 26
aleksanb 0:449ee9595cf6 27 enum Direction {
aleksanb 0:449ee9595cf6 28 UP,
aleksanb 0:449ee9595cf6 29 DOWN,
aleksanb 0:449ee9595cf6 30 LEFT,
aleksanb 0:449ee9595cf6 31 RIGHT,
aleksanb 0:449ee9595cf6 32 FRONT,
aleksanb 0:449ee9595cf6 33 BACK,
aleksanb 0:449ee9595cf6 34 UNDEFINED
aleksanb 0:449ee9595cf6 35 };
aleksanb 0:449ee9595cf6 36
aleksanb 0:449ee9595cf6 37 const static int16_t ACCELERATION_EXITATION_THRESHOLD = 15000;
aleksanb 0:449ee9595cf6 38
aleksanb 0:449ee9595cf6 39 MPU6050 mpu;
aleksanb 0:449ee9595cf6 40
aleksanb 0:449ee9595cf6 41 int16_t ax, ay, az;
aleksanb 0:449ee9595cf6 42 int16_t gx, gy, gz;
aleksanb 0:449ee9595cf6 43
aleksanb 0:449ee9595cf6 44 Direction direction = UNDEFINED;
aleksanb 0:449ee9595cf6 45
sigveseb 3:6a7310ea51f7 46 void log_direction(Direction direction) {
aleksanb 2:b9b42ff80e9a 47 switch(direction) {
sigveseb 3:6a7310ea51f7 48 case UP: LOG_INFO("Direction UP\n"); break;
sigveseb 3:6a7310ea51f7 49 case DOWN: LOG_INFO("Direction DOWN\n"); break;
sigveseb 3:6a7310ea51f7 50 case LEFT: LOG_INFO("Direction LEFT\n"); break;
sigveseb 3:6a7310ea51f7 51 case RIGHT: LOG_INFO("Direction RIGHT\n"); break;
sigveseb 3:6a7310ea51f7 52 case BACK: LOG_INFO("Direction BACK\n"); break;
sigveseb 3:6a7310ea51f7 53 case FRONT: LOG_INFO("Direction FRONT\n"); break;
sigveseb 3:6a7310ea51f7 54 default: LOG_INFO("Direction UNSET\n"); break;
aleksanb 0:449ee9595cf6 55 }
aleksanb 0:449ee9595cf6 56 }
aleksanb 0:449ee9595cf6 57
sigveseb 3:6a7310ea51f7 58 int16_t direction_if_exited(int16_t acceleration) {
aleksanb 0:449ee9595cf6 59 if (acceleration > ACCELERATION_EXITATION_THRESHOLD) {
aleksanb 0:449ee9595cf6 60 return 1;
sigveseb 3:6a7310ea51f7 61 }
sigveseb 3:6a7310ea51f7 62 if (acceleration < -ACCELERATION_EXITATION_THRESHOLD) {
aleksanb 0:449ee9595cf6 63 return -1;
aleksanb 0:449ee9595cf6 64 }
sigveseb 3:6a7310ea51f7 65 return 0;
aleksanb 0:449ee9595cf6 66 }
aleksanb 0:449ee9595cf6 67
sigveseb 3:6a7310ea51f7 68 void updateCubeDirection(void) {
aleksanb 0:449ee9595cf6 69 mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
aleksanb 2:b9b42ff80e9a 70
aleksanb 0:449ee9595cf6 71 int16_t x = direction_if_exited(ax);
aleksanb 0:449ee9595cf6 72 int16_t y = direction_if_exited(ay);
aleksanb 0:449ee9595cf6 73 int16_t z = direction_if_exited(az);
aleksanb 2:b9b42ff80e9a 74
aleksanb 0:449ee9595cf6 75 int16_t sum = abs(x) + abs(y) + abs(z);
aleksanb 2:b9b42ff80e9a 76 if (sum != 1) {
aleksanb 0:449ee9595cf6 77 return;
aleksanb 0:449ee9595cf6 78 }
aleksanb 2:b9b42ff80e9a 79
sigveseb 3:6a7310ea51f7 80 Direction new_direction = UNDEFINED;
aleksanb 0:449ee9595cf6 81 if (z == 1) {
aleksanb 2:b9b42ff80e9a 82 new_direction = UP;
aleksanb 0:449ee9595cf6 83 } else if (z == -1) {
aleksanb 2:b9b42ff80e9a 84 new_direction = DOWN;
aleksanb 0:449ee9595cf6 85 } else if (y == 1) {
aleksanb 2:b9b42ff80e9a 86 new_direction = LEFT;
aleksanb 0:449ee9595cf6 87 } else if (y == -1) {
aleksanb 2:b9b42ff80e9a 88 new_direction = RIGHT;
aleksanb 0:449ee9595cf6 89 } else if (x == 1) {
aleksanb 2:b9b42ff80e9a 90 new_direction = BACK;
aleksanb 0:449ee9595cf6 91 } else if (x == -1) {
aleksanb 2:b9b42ff80e9a 92 new_direction = FRONT;
aleksanb 0:449ee9595cf6 93 }
aleksanb 2:b9b42ff80e9a 94
aleksanb 2:b9b42ff80e9a 95 if (direction == new_direction) {
aleksanb 2:b9b42ff80e9a 96 return;
aleksanb 2:b9b42ff80e9a 97 }
aleksanb 2:b9b42ff80e9a 98
aleksanb 2:b9b42ff80e9a 99 direction = new_direction;
aleksanb 2:b9b42ff80e9a 100
sigveseb 3:6a7310ea51f7 101 log_direction(direction);
sigveseb 3:6a7310ea51f7 102 uint8_t directionAsInteger = direction;
sigveseb 4:6a2b306b6b41 103 int length = 1;
sigveseb 4:6a2b306b6b41 104 puck->updateCharacteristicValue(DIRECTION_UUID, &directionAsInteger, length);
aleksanb 2:b9b42ff80e9a 105 }
aleksanb 2:b9b42ff80e9a 106
aleksanb 2:b9b42ff80e9a 107
sigveseb 3:6a7310ea51f7 108 int main() {
sigveseb 3:6a7310ea51f7 109
aleksanb 0:449ee9595cf6 110
sigveseb 3:6a7310ea51f7 111 LOG_VERBOSE("MPU6050 test startup:\n");
aleksanb 0:449ee9595cf6 112
aleksanb 0:449ee9595cf6 113 mpu.initialize();
sigveseb 3:6a7310ea51f7 114 LOG_VERBOSE("TestConnection\n");
aleksanb 2:b9b42ff80e9a 115
aleksanb 2:b9b42ff80e9a 116 if (mpu.testConnection()) {
sigveseb 3:6a7310ea51f7 117 LOG_INFO("MPU initialized.\n");
aleksanb 2:b9b42ff80e9a 118 } else {
sigveseb 3:6a7310ea51f7 119 LOG_ERROR("MPU not properly initialized!\n");
aleksanb 0:449ee9595cf6 120 }
aleksanb 0:449ee9595cf6 121
sigveseb 4:6a2b306b6b41 122 int characteristicValueLength = 1;
sigveseb 3:6a7310ea51f7 123 puck->addCharacteristic(
sigveseb 3:6a7310ea51f7 124 CUBE_SERVICE_UUID,
sigveseb 3:6a7310ea51f7 125 DIRECTION_UUID,
sigveseb 4:6a2b306b6b41 126 characteristicValueLength,
sigveseb 3:6a7310ea51f7 127 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
sigveseb 3:6a7310ea51f7 128
cristea 6:4f2aaa06ff44 129 puck->init(0xC1BE);
sigveseb 4:6a2b306b6b41 130
sigveseb 4:6a2b306b6b41 131
sigveseb 4:6a2b306b6b41 132 Ticker ticker;
cristea 6:4f2aaa06ff44 133 ticker.attach(updateCubeDirection, 0.2);
sigveseb 4:6a2b306b6b41 134 LOG_INFO("Started listening to orientation changes.\n");
sigveseb 3:6a7310ea51f7 135
sigveseb 3:6a7310ea51f7 136 while(puck->drive());
aleksanb 0:449ee9595cf6 137 }