A retro gaming programme, designed for use on a portable embedded system. Incorporates power saving techniques.

Dependencies:   ConfigFile N5110 PowerControl beep mbed

Revision:
76:c11cb774bd27
Parent:
75:445c1ea33944
Child:
77:b37c6a0d8758
--- a/main.cpp	Sun May 10 13:31:26 2015 +0000
+++ b/main.cpp	Sun May 10 19:29:46 2015 +0000
@@ -48,23 +48,23 @@
     ///check file size - if size acceptable, prints buffer to location.
     if (cfg.getValue(key1, &player1initials[0], sizeof(player1initials))) {
         serial.printf("%s\n",player1initials);
-        char a = (int)player1initials[10];
-        highScore1 = a; 
-        serial.printf("%c\n",highScore1);
+        char a = player1initials[10];
+        highScore1 = atoi((char*)a); 
+        sprintf (player1initials, "%i",highScore1);
         }
     //check file size - if size acceptable, prints buffer to location.
     if (cfg.getValue(key2, &player2initials[0], sizeof(player2initials))) {
         serial.printf("%s\n",player2initials);
-        char b = (int)player2initials[10];
-        highScore2 = b;
-        serial.printf("%c\n",highScore2);
+        char b = player2initials[10];
+        highScore2 = atoi((char*)b);
+        serial.printf("%c\n",b);
      }
     //check file size - if size acceptable, prints buffer to location.
     if (cfg.getValue(key3, &player3initials[0], sizeof(player3initials))) {
         serial.printf("%s\n",player3initials);
-        char c = (int)player3initials[10];
-        highScore3 = c;
-        serial.printf("%c\n",highScore3);
+        char c = player3initials[10];
+        highScore3 = atoi((char*)c);
+        serial.printf("%c\n",c);
     }
 }
 
@@ -139,20 +139,22 @@
 ///If buttonA is pressed - set flag A.
 void timerExpiredA()
 {
-    if(buttonA == 1) {
+    if((deBounce1.read_ms()>=95)&&(buttonA == 1)) {
         buttonFlagA = 1;
         lcd.setBrightness(1.0);
         serial.printf("flagA set\n");
+        deBounce1.reset();//reset timer
     }
 }
 
 ///If buttonB is pressed - Set flag B.
 void timerExpiredB()
 {
-    if(buttonB == 1) {
+    if((deBounce2.read_ms()>=95)&&(buttonB == 1)) {
         buttonFlagB = 1;
         lcd.setBrightness(1.0);
         serial.printf("flagB set\n");
+        deBounce2.reset();//reset timer
     }
 }
 
@@ -946,6 +948,12 @@
     lcd.refresh();
 }
 
+
+//game flag for delay
+void gameLoop(){
+    gameFlag = 1;
+}
+
 ///Shifts and stores the new scores accordingly.
 void newScore()
 {
@@ -1075,6 +1083,8 @@
     
     while(1) {
         
+        if(gameFlag){
+           gameFlag = 0;         
         //print score - top left of display
         char buffer [14];//create buffer for string
         int length = sprintf(buffer,"Level:%d",score);//insert buffer
@@ -1084,8 +1094,8 @@
         drawNinja();//set character
         drawHazards();//initiates hazards
         hazardFall();//increments hazards towards floor
-        wait(0.1);//slow down objects
-
+        //wait(0.1);
+        
         if (printFlag) {  //if flag set, clear flag and print joystick values to serial port
             printFlag = 0;
             if (joystick.direction == RIGHT) {
@@ -1139,7 +1149,7 @@
                 write();
                 resetGame();//reset values
                 break;
-            }
+            }            
             lcd.refresh();//refresh screen
             startrek();//clears unset pixels, keeps set pixels
 
@@ -1181,8 +1191,10 @@
                 }
             }
             serial.printf("Score: %i \n",score);//print Score for debug
+     
+     }
         }
-    }
+   }
 }
 
 ///Draws Scores Menu.
@@ -1360,10 +1372,13 @@
     int exitOption = 1;//counter for exit menu
     
     read();//set high scores using flash memory
+    deBounce1.start();//timer buttonA debounce
+    deBounce2.start();//timer buttonB debounce
+    timerGame.attach(&gameLoop,0.1);//timer game delay
     timerA.attach(&timerExpiredA, 0.1);//checks state of buttonA
     timerB.attach(&timerExpiredB, 0.1);//checks state of buttonB
     idleMode();//turns off screen if idle.
-    
+        
     while(1) {
         drawMainMenu();//draws main menu
         mainMenu(mainOption);//presents main menu options