BAT / Mbed 2 deprecated BAT_Read_Final

Dependencies:   SDFileSystem emic2 mbed-rtos mbed

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