solved workshop exercise

Files at this revision

API Documentation at this revision

Comitter:
marcozecchini
Date:
Tue Mar 12 15:00:59 2019 +0000
Commit message:
Final commit;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r f4a1a7ba4cdc main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Mar 12 15:00:59 2019 +0000
@@ -0,0 +1,133 @@
+#include "main.h"
+
+string readline_serial(){ //LO FANNO
+    string buffer = "";
+    
+    while(!expired){
+        if(pc.readable() > 0){
+            char c = pc.getc();
+            buffer = buffer + c;
+            if (c=='!'){
+                return buffer.substr(0, buffer.length()-1);
+            }
+        }
+    }
+    
+    expired = false;
+    return 0;
+    
+}
+
+string readline_questions(){ // LO MANTENGO
+    size_t pos = questions.find("!"); 
+    string buffer = questions.substr(0, pos);
+    read_questions += buffer;
+    questions = questions.substr(pos+1);
+    
+    return buffer;
+}
+
+void parse_line(string line, question_t *res){ //LO FANNO
+    int word = 0;
+    size_t i = 0;
+    string temp = "";
+    
+    for (word = 0; word < 5; word++){
+        size_t pos = line.find(";");
+        res[word].answer = line.substr(i, pos);
+        res[word].shown = 0;
+        line = line.substr(pos+1);
+            
+    }
+}
+
+void expired_timeout(){ //LO FANNO
+    expired = true;
+    timeout.detach(); 
+}
+
+void pressed(){ //LO FANNO
+    if (!answering) pc.printf(">>> GAME PAUSED FOR 30 seconds <<<\n\r");
+    
+    wait(30.0);
+    
+    pc.printf(">>> GAME RESUMED <<<\n\r");
+    
+}
+
+void toggling(){ //LO FANNO
+    while(answering){
+        led = !led;
+        wait(0.1);
+    }    
+}
+
+int main()
+{   
+    mybutton.fall(&pressed);
+    pc.printf("Welcome to IoTQuiz! Are you ready to play? When you are ready send !\n\r");
+    readline_serial();
+
+    pc.printf("How many players are there?\n\r");
+    number_of_participants = atoi(readline_serial().data());
+    
+    pc.printf("There are %d participants\n\r", number_of_participants);
+    int turn = 0;
+    int results[number_of_participants];
+    while (turn < TURN * number_of_participants){
+        
+        int participant = turn % number_of_participants;
+        if (turn < number_of_participants) results[participant] = 0; // properly initialization of results.
+        
+        pc.printf("It's the turn of player %d\n\r", participant);
+        wait(5.0);
+        
+        answering = true; //from now on no more pause
+        question_t parsed_line[5];
+        parse_line(readline_questions(), parsed_line);
+        pc.printf(">>> QUESTION: %s\n\r", parsed_line[0].answer.data());
+        parsed_line[0].shown = 1;
+        
+        //To order randomly the answers
+        int question_shown = 0;
+        while (question_shown < 4){
+                int i = rand() % 4 + 1;
+                if (parsed_line[i].shown == 0){
+                    pc.printf(">>> %d) %s\n\r", ++question_shown, parsed_line[i].answer.data());
+                    parsed_line[i].shown = 1;
+                    parsed_line[i].shown_as = question_shown;   
+                }
+        }
+        
+        pc.printf("\n\r>>> Select the correct answer, you have 10 seconds! (A number among 1 and 4)\n\r");
+        
+        //timeout starting
+        timeout.attach(&expired_timeout, 10.0);
+        Thread thread; //thread toggling led
+        thread.start(toggling); // starts
+        
+        int correct = atoi(readline_serial().data());
+        answering = false;
+        
+        //join the thread and detach the timeout
+        thread.join();
+        led = 0;
+        timeout.detach();
+        
+        if (correct == parsed_line[1].shown_as) {
+            results[participant] += 1;
+            pc.printf("\n\r>>>Right Answer!\n\r");
+        }
+        else{
+            pc.printf("\n\r>>> Wrong Answer or Time Expired!\n\r");
+        }
+    
+        turn +=1 ;
+        if (turn % number_of_participants == 0){
+            for (int i = 0; i<number_of_participants; i++){
+                pc.printf("\n\rPLAYER %d: %d points\n\r", i, results[i]);
+            }    
+        }
+    }
+    
+}
diff -r 000000000000 -r f4a1a7ba4cdc main.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.h	Tue Mar 12 15:00:59 2019 +0000
@@ -0,0 +1,27 @@
+#include "mbed.h"
+
+#include <stdlib.h>
+#include <string>
+#define TURN 5
+
+using namespace std;
+
+typedef struct question {
+    string answer;
+    bool shown;
+    int shown_as;
+} question_t;
+
+//Global variable
+Serial pc(SERIAL_TX, SERIAL_RX);
+DigitalOut led(LED1);
+Timeout timeout;
+
+InterruptIn mybutton(USER_BUTTON);
+
+int number_of_participants = 0;
+bool expired = false, paused = false, answering = false;
+string questions = "When was Linux invented? ; 1991 ; 2005 ; 1965 ; 1999!Which one of the following is NOT open source? ; Skype ; Docker ; Python ; PHP ; LibreOffice!What does the 'A' stand for in the term LAMP stack? ; Apache ; Ajax ; ActiveX ; ASP.NET!Firefox was made by what community? ; Mozilla ; Apple ; IBM ; Oracle!What animal is Tux, the Linux mascot? ; Penguin ; Mouse ; Horse ; Spider!Which Linux OS is commonly recommended for beginners? ; Ubuntu ; Gentoo ; FreeBSD ; Fedora!What is Audacity used for? ; Audio ; Art ; Programming ; Gaming!Which one of the following is open source? ; MySQL ; Windows ; iTunes ; Photoshop!What language was the Linux kernel written in? ; C ; Java ; C++ ; Ruby!Which one of the following is an open source license? ; GPL ; FTP ; GUID ; API!";
+string read_questions = "";
+
+string readline_serial();
\ No newline at end of file
diff -r 000000000000 -r f4a1a7ba4cdc mbed-os.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Tue Mar 12 15:00:59 2019 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#51d55508e8400b60af467005646c4e2164738d48