Zhen Liu / Mbed 2 deprecated Mini_project

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "uLCD_4DGL.h"
00004 #include <vector>
00005 #include <stdlib.h>
00006 #include <time.h>
00007 
00008 DigitalIn yes(p5);
00009 DigitalIn no(p6);
00010 uLCD_4DGL lcd(p28, p27, p30);
00011 Mutex mutex; 
00012 LocalFileSystem local("local");  
00013 
00014 void setup()
00015 {
00016     lcd.baudrate(3000000);
00017     lcd.background_color(0);
00018     lcd.cls();
00019     lcd.text_width(3); //4X size text
00020     lcd.text_height(3);
00021     //lcd.printf("Initializing...");
00022     lcd.text_string("Initializing...", 0,0,FONT_8X12,BLUE);
00023 }
00024 
00025 void timer(void const *in) {
00026     int* timeNo = (int*) in;
00027     lcd.filled_rectangle(0,90,100,95,RED);
00028     for (int i = 10; i>=0; i--) {
00029         mutex.lock();
00030         lcd.locate(8,10);
00031         lcd.printf("00:%02d", i);
00032         lcd.filled_rectangle(i*10,90,(i+1)*10,95,BLACK);
00033         mutex.unlock();
00034         Thread::wait(1000);
00035     }
00036     *timeNo = 0;
00037 }
00038 
00039 int main() {
00040     yes.mode(PullUp);
00041     no.mode(PullUp);
00042     lcd.baudrate(3000000);
00043     lcd.background_color(0);
00044     lcd.cls();
00045     lcd.color(WHITE);
00046     
00047     lcd.printf("Initializing...");
00048     int answer = 1;
00049     bool flag = false;
00050     int response = -1;
00051     int score = 0;
00052     int time_count;
00053     int* timePointer = &time_count;
00054     int question_no = 0;
00055     bool endFlag = false;
00056     char* question = (char*) malloc(sizeof(char) * 150+1);;
00057     char* answerStr = (char*) malloc(sizeof(char)+1);;
00058     wait(2);
00059     lcd.cls();
00060     lcd.printf("press to start");
00061     while (yes == true && no == true) {}
00062     lcd.cls();
00063     lcd.printf("Quiz starts");
00064     wait(2);
00065     lcd.cls();
00066     lcd.printf("Ready");
00067     wait(2);
00068     //===========================
00069     int total_no_questions = 10;
00070     vector<int> question_inds(total_no_questions);
00071     for (int i=0; i<total_no_questions; i++) {
00072         question_inds[i] = i;
00073     }
00074     int randind;
00075     int rand_no;
00076     
00077     int max_question_no = 5; // maximum_number; assert it smaller than total_no_questions!!!
00078     //============================
00079     while (!endFlag) { 
00080         if (question_no == max_question_no) {
00081             lcd.cls();
00082             lcd.printf("Quiz ends");
00083             wait(2);
00084             endFlag = true;
00085             break;
00086         }
00087         srand(time(NULL));
00088         randind = (rand() % total_no_questions);
00089         total_no_questions--;
00090         rand_no = question_inds[randind];
00091         question_inds.erase(question_inds.begin() + randind);
00092         char file_name[100];
00093         char no_str[1];
00094         char txt[4];
00095         sprintf(no_str, "%d",rand_no);
00096         sprintf(txt, "%s", ".txt");
00097         strcpy(file_name, "/local/test");
00098         strncat(file_name, no_str,10);
00099         strncat(file_name, txt,10);
00100         FILE *f = fopen(file_name , "r"); 
00101         fgets(question, 150, f);
00102         fgets(answerStr, 150, f);
00103         char buffer[1];
00104         sprintf(buffer, "%s", answerStr);
00105         fclose(f);
00106         lcd.cls();
00107         lcd.printf("Next Question");
00108         wait(2);
00109         question_no++;
00110         if (buffer[0] == 't') {
00111             answer = 1;
00112         } else {
00113             answer = 0;
00114         }
00115         for (int i = 3; i > 0; i--) {
00116             lcd.cls();
00117             //lcd.text_char(i, 12, 12, RED);
00118             lcd.printf("%d",i);
00119             Thread::wait(1000);
00120         }
00121         lcd.color(WHITE);
00122         lcd.cls();
00123         lcd.locate(1,1);
00124         lcd.printf(question); // to be corrected
00125         lcd.locate(2,14);
00126         lcd.printf("Yes");
00127         lcd.locate(14,14);
00128         lcd.printf("No");
00129         lcd.locate(0,8);
00130         lcd.printf("%02d out of %02d", question_no, max_question_no);
00131         time_count = 10;
00132         Thread timerr(timer, (void *) timePointer);
00133         wait(0.1);
00134         while (time_count >= 0 && flag == false) {
00135             if (yes == true && no == true) {}
00136             else if (yes == true) {
00137                 flag = true;
00138                 response = 1;
00139             } else if (no == true) {
00140                 flag = true;
00141                 response = 0;
00142             }
00143             if (time_count == 0) {flag = true; response = -1;}
00144         }
00145         timerr.terminate();
00146         if (response == 1) {
00147             lcd.locate(2,14);
00148             if (response == answer) {
00149                 lcd.textbackground_color(GREEN);
00150                 lcd.printf("Yes");
00151                 score++;
00152             } else {
00153                 lcd.textbackground_color(RED);
00154                 lcd.printf("Yes");
00155             }
00156         } else if (response == 0) {
00157             lcd.locate(14,14);
00158             if (response == answer) {
00159                 lcd.textbackground_color(GREEN);
00160                 lcd.printf("No");
00161                 score++;
00162             } else {
00163                 lcd.textbackground_color(RED);
00164                 lcd.printf("No");
00165             }
00166         } else if (response == -1) {
00167             lcd.textbackground_color(RED);
00168             lcd.locate(2,14);
00169             lcd.printf("Yes");
00170             lcd.locate(14,14);
00171             lcd.printf("No");
00172         };
00173         wait(2);
00174         lcd.cls();
00175         lcd.locate(0,0);
00176         lcd.textbackground_color(BLACK);
00177         response = -2;
00178         flag = false;
00179     }
00180     lcd.cls();
00181     lcd.printf("Total Score: %2.2f", score/(question_no+0.0) * 100);
00182     while (true) {}
00183 }