ELEC2645 (2015/16) / Mbed 2 deprecated Treasure_Hunt

Dependencies:   N5110 SoftPWM mbed

Files at this revision

API Documentation at this revision

Comitter:
Nikollao
Date:
Thu Apr 28 16:29:06 2016 +0000
Parent:
10:1d229362c134
Child:
12:f591f1965abd
Child:
13:359d3aa66352
Commit message:
settings are working

Changed in this revision

SoftPWM.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/SoftPWM.lib	Thu Apr 28 16:29:06 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/komaida424/code/SoftPWM/#7918ce37626c
--- a/main.cpp	Wed Apr 27 17:09:13 2016 +0000
+++ b/main.cpp	Thu Apr 28 16:29:06 2016 +0000
@@ -9,17 +9,39 @@
 int main()
 {
     lcd.init();
+    button.rise(&button_isr); ///assign rise with ISR
+    button1.rise(&button1_isr);
     init_K64F();
     init_serial();
-    button.fall(&button_isr); ///assign rise with ISR
+    
+    /*while(1) {
+        
+        lcd.clear();
+        if (g_button_flag) {
+            lcd.printString("button is ON!",0,0);
+        } else {
+            lcd.printString("button is OFF!",0,0);
+        }
+        if (g_button1_flag) {
+            lcd.printString("button1 is ON",0,4);
+            lcd.refresh();
+        }else {
+            lcd.printString("button1 is OFF!",0,4);
+            lcd.refresh();
+        }
+        wait(0.2);
+        lcd.refresh();
+    }*/
+    
     init_game(); ///initialize game
     calibrateJoystick(); ///calibrate joystick
 
     ticker.attach(&timer_isr,0.1); ///attach ticker with ISR every 0.1 sec
     reset = level; ///set reset = level to check later if level has increased
-    buzzer = 0.5;
-    buzzer.period(1/4000);
+    // buzzer.start();
     //buzzer.write(0.5);
+    //buzzer.period_ms(4);    
+    menu();
         
     while (1) {
 
@@ -76,12 +98,12 @@
     // resistors that are enabled by default using InterruptIn
     sw2.mode(PullNone);
     sw3.mode(PullNone);
-    button.mode(PullUp);
+    button.mode(PullDown);
+    button1.mode(PullDown);
 }
 
 void timer_isr ()
 {
-
     g_timer_flag = 1;
 }
 
@@ -105,6 +127,11 @@
     g_button_flag =1;
 }
 
+void button1_isr() {
+    
+    g_button1_flag =1;
+}
+
 void timeout_isr()
 {
     if (button) {
--- a/main.h	Wed Apr 27 17:09:13 2016 +0000
+++ b/main.h	Thu Apr 28 16:29:06 2016 +0000
@@ -11,6 +11,7 @@
 #define MAIN_H
 #include "mbed.h"
 #include "N5110.h"
+#include "SoftPWM.h"
 
 #define DIRECTION_TOLERANCE 0.05
 /**
@@ -76,7 +77,7 @@
 @nameSpace buzzer
 @brief create PWM signal to set buzzer duty cycle and period
 */
-PwmOut buzzer(PTB9);
+SoftPWM buzzer(PTB9);
 
 /**
 @nameSpace ticker
@@ -130,6 +131,8 @@
 /// create struct variable
 Joystick joystick;
 
+int main();
+
 /**
 Displays an error message
 */
@@ -186,6 +189,11 @@
 /**
 Set-up flag to 1
 */
+void button1_isr();
+
+/**
+Set-up flag to 1
+*/
 void timeout_isr();
 
 
@@ -295,6 +303,8 @@
 
 int state1 = 1; /*!< controls the direction of the objtacle */
 
+int menuY = 5;
+
 
 void enemies()
 
@@ -330,10 +340,10 @@
     ///cotrol hero
     
     if (xPot > 0.60) { 
-        heroX++;
+        heroX--;
     }
      else if (xPot < 0.40){
-        heroX--;
+        heroX++;
     } else {
         heroX = heroX; 
     }
@@ -396,66 +406,6 @@
         sleep();
         lcd.clear();
     }
-    game_ticker.attach(game_timer_isr,0.2);
-
-    while (1) {
-
-        if (g_game_timer_flag) {
-
-            g_game_timer_flag = 0;
-            updateJoystick();
-            lcd.clear();
-            menu(); ///get Joystick's value to select option in menu
-            //option = menu();
-
-            if (option == 0) { /// select Start Game
-
-                lcd.printString("Start Game <",0,0);
-                lcd.printString("Settings",0,2);
-                lcd.printString("Exit",0,4);
-            } else if (option == 1) { /// select Settings
-
-                lcd.printString("Start Game",0,0);
-                lcd.printString("Settings <",0,2);
-                lcd.printString("Exit",0,4);
-            } else { /// select Exit
-
-                lcd.printString("Start Game",0,0);
-                lcd.printString("Settings",0,2);
-                lcd.printString("Exit <",0,4);
-            }
-        }
-        if (g_button_flag) {
-
-            g_button_flag = 0;
-
-            if (option == 0) {
-
-                game_ticker.detach();
-                break;
-            } else if (option == 1) {
-                
-                lcd.clear();
-                g_button_flag = 0;
-                while(1) {
-                    //settings_menu();
-                    pc.printf("Modify Settings!");
-                    lcd.printString("Lcd Inverse <",0,0);
-                    if (g_button_flag) {
-
-                        lcd.inverseMode();
-                        break;
-                    }
-                    sleep();
-                }
-            } else {
-
-                lcd.turnOff();
-                deepsleep();
-            }
-        }
-        sleep();
-    }
 }
 void guidance()
 {
@@ -480,14 +430,14 @@
         lcd.printString("Play again <",0,0);
         lcd.refresh();
         sleep();
+        if  (g_button1_flag) {
 
-        if  (g_button_flag) {
-
-            g_button_flag = 0;
+            g_button1_flag = 0;
             level = 0;
            // play++;
             lcd.clear();
-            //main();
+            main();
+            r_led = 0;
         }
     }
 }
@@ -640,15 +590,93 @@
 
 int menu()
 {
+    while(1) {
 
-    if (joystick.y <= 0.33) { /// if Joystick moves up
-        option = 0; /// the pointer moves upper in the menu display
-    } else if (joystick.y <= 0.66) { /// if joystick moves down
-        option = 1; /// else pointer moves down
-    } else {
-        option = 2;
+        updateJoystick();
+        lcd.clear();
+
+        lcd.printString("Start Game",0,0);
+        lcd.printString("Settings",0,2);
+        lcd.printString("Exit",0,4);
+        //lcd.drawCircle(70,4,2,1);
+
+        switch (joystick.direction) {
+            case UP:
+                option--;
+                break;
+            case DOWN:
+                option++;
+                break;
+        }
+        if (option < 0) {
+            option = 2;
+        }
+        if (option > 2) {
+            option = 0;
+        }
+
+        if (option == 0) {
+            lcd.drawCircle(70,4,2,1);
+        } else if (option == 1) {
+            lcd.drawCircle(55,20,2,1);
+        } else if (option == 2) {
+            lcd.drawCircle(35,35,2,1);
+        }
+
+        if(g_button_flag) {
+
+            g_button_flag = 0;
+
+            if (option == 0) {
+                break;
+            } else if (option == 1) {
+                
+                option = 0;
+
+                while(1) {
+
+                    updateJoystick();
+                    lcd.clear();
+                    lcd.printString("Game speed",0,0);
+                    lcd.printString("Lcd Mode",0,2);
+                    lcd.printString("Sound",0,4);
+                    switch (joystick.direction) {
+                        case UP:
+                            option--;
+                            break;
+                        case DOWN:
+                            option++;
+                            break;
+                        case CENTRE:
+                            option = option;
+                            break;
+                    }
+                    if (option < 0) {
+                        option = 2;
+                    }
+                    if (option > 2) {
+                        option = 0;
+                    }
+
+                    if (option == 0) {
+                        lcd.drawCircle(70,4,2,1);
+                    } else if (option == 1) {
+                        lcd.drawCircle(55,20,2,1);
+                    } else if (option == 2) {
+                        lcd.drawCircle(35,35,2,1);
+                    }
+                    lcd.refresh();
+                    sleep();
+                }
+            } else if (option == 2) {
+                lcd.clear();
+                lcd.turnOff();
+                deepsleep();
+            }
+        }
+        lcd.refresh();
+        sleep();
     }
-    return option;
 }
 
 int intersection(int i, int j)
@@ -680,8 +708,8 @@
 
     for (int i=40+heroX; i<50+heroX; i++) {
         for (int j=35+heroY; j<48+heroY; j++) {
-            
-            int count = intersection(i,j); 
+
+            int count = intersection(i,j);
             //lcd.setPixel(i,j);
         }
     }