Demo Team / Mbed 2 deprecated bluefruit

Dependencies:   mbed

main.cpp

Committer:
ShalajLawania
Date:
2014-09-18
Revision:
1:266895757ab3
Parent:
0:4959c491059f
Child:
2:98ab6e6dd8aa

File content as of revision 1:266895757ab3:

/* This Bluetooth Remote uses a Bluefruit E-Z Key

The default switch-key mapping for Bluefruit E-Z Key is
#0 - Up Arrow
#1 - Down Arrow
#2 - Left Arrow
#3 - Right Arrow
#4 - Return
#5 - Space
#6 - the number '1'
#7 - the number '2'
#8 - lowercase 'w'
#9 - lowercase 'a'
#10 - lowercase 's'
#11 - lowercase 'd'

These however can be remapped to include any keys the user needs (Shift, Esc, Backspace, VolumeUp, Play/Pause)

This Remote records all user input after one input has been entered.
 */

#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(p12);
DigitalOut aswitch(p11);
DigitalOut dswitch(p8);
DigitalOut oneswitch(p13);
DigitalOut twoswitch(p14 );
Serial bluefruit(p9, p10);
Serial pc(USBTX, USBRX);
InterruptIn playI(p25);
Timer t, t2,t3;

char buf [9];
char moves[400]; //change array size to increase recorded moves
unsigned long long times[400]; //change array size to increase recorded moves
unsigned long long times2[400]; //change array size to increase recorded moves
unsigned int i, j, k, z = 0;
unsigned long long reader = 0;
unsigned int onebutton, twobuttons = 0;
unsigned int subtractor;

void t2_stop()
{
    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++;
}

void t3_stop()
{
    t3.stop();
    reader = t3.read_ms();
    pc.printf("\n\r Time Taken %d", reader);
    pc.printf("\n");
    t3.reset();
}

void twobutton_handler()
{
    t2.stop();
    twobuttons = 0;
    times2[j] = t2.read_ms();
    subtractor = times2[j];
    pc.printf("\n\r Double Key Press Time: %d \n", times2[j]);
    j++;
}

void onebutton_handler(char m)
{
    onebutton = 1;
    t2.reset();
    t2.start();
    moves[i] = m;
    switch(m) {
        case 1:
            pc.printf("\n\r Move: Up");
            break;
        case 2:
            pc.printf("\n\r Move: Down");
            break;
        case 3:
            pc.printf("\n\r Move: Left");
            break;
        case 4:
            pc.printf("\n\r Move: Right");
            break;
        case 11:
            pc.printf("\n\r Move: w");
            break;
        case 12:
            pc.printf("\n\r Move: s");
            break;
        case 13:
            pc.printf("\n\r Move: a");
            break;
        case 14:
            pc.printf("\n\r Move: d");
            break;
        case 15:
            pc.printf("\n\r Move: Space");
            break;
        case 16:
            pc.printf("\n\r Move: Enter");
            break;
        case 17:
            pc.printf("\n\r Move: One");
            break;
        case 18:
            pc.printf("\n\r Move: Two");
            break;

        default:
            break;
    }
    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++;
}

void twobutton_instant(char n)
{
    twobuttons = 1;
    t2.start();
    moves[i] = n;
    switch(n) {
        case 5:
            pc.printf("\n\r Move: Up + Right Instant");
            break;
        case 6:
            pc.printf("\n\r Move: Up + Down Instant");
            break;
        case 7:
            pc.printf("\n\r Move: Up + Left Instant");
            break;
        case 19:
            pc.printf("\n\r Move: w + a Instant");
            break;
        case 20:
            pc.printf("\n\r Move: w + d Instant");
            break;
        case 21:
            pc.printf("\n\r Move: s + a Instant");
            break;
        case 22:
            pc.printf("\n\r Move: s + d Instant");
            break;
        case 23:
            pc.printf("\n\r Move: w + s Instant");
            break;
        case 24:
            pc.printf("\n\r Move: a + d Instant");
            break;
        case 25:
            pc.printf("\n\r Move: One + Two Instant");
            break;
        case 26:
            pc.printf("\n\r Move: Up + Space Instant");
            break;
        case 27:
            pc.printf("\n\r Move: Right + Space Instant");
            break;
        case 28:
            pc.printf("\n\r Move: Left + Space Instant");
            break;
        case 29:
            pc.printf("\n\r Move: Down + Space Instant");
            break;
        case 30:
            pc.printf("\n\r Move: Up + Enter Instant");
            break;
        case 31:
            pc.printf("\n\r Move: Down + Enter Instant");
            break;
        case 32:
            pc.printf("\n\r Move: Right + Enter Instant");
            break;
        case 33:
            pc.printf("\n\r Move: Left + Enter Instant");
        default:
            break;
    }
    pc.printf("\n\r Position: %d", i);
    i++;
}

void twobutton(char p)
{
    onebutton = 0;
    twobuttons = 1;
    t2.start();
    moves[i] = p;
    switch(p) {
        case 5:
            pc.printf("\n\r Move: Up + Right");
            break;
        case 6:
            pc.printf("\n\r Move:  Up + Down");
            break;
        case 7:
            pc.printf("\n\r Move: Up + Left");
            break;
        case 8:
            pc.printf("\n\r Move: Down + Right");
            break;
        case 9:
            pc.printf("\n\r Move: Down + Left");
            break;
        case 10:
            pc.printf("\n\r Move: Right + Left");
            break;
        case 19:
            pc.printf("\n\r Move: w + a");
            break;
        case 20:
            pc.printf("\n\r Move: w + d");
            break;
        case 21:
            pc.printf("\n\r Move: s + a");
            break;
        case 22:
            pc.printf("\n\r Move: s + d");
            break;
        case 23:
            pc.printf("\n\r Move: w + s");
            break;
        case 24:
            pc.printf("\n\r Move: a + d");
            break;
        case 25:
            pc.printf("\n\r Move: One + Two");
            break;
        case 26:
            pc.printf("\n\r Move: Up + Space");
            break;
        case 27:
            pc.printf("\n\r Move: Right + Space");
            break;
        case 28:
            pc.printf("\n\r Move: Left + Space");
            break;
        case 29:
            pc.printf("\n\r Move: Down + Space");
            break;
        case 30:
            pc.printf("\n\r Move: Up + Enter");
            break;
        case 31:
            pc.printf("\n\r Move: Down + Enter");
            break;
        case 32:
            pc.printf("\n\r Move: Right + Enter");
            break;
        case 33:
            pc.printf("\n\r Move: Left + Enter");
            break;
        default:
            break;
    }
    pc.printf("\n\r Position: %d", i);
    i++;
}

void TwoButtons(char l)
{
    t2_stop();

    if(onebutton == 0) {
        twobutton_instant(l);
    } else {
        twobutton(l);
    }
}

void OneButton(char q)
{
    subtractor = 0;
    if (twobuttons) {
        twobutton_handler();
    }
    onebutton_handler(q);
}

void comparison(char *b)
{
    char *up, *down, *left, *right, *nobutton, *upright, *updown, *upleft, *rightleft, *downright, *downleft, *a, *w, *s, *d, *enter, *space, *one, *two, *wa,
         *wd, *sa, *sd, *ws, *ad, *onetwo, *upspace, *rightspace, *downspace, *leftspace, *upenter, *downenter, *rightenter, *leftenter;
    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
    w = strstr(b, "K 0xEFF"); //11
    s = strstr(b, "K 0xBFF"); //12
    a = strstr(b, "K 0xDFF"); //13
    d = strstr(b, "K 0x7FF"); //14
    space = strstr(b, "K 0xFDF"); //15
    enter = strstr(b, "K 0xFEF"); //16
    one = strstr(b, "K 0xFBF"); //17
    two = strstr(b, "K 0xF7F"); //18
    wa = strstr(b, "K 0xCFF"); //19
    wd = strstr(b, "K 0x6FF"); //20
    sa = strstr(b, "K 0x9FF"); //21
    sd = strstr(b, "K 0x3FF"); //22
    ws = strstr(b, "K 0xAFF"); //23
    ad = strstr(b, "K 0x5FF"); //24
    onetwo = strstr(b, "K 0xF3F"); //25
    upspace = strstr(b, "K 0xFDE"); //26
    rightspace = strstr(b, "K 0xFD7"); //27
    leftspace = strstr(b, "K 0xFDB"); //28
    downspace = strstr(b, "K 0xFDD"); //29
    upenter = strstr(b, "K 0xFEE"); //30
    downenter = strstr(b, "K 0xFED"); //31
    rightenter = strstr(b, "K 0xFE7"); //32
    leftenter = strstr(b, "K 0xFEB"); //33

    nobutton = strstr(b, "K 0xFFF"); //

    if (strcmp(up, "K 0xFFE") == 0) {   // 1
        OneButton(1);
    }
    if (strcmp(down, "K 0xFFD") == 0) {  // 2
        OneButton(2);
    }
    if (strcmp(left, "K 0xFFB") == 0) { // 3
        OneButton(3);
    }
    if (strcmp(right, "K 0xFF7") == 0) { // 4
        OneButton(4);
    }
    if (strcmp(upright, "K 0xFF6") == 0) { // 5
        TwoButtons(5);
    }
    if (strcmp(updown, "K 0xFFC") == 0) { // 6
        TwoButtons(6);
    }
    if (strcmp(upleft, "K 0xFFA") == 0) { // 7
        TwoButtons(7);
    }
    if (strcmp(downright, "K 0xFF5") == 0) { // 8
        TwoButtons(8);
    }
    if (strcmp(downleft, "K 0xFF9") == 0) { // 9
        TwoButtons(9);
    }
    if (strcmp(rightleft, "K 0xFF3") == 0) { //10
        TwoButtons(10);
    }
    if (strcmp(w, "K 0xEFF") == 0) { // 11
        OneButton(11);
    }
    if (strcmp(s, "K 0xBFF") == 0) { // 12
        OneButton(12);
    }
    if (strcmp(a, "K 0xDFF") == 0) { // 13
        OneButton(13);
    }
    if (strcmp(d, "K 0x7FF") == 0) { // 14
        OneButton(14);
    }
    if (strcmp(space, "K 0xFDF") == 0) { // 15
        OneButton(15);
    }
    if (strcmp(enter, "K 0xFEF") == 0) { // 16
        OneButton(16);
    }
    if (strcmp(one, "K 0xFBF") == 0) { // 17
        OneButton(17);
    }
    if (strcmp(two, "K 0xF7F") == 0) { // 18
        OneButton(18);
    }
    if (strcmp(wa, "K 0xCFF") == 0) { // 19
        TwoButtons(19);
    }
    if (strcmp(wd, "K 0x6FF") == 0) { // 20
        TwoButtons(20);
    }
    if (strcmp(sa, "K 0x9FF") == 0) { // 21
        TwoButtons(21);
    }
    if (strcmp(sd, "K 0x3FF") == 0) { // 22
        TwoButtons(22);
    }
    if (strcmp(ws, "K 0xAFF") == 0) { // 23
        TwoButtons(23);
    }
    if (strcmp(ad, "K 0x5FF") == 0) { // 24
        TwoButtons(24);
    }
    if (strcmp(onetwo, "K 0xF3F") == 0) { // 25
        TwoButtons(25);
    }
    if (strcmp(upspace, "K 0xFDE") == 0) { // 26
        TwoButtons(26);
    }
    if (strcmp(rightspace, "K 0xFD7") == 0) { // 27
        TwoButtons(27);
    }
    if (strcmp(leftspace, "K 0xFDB") == 0) { // 28
        TwoButtons(28);
    }
    if (strcmp(downspace, "K 0xFDD") == 0) { //29
        TwoButtons(29);
    }
    if (strcmp(upenter, "K 0xFEE") == 0) { // 30
        TwoButtons(30);
    }
    if (strcmp(downenter, "K 0xFED") == 0) { // 31
        TwoButtons(31);
    }
    if (strcmp(rightenter, "K 0xFE7") == 0) { // 32
        TwoButtons(32);
    }
    if (strcmp(leftenter, "K 0xFEB") == 0) { // 33
        TwoButtons(33);
    }
    if (strcmp(nobutton, "K 0xFFF") == 0) {
        if(onebutton) {
            onebutton = 0;
            t2_stop();
        }
        if (twobuttons) {
            twobutton_handler();
            t2.reset();
        }
    }
}

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

            case 11:
                pc.printf("\n\r Move: w");
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    wswitch = 0;
                }
                wswitch = 1;
                t3_stop();
                break;

            case 12:
                pc.printf("\n\r Move: s");
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    sswitch = 0;
                }
                sswitch = 1;
                t3_stop();
                break;

            case 13:
                pc.printf("\n\r Move: a");
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    aswitch = 0;
                }
                aswitch = 1;
                t3_stop();
                break;

            case 14:
                pc.printf("\n\r Move: d");
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    dswitch = 0;
                }
                dswitch = 1;
                t3_stop();
                break;

            case 15:
                pc.printf("\n\r Move: Space");
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    spaceswitch = 0;
                }
                spaceswitch = 1;
                t3_stop();
                break;

            case 16:
                pc.printf("\n\r Move: Enter");
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    enterswitch = 0;
                }
                enterswitch = 1;
                t3_stop();
                break;

            case 17:
                pc.printf("\n\r Move: 1");
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    oneswitch = 0;
                }
                oneswitch = 1;
                t3_stop();
                break;

            case 18:
                pc.printf("\n\r Move: 2");
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    twoswitch = 0;
                }
                twoswitch = 1;
                t3_stop();
                break;

            case 19:
                pc.printf("\n\r Move: w + a");
                pc.printf("\n\r Position: %d", k);
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    wswitch = 0;
                    aswitch = 0;
                }
                wswitch = 1;
                aswitch = 1;
                t3_stop();
                break;

            case 20:
                pc.printf("\n\r Move: w + d");
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    wswitch = 0;
                    dswitch = 0;
                }
                wswitch = 1;
                dswitch = 1;
                t3_stop();
                break;

            case 21:
                pc.printf("\n\r Move: s + a");
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    sswitch = 0;
                    aswitch = 0;
                }
                sswitch = 1;
                aswitch = 1;
                t3_stop();
                break;

            case 22:
                pc.printf("\n\r Move: s + d");
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    sswitch = 0;
                    dswitch = 0;
                }
                sswitch = 1;
                dswitch = 1;
                t3_stop();
                break;

            case 23:
                pc.printf("\n\r Move: w + s");
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    wswitch = 0;
                    sswitch = 0;
                }
                wswitch = 1;
                sswitch = 1;
                t3_stop();
                break;

            case 24:
                pc.printf("\n\r Move: a + d");
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    aswitch = 0;
                    dswitch = 0;
                }
                aswitch = 1;
                dswitch = 1;
                t3_stop();
                break;

            case 25:
                pc.printf("\n\r Move: one + two");
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    oneswitch = 0;
                    twoswitch = 0;
                }
                oneswitch = 1;
                twoswitch = 1;
                t3_stop();
                break;

            case 26:
                pc.printf("\n\r Move: Up + Space");
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    upswitch = 0;
                    spaceswitch = 0;
                }
                upswitch = 1;
                spaceswitch = 1;
                t3_stop();
                break;

            case 27:
                pc.printf("\n\r Move: Right + Space");
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    rightswitch = 0;
                    spaceswitch = 0;
                }
                rightswitch = 1;
                spaceswitch = 1;
                t3_stop();
                break;

            case 28:
                pc.printf("\n\r Move: Space + Left");
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    spaceswitch = 0;
                    leftswitch = 0;
                }
                spaceswitch = 1;
                leftswitch = 1;
                t3_stop();
                break;

            case 29:
                pc.printf("\n\r Move: Down + Space");
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    downswitch = 0;
                    spaceswitch = 0;
                }
                downswitch = 1;
                spaceswitch = 1;
                t3_stop();
                break;

            case 30:
                pc.printf("\n\r Move: Up + Enter");
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    upswitch = 0;
                    enterswitch = 0;
                }
                upswitch = 1;
                enterswitch = 1;
                t3_stop();
                break;

            case 31:
                pc.printf("\n\r Move: Down + Enter");
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    downswitch = 0;
                    enterswitch = 0;
                }
                rightswitch = 1;
                leftswitch = 1;
                t3_stop();
                break;

            case 32:
                pc.printf("\n\r Move: Right + Enter");
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    rightswitch = 0;
                    enterswitch = 0;
                }
                rightswitch = 1;
                enterswitch = 1;
                t3_stop();
                break;

            case 33:
                pc.printf("\n\r Move: Enter + Left");
                t3.start();
                while(t3.read_ms() < times2[k]) {
                    enterswitch = 0;
                    leftswitch = 0;
                }
                rightswitch = 1;
                enterswitch = 1;
                t3_stop();
                break;
            default:
                break;
        }
    }
    pc.printf("\n\r DONE");
}

int main()
{
    pc.printf("\n\r START \n");
    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);

        }
    }
}