solved workshop exercise

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "main.h"
00002 
00003 string readline_serial(){ //LO FANNO
00004     string buffer = "";
00005     
00006     while(!expired){
00007         if(pc.readable() > 0){
00008             char c = pc.getc();
00009             buffer = buffer + c;
00010             if (c=='!'){
00011                 return buffer.substr(0, buffer.length()-1);
00012             }
00013         }
00014     }
00015     
00016     expired = false;
00017     return 0;
00018     
00019 }
00020 
00021 string readline_questions(){ // LO MANTENGO
00022     size_t pos = questions.find("!"); 
00023     string buffer = questions.substr(0, pos);
00024     read_questions += buffer;
00025     questions = questions.substr(pos+1);
00026     
00027     return buffer;
00028 }
00029 
00030 void parse_line(string line, question_t *res){ //LO FANNO
00031     int word = 0;
00032     size_t i = 0;
00033     string temp = "";
00034     
00035     for (word = 0; word < 5; word++){
00036         size_t pos = line.find(";");
00037         res[word].answer = line.substr(i, pos);
00038         res[word].shown = 0;
00039         line = line.substr(pos+1);
00040             
00041     }
00042 }
00043 
00044 void expired_timeout(){ //LO FANNO
00045     expired = true;
00046     timeout.detach(); 
00047 }
00048 
00049 void pressed(){ //LO FANNO
00050     if (!answering) pc.printf(">>> GAME PAUSED FOR 30 seconds <<<\n\r");
00051     
00052     wait(30.0);
00053     
00054     pc.printf(">>> GAME RESUMED <<<\n\r");
00055     
00056 }
00057 
00058 void toggling(){ //LO FANNO
00059     while(answering){
00060         led = !led;
00061         wait(0.1);
00062     }    
00063 }
00064 
00065 int main()
00066 {   
00067     mybutton.fall(&pressed);
00068     pc.printf("Welcome to IoTQuiz! Are you ready to play? When you are ready send !\n\r");
00069     readline_serial();
00070 
00071     pc.printf("How many players are there?\n\r");
00072     number_of_participants = atoi(readline_serial().data());
00073     
00074     pc.printf("There are %d participants\n\r", number_of_participants);
00075     int turn = 0;
00076     int results[number_of_participants];
00077     while (turn < TURN * number_of_participants){
00078         
00079         int participant = turn % number_of_participants;
00080         if (turn < number_of_participants) results[participant] = 0; // properly initialization of results.
00081         
00082         pc.printf("It's the turn of player %d\n\r", participant);
00083         wait(5.0);
00084         
00085         answering = true; //from now on no more pause
00086         question_t parsed_line[5];
00087         parse_line(readline_questions(), parsed_line);
00088         pc.printf(">>> QUESTION: %s\n\r", parsed_line[0].answer.data());
00089         parsed_line[0].shown = 1;
00090         
00091         //To order randomly the answers
00092         int question_shown = 0;
00093         while (question_shown < 4){
00094                 int i = rand() % 4 + 1;
00095                 if (parsed_line[i].shown == 0){
00096                     pc.printf(">>> %d) %s\n\r", ++question_shown, parsed_line[i].answer.data());
00097                     parsed_line[i].shown = 1;
00098                     parsed_line[i].shown_as = question_shown;   
00099                 }
00100         }
00101         
00102         pc.printf("\n\r>>> Select the correct answer, you have 10 seconds! (A number among 1 and 4)\n\r");
00103         
00104         //timeout starting
00105         timeout.attach(&expired_timeout, 10.0);
00106         Thread thread; //thread toggling led
00107         thread.start(toggling); // starts
00108         
00109         int correct = atoi(readline_serial().data());
00110         answering = false;
00111         
00112         //join the thread and detach the timeout
00113         thread.join();
00114         led = 0;
00115         timeout.detach();
00116         
00117         if (correct == parsed_line[1].shown_as) {
00118             results[participant] += 1;
00119             pc.printf("\n\r>>>Right Answer!\n\r");
00120         }
00121         else{
00122             pc.printf("\n\r>>> Wrong Answer or Time Expired!\n\r");
00123         }
00124     
00125         turn +=1 ;
00126         if (turn % number_of_participants == 0){
00127             for (int i = 0; i<number_of_participants; i++){
00128                 pc.printf("\n\rPLAYER %d: %d points\n\r", i, results[i]);
00129             }    
00130         }
00131     }
00132     
00133 }