ELEC2645 (2015/16) / Mbed 2 deprecated SnakeProjectRev1

Dependencies:   Joystick N5110 SDFileSystem beep fsmMenu mbed

Fork of SnakeProjectRev1 by Meurig Phillips

Revision:
10:7820b46476ea
Parent:
9:6d7074258c63
Child:
11:f8478bc749e0
--- a/main.cpp	Sun May 01 09:25:44 2016 +0000
+++ b/main.cpp	Sun May 01 11:11:19 2016 +0000
@@ -1,21 +1,11 @@
-/* Joystick
- 
-Example code of how to read a joystick
- 
-https://www.sparkfun.com/products/9032
- 
-Craig A. Evans
-7 March 2015
-*/
+
  
 #include "mbed.h"
 #include "N5110.h"
 #include "beep.h"
 #include "SDFileSystem.h"
+#include "Joystick.h"
  
-// change this to alter tolerance of joystick direction
-#define DIRECTION_TOLERANCE 0.2 // previously 0.05
-
 //         VCC,    SCE,   RST,   D/C,   MOSI,  SCLK,   LED
 N5110 lcd (PTD3, PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);
 // Can also power (VCC) directly from VOUT (3.3 V) -
@@ -24,15 +14,10 @@
 // Connections to SD card holder on K64F (SPI interface)
 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
  
-// connections for joystick
-DigitalIn button(PTB18);
-AnalogIn xPot(PTB2);
-AnalogIn yPot(PTB3);
 DigitalOut greenLed(PTC2);
 DigitalOut redLed(PTA2);
 
-
-Beep buzzer(PTA1);
+//Beep buzzer(PTA1);
 
 // timer to regularly read the joystick
 Ticker pollJoystick;
@@ -41,17 +26,6 @@
 
 Ticker gameTicker;
  
-// create enumerated type (0,1,2,3 etc. for direction)
-// could be extended for diagonals etc.
-enum DirectionName {
-    UP,
-    DOWN,
-    LEFT,
-    RIGHT,
-    CENTRE,
-    UNKNOWN
-};
-
 /// create enumerated type (0,1,2,3 etc. for current direction snake is travelling (not joystick reading))
 enum CurrentDirection {
     up,
@@ -61,22 +35,7 @@
     centre,
 };
 CurrentDirection currentDirection = centre; /// intialise direction at beginning
- 
-// struct for Joystick
-typedef struct JoyStick Joystick;
-struct JoyStick {
-    float x;    // current x value
-    float x0;   // 'centred' x value
-    float y;    // current y value
-    float y0;   // 'centred' y value
-    int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
-    DirectionName direction;  // current direction
-};
-// create struct variable
-Joystick joystick;
-
-int printFlag = 0;
- 
+  
 int pixels[84][48];
 
 int randomX =  rand() % 83 + 1;     // randomX in the range 1 to 81
@@ -106,9 +65,6 @@
 bool gamePlaying = false;
 
 // function prototypes
-void calibrateJoystick();
-void updateJoystick();
-void joystickDirection();
 void generateFood();
 void newFruitXY();
 void moveSnake();
@@ -123,7 +79,6 @@
 
 FILE *fp; // this is our file pointer
 
- 
 int main()
 {
 
@@ -160,42 +115,6 @@
 }
  
 
-void calibrateJoystick() // read default positions of the joystick to calibrate later readings
-{
-    button.mode(PullDown);
-    // must not move during calibration
-    joystick.x0 = xPot;  // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
-    joystick.y0 = yPot;
-}
-
-void updateJoystick()
-{
-    // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
-    joystick.x = xPot - joystick.x0;
-    joystick.y = yPot - joystick.y0;
-    // read button state
-    joystick.button = button;
- 
-    // calculate direction depending on x,y values
-    // tolerance allows a little lee-way in case joystick not exactly in the stated direction
-    if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
-        joystick.direction = CENTRE;
-    } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
-        joystick.direction = UP;
-    } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
-        joystick.direction = DOWN;
-    } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
-        joystick.direction = LEFT;
-    } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
-        joystick.direction = RIGHT;
-    } else {
-        joystick.direction = UNKNOWN;
-    }
- 
-    // set flag for printing
-    printFlag = 1;
-}
-
 void generateFood()
 {
     while (randomXoddEven ==0 || randomYoddEven ==0 || lcd.getPixel(randomX,randomY) >= 1) { // do while x or y is even or pixel is on
@@ -223,26 +142,6 @@
         randomYoddEven = randomY%2;
  
 }
-
-void joystickDirection() { // stcik direction for debug
- 
- serial.printf("x = %f y = %f button = %d ",joystick.x,joystick.y,joystick.button);
- 
-            // check joystick direction
-            if (joystick.direction == UP)
-                serial.printf(" UP\n");
-            if (joystick.direction == DOWN)
-                serial.printf(" DOWN\n");
-            if (joystick.direction == LEFT)
-                serial.printf(" LEFT\n");
-            if (joystick.direction == RIGHT)
-                serial.printf(" RIGHT\n");
-            if (joystick.direction == CENTRE)
-                serial.printf(" CENTRE\n");
-            if (joystick.direction == UNKNOWN)
-                serial.printf(" Unsupported direction\n");
-
- }
  
 void moveSnake() {
     
@@ -319,7 +218,7 @@
                 
                 if (i == randomX && j == randomY) { // if fruit is eaten
                     greenLed = 0;
-                    buzzer.beep(2000,0.2); 
+                    //buzzer.beep(2000,0.2); 
                     scoreCalculation();
                     snakeTailLength++;
                     newFruitXY();
@@ -330,8 +229,7 @@
     }
  }
  
-
-
+ 
 void hardWall() {
 
     lcd.drawRect(0,0,83,47,0);
@@ -370,17 +268,17 @@
     }
      gamePlaying = false;
         lcd.clear();
-        buzzer.beep(294,0.5);
+        //buzzer.beep(294,0.5);
         wait(0.5);
         lcd.printString("Game Over",15,0); // width(6) * n(9) = 54, 84-54 = 30, 30/2 = 15
-        buzzer.beep(277,0.5);
+        //buzzer.beep(277,0.5);
         wait(0.5);
         
         char buffer[14];
         int length = sprintf(buffer,"Score:%i",score); // display score of round
         if (length <= 14) { // if string will fit on display
             lcd.printString(buffer,0,1); }           // display on screen
-        buzzer.beep(262,2);
+        //buzzer.beep(262,2);
         
          ////////////////////// Simple reading example //////////////////////////
 
@@ -405,7 +303,9 @@
         lcd.printString("RB - Restart",0,4);
         wait(0.5);
         lcd.printString("LB - Menu",0,5);
-       
+       /* if (RB == 0) {
+            moveSnake();
+            } */
 }
 
 
@@ -526,16 +426,16 @@
     }  // x,y,radius,white fill
     
     lcd.printString("Snake",27,0);
-    buzzer.beep(2000,0.5);   
+    //buzzer.beep(2000,0.5);   
     wait(0.5);
     lcd.printString("by",36,2);
-    buzzer.beep(1000,0.5);
+    //buzzer.beep(1000,0.5);
     wait(0.5);
     lcd.printString("Meurig",24,3);
-    buzzer.beep(500,0.5);
+    //buzzer.beep(500,0.5);
     wait(0.5);
     lcd.printString("Phillips",18,4);
-    buzzer.beep(250,2);
+    //buzzer.beep(250,2);
     wait(2);
     
     lcd.clear();