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

Dependencies:   ConfigFile N5110 PowerControl beep mbed

Revision:
62:827cda7a2663
Parent:
61:64077ba14f3c
Child:
64:715fb5a1e58b
--- a/main.cpp	Sun May 03 22:57:57 2015 +0000
+++ b/main.cpp	Tue May 05 22:24:35 2015 +0000
@@ -11,34 +11,38 @@
 #include <ctime>
 #include <cstdlib>
 
-//read/write memory function
+
+///writes to CFG file. 
 void write()
 {
-    //write
+    ///Sets a Key and Value.
     if (!cfg.setValue("Open", player1initials)) {//key/value
         error("Failure to set a value.\n");
     }
-    //Write to a file.
-    if (!cfg.write("/local/towerMemory.cfg")) {
+    ///If a file does not already exist, create one.
+    ///If one already exits, append the file.
+    if (!cfg.write("/local/towerMemory.cfg")) {//Write key and value to CFG file
         error("Failure to write a configuration file.\n");
     }
 }
 
-//read file
+///Reads CFG file.
 void read(){
-    char *key = "Open";//check key matches
+    
+    //Checks if key matches.
+    char *key = "Open";
 
-    //check file-if read unsuccessful print  error message
+    ///Checks Key and Value - if read is unsuccessful it an prints error message.
     if (!cfg.read("/local/towerMemory.cfg")) {
         error("Failure to read a configuration file.\n");
     }
-    //check size, if read sucessful
+    ///check file size - if size acceptable, prints buffer to location.
     if (cfg.getValue(key, &player1initials[0], sizeof(player1initials))) {
         serial.printf("%s\n",player1initials);
     }
 }
 
-//struct for choosing initials
+///Struct declaration for storing alphabet arrays.
 struct State {
     char output1;
     char output2;
@@ -46,12 +50,13 @@
     int nextState[2];
 };
 
-//assigns new identifier to struct
+///Assigns new identifier to Struct.
 typedef const struct State STYP;
 
-//output array for struct Alphabet
+///Finite State Machine for Struct data members.
 STYP fsm[27] = {
-    //output//nextstate//previousstate//
+    
+    ///Outputs 1,2 and 3 for Initial Arrays.
     {'A','A','A',{1,26}},
     {'B','B','B',{2,0}},
     {'C','C','C',{3,1}},
@@ -82,11 +87,7 @@
     {'.','.','.',{0,25}},
 };
 
-//timer to read the joystick
-Ticker pollJoystick;
-
-//create enumerated type (0,1,2,3 etc. for direction)
-//could be extended for diagonals etc.
+///Creates enumerated type (0,1,2,3 etc. for direction).
 enum DirectionName {
     UP,
     DOWN,
@@ -96,7 +97,7 @@
     UNKNOWN
 };
 
-//struct for Joystick
+///Struct for Joystick.
 typedef struct JoyStick Joystick;
 struct JoyStick {
     float x;//current x value
@@ -110,7 +111,7 @@
 //create struct variable
 Joystick joystick;
 
-//if buttonA set flag A
+///If buttonA is pressed - set flag A.
 void timerExpiredA()
 {
     if(buttonA == 1) {
@@ -119,7 +120,7 @@
     }
 }
 
-//if buttonB set flag B
+///If buttonB is pressed - Set flag B.
 void timerExpiredB()
 {
     if(buttonB == 1) {
@@ -128,7 +129,7 @@
     }
 }
 
-//set seed/randomise initial co-Ordinates
+///Randomises initial co-ordinates for falling hazards.
 void randomise()
 {
     srand (time(NULL));//initial seed for randomisation
@@ -144,7 +145,7 @@
     randX6 = rand() % 74 + 5;
 }
 
-//static background
+///Draws the games static background.
 void drawBackground()
 {
     //x, y, w, h, fill - draw ground
@@ -166,7 +167,7 @@
     lcd.refresh();
 }
 
-//intro screen
+///Implements the Introduction screen.
 void drawWelcome()
 {
     //bottom border
@@ -212,7 +213,7 @@
     wait(0.6);
 }
 
-//pixel ninja character
+///Draws Pixel Ninja.
 void drawNinja()
 {
     //x, y, w, h, fill - left leg
@@ -249,7 +250,7 @@
     lcd.refresh();
 }
 
-//stops ninja going through walls
+///Implements Pixel Ninja's boundary conditions.
 void ninjaBoundaries()
 {
     //right eye
@@ -351,10 +352,9 @@
     a14 = 10;     
 }
 
-//resets variables initial values when game ends or is exited
+///Resets Global variables for Character/Hazard co-ordinates.
 void resetGame()
 {
-
     //global variables for movement (pixelNinja)
     a1 = 22;
     a2 = 24;
@@ -403,7 +403,7 @@
     lcd.clear();
 }
 
-//draws falling hazards
+///Draws falling hazards.
 void drawHazards()
 {
     //X, Y, radius, fill
@@ -417,7 +417,7 @@
     lcd.refresh();
 }
 
-//makes hazards fall - randomises X axis co-ordinates
+///Increments the hazards to make them "Fall".
 void hazardFall()
 {
     //increments randY1 variables
@@ -465,7 +465,7 @@
     }
 }
 
-//clears old pixels and keeps set pixels
+///Clears old pixels and keeps set pixels.
 void startrek()
 {
     for (int i=3; i<81; i++)//loops through rows
@@ -478,7 +478,7 @@
     lcd.refresh();
 }
 
-//clears old pixels and keeps set pixels
+///Clears old cursor, sets new one.
 void refreshCursor1()
 {
     for (int i=70; i<80; i++)//loops through rows
@@ -491,7 +491,7 @@
     lcd.refresh();
 }
 
-//clears old pixels and keeps set pixels
+///Clears old cursor, sets new one.
 void refreshCursor2()
 {
     for (int i=70; i<80; i++)//loops through rows
@@ -504,7 +504,7 @@
     lcd.refresh();
 }
 
-//clears old pixels and keeps set pixels
+///Clears old cursor, sets new one.
 void refreshCursor3()
 {
     for (int i=70; i<80; i++)//loops through rows
@@ -517,7 +517,7 @@
     lcd.refresh();
 }
 
-//beep/light when buttons are closed
+///Creates buzzer audible/LED to light when either button is pressed.
 void actionButtons()
 {
     if((FX == 0)&&(buttonA||buttonB)) {
@@ -531,7 +531,7 @@
     }
 }
 
-//presents main menu options
+///Implements Joystick navigation for main menu.
 void mainMenu(int& mainOption)
 {
     actionButtons();//set audible/light for button
@@ -573,7 +573,7 @@
     }
 }
 
-//draws main menu
+///Draws the Main Menu.
 void drawMainMenu()
 {
     //bottom border
@@ -642,7 +642,7 @@
     lcd.refresh();
 }
 
-//presents exit menu options
+///Implements Joystick navigation for Exit Menu.
 void exitMenu(int& exitOption)
 {
     actionButtons();
@@ -673,7 +673,7 @@
     }
 }
 
-//ninja go left
+///If the joystick is moved left, Pixel Ninja moves left.
 void ninjaLeft()
 {
     a1 = a1-=2;
@@ -694,7 +694,7 @@
     a16 = a3+2;
 }
 
-//ninja go right
+///If the Joystick is moved right, Pixel Ninja moves right.
 void ninjaRight()
 {
     a1 = a1+=2;
@@ -715,7 +715,7 @@
     a16 = a3-2;
 }
 
-//draws exit menu
+///Draws the Exit Menu.
 void drawExitMenu()
 {
     //set exit menu
@@ -737,7 +737,7 @@
     lcd.refresh();
 }
 
-//presents the options
+///Implements Joystick navigation for Options Menu.
 void optionsMenu(int& option)
 {
     //joystick selection
@@ -777,7 +777,7 @@
     lcd.refresh();
 }
 
-//draws options menu
+///Draws the Options Menu.
 void drawOptionsMenu()
 {
     lcd.clear();//clear screen
@@ -792,7 +792,7 @@
     lcd.refresh();
 }
 
-//present difficulty options
+///Implements Joystick navigation for Difficulty Options.
 void difficultyMenu(int& subOption)
 {
     actionButtons();
@@ -856,7 +856,7 @@
     lcd.refresh();
 }
 
-//draw difficulty settings
+///Draws Difficulty Menu.
 void drawDifficultyMenu()
 {
     lcd.clear();
@@ -870,7 +870,7 @@
     lcd.printString("Forget It",5,4);//title
 }
 
-//present sound FX options
+///Implements Joystick navigation for FX Menu.
 void soundFXMenu(int& fxOption)
 {
     actionButtons();
@@ -924,7 +924,7 @@
     lcd.refresh();
 }
 
-//draw Sound FX settings
+///Draws the FX Menu.
 void drawSoundFXMenu()
 {
     lcd.clear();
@@ -937,8 +937,7 @@
     lcd.refresh();
 }
 
-//if any of the high scores are beaten, they are replaced.
-//player enters initials using structs outputs
+///Shifts and stores the new scores accordingly.
 void newScore()
 {
     if(score >= highScore3) {//entry condition
@@ -1058,7 +1057,7 @@
     }
 }
 
-//actual game
+///Implements Game.
 void game(int& exitFlag, int& exitOption)
 {
     actionButtons();
@@ -1135,7 +1134,7 @@
             lcd.refresh();//refresh screen
             startrek();//clears unset pixels, keeps set pixels
 
-///Exit Menu (Back button pressed)///
+//Exit Menu (Back button pressed)///
             if(buttonFlagB) {
                 buttonFlagB = 0;//reset flags
                 buttonFlagA = 0;
@@ -1176,7 +1175,7 @@
     }
 }
 
-//high scores screen
+///Draws Scores Menu.
 void scores()
 {
     actionButtons();
@@ -1205,7 +1204,7 @@
     }
 }
 
-//options menu
+///Implements selection for Options Menu.
 void optionsMenu()
 {
     int option = 0;
@@ -1274,7 +1273,7 @@
     }
 }
 
-//read default positions of the joystick to calibrate later readings
+///Reads default position of Joystick for calibration.
 void calibrateJoystick()
 {
     joyButton.mode(PullDown);
@@ -1283,7 +1282,7 @@
     joystick.y0 = yPot;
 }
 
-//reads and updates position of joystick according to voltage readings
+///Updates Joytick position according to voltage readings.
 void updateJoystick()
 {
     //read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)