Working read code with mode button

Dependencies:   SDFileSystem emic2 mbed-rtos mbed

Fork of BAT_senior_design_Testnew by BAT

Committer:
nnguyen45
Date:
Mon Dec 04 19:58:40 2017 +0000
Revision:
0:5887cb744114
Child:
42:44a898adef26
Working read code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nnguyen45 0:5887cb744114 1 #include "mbed.h"
nnguyen45 0:5887cb744114 2 #include "rtos.h"
nnguyen45 0:5887cb744114 3 #include "SDFileSystem.h"
nnguyen45 0:5887cb744114 4 #include "button.h"
nnguyen45 0:5887cb744114 5 #include "buttonArray.h"
nnguyen45 0:5887cb744114 6 #include <string>
nnguyen45 0:5887cb744114 7 #include <iostream>
nnguyen45 0:5887cb744114 8 #include "emic2.h"
nnguyen45 0:5887cb744114 9
nnguyen45 0:5887cb744114 10 using namespace std;
nnguyen45 0:5887cb744114 11
nnguyen45 0:5887cb744114 12 // DEFINE I/O
nnguyen45 0:5887cb744114 13 PwmOut myservo(p21);
nnguyen45 0:5887cb744114 14 DigitalIn pb1 (p20);
nnguyen45 0:5887cb744114 15 PwmOut myservo2(p22);
nnguyen45 0:5887cb744114 16 DigitalIn pb2 (p19);
nnguyen45 0:5887cb744114 17 PwmOut myservo3(p23);
nnguyen45 0:5887cb744114 18 DigitalIn pb3 (p18);
nnguyen45 0:5887cb744114 19 PwmOut myservo4(p24);
nnguyen45 0:5887cb744114 20 DigitalIn pb4 (p17);
nnguyen45 0:5887cb744114 21 PwmOut myservo5(p25);
nnguyen45 0:5887cb744114 22 DigitalIn pb5 (p16);
nnguyen45 0:5887cb744114 23 PwmOut myservo6(p26);
nnguyen45 0:5887cb744114 24 DigitalIn pb6 (p15);
nnguyen45 0:5887cb744114 25
nnguyen45 0:5887cb744114 26 Serial pc(USBTX, USBRX);
nnguyen45 0:5887cb744114 27 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
nnguyen45 0:5887cb744114 28 button button1(myservo, pb1, 1);
nnguyen45 0:5887cb744114 29 button button2(myservo2, pb2, 2);
nnguyen45 0:5887cb744114 30 button button3(myservo3, pb3, 3);
nnguyen45 0:5887cb744114 31 button button4(myservo4, pb4, 4);
nnguyen45 0:5887cb744114 32 button button5(myservo5, pb5, 5);
nnguyen45 0:5887cb744114 33 button button6(myservo6, pb6, 6);
nnguyen45 0:5887cb744114 34
nnguyen45 0:5887cb744114 35 buttonArray buttonarr(button1, button2, button3, button4, button5, button6);
nnguyen45 0:5887cb744114 36
nnguyen45 0:5887cb744114 37 emic2 myTTS(p28, p27); //serial RX,TX pins to emic
nnguyen45 0:5887cb744114 38
nnguyen45 0:5887cb744114 39 // INITIALIZE VARIABLES
nnguyen45 0:5887cb744114 40 // add mode, reset buttons
nnguyen45 0:5887cb744114 41 int start = 0;
nnguyen45 0:5887cb744114 42 int submit = 0;
nnguyen45 0:5887cb744114 43 // Buttons begins in up state
nnguyen45 0:5887cb744114 44 int state = 0;
nnguyen45 0:5887cb744114 45 int state2 = 0;
nnguyen45 0:5887cb744114 46 int state3 = 0;
nnguyen45 0:5887cb744114 47 int state4 = 0;
nnguyen45 0:5887cb744114 48 int state5 = 0;
nnguyen45 0:5887cb744114 49 int state6 = 0;
nnguyen45 0:5887cb744114 50 int count = 0;
nnguyen45 0:5887cb744114 51
nnguyen45 0:5887cb744114 52 // THREADS
nnguyen45 0:5887cb744114 53 void button_thread()
nnguyen45 0:5887cb744114 54 {
nnguyen45 0:5887cb744114 55 while(true) {
nnguyen45 0:5887cb744114 56 state = button1.updateState();
nnguyen45 0:5887cb744114 57 state6 = button6.updateState();
nnguyen45 0:5887cb744114 58 Thread::wait(100); // wait till thread is done
nnguyen45 0:5887cb744114 59 }
nnguyen45 0:5887cb744114 60 }
nnguyen45 0:5887cb744114 61
nnguyen45 0:5887cb744114 62 void button2_thread()
nnguyen45 0:5887cb744114 63 {
nnguyen45 0:5887cb744114 64 while(true) {
nnguyen45 0:5887cb744114 65 state2 = button2.updateState();
nnguyen45 0:5887cb744114 66 Thread::wait(200); // wait till thread is done
nnguyen45 0:5887cb744114 67 }
nnguyen45 0:5887cb744114 68 }
nnguyen45 0:5887cb744114 69
nnguyen45 0:5887cb744114 70 void button3_thread()
nnguyen45 0:5887cb744114 71 {
nnguyen45 0:5887cb744114 72 while(true) {
nnguyen45 0:5887cb744114 73 state3 = button3.updateState();
nnguyen45 0:5887cb744114 74 Thread::wait(200); // wait till thread is done
nnguyen45 0:5887cb744114 75 }
nnguyen45 0:5887cb744114 76 }
nnguyen45 0:5887cb744114 77
nnguyen45 0:5887cb744114 78 void button4_thread()
nnguyen45 0:5887cb744114 79 {
nnguyen45 0:5887cb744114 80 while(true) {
nnguyen45 0:5887cb744114 81 state4 = button4.updateState();
nnguyen45 0:5887cb744114 82 Thread::wait(200); // wait till thread is done
nnguyen45 0:5887cb744114 83 }
nnguyen45 0:5887cb744114 84 }
nnguyen45 0:5887cb744114 85
nnguyen45 0:5887cb744114 86 void button5_thread()
nnguyen45 0:5887cb744114 87 {
nnguyen45 0:5887cb744114 88 while(true) {
nnguyen45 0:5887cb744114 89 state5 = button5.updateState();
nnguyen45 0:5887cb744114 90 Thread::wait(200); // wait till thread is done
nnguyen45 0:5887cb744114 91 }
nnguyen45 0:5887cb744114 92 }
nnguyen45 0:5887cb744114 93
nnguyen45 0:5887cb744114 94 void button6_thread()
nnguyen45 0:5887cb744114 95 {
nnguyen45 0:5887cb744114 96 while(true) {
nnguyen45 0:5887cb744114 97 state6 = button6.updateState();
nnguyen45 0:5887cb744114 98 Thread::wait(200); // wait till thread is done
nnguyen45 0:5887cb744114 99 }
nnguyen45 0:5887cb744114 100 }
nnguyen45 0:5887cb744114 101
nnguyen45 0:5887cb744114 102 void submit_thread()
nnguyen45 0:5887cb744114 103 {
nnguyen45 0:5887cb744114 104 Thread::wait(500); // wait till thread is done
nnguyen45 0:5887cb744114 105 }
nnguyen45 0:5887cb744114 106
nnguyen45 0:5887cb744114 107 void start_thread()
nnguyen45 0:5887cb744114 108 {
nnguyen45 0:5887cb744114 109 start = 1;
nnguyen45 0:5887cb744114 110 Thread::wait(500); // wait till thread is done
nnguyen45 0:5887cb744114 111 }
nnguyen45 0:5887cb744114 112
nnguyen45 0:5887cb744114 113 int main()
nnguyen45 0:5887cb744114 114 {
nnguyen45 0:5887cb744114 115 // SETUP; pull up the pushbutton to prevent bouncing
nnguyen45 0:5887cb744114 116 pb1.mode(PullUp);
nnguyen45 0:5887cb744114 117 pb2.mode(PullUp);
nnguyen45 0:5887cb744114 118 pb3.mode(PullUp);
nnguyen45 0:5887cb744114 119 pb4.mode(PullUp);
nnguyen45 0:5887cb744114 120 pb5.mode(PullUp);
nnguyen45 0:5887cb744114 121 pb6.mode(PullUp);
nnguyen45 0:5887cb744114 122 wait(.001);
nnguyen45 0:5887cb744114 123
nnguyen45 0:5887cb744114 124 // servo setup up function; servos begin at 30 degrees
nnguyen45 0:5887cb744114 125 buttonarr.setup();
nnguyen45 0:5887cb744114 126
nnguyen45 0:5887cb744114 127 // PARSE INPUT FILE FOR LETTERS AND WORDS
nnguyen45 0:5887cb744114 128 char delimiter = ',';
nnguyen45 0:5887cb744114 129 string letter[2];
nnguyen45 0:5887cb744114 130 string word[2];
nnguyen45 0:5887cb744114 131 char check;
nnguyen45 0:5887cb744114 132 string temp;
nnguyen45 0:5887cb744114 133 string tempword = "";
nnguyen45 0:5887cb744114 134 int counter = 0;
nnguyen45 0:5887cb744114 135 FILE *fp = fopen("/sd/plan.txt", "r"); //create file
nnguyen45 0:5887cb744114 136 check = fgetc(fp); //grabs a char from file
nnguyen45 0:5887cb744114 137 while(check != '\n') { //while not at the end of line for letters
nnguyen45 0:5887cb744114 138 if((check == delimiter) && (temp.length() == 1)) { //at a comma and have a letter stored
nnguyen45 0:5887cb744114 139 letter[counter] = temp; //write letter
nnguyen45 0:5887cb744114 140 counter = counter + 1; //increment counter
nnguyen45 0:5887cb744114 141 } else {
nnguyen45 0:5887cb744114 142 temp = check; //store letter
nnguyen45 0:5887cb744114 143 }
nnguyen45 0:5887cb744114 144 check = fgetc(fp); //grabs next char
nnguyen45 0:5887cb744114 145 }
nnguyen45 0:5887cb744114 146 counter = 0; //reset counter
nnguyen45 0:5887cb744114 147 check = fgetc(fp); //grabs next char
nnguyen45 0:5887cb744114 148 while(!feof(fp)) { //while not at the end of line for words
nnguyen45 0:5887cb744114 149 if(check == delimiter) { //when at the comma at the end of a word
nnguyen45 0:5887cb744114 150 word[counter] = tempword; //write word
nnguyen45 0:5887cb744114 151 tempword = "";
nnguyen45 0:5887cb744114 152 counter = counter + 1; //increment counter
nnguyen45 0:5887cb744114 153 } else {
nnguyen45 0:5887cb744114 154 tempword = tempword + check; //concatenate letters to build word
nnguyen45 0:5887cb744114 155 }
nnguyen45 0:5887cb744114 156 check = fgetc(fp); //grabs next char
nnguyen45 0:5887cb744114 157 }
nnguyen45 0:5887cb744114 158 fclose(fp); //close file
nnguyen45 0:5887cb744114 159
nnguyen45 0:5887cb744114 160 //INITIALIZE THREADS
nnguyen45 0:5887cb744114 161 Thread t1(button_thread);
nnguyen45 0:5887cb744114 162 Thread t2(button2_thread);
nnguyen45 0:5887cb744114 163 Thread t3(button3_thread);
nnguyen45 0:5887cb744114 164 Thread t4(button4_thread);
nnguyen45 0:5887cb744114 165 Thread t5(button5_thread);
nnguyen45 0:5887cb744114 166 t1.start(button_thread);
nnguyen45 0:5887cb744114 167 t2.start(button2_thread);
nnguyen45 0:5887cb744114 168 t3.start(button3_thread);
nnguyen45 0:5887cb744114 169 t4.start(button4_thread);
nnguyen45 0:5887cb744114 170 t5.start(button5_thread);
nnguyen45 0:5887cb744114 171
nnguyen45 0:5887cb744114 172 char currletter;
nnguyen45 0:5887cb744114 173 int lettersize = sizeof(letter)/sizeof(letter[0]);
nnguyen45 0:5887cb744114 174 int type = 0;
nnguyen45 0:5887cb744114 175
nnguyen45 0:5887cb744114 176 //TEXT-TO-SPEECH LOGIC
nnguyen45 0:5887cb744114 177 myTTS.volume(1); //max volume is 18
nnguyen45 0:5887cb744114 178 myTTS.voice(2);
nnguyen45 0:5887cb744114 179
nnguyen45 0:5887cb744114 180 char* braille;
nnguyen45 0:5887cb744114 181 char userinput;
nnguyen45 0:5887cb744114 182
nnguyen45 0:5887cb744114 183 // INITIAL RESET
nnguyen45 0:5887cb744114 184 if (type == 0) {
nnguyen45 0:5887cb744114 185 int reset = 1;
nnguyen45 0:5887cb744114 186 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");
nnguyen45 0:5887cb744114 187 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
nnguyen45 0:5887cb744114 188 while(reset == 1) {
nnguyen45 0:5887cb744114 189 wait(3);
nnguyen45 0:5887cb744114 190 sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(),
nnguyen45 0:5887cb744114 191 button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress());
nnguyen45 0:5887cb744114 192 userinput = buttonarr.checkVal(braille);
nnguyen45 0:5887cb744114 193 if(userinput == 'Z') {
nnguyen45 0:5887cb744114 194 reset = 0;
nnguyen45 0:5887cb744114 195 } else {
nnguyen45 0:5887cb744114 196 myTTS.speakf("SSetup failed. Please try again.\r");
nnguyen45 0:5887cb744114 197 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
nnguyen45 0:5887cb744114 198 }
nnguyen45 0:5887cb744114 199 }
nnguyen45 0:5887cb744114 200 }
nnguyen45 0:5887cb744114 201
nnguyen45 0:5887cb744114 202 for(int i = 0; i < lettersize; i++) { //iterate through the letter array
nnguyen45 0:5887cb744114 203 char currletter = letter[i][0];
nnguyen45 0:5887cb744114 204 int* pinsup = buttonarr.pinsUp(currletter);
nnguyen45 0:5887cb744114 205 int currpress;
nnguyen45 0:5887cb744114 206 int numpinsups = pinsup[0]; // size of array is first element of pinsup
nnguyen45 0:5887cb744114 207 string presspin = "STo write the letter ";
nnguyen45 0:5887cb744114 208 presspin = presspin + letter[i];
nnguyen45 0:5887cb744114 209 presspin = presspin + ", press buttons";
nnguyen45 0:5887cb744114 210
nnguyen45 0:5887cb744114 211 for (int j = 1; j < numpinsups; j++) { // get what pins to press
nnguyen45 0:5887cb744114 212 currpress = pinsup[j];
nnguyen45 0:5887cb744114 213 switch (currpress) {
nnguyen45 0:5887cb744114 214 case 1:
nnguyen45 0:5887cb744114 215 presspin = presspin + " 1,";
nnguyen45 0:5887cb744114 216 break;
nnguyen45 0:5887cb744114 217 case 2:
nnguyen45 0:5887cb744114 218 presspin = presspin + " 2,";
nnguyen45 0:5887cb744114 219 break;
nnguyen45 0:5887cb744114 220 case 3:
nnguyen45 0:5887cb744114 221 presspin = presspin + " 3,";
nnguyen45 0:5887cb744114 222 break;
nnguyen45 0:5887cb744114 223 case 4:
nnguyen45 0:5887cb744114 224 presspin = presspin + " 4,";
nnguyen45 0:5887cb744114 225 break;
nnguyen45 0:5887cb744114 226 case 5:
nnguyen45 0:5887cb744114 227 presspin = presspin + " 5,";
nnguyen45 0:5887cb744114 228 break;
nnguyen45 0:5887cb744114 229 case 6:
nnguyen45 0:5887cb744114 230 presspin = presspin + " 6,";
nnguyen45 0:5887cb744114 231 break;
nnguyen45 0:5887cb744114 232 }
nnguyen45 0:5887cb744114 233 }
nnguyen45 0:5887cb744114 234 myTTS.speakf("%s\r",presspin);
nnguyen45 0:5887cb744114 235 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
nnguyen45 0:5887cb744114 236 wait(2);
nnguyen45 0:5887cb744114 237
nnguyen45 0:5887cb744114 238 char* braille;
nnguyen45 0:5887cb744114 239 char userinput;
nnguyen45 0:5887cb744114 240 char* oldbraille;
nnguyen45 0:5887cb744114 241 if (type == 0) {
nnguyen45 0:5887cb744114 242 sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(),
nnguyen45 0:5887cb744114 243 button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress());
nnguyen45 0:5887cb744114 244 userinput = buttonarr.checkVal(braille);
nnguyen45 0:5887cb744114 245 } else {
nnguyen45 0:5887cb744114 246 // TYPE MODE
nnguyen45 0:5887cb744114 247 sprintf(braille, "%d%d%d%d%d%d", !button1.getPress(), !button2.getPress(),
nnguyen45 0:5887cb744114 248 !button3.getPress(), !button4.getPress(), !button5.getPress(), !button6.getPress());
nnguyen45 0:5887cb744114 249 userinput = buttonarr.checkVal(braille);
nnguyen45 0:5887cb744114 250 oldbraille = braille;
nnguyen45 0:5887cb744114 251 }
nnguyen45 0:5887cb744114 252
nnguyen45 0:5887cb744114 253 int currwrong;
nnguyen45 0:5887cb744114 254 string wrongpin;
nnguyen45 0:5887cb744114 255 // check result
nnguyen45 0:5887cb744114 256 pc.printf("\n %c %c", userinput, currletter);
nnguyen45 0:5887cb744114 257 int* wrongpins = buttonarr.wrongPins(braille, currletter);
nnguyen45 0:5887cb744114 258 int test = 1;
nnguyen45 0:5887cb744114 259 while(test == 1) {
nnguyen45 0:5887cb744114 260 wrongpin = "SYour answer is incorrect. Buttons";
nnguyen45 0:5887cb744114 261 for (int j = 1; j < wrongpins[0]; j++) { // get what pins are wrong
nnguyen45 0:5887cb744114 262 currwrong = wrongpins[j];
nnguyen45 0:5887cb744114 263 switch (currwrong) {
nnguyen45 0:5887cb744114 264 case 1:
nnguyen45 0:5887cb744114 265 wrongpin = wrongpin + " 1,";
nnguyen45 0:5887cb744114 266 break;
nnguyen45 0:5887cb744114 267 case 2:
nnguyen45 0:5887cb744114 268 wrongpin = wrongpin + " 2,";
nnguyen45 0:5887cb744114 269 break;
nnguyen45 0:5887cb744114 270 case 3:
nnguyen45 0:5887cb744114 271 wrongpin = wrongpin + " 3,";
nnguyen45 0:5887cb744114 272 break;
nnguyen45 0:5887cb744114 273 case 4:
nnguyen45 0:5887cb744114 274 wrongpin = wrongpin + " 4,";
nnguyen45 0:5887cb744114 275 break;
nnguyen45 0:5887cb744114 276 case 5:
nnguyen45 0:5887cb744114 277 wrongpin = wrongpin + " 5,";
nnguyen45 0:5887cb744114 278 break;
nnguyen45 0:5887cb744114 279 case 6:
nnguyen45 0:5887cb744114 280 wrongpin = wrongpin + " 6,";
nnguyen45 0:5887cb744114 281 break;
nnguyen45 0:5887cb744114 282 }
nnguyen45 0:5887cb744114 283 }
nnguyen45 0:5887cb744114 284 if (wrongpins[0] > 1) {
nnguyen45 0:5887cb744114 285 wrongpin = wrongpin + " are wrong. Please try again.";
nnguyen45 0:5887cb744114 286 myTTS.speakf("%s\r",wrongpin);
nnguyen45 0:5887cb744114 287 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
nnguyen45 0:5887cb744114 288 wait(2);
nnguyen45 0:5887cb744114 289 //UPDATE THE PINS THAT ARE WRONG BY CHECKING AGAIN BELOW
nnguyen45 0:5887cb744114 290 sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(),
nnguyen45 0:5887cb744114 291 button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress());
nnguyen45 0:5887cb744114 292 userinput = buttonarr.checkVal(braille);
nnguyen45 0:5887cb744114 293
nnguyen45 0:5887cb744114 294 // check result
nnguyen45 0:5887cb744114 295 wrongpins = buttonarr.wrongPins(braille, currletter);
nnguyen45 0:5887cb744114 296 } else {
nnguyen45 0:5887cb744114 297 test = 0;
nnguyen45 0:5887cb744114 298 myTTS.speakf("SGood job!\r");
nnguyen45 0:5887cb744114 299 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
nnguyen45 0:5887cb744114 300 }
nnguyen45 0:5887cb744114 301 }
nnguyen45 0:5887cb744114 302
nnguyen45 0:5887cb744114 303 if (type == 0) {
nnguyen45 0:5887cb744114 304 int reset = 1;
nnguyen45 0:5887cb744114 305 myTTS.speakf("SNow reset by pressing down all the buttons.\r");
nnguyen45 0:5887cb744114 306 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
nnguyen45 0:5887cb744114 307 while(reset == 1) {
nnguyen45 0:5887cb744114 308 char* braille;
nnguyen45 0:5887cb744114 309 sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(),
nnguyen45 0:5887cb744114 310 button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress());
nnguyen45 0:5887cb744114 311 userinput = buttonarr.checkVal(braille);
nnguyen45 0:5887cb744114 312 if(userinput == 'Z') {
nnguyen45 0:5887cb744114 313 reset = 0;
nnguyen45 0:5887cb744114 314 } else {
nnguyen45 0:5887cb744114 315 wait(2);
nnguyen45 0:5887cb744114 316 myTTS.speakf("SPlease try again.\r");
nnguyen45 0:5887cb744114 317 myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
nnguyen45 0:5887cb744114 318 }
nnguyen45 0:5887cb744114 319 }
nnguyen45 0:5887cb744114 320 }
nnguyen45 0:5887cb744114 321 }
nnguyen45 0:5887cb744114 322
nnguyen45 0:5887cb744114 323 // MAIN THREAD
nnguyen45 0:5887cb744114 324 while(true) {
nnguyen45 0:5887cb744114 325
nnguyen45 0:5887cb744114 326 Thread::wait(500); // wait till thread is done
nnguyen45 0:5887cb744114 327 }
nnguyen45 0:5887cb744114 328 }