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

Dependencies:   ConfigFile N5110 PowerControl beep mbed

Files at this revision

API Documentation at this revision

Comitter:
el13drt
Date:
Mon May 11 14:38:04 2015 +0000
Parent:
77:b37c6a0d8758
Child:
79:a92239ce3b20
Commit message:
gduhg

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
tower.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun May 10 20:04:26 2015 +0000
+++ b/main.cpp	Mon May 11 14:38:04 2015 +0000
@@ -14,15 +14,15 @@
 ///writes to CFG file.
 void write()
 {
-    ///Sets a Key and Value.
+    ///Sets a Key 1 and Value 1.
     if (!cfg.setValue("Open1", player1initials)) {//key/value
         error("Failure to set a value.\n");
     }
-    ///Sets a Key and Value.
+    ///Sets a Key 2 and Value 2.
     if (!cfg.setValue("Open2", player2initials)) {//key/value
         error("Failure to set a value.\n");
     }
-    ///Sets a Key and Value.
+    ///Sets a Key 3 and Value 3.
     if (!cfg.setValue("Open3", player3initials)) {//key/value
         error("Failure to set a value.\n");
     }
@@ -41,7 +41,7 @@
     char *key2 = "Open2";
     char *key3 = "Open3";
 
-    ///Checks Key and Value - if read is unsuccessful it an prints error message.
+    ///Checks Key for match, then reads Value - if read is unsuccessful it prints error message.
     if (!cfg.read("/local/towerMemory.cfg")) {
         error("Failure to read a configuration file.\n");
     }
@@ -50,7 +50,6 @@
         serial.printf("%s\n",player1initials);
         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))) {
@@ -133,34 +132,39 @@
     DirectionName direction;//current direction
 };
 
-//create struct variable
+///create struct variable
 Joystick joystick;
 
-///If buttonA is pressed - set flag A.
+///Called when timerA expires. If buttonA is pressed - set flag A.
 void timerExpiredA()
 {
+    ///Includes backlight reset for user input.
     if((deBounce1.read_ms()>=95)&&(buttonA == 1)) {
         buttonFlagA = 1;
         lcd.setBrightness(1.0);
         serial.printf("flagA set\n");
-        deBounce1.reset();//reset timer
+        deBounce1.reset();
+        ///Reset value for debounce timer
     }
 }
 
-///If buttonB is pressed - Set flag B.
+///Called when timer expires. If buttonB is pressed - Set flag B.
 void timerExpiredB()
 {
+    ///Includes backlight reset for user input.
     if((deBounce2.read_ms()>=95)&&(buttonB == 1)) {
         buttonFlagB = 1;
         lcd.setBrightness(1.0);
         serial.printf("flagB set\n");
         deBounce2.reset();//reset timer
+        ///Resets value for debounce timer 
     }
 }
 
 ///Randomises initial co-ordinates for falling hazards.
 void randomise()
 {
+    ///Creates initial seed for randomisation.
     srand (time(NULL));//initial seed for randomisation
 
     //initial random x co-ordinates
@@ -196,7 +200,7 @@
     lcd.refresh();
 }
 
-///Implements the Introduction screen.
+///Draws the Introduction screen.
 void drawWelcome()
 {
     //bottom border
@@ -282,6 +286,7 @@
 ///Implements Pixel Ninja's boundary conditions.
 void ninjaBoundaries()
 {
+    ///If ninja exceeds max boundary, Ninja equals max boundary.
     //right eye
     if(a11 > 80 )
         a11 = 80;
@@ -379,12 +384,14 @@
     //sword
     if(a14 < 10)
         a14 = 10;
+    ///If ninja falls below min boundary, Ninja equals min boundary.    
+    
 }
 
-///Resets Global variables for Character/Hazard co-ordinates.
+///Resets configuration values for the game.
 void resetGame()
-{
-    //global variables for movement (pixelNinja)
+{   
+    ///Resets Global variables for pixelNinja movement.
     a1 = 22;
     a2 = 24;
     a3 = 23;
@@ -401,21 +408,26 @@
     a14 = 19;
     a15 = 20;
     a16 = 21;
-
+    
+    ///Resets current score.
     score = 0;
 
     //in this case the X values are given a
     //new random variable each time the player
     //dies, exits or starts a new game
+    
+    ///Randomises X co-ordinates.
     randomise();
-
+ 
+    ///Resets Y co-ordinates.
     randY1 = 0;
     randY2 = 0;
     randY3 = 0;
     randY4 = 0;
     randY5 = 0;
     randY6 = 0;
-
+    
+    ///Clears screen.
     lcd.clear();
 }
 
@@ -429,15 +441,14 @@
     lcd.drawCircle(randX4,randY4,2,1);
     lcd.drawCircle(randX5,randY5,2,1);
     lcd.drawCircle(randX6,randY6,2,1);
-
+    
     lcd.refresh();
 }
 
 ///Increments the hazards to make them "Fall".
 void hazardFall()
 {
-    //increments randY1 variables
-    //appearing to make them fall
+    ///Increments values by chosen difficulty.
     randY1 = randY1 += fall;
     randY2 = randY2 += fall;
     randY3 = randY3 += fall;
@@ -445,9 +456,8 @@
     randY5 = randY5 += fall;
     randY6 = randY6 += fall;
 
-    //loops the objects once they 'hit the floor'
-    //this imitates a new set of objects falling
 
+    ///Loops objects once they've hit the floor.
     if (randY1>=48)
         randY1=0;
 
@@ -466,10 +476,11 @@
     //each time the objects loop, a new pseudo random value
     //is assigned to the global variables (randX) to
     //randomise their positions
-
+     
+    ///Assigns new random X co-ordinate once looped.
     if (randY6>=48) {
         randY6=0;
-
+        ///Incements current score with each wave of hazards.
         score = score++;//increment score by 1 after each wave of hazards
         randomise();//randomise x co-ordinates again
     }
@@ -478,6 +489,7 @@
 ///Clears old pixels and keeps set pixels.
 void startrek()
 {
+    ///Loops thorugh screen pixel by pixel.
     for (int i=3; i<81; i++)//loops through rows
         for (int j=0; j<47; j++)
             if (cells[i][j]) {//if there's a pixel then keep it
@@ -491,6 +503,7 @@
 ///Clears old cursor, sets new one.
 void refreshCursor1()
 {
+    ///Loops through select part of the screen.
     for (int i=70; i<80; i++)//loops through rows
         for (int j=17; j<25; j++)
             if (cells[i][j]) {//if there's a pixel then keep it
@@ -504,6 +517,7 @@
 ///Clears old cursor, sets new one.
 void refreshCursor2()
 {
+    ///Loops though select part of the screen.
     for (int i=70; i<80; i++)//loops through rows
         for (int j=25; j<32; j++)
             if (cells[i][j]) {//if there's a pixel then keep it
@@ -517,6 +531,7 @@
 ///Clears old cursor, sets new one.
 void refreshCursor3()
 {
+    ///Loops through select part of the screen.
     for (int i=70; i<80; i++)//loops through rows
         for (int j=32; j<40; j++)
             if (cells[i][j]) {//if there's a pixel then keep it
@@ -530,6 +545,7 @@
 ///Creates buzzer audible/LED to light when either button is pressed.
 void actionButtons()
 {
+    ///If FX is on emit beep and LED flash, else just flash.
     if((FX == 0)&&(buttonA||buttonB)) {
         ledA = 1;
         buzzer.beep(1500,0.3);
@@ -544,9 +560,9 @@
 ///Implements Joystick navigation for main menu.
 void mainMenu(int& mainOption)
 {
+    ///Includes actionButtons();
     actionButtons();//set audible/light for button
 
-    //joystick selection
     if (printFlag) {//if flag set, clear flag and print joystick values to serial port
         printFlag = 0;
 
@@ -655,11 +671,11 @@
 ///Implements Joystick navigation for Exit Menu.
 void exitMenu(int& exitOption)
 {
+    ///Includes actionButtons();
     actionButtons();
     if (printFlag) {//if flag set, clear flag and print joystick values to serial port
         printFlag = 0;
 
-        //check joystick direction
         if (joystick.direction == LEFT) {
             serial.printf(" LEFT\n");
             exitOption--;
@@ -686,6 +702,7 @@
 ///If the joystick is moved left, Pixel Ninja moves left.
 void ninjaLeft()
 {
+    ///Decrements values for left movement.
     a1 = a1-=2;
     a2 = a2-=2;
     a3 = a3-=2;
@@ -702,11 +719,13 @@
     a14 = a3+4;
     a15 = a3+3;
     a16 = a3+2;
+    ///Also turns head to face left.
 }
 
 ///If the Joystick is moved right, Pixel Ninja moves right.
 void ninjaRight()
 {
+    ///Increments for right movement.
     a1 = a1+=2;
     a2 = a2+=2;
     a3 = a3+=2;
@@ -723,6 +742,7 @@
     a14 = a3-4;
     a15 = a3-3;
     a16 = a3-2;
+    ///Turns head to face right.
 }
 
 ///Draws the Exit Menu.
@@ -804,6 +824,7 @@
 ///Implements Joystick navigation for Difficulty Options.
 void difficultyMenu(int& subOption)
 {
+    ///Includes actionButtons();
     actionButtons();
 
     //joystick selection
@@ -884,6 +905,7 @@
 ///Implements Joystick navigation for FX Menu.
 void soundFXMenu(int& fxOption)
 {
+    ///Inlcudes actionButtons();
     actionButtons();
 
     //joystick selection
@@ -949,15 +971,17 @@
 }
 
 
-//game flag for delay
+///Game flag for main game.
 void gameLoop()
 {
+    ///Sets gameFlag when game Timer expires.
     gameFlag = 1;
 }
 
 ///Shifts and stores the new scores accordingly.
 void newScore()
 {
+    ///Entry conditions mean function is only used if user equals or beats High Score 3.
     if(score >= highScore3) {//entry condition
         buttonFlagA = 0;//reset flags
         buttonFlagB = 0;
@@ -1078,6 +1102,7 @@
 ///Begins Game.
 void game(int& exitFlag, int& exitOption)
 {
+    ///Includes action buttons.
     actionButtons();
     lcd.clear();//clears screen
     drawBackground();//draw background
@@ -1200,6 +1225,7 @@
 ///Draws Scores Menu.
 void scores()
 {
+    ///Include actionButtons();
     actionButtons();
     lcd.clear();//clear screen
     drawBackground();//set background
@@ -1227,6 +1253,7 @@
 ///Implements selection for Options Menu.
 void optionsMenu()
 {
+    ///Includes actionButtons();
     int option = 0;
     int subOption = fall;//keeps cursor on selected option, even after exiting screen
     int fxOption = 0;
@@ -1337,12 +1364,14 @@
 ///Turns off screen backlighting.
 void screenOff()
 {
+    ///Sets lcd birghtness to 0.0.
     lcd.setBrightness(0.0);
 }
 
 ///When idle, calls the funtion screenOff();
 void idleMode()
 {
+    ///If user input detected, reset timer.
     if((buttonFlagA)||(buttonFlagB)||(joystick.direction)) { //call standby mode if idle
         int time = 60;
         standby.attach(&screenOff,time);
@@ -1371,7 +1400,7 @@
     read();//set high scores using flash memory
     deBounce1.start();//timer buttonA debounce
     deBounce2.start();//timer buttonB debounce
-    timerGame.attach(&gameLoop,0.75);//timer game delay
+    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.
--- a/tower.h	Sun May 10 20:04:26 2015 +0000
+++ b/tower.h	Mon May 11 14:38:04 2015 +0000
@@ -18,6 +18,11 @@
 */
 N5110 lcd(p7,p8,p9,p10,p11,p13,p22);//VCC, SCE, RST, D/C, MOSI, SCLK, LED - assign pins for LCD.
 
+
+/**
+@namespace local
+@brief Local file system, used to create read/write file path within the Mbeds flash memory.
+*/
 LocalFileSystem local("local");
 
 /**
@@ -33,20 +38,20 @@
 Serial serial(USBTX, USBRX);
 
 /**  
-@namespace deBounce
-@brief Creates a minimum time span of before buttonA is acknowledged; at intervals of 0.1 sec.
+@namespace deBounce A
+@brief Creates a minimum time span before buttonA is acknowledged; at intervals of 0.1 sec.
 */
 Timer deBounce1;//for buttonA
 
 /**  
-@namespace deBounce
-@brief Creates a minimum time span of before buttonB is acknowledged; at intervals of 0.1 sec.
+@namespace deBounce B
+@brief Creates a minimum time span before buttonB is acknowledged; at intervals of 0.1 sec.
 */
 Timer deBounce2;//for buttonB
 
 /**  
 @namespace timerGame
-@brief Creates a minimum time span before the game loops; at intervals of 0.1 sec.
+@brief Calls the function, gameLoop(); at intervals of 0.1 sec.
 */
 Ticker timerGame;//for game
 
@@ -93,32 +98,32 @@
 DigitalIn buttonB(p19);
 
 /**
-@namespace ledA.
+@namespace ledA
 @brief AnalgOut,allocates a pin to the Action LED.
 */
 AnalogOut ledA(p18);//action LED
 
 /**
-@namespace ledP.
-@brief Object of DigitalOut, allocates a pin to the Power LED.
+@namespace ledP
+@brief DigitalOut, allocates a pin to the Power LED.
 */
 DigitalOut ledP(p24);//Power LED
 
 /**
-@namespace joyButton.
-@brief Object of InterruptIn, allocates a pin to the Joy Stick button.
+@namespace joyButton
+@brief InterruptIn, allocates a pin to the Joy Stick button.
 */
 InterruptIn joyButton(p17);//Interrupt for ISR
 
 /**
-@namespace xpot.
-@brief Objct of AnalogIn, allocates a pin to xPot.
+@namespace xpot
+@brief AnalogIn, allocates a pin to xPot.
 */
 AnalogIn xPot(p15);//left/right
 
 /**
-@namespace ypot.
-@brief Objct of AnalogIn, allocates a pin to yPot.
+@namespace ypot
+@brief AnalogIn, allocates a pin to yPot.
 */
 AnalogIn yPot(p16);//up/down
 
@@ -136,7 +141,7 @@
 int buttonFlagB = 0;/*!< Button flag set for ISR when state of buttonB changes. */
 
 //flag for game loop
-int gameFlag = 0;/*!< Button flag set for ISR when game is entered. */
+int gameFlag = 0;/*!< Button flag set for ISR when game has started. */
 
 //flag for joystick reading
 int printFlag = 0;/*!< Print flag set for ISR when Joystick is moved. */
@@ -153,9 +158,9 @@
 int highScore3;/*!< Integer to store Score 3. */
 
 //global char buffers to store initials/score
-char player1initials[14];// = {"1.AAA.....0"};/*!< Buffer for printing Initials and Top Score 1.*/
-char player2initials[14];// = {"2.BBB.....0"};/*!< Buffer for printing Initials and Top Score 2.*/
-char player3initials[14];// = {"3.CCC.....0"};/*!< Buffer for printing Initials and Top Score 3.*/
+char player1initials[14];/*!< Buffer for printing Initials and Top Score 1.*/
+char player2initials[14];/*!< Buffer for printing Initials and Top Score 2.*/
+char player3initials[14];/*!< Buffer for printing Initials and Top Score 3.*/
 
 //difficulty variable - hazards fall at 2 pixels per refresh
 int fall = 2;/*!< Increments hazards each Iteration by the Integer stored. */
@@ -201,11 +206,11 @@
 
 /**
 */
-void calibrateJoystick();
+void write();
 
 /**
 */
-void updateJoystick();
+void read();
 
 /**
 */
@@ -217,18 +222,38 @@
 
 /**
 */
-void actionButton();
-
-/**
-*/
 void randomise();
 
 /**
 */
+void drawBackground();
+
+/**
+*/
+void drawWelcome();
+
+/**
+*/
+void drawNinja();
+
+/**
+*/
+void ninjaBoundaries();
+
+/**
+*/
 void resetGame();
 
 /**
 */
+void drawHazards();
+
+/**
+*/
+void hazardFall();
+
+/**
+*/
 void startrek();
 
 /**
@@ -242,10 +267,27 @@
 /**
 */
 void refreshCursor3();
+/**
+*/
+void actionbuttons();
+
+/**
+Uses a counter and Joystick directions for navigation
+Presents user with options
+@param mainOption - integer to increment / decrement
+*/
+void mainMenu();
 
 /**
 */
-void ninjaBoundaries();
+void drawMainMenu();
+
+/**
+Uses a counter and Joystick directions for navigation
+Presents user with options
+@param exitOption - integer to increment / decrement
+*/
+void exitMenu();
 
 /**
 */
@@ -257,72 +299,78 @@
 
 /**
 */
-void hazardFall();
+void drawExitMenu();
 
 /**
-*/
-void newScore();
-/**
-*/
-void mainMenu();
-
-/**
-*/
-void exitMenu();
-
-/**
+Uses a counter and Joystick directions for navigation
+Presents user with options
+@param option - integer to incremenet / decrement
 */
 void optionsMenu();
 
 /**
 */
-void game();
+void drawOptionsMenu();
 
 /**
+Uses a counter and Joystick directions for navigation
+Presents user the options
+@param subOption - integer to increment / decrement
 */
 void difficultyMenu();
 
 /**
 */
+void drawDifficultyMenu();
+
+/**
+Uses a counter and Joystick directions for navigation
+Presents user the options
+@param fxOption - integer to increment / decrement
+*/
 void soundFXMenu();
 
 /**
 */
+void drawSoundFXMenu();
+
+/**
+*/
+void gameLoop();
+
+/**
+*/
+void newScore();
+
+/**
+Uses a counter and Joystick directions to navigate the exitMenu
+Presents user with options
+@param exitFlag - integer set to break main loop and return to the main menu 
+@param exitOption - integer to increment / decrement
+*/
+void game();
+
+
+/**
+*/
 void scores();
 
 /**
 */
-void drawNinja();
+void optionsMenu();
 
 /**
 */
-void drawHazards();
-
-/**
-*/
-void drawWelcome();
-
-/**
-*/
-void drawBackground();
+void calibrateJoystick();
 
 /**
 */
-void drawSoundFXMenu();
-
-/**
-*/
-void drawDifficultyMenu();
+void updateJoystick();
 
 /**
 */
-void drawMainMenu();
+void screenOff();
 
 /**
 */
-void drawOptionsMenu();
-
-/**
-*/
-void drawExitMenu();
-
+void idleMode();