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@0:00f0deb0a029, 2016-05-05 (annotated)
- Committer:
- Martin48
- Date:
- Thu May 05 13:45:31 2016 +0000
- Revision:
- 0:00f0deb0a029
Almost complete game asteroids somewhat temporamental
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Martin48 | 0:00f0deb0a029 | 1 | /** |
Martin48 | 0:00f0deb0a029 | 2 | @file main.h |
Martin48 | 0:00f0deb0a029 | 3 | @brief Header file containing functions prototypes, defines and global variables. |
Martin48 | 0:00f0deb0a029 | 4 | @brief Shows examples of creating Doxygen documentation. |
Martin48 | 0:00f0deb0a029 | 5 | @brief Revision 1.0. |
Martin48 | 0:00f0deb0a029 | 6 | @author Martin Woodhams |
Martin48 | 0:00f0deb0a029 | 7 | @date March 2015 |
Martin48 | 0:00f0deb0a029 | 8 | */ |
Martin48 | 0:00f0deb0a029 | 9 | |
Martin48 | 0:00f0deb0a029 | 10 | #ifndef MAIN_H |
Martin48 | 0:00f0deb0a029 | 11 | #define MAIN_H |
Martin48 | 0:00f0deb0a029 | 12 | |
Martin48 | 0:00f0deb0a029 | 13 | #include "mbed.h" |
Martin48 | 0:00f0deb0a029 | 14 | #include "N5110.h" |
Martin48 | 0:00f0deb0a029 | 15 | #include "Arrays.h" |
Martin48 | 0:00f0deb0a029 | 16 | |
Martin48 | 0:00f0deb0a029 | 17 | |
Martin48 | 0:00f0deb0a029 | 18 | #define UP 0 |
Martin48 | 0:00f0deb0a029 | 19 | #define UPRIGHT 1 |
Martin48 | 0:00f0deb0a029 | 20 | #define RIGHT 2 |
Martin48 | 0:00f0deb0a029 | 21 | #define DOWNRIGHT 3 |
Martin48 | 0:00f0deb0a029 | 22 | #define DOWN 4 |
Martin48 | 0:00f0deb0a029 | 23 | #define DOWNLEFT 5 |
Martin48 | 0:00f0deb0a029 | 24 | #define LEFT 6 |
Martin48 | 0:00f0deb0a029 | 25 | #define UPLEFT 7 |
Martin48 | 0:00f0deb0a029 | 26 | #define STAY 8 |
Martin48 | 0:00f0deb0a029 | 27 | |
Martin48 | 0:00f0deb0a029 | 28 | #define DIRECTION_TOLERANCE 0.025f |
Martin48 | 0:00f0deb0a029 | 29 | |
Martin48 | 0:00f0deb0a029 | 30 | /** |
Martin48 | 0:00f0deb0a029 | 31 | @namespace lcd |
Martin48 | 0:00f0deb0a029 | 32 | @brief This allows the mbed to interface with the N5110 screen |
Martin48 | 0:00f0deb0a029 | 33 | @param VCC - PTE26 |
Martin48 | 0:00f0deb0a029 | 34 | @param SCE - PTA0 |
Martin48 | 0:00f0deb0a029 | 35 | @param RST - PTC4 |
Martin48 | 0:00f0deb0a029 | 36 | @param D/C - PTD0 |
Martin48 | 0:00f0deb0a029 | 37 | @param MOSI - PTD2 |
Martin48 | 0:00f0deb0a029 | 38 | @param SCLK - PTD1 |
Martin48 | 0:00f0deb0a029 | 39 | @param LED - PTC3 |
Martin48 | 0:00f0deb0a029 | 40 | */ |
Martin48 | 0:00f0deb0a029 | 41 | N5110 lcd (PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3); |
Martin48 | 0:00f0deb0a029 | 42 | |
Martin48 | 0:00f0deb0a029 | 43 | /** |
Martin48 | 0:00f0deb0a029 | 44 | @namespace b1 |
Martin48 | 0:00f0deb0a029 | 45 | @brief Sets up a button that allows for interrupts to be called on pin PTB19 |
Martin48 | 0:00f0deb0a029 | 46 | */ |
Martin48 | 0:00f0deb0a029 | 47 | InterruptIn b1 (PTB19); |
Martin48 | 0:00f0deb0a029 | 48 | |
Martin48 | 0:00f0deb0a029 | 49 | /** |
Martin48 | 0:00f0deb0a029 | 50 | @namespace b2 |
Martin48 | 0:00f0deb0a029 | 51 | @brief Sets up a button that allows for interrupts to be called on pin PTB18 |
Martin48 | 0:00f0deb0a029 | 52 | */ |
Martin48 | 0:00f0deb0a029 | 53 | InterruptIn b2 (PTB18); |
Martin48 | 0:00f0deb0a029 | 54 | |
Martin48 | 0:00f0deb0a029 | 55 | /** |
Martin48 | 0:00f0deb0a029 | 56 | @namespace xPot |
Martin48 | 0:00f0deb0a029 | 57 | @brief Sets up the potentiometer that determines the horizontal position of the joystick |
Martin48 | 0:00f0deb0a029 | 58 | */ |
Martin48 | 0:00f0deb0a029 | 59 | AnalogIn xPot(PTB2); |
Martin48 | 0:00f0deb0a029 | 60 | |
Martin48 | 0:00f0deb0a029 | 61 | /** |
Martin48 | 0:00f0deb0a029 | 62 | @namespace yPot |
Martin48 | 0:00f0deb0a029 | 63 | @brief Sets up the potentiometer that determines the vertical position of the joystick |
Martin48 | 0:00f0deb0a029 | 64 | */ |
Martin48 | 0:00f0deb0a029 | 65 | AnalogIn yPot(PTB3); |
Martin48 | 0:00f0deb0a029 | 66 | |
Martin48 | 0:00f0deb0a029 | 67 | /** |
Martin48 | 0:00f0deb0a029 | 68 | @namespace ticker |
Martin48 | 0:00f0deb0a029 | 69 | @brief A ticker that is attatched to a time based interrupt to allow the screen to periodically refresh |
Martin48 | 0:00f0deb0a029 | 70 | */ |
Martin48 | 0:00f0deb0a029 | 71 | Ticker ticker; |
Martin48 | 0:00f0deb0a029 | 72 | |
Martin48 | 0:00f0deb0a029 | 73 | /** |
Martin48 | 0:00f0deb0a029 | 74 | @namespace timer |
Martin48 | 0:00f0deb0a029 | 75 | @brief A timer that is used as the seed to generate a random number for asteroid creation |
Martin48 | 0:00f0deb0a029 | 76 | */ |
Martin48 | 0:00f0deb0a029 | 77 | Timer timer; |
Martin48 | 0:00f0deb0a029 | 78 | |
Martin48 | 0:00f0deb0a029 | 79 | volatile int g_b1_flag = 0; /*!<A global flag which is called on when the interrupt on b1 is called */ |
Martin48 | 0:00f0deb0a029 | 80 | volatile int g_b2_flag = 0; /*!<A global flag which is called on when the interrupt on b2 is called */ |
Martin48 | 0:00f0deb0a029 | 81 | volatile int g_timer_flag = 0; /*!<A global flag which is called on when the timer flags */ |
Martin48 | 0:00f0deb0a029 | 82 | int score = 0; /*!< A variable that keeps track of the score the player is achieving */ |
Martin48 | 0:00f0deb0a029 | 83 | int bullet_number=0; /*!< A variable that keeps count of how many bullets are on screen */ |
Martin48 | 0:00f0deb0a029 | 84 | int shipDirection = 0; /*!< A variable determined by the FSM, this variable chooses which ship array is displayed and which direction bullets are shot*/ |
Martin48 | 0:00f0deb0a029 | 85 | int game_over = 0; /*!< A variable that determines when the game is over */ |
Martin48 | 0:00f0deb0a029 | 86 | int printFlag = 0; /*!<a flag to show that the joystick can be read*/ |
Martin48 | 0:00f0deb0a029 | 87 | int state = 1; /*!< set initial state of the FSM state 1 UP*/ |
Martin48 | 0:00f0deb0a029 | 88 | int direction = UP; /*!<the initial direction of the ship is to travel UP */ |
Martin48 | 0:00f0deb0a029 | 89 | int settings = 0; /*!<aa flag to tell if the settings option has been selected*/ |
Martin48 | 0:00f0deb0a029 | 90 | |
Martin48 | 0:00f0deb0a029 | 91 | struct State { |
Martin48 | 0:00f0deb0a029 | 92 | int shipDirection; // current state |
Martin48 | 0:00f0deb0a029 | 93 | int next_state[9]; // next state can be up determined by up to 9 different inputs |
Martin48 | 0:00f0deb0a029 | 94 | |
Martin48 | 0:00f0deb0a029 | 95 | }; /*!< creates the structure for the FSM*/ |
Martin48 | 0:00f0deb0a029 | 96 | |
Martin48 | 0:00f0deb0a029 | 97 | typedef const struct State STyp; |
Martin48 | 0:00f0deb0a029 | 98 | |
Martin48 | 0:00f0deb0a029 | 99 | STyp fsm[8] = { |
Martin48 | 0:00f0deb0a029 | 100 | {0,{0,1,1,1,1,7,7,7,0}}, |
Martin48 | 0:00f0deb0a029 | 101 | {1,{0,1,2,2,2,2,0,0,1}}, |
Martin48 | 0:00f0deb0a029 | 102 | {2,{1,1,2,3,3,3,3,1,2}}, |
Martin48 | 0:00f0deb0a029 | 103 | {3,{2,2,2,3,4,4,4,4,3}}, |
Martin48 | 0:00f0deb0a029 | 104 | {4,{5,3,3,3,4,5,5,5,4}}, |
Martin48 | 0:00f0deb0a029 | 105 | {5,{6,6,4,4,4,5,6,6,5}}, |
Martin48 | 0:00f0deb0a029 | 106 | {6,{7,7,7,5,5,5,6,7,6}}, |
Martin48 | 0:00f0deb0a029 | 107 | {7,{0,0,0,0,6,6,6,7,7}}, |
Martin48 | 0:00f0deb0a029 | 108 | |
Martin48 | 0:00f0deb0a029 | 109 | }; /*!< The FSM has 9 possible inputs and 8 possible outcomes the inputs are taken from the joystick direction and go as following |
Martin48 | 0:00f0deb0a029 | 110 | UP = 0, UPRIGHT = 1, RIGHT = 2, DOWNRIGHT = 3, DOWN = 4, DOWNLEFT = 5, LEFT = 6, UPLEFT = 7, STAY = 8 |
Martin48 | 0:00f0deb0a029 | 111 | The ships direction follows the same pattern and the FSM aims to get the ship to face that direction in the quickest way possible |
Martin48 | 0:00f0deb0a029 | 112 | If the joystick is pushed in a direction completly opposite to the ship the ship will spin clockwise |
Martin48 | 0:00f0deb0a029 | 113 | */ |
Martin48 | 0:00f0deb0a029 | 114 | |
Martin48 | 0:00f0deb0a029 | 115 | /** |
Martin48 | 0:00f0deb0a029 | 116 | A function which alters the b1 flag from 0 to 1 when an interrupt occurs |
Martin48 | 0:00f0deb0a029 | 117 | */ |
Martin48 | 0:00f0deb0a029 | 118 | void b1_isr(); |
Martin48 | 0:00f0deb0a029 | 119 | |
Martin48 | 0:00f0deb0a029 | 120 | /** |
Martin48 | 0:00f0deb0a029 | 121 | A function which alters the b2 flag from 0 to 1 when an interrupt occurs |
Martin48 | 0:00f0deb0a029 | 122 | */ |
Martin48 | 0:00f0deb0a029 | 123 | void b2_isr(); |
Martin48 | 0:00f0deb0a029 | 124 | |
Martin48 | 0:00f0deb0a029 | 125 | /** |
Martin48 | 0:00f0deb0a029 | 126 | A function which alters the timer flag from 0 to 1 when the time interrupt occurs |
Martin48 | 0:00f0deb0a029 | 127 | */ |
Martin48 | 0:00f0deb0a029 | 128 | void timer_isr(); |
Martin48 | 0:00f0deb0a029 | 129 | |
Martin48 | 0:00f0deb0a029 | 130 | /** |
Martin48 | 0:00f0deb0a029 | 131 | Reads the initial positon of the joystick and sets this as centered |
Martin48 | 0:00f0deb0a029 | 132 | */ |
Martin48 | 0:00f0deb0a029 | 133 | void calibrateJoystick(); |
Martin48 | 0:00f0deb0a029 | 134 | |
Martin48 | 0:00f0deb0a029 | 135 | /** |
Martin48 | 0:00f0deb0a029 | 136 | Compares the current positon of the joystick to the centred position of the joystick and calculates which way the joystick has been pushed |
Martin48 | 0:00f0deb0a029 | 137 | */ |
Martin48 | 0:00f0deb0a029 | 138 | void updateJoystick(); |
Martin48 | 0:00f0deb0a029 | 139 | |
Martin48 | 0:00f0deb0a029 | 140 | /** |
Martin48 | 0:00f0deb0a029 | 141 | The main game function in it the |
Martin48 | 0:00f0deb0a029 | 142 | */ |
Martin48 | 0:00f0deb0a029 | 143 | void game(); |
Martin48 | 0:00f0deb0a029 | 144 | |
Martin48 | 0:00f0deb0a029 | 145 | /** |
Martin48 | 0:00f0deb0a029 | 146 | A function that reads the ships direction and fires a bullet in that direction. |
Martin48 | 0:00f0deb0a029 | 147 | The function also checks any exsisting bullets and moves them in the appropriate direction. |
Martin48 | 0:00f0deb0a029 | 148 | */ |
Martin48 | 0:00f0deb0a029 | 149 | void shoots(); |
Martin48 | 0:00f0deb0a029 | 150 | |
Martin48 | 0:00f0deb0a029 | 151 | /** |
Martin48 | 0:00f0deb0a029 | 152 | A function that using random numbers determines which size asteroid to generate and from which direction |
Martin48 | 0:00f0deb0a029 | 153 | 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 |
Martin48 | 0:00f0deb0a029 | 154 | */ |
Martin48 | 0:00f0deb0a029 | 155 | void asteroidMove(); |
Martin48 | 0:00f0deb0a029 | 156 | |
Martin48 | 0:00f0deb0a029 | 157 | /** |
Martin48 | 0:00f0deb0a029 | 158 | checks to see if the bullet has left the screen without hitting anything if it has the bullet is erased |
Martin48 | 0:00f0deb0a029 | 159 | */ |
Martin48 | 0:00f0deb0a029 | 160 | void checkbullet(); |
Martin48 | 0:00f0deb0a029 | 161 | |
Martin48 | 0:00f0deb0a029 | 162 | #endif |