Working reset, flipped logic

Dependencies:   SDFileSystem emic2 mbed-rtos mbed

Fork of BAT_senior_design_Nhi by BAT

Committer:
aismail1997
Date:
Fri Nov 17 15:17:36 2017 +0000
Revision:
30:79cdf893c8b8
Parent:
29:b9bd85682db1
Child:
31:e5024aeca3a7
buttonarray functions work!

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