Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.h
00001 /** 00002 @file main.h 00003 @brief Header file containing functions prototypes, defines and global variables. 00004 @brief Shows examples of creating Doxygen documentation. 00005 @brief Revision 1.0. 00006 @author Martin Woodhams 00007 @date March 2015 00008 */ 00009 00010 #ifndef MAIN_H 00011 #define MAIN_H 00012 00013 #include "mbed.h" 00014 #include "N5110.h" 00015 #include "Arrays.h" 00016 00017 00018 #define UP 0 00019 #define UPRIGHT 1 00020 #define RIGHT 2 00021 #define DOWNRIGHT 3 00022 #define DOWN 4 00023 #define DOWNLEFT 5 00024 #define LEFT 6 00025 #define UPLEFT 7 00026 #define STAY 8 00027 00028 #define DIRECTION_TOLERANCE 0.025f 00029 00030 /** 00031 @namespace lcd 00032 @brief This allows the mbed to interface with the N5110 screen 00033 @param VCC - PTE26 00034 @param SCE - PTA0 00035 @param RST - PTC4 00036 @param D/C - PTD0 00037 @param MOSI - PTD2 00038 @param SCLK - PTD1 00039 @param LED - PTC3 00040 */ 00041 N5110 lcd (PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3); 00042 00043 /** 00044 @namespace b1 00045 @brief Sets up a button that allows for interrupts to be called on pin PTB19 00046 */ 00047 InterruptIn b1 (PTB19); 00048 00049 /** 00050 @namespace b2 00051 @brief Sets up a button that allows for interrupts to be called on pin PTB18 00052 */ 00053 InterruptIn b2 (PTB18); 00054 00055 /** 00056 @namespace xPot 00057 @brief Sets up the potentiometer that determines the horizontal position of the joystick 00058 */ 00059 AnalogIn xPot(PTB2); 00060 00061 /** 00062 @namespace yPot 00063 @brief Sets up the potentiometer that determines the vertical position of the joystick 00064 */ 00065 AnalogIn yPot(PTB3); 00066 00067 /** 00068 @namespace ticker 00069 @brief A ticker that is attatched to a time based interrupt to allow the screen to periodically refresh 00070 */ 00071 Ticker ticker; 00072 00073 /** 00074 @namespace timer 00075 @brief A timer that is used as the seed to generate a random number for asteroid creation 00076 */ 00077 Timer timer; 00078 00079 volatile int g_b1_flag = 0; /*!<A global flag which is called on when the interrupt on b1 is called */ 00080 volatile int g_b2_flag = 0; /*!<A global flag which is called on when the interrupt on b2 is called */ 00081 volatile int g_timer_flag = 0; /*!<A global flag which is called on when the timer flags */ 00082 int score = 0; /*!< A variable that keeps track of the score the player is achieving */ 00083 int bullet_number =0; /*!< A variable that keeps count of how many bullets are on screen */ 00084 int shipDirection = 0; /*!< A variable determined by the FSM, this variable chooses which ship array is displayed and which direction bullets are shot*/ 00085 int game_over = 0; /*!< A variable that determines when the game is over */ 00086 int printFlag = 0; /*!<a flag to show that the joystick can be read*/ 00087 int state = 1; /*!< set initial state of the FSM state 1 UP*/ 00088 int direction = UP; /*!<the initial direction of the ship is to travel UP */ 00089 int settings = 0; /*!<aa flag to tell if the settings option has been selected*/ 00090 00091 struct State { 00092 int shipDirection; // current state 00093 int next_state[9]; // next state can be up determined by up to 9 different inputs 00094 00095 }; /*!< creates the structure for the FSM*/ 00096 00097 typedef const struct State STyp ; 00098 00099 STyp fsm [8] = { 00100 {0,{0,1,1,1,1,7,7,7,0}}, 00101 {1,{0,1,2,2,2,2,0,0,1}}, 00102 {2,{1,1,2,3,3,3,3,1,2}}, 00103 {3,{2,2,2,3,4,4,4,4,3}}, 00104 {4,{5,3,3,3,4,5,5,5,4}}, 00105 {5,{6,6,4,4,4,5,6,6,5}}, 00106 {6,{7,7,7,5,5,5,6,7,6}}, 00107 {7,{0,0,0,0,6,6,6,7,7}}, 00108 00109 }; /*!< The FSM has 9 possible inputs and 8 possible outcomes the inputs are taken from the joystick direction and go as following 00110 UP = 0, UPRIGHT = 1, RIGHT = 2, DOWNRIGHT = 3, DOWN = 4, DOWNLEFT = 5, LEFT = 6, UPLEFT = 7, STAY = 8 00111 The ships direction follows the same pattern and the FSM aims to get the ship to face that direction in the quickest way possible 00112 If the joystick is pushed in a direction completly opposite to the ship the ship will spin clockwise 00113 */ 00114 00115 /** 00116 A function which alters the b1 flag from 0 to 1 when an interrupt occurs 00117 */ 00118 void b1_isr(); 00119 00120 /** 00121 A function which alters the b2 flag from 0 to 1 when an interrupt occurs 00122 */ 00123 void b2_isr(); 00124 00125 /** 00126 A function which alters the timer flag from 0 to 1 when the time interrupt occurs 00127 */ 00128 void timer_isr(); 00129 00130 /** 00131 Reads the initial positon of the joystick and sets this as centered 00132 */ 00133 void calibrateJoystick(); 00134 00135 /** 00136 Compares the current positon of the joystick to the centred position of the joystick and calculates which way the joystick has been pushed 00137 */ 00138 void updateJoystick(); 00139 00140 /** 00141 The main game function in it the 00142 */ 00143 void game(); 00144 00145 /** 00146 A function that reads the ships direction and fires a bullet in that direction. 00147 The function also checks any exsisting bullets and moves them in the appropriate direction. 00148 */ 00149 void shoots(); 00150 00151 /** 00152 A function that using random numbers determines which size asteroid to generate and from which direction 00153 The function also checks to see if there is a bullet or ship in front of it, if there isn't the asteroid moves forward 00154 */ 00155 void asteroidMove(); 00156 00157 /** 00158 checks to see if the bullet has left the screen without hitting anything if it has the bullet is erased 00159 */ 00160 void checkbullet(); 00161 00162 #endif
Generated on Tue Jul 12 2022 16:35:19 by
