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 15:59:21 2017 +0000
Revision:
25:23970cf718ee
Parent:
24:6931917c70cd
Child:
26:4b2940eec275
Child:
29:b2e53ce54b3e
Integrated parsing code and tested button functions

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 25:23970cf718ee 131 /*
aismail1997 8:1aa86ad19af9 132 // SETUP
aismail1997 6:cc181f8f2bd1 133 // pull up the pushbutton to prevent bouncing
aismail1997 18:5c91af9b4e7c 134 pb1.mode(PullUp);
aismail1997 18:5c91af9b4e7c 135 pb2.mode(PullUp);
aismail1997 20:d14bf57f435b 136 pb3.mode(PullUp);
aismail1997 20:d14bf57f435b 137 pb4.mode(PullUp);
aismail1997 20:d14bf57f435b 138 pb5.mode(PullUp);
aismail1997 20:d14bf57f435b 139 pb6.mode(PullUp);
aismail1997 18:5c91af9b4e7c 140 wait(.001);
aismail1997 17:7e9308d14faa 141
aismail1997 24:6931917c70cd 142 // servos begin at 30 degrees
aismail1997 12:21268d8bf979 143 // replace with a button setup function
aismail1997 2:9eda4611081a 144 for(int i=0; i<=3; i++) {
aismail1997 2:9eda4611081a 145 myservo = i/100.0;
aismail1997 2:9eda4611081a 146 wait(0.01);
aismail1997 2:9eda4611081a 147 }
aismail1997 16:581a3b02f4c3 148 for(int i=0; i<=3; i++) {
aismail1997 16:581a3b02f4c3 149 myservo2 = i/100.0;
aismail1997 16:581a3b02f4c3 150 wait(0.01);
aismail1997 16:581a3b02f4c3 151 }
aismail1997 21:ceac47be2e64 152
aismail1997 20:d14bf57f435b 153 for(int i=0; i<=3; i++) {
aismail1997 20:d14bf57f435b 154 myservo3 = i/100.0;
aismail1997 20:d14bf57f435b 155 wait(0.01);
aismail1997 20:d14bf57f435b 156 }
aismail1997 21:ceac47be2e64 157
aismail1997 20:d14bf57f435b 158 for(int i=0; i<=3; i++) {
aismail1997 20:d14bf57f435b 159 myservo4 = i/100.0;
aismail1997 20:d14bf57f435b 160 wait(0.01);
aismail1997 20:d14bf57f435b 161 }
aismail1997 21:ceac47be2e64 162
aismail1997 20:d14bf57f435b 163 for(int i=0; i<=3; i++) {
aismail1997 20:d14bf57f435b 164 myservo5 = i/100.0;
aismail1997 20:d14bf57f435b 165 wait(0.01);
aismail1997 20:d14bf57f435b 166 }
aismail1997 6:cc181f8f2bd1 167
aismail1997 20:d14bf57f435b 168 for(int i=0; i<=3; i++) {
aismail1997 20:d14bf57f435b 169 myservo6 = i/100.0;
aismail1997 20:d14bf57f435b 170 wait(0.01);
aismail1997 25:23970cf718ee 171 }*/
aismail1997 21:ceac47be2e64 172
aismail1997 20:d14bf57f435b 173 //led1 = 1;
aismail1997 18:5c91af9b4e7c 174 //led2 = 1;
aismail1997 25:23970cf718ee 175 /*Thread t1(button_thread);
aismail1997 20:d14bf57f435b 176 Thread t2(button2_thread);
aismail1997 20:d14bf57f435b 177 Thread t3(button3_thread);
aismail1997 20:d14bf57f435b 178 Thread t4(button4_thread);
aismail1997 20:d14bf57f435b 179 Thread t5(button5_thread);
aismail1997 20:d14bf57f435b 180 Thread t6(button6_thread);
aismail1997 20:d14bf57f435b 181 t1.start(button_thread);
aismail1997 20:d14bf57f435b 182 t2.start(button2_thread);
aismail1997 20:d14bf57f435b 183 t3.start(button3_thread);
aismail1997 20:d14bf57f435b 184 t4.start(button4_thread);
aismail1997 20:d14bf57f435b 185 t5.start(button5_thread);
aismail1997 25:23970cf718ee 186 t6.start(button6_thread);*/
aismail1997 25:23970cf718ee 187
aismail1997 25:23970cf718ee 188 // PARSE INPUT FILE FOR LETTERS AND WORDS
aismail1997 25:23970cf718ee 189 char delimiter = ',';
aismail1997 25:23970cf718ee 190 string letter[2];
aismail1997 25:23970cf718ee 191 string word[2];
aismail1997 25:23970cf718ee 192 char check;
aismail1997 25:23970cf718ee 193 string temp;
aismail1997 25:23970cf718ee 194 string tempword = "";
aismail1997 25:23970cf718ee 195 int counter = 0;
aismail1997 25:23970cf718ee 196 FILE *fp = fopen("/sd/plan.txt", "r"); //create file
aismail1997 25:23970cf718ee 197 if(fp == NULL) {
aismail1997 25:23970cf718ee 198 pc.printf("Could not open file for write\n");
aismail1997 25:23970cf718ee 199 }
aismail1997 25:23970cf718ee 200 check = fgetc(fp); //grabs a char from file
aismail1997 25:23970cf718ee 201 while(check != '\n') { //while not at the end of line for letters
aismail1997 25:23970cf718ee 202 if((check == delimiter) && (temp.length() == 1)) { //at a comma and have a letter stored
aismail1997 25:23970cf718ee 203 letter[counter] = temp; //write letter
aismail1997 25:23970cf718ee 204 pc.printf("Letter: %s \n", letter[counter]);
aismail1997 25:23970cf718ee 205 counter = counter + 1; //increment counter
aismail1997 25:23970cf718ee 206 } else {
aismail1997 25:23970cf718ee 207 temp = check; //store letter
aismail1997 25:23970cf718ee 208 }
aismail1997 25:23970cf718ee 209 check = fgetc(fp); //grabs next char
aismail1997 25:23970cf718ee 210 }
aismail1997 25:23970cf718ee 211 counter = 0; //reset counter
aismail1997 25:23970cf718ee 212 check = fgetc(fp); //grabs next char
aismail1997 25:23970cf718ee 213 while(!feof(fp)) { //while not at the end of line for words
aismail1997 25:23970cf718ee 214 if(check == delimiter) { //when at the comma at the end of a word
aismail1997 25:23970cf718ee 215 word[counter] = tempword; //write word
aismail1997 25:23970cf718ee 216 pc.printf("Word: %s \n", word[counter]);
aismail1997 25:23970cf718ee 217 tempword = "";
aismail1997 25:23970cf718ee 218 counter = counter + 1; //increment counter
aismail1997 25:23970cf718ee 219 } else {
aismail1997 25:23970cf718ee 220 tempword = tempword + check; //concatenate letters to build word
aismail1997 25:23970cf718ee 221 }
aismail1997 25:23970cf718ee 222 check = fgetc(fp); //grabs next char
aismail1997 25:23970cf718ee 223 }
aismail1997 25:23970cf718ee 224 fclose(fp); //close file
aismail1997 16:581a3b02f4c3 225
aismail1997 16:581a3b02f4c3 226 // start threads for reset, mode, start
aismail1997 16:581a3b02f4c3 227 //Thread t1(start_thread);
aismail1997 16:581a3b02f4c3 228 //pc.printf("start thread");
aismail1997 16:581a3b02f4c3 229 //Thread t2(submit_thread);
aismail1997 16:581a3b02f4c3 230 // setup SDcard and Speaker
aismail1997 16:581a3b02f4c3 231
aismail1997 16:581a3b02f4c3 232 // when started
aismail1997 16:581a3b02f4c3 233 //while (start == 0){}
aismail1997 16:581a3b02f4c3 234 //Thread t3(button_thread);
aismail1997 16:581a3b02f4c3 235 //pc.printf("button thread");
aismail1997 16:581a3b02f4c3 236 //Thread t4(button2_thread);
aismail1997 16:581a3b02f4c3 237 //pc.printf("button2 thread");
aismail1997 16:581a3b02f4c3 238
aismail1997 16:581a3b02f4c3 239 //Thread t3(submit_thread);
aismail1997 16:581a3b02f4c3 240
aismail1997 16:581a3b02f4c3 241 // when submitted
aismail1997 16:581a3b02f4c3 242 //while (submit == 0) {}
aismail1997 16:581a3b02f4c3 243
aismail1997 16:581a3b02f4c3 244 // start button threads and submit thread
aismail1997 16:581a3b02f4c3 245 // if submit close button threads and submit thread
aismail1997 16:581a3b02f4c3 246 // check result
aismail1997 16:581a3b02f4c3 247 // play results on speaker
aismail1997 16:581a3b02f4c3 248 // save results
aismail1997 25:23970cf718ee 249 char currletter;
aismail1997 25:23970cf718ee 250 int lettersize = sizeof(letter)/sizeof(letter[0]);
aismail1997 25:23970cf718ee 251 for (int i = 0; i < lettersize; i++) {
aismail1997 25:23970cf718ee 252 currletter = letter[i][0];
aismail1997 25:23970cf718ee 253 pc.printf("currletter %c \n", currletter);
aismail1997 25:23970cf718ee 254 pc.printf("braille %s \n", buttonarr.getBraille(currletter));
aismail1997 25:23970cf718ee 255 }
aismail1997 25:23970cf718ee 256 int* pinsup = buttonarr.pinsUp('M');
aismail1997 25:23970cf718ee 257 pc.printf("pins up M: %d \n", pinsup[0]);
aismail1997 25:23970cf718ee 258 pc.printf("pins up M: %d \n", pinsup[1]);
aismail1997 25:23970cf718ee 259 pc.printf("pins up M: %d \n", pinsup[2]);
aismail1997 25:23970cf718ee 260 int* wrongpins = buttonarr.wrongPins('M', 'O');
aismail1997 25:23970cf718ee 261 pc.printf("wrong pins M & O %d \n", wrongpins[0]);
aismail1997 25:23970cf718ee 262 pc.printf("wrong pins M & O %d \n", wrongpins[1]);
aismail1997 25:23970cf718ee 263 //pc.printf("current braille %c \n", buttonarr.checkVal());
aismail1997 24:6931917c70cd 264 //pc.printf("%c\n", buttonarr.releaseButtons());
aismail1997 21:ceac47be2e64 265
aismail1997 8:1aa86ad19af9 266 // MAIN THREAD
aismail1997 8:1aa86ad19af9 267 while(true) {
aismail1997 24:6931917c70cd 268
aismail1997 24:6931917c70cd 269 // read linear potentiometer
aismail1997 24:6931917c70cd 270 //led4 = linpot;
aismail1997 24:6931917c70cd 271
aismail1997 16:581a3b02f4c3 272 Thread::wait(500); // wait till thread is done
aismail1997 2:9eda4611081a 273 }
aismail1997 2:9eda4611081a 274 }