Trying to separate read/write mode from typing mode

Dependencies:   SDFileSystem emic2 mbed-rtos mbed

Fork of BAT_senior_design_Nhi by BAT

Files at this revision

API Documentation at this revision

Comitter:
aismail1997
Date:
Sat Dec 02 23:53:06 2017 +0000
Parent:
38:68f7f54b723b
Child:
40:85be3490d3a7
Commit message:
gets stuck at recheck, works correctly otherwise;

Changed in this revision

button.cpp Show annotated file Show diff for this revision Revisions of this file
button.h Show annotated file Show diff for this revision Revisions of this file
buttonArray.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/button.cpp	Sat Dec 02 01:49:48 2017 +0000
+++ b/button.cpp	Sat Dec 02 23:53:06 2017 +0000
@@ -9,7 +9,7 @@
 
 // button constructor
 button::button(PwmOut servo, DigitalIn pb, int id)
-    : servo(servo), pb(pb), state(0), press(0), id(id) {}
+    : servo(servo), pb(pb), state(0), press(0), id(id), type(0) {}
 /*button::button(PwmOut servo, DigitalIn pb, AnalogIn lp)
     : servo(servo), pb(pb), linpot(lp), press(0), state(0) {}
 */
@@ -124,41 +124,41 @@
     led4 = 0;
 }
 
-int button::updateState()
+// get servo pin
+void button::setMode(int mymode)
 {
-    //myled = 0;
-    // state 0 - button is up, pb = 0
-    if (pb == 0 && state == 3) {
-        // nothing happens here, servo is still
-        state = 0;
-    }
-    // state 1 - button is moving down, pb = 1
-    if (pb == 1 && state == 0) {
-        moveServoIn();
-        state = 1;
-        press = 1;
+    type = mymode;
+}
 
-        // Speaker says stuff
-        /*myTTS.volume(18); //max volume
-        myTTS.speakf("S");//Speak command starts with "S"
-        myTTS.speakf("Hey, you pressed a pin!");  // Send the desired string to convert to speech
-        myTTS.speakf("\r"); //marks end of speak command*/
-    }
-    // state 2 - button is down, pb = 0
-    if (pb == 0 && state == 1) {
-        // nothing happens here, servo is still
-        state = 2;
+int button::updateState()
+{    if (type == 0) {
+        //myled = 0;
+        // state 0 - button is up, pb = 0
+        if (pb == 0 && state == 3) {
+            // nothing happens here, servo is still
+            state = 0;
+        }
+        // state 1 - button is moving down, pb = 1
+        if (pb == 1 && state == 0) {
+            moveServoIn();
+            state = 1;
+            press = 1;
+        }
+        // state 2 - button is down, pb = 0
+        if (pb == 0 && state == 1) {
+            // nothing happens here, servo is still
+            state = 2;
+        }
+        // state 3 - button is moving up, pb = 1
+        if (pb == 1 && state == 2) {
+            moveServoOut();
+            state = 3;
+            press = 0;
+        }
+    } else {
+        state = pb;
+        press = pb;
     }
-    // state 3 - button is moving up, pb = 1
-    if (pb == 1 && state == 2) {
-        moveServoOut();
-        state = 3;
-        press = 0;
-    }
-    // state 4 - handle debouncing while button is down
-    /*if (pb1 = 1 && state == 2) {
-        count++;
-    }*/
     return state;
 }
 
--- a/button.h	Sat Dec 02 01:49:48 2017 +0000
+++ b/button.h	Sat Dec 02 23:53:06 2017 +0000
@@ -13,7 +13,7 @@
     int state; // where is the button (0 - 4)
     int press; // is the button up or down
     int id;    // this is the ID, each button should have a unique id
-    // int mode; // is the system in reading or typing mode
+    int type; // is the system in reading or typing mode
     //AnalogIn linpot;
 
 public:
@@ -29,6 +29,7 @@
     //void setMode(int mode); // set mode
     void setState(int);     // set what state the button is in - up or down
     void setPress(int);     // set the button press
+    void setMode(int);     // set what state the button is in - up or down
     void moveServoIn();   // move servo into the slot
     void moveServoOut();  // move servo out of the slot
     int getID();
--- a/buttonArray.cpp	Sat Dec 02 01:49:48 2017 +0000
+++ b/buttonArray.cpp	Sat Dec 02 23:53:06 2017 +0000
@@ -59,7 +59,8 @@
 char* buttonArray::getBraille(char val)
 {
     char* braille;
-    if (val == 'A') braille = "011111";
+    if (val == 'A') braille = "100000";
+    if (val == 'D') braille = "100110";
     if (val == 'X') braille = "000000";
     if (val == 'Z') braille = "111111";
     if (val == 'M') braille = "010011";
--- a/main.cpp	Sat Dec 02 01:49:48 2017 +0000
+++ b/main.cpp	Sat Dec 02 23:53:06 2017 +0000
@@ -209,171 +209,6 @@
     myTTS.volume(1); //max volume is 18
     myTTS.voice(2);
 
-/*
-    char* braille;
-    char userinput;
-
-    // INITIAL RESET
-    if (type == 0) {
-        int reset = 1;
-        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");
-        myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
-        while(reset == 1) {
-            wait(3);
-            sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(),
-                    button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress());
-            userinput = buttonarr.checkVal(braille);
-            if(userinput == 'Z') {
-                reset = 0;
-            } else {
-                //reset = 0;
-                myTTS.speakf("SSetup failed. Please try again.\r");
-                myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
-            }
-        }
-    }
-
-    for(int i = 0; i < lettersize; i++) {  //iterate through the letter array
-        char currletter = letter[i][0];
-        //pc.printf("letter: %c \n", currletter);
-        int* pinsup = buttonarr.pinsUp(currletter);
-        int currpress;
-        int numpinsups = pinsup[0];  // size of array is first element of pinsup
-        //string presspin;
-        string presspin = "STo write the letter ";
-        presspin = presspin + letter[i];
-        presspin = presspin + ", press buttons";
-
-        for (int j = 1; j < numpinsups; j++) {  // get what pins to press
-            currpress = pinsup[j];
-            switch (currpress) {
-                case 1:
-                    presspin = presspin + " 1,";
-                    break;
-                case 2:
-                    presspin = presspin + " 2,";
-                    break;
-                case 3:
-                    presspin = presspin + " 3,";
-                    break;
-                case 4:
-                    presspin = presspin + " 4,";
-                    break;
-                case 5:
-                    presspin = presspin + " 5,";
-                    break;
-                case 6:
-                    presspin = presspin + " 6,";
-                    break;
-            }
-        }
-        myTTS.speakf("%s\r",presspin);
-        myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
-        wait(2);
-        //pc.printf("hello");
-
-        //WORKS UP TO HERE
-        char* braille;
-        char userinput;
-        char* oldbraille;
-        if (type == 0) {
-            sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(),
-                    button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress());
-            userinput = buttonarr.checkVal(braille);
-        } else {
-            // TYPE MODE
-            sprintf(braille, "%d%d%d%d%d%d", !button1.getPress(), !button2.getPress(),
-                    !button3.getPress(), !button4.getPress(), !button5.getPress(), !button6.getPress());
-            userinput = buttonarr.checkVal(braille);
-            oldbraille = braille;
-        }
-
-        int currwrong;
-        string wrongpin;
-        // check result
-        pc.printf("\n %c %c", userinput, currletter);
-        int* wrongpins = buttonarr.wrongPins(braille, currletter);
-        int test = 1;
-        while(test == 1) {
-            //pc.printf("In while loop");
-            wrongpin = "SYour answer is incorrect. Buttons";
-            //wrongpin = wrongpin + letter[i];
-            //wrongpin = wrongpin + ", press pins";
-            for (int j = 1; j < wrongpins[0]; j++) {  // get what pins are wrong
-                currwrong = wrongpins[j];
-                //pc.printf("currwrong: %d", currwrong);
-                switch (currwrong) {
-                    case 1:
-                        wrongpin = wrongpin + " 1,";
-                        break;
-                    case 2:
-                        wrongpin = wrongpin + " 2,";
-                        break;
-                    case 3:
-                        wrongpin = wrongpin + " 3,";
-                        break;
-                    case 4:
-                        wrongpin = wrongpin + " 4,";
-                        break;
-                    case 5:
-                        wrongpin = wrongpin + " 5,";
-                        break;
-                    case 6:
-                        wrongpin = wrongpin + " 6,";
-                        break;
-                }
-            }
-            //pc.printf("end for loop");
-            if (wrongpins[0] > 1) {
-                wrongpin = wrongpin + " are wrong. Please try again.";
-                myTTS.speakf("%s\r",wrongpin);
-                myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
-                wait(2);
-                //UPDATE THE PINS THAT ARE WRONG BY CHECKING AGAIN BELOW
-                //char* braille;
-                if (type == 0) {
-                    sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(),
-                            button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress());
-                    userinput = buttonarr.checkVal(braille);
-                } else {
-                    // TYPE MODE
-                    sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(),
-                            button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress());
-                    userinput = buttonarr.checkVal(braille);
-                }
-                // get input
-                //pc.printf("Your input was: %c", userinput);
-
-                // check result
-                wrongpins = buttonarr.wrongPins(braille, currletter);
-            } else {
-                test = 0;
-                myTTS.speakf("SGood job!\r");
-                myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
-            }
-        }
-
-        if (type == 0) {
-            int reset = 1;
-            myTTS.speakf("SNow reset by pressing down all the buttons.\r");
-            myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
-            while(reset == 1) {
-                char* braille;
-                sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(),
-                        button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress());
-                userinput = buttonarr.checkVal(braille);
-                if(userinput == 'Z') {
-                    reset = 0;
-                } else {
-                    wait(2);
-                    myTTS.speakf("SPlease try again.\r");
-                    myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
-                }
-            }
-        }
-        //pc.printf("wrongpins are: %d", wrongpins[0]);
-    }*/
-
 // WORD CODE
 
 // WORD COUNTER
@@ -388,12 +223,22 @@
 
     //SETTING UP SERVOS TO RELEASE FOR TYPING MODE FREEZES THE CODE
     buttonarr.setup(); //Reset servos so that buttons are up for typing mode
+
+    button1.setMode(1);
+    button2.setMode(1);
+    button3.setMode(1);
+    button4.setMode(1);
+    button5.setMode(1);
+    button6.setMode(1);
+
     string currword;
     int wordsize = sizeof(word)/sizeof(word[0]);
 
     //myTTS.speakf("SLet us now put these letters together to form words. Please reset buttons after each letter. Press down the submit button when done.\r");  // Send the desired string to convert to speech
     myTTS.speakf("SPlease switch the device into typing mode. Press down the submit button when done.\r");  // Send the desired string to convert to speech
     myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
+
+    // loop through each of the words
     for(int i = 0; i < wordsize; i++) {  //iterate through the word array
         currword = word[i];
         string presspin = "To type the word ";
@@ -402,73 +247,83 @@
         for (int j = 0; j < currword.length(); j++) {
             presspin = presspin + ", "+ currword[j];
         }
-        //myTTS.speakf("S%s\r", presspin); //UNCOMMENTING HERE WILL MESS UP READ/WRITE CODE
-        //myTTS.ready();
-    }
+
+        myTTS.speakf("S%s\r", presspin);
+        myTTS.ready();
+
+        // loop through each of the letters
+        int incorrect = 1;
+        char userinput;
 
-    
-        // loop through each of the words
-        for(int i = 0; i < wordsize; i++) {  //iterate through the word array
-            currword = word[i];
-            string presspin = "To type the word ";
-            presspin = presspin + word[i];
-            presspin = presspin + ", type letters ";
+        // while(!submit)
+        while (incorrect == 1) {
+            //myTTS.speakf("S%s\r", presspin);
+            //myTTS.ready();
+
+            char wordinput[currword.length()];
+            // replace for loop with submit
+            // while (!submit) {
+
             for (int j = 0; j < currword.length(); j++) {
-                presspin = presspin + ", "+ currword[j];
-            }
-            myTTS.speakf("S%s\r", presspin);
-            myTTS.ready();
 
-            string wordinput = "";
-
-            // loop through each of the letters
-            //olduserinput;
-            int incorrect = 1;
-            //char userinput;
-            //char* braille;
+                //currletter = currword[j];
+                // check if all buttons were pressed
 
-            // while(!submit)
-            while (incorrect) {
-                // replace for loop with submit
-                // while (!submit) {
-                for (int j = 0; j < currword.length(); j++) {
-                    //currletter = currword[j];
-                    // check if all buttons were pressed
-                    wait(1);
+                char* braille;
+                char* oldbraille;
 
-                    char* braille;
-                    char* oldbraille;
+                wait(1);
+
+                sprintf(braille, "%d%d%d%d%d%d", !button1.getPress(), !button2.getPress(),
+                        !button3.getPress(), !button4.getPress(), !button5.getPress(), !button6.getPress());
+                //pc.printf("test %s ", braille);
 
-                    // wait for input
-                    while (strcmp(braille, "000000") == 0) {}
+                // wait for input
+                while (strcmp(braille, "111111") == 0) {
+                    sprintf(braille, "%d%d%d%d%d%d", !button1.getPress(), !button2.getPress(),
+                            !button3.getPress(), !button4.getPress(), !button5.getPress(), !button6.getPress());
+                }
+                //pc.printf("reached");
 
-                    // while buttons have not been released
-                    while (strcmp(braille, "000000") != 0) {
-                        oldbraille = braille;
-                        sprintf(braille, "%d%d%d%d%d%d", button1.getPress(), button2.getPress(),
-                                button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress());
-                    }
-
-                    // at the end of this, oldbraille contains the letter representation
-                    // flip braille values
-                    //userinput = checkVal(oldbraille);
-                    wordinput[j] = buttonarr.checkVal(oldbraille);
-                    //wordinput[j] = olduserinput;
+                // while buttons have not been released
+                int oldsum = 0;
+                int sum = 0;
+                while ((strcmp(braille, "111111") != 0) && (oldsum <= sum)) {
+                    oldbraille = braille;
+                    oldsum = sum;
+                    sprintf(braille, "%d%d%d%d%d%d", !button1.getPress(), !button2.getPress(),
+                            !button3.getPress(), !button4.getPress(), !button5.getPress(), !button6.getPress());
+                    sum = button1.getPress() + button2.getPress() + button3.getPress() + button4.getPress()
+                          + button5.getPress() + button6.getPress();
                 }
 
-                // check result of words
-                if (wordinput.compare(currword) == 0) { // input is correct
-                    incorrect = 0;
-                    myTTS.speakf("Good job!\r");
-                    myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
-                } else {
-                    incorrect = 0; // delete this line
-                    myTTS.speakf("SPlease try again!.\r");
-                    myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
+                // at the end of this, oldbraille contains the letter representation
+                //pc.printf("%s ", oldbraille);
+                userinput = buttonarr.checkVal(oldbraille);
+                pc.printf("%c ", userinput);
+                wordinput[j] = userinput;
+            }
+
+            // check result of words
+            incorrect = 0;
+            for (int k = 0; k < currword.length(); k++) {
+                if (currword[k] != wordinput[k]) { // if any letter doesn't match
+                    incorrect = 1; // spelling is wrong
                 }
             }
 
+            if (incorrect == 1) {
+                myTTS.speakf("SPlease try again! %s\r", presspin);
+                myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
+                //wait(2);
+                //incorrect = 1;
+            } else {
+                //wait(1);
+                myTTS.speakf("SGood job!\r");
+                myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
+            }
         }
+    }
 
 // MAIN THREAD
     while(true) {