Demo Team / Mbed 2 deprecated bluefruit

Dependencies:   mbed

main.cpp

Committer:
ShalajLawania
Date:
2014-09-17
Revision:
0:4959c491059f
Child:
1:266895757ab3

File content as of revision 0:4959c491059f:

#include "mbed.h"
#include "string.h"

DigitalOut upswitch (p30);
DigitalOut downswitch (p29);
DigitalOut rightswitch (p28);
DigitalOut leftswitch (p27);
DigitalOut enterswitch(p5);
DigitalOut spaceswitch(p6);
DigitalOut wswitch(p7);
DigitalOut sswitch(p8);
DigitalOut aswitch(p9);
DigitalOut dswitch(p10);
DigitalOut oneswitch(p11);
DigitalOut twoswitch(p12);
DigitalInOut upreader(p21);
DigitalOut myled1 (LED1);
DigitalOut myled2 (LED2);
Serial bluefruit(p9, p10);
Serial pc(USBTX, USBRX);
InterruptIn playI(p25);
Timer t;
Timer t2;
Timer t3;
//Timer t4;

char buf [9];
char moves[200];
unsigned long long times[200];
unsigned long long times2[200];
//unsigned long long times4[200];
unsigned int i = 0;
unsigned int j = 0;
unsigned int z = 0;
unsigned int k;
unsigned long long reader = 0;
unsigned int off = 1;
unsigned int twobuttons = 0;
unsigned int onebutton = 0;
unsigned int y = 0;

void comparison(char *b)
{
    char *up, *down, *left, *right, *nobutton, *upright, *updown, *upleft, *rightleft, *downright, *downleft;
    unsigned int subtractor;
    up = strstr(b, "K 0xFFE"); // 1
    down = strstr(b, "K 0xFFD"); // 2
    left = strstr(b, "K 0xFFB"); // 3
    right = strstr(b, "K 0xFF7"); // 4
    upright = strstr(b, "K 0xFF6"); // 5
    updown = strstr(b, "K 0xFFC"); // 6
    upleft = strstr(b, "K 0xFFA"); // 7
    downright = strstr(b, "K 0xFF5"); // 8
    downleft = strstr(b, "K 0xFF9"); // 9
    rightleft = strstr(b, "K 0xFF3"); //10
    nobutton = strstr(b, "K 0xFFF"); //16

    if (strcmp(up, "K 0xFFE") == 0) {
        subtractor = 0; 
        if (twobuttons) {
            t2.stop();
            twobuttons = 0;
            times2[j] = t2.read_ms();
            subtractor = times2[j];
            pc.printf("\n\r Time stored in array: %d \n", times2[j]);
            j++;
        }
        onebutton = 1;
        t2.reset();
        t2.start();
        moves[i] = 1;
        pc.printf("\n\r Move: Up");
        pc.printf("\n\r Position: %d", i);
        t.stop();
        times[i] = t.read_ms()-subtractor;
        pc.printf("\n\r Gap Time: %d", times[i]);
        t.reset();
        i++;
    }
    if (strcmp(down, "K 0xFFD") == 0) {
        subtractor = 0; 
        if (twobuttons) {
            t2.stop();
            twobuttons = 0;
            times2[j] = t2.read_ms();
            subtractor = times2[j];
            pc.printf("\n\r Time stored in array: %d \n", times2[j]);
            j++;
        }
        onebutton = 1;
        t2.reset();
        t2.start();
        moves[i] = 2;
        pc.printf("\n\r Move: Down");
        pc.printf("\n\r Position: %d", i);

        t.stop();
        times[i] = t.read_ms()-subtractor;
        pc.printf("\n\r Gap Time: %d", times[i]);
        t.reset();
        i++;
    }

    if (strcmp(left, "K 0xFFB") == 0) {
        subtractor = 0; 

        if (twobuttons) {
            t2.stop();
            twobuttons = 0;
            times2[j] = t2.read_ms();
            subtractor = times2[j];
            pc.printf("\n\r Time stored in array: %d \n", times2[j]);
            j++;
        }
        onebutton = 1;
        t2.reset();
        t2.start();
        moves[i] = 3;
        pc.printf("\n\r Move:  Left");
        pc.printf("\n\r Position: %d", i);

        t.stop();
        times[i] = t.read_ms()-subtractor;
        pc.printf("\n\r Gap Time: %d", times[i]);
        t.reset();
        i++;
    }
    if (strcmp(right, "K 0xFF7") == 0) {
        subtractor = 0; 
        if (twobuttons) {
            t2.stop();
            twobuttons = 0;
            times2[j] = t2.read_ms();
            subtractor = times2[j];
            pc.printf("\n\r Time stored in array: %d \n", times2[j]);
            j++;
        }
        onebutton = 1;
        t2.reset();
        t2.start();
        moves[i] = 4;
        pc.printf("\n\r Move: Right");
        pc.printf("\n\r Position: %d", i);

        t.stop();
        times[i] = t.read_ms()-subtractor;
        pc.printf("\n\r Gap Time: %d", times[i]);
        t.reset();
        i++;
    }
    if (strcmp(upright, "K 0xFF6") == 0) {
        t2.stop();
        if(t2.read_ms()>0) {
        times2[j] = t2.read_ms();
        pc.printf("\n\r Press Time: %d", times2[j]);
        pc.printf("\n");
        }
        t2.reset();
        j++;
        
        if(onebutton == 0) {
            twobuttons = 1;
            t2.start();
            moves[i] = 5;
            pc.printf("\n\r Move: Up + Right Instant");
            pc.printf("\n\r Position: %d", i);
            i++;
            }
        else {
        onebutton = 0;
        twobuttons = 1;
        t2.start();
        moves[i] = 5;
        pc.printf("\n\r Move: Up + Right");
        pc.printf("\n\r Position: %d", i);
        i++;
        }
    }
    if (strcmp(updown, "K 0xFFC") == 0) {
        t2.stop();
        if(t2.read_ms()>0) {
        times2[j] = t2.read_ms();
        pc.printf("\n\r Press Time: %d", times2[j]);
        pc.printf("\n");
        }
        t2.reset();
        j++;
        
        if(onebutton == 0) {
            twobuttons = 1;
            t2.start();
            moves[i] = 6;
            pc.printf("\n\r Move: Up + Down Instant");
            pc.printf("\n\r Position: %d", i);
            i++;
            }
        else {
        
        onebutton = 0;
        twobuttons = 1;
        t2.start();
        moves[i] = 6;
        pc.printf("\n\r Move:  Up + Down");
        pc.printf("\n\r Position: %d", i);
        i++;
        }
    }
    if (strcmp(upleft, "K 0xFFA") == 0) {
        t2.stop();
        if(t2.read_ms()>0) {
        times2[j] = t2.read_ms();
        pc.printf("\n\r Press Time: %d", times2[j]);
        pc.printf("\n");
        }
        t2.reset();
        j++;
        
        if(onebutton == 0) {
            twobuttons = 1;
            t2.start();
            moves[i] = 7;
            pc.printf("\n\r Move: Up + Left Instant");
            pc.printf("\n\r Position: %d", i);
            i++;
            }
        else {
        onebutton = 0;
        twobuttons = 1;
        t2.start();
        moves[i] = 7;
        pc.printf("\n\r Move: Up + Left");
        pc.printf("\n\r Position: %d", i);
        i++;
        }
    }
    
    if (strcmp(downright, "K 0xFF5") == 0) {
        t2.stop();
        times2[j] = t2.read_ms();
        pc.printf("\n\r Press Time: %d", times2[j]);
        pc.printf("\n");
        t2.reset();
        j++;
        
        if(onebutton == 0) {
            twobuttons = 1;
            t2.start();
            moves[i] = 8;
            pc.printf("\n\r Move: Down + Right Instant");
            pc.printf("\n\r Position: %d", i);
            i++;
            }
        else {
        
        onebutton = 0;
        twobuttons = 1;
        t2.start();
        moves[i] = 8;
        pc.printf("\n\r Move: Down + Right");
        pc.printf("\n\r Position: %d", i);
        i++;
        }
    }
    
    if (strcmp(downleft, "K 0xFF9") == 0) {
        t2.stop();
        times2[j] = t2.read_ms();
        pc.printf("\n\r Press Time: %d", times2[j]);
        pc.printf("\n");
        t2.reset();
        j++;
        
        if(onebutton == 0) {
            twobuttons = 1;
            t2.start();
            moves[i] = 9;
            pc.printf("\n\r Move: Down + Left Instant");
            pc.printf("\n\r Position: %d", i);
            i++;
            }
        else {
        onebutton = 0;
        twobuttons = 1;
        t2.start();
        moves[i] = 9;
        pc.printf("\n\r Move: Down + Left");
        pc.printf("\n\r Position: %d", i);
        i++;
        }
    }
    
    if (strcmp(rightleft, "K 0xFF3") == 0) {
        t2.stop();
        times2[j] = t2.read_ms();
        pc.printf("\n\r Press Time: %d", times2[j]);
        pc.printf("\n");
        t2.reset();
        j++;
        if(onebutton == 0) {
            twobuttons = 1;
            t2.start();
            moves[i] = 10;
            pc.printf("\n\r Move: Left + Right Instant");
            pc.printf("\n\r Position: %d", i);
            i++;
            }
        else {
        
        onebutton = 0;
        twobuttons = 1;
        t2.start();
        moves[i] = 10;
        pc.printf("\n\r Move: Right + Left");
        pc.printf("\n\r Position: %d", i);
        i++;
        }
    }
    if (strcmp(nobutton, "K 0xFFF") == 0) {
  
        if(onebutton){
            onebutton = 0;
        t2.stop();
        if(t2.read_ms()>0) {
        times2[j] = t2.read_ms();
        pc.printf("\n\r Press Time: %d", times2[j]);
        pc.printf("\n");
        t2.reset();
        j++;
        }
        }
        if (twobuttons) {
        twobuttons = 0;
        t2.stop();
        times2[j] = t2.read_ms(); 
        pc.printf("\n\r Time stored in array: %d \n", times2[j]);
        t2.reset();
        j++;
        }
    }
}

void playfunction()
{
    t.stop();
    t.reset();
    pc.printf("\n\r PLAY \n");
   /* for (int k = 0; k < j; k++) {
        pc.printf("\n\r Time %d", k);
        pc.printf(" %d", times2[k]);
        } */
    for (int k = 0; k < j; k++) {
        if (times2[k] == 0) {
            for (int z = k; z < j; z++) {
            times2[z] = times2[z+1];
            }
            j--;
            }
        }
            
    for(int k = 0; k < i; k++) {
        wait_ms(times[k]);
        pc.printf("\n\r Wait Time: %d",times[k]);
        switch (moves[k]) {
            case 1:
                t3.start();
                pc.printf("\n\r Move: Up");
                pc.printf("\n\r Position: %d", k);
                while(t3.read_ms() < times2[k]) {
                    upswitch = 0;
                }
                upswitch = 1;
                t3.stop();
                reader = t3.read_ms();
                pc.printf("\n\r Time Taken %d", reader);
                pc.printf("\n");
                t3.reset();
                break;
            case 2:
                pc.printf("\n\r Move: Down");
                pc.printf("\n\r Position: %d", k);
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    downswitch = 0;
                }
                downswitch = 1;
                t3.stop();
                reader = t3.read_ms();
                pc.printf("\n\r Time Taken %d", reader);
                pc.printf("\n");
                t3.reset();
                break;
            case 3:
                pc.printf("\n\r Move: Left");
                pc.printf("\n\r Position: %d", k);
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    leftswitch = 0;
                }
                leftswitch = 1;
                t3.stop();
                reader = t3.read_ms();
                pc.printf("\n\r Time Taken %d", reader);
                pc.printf("\n");
                t3.reset();
                break;
            case 4:
                pc.printf("\n\r Move: Right");
                pc.printf("\n\r Position: %d", k);
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    rightswitch = 0;
                }
                rightswitch = 1;
                t3.stop();
                reader = t3.read_ms();
                pc.printf("\n\r Time Taken %d", reader);
                pc.printf("\n");
                t3.reset();
                break;
            case 5:
                pc.printf("\n\r Move: Up + Right");
                pc.printf("\n\r Position: %d", k);
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    upswitch = 0;
                    rightswitch = 0;
                }
                upswitch = 1;
                rightswitch = 1;
                t3.stop();
                reader = t3.read_ms();
                pc.printf("\n\r Time Taken %d", reader);
                pc.printf("\n");
                t3.reset();
                y++;
                break;
            case 6:
                pc.printf("\n\r Move: Up + Down");
                pc.printf("\n\r Position: %d", k);
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    upswitch = 0;
                    downswitch = 0;
                }
                upswitch = 1;
                downswitch = 1;
                t3.stop();
                reader = t3.read_ms();
                pc.printf("\n\r Time Taken %d", reader);
                pc.printf("\n");
                t3.reset();
                y++;
                break;
            case 7:
                pc.printf("\n\r Move: Up + Left");
                pc.printf("\n\r Position: %d", k);
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    upswitch = 0;
                    leftswitch = 0;
                }
                upswitch = 1;
                leftswitch = 1;
                t3.stop();
                reader = t3.read_ms();
                pc.printf("\n\r Time Taken %d", reader);
                pc.printf("\n");
                t3.reset();
                y++;
                break;
            case 8:
                pc.printf("\n\r Move: Down + Right");
                pc.printf("\n\r Position: %d", k);
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    downswitch = 0;
                    rightswitch = 0;
                }
                downswitch = 1;
                rightswitch = 1;
                t3.stop();
                reader = t3.read_ms();
                pc.printf("\n\r Time Taken %d", reader);
                pc.printf("\n");
                t3.reset();
                y++;
                break;
            case 9:
                pc.printf("\n\r Move: Down + Left");
                pc.printf("\n\r Position: %d", k);
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    downswitch = 0;
                    leftswitch = 0;
                }
                downswitch = 1;
                leftswitch = 1;
                t3.stop();
                reader = t3.read_ms();
                pc.printf("\n\r Time Taken %d", reader);
                pc.printf("\n");
                t3.reset();
                y++;
                break;
            case 10:
                pc.printf("\n\r Move: Right + Left");
                pc.printf("\n\r Position: %d", k);
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    rightswitch = 0;
                    leftswitch = 0;
                }
                rightswitch = 1;
                leftswitch = 1;
                t3.stop();
                reader = t3.read_ms();
                pc.printf("\n\r Time Taken %d", reader);
                pc.printf("\n");
                t3.reset();
                y++;
                break;

            default:
                break;
        }
    }
    pc.printf("\n\r DONE");
}

int main()
{
    pc.printf("\n\r START");
    pc.printf("\n");
    //bluefruit.gets("K 0xFFF", 9);
    while(1) {

        upswitch = 1;
        downswitch = 1;
        leftswitch = 1;
        rightswitch = 1;
        wswitch = 1;
        sswitch = 1;
        aswitch = 1;
        dswitch = 1;
        enterswitch = 1;
        spaceswitch = 1;
        oneswitch = 1;
        twoswitch = 1;
        if(twobuttons == 0) {
        t.start();
        }
        playI.rise(&playfunction);
        if (bluefruit.readable()) {
            bluefruit.gets(buf, 9);
            //pc.puts(buf);
            comparison(buf);
        }
    }
}