Completed Snake Program

Dependencies:   N5110 PinDetect PowerControl mbed

Fork of DocTest by Craig Evans

Files at this revision

API Documentation at this revision

Comitter:
MBirney
Date:
Fri May 08 23:47:17 2015 +0000
Parent:
9:77b83754460c
Commit message:
Michael Birney Final Snake Program

Changed in this revision

PowerControl.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PowerControl.lib	Fri May 08 23:47:17 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/JST2011/code/PowerControl/#d0fa2aeb02a4
--- a/main.cpp	Fri May 01 08:53:21 2015 +0000
+++ b/main.cpp	Fri May 08 23:47:17 2015 +0000
@@ -15,6 +15,7 @@
     EASY,
     MEDIUM,
     HARD,
+    
 };
 
 // create enumerated type (0,1 for Game Mode)
@@ -383,7 +384,7 @@
 }
 
 
-void updateSnakeArray()
+void updateSnakeVector()
 {
 // code to prevent snake moving in opposite direction to its current path of travel
     if (joystick.direction==LEFT && previousDirection==RIGHT) { // if snake is travelling right but joystick is left
@@ -468,27 +469,28 @@
 void gameOverTune() //
 {
 
- //   float frequency[]={440,659};
+//   float frequency[]={440,659};
     //float beat[]={1,1,};
-    
-      Buzzer.period(1.0/(220)); // set PWM period 1/f  A
-Buzzer=0.5; // set duty cycle
-wait(0.5); // wait time 
-Buzzer.period(1.0/(164)); // E
-wait(0.5);//
-Buzzer=0;// set duty cycle 0
 
-  //   set PWM period
+    Buzzer.period(1.0/(220)); // set PWM period 1/f  A
+    Buzzer=0.5; // set duty cycle
+    wait(0.5); // wait time
+    Buzzer.period(1.0/(164)); // E
+    wait(0.5);//
+    Buzzer=0;// set duty cycle 0
+
+    //   set PWM period
 
 }
 
-void eatFoodTune(){
-      Buzzer.period(1.0/(440)); // set PWM period 1/f  A
-Buzzer=0.5; // set duty cycle
-wait(0.1); // wait time 
-Buzzer=0;// set duty cycle 0
-    }
-    
+void eatFoodTune()
+{
+    Buzzer.period(1.0/(440)); // set PWM period 1/f  A
+    Buzzer=0.5; // set duty cycle
+    wait(0.1); // wait time
+    Buzzer=0;// set duty cycle 0
+}
+
 void gameOver()
 {
     leds=1;
@@ -512,8 +514,8 @@
     lcd.printString("Your Score" ,12,0);// print score
     lcd.printString("=" ,34,1);
 
-    int updatedScore=score;
-    int length = sprintf(buffer,"%2d",updatedScore);
+    
+    int length = sprintf(buffer,"%2d",score);
 
     if (length <= 14)  // if string will fit on display
         lcd.printString(buffer,40,1);
@@ -578,15 +580,13 @@
                 // snakeY.push_back(foodY[0]+2);
                 break;
         }
-eatFoodTune();
+        eatFoodTune();
         lcd.drawRect(snakeX[0],snakeY[0],1,1,1); // draw the new tail
 
-        int updatedScore; // to store updated score
-
-
-        updatedScore= score+5; // 5 points for every food eaten
-        score=updatedScore;
-        int length = sprintf(buffer,"%2d",updatedScore);// print updated score to screen
+     
+       score+=5; // 5 points for every food eaten
+        
+        int length = sprintf(buffer,"%2d",score);// print updated score to screen
 
         if (length <= 14)  // if string will fit on display
             lcd.printString(buffer,0,0);
@@ -690,19 +690,21 @@
 
 int main()
 {
- startButton.setSampleFrequency();
+    startButton.setSampleFrequency(); //for pindetect
+    button.setSampleFrequency();// for pindetect
     lcd.init();
-    resetButton.mode(PullDown);
+    resetButton.mode(PullDown); 
     startButton.mode(PullDown);
     button.mode(PullDown);
-  //  startButton.rise(&startButtonPressed);
     startButton.attach_asserted( &startButtonPressed );
     resetButton.rise(&resetButtonPressed);
-    button.rise(&pauseButtonPressed);
+    button.attach_asserted(&pauseButtonPressed);
     displaySplash();
     leds=1;
     wait(4);
-
+int result = semihost_powerdown(); //USB POWERDOWN
+ PHY_PowerDown(); //ETHERNET POWERDOWN
+ 
     while(1) {
         leds=1;// red light
         startUpMenu();
@@ -736,11 +738,12 @@
             joystick.direction=RIGHT; // make sure when game reset that joystick is reset to right
             startingSnake(); // print starting snake
             randomiseFood(); // show random food
-            int score=5;// initial score is 5
+         score=5;// initial score is 5
+        
             int length = sprintf(buffer,"%2d",score);
             if (length <= 14)  // if string will fit on display
                 lcd.printString(buffer,0,0);
-            // lcd.drawRect(0,0,83,7,0);
+          
             lcd.refresh();
             startGame.attach(&updateGameISR,gameSpeed); // start game isr speed determined by difficulty chosen
 
@@ -753,7 +756,7 @@
                     updateGameFlag=0; // reset flag
                     updateJoystick(); // check joystick direction
 
-                    updateSnakeArray(); // update the array
+                    updateSnakeVector(); // update the Vector
                     for (int i=0; i<snakeX.size(); i++) { // for all elements of snake draw 2x2 image
                         lcd.drawRect(snakeX[i],snakeY[i],1,1,1);
 
@@ -772,11 +775,9 @@
                     }
 
                 }
-
+                sleep();
             }
         }
 
-
-
     }
 }
\ No newline at end of file
--- a/main.h	Fri May 01 08:53:21 2015 +0000
+++ b/main.h	Fri May 08 23:47:17 2015 +0000
@@ -11,8 +11,16 @@
 #include "mbed.h"
 #include "N5110.h"
 #include "PinDetect.h"
+#include "PowerControl/PowerControl.h"
+#include "PowerControl/EthernetPowerControl.h"
 #define DIRECTION_TOLERANCE 0.1
 
+#define USR_POWERDOWN (0x104)
+int semihost_powerdown(){
+    
+uint32_t arg;
+return __semihost(USR_POWERDOWN,& arg);
+}
 
 AnalogIn pot(p20);
 PwmOut Buzzer(p21);
@@ -27,43 +35,226 @@
 
 /** 
 @namespace myleds
-@brief connections lcd mosi connections for nokia N5110
+@brief busout to onboard mbed LEDS for debug
 */ 
 
 BusOut myleds(LED1,LED2,LED3,LED4);
+
+/** 
+@namespace leds
+@brief bus output for game indicator leds 
+*/ 
 BusOut leds(p22,p23,p24);
+
+/** 
+@namespace startButton
+@PinDetect for start button ISR
+*/ 
 PinDetect startButton(p5);
+/** 
+@namespace resetButton
+@Interrupt in for reset button ISR
+*/ 
 InterruptIn resetButton(p6);
-int score=5;
-Ticker pollJoystick;
+
+int score=5; /*!< integer to store user score */
+
+/** 
+@namespace startGame
+@brief Ticker to set updateGameFlag 
+*/ 
 Ticker startGame;
-char buffer[14];
+
+
+char buffer[14]; /*!< stores data to print to screen */
 int length = sprintf(buffer,"%2d",score); // print formatted data to buffer
 // it is important the format specifier ensures the length will fit in the buffer
 // connections for joystick
-InterruptIn button(p17);
+
+/** 
+@namespace button 
+@brief Joysitck button GPIO 
+*/ 
+PinDetect button(p17);
+/** 
+@namespace xPot 
+@brief joystick x pot GPIO
+*/ 
 AnalogIn xPot(p15);
+/** 
+@namespace yPot 
+@brief joystick y pot GPIO 
+*/ 
 AnalogIn yPot(p16);
 //for debug
+
+/** 
+@namespace serial 
+@brief Print to terminal for debug 
+*/ 
 Serial serial(USBTX,USBRX);
+
 //forGame
-int snake[84][47];
-bool gamePlaying =0;
-bool gamePaused=0;
-vector<int> snakeX (5); // vector to store all x coordinates of snake
-vector<int> snakeY (5,27);// vector to store all y coordinates of snake
-int foodX[1];// store x value of food
-int foodY[1];// store y value of food
-int gameState=0;// 
+
+bool gamePaused=0; /*!< bool to store paused state */
+vector<int> snakeX (5); /*!<vector to store all x coordinates of snake */ 
+vector<int> snakeY (5,27);/*!< vector to store all y coordinates of snake */ 
+int foodX[1];/*!< int to store x value of food */
+int foodY[1];/*!< int to store y value of food */
+int gameState=0;/*!<indicator for various game states */ 
 
 
-float gameSpeed;//change depending on difficulty selected
-int updateGameFlag=0;
+float gameSpeed;/*!< changes depending on difficulty selected */
+int updateGameFlag=0; /*!< ISR to update game */
+
+
+// GAME MENU
 
 /** 
 Displays a Snake Logo Splash Screen 
 */
 void displaySplash();
 
+/** 
+Reads default positions of the joystick to calibrate later readings 
+*/
+void calibrateJoystick();
+
+/** 
+Reads X and Y pots and determines what direction joystick is in 
+*/
+void updateJoystick();
+
+/** 
+Displays easy selected on display and updates gameSpeed variable
+*/
+void easySelected();
+/** 
+Displays Medium selected on display and updates gameSpeed variable
+*/
+void mediumSelected();
+/** 
+Displays Hard selected on display and updates gameSpeed variable
+*/
+void hardSelected() ;
+
+/** 
+Displays classicMode selected on display and updates gameMode variable
+*/
+void classicModeSelected();
+
+/** 
+Displays BoundaryMode selected on display and updates gameMode variable
+*/
+void boundaryModeSelected();
+
+/** 
+Uses joystick direction to determine what difficulty too update to
+*/
+void checkSelectedGameMode();
+
+/** 
+Uses joystick direction to determine what game mode too update to
+*/
+void checkSelectedDifficulty();
+
+
+//GAMEPLAY
+
+
+/** 
+resizes the vector back to 5 incase game is reset
+prints to screen using coordinates stored in the snakeX and snakeY vectors
+drawRect used for 2x2 snake
+*/
+void startingSnake();
+
+
+/** 
+Funtion to generate random food 
+*/
+void randomiseFood();
+
+
+/** 
+prints a solid boundary around screen for use in BOUNDARY mode
+*/
+void hardBoundary();
+
+/** 
+Prints a dotted boundary around the screen for use in CLASSIC mode
+*/
+void classicBoundary();
 
 
+/** 
+Uses joystick direction and previous joysitck direction to determine new direction of snake
+Clears Tail of Snake
+Elements in SnakeX and Y are shifted along one to the right 
+New snake head cordinates are added to last element of vector depending on direction
+*/
+void updateSnakeVector();
+
+/** 
+triggers Buzzer to play when gameover
+*/
+void gameOverTune();
+
+/** 
+called when game is over.
+displays final score for user 
+*/
+void gameOver();
+
+
+/** 
+called in boundary mode
+everytime snake updates checks for a collision with boundary
+*/
+void checkForCollision();
+
+
+/** 
+Everytime snake is updated 
+compares coordinates of snake to the food to see if they match
+*/
+void checkForFood();
+
+
+/** 
+function called when pindetect startButton is triggered
+*/
+void startButtonPressed();
+
+
+/** 
+Reads from brightness potentiometer to update lcd brightness
+*/
+void updateBrightness();
+
+
+/** 
+function called by updateGame timer
+*/
+void updateGameISR();
+
+
+/** 
+For debug. Prints content of snakeX and snakeY vectors
+*/
+void printVectorContent();
+
+/** 
+function called when interupt occurs on joystick button
+*/
+void pauseButtonPressed();
+
+
+/** 
+Compares all elements in snake vectors against head of snake
+@param i - integer signifying element in snake vector
+*/
+void checkForCollisionWithSelf(int i);
+
+
+