Working read code with mode button

Dependencies:   SDFileSystem emic2 mbed-rtos mbed

Fork of BAT_senior_design_Testnew by BAT

Files at this revision

API Documentation at this revision

Comitter:
nnguyen45
Date:
Tue Dec 05 03:31:58 2017 +0000
Parent:
42:44a898adef26
Commit message:
Working with mode button

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	Mon Dec 04 23:25:38 2017 +0000
+++ b/button.cpp	Tue Dec 05 03:31:58 2017 +0000
@@ -10,15 +10,20 @@
 // button constructor
 button::button(PwmOut servo, DigitalIn pb, int id)
     : servo(servo), pb(pb), state(0), press(0), id(id) {}
+/*button::button(PwmOut servo, DigitalIn pb, AnalogIn lp)
+    : servo(servo), pb(pb), linpot(lp), press(0), state(0) {}
+*/
+//Serial pc(USBTX, USBRX);
 
 // FUNCTIONS
+
 // get servo pin
 PwmOut button::getServoPin()
 {
     return servo;
 }
 
-// set button state
+// get servo pin
 void button::setState(int mystate)
 {
     state = mystate;
@@ -29,6 +34,12 @@
     press = mypress;
 }
 
+// get servo pin
+/*void button::setMode(int mymode)
+{
+    mode = mymode;
+}*/
+
 // get current state of the button
 int button::getState()
 {
@@ -43,23 +54,29 @@
 
 int button::getPress()
 {
+    //pc.printf("%d", press);
     return press;
 }
 
 // get current state of the button
 int button::getLp()
 {
+    /*    if (linpot < 2)
+            return 1;
+        else*/
     return 0;
 }
 
 // move servo into the slot
 void button::moveServoIn()
 {
+    //myled = 1;
     // rotate 90 degrees one way
     for(int i=4; i<=7; i++) {
         servo = i/100.0;
         wait(0.01);
     }
+    //press = 1;
     switch (id) {
         case 1:
             led2 = 0;
@@ -97,6 +114,7 @@
 // move servo out of the slot
 void button::moveServoOut()
 {
+    //myled = 0;
     for(int i=7; i>4; i--) {
         servo = i/100.0;
         wait(0.01);
@@ -108,6 +126,7 @@
 
 int button::updateState()
 {
+    //myled = 0;
     // state 0 - button is up, pb = 0
     if (pb == 0 && state == 3) {
         // nothing happens here, servo is still
@@ -118,6 +137,12 @@
         moveServoIn();
         state = 1;
         press = 1;
+
+        // 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) {
@@ -131,6 +156,9 @@
         press = 0;
     }
     // state 4 - handle debouncing while button is down
+    /*if (pb1 = 1 && state == 2) {
+        count++;
+    }*/
     return state;
 }
 
--- a/button.h	Mon Dec 04 23:25:38 2017 +0000
+++ b/button.h	Tue Dec 05 03:31:58 2017 +0000
@@ -13,14 +13,20 @@
     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
+    //AnalogIn linpot;
 
 public:
     // constructors
     button(); // Default
     button(PwmOut servo, DigitalIn pb, int id);
+    //button(PwmOut servo, DigitalIn pb, AnalogIn linpot);
 
+    // button(PwmOut servo, DigitalIn pb, AnalogIn linpot);
     // functions
     PwmOut getServoPin(); // get the servo pin
+    //void setState(int state); // set state
+    //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 moveServoIn();   // move servo into the slot
--- a/buttonArray.cpp	Mon Dec 04 23:25:38 2017 +0000
+++ b/buttonArray.cpp	Tue Dec 05 03:31:58 2017 +0000
@@ -1,17 +1,27 @@
 #include "mbed.h"
 #include "buttonArray.h"
 
+// type mode
+
 // buttonArray constructor
 buttonArray::buttonArray(button b1, button b2, button b3, button b4, button b5, button b6)
     : button1(b1), button2(b2), button3(b3), button4(b4), button5(b5), button6(b6) {}
 
+//Serial pc(USBTX, USBRX);
+
 // FUNCTIONS
 
 // map input braille to ascii
 // braille respresentation here - https://en.wikipedia.org/wiki/Braille_ASCII
 char buttonArray::checkVal(char* braille)
 {
+    //pc.printf(" checkVal \n");
+    //char* braille;
     char val = 'K';
+    /*int test = button1.getPress();
+    sprintf(braille, "%d%d%d%d%d%d", test, button2.getPress(),
+            button3.getPress(), button4.getPress(), button5.getPress(), button6.getPress());*/
+    //pc.printf(" %s \n", braille);
     if (strcmp(braille, "000000") == 0) val = 'X';
     if (strcmp(braille, "011111") == 0) val = 'A';
     if (strcmp(braille, "001111") == 0) val = 'B';
@@ -23,12 +33,12 @@
     if (strcmp(braille, "001101") == 0) val = 'H';
     if (strcmp(braille, "101011") == 0) val = 'I';
     if (strcmp(braille, "101001") == 0) val = 'J';
-    //if (strcmp(braille, "011111") == 0) val = 'K';
-    if (strcmp(braille, "000111") == 0) val = 'L';
+    /*if (strcmp(braille, "011111") == 0) val = 'K';
+    if (strcmp(braille, "000111") == 0) val = 'L';*/
     if (strcmp(braille, "010011") == 0) val = 'M';
     if (strcmp(braille, "010001") == 0) val = 'N';
     if (strcmp(braille, "010101") == 0) val = 'O';
-    if (strcmp(braille, "000011") == 0) val = 'P';
+    /*if (strcmp(braille, "000011") == 0) val = 'P';
     if (strcmp(braille, "000001") == 0) val = 'Q';
     if (strcmp(braille, "000101") == 0) val = 'R';
     if (strcmp(braille, "100011") == 0) val = 'S';
@@ -36,11 +46,12 @@
     if (strcmp(braille, "010110") == 0) val = 'U';
     if (strcmp(braille, "000110") == 0) val = 'V';
     if (strcmp(braille, "101000") == 0) val = 'W';
-    //if (strcmp(braille, "010010") == 0) val = 'X';
+    if (strcmp(braille, "010010") == 0) val = 'X';
     if (strcmp(braille, "010000") == 0) val = 'Y';
-    //if (strcmp(braille, "010100") == 0) val = 'Z';
+    if (strcmp(braille, "010100") == 0) val = 'Z';*/
     // check if reset
     if (strcmp(braille, "111111") == 0) val = 'Z';
+    //pc.printf(" %c \n", val);
     return val;
 }
 
@@ -58,12 +69,12 @@
     if (val == 'H') braille = "001101";
     if (val == 'I') braille = "101011";
     if (val == 'J') braille = "101001";
-    //if (val == 'K') braille = "010111";
-    if (val == 'L') braille = "000111";
+    /*if (val == 'K') braille = "011111";
+    if (val == 'L') braille = "000111";*/
     if (val == 'M') braille = "010011";
     if (val == 'N') braille = "010001";
     if (val == 'O') braille = "010101";
-    if (val == 'P') braille = "000011";
+    /*if (val == 'P') braille = "000011";
     if (val == 'Q') braille = "000001";
     if (val == 'R') braille = "000101";
     if (val == 'S') braille = "100011";
@@ -71,9 +82,9 @@
     if (val == 'U') braille = "010110";
     if (val == 'V') braille = "000110";
     if (val == 'W') braille = "101000";
-    //if (val == 'X') braille = "010010";
+    if (val == 'X') braille = "010010";
     if (val == 'Y') braille = "010000";
-    //if (val == 'Z') braille = "010100";
+    if (val == 'Z') braille = "010100";*/
     // check if reset
     if (val == ' ') braille = "111111";
     return braille;
@@ -83,6 +94,7 @@
 int* buttonArray::pinsUp(char val)
 {
     int* pinsup;
+    //pinsup = new int[7];
     char* braille = getBraille(val);
     int j = 1;
     for (int i = 0; i < 6; i++) {
@@ -100,13 +112,16 @@
 // takes in current and actual char as input and returns status of each char
 int* buttonArray::wrongPins(char* inarr, char actual)
 {
+    // TODO check initial value of wrong
     int* wrong;
     wrong = new int[7];
     char* actarr = getBraille(actual);
+    //pc.printf("wrong pins");
     int j = 1;
     for (int i = 0; i < 6; i++) {
         if(inarr[i] != actarr[i]) {
             wrong[j] = i+1;
+            //pc.printf("%d ", wrong[j]);
             j++;
         }
     }
@@ -152,7 +167,8 @@
 
 void buttonArray::setup()
 {
-    // servo setup function; servos begin at 30 degrees
+    // servos begin at 30 degrees
+    // replace with a button setup function
     button1.setup();
     button2.setup();
     button3.setup();
--- a/main.cpp	Mon Dec 04 23:25:38 2017 +0000
+++ b/main.cpp	Tue Dec 05 03:31:58 2017 +0000
@@ -22,9 +22,17 @@
 DigitalIn pb5 (p16);
 PwmOut myservo6(p26);
 DigitalIn pb6 (p15);
+DigitalIn read (p29);
 
+
+//DigitalOut led1(LED1);
+//DigitalOut led3(LED3);
+//DigitalOut led4(LED4);
+
+//DigitalIn linpot(p9);
 Serial pc(USBTX, USBRX);
 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
+//button button1(myservo, pb1, linpot);
 button button1(myservo, pb1, 1);
 button button2(myservo2, pb2, 2);
 button button3(myservo3, pb3, 3);
@@ -50,11 +58,14 @@
 int count = 0;
 
 // THREADS
+
 void button_thread()
 {
     while(true) {
         state = button1.updateState();
         state6 = button6.updateState();
+
+        //led4 = button1.getLp();
         Thread::wait(100); // wait till thread is done
     }
 }
@@ -67,6 +78,7 @@
     }
 }
 
+// thread for the custom button
 void button3_thread()
 {
     while(true) {
@@ -75,6 +87,7 @@
     }
 }
 
+// thread for the custom button
 void button4_thread()
 {
     while(true) {
@@ -83,6 +96,7 @@
     }
 }
 
+// thread for the custom button
 void button5_thread()
 {
     while(true) {
@@ -91,6 +105,7 @@
     }
 }
 
+// thread for the custom button
 void button6_thread()
 {
     while(true) {
@@ -106,26 +121,40 @@
 
 void start_thread()
 {
+    // read pb_start
+    // if 1
     start = 1;
+    //pc.printf("start %d ", start);
+    // else 0
     Thread::wait(500); // wait till thread is done
 }
 
+
 int main()
 {
-    // SETUP; pull up the pushbutton to prevent bouncing
+    // SETUP
+    // pull up the pushbutton to prevent bouncing
     pb1.mode(PullUp);
     pb2.mode(PullUp);
     pb3.mode(PullUp);
     pb4.mode(PullUp);
     pb5.mode(PullUp);
     pb6.mode(PullUp);
+    read.mode(PullUp);
     wait(.001);
 
-    // servo setup up function; servos begin at 30 degrees
+    // servos begin at 30 degrees
+    // replace with a button setup function
     buttonarr.setup();
+    //buttonarr.setMode();
+
+    //led1 = 1;
+    //led2 = 1;
+
 
     // PARSE INPUT FILE FOR LETTERS AND WORDS
     char delimiter = ',';
+    // TODO: fix the letters
     string letter[2];
     string word[2];
     char check;
@@ -133,10 +162,14 @@
     string tempword = "";
     int counter = 0;
     FILE *fp = fopen("/sd/plan.txt", "r"); //create file
+    if(fp == NULL) {
+        //pc.printf("Could not open file for write\n");
+    }
     check = fgetc(fp); //grabs a char from file
     while(check != '\n') {  //while not at the end of line for letters
         if((check == delimiter) && (temp.length() == 1)) {  //at a comma and have a letter stored
             letter[counter] = temp; //write letter
+            //pc.printf("Letter: %s \n", letter[counter]);
             counter = counter + 1;  //increment counter
         } else {
             temp = check;   //store letter
@@ -148,6 +181,7 @@
     while(!feof(fp)) {  //while not at the end of line for words
         if(check == delimiter) {  //when at the comma at the end of a word
             word[counter] = tempword;   //write word
+            //pc.printf("Word: %s \n", word[counter]);
             tempword = "";
             counter = counter + 1;  //increment counter
         } else {
@@ -156,54 +190,62 @@
         check = fgetc(fp);  //grabs next char
     }
     fclose(fp); //close file
-    
-    //INITIALIZE THREADS
+
     Thread t1(button_thread);
     Thread t2(button2_thread);
     Thread t3(button3_thread);
     Thread t4(button4_thread);
     Thread t5(button5_thread);
+    //Thread t6(button6_thread);
     t1.start(button_thread);
     t2.start(button2_thread);
     t3.start(button3_thread);
     t4.start(button4_thread);
     t5.start(button5_thread);
-
+    //t6.start(button6_thread);
     char currletter;
     int lettersize = sizeof(letter)/sizeof(letter[0]);
+    //pc.printf("lettersize: %d", lettersize);
     int type = 0;
 
     //TEXT-TO-SPEECH LOGIC
-    myTTS.volume(18); //max volume is 18
+    myTTS.volume(18); //max volume
     myTTS.voice(2);
 
+    //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
+    //myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
     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 {
-                myTTS.speakf("SSetup failed. Please try again.\r");
-                myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
-            }
+    //if (type == 0) {
+    int reset = 1;
+    while(read==0) {}
+    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(read==0) {}
+    while(reset == 1) {
+        wait(2);
+        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";
@@ -234,21 +276,23 @@
         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 {
+        //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;
@@ -257,9 +301,13 @@
         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,";
@@ -281,16 +329,27 @@
                         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());
+                        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 {
@@ -300,24 +359,56 @@
             }
         }
 
-        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
-                }
+        //if (type == 0) {
+        int reset = 1;
+        myTTS.speakf("SNow reset by repressing 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
+// go through each word
+// go through each letter in the word
+// get user input
+// when buttons reset - save previous letter
+// loop until submit button is pressed
+// compare words
+// check what letters were wrong
+// give wrong letters - correct spelling is...
+
+    //SETTING UP SERVOS TO RELEASE FOR TYPING MODE FREEZES THE CODE
+    //buttonarr.setup(); //Reset servos so that buttons are up for typing mode
+    string currword;
+    int wordsize = sizeof(word)/sizeof(word[0]);
+
+    myTTS.speakf("SThis is the end of the module.\r");  // Send the desired string to convert to speech
+    myTTS.ready(); //ready waits for speech to finish from last command with a ":" response
+    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 ";
+        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();
     }
 
 // MAIN THREAD