ELEC2645 (2015/16) / Mbed 2 deprecated SnakeProjectRev1

Dependencies:   Joystick N5110 SDFileSystem beep fsmMenu mbed

Fork of SnakeProjectRev1 by Meurig Phillips

Files at this revision

API Documentation at this revision

Comitter:
meurigp
Date:
Wed May 04 15:16:12 2016 +0000
Parent:
15:a5590211888c
Child:
17:4e6f0f7f22fb
Commit message:
fsm working, need to initialise game accordingly

Changed in this revision

fsmMenu.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
--- a/fsmMenu.lib	Wed May 04 12:33:52 2016 +0000
+++ b/fsmMenu.lib	Wed May 04 15:16:12 2016 +0000
@@ -1,1 +1,1 @@
-fsmMenu#d74f3a7ca036
+fsmMenu#b5f4e9c759a9
--- a/main.cpp	Wed May 04 12:33:52 2016 +0000
+++ b/main.cpp	Wed May 04 15:16:12 2016 +0000
@@ -26,11 +26,13 @@
 Ticker pollJoystick;
 Ticker gameTicker;
 
-volatile int game_timer_flag = 0;
+
  
 FILE *fp; // this is our file pointer
 
 void timer_isr();
+void rb_isr();
+void lb_isr();
 
 int main()
 {    
@@ -44,14 +46,76 @@
     lcd.init();
     //snakeIntro();
     //hardWall();
-    wrapAround();
-    generateFood();
-    gamePlaying = true;
-    initSnakeTail();
+    //wrapAround();
+    //generateFood();
+    gamePlaying = false;
+    //initSnakeTail();
     button.rise(&buttonISR);
     gameTicker.attach(&timer_isr,0.1);
+    RB.mode(PullDown);
+    LB.mode(PullDown);
+    RB.rise(&rb_isr);
+    LB.rise(&lb_isr);
+    
+    
+    while(1) {
+     
+        lcd.printString("Classic",0,1);
+        lcd.printString("Infinite",0,3);
+        lcd.printString("Hard Map",0,5); 
+        
+        if (rb_flag == 1) {
+            rb_flag = 0;
+            gamePlaying = true;
+            break;   
+        }
+        
+    // check if flag i.e. interrupt has occured
+        if (printFlag ==1) {
+            printFlag = 0;  // if it has, clear the flag
  
-  /*  while(gamePlaying == true) {
+            // swap direction when button has been pressed
+            // (could just use ! but want this to be explicit to aid understanding)
+            if (joystick.direction == CENTRE) {
+                serial.printf(" CENTRE\n");
+                menuDirection = menuSTOP;
+                }
+            else if (joystick.direction == UP) {
+                serial.printf(" UP\n");
+                menuDirection = menuUP;
+                buzzer.beep(2000,0.2);    
+            }
+            else if (joystick.direction == DOWN) {
+                serial.printf(" DOWN\n");
+                menuDirection = menuDOWN;
+                buzzer.beep(2000,0.2);  
+            }
+        }
+    
+    
+        menuFSM();
+    
+    
+        wait(0.2); // small delay
+        if (state ==0) {
+        lcd.clear();   
+        lcd.printString("*",70,1);   
+        gameType = classicMode; 
+        }
+        else if (state ==1) {
+        lcd.clear();
+        lcd.printString("*",70,3);
+        gameType = infiniteMode;   
+        }
+        else if (state ==2) {
+        lcd.clear();
+        lcd.printString("*",70,5);
+        gameType = hardMode;  
+        }  
+           
+    }
+ 
+    while(gamePlaying == true) {
         
         lcd.setBrightness(1-pot); // turn pot right for brightness
         
@@ -71,7 +135,7 @@
             moveSnake();
             } 
             sleep(); // put the MCU to sleep until an interrupt wakes it up
-    } */
+    }
 }
  
 
@@ -266,31 +330,6 @@
 
 void specialMap() {
 
-    for (int i=0; i<21; i++) { // top/bottom left x line
-       lcd.setPixel(i,0);
-       lcd.setPixel(i,47);
-       lcd.refresh();
-       }
-    for (int i=63; i<84; i++) { // top/bottom right x line
-       lcd.setPixel(i,0);
-       lcd.setPixel(i,47);
-       lcd.refresh();
-       }
-    for (int i=37; i<47; i++) { // top/bottom middle x line
-       lcd.setPixel(i,0);
-       lcd.setPixel(i,47);
-       lcd.refresh();
-       }
-    for (int j=0; j<17; j++) { // left/right top y line
-       lcd.setPixel(0,j);
-       lcd.setPixel(83,j);
-       lcd.refresh();
-       }
-    for (int j=29; j<48; j++) { // left/right bottom y line
-       lcd.setPixel(0,j);
-       lcd.setPixel(83,j);
-       lcd.refresh();
-       }
     for (int i=41; i<43; i++) { // vertical cross line
        for (int j=14; j<34; j++) {
            lcd.setPixel(i,j);
@@ -410,4 +449,14 @@
 void timer_isr()
 {
 game_timer_flag = 1;   
+}
+
+void rb_isr()
+{
+rb_flag = 1;   
+}
+
+void lb_isr()
+{
+lb_flag = 1;   
 }
\ No newline at end of file
--- a/main.h	Wed May 04 12:33:52 2016 +0000
+++ b/main.h	Wed May 04 15:16:12 2016 +0000
@@ -27,6 +27,8 @@
 DigitalOut redLed(PTA2);
 AnalogIn pot(PTB10);
 Beep buzzer(PTA1);
+InterruptIn RB(PTE24);
+InterruptIn LB(PTE25);
 
 /// create enumerated type (0,1,2,3 etc. for current direction snake is travelling (not joystick reading))
 enum CurrentDirection {
@@ -40,12 +42,15 @@
 
 /// create enumerated type (0,1,2 etc. for different game modes on the menu)
 enum GameType {
-    classic,
-    infiniteMap,
-    hardMap,
+    classicMode,
+    infiniteMode,
+    hardMode,
 };
+GameType gameType = classicMode;
 
-
+volatile int game_timer_flag = 0; /*!< flag for game timer isr */
+volatile int rb_flag = 0; /*!< flag for right button isr */
+volatile int lb_flag = 0; /*!< flag for left button isr */
 int randomX =  rand() % 83 + 1; /*!< random number in the range of 1 to 83 assigned to randomX */
 int randomY =  rand() % 47 + 1; /*!< random number in the range of 1 to 47 assigned to randomY */  
 int randomXoddEven = randomX%2; /*!< distinguish whether randomX is odd or even */