Working read code with mode button

Dependencies:   SDFileSystem emic2 mbed-rtos mbed

Fork of BAT_senior_design_Testnew by BAT

Committer:
nnguyen45
Date:
Fri Nov 10 16:48:26 2017 +0000
Revision:
26:4b2940eec275
Parent:
25:23970cf718ee
Child:
27:0ed2a9cad0e1
text to speech logic with errors

Who changed what in which revision?

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