Demo Team / Mbed 2 deprecated bluefruit

Dependencies:   mbed

main.cpp

Committer:
ShalajLawania
Date:
2014-11-18
Revision:
4:4775701a0d62
Parent:
3:b519acc65986
Child:
5:9af1305f7779

File content as of revision 4:4775701a0d62:

/* 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) along with Mouse movement coordinates and buttons

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

#include "mbed.h"
#include "string.h"
#include "OneButton.h"
#include "TwoButton.h"

void TwoButtons(char l)
{
    t2_stop();

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

void OneButton(char q)
{
    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, *oneup, *onedown, *oneleft, *oneright,
         *twoup, *twodown, *tworight, *twoleft, *onew, *ones, *oned, *onea, *oneenter, *onespace, *twow, *twos, *twod, *twoa, *twospace, *twoenter;
    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
    oneup = strstr(b, "K 0xFBE"); //34 move 34 - 53 have just been accounted to prevent array gaps, play feature can be incorporated
    onedown = strstr(b, "K 0xFBD"); //35
    oneleft = strstr(b, "K 0xFBB"); //36
    oneright = strstr(b, "K 0xFB7"); //37
    onew = strstr(b, "K 0xEBF"); //38
    ones = strstr(b, "K 0xBBF"); //39
    onea = strstr(b, "K 0xDBF"); //40
    oned = strstr(b, "K 0x7BF"); //41
    onespace = strstr(b, "K 0xF9F"); //42
    oneenter = strstr(b, "K 0xFAF"); //43
    twoup = strstr(b, "K 0xF7E"); //44
    twodown = strstr(b, "K 0xF7D"); //45
    tworight = strstr(b, "K 0xF77"); //46
    twoleft = strstr(b, "K 0xF7B"); //47
    twow = strstr(b, "K 0xE7F"); //48
    twos = strstr(b, "K 0xB7F"); //49
    twoa = strstr(b, "K 0xD7F"); //50
    twod = strstr(b, "K 0x77F"); //51
    twoenter = strstr(b, "K 0xF6F"); //52
    twospace = strstr(b, "K 0xF5F"); //53
    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");
                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) {
        setup();
        if(twobuttons == 0) {
            t.start();
        }
        playI.rise(&playfunction);
        if (bluefruit.readable()) {
            bluefruit.gets(buf, 9);
            //pc.puts(buf);
            comparison(buf);

        }
    }
}