Working reset, flipped logic
Dependencies: SDFileSystem emic2 mbed-rtos mbed
Fork of BAT_senior_design_Nhi by
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 //buttonarr.setMode(); 00147 00148 //led1 = 1; 00149 //led2 = 1; 00150 00151 00152 // PARSE INPUT FILE FOR LETTERS AND WORDS 00153 char delimiter = ','; 00154 // TODO: fix the letters 00155 string letter[2]; 00156 string word[2]; 00157 char check; 00158 string temp; 00159 string tempword = ""; 00160 int counter = 0; 00161 FILE *fp = fopen("/sd/plan.txt", "r"); //create file 00162 if(fp == NULL) { 00163 //pc.printf("Could not open file for write\n"); 00164 } 00165 check = fgetc(fp); //grabs a char from file 00166 while(check != '\n') { //while not at the end of line for letters 00167 if((check == delimiter) && (temp.length() == 1)) { //at a comma and have a letter stored 00168 letter[counter] = temp; //write letter 00169 //pc.printf("Letter: %s \n", letter[counter]); 00170 counter = counter + 1; //increment counter 00171 } else { 00172 temp = check; //store letter 00173 } 00174 check = fgetc(fp); //grabs next char 00175 } 00176 counter = 0; //reset counter 00177 check = fgetc(fp); //grabs next char 00178 while(!feof(fp)) { //while not at the end of line for words 00179 if(check == delimiter) { //when at the comma at the end of a word 00180 word[counter] = tempword; //write word 00181 //pc.printf("Word: %s \n", word[counter]); 00182 tempword = ""; 00183 counter = counter + 1; //increment counter 00184 } else { 00185 tempword = tempword + check; //concatenate letters to build word 00186 } 00187 check = fgetc(fp); //grabs next char 00188 } 00189 fclose(fp); //close file 00190 00191 Thread t1(button_thread); 00192 Thread t2(button2_thread); 00193 Thread t3(button3_thread); 00194 Thread t4(button4_thread); 00195 Thread t5(button5_thread); 00196 //Thread t6(button6_thread); 00197 t1.start(button_thread); 00198 t2.start(button2_thread); 00199 t3.start(button3_thread); 00200 t4.start(button4_thread); 00201 t5.start(button5_thread); 00202 //t6.start(button6_thread); 00203 00204 char currletter; 00205 int lettersize = sizeof(letter)/sizeof(letter[0]); 00206 int type = 0; 00207 00208 //TEXT-TO-SPEECH LOGIC 00209 myTTS.volume(1); //max volume 00210 myTTS.voice(2); 00211 00212 //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 00213 //myTTS.ready(); //ready waits for speech to finish from last command with a ":" response 00214 char* braille; 00215 char userinput; 00216 00217 // INITIAL RESET 00218 if (type == 0) { 00219 int reset = 1; 00220 myTTS.speakf("SPlease setup the device by pressing down all the buttons.\r"); 00221 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response 00222 while(reset == 1) { 00223 wait(2); 00224 sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(), 00225 button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress()); 00226 userinput = buttonarr.checkVal(braille); 00227 if(userinput == 'Z') { 00228 reset = 0; 00229 } else { 00230 //reset = 0; 00231 myTTS.speakf("SSetup failed. Please try again.\r"); 00232 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response 00233 } 00234 } 00235 } 00236 00237 for(int i = 0; i < lettersize; i++) { //iterate through the letter array 00238 char currletter = letter[i][0]; 00239 //pc.printf("letter: %c \n", currletter); 00240 int* pinsup = buttonarr.pinsUp(currletter); 00241 int currpress; 00242 int numpinsups = pinsup[0]; // size of array is first element of pinsup 00243 //string presspin; 00244 string presspin = "STo write the letter "; 00245 presspin = presspin + letter[i]; 00246 presspin = presspin + ", press buttons"; 00247 00248 for (int j = 1; j < numpinsups; j++) { // get what pins to press 00249 currpress = pinsup[j]; 00250 switch (currpress) { 00251 case 1: 00252 presspin = presspin + " 1,"; 00253 break; 00254 case 2: 00255 presspin = presspin + " 2,"; 00256 break; 00257 case 3: 00258 presspin = presspin + " 3,"; 00259 break; 00260 case 4: 00261 presspin = presspin + " 4,"; 00262 break; 00263 case 5: 00264 presspin = presspin + " 5,"; 00265 break; 00266 case 6: 00267 presspin = presspin + " 6,"; 00268 break; 00269 } 00270 } 00271 myTTS.speakf("%s\r",presspin); 00272 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response 00273 wait(1); 00274 //pc.printf("hello"); 00275 00276 //WORKS UP TO HERE 00277 char* braille; 00278 char userinput; 00279 char* oldbraille; 00280 if (type == 0) { 00281 sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(), 00282 button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress()); 00283 userinput = buttonarr.checkVal(braille); 00284 } else { 00285 // TYPE MODE 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 oldbraille = braille; 00290 } 00291 00292 int currwrong; 00293 string wrongpin; 00294 // check result 00295 pc.printf("\n %c %c", userinput, currletter); 00296 int* wrongpins = buttonarr.wrongPins(braille, currletter); 00297 int test = 1; 00298 while(test == 1) { 00299 //pc.printf("In while loop"); 00300 wrongpin = "SYour answer is incorrect. Buttons"; 00301 //wrongpin = wrongpin + letter[i]; 00302 //wrongpin = wrongpin + ", press pins"; 00303 for (int j = 1; j < wrongpins[0]; j++) { // get what pins are wrong 00304 currwrong = wrongpins[j]; 00305 //pc.printf("currwrong: %d", currwrong); 00306 switch (currwrong) { 00307 case 1: 00308 wrongpin = wrongpin + " 1,"; 00309 break; 00310 case 2: 00311 wrongpin = wrongpin + " 2,"; 00312 break; 00313 case 3: 00314 wrongpin = wrongpin + " 3,"; 00315 break; 00316 case 4: 00317 wrongpin = wrongpin + " 4,"; 00318 break; 00319 case 5: 00320 wrongpin = wrongpin + " 5,"; 00321 break; 00322 case 6: 00323 wrongpin = wrongpin + " 6,"; 00324 break; 00325 } 00326 } 00327 //pc.printf("end for loop"); 00328 if (wrongpins[0] > 1) { 00329 wrongpin = wrongpin + " are wrong. Please try again."; 00330 myTTS.speakf("%s\r",wrongpin); 00331 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response 00332 //UPDATE THE PINS THAT ARE WRONG BY CHECKING AGAIN BELOW 00333 //char* braille; 00334 if (type == 0) { 00335 sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(), 00336 button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress()); 00337 userinput = buttonarr.checkVal(braille); 00338 } else { 00339 // TYPE MODE 00340 sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(), 00341 button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress()); 00342 userinput = buttonarr.checkVal(braille); 00343 } 00344 // get input 00345 //pc.printf("Your input was: %c", userinput); 00346 00347 // check result 00348 wrongpins = buttonarr.wrongPins(braille, currletter); 00349 } else { 00350 test = 0; 00351 myTTS.speakf("SGood job!\r"); 00352 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response 00353 } 00354 } 00355 00356 if (type == 0) { 00357 int reset = 1; 00358 myTTS.speakf("SNow reset by pressing down all the buttons.\r"); 00359 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response 00360 while(reset == 1) { 00361 char* braille; 00362 sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(), 00363 button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress()); 00364 userinput = buttonarr.checkVal(braille); 00365 if(userinput == 'Z') { 00366 reset = 0; 00367 } else { 00368 myTTS.speakf("SPlease try again.\r"); 00369 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response 00370 } 00371 } 00372 } 00373 //pc.printf("wrongpins are: %d", wrongpins[0]); 00374 } 00375 00376 // WORD CODE 00377 00378 // WORD COUNTER 00379 // go through each word 00380 // go through each letter in the word 00381 // get user input 00382 // when buttons reset - save previous letter 00383 // loop until submit button is pressed 00384 // compare words 00385 // check what letters were wrong 00386 // give wrong letters - correct spelling is... 00387 00388 string currword; 00389 int wordsize = sizeof(word)/sizeof(word[0]); 00390 00391 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 00392 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response 00393 00394 for(int i = 0; i < wordsize; i++) { //iterate through the word array 00395 currword = word[i]; 00396 string presspin = "To type the word "; 00397 presspin = presspin + word[i]; 00398 presspin = presspin + ", type letters "; 00399 for (int j = 0; j < currword.length(); j++) { 00400 presspin = presspin + ", "+ currword[j]; 00401 } 00402 //myTTS.speakf("S%s\r", presspin); 00403 //myTTS.ready(); 00404 } 00405 00406 /* 00407 // loop through each of the words 00408 for(int i = 0; i < wordsize; i++) { //iterate through the word array 00409 currword = word[i]; 00410 string presspin = "To type the word "; 00411 presspin = presspin + word[i]; 00412 presspin = presspin + ", type letters "; 00413 for (int j = 0; j < currword.length(); j++) { 00414 presspin = presspin + ", "+ currword[j]; 00415 } 00416 myTTS.speakf("S%s\r", presspin); 00417 myTTS.ready(); 00418 00419 string wordinput = ""; 00420 00421 // loop through each of the letters 00422 //olduserinput; 00423 int incorrect = 1; 00424 //char userinput; 00425 //char* braille; 00426 00427 // while(!submit) 00428 while (incorrect) { 00429 // replace for loop with submit 00430 // while (!submit) { 00431 for (int j = 0; j < currword.length(); j++) { 00432 //currletter = currword[j]; 00433 // check if all buttons were pressed 00434 wait(1); 00435 00436 char* braille; 00437 char* oldbraille; 00438 00439 // wait for input 00440 while (strcmp(braille, "000000") == 0) {} 00441 00442 // while buttons have not been released 00443 while (strcmp(braille, "000000") != 0) { 00444 oldbraille = braille; 00445 sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(), 00446 button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress()); 00447 } 00448 00449 // at the end of this, oldbraille contains the letter representation 00450 // flip braille values 00451 //userinput = checkVal(oldbraille); 00452 wordinput[j] = buttonarr.checkVal(oldbraille); 00453 //wordinput[j] = olduserinput; 00454 } 00455 00456 // check result of words 00457 if (wordinput.compare(currword) == 0) { // input is correct 00458 incorrect = 0; 00459 myTTS.speakf("Good job!\r"); 00460 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response 00461 } else { 00462 incorrect = 0; // delete this line 00463 myTTS.speakf("SPlease try again!.\r"); 00464 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response 00465 } 00466 } 00467 00468 }*/ 00469 00470 // MAIN THREAD 00471 while(true) { 00472 00473 Thread::wait(500); // wait till thread is done 00474 } 00475 }
Generated on Mon Jul 18 2022 15:26:09 by
1.7.2
