My ELEC2645 project. Nikollao Sulollari. 200804685

Dependencies:   N5110 SoftPWM mbed

Fork of Treasure_Hunt by ELEC2645 (2015/16)

Revision:
7:f31a4b4beb91
Parent:
6:c11bb31a06b3
Child:
8:606a488fa720
--- a/main.h	Mon Mar 28 12:10:24 2016 +0000
+++ b/main.h	Mon Apr 04 12:20:42 2016 +0000
@@ -10,24 +10,117 @@
 #ifndef MAIN_H
 #define MAIN_H
 #include "mbed.h"
+#include "N5110.h"
 
+#define DIRECTION_TOLERANCE 0.05
+/**
+@nameSpace r_led
+@brief output for status of red LED
+*/
+DigitalOut r_led(LED_RED);
+
+/**
+@nameSpace g_led
+@brief output for status of green LED
+*/
+DigitalOut g_led(LED_GREEN);
 
 /**
-@nameSpace led
-@brief GPO output for status of LED
+@nameSpace b_led
+@brief output for status of blue LED
 */
-DigitalOut r_led(LED_RED);
-DigitalOut g_led(LED_GREEN);
 DigitalOut b_led(LED_BLUE);
 
+/**
+@nameSpace lcd
+@brief object of the N5110 class
+*/
+N5110 lcd(PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3);
+
+/**
+@nameSpace pc
+@brief serial connection between mbed and pc
+*/
+Serial pc(USBTX,USBRX);
+
 // K64F on-board switches
 InterruptIn sw2(SW2);
 InterruptIn sw3(SW3);
 
+/**
+@nameSpace button
+@brief interrupt executes an event triggered task
+*/
 InterruptIn button(PTB2);
+
+/**
+@nameSpace xPot
+@brief read x-axis position from the value of the joystick
+*/
 AnalogIn xPot(PTB3);
+
+/**
+@nameSpace yPot
+@brief read y-axis position from the value of the joystick
+*/
 AnalogIn yPot(PTB10);
-PwmOut speaker(PTC11);
+
+/**
+@nameSpace buzzer
+@brief create PWM signal to set buzzer duty cycle and period
+*/
+PwmOut buzzer(PTC11);
+
+/**
+@nameSpace ticker
+@brief interrupt executes a time-triggered task
+*/
+Ticker ticker;
+
+/**
+@nameSpace game_ticker
+@brief interrupt executes a time-triggered task
+*/
+Ticker game_ticker;
+
+/**
+@nameSpace timeout
+@brief interrupt calls a function after a specified amount of time
+*/
+Timeout timeout;
+
+
+/**
+@nameSpace DirectionName
+@brief define joystick's direction based on its x,y values
+*/
+enum DirectionName {
+    UP,
+    DOWN,
+    LEFT,
+    RIGHT,
+    CENTRE,
+    UP_LEFT,
+    UP_RIGHT,
+    DOWN_LEFT,
+    DOWN_RIGHT
+};
+
+/**
+@nameSpace Joystick
+@brief create strcut Joystick
+*/
+typedef struct JoyStick Joystick;
+struct JoyStick {
+    double x;    /// current x value
+    double x0;   /// 'centred' x value
+    double y;    /// current y value
+    double 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;
 
 /**
 Displays an error message
@@ -144,9 +237,11 @@
 */
 int intersection(int i, int j);
 
-volatile int rectY; /*!< used to determine x-axis position of the rect enemy */
 
-volatile int rectX ;/*!< used to determine y-axis position of the rect enemy */
+//vars
+volatile int rectX; /*!< used to determine x-axis position of the rect enemy */
+
+volatile int rectY ;/*!< used to determine y-axis position of the rect enemy */
 
 volatile int circleX;/*!< used to determine x-axis position of the circular enemy */
 
@@ -179,5 +274,355 @@
 
 int reset = 0; /*!< used to save current level of difficulty */
 
+int objectX = 10;
+
+int objectY = 20;
+
+void enemies()
+{
+    /// generate enemies in the screen depending on the level difficulty
+
+    if (level == 0) {
+
+        enemyRect();
+        //enemyCircle();
+    } else if (level == 1) {
+
+        enemyRect();
+        //enemyCircle();
+    } else if (level == 2) {
+
+    } else if (level == 3) {
+
+    }
+    enemyCircle();
+}
+
+void enemyRect()
+{
+    ///generate rect shape enemy
+    lcd.drawRect(rectX,rectY,5,4,1);
+    rectX = rectX + rand()%4 - 2;
+    rectY++; ///enemy moving towards hero
+}
+
+void hero()
+{
+    ///cotrol hero
+    
+    if (xPot > 0.5) { 
+        heroX++;
+    } else {
+        heroX--;
+    }
+    
+    if (yPot > 0.5) {
+        heroY--;
+    } else {
+        heroY++;
+    }
+    ///set x-axis boundaries so hero does not go out of screen
+    if (heroX > 35) {
+        heroX = 35;
+    }
+    if (heroX < -40) {
+        heroX = -40;
+    }
+    ///draw hero
+    lcd.drawLine(40+heroX, 47+heroY, 48+heroX, 43+heroY,1);
+    lcd.drawLine(40+heroX, 43+heroY,48+heroX, 47+heroY,1);
+    lcd.drawLine(44+heroX, 45+heroY,44+heroX, 41+heroY,1);
+    lcd.drawCircle(44+heroX, 39+heroY,2,0);
+}
+
+void enemyCircle()
+{
+    ///generate circle shape enemy
+    lcd.drawCircle(circleX,circleY,4,1);
+    circleY = circleY + rand() %4 - 2;
+
+    circleX++; ///enemy moving towards hero
+}
+
+void init_game()
+{
+    ///initialise game
+    //button.mode(PullNone);
+    srand(time(NULL)); /// generate random numbers
+    rectY = 0; /// init rectX, rectY
+    rectX = rand() %40 + 20;
+    circleY = rand() %20 + 10; /// init circleX, circleY
+    circleX = 0;
+
+    if ( play == 0) { ///if its first play print welcome message, otherwise don't
+
+        lcd.setBrightness(0.5); // put LED backlight on 50%
+        timeout.attach(&timeout_isr,2);
+        lcd.printString("Welcome to",11,1);
+        lcd.printString("Treasure Hunt!",1,3);
+        lcd.refresh();
+        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) {
+
+                //settings_menu();
+                pc.printf("Modify Settings!");
+            } else {
+
+                lcd.turnOff();
+                deepsleep();
+            }
+        }
+        sleep();
+    }
+}
+void guidance()
+{
+    /// show arrow to act as guidance towards the treasure
+    if (level < 7) { ///check level of difficulty
+
+        lcd.drawLine(42,0,42,4,1);
+        lcd.drawLine(42,0,40,2,1);
+        lcd.drawLine(42,0,44,2,1);
+    } else if (level == 7) {
+
+        lcd.printString("F",42,0); /// print the treasure icon
+    } else if (level == 8) {
+
+        ticker.detach();
+        lcd.clear();
+        lcd.printString("Well done!",0,0);
+        lcd.refresh();
+        timeout.attach(&timeout_isr,2);
+        sleep();
+        lcd.clear();
+        lcd.printString("Play again <",0,0);
+        lcd.refresh();
+        sleep();
+
+        if  (g_button_flag) {
+
+            g_button_flag = 0;
+            level = 0;
+            play++;
+            lcd.clear();
+            //main();
+        }
+    }
+}
+
+void obstacles()
+{           
+    /// place obstacles in the screen
+    /// as level difficulty increases, more obstacles are added
+    if (level == 1) {
+
+        lcd.drawRect(10,15,2,2,1);
+        lcd.drawRect(74,15,2,2,1);
+    } else if (level == 2) {
+
+        lcd.drawRect(10,15,2,2,1);
+        lcd.drawRect(74,15,2,2,1);
+        lcd.drawRect(10,28,2,2,1);
+        lcd.drawRect(74,28,2,2,1);
+    } else if (level == 3) {
+
+        lcd.drawRect(10,15,2,2,1);
+        lcd.drawRect(74,15,2,2,1);
+        lcd.drawRect(10,28,2,2,1);
+        lcd.drawRect(74,28,2,2,1);
+        lcd.drawRect(30,15,2,2,1);
+        lcd.drawRect(54,15,2,2,1);
+        lcd.drawRect(30,28,2,2,1);
+        lcd.drawRect(54,28,2,2,1);
+    } else if (level == 4) {
+
+        lcd.drawRect(10 + rand() %4 - 2,15 + rand() &4 -2,2,2,1);
+        lcd.drawRect(74 + rand() %4 - 2,15 + rand() %4 - 2,2,2,1);
+        lcd.drawRect(10 + rand() %4 - 2,28 + rand() %4 - 2,2,2,1);
+        lcd.drawRect(74 + rand() %4 - 2,28 + rand() %4 - 2,2,2,1);
+        lcd.drawRect(30 + rand() %4 - 2,15,2,2,1);
+        lcd.drawRect(54,15 + rand() %4 - 2,2,2,1);
+        lcd.drawRect(30,28 + rand() %4 - 2,2,2,1);
+        lcd.drawRect(54 + rand() %4 - 2,28,2,2,1);
+    } else if ( level == 5) {
+        
+        bool state = 0;
+        if (objectX == 71) {
+            state = 1;
+        } 
+        if (objectX == 15) {
+            state = 0;
+        }
+        if (state == 0) {
+            objectX++;
+        } else if (state == 1)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          {
+            objectX--;
+        }
+        
+        objectY = objectY + rand() %4 - 2;
+        ///keep moving object within boundaries
+        if (objectY <= 10) { 
+            objectY = 10;
+        } 
+        if (objectY >= 37) {
+            objectY = 37;
+        }
+        
+        lcd.drawLine(15,10,15,37,1);
+        lcd.drawLine(71,10,71,37,1);
+        lcd.drawRect(10+objectX,objectY,2,2,1); 
+    } else if (level == 6) {
+
+    } else if (level == 7) {
+
+        /*
+        lcd.drawLine(50,15,30,15,1);
+        lcd.drawLine(30,15,30,35,1);
+        lcd.drawLine(30,25,45,25,1);
+        */
+    }
+}
+
+
+void calibrateJoystick()
+{
+    // 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 = RIGHT;
+    } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
+        joystick.direction = LEFT;
+    } else if (joystick.y > DIRECTION_TOLERANCE  && joystick.x  < DIRECTION_TOLERANCE) {
+        joystick.direction = UP_LEFT;
+    } else if (joystick.y > DIRECTION_TOLERANCE && joystick.x > DIRECTION_TOLERANCE) {
+        joystick.direction = UP_RIGHT;
+    } else if (joystick.y < DIRECTION_TOLERANCE && joystick.x < DIRECTION_TOLERANCE) {
+        joystick.direction = DOWN_LEFT;
+    }   else if (joystick.y < DIRECTION_TOLERANCE && joystick.x > DIRECTION_TOLERANCE) {
+        joystick.direction = DOWN_RIGHT;
+    }
+}
+
+int menu()
+{
+
+    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;
+    }
+    return option;
+}
+
+int intersection(int i, int j)
+{
+    /// check for overlap between enemies and hero
+    n=0;
+
+    if (lcd.getPixel(i-1,j-1)!=0) //pixel to the top-left
+        n++; // increase n by 1
+    if (lcd.getPixel(i-1,j)!=0) //pixel to the left
+        n++; // increase n by 1
+    if (lcd.getPixel(i-1,j+1)!=0) //pixel to the bottom-left
+        n++; // increase n by 1
+    if (lcd.getPixel(i,j-1)!=0) // pixel to the top
+        n++; // increase n by 1
+    if (lcd.getPixel(i,j+1)!=0) //pixel to the bottom
+        n++; // increase n by 1
+    if (lcd.getPixel(i+1,j-1)!=0) //pixel to the top-right
+        n++; // increase n by 1
+    if (lcd.getPixel(i+1,j)!=0) // pixel to the right
+        n++; // increase n by 1
+    if (lcd.getPixel(i+1,j+1)!=0) //pixel to the bottom right
+        n++; // increase n by 1
+    return n;
+}
+
+void checkOverlap()
+{
+
+    for (int i=40+heroX; i<50+heroX; i++) {
+        for (int j=35+heroY; j<48+heroY; j++) {
+            
+            int count = intersection(i,j); 
+            //lcd.setPixel(i,j);
+        }
+    }
+}
+
+void error()
+{
+    /// display error message
+    while (1) {
+
+        lcd.printString("Error!",0,0);
+        r_led = 0;
+        wait(0.2);
+        r_led = 1;
+        wait(0.2);
+    }
+}
+
+
 #endif
 
+