Zhen Liu / Mbed 2 deprecated Mini_project

Dependencies:   4DGL-uLCD-SE mbed-rtos mbed

Revision:
0:9e4f9c949728
Child:
1:9465d72db54f
diff -r 000000000000 -r 9e4f9c949728 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Mar 09 21:04:44 2015 +0000
@@ -0,0 +1,159 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "uLCD_4DGL.h"
+
+DigitalIn yes(p5);
+DigitalIn no(p6);
+uLCD_4DGL lcd(p28, p27, p30);
+Mutex mutex; 
+LocalFileSystem local("local");  
+
+void setup()
+{
+    lcd.baudrate(3000000);
+    lcd.background_color(0);
+    lcd.cls();
+    lcd.text_width(3); //4X size text
+    lcd.text_height(3);
+    //lcd.printf("Initializing...");
+    lcd.text_string("Initializing...", 0,0,FONT_8X12,BLUE);
+}
+
+void timer(void const *in) {
+    int* timeNo = (int*) in;
+    lcd.filled_rectangle(0,90,100,95,RED);
+    for (int i = 10; i>=0; i--) {
+        mutex.lock();
+        lcd.locate(9,10);
+        lcd.printf("00:%02d", i);
+        lcd.filled_rectangle(i*10,90,(i+1)*10,95,BLACK);
+        mutex.unlock();
+        Thread::wait(1000);
+    }
+    *timeNo = 0;
+}
+
+int main() {
+    yes.mode(PullUp);
+    no.mode(PullUp);
+    lcd.baudrate(3000000);
+    lcd.background_color(0);
+    lcd.cls();
+    lcd.color(WHITE);
+    
+    lcd.printf("Initializing...");
+    int answer = 1;
+    bool flag = false;
+    int response = -1;
+    int score = 0;
+    int time;
+    int* timePointer = &time;
+    int question_no = 0;
+    bool endFlag = false;
+    char* question = (char*) malloc(sizeof(char) * 150+1);;
+    char* answerStr = (char*) malloc(sizeof(char)+1);;
+    wait(2);
+    lcd.cls();
+    lcd.printf("press to start");
+    while (yes == true && no == true) {}
+    lcd.cls();
+    lcd.printf("Quiz starts");
+    wait(2);
+    lcd.cls();
+    lcd.printf("Ready");
+    wait(2);
+    int total_no_questions = 100;
+    std::vector<std::string> question_inds;
+    for (i=0; i<total_no_questions; i++) {
+        question_ind[i] = i;
+    }
+    FILE *f = fopen("/local/test.txt", "r"); 
+    char buffer[1];
+    while (!endFlag) { 
+        fgets(question, 150, f);
+        fgets(answerStr, 150, f);
+        sprintf(buffer, "%s", answerStr);
+        if (buffer[0] != 't' && buffer[0] != 'f') {
+            lcd.cls();
+            lcd.printf("Quiz ends");
+            wait(2);
+            endFlag = true;
+            break;
+        }
+        lcd.cls();
+        lcd.printf("Next Question");
+        wait(2);
+        question_no++;
+        if (buffer[0] == 't') {
+            answer = 1;
+        } else {
+            answer = 0;
+        }
+        for (int i = 3; i > 0; i--) {
+            lcd.cls();
+            //lcd.text_char(i, 12, 12, RED);
+            lcd.printf("%d",i);
+            Thread::wait(1000);
+        }
+        lcd.color(WHITE);
+        lcd.cls();
+        lcd.locate(1,1);
+        lcd.printf(question); // to be corrected
+        lcd.locate(2,14);
+        lcd.printf("Yes");
+        lcd.locate(14,14);
+        lcd.printf("No");
+        time = 10;
+        Thread timerr(timer, (void *) timePointer);
+        wait(0.1);
+        while (time >= 0 && flag == false) {
+            if (yes == true && no == true) {}
+            else if (yes == true) {
+                flag = true;
+                response = 1;
+            } else if (no == true) {
+                flag = true;
+                response = 0;
+            }
+            if (time == 0) {flag = true; response = -1;}
+        }
+        timerr.terminate();
+        if (response == 1) {
+            lcd.locate(2,14);
+            if (response == answer) {
+                lcd.textbackground_color(GREEN);
+                lcd.printf("Yes");
+                score++;
+            } else {
+                lcd.textbackground_color(RED);
+                lcd.printf("Yes");
+            }
+        } else if (response == 0) {
+            lcd.locate(14,14);
+            if (response == answer) {
+                lcd.textbackground_color(GREEN);
+                lcd.printf("No");
+                score++;
+            } else {
+                lcd.textbackground_color(RED);
+                lcd.printf("No");
+            }
+        } else if (response == -1) {
+            lcd.textbackground_color(RED);
+            lcd.locate(2,14);
+            lcd.printf("Yes");
+            lcd.locate(14,14);
+            lcd.printf("No");
+        };
+        wait(2);
+        lcd.cls();
+        lcd.locate(0,0);
+        lcd.textbackground_color(BLACK);
+        response = -2;
+        flag = false;
+    }
+    fclose(f);
+    lcd.cls();
+    lcd.printf("Total Score: %2.2f", score/(question_no+0.0) * 100);
+    while (true) {}
+}
\ No newline at end of file