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

Dependencies:   ConfigFile N5110 PowerControl beep mbed

Revision:
77:b37c6a0d8758
Parent:
76:c11cb774bd27
Child:
78:e2fc547c6b99
--- a/main.cpp	Sun May 10 19:29:46 2015 +0000
+++ b/main.cpp	Sun May 10 20:04:26 2015 +0000
@@ -11,9 +11,9 @@
 #include "tower.h"
 #include <ctime>
 
-///writes to CFG file. 
+///writes to CFG file.
 void write()
-{    
+{
     ///Sets a Key and Value.
     if (!cfg.setValue("Open1", player1initials)) {//key/value
         error("Failure to set a value.\n");
@@ -35,7 +35,7 @@
 
 ///Reads CFG file.
 void read()
-{    
+{
     //Checks if key matches.
     char *key1 = "Open1";
     char *key2 = "Open2";
@@ -49,16 +49,16 @@
     if (cfg.getValue(key1, &player1initials[0], sizeof(player1initials))) {
         serial.printf("%s\n",player1initials);
         char a = player1initials[10];
-        highScore1 = atoi((char*)a); 
+        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 = 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);
@@ -81,7 +81,7 @@
 
 ///Finite State Machine for Struct data members.
 STYP fsm[27] = {
-    
+
     ///Outputs 1,2 and 3 for Initial Arrays.
     {'A','A','A',{1,26}},
     {'B','B','B',{2,0}},
@@ -275,7 +275,7 @@
     lcd.drawRect(a14,36,0,0,1);
     lcd.drawRect(a15,37,0,0,1);
     lcd.drawRect(a16,38,0,0,1);
-   
+
     lcd.refresh();
 }
 
@@ -293,44 +293,44 @@
         a10 = 80;
     //right shoulder
     if(a7 > 79)
-        a7 = 79;        
-    //right leg    
+        a7 = 79;
+    //right leg
     if(a2 > 78)
         a2 = 78;
-    //jaw    
+    //jaw
     if(a6 > 78)
         a6 = 78;
     //left eye
     if(a13 > 78)
-        a13 = 78;  
-    //centre    
+        a13 = 78;
+    //centre
     if(a3 > 77)
         a3 = 77;
     //back of the head
     if(a4 > 76 )
-     a4= 76;
-     //topf of the head
-     if(a5 > 76)
-     a5 = 76;
+        a4= 76;
+    //topf of the head
+    if(a5 > 76)
+        a5 = 76;
     //left leg
     if(a1>76)
-    a1 = 76;
+        a1 = 76;
     //sword
     if(a16>75)
-    a16 = 75;
+        a16 = 75;
     //left shoulder
     if(a8 >74)
-    a8 = 74;
+        a8 = 74;
     //left arm
     if(a9>74)
-    a9 = 74;
+        a9 = 74;
     //sword
     if(a15>74)
-    a15 = 74;
+        a15 = 74;
     //sword
     if(a14 > 73)
-    a14 = 73;
-    
+        a14 = 73;
+
     //right eye
     if(a11 < 5 )
         a11 = 5;
@@ -342,43 +342,43 @@
         a10 = 9;
     //right shoulder
     if(a7 < 8)
-        a7 = 8;        
-    //right leg    
+        a7 = 8;
+    //right leg
     if(a2 < 7)
         a2 = 7;
-    //jaw    
+    //jaw
     if(a6 < 3)
         a6 = 3;
     //left eye
     if(a13 < 3)
-        a13 = 3;  
-    //centre    
+        a13 = 3;
+    //centre
     if(a3 < 6)
         a3 = 6;
     //back of the head
     if(a4 < 7)
-     a4= 7;
-     //top of the head
-     if(a5 < 3)
-     a5 = 3;
+        a4= 7;
+    //top of the head
+    if(a5 < 3)
+        a5 = 3;
     //left leg
     if(a1 < 5)
-    a1 = 5;
+        a1 = 5;
     //sword
     if(a16 < 8)
-    a16 = 8;
+        a16 = 8;
     //left shoulder
     if(a8 < 3)
-    a8 = 3;
+        a8 = 3;
     //left arm
     if(a9 < 3)
-    a9 = 3;
+        a9 = 3;
     //sword
     if(a15 < 9)
-    a15 = 9;
+        a15 = 9;
     //sword
     if(a14 < 10)
-    a14 = 10;     
+        a14 = 10;
 }
 
 ///Resets Global variables for Character/Hazard co-ordinates.
@@ -401,7 +401,7 @@
     a14 = 19;
     a15 = 20;
     a16 = 21;
-    
+
     score = 0;
 
     //in this case the X values are given a
@@ -415,7 +415,7 @@
     randY4 = 0;
     randY5 = 0;
     randY6 = 0;
-    
+
     lcd.clear();
 }
 
@@ -834,7 +834,7 @@
             refreshCursor2();
             refreshCursor3();
             if(buttonFlagA) { //select easy
-                //rightness(1.0); 
+                //rightness(1.0);
                 buttonFlagA = 0;
                 fall = 1;
             }
@@ -846,7 +846,7 @@
             refreshCursor3();
 
             if(buttonFlagA) { //select normal
-                //rightness(1.0); 
+                //rightness(1.0);
                 buttonFlagA = 0;
                 fall = 2;
             }
@@ -858,7 +858,7 @@
             refreshCursor2();
 
             if(buttonFlagA) { //select difficult
-                //rightness(1.0); 
+                //rightness(1.0);
                 buttonFlagA = 0;
                 fall = 3;
             }
@@ -926,7 +926,7 @@
         refreshCursor1();
         refreshCursor2();
 
-        if(buttonFlagA) { 
+        if(buttonFlagA) {
             buttonFlagA =0;
             FX = 1;
             serial.printf("FX = %d\n",FX);
@@ -950,7 +950,8 @@
 
 
 //game flag for delay
-void gameLoop(){
+void gameLoop()
+{
     gameFlag = 1;
 }
 
@@ -1038,7 +1039,7 @@
                 z=fsm[state3].output3;
                 lcd.printChar(z,52,2);
             }
-            if(buttonFlagA) { 
+            if(buttonFlagA) {
                 actionButtons();
                 buttonFlagA = 0;
                 buttonFlagB = 0;
@@ -1080,11 +1081,9 @@
     actionButtons();
     lcd.clear();//clears screen
     drawBackground();//draw background
-    
+
     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
@@ -1093,108 +1092,109 @@
         actionButtons();
         drawNinja();//set character
         drawHazards();//initiates hazards
-        hazardFall();//increments hazards towards floor
-        //wait(0.1);
         
-        if (printFlag) {  //if flag set, clear flag and print joystick values to serial port
-            printFlag = 0;
-            if (joystick.direction == RIGHT) {
-                serial.printf(" RIGHT\n");
-                ninjaRight();//move right
-                ninjaBoundaries();
-            }
-            //check joystick direction
-            if (joystick.direction == LEFT) {
-                serial.printf(" LEFT\n");
-                ninjaLeft();//move left
-                ninjaBoundaries();
-            }
-            if (joystick.direction == CENTRE)
-                serial.printf(" CENTRE\n");
-            if (joystick.direction == UNKNOWN)
-                serial.printf(" Unsupported direction\n");
+        if(gameFlag) {
+            gameFlag = 0;
+            hazardFall();//increments hazards towards floor
+            if (printFlag) {  //if flag set, clear flag and print joystick values to serial port
+                printFlag = 0;
+                if (joystick.direction == RIGHT) {
+                    serial.printf(" RIGHT\n");
+                    ninjaRight();//move right
+                    ninjaBoundaries();
+                }
+                //check joystick direction
+                if (joystick.direction == LEFT) {
+                    serial.printf(" LEFT\n");
+                    ninjaLeft();//move left
+                    ninjaBoundaries();
+                }
+                if (joystick.direction == CENTRE)
+                    serial.printf(" CENTRE\n");
+                if (joystick.direction == UNKNOWN)
+                    serial.printf(" Unsupported direction\n");
 
-            //integer to represent character being
-            //struck by falling object
-            int contactPoint = 0;
+                //integer to represent character being
+                //struck by falling object
+                int contactPoint = 0;
 
-            //contact points
-            if(lcd.getPixel((a3),32))
-                contactPoint++;
-            if(lcd.getPixel((a3+3),32))
-                contactPoint++;
-            if(lcd.getPixel((a3-3),32))
-                contactPoint++;
+                //contact points
+                if(lcd.getPixel((a3),32))
+                    contactPoint++;
+                if(lcd.getPixel((a3+3),32))
+                    contactPoint++;
+                if(lcd.getPixel((a3-3),32))
+                    contactPoint++;
 
-            //if contact point is not zero
-            //character has been hit
-            //and the game ends
-            if ( contactPoint !=0) {
-                lcd.printString("Game Over",17,2);
-                lcd.inverseMode();
-                buzzer.beep(2000,0.2);//frequeny/duration
-                wait(0.5);
-                lcd.normalMode();
-                wait(0.5);
-                lcd.inverseMode();
-                buzzer.beep(2000,0.2);
-                wait(0.5);
-                lcd.normalMode();
-                wait(0.5);
-                lcd.inverseMode();
-                buzzer.beep(2000,0.2);
-                wait(0.5);
-                lcd.normalMode();
-                newScore();//enter initial screen if previous scores are beaten
-                write();
-                resetGame();//reset values
-                break;
-            }            
-            lcd.refresh();//refresh screen
-            startrek();//clears unset pixels, keeps set pixels
+                //if contact point is not zero
+                //character has been hit
+                //and the game ends
+                if ( contactPoint !=0) {
+                    lcd.printString("Game Over",17,2);
+                    lcd.inverseMode();
+                    buzzer.beep(2000,0.2);//frequeny/duration
+                    wait(0.5);
+                    lcd.normalMode();
+                    wait(0.5);
+                    lcd.inverseMode();
+                    buzzer.beep(2000,0.2);
+                    wait(0.5);
+                    lcd.normalMode();
+                    wait(0.5);
+                    lcd.inverseMode();
+                    buzzer.beep(2000,0.2);
+                    wait(0.5);
+                    lcd.normalMode();
+                    newScore();//enter initial screen if previous scores are beaten
+                    write();
+                    resetGame();//reset values
+                    break;
+                }
+                lcd.refresh();//refresh screen
+                startrek();//clears unset pixels, keeps set pixels
 
 //Exit Menu (Back button pressed)//
-            if(buttonFlagB) { 
-                buttonFlagB = 0;//reset flags
-                buttonFlagA = 0;
-                actionButtons();
-                drawExitMenu();//draws the exit menu
+                if(buttonFlagB) {
+                    buttonFlagB = 0;//reset flags
+                    buttonFlagA = 0;
+                    actionButtons();
+                    drawExitMenu();//draws the exit menu
 
-                while(1) {
-                    exitMenu(exitOption);//presents exit options
+                    while(1) {
+                        exitMenu(exitOption);//presents exit options
 
-                    //'exit' option YES
-                    if((buttonFlagA)&&(exitOption == 0)) { //returns to menu
-                        //rightness(1.0); 
-                        exitOption = 1;//reset intial option value
-                        buttonFlagA = 0;//reset flags
-                        buttonFlagB = 0;
-                        actionButtons();
-                        lcd.clear();//clears screen
-                        resetGame();//resets scores/objects
-                        exitFlag = 1;//sets exit flag
-                        break;
+                        //'exit' option YES
+                        if((buttonFlagA)&&(exitOption == 0)) { //returns to menu
+                            //rightness(1.0);
+                            exitOption = 1;//reset intial option value
+                            buttonFlagA = 0;//reset flags
+                            buttonFlagB = 0;
+                            actionButtons();
+                            lcd.clear();//clears screen
+                            resetGame();//resets scores/objects
+                            exitFlag = 1;//sets exit flag
+                            break;
+                        }
+                        //'exit' option NO - returns to game
+                        if((buttonFlagA)&&(exitOption == 1)) {
+                            buttonFlagA = 0;//resets flags
+                            buttonFlagB = 0;
+                            break;
+                        }
+                        sleep();//put while to sleep
                     }
-                    //'exit' option NO - returns to game
-                    if((buttonFlagA)&&(exitOption == 1)) { 
-                        buttonFlagA = 0;//resets flags
-                        buttonFlagB = 0;
-                        break;
+                    //if 'exit' option YES, resets
+                    //game values returns to main menu
+                    if (exitFlag!=0) { //if exit flag set
+                        exitFlag = 0;//reset flag
+                        break;//break to main menu
                     }
-                    sleep();//put while to sleep
                 }
-                //if 'exit' option YES, resets
-                //game values returns to main menu
-                if (exitFlag!=0) { //if exit flag set
-                    exitFlag = 0;//reset flag
-                    break;//break to main menu
-                }
+                serial.printf("Score: %i \n",score);//print Score for debug
+
             }
-            serial.printf("Score: %i \n",score);//print Score for debug
-     
-     }
         }
-   }
+    }
 }
 
 ///Draws Scores Menu.
@@ -1204,12 +1204,10 @@
     lcd.clear();//clear screen
     drawBackground();//set background
     lcd.printString("High Scores",10,0);//title
-    
+
     //prints scores with names
     lcd.printString(player1initials,5,2);//display
-
     lcd.printString(player2initials,5,3);//display
-
     lcd.printString(player3initials,5,4);//display
 
     while(1) {
@@ -1217,7 +1215,6 @@
 
         //back to menu
         if(buttonFlagB) {
-            //rightness(1.0); 
             buttonFlagA = 0;//reset flags
             buttonFlagB = 0;
             lcd.clear();
@@ -1243,7 +1240,7 @@
 
 //================ Difficulty menu =======================
         if ((option == 0)&&(buttonFlagA)) {
-            //rightness(1.0); 
+            //rightness(1.0);
             buttonFlagA = 0;//reset flag
             actionButtons();
             drawDifficultyMenu();//draws difficulty menu
@@ -1252,7 +1249,7 @@
                 actionButtons();
                 difficultyMenu(subOption);//presents difficulty options
 
-                if(buttonFlagB) { 
+                if(buttonFlagB) {
                     buttonFlagB = 0;//reset flags
                     buttonFlagA = 0;
                     lcd.clear();//clear screen
@@ -1262,7 +1259,7 @@
             }
             drawOptionsMenu();
         }
-//================sound FX menu==========================
+//================sound FX menu==================================
 
         if((option ==1)&&(buttonFlagA)) {
             buttonFlagA = 0;//reset flags
@@ -1275,7 +1272,7 @@
                 soundFXMenu(fxOption);//presents options
 
                 //back to options menu
-                if(buttonFlagB) { 
+                if(buttonFlagB) {
                     buttonFlagB = 0;//reset flags
                     buttonFlagA = 0;
                     lcd.clear();//clear screen
@@ -1337,13 +1334,13 @@
     printFlag = 1;
 }
 
-///Turns screen backlighting.
+///Turns off screen backlighting.
 void screenOff()
 {
     lcd.setBrightness(0.0);
 }
 
-///When idle, calls the funtion void screenOff();
+///When idle, calls the funtion screenOff();
 void idleMode()
 {
     if((buttonFlagA)||(buttonFlagB)||(joystick.direction)) { //call standby mode if idle
@@ -1370,20 +1367,20 @@
     int exitFlag = 0;//exit flag
     int mainOption = 0;//counter for main menu
     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
+    timerGame.attach(&gameLoop,0.75);//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
         actionButtons();//sound light when buttons pressed
-        
+
         // if 'Play Game' selected
         if ((mainOption == 0)&&(buttonFlagA)) {
             buttonFlagA = 0;
@@ -1397,7 +1394,7 @@
             scores();
         }
         // if 'option' selected
-        if((mainOption == 2)&&(buttonFlagA)) {   
+        if((mainOption == 2)&&(buttonFlagA)) {
             buttonFlagA = 0;
             buttonFlagB = 0;
             optionsMenu();