Words in Typing mode FINAL

Dependencies:   SDFileSystem emic2 mbed-rtos mbed

Fork of BAT_Type_word_FINAL by Azra Ismail

Files at this revision

API Documentation at this revision

Comitter:
aismail1997
Date:
Sun Oct 22 22:20:11 2017 +0000
Parent:
13:b80dde24e9bc
Child:
15:7e9308d14faa
Commit message:
Last working code

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
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/button.cpp	Wed Oct 18 14:41:04 2017 +0000
+++ b/button.cpp	Sun Oct 22 22:20:11 2017 +0000
@@ -4,6 +4,8 @@
 // button constructor
 button::button(PwmOut servo, DigitalIn pb)
     : servo(servo), pb(pb) {}
+//button::button(PwmOut servo, DigitalIn pb, AnalogIn lp)
+//    : servo(servo), pb(pb), linpot(lp), mode(0), state(0) {}
 
 // FUNCTIONS
 
@@ -13,6 +15,18 @@
     return servo;
 }
 
+// get servo pin
+/*void button::setState(int mystate)
+{
+    state = mystate;
+}
+
+// get servo pin
+void button::setMode(int mymode)
+{
+    mode = mymode;
+}*/
+
 // get current state of the button
 /*int button::getState()
 {
--- a/button.h	Wed Oct 18 14:41:04 2017 +0000
+++ b/button.h	Sun Oct 22 22:20:11 2017 +0000
@@ -7,16 +7,19 @@
 private:
     PwmOut servo;
     DigitalIn pb;
-    //int state; // is the button up or down
+    // int state; // is the button up or down
     // int mode; // is the system in reading or typing mode
-    //AnalogIn linpot;
+    // AnalogIn linpot;
     
 public:
     // constructors
-    button();
+    button(); // Default
     button(PwmOut servo, DigitalIn pb);
+    // 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
     //int getState();     // determine what state the button is in - up or down
     void moveServoIn();   // move servo into the slot
     void moveServoOut();  // move servo out of the slot
--- a/main.cpp	Wed Oct 18 14:41:04 2017 +0000
+++ b/main.cpp	Sun Oct 22 22:20:11 2017 +0000
@@ -7,13 +7,18 @@
 // DEFINE I/O
 PwmOut myservo(p21);
 DigitalIn pb1 (p20);
+PwmOut myservo2(p22);
+DigitalIn pb2 (p19);
+DigitalOut led1(LED1);
+DigitalOut led2(LED4);
+
 //AnalogIn linpot(p20);
-Serial pc(USBTX, USBRX);
-//DigitalOut myled(LED1);
-SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
-AnalogOut DACout(p26);
-wave_player waver(&DACout);
+//Serial pc(USBTX, USBRX);
+//SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
+//AnalogOut DACout(p26);
+//wave_player waver(&DACout);
 button button1(myservo, pb1);
+button button2(myservo2, pb2);
 
 // INITIALIZE VARIABLES
 // add mode, reset buttons
@@ -21,33 +26,61 @@
 int submit = 0;
 // FIX THIS: button up: state = 2, button halfway: state = 0; button down: state = 1
 int state = 2;
+int state2 = 2;
+int count = 0;
 
 // FUNCTIONS
 // play a file on the speaker
-void playSound(wave_player waver)
+/*void playSound(wave_player waver)
 {
     FILE *wave_file;
     wave_file=fopen("/sd/lesson.wav","r");
     waver.play(wave_file);
     fclose(wave_file);
-}
+}*/
 
 // THREADS
 
 // thread for the custom button
 void button_thread()
 {
-    // button was up and is moving down, move servo in
-    if (pb1 == 1 && state == 2) {
-        button1.moveServoIn();
-        state = 1;
+    while(true) {
+        // button was up and is moving down, move servo in
+        //pc.printf("in button thread");
+        if (pb1 == 1 && state == 2) {
+            button1.moveServoIn();
+            state = 1;
+            led1 = 0;
+        }
+        // button was down and is being pushed again, move servo out
+        else if (pb1 == 1 && state == 1) {
+            button1.moveServoOut();
+            state = 2;
+            led1 = 1;
+        }
+        Thread::wait(100); // wait till thread is done
     }
-    // button was down and is being pushed again, move servo out
-    if (pb1 == 1 && state == 1) {
-        button1.moveServoOut();
-        state = 2;
+}
+
+void button2_thread()
+{
+    // TODO: Add states
+    while(true) {
+    // button was up and is moving down, move servo in
+        if (pb2 == 1 && state2 == 2) {
+                button2.moveServoIn();
+                state2 = 1;
+                led2 = 0;
+                count = 0;
+        }
+        // button was down and is being pushed again, move servo out
+        else if (pb2 == 1 && state2 == 1) {
+            button2.moveServoOut();
+            state2 = 2;
+            led2 = 1;
+        }
+        Thread::wait(100); // wait till thread is done
     }
-    Thread::wait(200); // wait till thread is done
 }
 
 void submit_thread()
@@ -59,51 +92,69 @@
 {
     // read pb_start
     // if 1
-    start == 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
-    pb1.mode(PullUp);
-    wait(.001);
+    //pb1.mode(PullUp);
+    //pb2.mode(PullUp);
+    //wait(.001);
+    
     // servo begins at 30 degrees
     // replace with a button setup function
     for(int i=0; i<=3; i++) {
         myservo = i/100.0;
         wait(0.01);
     }
+    for(int i=0; i<=3; i++) {
+        myservo2 = i/100.0;
+        wait(0.01);
+    }
 
+    led1 = 1;
+    led2 = 1;
+    Thread t3(button_thread);
+    Thread t4(button2_thread);
+    t3.start(button_thread);
+    t4.start(button2_thread);
+
+    // start threads for reset, mode, start
+    //Thread t1(start_thread);
+    //pc.printf("start thread");
+    //Thread t2(submit_thread);
+    // setup SDcard and Speaker
+
+    // when started
+    //while (start == 0){}
+    //Thread t3(button_thread);
+    //pc.printf("button thread");
+    //Thread t4(button2_thread);
+    //pc.printf("button2 thread");
+
+    //Thread t3(submit_thread);
+
+    // when submitted
+    //while (submit == 0) {}
+
+    // start button threads and submit thread
+    // if submit close button threads and submit thread
+    // check result
+    // play results on speaker
+    // save results
+
+    // read linear potentiometer
+    //if (linpot < 0.5) {
+    //float potval = linpot;
+    //pc.printf("linear pot: %f\n", potval);
     // MAIN THREAD
     while(true) {
-
-        // start threads for reset, mode, start
-        Thread t1(start_thread);
-        // setup SDcard and Speaker
-        
-        // when started
-        while (start == 0) {}
-        Thread t2(button_thread);
-        
-        //Thread t3(submit_thread);
-
-        // when submitted
-        //while (submit == 0) {}
-
-        // start button threads and submit thread
-        // if submit close button threads and submit thread
-        // check result
-        // play results on speaker
-        // save results
-
-        // read linear potentiometer
-        //if (linpot < 0.5) {
-        //float potval = linpot;
-        //pc.printf("linear pot: %f\n", potval);
-        
-        Thread::wait(200); // wait till thread is done
+        Thread::wait(500); // wait till thread is done
     }
 }
\ No newline at end of file