ELEC2645 (2015/16) / Mbed 2 deprecated 2645_ProjectCode

Dependencies:   N5110 mbed

Revision:
1:9d69901e18d0
Parent:
0:d3a050703801
Child:
2:9e791f33c49f
--- a/main.cpp	Mon Apr 25 17:14:30 2016 +0000
+++ b/main.cpp	Wed Apr 27 22:27:56 2016 +0000
@@ -7,7 +7,7 @@
 N5110 lcd (PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);
 
 // connections for joystick
-DigitalIn buttonJoy(PTB11);
+DigitalIn button(PTB11);
 AnalogIn xPot(PTB2);
 AnalogIn yPot(PTB3);
 
@@ -50,33 +50,122 @@
 
 int printFlag = 0;
 
+// function prototypes
+void calibrateJoystick();
+void updateJoystick();
+
+int menuSelect = 2;
+
 int main()
 {
-    lcd.init();
-    lcd.clear();
-    lcd.normalMode();
-    lcd.printString("Comet Crusher!",0,0);
-    lcd.printString("Start",10,2);
-    lcd.printString("Difficulty",10,3);
-    lcd.printString("Scores",10,4);
-    lcd.printString("Instructions",10,5);
-    lcd.printString(">",5,2);
-    while(1) {
+    for (; 2 > menuSelect < 5;) {
+        calibrateJoystick();  // get centred values of joystick
+        pollJoystick.attach(&updateJoystick,1.0/10.0);  // read joystick 10 times per second
+        lcd.init();
+        lcd.clear();
+        lcd.normalMode();
+        lcd.printString("Comet Crusher!",0,0);
+        lcd.printString("Start",10,2);
+        lcd.printString("Instructions",10,3);
+        lcd.printString("Difficulty",10,4);
+        lcd.printString("Scores",10,5);
+        lcd.printString(">",5,menuSelect);
+        lcd.refresh();
+
+        if (menuSelect == 2) {
+            if (joystick.direction == DOWN) {
+                menuSelect = 3;
+                sleep();
+            } else if (joystick.direction == UP) {
+                menuSelect = 5;
+                sleep();
+            }
+            lcd.refresh();
+            sleep();
+        }
+        if (menuSelect == 3) {
+            if (joystick.direction == DOWN) {
+                menuSelect = 4;
+                sleep();
+            } else if (joystick.direction == UP) {
+                menuSelect = 2;
+                sleep();
+            }
+            lcd.refresh();
+            sleep();
+        }
+        if (menuSelect == 4) {
+            if (joystick.direction == DOWN) {
+                menuSelect = 5; 
+                sleep();
+            } else if (joystick.direction == UP) {
+                menuSelect = 3;
+                sleep();
+            }
+            lcd.refresh();
+            sleep();
+        }
+        if (menuSelect == 5) {
+            if (joystick.direction == DOWN) {
+                menuSelect = 2;
+                sleep();
+            } else if (joystick.direction == UP) {
+                menuSelect = 4;
+                sleep();
+            }
+            lcd.refresh();
+            sleep();
+        }
+        sleep();
     }
 }
-
-void Game()
+void game()
 {
 }
 
-void DifficultySet()
+void difficultySet()
+{
+}
+
+void highScore()
+{
+}
+
+void instructions()
 {
 }
 
-void HighScore()
+void calibrateJoystick()
 {
+    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;
 
-void Instructions()
-{
-}
+    // 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 = DOWN;
+    } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
+        joystick.direction = UP;
+    } 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;
+}
\ No newline at end of file