![](/media/cache/profiles/909f269e0b3ffbd61d906f9b25eecaf6.jpg.50x50_q85.jpg)
a hack to use buttons to fake a cube puck
Dependencies: MPU6050 Puck mbed
Fork of cube-puck by
Diff: main.cpp
- Revision:
- 12:a7051fc219b8
- Parent:
- 10:3d708495b7a0
--- a/main.cpp Mon Mar 09 14:12:19 2015 +0000 +++ b/main.cpp Wed Mar 11 17:26:36 2015 +0000 @@ -14,9 +14,6 @@ * limitations under the License */ -#include "MPU6050.h" -#include <math.h> - #include "Puck.h" Puck* puck = &Puck::getPuck(); @@ -24,109 +21,26 @@ const UUID CUBE_SERVICE_UUID = stringToUUID("bftj cube "); const UUID DIRECTION_UUID = stringToUUID("bftj cube dirctn"); -enum Direction { - UP, - DOWN, - LEFT, - RIGHT, - FRONT, - BACK, - UNDEFINED -}; - -const static int16_t ACCELERATION_EXITATION_THRESHOLD = 15000; - -MPU6050 mpu; - -int16_t ax, ay, az; -int16_t gx, gy, gz; - -Direction direction = UNDEFINED; -void log_direction(Direction direction) { - switch(direction) { - 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; - } -} +InterruptIn myButton(P0_16); +InterruptIn button2(P0_17); -int16_t direction_if_exited(int16_t acceleration) { - if (acceleration > ACCELERATION_EXITATION_THRESHOLD) { - return 1; - } - if (acceleration < -ACCELERATION_EXITATION_THRESHOLD) { - return -1; - } - return 0; -} +uint8_t buttonHasBeenPressed = 0; -void updateCubeDirection(void) { - - if(!mpu.testConnection()) { - LOG_ERROR("MPU DIED! Resetting...\n"); - mpu.reset(); - mpu.initialize(); - LOG_ERROR("Reset complete.\n"); - return; - } - - 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 != 1) { - return; - } - - Direction new_direction = UNDEFINED; - if (z == 1) { - new_direction = UP; - } else if (z == -1) { - new_direction = DOWN; - } else if (y == 1) { - new_direction = LEFT; - } else if (y == -1) { - new_direction = RIGHT; - } else if (x == 1) { - new_direction = BACK; - } else if (x == -1) { - new_direction = FRONT; - } - - if (direction == new_direction) { - return; - } - - direction = new_direction; - - log_direction(direction); - uint8_t directionAsInteger = direction; - int length = 1; - puck->updateCharacteristicValue(DIRECTION_UUID, &directionAsInteger, length); +void callback() { + buttonHasBeenPressed = 1; } - +void callback2() { + buttonHasBeenPressed = 2; +} int main() { - - LOG_VERBOSE("MPU6050 test startup:\n"); - - mpu.initialize(); - LOG_VERBOSE("TestConnection\n"); + myButton.rise(&callback); + myButton.enable_irq(); - if (mpu.testConnection()) { - LOG_INFO("MPU initialized.\n"); - } else { - LOG_ERROR("MPU not properly initialized!\n"); - } + button2.rise(&callback2); + button2.enable_irq(); int characteristicValueLength = 1; puck->addCharacteristic( @@ -136,11 +50,15 @@ GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); puck->init(0xC1BE); - - - Ticker ticker; - ticker.attach(updateCubeDirection, 0.2); - LOG_INFO("Started listening to orientation changes.\n"); - while(puck->drive()); + while(puck->drive()) { + if (buttonHasBeenPressed) { + /* do stuff */ + int length = 1; + puck->updateCharacteristicValue(DIRECTION_UUID, &buttonHasBeenPressed, length); + + // Reset + buttonHasBeenPressed = 0; + } + } } \ No newline at end of file