Test fork nhi

Dependencies:   SDFileSystem mbed-rtos mbed emic2

Fork of BAT_senior_design by BAT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "SDFileSystem.h"
00004 #include "button.h"
00005 #include "buttonArray.h"
00006 #include <string>
00007 #include <iostream>
00008 #include "emic2.h"
00009 
00010 using namespace std;
00011 
00012 // DEFINE I/O
00013 PwmOut myservo(p21);
00014 DigitalIn pb1 (p20);
00015 PwmOut myservo2(p22);
00016 DigitalIn pb2 (p19);
00017 PwmOut myservo3(p23);
00018 DigitalIn pb3 (p18);
00019 PwmOut myservo4(p24);
00020 DigitalIn pb4 (p17);
00021 PwmOut myservo5(p25);
00022 DigitalIn pb5 (p16);
00023 PwmOut myservo6(p26);
00024 DigitalIn pb6 (p15);
00025 
00026 //DigitalOut led1(LED1);
00027 //DigitalOut led3(LED3);
00028 //DigitalOut led4(LED4);
00029 
00030 //DigitalIn linpot(p9);
00031 Serial pc(USBTX, USBRX);
00032 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
00033 //button button1(myservo, pb1, linpot);
00034 button button1(myservo, pb1, 1);
00035 button button2(myservo2, pb2, 2);
00036 button button3(myservo3, pb3, 3);
00037 button button4(myservo4, pb4, 4);
00038 button button5(myservo5, pb5, 5);
00039 button button6(myservo6, pb6, 6);
00040 
00041 buttonArray buttonarr(button1, button2, button3, button4, button5, button6);
00042 
00043 emic2 myTTS(p28, p27); //serial RX,TX pins to emic
00044 
00045 // INITIALIZE VARIABLES
00046 // add mode, reset buttons
00047 int start = 0;
00048 int submit = 0;
00049 // Buttons begins in up state
00050 int state = 0;
00051 int state2 = 0;
00052 int state3 = 0;
00053 int state4 = 0;
00054 int state5 = 0;
00055 int state6 = 0;
00056 int count = 0;
00057 
00058 // THREADS
00059 
00060 void button_thread()
00061 {
00062     while(true) {
00063         state = button1.updateState();
00064         state6 = button6.updateState();
00065 
00066         //led4 = button1.getLp();
00067         Thread::wait(100); // wait till thread is done
00068     }
00069 }
00070 
00071 void button2_thread()
00072 {
00073     while(true) {
00074         state2 = button2.updateState();
00075         Thread::wait(200); // wait till thread is done
00076     }
00077 }
00078 
00079 // thread for the custom button
00080 void button3_thread()
00081 {
00082     while(true) {
00083         state3 = button3.updateState();
00084         Thread::wait(200); // wait till thread is done
00085     }
00086 }
00087 
00088 // thread for the custom button
00089 void button4_thread()
00090 {
00091     while(true) {
00092         state4 = button4.updateState();
00093         Thread::wait(200); // wait till thread is done
00094     }
00095 }
00096 
00097 // thread for the custom button
00098 void button5_thread()
00099 {
00100     while(true) {
00101         state5 = button5.updateState();
00102         Thread::wait(200); // wait till thread is done
00103     }
00104 }
00105 
00106 // thread for the custom button
00107 void button6_thread()
00108 {
00109     while(true) {
00110         state6 = button6.updateState();
00111         Thread::wait(200); // wait till thread is done
00112     }
00113 }
00114 
00115 void submit_thread()
00116 {
00117     Thread::wait(500); // wait till thread is done
00118 }
00119 
00120 void start_thread()
00121 {
00122     // read pb_start
00123     // if 1
00124     start = 1;
00125     //pc.printf("start %d ", start);
00126     // else 0
00127     Thread::wait(500); // wait till thread is done
00128 }
00129 
00130 
00131 int main()
00132 {
00133     // SETUP
00134     // pull up the pushbutton to prevent bouncing
00135     pb1.mode(PullUp);
00136     pb2.mode(PullUp);
00137     pb3.mode(PullUp);
00138     pb4.mode(PullUp);
00139     pb5.mode(PullUp);
00140     pb6.mode(PullUp);
00141     wait(.001);
00142 
00143     // servos begin at 30 degrees
00144     // replace with a button setup function
00145     buttonarr.setup();
00146 
00147     // PARSE INPUT FILE FOR LETTERS AND WORDS
00148     char delimiter = ',';
00149     // TODO: fix the letters
00150     string letter[2];
00151     string word[2];
00152     char check;
00153     string temp;
00154     string tempword = "";
00155     int counter = 0;
00156     FILE *fp = fopen("/sd/plan.txt", "r"); //create file
00157     if(fp == NULL) {
00158         //pc.printf("Could not open file for write\n");
00159     }
00160     check = fgetc(fp); //grabs a char from file
00161     while(check != '\n') {  //while not at the end of line for letters
00162         if((check == delimiter) && (temp.length() == 1)) {  //at a comma and have a letter stored
00163             letter[counter] = temp; //write letter
00164             //pc.printf("Letter: %s \n", letter[counter]);
00165             counter = counter + 1;  //increment counter
00166         } else {
00167             temp = check;   //store letter
00168         }
00169         check = fgetc(fp);  //grabs next char
00170     }
00171     counter = 0;    //reset counter
00172     check = fgetc(fp);  //grabs next char
00173     while(!feof(fp)) {  //while not at the end of line for words
00174         if(check == delimiter) {  //when at the comma at the end of a word
00175             word[counter] = tempword;   //write word
00176             //pc.printf("Word: %s \n", word[counter]);
00177             tempword = "";
00178             counter = counter + 1;  //increment counter
00179         } else {
00180             tempword = tempword + check;    //concatenate letters to build word
00181         }
00182         check = fgetc(fp);  //grabs next char
00183     }
00184     fclose(fp); //close file
00185 
00186     Thread t1(button_thread);
00187     Thread t2(button2_thread);
00188     Thread t3(button3_thread);
00189     Thread t4(button4_thread);
00190     Thread t5(button5_thread);
00191     //Thread t6(button6_thread);
00192     t1.start(button_thread);
00193     t2.start(button2_thread);
00194     t3.start(button3_thread);
00195     t4.start(button4_thread);
00196     t5.start(button5_thread);
00197 
00198     // when submitted
00199     //while (submit == 0) {}
00200     // start button threads and submit thread
00201     // if submit close button threads and submit thread
00202     // check result
00203     // play results on speaker
00204     // save results
00205 
00206     //TEXT-TO-SPEECH LOGIC
00207     myTTS.volume(18); //max volume
00208     myTTS.voice(2);
00209 
00210     myTTS.speakf("SWelcome to Bat, the Braille Assistive Teacher. This device will help you learn how to write and type braille.\r");  // Send the desired string to convert to speech
00211     myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
00212 
00213     char* braille;
00214     char userinput;
00215 
00216     // INITIAL RESET
00217     int reset = 1;
00218     myTTS.speakf("SPlease setup the device by pressing down all the buttons.\r");
00219     myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
00220     while(reset == 1) {
00221         sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(),
00222                 button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress());
00223         userinput = buttonarr.checkVal(braille);
00224         if(userinput == '!') {
00225             reset = 0;
00226         } else {
00227             reset = 0; // TODO: remove this line
00228             myTTS.speakf("SSetup failed. Please try again.\r");
00229             myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
00230         }
00231     }
00232 
00233     int lettersize = sizeof(letter)/sizeof(letter[0]);
00234 
00235     for(int i = 0; i < lettersize; i++) {  //iterate through the letter array
00236         char currletter = letter[i][0];
00237         //pc.printf("letter: %c \n", currletter);
00238         int* pinsup = buttonarr.pinsUp(currletter);
00239         int currpress;
00240         int numpinsups = pinsup[0];  // size of array is first element of pinsup
00241         string presspin = "STo write the letter ";
00242         presspin = presspin + letter[i];
00243         presspin = presspin + ", press buttons";
00244 
00245         for (int j = 1; j < numpinsups; j++) {  // get what pins to press
00246             currpress = pinsup[j];
00247             switch (currpress) {
00248                 case 1:
00249                     presspin = presspin + " 1,";
00250                     break;
00251                 case 2:
00252                     presspin = presspin + " 2,";
00253                     break;
00254                 case 3:
00255                     presspin = presspin + " 3,";
00256                     break;
00257                 case 4:
00258                     presspin = presspin + " 4,";
00259                     break;
00260                 case 5:
00261                     presspin = presspin + " 5,";
00262                     break;
00263                 case 6:
00264                     presspin = presspin + " 6,";
00265                     break;
00266             }
00267         }
00268         myTTS.speakf("%s\r",presspin);
00269         myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
00270         wait(1);
00271 
00272         //WORKS UP TO HERE
00273 
00274         // PARSE LETTERS
00275         sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(),
00276                 button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress());
00277         userinput = buttonarr.checkVal(braille);
00278         pc.printf(" %s ", braille);
00279         //pc.printf("userinput: %c", userinput);
00280         // get input
00281         //pc.printf("Your input was: %c", userinput);
00282         int currwrong;
00283         string wrongpin;
00284         // check result
00285         pc.printf("\n %c %c", userinput, currletter);
00286         int* wrongpins = buttonarr.wrongPins(braille, currletter);
00287         int test = 1;
00288         while(test == 1) {
00289             //pc.printf("In while loop");
00290             wrongpin = "SYour answer is incorrect. Buttons";
00291             for (int j = 1; j < wrongpins[0]; j++) {  // get what pins are wrong
00292                 currwrong = wrongpins[j];
00293                 //pc.printf("currwrong: %d", currwrong);
00294                 switch (currwrong) {
00295                     case 1:
00296                         wrongpin = wrongpin + " 1,";
00297                         break;
00298                     case 2:
00299                         wrongpin = wrongpin + " 2,";
00300                         break;
00301                     case 3:
00302                         wrongpin = wrongpin + " 3,";
00303                         break;
00304                     case 4:
00305                         wrongpin = wrongpin + " 4,";
00306                         break;
00307                     case 5:
00308                         wrongpin = wrongpin + " 5,";
00309                         break;
00310                     case 6:
00311                         wrongpin = wrongpin + " 6,";
00312                         break;
00313                 }
00314             }
00315             //pc.printf("end for loop");
00316             if (wrongpins[0] > 1) {
00317                 wrongpin = wrongpin + " are wrong. Please try again.";
00318                 myTTS.speakf("%s\r",wrongpin);
00319                 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
00320                 //UPDATE THE PINS THAT ARE WRONG BY CHECKING AGAIN BELOW
00321                 sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(),
00322                         button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress());
00323                 userinput = buttonarr.checkVal(braille);
00324                 // get input
00325                 //pc.printf("Your input was: %c", userinput);
00326 
00327                 // check result
00328                 wrongpins = buttonarr.wrongPins(braille, currletter);
00329             } else {
00330                 test = 0;
00331                 myTTS.speakf("SGood job! Your answer is correct.\r");
00332                 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
00333             }
00334         }
00335         reset = 1;
00336         myTTS.speakf("SNow reset the device by pressing down all the buttons.\r");
00337         myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
00338         while(reset == 1) {
00339             sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(),
00340                     button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress());
00341             userinput = buttonarr.checkVal(braille);
00342             if(userinput == '!') {
00343                 reset = 0;
00344             } else {
00345                 reset = 0; // TODO: remove this line
00346                 myTTS.speakf("SSetup failed. Please try again.\r");
00347                 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
00348             }
00349         }
00350     }
00351 
00352     // WORD CODE
00353 
00354     // WORD COUNTER
00355     // go through each word
00356     // go through each letter in the word
00357     // get user input
00358     // when buttons reset - save previous letter
00359     // loop until submit button is pressed
00360     // compare words
00361     // check what letters were wrong
00362     // give wrong letters - correct spelling is...
00363     /*
00364     string currword;
00365     int wordsize = sizeof(word)/sizeof(word[0]);
00366 
00367     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
00368     myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
00369 
00370     // loop through each of the words
00371     for(int i = 0; i < wordsize; i++) {  //iterate through the word array
00372         currword = word[i];
00373         presspin = "To type the word ";
00374         presspin = presspin + word[i];
00375         presspin = presspin + ", type letters ";
00376         for (int j = 0; j < currword.length(); j++) {
00377             presspin = presspin + ", "+ currword[j];
00378         }
00379         myTTS.speakf("S%s\r", presspin);
00380         myTTS.ready();
00381 
00382         string wordinput;
00383 
00384         // loop through each of the letters
00385         olduserinput;
00386         incorrect = 1;
00387         char userinput;
00388         char* braille;
00389 
00390         // while(!submit)
00391         while (incorrect) {
00392             // replace for loop with submit
00393             // while (!submit) {
00394             for (int j = 0; j < currword.length(); j++) {
00395                 //currletter = currword[j];
00396                 // check if all buttons were pressed
00397                 wait(1);
00398 
00399                 char* braille;
00400                 char* oldbraille;
00401                 
00402                 // wait for input
00403                 while (strcmp(braille, "000000") == 0) {}
00404                 
00405                 // while buttons have not been released
00406                 while (strcmp(braille, "000000") != 0) {
00407                     oldbraille = braille;
00408                     sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(),
00409                             button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress());
00410                 } // at the end of this, braille contains the letter representation
00411                 // flip braille values
00412                 userinput = checkVal(braille);
00413                 wordinput[j] = olduserinput;
00414             }
00415 
00416             // check result of words
00417             if (wordinput.compare(currword) == 0) { // input is correct
00418                 incorrect = 0;
00419                 myTTS.speakf("Good job!\r");
00420                 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
00421             } else {
00422                 incorrect = 0; // delete this line
00423                 myTTS.speakf("SPlease try again!.\r");
00424                 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
00425             }
00426         }*/
00427 
00428 
00429     // MAIN THREAD
00430     while(true) {
00431         Thread::wait(500); // wait till thread is done
00432     }
00433 }