Display text on screen.

Dependencies:   TextLCD mbed MaxSonar RTC-DS1307

Fork of TextLCD_HelloWorld by Simon Ford

Revision:
9:39190ed25585
Parent:
8:8ce2ab8191cf
Child:
10:21c24327e65f
--- a/main.cpp	Thu May 25 09:15:49 2017 +0000
+++ b/main.cpp	Thu May 25 10:55:51 2017 +0000
@@ -12,8 +12,24 @@
 DigitalOut screen1(PTE5);
 DigitalOut screen2(PTE4);
 
+//All functions for controlling screens
 void UpdateScreen(DigitalOut screen, string text);
 void UpdateScreen(DigitalOut screen, char text[1024]);
+void ClearAllScreen();
+
+//All funcitons for range sensors
+void UpdateRange();
+
+//All functions for menu page
+void GameOption();
+void StartGame(int gameOptionLocation);
+
+//All functions for exit screen menu
+bool ExitScreen();
+bool CheckExit(float r1, float r2);
+
+//all functions for Multi-Maths game
+void MultiChoice();
 void DisplayNewQuestion();
 bool CheckCorrect(int screenNumber);
 
@@ -28,15 +44,15 @@
 
 int questionCount = 0;
 
-int main() {
-    UpdateScreen(screen2, "Hello!");
-    
-    MaxSonar *range1;
+//all functions for jumping game
+void Jumping();
+
+MaxSonar *range1;
     float r1;
     
     MaxSonar *range2;
     float r2;
- 
+int main() {
     // Create and configure object for 3.3V powered LV-series device, 
     // accessed with analog reads (in cm) on p16, triggered by p7.
     range1 = new MaxSonar(MS_LV, MS_ANALOG, PTB8, PTC2);
@@ -46,18 +62,70 @@
     range2 = new MaxSonar(MS_LV, MS_ANALOG, PTB9, PTB3);
     range2->setVoltage(3.3);
     range2->setUnits(MS_CM);
- 
-    while(1) {
-        // Trigger read, wait 49ms until ranger finder has
-        // finished, then read. 
-        range1->triggerRead();
-        wait_ms(49);
-        r1 = range1->read();
+    
+    ClearAllScreen();
+    
+    GameOption();
+}
+
+int gameOptionLocation = 0;
+string gameName[2] = {"Muti-Maths", "Jumping!"};
+void GameOption()
+{
+    ClearAllScreen();
+    
+    UpdateScreen(questionScreen, "Select a game:  >" + gameName[gameOptionLocation]);
+    
+    bool gameOption = true;    
+    while(gameOption)
+    {
+        UpdateRange();
+        
+        if(r1 < 25 && r2 < 25)
+        {
+            UpdateScreen(questionScreen, "Entering " + gameName[gameOptionLocation] + ".....");
+            wait(1);
+            
+            gameOption = false;
+        }
         
-        range2->triggerRead();
-        wait_ms(49);
-        r2 = range2->read();
-      
+        else if(r1 < 20)
+        {
+            if(gameOptionLocation > 0) { gameOptionLocation--; UpdateScreen(questionScreen, "Select a game:  >" + gameName[gameOptionLocation]); }
+        }
+        else if(r2 < 20)
+        {
+            if(gameOptionLocation < 1) { gameOptionLocation++; UpdateScreen(questionScreen, "Select a game:  >" + gameName[gameOptionLocation]); }
+        }
+    }
+    
+    StartGame(gameOptionLocation);
+}
+
+void StartGame(int gameOptionLocation)
+{
+    switch(gameOptionLocation)
+    {
+        case 0:
+            MultiChoice();
+        case 1:
+            Jumping();
+    }    
+}
+
+void MultiChoice()
+{
+    bool multiMaths = true;
+    
+    DisplayNewQuestion();
+    while(multiMaths) {
+        UpdateRange();
+        
+        if(CheckExit(r1, r2))
+        {
+            GameOption();    
+        }
+        
         if(r1 < 20)
         {
             if(CheckCorrect(1))
@@ -89,16 +157,6 @@
             }
         }    
         
-        /* 
-        // Print and delay 0.5s.
-        char range1_char_array[1024];
-        char range2_char_array[1024];
-        sprintf (range1_char_array, "Range: %.3f cm\n", r1);
-        sprintf (range2_char_array, "Range: %.3f cm\n", r2);
-        UpdateScreen(questionScreen, range1_char_array);
-        UpdateScreen(screen2, range2_char_array);
-        */
-        
         wait(0.5);
     }
 }
@@ -131,6 +189,64 @@
     }
 }
 
+void Jumping()
+{
+        
+}
+
+string exitOption[2] = {"Yes", "No"};
+bool CheckExit(float r1, float r2)
+{
+    if(r1 < 25 && r2 < 25)
+    {
+        return ExitScreen();
+    }
+    else
+    {
+        return false;
+    }
+}
+
+bool ExitScreen()
+{
+    int exitOption_int = 0;
+    UpdateScreen(questionScreen, "AreYouSureYouWantToExit? >" + exitOption[exitOption_int]);
+    
+    wait(1);
+    
+    bool choosing = true;
+    while(choosing)
+    {
+        wait(0.5);
+        UpdateRange();
+        if(r1 < 25 && r2 < 25)
+        {
+            if(exitOption_int == 0) 
+            { 
+                choosing = false;
+                UpdateScreen(questionScreen, "Exiting...");
+                wait(1);
+                return true; 
+            }
+            else 
+            { 
+                choosing = false; 
+                UpdateScreen(questionScreen, "Returning to game");
+                wait(1);
+                return false; 
+            }
+        }
+        else if(r1 < 20)
+        { 
+            if(exitOption_int > 0) { exitOption_int--; UpdateScreen(questionScreen, "AreYouSureYouWantToExit? >" + exitOption[exitOption_int]); }
+        }
+        else if(r2 < 20)
+        {
+            if(exitOption_int < 1) { exitOption_int++; UpdateScreen(questionScreen, "AreYouSureYouWantToExit? >" + exitOption[exitOption_int]); }
+        }
+    }
+}
+
 void UpdateScreen(DigitalOut screen, string text)
 {
     //disable all E pin for all screens
@@ -169,3 +285,25 @@
         lcd.printf(text);
     }
 }
+
+void ClearAllScreen()
+{
+    questionScreen = 1;
+    screen1 = 1;
+    screen2 = 1;
+    
+    lcd.cls();
+}
+
+void UpdateRange()
+{
+    // Trigger read, wait 49ms until ranger finder has
+    // finished, then read. 
+    range1->triggerRead();
+    wait_ms(49);
+    r1 = range1->read();
+        
+    range2->triggerRead();
+    wait_ms(49);
+    r2 = range2->read();
+}
\ No newline at end of file