Demo Team / Mbed 2 deprecated bluefruit

Dependencies:   mbed

Controller.h

Committer:
ShalajLawania
Date:
2015-04-16
Revision:
6:ee7485ea71c9
Parent:
5:9af1305f7779
Child:
8:cb50a25a7f8a

File content as of revision 6:ee7485ea71c9:

/*****************************************
* Bluetooth Remote control methods library
* Author: Shalaj Lawania.
*****************************************/

#include "mbed.h"

/**********************************************************
* LocalFileSystem local declared to save test sequence in a 
* .txt file which can be read by the play() function
**********************************************************/

LocalFileSystem local("local");

int flag = 0;
int s = 0;

/************************************************************
* string_comparator(): Compares the received string from
* Bluefruit E-Z Key to a set of preset substrings that each
* contain a sequence unique to a key or set of keys. A unique 
* value is sent to one_button() or two_buttons() for each key 
* or combination of keys
************************************************************/

void string_comparator(char *b)
{
    if (strcmp(strstr(b, "K 0xFFE"), "K 0xFFE") == 0) {  // 1
        one_button(1);
    }
    if (strcmp(strstr(b, "K 0xFFD"), "K 0xFFD") == 0) {  // 2
        one_button(2);
    }
    if (strcmp(strstr(b, "K 0xFFB"), "K 0xFFB") == 0) { // 3
        one_button(3);
    }
    if (strcmp(strstr(b, "K 0xFF7"), "K 0xFF7") == 0) { // 4
        one_button(4);
    }
    if (strcmp(strstr(b, "K 0xFF6"), "K 0xFF6") == 0) { // 5
        two_buttons(5);
    }
    if (strcmp(strstr(b, "K 0xFFC"), "K 0xFFC") == 0) { // 6
        two_buttons(6);
    }
    if (strcmp(strstr(b, "K 0xFFA"), "K 0xFFA") == 0) { // 7
        two_buttons(7);
    }
    if (strcmp(strstr(b, "K 0xFF5"), "K 0xFF5") == 0) { // 8
        two_buttons(8);
    }
    if (strcmp(strstr(b, "K 0xFF9"), "K 0xFF9") == 0) { // 9
        two_buttons(9);
    }
    if (strcmp(strstr(b, "K 0xFF3"), "K 0xFF3") == 0) { //10
        two_buttons(10);
    }
    if (strcmp(strstr(b, "K 0xEFF"), "K 0xEFF") == 0) { // 11
        one_button(11);
    }
    if (strcmp(strstr(b, "K 0xBFF"), "K 0xBFF") == 0) { // 12
        one_button(12);
    }
    if (strcmp(strstr(b, "K 0xDFF"), "K 0xDFF") == 0) { // 13
        one_button(13);
    }
    if (strcmp(strstr(b, "K 0x7FF"), "K 0x7FF") == 0) { // 14
        one_button(14);
    }
    if (strcmp(strstr(b, "K 0xFDF"), "K 0xFDF") == 0) { // 15
        one_button(15);
    }
    if (strcmp(strstr(b, "K 0xFEF"), "K 0xFEF") == 0) { // 16
        one_button(16);
    }
    if (strcmp(strstr(b, "K 0xFBF"), "K 0xFBF") == 0) { // 17
        one_button(17);
    }
    if (strcmp(strstr(b, "K 0xF7F"), "K 0xF7F") == 0) { // 18
        one_button(18);
    }
    if (strcmp(strstr(b, "K 0xCFF"), "K 0xCFF") == 0) { // 19
        two_buttons(19);
    }
    if (strcmp(strstr(b, "K 0x6FF"), "K 0x6FF") == 0) { // 20
        two_buttons(20);
    }
    if (strcmp(strstr(b, "K 0x9FF"), "K 0x9FF") == 0) { // 21
        two_buttons(21);
    }
    if (strcmp(strstr(b, "K 0x3FF"), "K 0x3FF") == 0) { // 22
        two_buttons(22);
    }
    if (strcmp(strstr(b, "K 0xAFF"), "K 0xAFF") == 0) { // 23
        two_buttons(23);
    }
    if (strcmp(strstr(b, "K 0x5FF"), "K 0x5FF") == 0) { // 24
        two_buttons(24);
    }
    if (strcmp(strstr(b, "K 0xF3F"), "K 0xF3F") == 0) { // 25
        two_buttons(25);
    }
    if (strcmp(strstr(b, "K 0xFDE"), "K 0xFDE") == 0) { // 26
        two_buttons(26);
    }
    if (strcmp(strstr(b, "K 0xFD7"), "K 0xFD7") == 0) { // 27
        two_buttons(27);
    }
    if (strcmp(strstr(b, "K 0xFDB"), "K 0xFDB") == 0) { // 28
        two_buttons(28);
    }
    if (strcmp(strstr(b, "K 0xFDD"), "K 0xFDD") == 0) { //29
        two_buttons(29);
    }
    if (strcmp(strstr(b, "K 0xFEE"), "K 0xFEE") == 0) { // 30
        two_buttons(30);
    }
    if (strcmp(strstr(b, "K 0xFED"), "K 0xFED") == 0) { // 31
        two_buttons(31);
    }
    if (strcmp(strstr(b, "K 0xFE7"), "K 0xFE7") == 0) { // 32
        two_buttons(32);
    }
    if (strcmp(strstr(b, "K 0xFEB"), "K 0xFEB") == 0) { // 33
        two_buttons(33);
    }
    if (strcmp(strstr(b, "K 0xFBE"), "K 0xFBE") == 0) { // 34
        two_buttons(34);
    }
    if (strcmp(strstr(b, "K 0xFBD"), "K 0xFBD") == 0) { // 35
        two_buttons(35);
    }
    if (strcmp(strstr(b, "K 0xFBB"), "K 0xFBB") == 0) { // 36
        two_buttons(36);
    }
    if (strcmp(strstr(b, "K 0xFB7"), "K 0xFB7") == 0) { // 37
        two_buttons(37);
    }
    if (strcmp(strstr(b, "K 0xEBF"), "K 0xEBF") == 0) { // 38
        two_buttons(38);
    }
    if (strcmp(strstr(b, "K 0xBBF"), "K 0xBBF") == 0) { // 39
        two_buttons(39);
    }
    if (strcmp(strstr(b, "K 0xDBF"), "K 0xDBF") == 0) { // 40
        two_buttons(40);
    }
    if (strcmp(strstr(b, "K 0x7BF"), "K 0x7BF") == 0) { // 41
        two_buttons(41);
    }
    if (strcmp(strstr(b, "K 0xF9F"), "K 0xF9F") == 0) { // 42
        two_buttons(42);
    }
    if (strcmp(strstr(b, "K 0xFAF"), "K 0xFAF") == 0) { // 43
        two_buttons(43);
    }
    if (strcmp(strstr(b, "K 0xF7E"), "K 0xF7E") == 0) { // 44
        two_buttons(44);
    }
    if (strcmp(strstr(b, "K 0xF7D"), "K 0xF7D") == 0) { // 45
        two_buttons(45);
    }
    if (strcmp(strstr(b, "K 0xF77"), "K 0xF77") == 0) { // 46
        two_buttons(46);
    }
    if (strcmp(strstr(b, "K 0xF7B"), "K 0xF7B") == 0) { // 47
        two_buttons(47);
    }
    if (strcmp(strstr(b, "K 0xE7F"), "K 0xE7F") == 0) { // 48
        two_buttons(48);
    }
    if (strcmp(strstr(b, "K 0xB7F"), "K 0xB7F") == 0) { // 49
        two_buttons(49);
    }
    if (strcmp(strstr(b, "K 0xD7F"), "K 0xD7F") == 0) { // 50
        two_buttons(50);
    }
    if (strcmp(strstr(b, "K 0x77F"), "K 0x77F") == 0) { // 51
        two_buttons(51);
    }
    if (strcmp(strstr(b, "K 0xF6F"), "K 0xF6F") == 0) { // 52
        two_buttons(52);
    }
    if (strcmp(strstr(b, "K 0xF5F"), "K 0xF5F") == 0) { // 53
        two_buttons(53);
    }
    if (strcmp(strstr(b, "K 0xFFF"), "K 0xFFF") == 0) {
        if(one_pressed) {
            one_pressed = 0;
            press_time_stop();
        }
        if (two_pressed) {
            two_button_controller();
            press_time.reset();
        }
    }
}

/*************************************************************
* save_file(): Saves the new sequence in a file titled OUT.txt
* saves it in the format 'move_type wait_time press_time' 
*************************************************************/

void save_file()
{   
    int f = 0;
    FILE *fp = fopen("/local/out.txt", "w");
    fseek(fp, 0, SEEK_SET);
lb:
    if (f < i) {
        fprintf(fp, "%d \n", move_type[f]);
        fprintf(fp, "%d \n", wait_times[f]);
        fprintf(fp, "%d \n", press_times[f]);
        f++;
        goto lb;
    }
    fclose(fp);
}

/*****************************************************************
* read_file(): reads the saved sequence from a file titled OUT.txt
* inteprets the data in the order move_type, wait_time and press_time
* prints the stored values  
*****************************************************************/

void read_file()
{
    pc.printf("\n\r");
    char c;
    char f[6];
    int r = 0, q = 0, t = 0;
    
    s = 0;

    FILE *fp = fopen("/local/out.txt", "r");
    if (fp) {
    fseek(fp, 0, SEEK_SET);
        while (!feof(fp)) {
            c = fgetc(fp);
            if (c != '\n') {
                if (c == ' ') {
                    t++;
                    if (t == 4) {
                        s++;
                        t = 1;
                    }
                    sscanf(f, "%d", &r);
                    switch(t) {
                        case 1:
                            move_type[s] = r;
                            break;
                        case 2:
                            wait_times[s] = (unsigned long long) r;
                            break;
                        case 3:
                            press_times[s] = (unsigned long long) r;
                            break;
                        default:
                            break;
                    }
                    for (int i = 0; i < q; i++) {
                        f[i] = '\0';
                    }
                    q = 0;
                } else {
                    //pc.printf("%c", c);
                    f[q] = c;
                    q++;
                }
            }
        }
        for (int i = 0; i < s; i++) {
            pc.printf("\n\r %d", move_type[i]);
            pc.printf(" %d", wait_times[i]);
            pc.printf(" %d", press_times[i]);
        }
        fclose(fp);
    }
}

/***********************************************************
* display_move() displays the type of move depending on the
* unique number assigned to it and also it's position in the
* array
***********************************************************/

void display_move(int b, int c)
{   
    switch(b) {
        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 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 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;

        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;

        case 34:
            pc.printf("\n\r Move: One + Up");
            break;

        case 35:
            pc.printf("\n\r Move: One + Down");
            break;

        case 36:
            pc.printf("\n\r Move: One + Left");
            break;

        case 37:
            pc.printf("\n\r Move: One + Right");
            break;

        case 38:
            pc.printf("\n\r Move: One + W");
            break;

        case 39:
            pc.printf("\n\r Move: One + S");
            break;

        case 40:
            pc.printf("\n\r Move: One + A");
            break;

        case 41:
            pc.printf("\n\r Move: One + D");
            break;

        case 42:
            pc.printf("\n\r Move: One + Space");
            break;

        case 43:
            pc.printf("\n\r Move: One + Enter");
            break;

        case 44:
            pc.printf("\n\r Move: Two + Up");
            break;

        case 45:
            pc.printf("\n\r Move: Two + Down");
            break;

        case 46:
            pc.printf("\n\r Move: Two + Right");
            break;

        case 47:
            pc.printf("\n\r Move: Two + Left");
            break;

        case 48:
            pc.printf("\n\r Move: Two + W");
            break;

        case 49:
            pc.printf("\n\r Move: Two + S");
            break;

        case 50:
            pc.printf("\n\r Move: Two + A");
            break;

        case 51:
            pc.printf("\n\r Move: Two + D");
            break;

        case 52:
            pc.printf("\n\r Move: Two + Enter");
            break;

        case 53:
            pc.printf("\n\r Move: Two + Space");
            break;

        default:
            break;
    }
    pc.printf("\n\r Position: %d", c);
}