Working read code with mode button

Dependencies:   SDFileSystem emic2 mbed-rtos mbed

Fork of BAT_senior_design_Testnew by BAT

Committer:
aismail1997
Date:
Fri Nov 10 16:22:04 2017 +0000
Revision:
29:b2e53ce54b3e
Parent:
25:23970cf718ee
Child:
30:3c4ebc4fde0f
added iterators for the arrays

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>
aismail1997 25:23970cf718ee 9
aismail1997 25:23970cf718ee 10 using namespace std;
aismail1997 5:9ed43e974156 11
aismail1997 15:b80dde24e9bc 12 // DEFINE I/O
aismail1997 2:9eda4611081a 13 PwmOut myservo(p21);
aismail1997 2:9eda4611081a 14 DigitalIn pb1 (p20);
aismail1997 16:581a3b02f4c3 15 PwmOut myservo2(p22);
aismail1997 16:581a3b02f4c3 16 DigitalIn pb2 (p19);
aismail1997 20:d14bf57f435b 17 PwmOut myservo3(p23);
aismail1997 20:d14bf57f435b 18 DigitalIn pb3 (p18);
aismail1997 20:d14bf57f435b 19 PwmOut myservo4(p24);
aismail1997 20:d14bf57f435b 20 DigitalIn pb4 (p17);
aismail1997 20:d14bf57f435b 21 PwmOut myservo5(p25);
aismail1997 20:d14bf57f435b 22 DigitalIn pb5 (p16);
aismail1997 20:d14bf57f435b 23 PwmOut myservo6(p26);
aismail1997 20:d14bf57f435b 24 DigitalIn pb6 (p15);
aismail1997 20:d14bf57f435b 25
aismail1997 20:d14bf57f435b 26 //DigitalOut led1(LED1);
aismail1997 20:d14bf57f435b 27 //DigitalOut led3(LED3);
aismail1997 24:6931917c70cd 28 //DigitalOut led4(LED4);
aismail1997 16:581a3b02f4c3 29
aismail1997 24:6931917c70cd 30 DigitalIn linpot(p9);
aismail1997 25:23970cf718ee 31 Serial pc(USBTX, USBRX);
aismail1997 25:23970cf718ee 32 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
aismail1997 16:581a3b02f4c3 33 //AnalogOut DACout(p26);
aismail1997 16:581a3b02f4c3 34 //wave_player waver(&DACout);
aismail1997 23:c5df903f068a 35 //button button1(myservo, pb1, linpot);
aismail1997 23:c5df903f068a 36 button button1(myservo, pb1, 1);
aismail1997 23:c5df903f068a 37 button button2(myservo2, pb2, 2);
aismail1997 23:c5df903f068a 38 button button3(myservo3, pb3, 3);
aismail1997 23:c5df903f068a 39 button button4(myservo4, pb4, 4);
aismail1997 23:c5df903f068a 40 button button5(myservo5, pb5, 5);
aismail1997 23:c5df903f068a 41 button button6(myservo6, pb6, 6);
aismail1997 6:cc181f8f2bd1 42
aismail1997 21:ceac47be2e64 43 buttonArray buttonarr(button1, button2, button3, button4, button5, button6);
aismail1997 21:ceac47be2e64 44
aismail1997 15:b80dde24e9bc 45 // INITIALIZE VARIABLES
aismail1997 15:b80dde24e9bc 46 // add mode, reset buttons
aismail1997 11:418a4437a693 47 int start = 0;
aismail1997 11:418a4437a693 48 int submit = 0;
aismail1997 20:d14bf57f435b 49 // Buttons begins in up state
aismail1997 19:08c575082052 50 int state = 0;
aismail1997 20:d14bf57f435b 51 int state2 = 0;
aismail1997 20:d14bf57f435b 52 int state3 = 0;
aismail1997 20:d14bf57f435b 53 int state4 = 0;
aismail1997 20:d14bf57f435b 54 int state5 = 0;
aismail1997 20:d14bf57f435b 55 int state6 = 0;
aismail1997 16:581a3b02f4c3 56 int count = 0;
aismail1997 8:1aa86ad19af9 57
aismail1997 8:1aa86ad19af9 58 // THREADS
aismail1997 15:b80dde24e9bc 59
aismail1997 8:1aa86ad19af9 60 void button_thread()
aismail1997 6:cc181f8f2bd1 61 {
aismail1997 16:581a3b02f4c3 62 while(true) {
aismail1997 21:ceac47be2e64 63 state = button1.updateState();
aismail1997 23:c5df903f068a 64 //led4 = button1.getLp();
aismail1997 21:ceac47be2e64 65 Thread::wait(100); // wait till thread is done
aismail1997 12:21268d8bf979 66 }
aismail1997 16:581a3b02f4c3 67 }
aismail1997 16:581a3b02f4c3 68
aismail1997 16:581a3b02f4c3 69 void button2_thread()
aismail1997 16:581a3b02f4c3 70 {
aismail1997 16:581a3b02f4c3 71 while(true) {
aismail1997 21:ceac47be2e64 72 state2 = button2.updateState();
aismail1997 16:581a3b02f4c3 73 Thread::wait(100); // wait till thread is done
aismail1997 12:21268d8bf979 74 }
aismail1997 6:cc181f8f2bd1 75 }
aismail1997 6:cc181f8f2bd1 76
aismail1997 20:d14bf57f435b 77 // thread for the custom button
aismail1997 20:d14bf57f435b 78 void button3_thread()
aismail1997 20:d14bf57f435b 79 {
aismail1997 20:d14bf57f435b 80 while(true) {
aismail1997 21:ceac47be2e64 81 state3 = button3.updateState();
aismail1997 21:ceac47be2e64 82 Thread::wait(100); // wait till thread is done
aismail1997 20:d14bf57f435b 83 }
aismail1997 20:d14bf57f435b 84 }
aismail1997 20:d14bf57f435b 85
aismail1997 20:d14bf57f435b 86 // thread for the custom button
aismail1997 20:d14bf57f435b 87 void button4_thread()
aismail1997 20:d14bf57f435b 88 {
aismail1997 20:d14bf57f435b 89 while(true) {
aismail1997 21:ceac47be2e64 90 state4 = button4.updateState();
aismail1997 21:ceac47be2e64 91 Thread::wait(100); // wait till thread is done
aismail1997 20:d14bf57f435b 92 }
aismail1997 20:d14bf57f435b 93 }
aismail1997 20:d14bf57f435b 94
aismail1997 20:d14bf57f435b 95 // thread for the custom button
aismail1997 20:d14bf57f435b 96 void button5_thread()
aismail1997 20:d14bf57f435b 97 {
aismail1997 20:d14bf57f435b 98 while(true) {
aismail1997 21:ceac47be2e64 99 state5 = button5.updateState();
aismail1997 21:ceac47be2e64 100 Thread::wait(100); // wait till thread is done
aismail1997 20:d14bf57f435b 101 }
aismail1997 20:d14bf57f435b 102 }
aismail1997 20:d14bf57f435b 103
aismail1997 20:d14bf57f435b 104 // thread for the custom button
aismail1997 20:d14bf57f435b 105 void button6_thread()
aismail1997 20:d14bf57f435b 106 {
aismail1997 20:d14bf57f435b 107 while(true) {
aismail1997 21:ceac47be2e64 108 state6 = button6.updateState();
aismail1997 21:ceac47be2e64 109 Thread::wait(100); // wait till thread is done
aismail1997 20:d14bf57f435b 110 }
aismail1997 20:d14bf57f435b 111 }
aismail1997 20:d14bf57f435b 112
aismail1997 8:1aa86ad19af9 113 void submit_thread()
aismail1997 6:cc181f8f2bd1 114 {
aismail1997 15:b80dde24e9bc 115 Thread::wait(500); // wait till thread is done
aismail1997 6:cc181f8f2bd1 116 }
aismail1997 6:cc181f8f2bd1 117
aismail1997 8:1aa86ad19af9 118 void start_thread()
aismail1997 6:cc181f8f2bd1 119 {
aismail1997 8:1aa86ad19af9 120 // read pb_start
aismail1997 8:1aa86ad19af9 121 // if 1
aismail1997 16:581a3b02f4c3 122 start = 1;
aismail1997 16:581a3b02f4c3 123 //pc.printf("start %d ", start);
aismail1997 8:1aa86ad19af9 124 // else 0
aismail1997 15:b80dde24e9bc 125 Thread::wait(500); // wait till thread is done
aismail1997 6:cc181f8f2bd1 126 }
aismail1997 2:9eda4611081a 127
aismail1997 16:581a3b02f4c3 128
aismail1997 2:9eda4611081a 129 int main()
aismail1997 2:9eda4611081a 130 {
aismail1997 29:b2e53ce54b3e 131
aismail1997 25:23970cf718ee 132 /*
aismail1997 8:1aa86ad19af9 133 // SETUP
aismail1997 6:cc181f8f2bd1 134 // pull up the pushbutton to prevent bouncing
aismail1997 18:5c91af9b4e7c 135 pb1.mode(PullUp);
aismail1997 18:5c91af9b4e7c 136 pb2.mode(PullUp);
aismail1997 20:d14bf57f435b 137 pb3.mode(PullUp);
aismail1997 20:d14bf57f435b 138 pb4.mode(PullUp);
aismail1997 20:d14bf57f435b 139 pb5.mode(PullUp);
aismail1997 20:d14bf57f435b 140 pb6.mode(PullUp);
aismail1997 29:b2e53ce54b3e 141 wait(.001); */
aismail1997 17:7e9308d14faa 142
aismail1997 24:6931917c70cd 143 // servos begin at 30 degrees
aismail1997 12:21268d8bf979 144 // replace with a button setup function
aismail1997 29:b2e53ce54b3e 145 buttonarr.setup();
aismail1997 21:ceac47be2e64 146
aismail1997 20:d14bf57f435b 147 //led1 = 1;
aismail1997 18:5c91af9b4e7c 148 //led2 = 1;
aismail1997 25:23970cf718ee 149 /*Thread t1(button_thread);
aismail1997 20:d14bf57f435b 150 Thread t2(button2_thread);
aismail1997 20:d14bf57f435b 151 Thread t3(button3_thread);
aismail1997 20:d14bf57f435b 152 Thread t4(button4_thread);
aismail1997 20:d14bf57f435b 153 Thread t5(button5_thread);
aismail1997 20:d14bf57f435b 154 Thread t6(button6_thread);
aismail1997 20:d14bf57f435b 155 t1.start(button_thread);
aismail1997 20:d14bf57f435b 156 t2.start(button2_thread);
aismail1997 20:d14bf57f435b 157 t3.start(button3_thread);
aismail1997 20:d14bf57f435b 158 t4.start(button4_thread);
aismail1997 20:d14bf57f435b 159 t5.start(button5_thread);
aismail1997 25:23970cf718ee 160 t6.start(button6_thread);*/
aismail1997 25:23970cf718ee 161
aismail1997 25:23970cf718ee 162 // PARSE INPUT FILE FOR LETTERS AND WORDS
aismail1997 25:23970cf718ee 163 char delimiter = ',';
aismail1997 25:23970cf718ee 164 string letter[2];
aismail1997 25:23970cf718ee 165 string word[2];
aismail1997 25:23970cf718ee 166 char check;
aismail1997 25:23970cf718ee 167 string temp;
aismail1997 25:23970cf718ee 168 string tempword = "";
aismail1997 25:23970cf718ee 169 int counter = 0;
aismail1997 25:23970cf718ee 170 FILE *fp = fopen("/sd/plan.txt", "r"); //create file
aismail1997 25:23970cf718ee 171 if(fp == NULL) {
aismail1997 25:23970cf718ee 172 pc.printf("Could not open file for write\n");
aismail1997 25:23970cf718ee 173 }
aismail1997 25:23970cf718ee 174 check = fgetc(fp); //grabs a char from file
aismail1997 25:23970cf718ee 175 while(check != '\n') { //while not at the end of line for letters
aismail1997 25:23970cf718ee 176 if((check == delimiter) && (temp.length() == 1)) { //at a comma and have a letter stored
aismail1997 25:23970cf718ee 177 letter[counter] = temp; //write letter
aismail1997 25:23970cf718ee 178 pc.printf("Letter: %s \n", letter[counter]);
aismail1997 25:23970cf718ee 179 counter = counter + 1; //increment counter
aismail1997 25:23970cf718ee 180 } else {
aismail1997 25:23970cf718ee 181 temp = check; //store letter
aismail1997 25:23970cf718ee 182 }
aismail1997 25:23970cf718ee 183 check = fgetc(fp); //grabs next char
aismail1997 25:23970cf718ee 184 }
aismail1997 25:23970cf718ee 185 counter = 0; //reset counter
aismail1997 25:23970cf718ee 186 check = fgetc(fp); //grabs next char
aismail1997 25:23970cf718ee 187 while(!feof(fp)) { //while not at the end of line for words
aismail1997 25:23970cf718ee 188 if(check == delimiter) { //when at the comma at the end of a word
aismail1997 25:23970cf718ee 189 word[counter] = tempword; //write word
aismail1997 25:23970cf718ee 190 pc.printf("Word: %s \n", word[counter]);
aismail1997 25:23970cf718ee 191 tempword = "";
aismail1997 25:23970cf718ee 192 counter = counter + 1; //increment counter
aismail1997 25:23970cf718ee 193 } else {
aismail1997 25:23970cf718ee 194 tempword = tempword + check; //concatenate letters to build word
aismail1997 25:23970cf718ee 195 }
aismail1997 25:23970cf718ee 196 check = fgetc(fp); //grabs next char
aismail1997 25:23970cf718ee 197 }
aismail1997 25:23970cf718ee 198 fclose(fp); //close file
aismail1997 16:581a3b02f4c3 199
aismail1997 16:581a3b02f4c3 200 // start threads for reset, mode, start
aismail1997 16:581a3b02f4c3 201 //Thread t1(start_thread);
aismail1997 16:581a3b02f4c3 202 //pc.printf("start thread");
aismail1997 16:581a3b02f4c3 203 //Thread t2(submit_thread);
aismail1997 16:581a3b02f4c3 204 // setup SDcard and Speaker
aismail1997 16:581a3b02f4c3 205
aismail1997 16:581a3b02f4c3 206 // when started
aismail1997 16:581a3b02f4c3 207 //while (start == 0){}
aismail1997 16:581a3b02f4c3 208 //Thread t3(button_thread);
aismail1997 16:581a3b02f4c3 209 //pc.printf("button thread");
aismail1997 16:581a3b02f4c3 210 //Thread t4(button2_thread);
aismail1997 16:581a3b02f4c3 211 //pc.printf("button2 thread");
aismail1997 16:581a3b02f4c3 212
aismail1997 16:581a3b02f4c3 213 //Thread t3(submit_thread);
aismail1997 16:581a3b02f4c3 214
aismail1997 16:581a3b02f4c3 215 // when submitted
aismail1997 16:581a3b02f4c3 216 //while (submit == 0) {}
aismail1997 16:581a3b02f4c3 217
aismail1997 16:581a3b02f4c3 218 // start button threads and submit thread
aismail1997 16:581a3b02f4c3 219 // if submit close button threads and submit thread
aismail1997 16:581a3b02f4c3 220 // check result
aismail1997 16:581a3b02f4c3 221 // play results on speaker
aismail1997 16:581a3b02f4c3 222 // save results
aismail1997 29:b2e53ce54b3e 223
aismail1997 25:23970cf718ee 224 char currletter;
aismail1997 25:23970cf718ee 225 int lettersize = sizeof(letter)/sizeof(letter[0]);
aismail1997 25:23970cf718ee 226 for (int i = 0; i < lettersize; i++) {
aismail1997 25:23970cf718ee 227 currletter = letter[i][0];
aismail1997 25:23970cf718ee 228 pc.printf("currletter %c \n", currletter);
aismail1997 25:23970cf718ee 229 pc.printf("braille %s \n", buttonarr.getBraille(currletter));
aismail1997 25:23970cf718ee 230 }
aismail1997 29:b2e53ce54b3e 231
aismail1997 25:23970cf718ee 232 int* pinsup = buttonarr.pinsUp('M');
aismail1997 29:b2e53ce54b3e 233 int currpress;
aismail1997 29:b2e53ce54b3e 234 // get what pins to press
aismail1997 29:b2e53ce54b3e 235 for (int i = 1; i <= pinsup[0]; i++) {
aismail1997 29:b2e53ce54b3e 236 // speaker stuff
aismail1997 29:b2e53ce54b3e 237 currpress = pinsup[i];
aismail1997 29:b2e53ce54b3e 238 }
aismail1997 29:b2e53ce54b3e 239
aismail1997 25:23970cf718ee 240 int* wrongpins = buttonarr.wrongPins('M', 'O');
aismail1997 29:b2e53ce54b3e 241 int currwrong;
aismail1997 29:b2e53ce54b3e 242 // get what pins are wrong
aismail1997 29:b2e53ce54b3e 243 for (int i = 1; i <= wrongpins[0]; i++) {
aismail1997 29:b2e53ce54b3e 244 // speaker stuff
aismail1997 29:b2e53ce54b3e 245 currwrong = wrongpins[i];
aismail1997 29:b2e53ce54b3e 246 }
aismail1997 29:b2e53ce54b3e 247
aismail1997 25:23970cf718ee 248 //pc.printf("current braille %c \n", buttonarr.checkVal());
aismail1997 24:6931917c70cd 249 //pc.printf("%c\n", buttonarr.releaseButtons());
aismail1997 21:ceac47be2e64 250
aismail1997 8:1aa86ad19af9 251 // MAIN THREAD
aismail1997 8:1aa86ad19af9 252 while(true) {
aismail1997 24:6931917c70cd 253
aismail1997 24:6931917c70cd 254 // read linear potentiometer
aismail1997 24:6931917c70cd 255 //led4 = linpot;
aismail1997 24:6931917c70cd 256
aismail1997 16:581a3b02f4c3 257 Thread::wait(500); // wait till thread is done
aismail1997 2:9eda4611081a 258 }
aismail1997 2:9eda4611081a 259 }