Abraham Marsen / Mbed 2 deprecated Jazz_Hands_Nordic

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "APDS9960.h"
00003 #include <math.h>
00004 #include "Puck.h"
00005 
00006 DigitalOut mplay(P0_0);
00007 DigitalOut mpause(P0_2);
00008 DigitalOut mnext(P0_4);
00009 DigitalOut mrev(P0_1);
00010 DigitalOut mup(P0_3);
00011 DigitalOut mdown(P0_5);
00012 
00013 APDS9960    apds(P0_22, P0_20);                             // FIX THIS LINE
00014 
00015 Puck* puck = &Puck::getPuck();
00016 
00017 const UUID CUBE_SERVICE_UUID = stringToUUID("bftj cube       ");
00018 const UUID DIRECTION_UUID = stringToUUID("bftj cube dirctn");
00019 
00020 enum Direction {
00021     UP,
00022     DOWN,
00023     LEFT,
00024     RIGHT,
00025     FRONT,
00026     BACK,
00027     UNDEFINED
00028 };
00029 
00030 Direction direction = UNDEFINED;
00031 Direction new_direction = UNDEFINED;
00032 void log_direction(Direction direction)                         // May be unnecessary.
00033 {
00034     switch(direction) {
00035         case UP:
00036             LOG_INFO("Direction UP\n");
00037             mplay=1;
00038             break;
00039         case DOWN:
00040             LOG_INFO("Direction DOWN\n");
00041             mpause=1;
00042             break;
00043         case LEFT:
00044             LOG_INFO("Direction LEFT\n");
00045             mrev=1;
00046             break;
00047         case RIGHT:
00048             LOG_INFO("Direction RIGHT\n");
00049             mnext=1;
00050             break;
00051         case BACK:
00052             LOG_INFO("Direction BACK\n");
00053             mup=1;
00054             break;
00055         case FRONT:
00056             LOG_INFO("Direction FRONT\n");
00057             mdown=1;
00058             break;
00059         default:
00060             LOG_INFO("Direction UNSET\n");
00061             mplay=0;
00062             mpause=0;
00063             mnext=0;
00064             mrev=0;
00065             mup=0;
00066             mdown=0;
00067             break;
00068     }
00069 }
00070 
00071 void log_gesture(void)
00072 {
00073     new_direction = UNDEFINED;
00074     if ( apds.isGestAvailable() ) {
00075         printf("Gesture is Available\n\r");
00076         switch ( apds.readGest() ) {
00077             case DIR_O:
00078                 printf("OUT.\r\n");
00079                 new_direction = UP;
00080                 break;
00081             case DIR_I:
00082                 printf("IN.\n\r");
00083                 new_direction = DOWN;
00084                 break;
00085             case DIR_W:
00086                 printf("West.\n\r");
00087                 new_direction = LEFT;
00088                 break;
00089             case DIR_E:
00090                 printf("East.\n\r");
00091                 new_direction = RIGHT;
00092                 break;
00093             case DIR_N:
00094                 printf("North.\r\n");
00095                 new_direction = FRONT;
00096                 break;
00097             case DIR_S:
00098                 printf("South.\n\r");
00099                 new_direction = BACK;
00100                 break;
00101 
00102             default:
00103                 printf("Waiting on Gesture.\n\r");
00104         }
00105     }
00106     if (direction == new_direction) {
00107         return;
00108     }
00109     direction = new_direction;
00110 
00111     log_direction(direction);
00112     uint8_t directionAsInteger = direction;
00113     int length = 1;
00114     puck->updateCharacteristicValue(DIRECTION_UUID, &directionAsInteger, length);
00115 }
00116 
00117 int main()
00118 {
00119     if(!apds.init()) {
00120         printf("Initialization ERROR.\n\r");
00121     } else {
00122         printf("Initialization Complete.\n\r");
00123     }
00124 
00125     if(apds.enGestSens(true)) {
00126         printf("Gesture Sensor Enabled\r\n");
00127     } else {
00128         printf("Gesture Sensor is not enabled.\r\n");
00129     }
00130 
00131 
00132     int characteristicValueLength = 1;
00133     puck->addCharacteristic(
00134         CUBE_SERVICE_UUID,
00135         DIRECTION_UUID,
00136         characteristicValueLength,
00137         GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
00138 
00139     puck->init(0xC1BE);
00140 
00141 
00142     Ticker ticker;
00143     ticker.attach(log_gesture, 0.2);
00144     LOG_INFO("Started listening to orientation changes.\n");
00145 
00146     while(puck->drive());
00147 }