Words in Typing mode FINAL

Dependencies:   SDFileSystem emic2 mbed-rtos mbed

Fork of BAT_Type_word_FINAL by Azra Ismail

main.cpp

Committer:
aismail1997
Date:
2017-12-05
Revision:
44:9ccc9f8524fd
Parent:
43:fa8d0db68228

File content as of revision 44:9ccc9f8524fd:

#include "mbed.h"
#include "rtos.h"
#include "SDFileSystem.h"
#include "button.h"
#include "buttonArray.h"
#include <string>
#include <iostream>
#include "emic2.h"

using namespace std;

// DEFINE I/O
PwmOut myservo(p21);
DigitalIn pb1 (p20);
PwmOut myservo2(p22);
DigitalIn pb2 (p19);
PwmOut myservo3(p23);
DigitalIn pb3 (p18);
PwmOut myservo4(p24);
DigitalIn pb4 (p17);
PwmOut myservo5(p25);
DigitalIn pb5 (p16);
PwmOut myservo6(p26);
DigitalIn pb6 (p15);
DigitalIn read (p29);
DigitalIn submit (p30);


//DigitalOut led1(LED1);
//DigitalOut led3(LED3);
//DigitalOut led4(LED4);

//DigitalIn linpot(p9);
Serial pc(USBTX, USBRX);
SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
//button button1(myservo, pb1, linpot);
button button1(myservo, pb1, 1);
button button2(myservo2, pb2, 2);
button button3(myservo3, pb3, 3);
button button4(myservo4, pb4, 4);
button button5(myservo5, pb5, 5);
button button6(myservo6, pb6, 6);

buttonArray buttonarr(button1, button2, button3, button4, button5, button6);

emic2 myTTS(p28, p27); //serial RX,TX pins to emic

// INITIALIZE VARIABLES
// add mode, reset buttons
// Buttons begins in up state
int state = 0;
int state2 = 0;
int state3 = 0;
int state4 = 0;
int state5 = 0;
int state6 = 0;
int count = 0;

// THREADS

void button_thread()
{
    while(true) {
        state = button1.updateState();
        state6 = button6.updateState();

        //led4 = button1.getLp();
        Thread::wait(100); // wait till thread is done
    }
}

void button2_thread()
{
    while(true) {
        state2 = button2.updateState();
        //state5 = button5.updateState();
        Thread::wait(200); // wait till thread is done
    }
}

// thread for the custom button
void button3_thread()
{
    while(true) {
        state3 = button3.updateState();
        //state5 = button4.updateState();
        Thread::wait(200); // wait till thread is done
    }
}

// thread for the custom button
void button4_thread()
{
    while(true) {
        state4 = button4.updateState();
        Thread::wait(200); // wait till thread is done
    }
}

// thread for the custom button
void button5_thread()
{
    while(true) {
        state5 = button5.updateState();
        Thread::wait(200); // wait till thread is done
    }
}


int main()
{
    // SETUP
    // pull up the pushbutton to prevent bouncing
    pb1.mode(PullUp);
    pb2.mode(PullUp);
    pb3.mode(PullUp);
    pb4.mode(PullUp);
    pb5.mode(PullUp);
    pb6.mode(PullUp);
    read.mode(PullUp);
    submit.mode(PullUp);
    wait(.001);

    // PARSE INPUT FILE FOR LETTERS AND WORDS
    char delimiter = ',';
    // TODO: fix the letters
    string letter[2];
    string word[2];
    char check;
    string temp;
    string tempword = "";
    int counter = 0;
    FILE *fp = fopen("/sd/plan.txt", "r"); //create file
    if(fp == NULL) {
        //pc.printf("Could not open file for write\n");
    }
    check = fgetc(fp); //grabs a char from file
    while(check != '\n') {  //while not at the end of line for letters
        if((check == delimiter) && (temp.length() == 1)) {  //at a comma and have a letter stored
            letter[counter] = temp; //write letter
            counter = counter + 1;  //increment counter
        } else {
            temp = check;   //store letter
        }
        check = fgetc(fp);  //grabs next char
    }
    counter = 0;    //reset counter
    check = fgetc(fp);  //grabs next char
    while(!feof(fp)) {  //while not at the end of line for words
        if(check == delimiter) {  //when at the comma at the end of a word
            word[counter] = tempword;   //write word
            tempword = "";
            counter = counter + 1;  //increment counter
        } else {
            tempword = tempword + check;    //concatenate letters to build word
        }
        check = fgetc(fp);  //grabs next char
    }
    fclose(fp); //close file

    Thread t1(button_thread);
    Thread t2(button2_thread);
    Thread t3(button3_thread);
    Thread t4(button4_thread);
    Thread t5(button5_thread);
    //Thread t6(button6_thread);
    t1.start(button_thread);
    t2.start(button2_thread);
    t3.start(button3_thread);
    t4.start(button4_thread);
    t5.start(button5_thread);
    //t6.start(button6_thread);

    //char currletter;
    int lettersize = sizeof(letter)/sizeof(letter[0]);
    //int type = 0;

    //TEXT-TO-SPEECH LOGIC
    myTTS.volume(18); //max volume is 18
    myTTS.voice(2);


    // SETUP
    buttonarr.setup(); //Reset servos so that buttons are up for typing mode
    button1.setMode(1);
    button2.setMode(1);
    button3.setMode(1);
    button4.setMode(1);
    button5.setMode(1);
    button6.setMode(1);


// WORD CODE

// WORD COUNTER
// go through each word
// go through each letter in the word
// get user input
// when buttons reset - save previous letter
// loop until submit button is pressed
// compare words
// check what letters were wrong
// give wrong letters - correct spelling is...

    string currword;
    int wordsize = sizeof(word)/sizeof(word[0]);

    //myTTS.speakf("SLet us now put these letters together to form words. Please reset buttons after each letter. Press down the submit button when done.\r");  // Send the desired string to convert to speech
    myTTS.speakf("SPlease switch the device into typing mode. After each word, press the submit button.\r");  // Send the desired string to convert to speech
    myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
    while(read==1) {}

    // loop through each of the words
    for(int i = 0; i < wordsize; i++) {  //iterate through the word array
        //int i = 0;
        currword = word[i];
        char currword1[3];

        string presspin = "To type the word ";
        presspin = presspin + word[i];
        presspin = presspin + ", type letters ";
        for (int j = 0; j < currword.length(); j++) {
            presspin = presspin + ", "+ currword[j];
            currword1[j] = currword[j];
        }

        myTTS.speakf("S%s\r", presspin);
        myTTS.ready();

        // loop through each of the letters
        int incorrect = 1;
        char userinput;

        // while(!submit)
        while (incorrect == 1) {

            char wordinput[3];
            // replace for loop with submit
            // while (!submit) {
            //int lettercount = 0;
            //int mysubmit = 0;
            //while(submit==0) {
            for (int j = 0; j < 3; j++) {
                // check if all buttons were pressed

                char* braille;
                char* oldbraille;

                wait(0.5);

                sprintf(braille, "%d%d%d%d%d%d", !button1.getPress(), !button2.getPress(),
                        !button3.getPress(), !button4.getPress(), !button5.getPress(), !button6.getPress());

                // wait for input
                while ((strcmp(braille, "111111") == 0)) {
                    sprintf(braille, "%d%d%d%d%d%d", !button1.getPress(), !button2.getPress(),
                            !button3.getPress(), !button4.getPress(), !button5.getPress(), !button6.getPress());
                }
                /*if(submit==1) {
                    pc.printf("submit\n");
                    break;
                }*/

                // while buttons have not been released
                int oldsum = 0;
                int sum = 0;
                while ((strcmp(braille, "111111") != 0) && (oldsum <= sum)) {
                    oldbraille = braille;
                    oldsum = sum;
                    sprintf(braille, "%d%d%d%d%d%d", !button1.getPress(), !button2.getPress(),
                            !button3.getPress(), !button4.getPress(), !button5.getPress(), !button6.getPress());
                    sum = button1.getPress() + button2.getPress() + button3.getPress() + button4.getPress()
                          + button5.getPress() + button6.getPress();
                }

                // at the end of this, oldbraille contains the letter representation
                //pc.printf("%s ", oldbraille);
                //if(mysubmit==0) {
                userinput = buttonarr.checkVal(oldbraille);
                //pc.printf("%c ", userinput);
                //wordinput[j] = userinput;
                //if(j <= 2) {
                wordinput[j] = userinput;
                //}
                //lettercount++;
            }
            
            //char* braille;            
            while (submit == 0) {
                /*sprintf(braille, "%d%d%d%d%d%d", !button1.getPress(), !button2.getPress(),
                            !button3.getPress(), !button4.getPress(), !button5.getPress(), !button6.getPress());                
                userinput = buttonarr.checkVal(braille);
                if (userinput != 'Z') {
                    lettercount++;
                }*/
            }
            
            //pc.printf("submit button pressed\n");

            // check result of words
            incorrect = 0;
            for (int k = 0; k < 3; k++) {
                /*if (lettercount > 3) {
                    incorrect = 1;
                }*/
                if (currword1[k] != wordinput[k]) { // if any letter doesn't match
                    incorrect = 1; // spelling is wrong
                }
            }

            if (incorrect == 1) {
                myTTS.speakf("SPlease try again! %s\r", presspin);
                myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
            } else {
                myTTS.speakf("SGood job!\r");
                myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
            }
        }
    }
    myTTS.speakf("SThis is the end of the module.\r");  // Send the desired string to convert to speech
    myTTS.ready(); //ready waits for speech to finish from last command with a ":" response

// MAIN THREAD
    while(true) {

        Thread::wait(500); // wait till thread is done
    }
}