A retro gaming programme, designed for use on a portable embedded system. Incorporates power saving techniques.
Dependencies: ConfigFile N5110 PowerControl beep mbed
Diff: main.cpp
- Revision:
- 78:e2fc547c6b99
- Parent:
- 77:b37c6a0d8758
- Child:
- 79:a92239ce3b20
--- 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.