BAT / Mbed 2 deprecated BAT_Read

Dependencies:   SDFileSystem emic2 mbed-rtos mbed

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 Serial pc(USBTX, USBRX);
00027 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
00028 button button1(myservo, pb1, 1);
00029 button button2(myservo2, pb2, 2);
00030 button button3(myservo3, pb3, 3);
00031 button button4(myservo4, pb4, 4);
00032 button button5(myservo5, pb5, 5);
00033 button button6(myservo6, pb6, 6);
00034 
00035 buttonArray buttonarr(button1, button2, button3, button4, button5, button6);
00036 
00037 emic2 myTTS(p28, p27); //serial RX,TX pins to emic
00038 
00039 // INITIALIZE VARIABLES
00040 // add mode, reset buttons
00041 int start = 0;
00042 int submit = 0;
00043 // Buttons begins in up state
00044 int state = 0;
00045 int state2 = 0;
00046 int state3 = 0;
00047 int state4 = 0;
00048 int state5 = 0;
00049 int state6 = 0;
00050 int count = 0;
00051 
00052 // THREADS
00053 void button_thread()
00054 {
00055     while(true) {
00056         state = button1.updateState();
00057         state6 = button6.updateState();
00058         Thread::wait(100); // wait till thread is done
00059     }
00060 }
00061 
00062 void button2_thread()
00063 {
00064     while(true) {
00065         state2 = button2.updateState();
00066         Thread::wait(200); // wait till thread is done
00067     }
00068 }
00069 
00070 void button3_thread()
00071 {
00072     while(true) {
00073         state3 = button3.updateState();
00074         Thread::wait(200); // wait till thread is done
00075     }
00076 }
00077 
00078 void button4_thread()
00079 {
00080     while(true) {
00081         state4 = button4.updateState();
00082         Thread::wait(200); // wait till thread is done
00083     }
00084 }
00085 
00086 void button5_thread()
00087 {
00088     while(true) {
00089         state5 = button5.updateState();
00090         Thread::wait(200); // wait till thread is done
00091     }
00092 }
00093 
00094 void button6_thread()
00095 {
00096     while(true) {
00097         state6 = button6.updateState();
00098         Thread::wait(200); // wait till thread is done
00099     }
00100 }
00101 
00102 void submit_thread()
00103 {
00104     Thread::wait(500); // wait till thread is done
00105 }
00106 
00107 void start_thread()
00108 {
00109     start = 1;
00110     Thread::wait(500); // wait till thread is done
00111 }
00112 
00113 int main()
00114 {
00115     // SETUP; pull up the pushbutton to prevent bouncing
00116     pb1.mode(PullUp);
00117     pb2.mode(PullUp);
00118     pb3.mode(PullUp);
00119     pb4.mode(PullUp);
00120     pb5.mode(PullUp);
00121     pb6.mode(PullUp);
00122     wait(.001);
00123 
00124     // servo setup up function; servos begin at 30 degrees
00125     buttonarr.setup();
00126 
00127     // PARSE INPUT FILE FOR LETTERS AND WORDS
00128     char delimiter = ',';
00129     string letter[2];
00130     string word[2];
00131     char check;
00132     string temp;
00133     string tempword = "";
00134     int counter = 0;
00135     FILE *fp = fopen("/sd/plan.txt", "r"); //create file
00136     check = fgetc(fp); //grabs a char from file
00137     while(check != '\n') {  //while not at the end of line for letters
00138         if((check == delimiter) && (temp.length() == 1)) {  //at a comma and have a letter stored
00139             letter[counter] = temp; //write letter
00140             counter = counter + 1;  //increment counter
00141         } else {
00142             temp = check;   //store letter
00143         }
00144         check = fgetc(fp);  //grabs next char
00145     }
00146     counter = 0;    //reset counter
00147     check = fgetc(fp);  //grabs next char
00148     while(!feof(fp)) {  //while not at the end of line for words
00149         if(check == delimiter) {  //when at the comma at the end of a word
00150             word[counter] = tempword;   //write word
00151             tempword = "";
00152             counter = counter + 1;  //increment counter
00153         } else {
00154             tempword = tempword + check;    //concatenate letters to build word
00155         }
00156         check = fgetc(fp);  //grabs next char
00157     }
00158     fclose(fp); //close file
00159     
00160     //INITIALIZE THREADS
00161     Thread t1(button_thread);
00162     Thread t2(button2_thread);
00163     Thread t3(button3_thread);
00164     Thread t4(button4_thread);
00165     Thread t5(button5_thread);
00166     t1.start(button_thread);
00167     t2.start(button2_thread);
00168     t3.start(button3_thread);
00169     t4.start(button4_thread);
00170     t5.start(button5_thread);
00171 
00172     char currletter;
00173     int lettersize = sizeof(letter)/sizeof(letter[0]);
00174     int type = 0;
00175 
00176     //TEXT-TO-SPEECH LOGIC
00177     myTTS.volume(1); //max volume is 18
00178     myTTS.voice(2);
00179 
00180     char* braille;
00181     char userinput;
00182 
00183     // INITIAL RESET
00184     if (type == 0) {
00185         int reset = 1;
00186         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");
00187         myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
00188         while(reset == 1) {
00189             wait(3);
00190             sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(),
00191                     button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress());
00192             userinput = buttonarr.checkVal(braille);
00193             if(userinput == 'Z') {
00194                 reset = 0;
00195             } else {
00196                 myTTS.speakf("SSetup failed. Please try again.\r");
00197                 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
00198             }
00199         }
00200     }
00201 
00202     for(int i = 0; i < lettersize; i++) {  //iterate through the letter array
00203         char currletter = letter[i][0];
00204         int* pinsup = buttonarr.pinsUp(currletter);
00205         int currpress;
00206         int numpinsups = pinsup[0];  // size of array is first element of pinsup
00207         string presspin = "STo write the letter ";
00208         presspin = presspin + letter[i];
00209         presspin = presspin + ", press buttons";
00210 
00211         for (int j = 1; j < numpinsups; j++) {  // get what pins to press
00212             currpress = pinsup[j];
00213             switch (currpress) {
00214                 case 1:
00215                     presspin = presspin + " 1,";
00216                     break;
00217                 case 2:
00218                     presspin = presspin + " 2,";
00219                     break;
00220                 case 3:
00221                     presspin = presspin + " 3,";
00222                     break;
00223                 case 4:
00224                     presspin = presspin + " 4,";
00225                     break;
00226                 case 5:
00227                     presspin = presspin + " 5,";
00228                     break;
00229                 case 6:
00230                     presspin = presspin + " 6,";
00231                     break;
00232             }
00233         }
00234         myTTS.speakf("%s\r",presspin);
00235         myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
00236         wait(2);
00237 
00238         char* braille;
00239         char userinput;
00240         char* oldbraille;
00241         if (type == 0) {
00242             sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(),
00243                     button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress());
00244             userinput = buttonarr.checkVal(braille);
00245         } else {
00246             // TYPE MODE
00247             sprintf(braille, "%d%d%d%d%d%d", !button1.getPress(), !button2.getPress(),
00248                     !button3.getPress(), !button4.getPress(), !button5.getPress(), !button6.getPress());
00249             userinput = buttonarr.checkVal(braille);
00250             oldbraille = braille;
00251         }
00252 
00253         int currwrong;
00254         string wrongpin;
00255         // check result
00256         pc.printf("\n %c %c", userinput, currletter);
00257         int* wrongpins = buttonarr.wrongPins(braille, currletter);
00258         int test = 1;
00259         while(test == 1) {
00260             wrongpin = "SYour answer is incorrect. Buttons";
00261             for (int j = 1; j < wrongpins[0]; j++) {  // get what pins are wrong
00262                 currwrong = wrongpins[j];
00263                 switch (currwrong) {
00264                     case 1:
00265                         wrongpin = wrongpin + " 1,";
00266                         break;
00267                     case 2:
00268                         wrongpin = wrongpin + " 2,";
00269                         break;
00270                     case 3:
00271                         wrongpin = wrongpin + " 3,";
00272                         break;
00273                     case 4:
00274                         wrongpin = wrongpin + " 4,";
00275                         break;
00276                     case 5:
00277                         wrongpin = wrongpin + " 5,";
00278                         break;
00279                     case 6:
00280                         wrongpin = wrongpin + " 6,";
00281                         break;
00282                 }
00283             }
00284             if (wrongpins[0] > 1) {
00285                 wrongpin = wrongpin + " are wrong. Please try again.";
00286                 myTTS.speakf("%s\r",wrongpin);
00287                 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
00288                 wait(2);
00289                 //UPDATE THE PINS THAT ARE WRONG BY CHECKING AGAIN BELOW
00290                 sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(),
00291                 button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress());
00292                 userinput = buttonarr.checkVal(braille);
00293                 
00294                 // check result
00295                 wrongpins = buttonarr.wrongPins(braille, currletter);
00296             } else {
00297                 test = 0;
00298                 myTTS.speakf("SGood job!\r");
00299                 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
00300             }
00301         }
00302 
00303         if (type == 0) {
00304             int reset = 1;
00305             myTTS.speakf("SNow reset by pressing down all the buttons.\r");
00306             myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
00307             while(reset == 1) {
00308                 char* braille;
00309                 sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(),
00310                         button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress());
00311                 userinput = buttonarr.checkVal(braille);
00312                 if(userinput == 'Z') {
00313                     reset = 0;
00314                 } else {
00315                     wait(2);
00316                     myTTS.speakf("SPlease try again.\r");
00317                     myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
00318                 }
00319             }
00320         }
00321     }
00322 
00323 // MAIN THREAD
00324     while(true) {
00325 
00326         Thread::wait(500); // wait till thread is done
00327     }
00328 }