Words in Typing mode FINAL

Dependencies:   SDFileSystem emic2 mbed-rtos mbed

Fork of BAT_Type_word by BAT

Committer:
aismail1997
Date:
Tue Dec 05 16:46:37 2017 +0000
Revision:
44:9ccc9f8524fd
Parent:
43:fa8d0db68228
works FINAL

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aismail1997 0:9eda4611081a 1 #include "mbed.h"
aismail1997 6:1aa86ad19af9 2 #include "rtos.h"
aismail1997 9:418a4437a693 3 #include "SDFileSystem.h"
aismail1997 10:21268d8bf979 4 #include "button.h"
aismail1997 19:ceac47be2e64 5 #include "buttonArray.h"
aismail1997 23:23970cf718ee 6 #include <string>
aismail1997 23:23970cf718ee 7 #include <iostream>
aismail1997 28:3c4ebc4fde0f 8 #include "emic2.h"
aismail1997 23:23970cf718ee 9
aismail1997 23:23970cf718ee 10 using namespace std;
aismail1997 3:9ed43e974156 11
aismail1997 13:b80dde24e9bc 12 // DEFINE I/O
aismail1997 0:9eda4611081a 13 PwmOut myservo(p21);
aismail1997 0:9eda4611081a 14 DigitalIn pb1 (p20);
aismail1997 14:581a3b02f4c3 15 PwmOut myservo2(p22);
aismail1997 14:581a3b02f4c3 16 DigitalIn pb2 (p19);
aismail1997 18:d14bf57f435b 17 PwmOut myservo3(p23);
aismail1997 18:d14bf57f435b 18 DigitalIn pb3 (p18);
aismail1997 18:d14bf57f435b 19 PwmOut myservo4(p24);
aismail1997 18:d14bf57f435b 20 DigitalIn pb4 (p17);
aismail1997 18:d14bf57f435b 21 PwmOut myservo5(p25);
aismail1997 18:d14bf57f435b 22 DigitalIn pb5 (p16);
aismail1997 18:d14bf57f435b 23 PwmOut myservo6(p26);
aismail1997 18:d14bf57f435b 24 DigitalIn pb6 (p15);
nnguyen45 43:fa8d0db68228 25 DigitalIn read (p29);
nnguyen45 43:fa8d0db68228 26 DigitalIn submit (p30);
nnguyen45 43:fa8d0db68228 27
aismail1997 18:d14bf57f435b 28
aismail1997 18:d14bf57f435b 29 //DigitalOut led1(LED1);
aismail1997 18:d14bf57f435b 30 //DigitalOut led3(LED3);
aismail1997 22:6931917c70cd 31 //DigitalOut led4(LED4);
aismail1997 14:581a3b02f4c3 32
nnguyen45 32:e87bb6e83072 33 //DigitalIn linpot(p9);
nnguyen45 33:3090ec93b4aa 34 Serial pc(USBTX, USBRX);
aismail1997 23:23970cf718ee 35 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
aismail1997 21:c5df903f068a 36 //button button1(myservo, pb1, linpot);
aismail1997 21:c5df903f068a 37 button button1(myservo, pb1, 1);
aismail1997 21:c5df903f068a 38 button button2(myservo2, pb2, 2);
aismail1997 21:c5df903f068a 39 button button3(myservo3, pb3, 3);
aismail1997 21:c5df903f068a 40 button button4(myservo4, pb4, 4);
aismail1997 21:c5df903f068a 41 button button5(myservo5, pb5, 5);
aismail1997 21:c5df903f068a 42 button button6(myservo6, pb6, 6);
aismail1997 4:cc181f8f2bd1 43
aismail1997 19:ceac47be2e64 44 buttonArray buttonarr(button1, button2, button3, button4, button5, button6);
aismail1997 19:ceac47be2e64 45
aismail1997 28:3c4ebc4fde0f 46 emic2 myTTS(p28, p27); //serial RX,TX pins to emic
aismail1997 28:3c4ebc4fde0f 47
aismail1997 13:b80dde24e9bc 48 // INITIALIZE VARIABLES
aismail1997 13:b80dde24e9bc 49 // add mode, reset buttons
aismail1997 18:d14bf57f435b 50 // Buttons begins in up state
aismail1997 17:08c575082052 51 int state = 0;
aismail1997 18:d14bf57f435b 52 int state2 = 0;
aismail1997 18:d14bf57f435b 53 int state3 = 0;
aismail1997 18:d14bf57f435b 54 int state4 = 0;
aismail1997 18:d14bf57f435b 55 int state5 = 0;
aismail1997 18:d14bf57f435b 56 int state6 = 0;
aismail1997 14:581a3b02f4c3 57 int count = 0;
aismail1997 6:1aa86ad19af9 58
aismail1997 6:1aa86ad19af9 59 // THREADS
aismail1997 13:b80dde24e9bc 60
aismail1997 6:1aa86ad19af9 61 void button_thread()
aismail1997 4:cc181f8f2bd1 62 {
aismail1997 14:581a3b02f4c3 63 while(true) {
aismail1997 19:ceac47be2e64 64 state = button1.updateState();
nnguyen45 33:3090ec93b4aa 65 state6 = button6.updateState();
nnguyen45 33:3090ec93b4aa 66
aismail1997 21:c5df903f068a 67 //led4 = button1.getLp();
aismail1997 19:ceac47be2e64 68 Thread::wait(100); // wait till thread is done
aismail1997 10:21268d8bf979 69 }
aismail1997 14:581a3b02f4c3 70 }
aismail1997 14:581a3b02f4c3 71
aismail1997 14:581a3b02f4c3 72 void button2_thread()
aismail1997 14:581a3b02f4c3 73 {
aismail1997 14:581a3b02f4c3 74 while(true) {
aismail1997 19:ceac47be2e64 75 state2 = button2.updateState();
aismail1997 40:85be3490d3a7 76 //state5 = button5.updateState();
nnguyen45 32:e87bb6e83072 77 Thread::wait(200); // wait till thread is done
aismail1997 10:21268d8bf979 78 }
aismail1997 4:cc181f8f2bd1 79 }
aismail1997 4:cc181f8f2bd1 80
aismail1997 18:d14bf57f435b 81 // thread for the custom button
aismail1997 18:d14bf57f435b 82 void button3_thread()
aismail1997 18:d14bf57f435b 83 {
aismail1997 18:d14bf57f435b 84 while(true) {
aismail1997 19:ceac47be2e64 85 state3 = button3.updateState();
aismail1997 40:85be3490d3a7 86 //state5 = button4.updateState();
nnguyen45 32:e87bb6e83072 87 Thread::wait(200); // wait till thread is done
aismail1997 18:d14bf57f435b 88 }
aismail1997 18:d14bf57f435b 89 }
aismail1997 18:d14bf57f435b 90
aismail1997 18:d14bf57f435b 91 // thread for the custom button
aismail1997 18:d14bf57f435b 92 void button4_thread()
aismail1997 18:d14bf57f435b 93 {
aismail1997 18:d14bf57f435b 94 while(true) {
aismail1997 19:ceac47be2e64 95 state4 = button4.updateState();
nnguyen45 32:e87bb6e83072 96 Thread::wait(200); // wait till thread is done
aismail1997 18:d14bf57f435b 97 }
aismail1997 18:d14bf57f435b 98 }
aismail1997 18:d14bf57f435b 99
aismail1997 18:d14bf57f435b 100 // thread for the custom button
aismail1997 18:d14bf57f435b 101 void button5_thread()
aismail1997 18:d14bf57f435b 102 {
aismail1997 18:d14bf57f435b 103 while(true) {
aismail1997 19:ceac47be2e64 104 state5 = button5.updateState();
nnguyen45 32:e87bb6e83072 105 Thread::wait(200); // wait till thread is done
aismail1997 18:d14bf57f435b 106 }
aismail1997 18:d14bf57f435b 107 }
aismail1997 18:d14bf57f435b 108
aismail1997 14:581a3b02f4c3 109
aismail1997 0:9eda4611081a 110 int main()
aismail1997 0:9eda4611081a 111 {
aismail1997 6:1aa86ad19af9 112 // SETUP
aismail1997 4:cc181f8f2bd1 113 // pull up the pushbutton to prevent bouncing
aismail1997 16:5c91af9b4e7c 114 pb1.mode(PullUp);
aismail1997 16:5c91af9b4e7c 115 pb2.mode(PullUp);
aismail1997 18:d14bf57f435b 116 pb3.mode(PullUp);
aismail1997 18:d14bf57f435b 117 pb4.mode(PullUp);
aismail1997 18:d14bf57f435b 118 pb5.mode(PullUp);
aismail1997 18:d14bf57f435b 119 pb6.mode(PullUp);
nnguyen45 43:fa8d0db68228 120 read.mode(PullUp);
nnguyen45 43:fa8d0db68228 121 submit.mode(PullUp);
aismail1997 30:79cdf893c8b8 122 wait(.001);
aismail1997 15:7e9308d14faa 123
aismail1997 23:23970cf718ee 124 // PARSE INPUT FILE FOR LETTERS AND WORDS
aismail1997 23:23970cf718ee 125 char delimiter = ',';
aismail1997 29:b9bd85682db1 126 // TODO: fix the letters
aismail1997 23:23970cf718ee 127 string letter[2];
aismail1997 23:23970cf718ee 128 string word[2];
aismail1997 23:23970cf718ee 129 char check;
aismail1997 23:23970cf718ee 130 string temp;
aismail1997 23:23970cf718ee 131 string tempword = "";
aismail1997 23:23970cf718ee 132 int counter = 0;
aismail1997 23:23970cf718ee 133 FILE *fp = fopen("/sd/plan.txt", "r"); //create file
aismail1997 23:23970cf718ee 134 if(fp == NULL) {
aismail1997 30:79cdf893c8b8 135 //pc.printf("Could not open file for write\n");
aismail1997 23:23970cf718ee 136 }
aismail1997 23:23970cf718ee 137 check = fgetc(fp); //grabs a char from file
aismail1997 23:23970cf718ee 138 while(check != '\n') { //while not at the end of line for letters
aismail1997 23:23970cf718ee 139 if((check == delimiter) && (temp.length() == 1)) { //at a comma and have a letter stored
aismail1997 23:23970cf718ee 140 letter[counter] = temp; //write letter
aismail1997 23:23970cf718ee 141 counter = counter + 1; //increment counter
aismail1997 23:23970cf718ee 142 } else {
aismail1997 23:23970cf718ee 143 temp = check; //store letter
aismail1997 23:23970cf718ee 144 }
aismail1997 23:23970cf718ee 145 check = fgetc(fp); //grabs next char
aismail1997 23:23970cf718ee 146 }
aismail1997 23:23970cf718ee 147 counter = 0; //reset counter
aismail1997 23:23970cf718ee 148 check = fgetc(fp); //grabs next char
aismail1997 23:23970cf718ee 149 while(!feof(fp)) { //while not at the end of line for words
aismail1997 23:23970cf718ee 150 if(check == delimiter) { //when at the comma at the end of a word
aismail1997 23:23970cf718ee 151 word[counter] = tempword; //write word
aismail1997 23:23970cf718ee 152 tempword = "";
aismail1997 23:23970cf718ee 153 counter = counter + 1; //increment counter
aismail1997 23:23970cf718ee 154 } else {
aismail1997 23:23970cf718ee 155 tempword = tempword + check; //concatenate letters to build word
aismail1997 23:23970cf718ee 156 }
aismail1997 23:23970cf718ee 157 check = fgetc(fp); //grabs next char
aismail1997 23:23970cf718ee 158 }
aismail1997 23:23970cf718ee 159 fclose(fp); //close file
aismail1997 14:581a3b02f4c3 160
nnguyen45 33:3090ec93b4aa 161 Thread t1(button_thread);
nnguyen45 33:3090ec93b4aa 162 Thread t2(button2_thread);
nnguyen45 33:3090ec93b4aa 163 Thread t3(button3_thread);
nnguyen45 33:3090ec93b4aa 164 Thread t4(button4_thread);
nnguyen45 33:3090ec93b4aa 165 Thread t5(button5_thread);
nnguyen45 33:3090ec93b4aa 166 //Thread t6(button6_thread);
nnguyen45 33:3090ec93b4aa 167 t1.start(button_thread);
nnguyen45 33:3090ec93b4aa 168 t2.start(button2_thread);
nnguyen45 33:3090ec93b4aa 169 t3.start(button3_thread);
nnguyen45 33:3090ec93b4aa 170 t4.start(button4_thread);
nnguyen45 33:3090ec93b4aa 171 t5.start(button5_thread);
nnguyen45 33:3090ec93b4aa 172 //t6.start(button6_thread);
aismail1997 34:1248dfa0740b 173
aismail1997 41:560ee7866781 174 //char currletter;
aismail1997 23:23970cf718ee 175 int lettersize = sizeof(letter)/sizeof(letter[0]);
aismail1997 41:560ee7866781 176 //int type = 0;
aismail1997 34:1248dfa0740b 177
aismail1997 28:3c4ebc4fde0f 178 //TEXT-TO-SPEECH LOGIC
nnguyen45 43:fa8d0db68228 179 myTTS.volume(18); //max volume is 18
nnguyen45 31:e5024aeca3a7 180 myTTS.voice(2);
aismail1997 28:3c4ebc4fde0f 181
aismail1997 42:aa46c2be7dbb 182
aismail1997 41:560ee7866781 183 // SETUP
aismail1997 42:aa46c2be7dbb 184 buttonarr.setup(); //Reset servos so that buttons are up for typing mode
aismail1997 41:560ee7866781 185 button1.setMode(1);
aismail1997 41:560ee7866781 186 button2.setMode(1);
aismail1997 41:560ee7866781 187 button3.setMode(1);
aismail1997 41:560ee7866781 188 button4.setMode(1);
aismail1997 41:560ee7866781 189 button5.setMode(1);
aismail1997 41:560ee7866781 190 button6.setMode(1);
aismail1997 41:560ee7866781 191
aismail1997 41:560ee7866781 192
aismail1997 37:d873136dfb12 193 // WORD CODE
aismail1997 37:d873136dfb12 194
aismail1997 37:d873136dfb12 195 // WORD COUNTER
aismail1997 37:d873136dfb12 196 // go through each word
aismail1997 37:d873136dfb12 197 // go through each letter in the word
aismail1997 37:d873136dfb12 198 // get user input
aismail1997 37:d873136dfb12 199 // when buttons reset - save previous letter
aismail1997 37:d873136dfb12 200 // loop until submit button is pressed
aismail1997 37:d873136dfb12 201 // compare words
aismail1997 37:d873136dfb12 202 // check what letters were wrong
aismail1997 37:d873136dfb12 203 // give wrong letters - correct spelling is...
aismail1997 37:d873136dfb12 204
aismail1997 37:d873136dfb12 205 string currword;
aismail1997 37:d873136dfb12 206 int wordsize = sizeof(word)/sizeof(word[0]);
nnguyen45 43:fa8d0db68228 207
nnguyen45 38:68f7f54b723b 208 //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
nnguyen45 43:fa8d0db68228 209 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
aismail1997 37:d873136dfb12 210 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
nnguyen45 43:fa8d0db68228 211 while(read==1) {}
aismail1997 39:8cc80f0b4398 212
aismail1997 39:8cc80f0b4398 213 // loop through each of the words
aismail1997 37:d873136dfb12 214 for(int i = 0; i < wordsize; i++) { //iterate through the word array
nnguyen45 43:fa8d0db68228 215 //int i = 0;
aismail1997 37:d873136dfb12 216 currword = word[i];
aismail1997 40:85be3490d3a7 217 char currword1[3];
aismail1997 41:560ee7866781 218
aismail1997 37:d873136dfb12 219 string presspin = "To type the word ";
aismail1997 37:d873136dfb12 220 presspin = presspin + word[i];
aismail1997 37:d873136dfb12 221 presspin = presspin + ", type letters ";
aismail1997 37:d873136dfb12 222 for (int j = 0; j < currword.length(); j++) {
aismail1997 37:d873136dfb12 223 presspin = presspin + ", "+ currword[j];
aismail1997 40:85be3490d3a7 224 currword1[j] = currword[j];
aismail1997 37:d873136dfb12 225 }
aismail1997 39:8cc80f0b4398 226
aismail1997 39:8cc80f0b4398 227 myTTS.speakf("S%s\r", presspin);
aismail1997 39:8cc80f0b4398 228 myTTS.ready();
aismail1997 39:8cc80f0b4398 229
aismail1997 39:8cc80f0b4398 230 // loop through each of the letters
aismail1997 39:8cc80f0b4398 231 int incorrect = 1;
aismail1997 39:8cc80f0b4398 232 char userinput;
aismail1997 37:d873136dfb12 233
aismail1997 39:8cc80f0b4398 234 // while(!submit)
aismail1997 39:8cc80f0b4398 235 while (incorrect == 1) {
aismail1997 39:8cc80f0b4398 236
aismail1997 40:85be3490d3a7 237 char wordinput[3];
aismail1997 39:8cc80f0b4398 238 // replace for loop with submit
aismail1997 39:8cc80f0b4398 239 // while (!submit) {
aismail1997 44:9ccc9f8524fd 240 //int lettercount = 0;
aismail1997 44:9ccc9f8524fd 241 //int mysubmit = 0;
aismail1997 44:9ccc9f8524fd 242 //while(submit==0) {
aismail1997 44:9ccc9f8524fd 243 for (int j = 0; j < 3; j++) {
aismail1997 39:8cc80f0b4398 244 // check if all buttons were pressed
aismail1997 37:d873136dfb12 245
aismail1997 39:8cc80f0b4398 246 char* braille;
aismail1997 39:8cc80f0b4398 247 char* oldbraille;
aismail1997 37:d873136dfb12 248
nnguyen45 43:fa8d0db68228 249 wait(0.5);
aismail1997 39:8cc80f0b4398 250
aismail1997 39:8cc80f0b4398 251 sprintf(braille, "%d%d%d%d%d%d", !button1.getPress(), !button2.getPress(),
aismail1997 39:8cc80f0b4398 252 !button3.getPress(), !button4.getPress(), !button5.getPress(), !button6.getPress());
aismail1997 37:d873136dfb12 253
aismail1997 39:8cc80f0b4398 254 // wait for input
aismail1997 44:9ccc9f8524fd 255 while ((strcmp(braille, "111111") == 0)) {
aismail1997 39:8cc80f0b4398 256 sprintf(braille, "%d%d%d%d%d%d", !button1.getPress(), !button2.getPress(),
aismail1997 39:8cc80f0b4398 257 !button3.getPress(), !button4.getPress(), !button5.getPress(), !button6.getPress());
aismail1997 39:8cc80f0b4398 258 }
aismail1997 44:9ccc9f8524fd 259 /*if(submit==1) {
nnguyen45 43:fa8d0db68228 260 pc.printf("submit\n");
nnguyen45 43:fa8d0db68228 261 break;
nnguyen45 43:fa8d0db68228 262 }*/
aismail1997 19:ceac47be2e64 263
aismail1997 39:8cc80f0b4398 264 // while buttons have not been released
aismail1997 39:8cc80f0b4398 265 int oldsum = 0;
aismail1997 39:8cc80f0b4398 266 int sum = 0;
aismail1997 39:8cc80f0b4398 267 while ((strcmp(braille, "111111") != 0) && (oldsum <= sum)) {
aismail1997 39:8cc80f0b4398 268 oldbraille = braille;
aismail1997 39:8cc80f0b4398 269 oldsum = sum;
aismail1997 39:8cc80f0b4398 270 sprintf(braille, "%d%d%d%d%d%d", !button1.getPress(), !button2.getPress(),
aismail1997 39:8cc80f0b4398 271 !button3.getPress(), !button4.getPress(), !button5.getPress(), !button6.getPress());
aismail1997 39:8cc80f0b4398 272 sum = button1.getPress() + button2.getPress() + button3.getPress() + button4.getPress()
aismail1997 39:8cc80f0b4398 273 + button5.getPress() + button6.getPress();
aismail1997 37:d873136dfb12 274 }
aismail1997 22:6931917c70cd 275
aismail1997 39:8cc80f0b4398 276 // at the end of this, oldbraille contains the letter representation
aismail1997 39:8cc80f0b4398 277 //pc.printf("%s ", oldbraille);
nnguyen45 43:fa8d0db68228 278 //if(mysubmit==0) {
aismail1997 39:8cc80f0b4398 279 userinput = buttonarr.checkVal(oldbraille);
aismail1997 44:9ccc9f8524fd 280 //pc.printf("%c ", userinput);
nnguyen45 43:fa8d0db68228 281 //wordinput[j] = userinput;
nnguyen45 43:fa8d0db68228 282 //if(j <= 2) {
aismail1997 39:8cc80f0b4398 283 wordinput[j] = userinput;
nnguyen45 43:fa8d0db68228 284 //}
aismail1997 44:9ccc9f8524fd 285 //lettercount++;
aismail1997 39:8cc80f0b4398 286 }
aismail1997 44:9ccc9f8524fd 287
aismail1997 44:9ccc9f8524fd 288 //char* braille;
aismail1997 44:9ccc9f8524fd 289 while (submit == 0) {
aismail1997 44:9ccc9f8524fd 290 /*sprintf(braille, "%d%d%d%d%d%d", !button1.getPress(), !button2.getPress(),
aismail1997 44:9ccc9f8524fd 291 !button3.getPress(), !button4.getPress(), !button5.getPress(), !button6.getPress());
aismail1997 44:9ccc9f8524fd 292 userinput = buttonarr.checkVal(braille);
aismail1997 44:9ccc9f8524fd 293 if (userinput != 'Z') {
aismail1997 44:9ccc9f8524fd 294 lettercount++;
aismail1997 44:9ccc9f8524fd 295 }*/
aismail1997 44:9ccc9f8524fd 296 }
aismail1997 44:9ccc9f8524fd 297
nnguyen45 43:fa8d0db68228 298 //pc.printf("submit button pressed\n");
aismail1997 39:8cc80f0b4398 299
aismail1997 39:8cc80f0b4398 300 // check result of words
aismail1997 39:8cc80f0b4398 301 incorrect = 0;
aismail1997 40:85be3490d3a7 302 for (int k = 0; k < 3; k++) {
aismail1997 44:9ccc9f8524fd 303 /*if (lettercount > 3) {
aismail1997 44:9ccc9f8524fd 304 incorrect = 1;
aismail1997 44:9ccc9f8524fd 305 }*/
aismail1997 40:85be3490d3a7 306 if (currword1[k] != wordinput[k]) { // if any letter doesn't match
aismail1997 39:8cc80f0b4398 307 incorrect = 1; // spelling is wrong
aismail1997 37:d873136dfb12 308 }
aismail1997 37:d873136dfb12 309 }
aismail1997 37:d873136dfb12 310
aismail1997 39:8cc80f0b4398 311 if (incorrect == 1) {
aismail1997 39:8cc80f0b4398 312 myTTS.speakf("SPlease try again! %s\r", presspin);
aismail1997 39:8cc80f0b4398 313 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
aismail1997 39:8cc80f0b4398 314 } else {
aismail1997 39:8cc80f0b4398 315 myTTS.speakf("SGood job!\r");
aismail1997 39:8cc80f0b4398 316 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
aismail1997 39:8cc80f0b4398 317 }
nnguyen45 38:68f7f54b723b 318 }
aismail1997 39:8cc80f0b4398 319 }
nnguyen45 43:fa8d0db68228 320 myTTS.speakf("SThis is the end of the module.\r"); // Send the desired string to convert to speech
nnguyen45 43:fa8d0db68228 321 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
nnguyen45 43:fa8d0db68228 322
aismail1997 37:d873136dfb12 323 // MAIN THREAD
aismail1997 37:d873136dfb12 324 while(true) {
aismail1997 22:6931917c70cd 325
aismail1997 14:581a3b02f4c3 326 Thread::wait(500); // wait till thread is done
aismail1997 0:9eda4611081a 327 }
aismail1997 0:9eda4611081a 328 }