ELEC2645 (2015/16)
/
Dodgemania
Wall dodging game utilising a joystick and Nokia 5110 LCD display
main.cpp@0:6b29f9c29a2a, 2016-04-22 (annotated)
- Committer:
- el14moh
- Date:
- Fri Apr 22 19:38:51 2016 +0000
- Revision:
- 0:6b29f9c29a2a
- Child:
- 1:26ebbb94cf36
Testing replacing the game_over_flag with break;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el14moh | 0:6b29f9c29a2a | 1 | /* Joystick |
el14moh | 0:6b29f9c29a2a | 2 | Max Hamilton |
el14moh | 0:6b29f9c29a2a | 3 | |
el14moh | 0:6b29f9c29a2a | 4 | My project will be designing a game in which the player must avoid a number |
el14moh | 0:6b29f9c29a2a | 5 | of obstacles coming from different locations for as long as possible |
el14moh | 0:6b29f9c29a2a | 6 | |
el14moh | 0:6b29f9c29a2a | 7 | Week 19 Code - Initial test of mbed screen and initilisations |
el14moh | 0:6b29f9c29a2a | 8 | Week 20 Code - Add code to move a player controlled ball around the screen |
el14moh | 0:6b29f9c29a2a | 9 | Week 21 Code - Add code to generate and move an obstacle down the screen |
el14moh | 0:6b29f9c29a2a | 10 | |
el14moh | 0:6b29f9c29a2a | 11 | Week 22 Code - Significant progress over easter holiday |
el14moh | 0:6b29f9c29a2a | 12 | - Diagonal directions added to joystick |
el14moh | 0:6b29f9c29a2a | 13 | - Collisions implemented. Game over triggers if character hits a wall |
el14moh | 0:6b29f9c29a2a | 14 | - Walls/obstacles developed greatly |
el14moh | 0:6b29f9c29a2a | 15 | - Obstacle extended into full wall. Wall structs created and walls travel from all sides of the screen |
el14moh | 0:6b29f9c29a2a | 16 | - Walls start of appearing from one side of the screen. As time goes on, more will appear from differnt sides, with multiple walls on screen at once |
el14moh | 0:6b29f9c29a2a | 17 | - Cooldown added to walls to ensure a new one will not instantly appear from a side that another wall has just travelled to. |
el14moh | 0:6b29f9c29a2a | 18 | - visual warning on screen when walls are about to appear from a new direction |
el14moh | 0:6b29f9c29a2a | 19 | - Game now keeps track of score and displays it at the end of the game |
el14moh | 0:6b29f9c29a2a | 20 | - Intro screen added |
el14moh | 0:6b29f9c29a2a | 21 | |
el14moh | 0:6b29f9c29a2a | 22 | */ |
el14moh | 0:6b29f9c29a2a | 23 | |
el14moh | 0:6b29f9c29a2a | 24 | #include "mbed.h" |
el14moh | 0:6b29f9c29a2a | 25 | #include "N5110.h" |
el14moh | 0:6b29f9c29a2a | 26 | #include "tones.h" |
el14moh | 0:6b29f9c29a2a | 27 | |
el14moh | 0:6b29f9c29a2a | 28 | #define DIRECTION_TOLERANCE 0.05 // tolerance of joystick direction |
el14moh | 0:6b29f9c29a2a | 29 | #define PLAYERRADIUS 2 // size of player ball |
el14moh | 0:6b29f9c29a2a | 30 | #define GAMESPEED 20 // game timer speed |
el14moh | 0:6b29f9c29a2a | 31 | #define JOYSPEED 20 // rate at which the joystick is read |
el14moh | 0:6b29f9c29a2a | 32 | #define PI 3.14159265359 |
el14moh | 0:6b29f9c29a2a | 33 | |
el14moh | 0:6b29f9c29a2a | 34 | // VCC, SCE, RST, D/C, MOSI, SCLK, LED |
el14moh | 0:6b29f9c29a2a | 35 | N5110 lcd (PTD3 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3); |
el14moh | 0:6b29f9c29a2a | 36 | |
el14moh | 0:6b29f9c29a2a | 37 | AnalogIn backlight(PTB2); // pot to control brightness |
el14moh | 0:6b29f9c29a2a | 38 | DigitalIn button(PTB3); // joystick button object |
el14moh | 0:6b29f9c29a2a | 39 | AnalogIn xPot(PTB11); // joystick x direction object |
el14moh | 0:6b29f9c29a2a | 40 | AnalogIn yPot(PTB10); // joystick y direction object |
el14moh | 0:6b29f9c29a2a | 41 | DigitalOut buzzer(PTA2); // buzzer object |
el14moh | 0:6b29f9c29a2a | 42 | |
el14moh | 0:6b29f9c29a2a | 43 | Ticker pollJoystick; // timer to regularly read the joystick |
el14moh | 0:6b29f9c29a2a | 44 | Ticker game_timer; // timer to regularly update the screen |
el14moh | 0:6b29f9c29a2a | 45 | Timer noteTimer; // timer for note tones |
el14moh | 0:6b29f9c29a2a | 46 | |
el14moh | 0:6b29f9c29a2a | 47 | Serial serial(USBTX,USBRX); // Serial for debug |
el14moh | 0:6b29f9c29a2a | 48 | |
el14moh | 0:6b29f9c29a2a | 49 | // create enumerated type (0,1,2,3 etc. for direction) |
el14moh | 0:6b29f9c29a2a | 50 | enum DirectionName { |
el14moh | 0:6b29f9c29a2a | 51 | UP, |
el14moh | 0:6b29f9c29a2a | 52 | DOWN, |
el14moh | 0:6b29f9c29a2a | 53 | LEFT, |
el14moh | 0:6b29f9c29a2a | 54 | RIGHT, |
el14moh | 0:6b29f9c29a2a | 55 | UPRIGHT, // Diagonally up + right |
el14moh | 0:6b29f9c29a2a | 56 | UPLEFT, // Diagonally up + left |
el14moh | 0:6b29f9c29a2a | 57 | DOWNRIGHT, // Diagonally down + right |
el14moh | 0:6b29f9c29a2a | 58 | DOWNLEFT, // Diagonally down + left |
el14moh | 0:6b29f9c29a2a | 59 | CENTRE, |
el14moh | 0:6b29f9c29a2a | 60 | UNKNOWN |
el14moh | 0:6b29f9c29a2a | 61 | }; |
el14moh | 0:6b29f9c29a2a | 62 | |
el14moh | 0:6b29f9c29a2a | 63 | typedef struct JoyStick Joystick; // struct for Joystick |
el14moh | 0:6b29f9c29a2a | 64 | typedef struct Wall Wall; // struct for Walls |
el14moh | 0:6b29f9c29a2a | 65 | |
el14moh | 0:6b29f9c29a2a | 66 | struct JoyStick { |
el14moh | 0:6b29f9c29a2a | 67 | float x; // current x value |
el14moh | 0:6b29f9c29a2a | 68 | float x0; // 'centred' x value |
el14moh | 0:6b29f9c29a2a | 69 | float y; // current y value |
el14moh | 0:6b29f9c29a2a | 70 | float y0; // 'centred' y value |
el14moh | 0:6b29f9c29a2a | 71 | int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed) |
el14moh | 0:6b29f9c29a2a | 72 | DirectionName direction; // current direction |
el14moh | 0:6b29f9c29a2a | 73 | }; |
el14moh | 0:6b29f9c29a2a | 74 | |
el14moh | 0:6b29f9c29a2a | 75 | struct Wall { |
el14moh | 0:6b29f9c29a2a | 76 | int x; // x-coordinate of wall (realtive to centre of the gap) |
el14moh | 0:6b29f9c29a2a | 77 | int y; // y-coordinate of wall (relative to centre of the gap) |
el14moh | 0:6b29f9c29a2a | 78 | DirectionName direction; // Direction the wall travels in |
el14moh | 0:6b29f9c29a2a | 79 | int random; // randomly generated integer to determine when a wall begins to travel |
el14moh | 0:6b29f9c29a2a | 80 | int cooldown; // stops a wall respawning before a certain amount of time |
el14moh | 0:6b29f9c29a2a | 81 | bool moveFlag; // flag to determine if wall is on screen |
el14moh | 0:6b29f9c29a2a | 82 | bool genFlag; // flag to determine if wall has been generated |
el14moh | 0:6b29f9c29a2a | 83 | }; |
el14moh | 0:6b29f9c29a2a | 84 | |
el14moh | 0:6b29f9c29a2a | 85 | // struct variable for joystick |
el14moh | 0:6b29f9c29a2a | 86 | Joystick joystick; |
el14moh | 0:6b29f9c29a2a | 87 | // struct variable for moving walls |
el14moh | 0:6b29f9c29a2a | 88 | Wall leftwall; |
el14moh | 0:6b29f9c29a2a | 89 | Wall rightwall; |
el14moh | 0:6b29f9c29a2a | 90 | Wall downwall; |
el14moh | 0:6b29f9c29a2a | 91 | Wall upwall; |
el14moh | 0:6b29f9c29a2a | 92 | |
el14moh | 0:6b29f9c29a2a | 93 | void calibrateJoystick(); // read default positions of the joystick to calibrate later readings |
el14moh | 0:6b29f9c29a2a | 94 | void updateJoystick(); // reads direction the joystick has been moved |
el14moh | 0:6b29f9c29a2a | 95 | void initDisplay(); // initialises the LCD display |
el14moh | 0:6b29f9c29a2a | 96 | void game_timer_isr(); // sets flag for timer interrupt |
el14moh | 0:6b29f9c29a2a | 97 | void moveBall(); // reads joystick direction and moves position of the player |
el14moh | 0:6b29f9c29a2a | 98 | void moveWall(); // moves walls along the screen |
el14moh | 0:6b29f9c29a2a | 99 | void checkCollision(); // checks for any collisions (i.e. player has hit a wall or side of the screen) |
el14moh | 0:6b29f9c29a2a | 100 | void updateScreen(); // refreshes the screen, redraws player and walls |
el14moh | 0:6b29f9c29a2a | 101 | void warning(); // flashes screen when a new wall is ready to appear |
el14moh | 0:6b29f9c29a2a | 102 | void init_serial(); // sets baud rate for serial |
el14moh | 0:6b29f9c29a2a | 103 | void debug(); // prints for debug purposes |
el14moh | 0:6b29f9c29a2a | 104 | |
el14moh | 0:6b29f9c29a2a | 105 | float refresh_rate = GAMESPEED; // how often to update display (Hz) |
el14moh | 0:6b29f9c29a2a | 106 | float g_dt = 1.0F/refresh_rate; // global to store time step (F makes it a float, gets rid of compiler warning) |
el14moh | 0:6b29f9c29a2a | 107 | volatile int g_timer_flag = 0; // flag for timer interrupt |
el14moh | 0:6b29f9c29a2a | 108 | int printFlag = 0; // flag for printing |
el14moh | 0:6b29f9c29a2a | 109 | int game_over_flag = 0; // flag to signal game over |
el14moh | 0:6b29f9c29a2a | 110 | int game_start_flag = 0; // flag to start the game |
el14moh | 0:6b29f9c29a2a | 111 | int i = 42; // x-coordinate value of player |
el14moh | 0:6b29f9c29a2a | 112 | int j = 24; // y-coordinate value of player |
el14moh | 0:6b29f9c29a2a | 113 | int counter = 0; // number of times code has looped |
el14moh | 0:6b29f9c29a2a | 114 | |
el14moh | 0:6b29f9c29a2a | 115 | |
el14moh | 0:6b29f9c29a2a | 116 | |
el14moh | 0:6b29f9c29a2a | 117 | int main() |
el14moh | 0:6b29f9c29a2a | 118 | { |
el14moh | 0:6b29f9c29a2a | 119 | init_serial(); |
el14moh | 0:6b29f9c29a2a | 120 | srand(time(NULL)); // generate seed for random number generation |
el14moh | 0:6b29f9c29a2a | 121 | calibrateJoystick(); |
el14moh | 0:6b29f9c29a2a | 122 | pollJoystick.attach(&updateJoystick,1.0/JOYSPEED); // read joystick (JOYSPEED) times per second |
el14moh | 0:6b29f9c29a2a | 123 | initDisplay(); |
el14moh | 0:6b29f9c29a2a | 124 | |
el14moh | 0:6b29f9c29a2a | 125 | |
el14moh | 0:6b29f9c29a2a | 126 | wait (1.0); |
el14moh | 0:6b29f9c29a2a | 127 | |
el14moh | 0:6b29f9c29a2a | 128 | game_timer.attach(&game_timer_isr,g_dt); |
el14moh | 0:6b29f9c29a2a | 129 | |
el14moh | 0:6b29f9c29a2a | 130 | while(1) { |
el14moh | 0:6b29f9c29a2a | 131 | |
el14moh | 0:6b29f9c29a2a | 132 | lcd.printString("Dodgemania",13,2); // Print game title on screen |
el14moh | 0:6b29f9c29a2a | 133 | wait (2.5); |
el14moh | 0:6b29f9c29a2a | 134 | for (int z=0; z<88; z++) { |
el14moh | 0:6b29f9c29a2a | 135 | lcd.drawCircle(z,20,4,1); |
el14moh | 0:6b29f9c29a2a | 136 | |
el14moh | 0:6b29f9c29a2a | 137 | lcd.clearPixel(z-3,16); |
el14moh | 0:6b29f9c29a2a | 138 | lcd.clearPixel(z-4,17); |
el14moh | 0:6b29f9c29a2a | 139 | lcd.clearPixel(z-4,18); |
el14moh | 0:6b29f9c29a2a | 140 | lcd.clearPixel(z-5,19); |
el14moh | 0:6b29f9c29a2a | 141 | lcd.clearPixel(z-5,20); |
el14moh | 0:6b29f9c29a2a | 142 | lcd.clearPixel(z-5,21); |
el14moh | 0:6b29f9c29a2a | 143 | lcd.clearPixel(z-4,22); |
el14moh | 0:6b29f9c29a2a | 144 | lcd.clearPixel(z-4,23); |
el14moh | 0:6b29f9c29a2a | 145 | lcd.clearPixel(z-3,24); |
el14moh | 0:6b29f9c29a2a | 146 | lcd.refresh(); |
el14moh | 0:6b29f9c29a2a | 147 | wait(0.01); |
el14moh | 0:6b29f9c29a2a | 148 | } |
el14moh | 0:6b29f9c29a2a | 149 | |
el14moh | 0:6b29f9c29a2a | 150 | lcd.clear(); |
el14moh | 0:6b29f9c29a2a | 151 | wait(0.5); |
el14moh | 0:6b29f9c29a2a | 152 | |
el14moh | 0:6b29f9c29a2a | 153 | lcd.inverseMode(); |
el14moh | 0:6b29f9c29a2a | 154 | wait(0.2); |
el14moh | 0:6b29f9c29a2a | 155 | lcd.normalMode(); |
el14moh | 0:6b29f9c29a2a | 156 | |
el14moh | 0:6b29f9c29a2a | 157 | lcd.printString("Press button",6,2); |
el14moh | 0:6b29f9c29a2a | 158 | lcd.printString("to start",18,3); |
el14moh | 0:6b29f9c29a2a | 159 | |
el14moh | 0:6b29f9c29a2a | 160 | while(game_start_flag == 0) { // replace with interrupt? |
el14moh | 0:6b29f9c29a2a | 161 | if(button) { |
el14moh | 0:6b29f9c29a2a | 162 | game_start_flag = 1; |
el14moh | 0:6b29f9c29a2a | 163 | lcd.clear(); |
el14moh | 0:6b29f9c29a2a | 164 | wait(0.5); |
el14moh | 0:6b29f9c29a2a | 165 | } |
el14moh | 0:6b29f9c29a2a | 166 | } |
el14moh | 0:6b29f9c29a2a | 167 | |
el14moh | 0:6b29f9c29a2a | 168 | lcd.inverseMode(); |
el14moh | 0:6b29f9c29a2a | 169 | wait(0.2); |
el14moh | 0:6b29f9c29a2a | 170 | lcd.normalMode(); |
el14moh | 0:6b29f9c29a2a | 171 | |
el14moh | 0:6b29f9c29a2a | 172 | // Draw game border |
el14moh | 0:6b29f9c29a2a | 173 | lcd.drawLine(2,2,81,2,1); |
el14moh | 0:6b29f9c29a2a | 174 | lcd.drawLine(2,2,2,45,1); |
el14moh | 0:6b29f9c29a2a | 175 | lcd.drawLine(81,2,81,45,1); |
el14moh | 0:6b29f9c29a2a | 176 | lcd.drawLine(2,45,81,45,1); |
el14moh | 0:6b29f9c29a2a | 177 | |
el14moh | 0:6b29f9c29a2a | 178 | lcd.drawLine(0,0,83,0,1); |
el14moh | 0:6b29f9c29a2a | 179 | lcd.drawLine(0,0,0,47,1); |
el14moh | 0:6b29f9c29a2a | 180 | lcd.drawLine(83,0,83,47,1); |
el14moh | 0:6b29f9c29a2a | 181 | lcd.drawLine(0,47,83,47,1); |
el14moh | 0:6b29f9c29a2a | 182 | |
el14moh | 0:6b29f9c29a2a | 183 | lcd.refresh(); |
el14moh | 0:6b29f9c29a2a | 184 | |
el14moh | 0:6b29f9c29a2a | 185 | // Countdown |
el14moh | 0:6b29f9c29a2a | 186 | wait(0.5); |
el14moh | 0:6b29f9c29a2a | 187 | lcd.printString("3",40,2); |
el14moh | 0:6b29f9c29a2a | 188 | wait(0.5); |
el14moh | 0:6b29f9c29a2a | 189 | lcd.drawRect(10,10,64,28,2); |
el14moh | 0:6b29f9c29a2a | 190 | lcd.refresh(); |
el14moh | 0:6b29f9c29a2a | 191 | wait(0.5); |
el14moh | 0:6b29f9c29a2a | 192 | lcd.printString("2",40,2); |
el14moh | 0:6b29f9c29a2a | 193 | wait(0.5); |
el14moh | 0:6b29f9c29a2a | 194 | lcd.drawRect(10,10,64,28,2); |
el14moh | 0:6b29f9c29a2a | 195 | lcd.refresh(); |
el14moh | 0:6b29f9c29a2a | 196 | wait(0.5); |
el14moh | 0:6b29f9c29a2a | 197 | lcd.printString("1",40,2); |
el14moh | 0:6b29f9c29a2a | 198 | wait(0.5); |
el14moh | 0:6b29f9c29a2a | 199 | lcd.drawRect(10,10,64,28,2); |
el14moh | 0:6b29f9c29a2a | 200 | lcd.refresh(); |
el14moh | 0:6b29f9c29a2a | 201 | wait(0.5); |
el14moh | 0:6b29f9c29a2a | 202 | lcd.drawRect(10,10,64,28,2); |
el14moh | 0:6b29f9c29a2a | 203 | lcd.refresh(); |
el14moh | 0:6b29f9c29a2a | 204 | lcd.printString("Go!",36,2); |
el14moh | 0:6b29f9c29a2a | 205 | wait(0.5); |
el14moh | 0:6b29f9c29a2a | 206 | lcd.drawRect(10,10,64,28,2); |
el14moh | 0:6b29f9c29a2a | 207 | lcd.refresh(); |
el14moh | 0:6b29f9c29a2a | 208 | |
el14moh | 0:6b29f9c29a2a | 209 | |
el14moh | 0:6b29f9c29a2a | 210 | leftwall.y = rand() % 27+8; |
el14moh | 0:6b29f9c29a2a | 211 | rightwall.y = rand() % 27+8; |
el14moh | 0:6b29f9c29a2a | 212 | downwall.x = rand() % 27+8; |
el14moh | 0:6b29f9c29a2a | 213 | upwall.x = rand() % 27+8; |
el14moh | 0:6b29f9c29a2a | 214 | rightwall.cooldown = 61; |
el14moh | 0:6b29f9c29a2a | 215 | |
el14moh | 0:6b29f9c29a2a | 216 | while (game_over_flag == 0) { |
el14moh | 0:6b29f9c29a2a | 217 | |
el14moh | 0:6b29f9c29a2a | 218 | if ( g_timer_flag ) { // ticker interrupt |
el14moh | 0:6b29f9c29a2a | 219 | g_timer_flag = 0; // clear flag |
el14moh | 0:6b29f9c29a2a | 220 | moveWall(); |
el14moh | 0:6b29f9c29a2a | 221 | moveBall(); |
el14moh | 0:6b29f9c29a2a | 222 | checkCollision(); |
el14moh | 0:6b29f9c29a2a | 223 | updateScreen(); |
el14moh | 0:6b29f9c29a2a | 224 | warning(); |
el14moh | 0:6b29f9c29a2a | 225 | debug(); |
el14moh | 0:6b29f9c29a2a | 226 | |
el14moh | 0:6b29f9c29a2a | 227 | counter++; // increment counter each cycle (approx. 20 points a second) |
el14moh | 0:6b29f9c29a2a | 228 | |
el14moh | 0:6b29f9c29a2a | 229 | // wall cooldowns increased. N.B these are set to 0 when a wall finishes moving |
el14moh | 0:6b29f9c29a2a | 230 | leftwall.cooldown++; |
el14moh | 0:6b29f9c29a2a | 231 | rightwall.cooldown++; |
el14moh | 0:6b29f9c29a2a | 232 | downwall.cooldown++; |
el14moh | 0:6b29f9c29a2a | 233 | upwall.cooldown++; |
el14moh | 0:6b29f9c29a2a | 234 | } |
el14moh | 0:6b29f9c29a2a | 235 | sleep(); |
el14moh | 0:6b29f9c29a2a | 236 | } |
el14moh | 0:6b29f9c29a2a | 237 | |
el14moh | 0:6b29f9c29a2a | 238 | lcd.inverseMode(); |
el14moh | 0:6b29f9c29a2a | 239 | wait(0.1); |
el14moh | 0:6b29f9c29a2a | 240 | lcd.normalMode(); |
el14moh | 0:6b29f9c29a2a | 241 | wait(2.0); |
el14moh | 0:6b29f9c29a2a | 242 | lcd.clear(); |
el14moh | 0:6b29f9c29a2a | 243 | wait(0.2); |
el14moh | 0:6b29f9c29a2a | 244 | |
el14moh | 0:6b29f9c29a2a | 245 | lcd.drawRect(11,5,62,11,0); |
el14moh | 0:6b29f9c29a2a | 246 | lcd.printString("Game Over!",14,1); |
el14moh | 0:6b29f9c29a2a | 247 | lcd.refresh(); |
el14moh | 0:6b29f9c29a2a | 248 | |
el14moh | 0:6b29f9c29a2a | 249 | wait(1.0); |
el14moh | 0:6b29f9c29a2a | 250 | lcd.drawRect(11,21,62,21,0); |
el14moh | 0:6b29f9c29a2a | 251 | lcd.printString("Score:",14,3); |
el14moh | 0:6b29f9c29a2a | 252 | char buffer[14]; |
el14moh | 0:6b29f9c29a2a | 253 | int length = sprintf(buffer,"%d",counter); |
el14moh | 0:6b29f9c29a2a | 254 | if (counter <= 999999999) { // 999999999 is highest number that fits in score display box |
el14moh | 0:6b29f9c29a2a | 255 | lcd.printString(buffer,14,4); |
el14moh | 0:6b29f9c29a2a | 256 | } else { |
el14moh | 0:6b29f9c29a2a | 257 | lcd.printString ("Too High!",14,4); // if score is too large to fit in box (unlikely!) |
el14moh | 0:6b29f9c29a2a | 258 | } |
el14moh | 0:6b29f9c29a2a | 259 | lcd.refresh(); |
el14moh | 0:6b29f9c29a2a | 260 | |
el14moh | 0:6b29f9c29a2a | 261 | wait(5.0); |
el14moh | 0:6b29f9c29a2a | 262 | lcd.inverseMode(); |
el14moh | 0:6b29f9c29a2a | 263 | wait(0.2); |
el14moh | 0:6b29f9c29a2a | 264 | lcd.normalMode(); |
el14moh | 0:6b29f9c29a2a | 265 | lcd.clear(); |
el14moh | 0:6b29f9c29a2a | 266 | |
el14moh | 0:6b29f9c29a2a | 267 | return 0; |
el14moh | 0:6b29f9c29a2a | 268 | } |
el14moh | 0:6b29f9c29a2a | 269 | |
el14moh | 0:6b29f9c29a2a | 270 | } |
el14moh | 0:6b29f9c29a2a | 271 | |
el14moh | 0:6b29f9c29a2a | 272 | void moveBall() // reads joystick direction and moves position of the player |
el14moh | 0:6b29f9c29a2a | 273 | { |
el14moh | 0:6b29f9c29a2a | 274 | if (joystick.direction == UP) { |
el14moh | 0:6b29f9c29a2a | 275 | j-=1; |
el14moh | 0:6b29f9c29a2a | 276 | } else if (joystick.direction == DOWN) { |
el14moh | 0:6b29f9c29a2a | 277 | j+=1; |
el14moh | 0:6b29f9c29a2a | 278 | } else if (joystick.direction == LEFT) { |
el14moh | 0:6b29f9c29a2a | 279 | i-=1; |
el14moh | 0:6b29f9c29a2a | 280 | } else if (joystick.direction == RIGHT) { |
el14moh | 0:6b29f9c29a2a | 281 | i+=1; |
el14moh | 0:6b29f9c29a2a | 282 | } else if (joystick.direction == UPRIGHT) { |
el14moh | 0:6b29f9c29a2a | 283 | j-=1; |
el14moh | 0:6b29f9c29a2a | 284 | i+=1; |
el14moh | 0:6b29f9c29a2a | 285 | } else if (joystick.direction == UPLEFT) { |
el14moh | 0:6b29f9c29a2a | 286 | j-=1; |
el14moh | 0:6b29f9c29a2a | 287 | i-=1; |
el14moh | 0:6b29f9c29a2a | 288 | } else if (joystick.direction == DOWNRIGHT) { |
el14moh | 0:6b29f9c29a2a | 289 | j+=1; |
el14moh | 0:6b29f9c29a2a | 290 | i+=1; |
el14moh | 0:6b29f9c29a2a | 291 | } else if (joystick.direction == DOWNLEFT) { |
el14moh | 0:6b29f9c29a2a | 292 | j+=1; |
el14moh | 0:6b29f9c29a2a | 293 | i-=1; |
el14moh | 0:6b29f9c29a2a | 294 | } |
el14moh | 0:6b29f9c29a2a | 295 | } |
el14moh | 0:6b29f9c29a2a | 296 | |
el14moh | 0:6b29f9c29a2a | 297 | void moveWall() // moves walls along the screen |
el14moh | 0:6b29f9c29a2a | 298 | { |
el14moh | 0:6b29f9c29a2a | 299 | leftwall.random = rand()%20; |
el14moh | 0:6b29f9c29a2a | 300 | rightwall.random = rand()%20; |
el14moh | 0:6b29f9c29a2a | 301 | downwall.random = rand()%50; |
el14moh | 0:6b29f9c29a2a | 302 | upwall.random = rand()%50; |
el14moh | 0:6b29f9c29a2a | 303 | |
el14moh | 0:6b29f9c29a2a | 304 | // LEFT WALL |
el14moh | 0:6b29f9c29a2a | 305 | if (leftwall.moveFlag == 1) { // if wall is moving |
el14moh | 0:6b29f9c29a2a | 306 | leftwall.x-=1; // move wall left |
el14moh | 0:6b29f9c29a2a | 307 | if (leftwall.x<3) { // if wall hits a border |
el14moh | 0:6b29f9c29a2a | 308 | leftwall.moveFlag = 0; // stop wall moving |
el14moh | 0:6b29f9c29a2a | 309 | leftwall.cooldown = 0; // reset the cooldown for the wall |
el14moh | 0:6b29f9c29a2a | 310 | } |
el14moh | 0:6b29f9c29a2a | 311 | } else { // if wall has stopped moving |
el14moh | 0:6b29f9c29a2a | 312 | if (leftwall.genFlag == 0) { // if a new wall HASN'T been generated |
el14moh | 0:6b29f9c29a2a | 313 | leftwall.y = rand() % 27+8; // make new random y-coordinate |
el14moh | 0:6b29f9c29a2a | 314 | leftwall.x = 82; // reset x-coordinate to rightmost position |
el14moh | 0:6b29f9c29a2a | 315 | leftwall.genFlag = 1; // wall has been generated |
el14moh | 0:6b29f9c29a2a | 316 | } else { // if a new wall HAS been generated |
el14moh | 0:6b29f9c29a2a | 317 | if (leftwall.cooldown > 80) { // if a new wall hasnt started moving in 4 seconds, force it to move |
el14moh | 0:6b29f9c29a2a | 318 | leftwall.moveFlag = 1; |
el14moh | 0:6b29f9c29a2a | 319 | leftwall.genFlag = 0; |
el14moh | 0:6b29f9c29a2a | 320 | } else if ((leftwall.random == 1)&&(rightwall.cooldown > 60)) { // if wall starts moving again |
el14moh | 0:6b29f9c29a2a | 321 | leftwall.moveFlag = 1; // start wall moving |
el14moh | 0:6b29f9c29a2a | 322 | leftwall.genFlag = 0; // clear 'wall generated' flag |
el14moh | 0:6b29f9c29a2a | 323 | } else { // else if wall has not started moving again |
el14moh | 0:6b29f9c29a2a | 324 | leftwall.moveFlag = 0; // wall is stopped |
el14moh | 0:6b29f9c29a2a | 325 | } |
el14moh | 0:6b29f9c29a2a | 326 | } |
el14moh | 0:6b29f9c29a2a | 327 | } |
el14moh | 0:6b29f9c29a2a | 328 | |
el14moh | 0:6b29f9c29a2a | 329 | // RIGHT WALL |
el14moh | 0:6b29f9c29a2a | 330 | if (counter > 200) { |
el14moh | 0:6b29f9c29a2a | 331 | if (rightwall.moveFlag == 1) { |
el14moh | 0:6b29f9c29a2a | 332 | rightwall.x+=1; |
el14moh | 0:6b29f9c29a2a | 333 | if (rightwall.x>80) { |
el14moh | 0:6b29f9c29a2a | 334 | rightwall.moveFlag = 0; |
el14moh | 0:6b29f9c29a2a | 335 | rightwall.cooldown = 0; |
el14moh | 0:6b29f9c29a2a | 336 | } |
el14moh | 0:6b29f9c29a2a | 337 | } else { |
el14moh | 0:6b29f9c29a2a | 338 | if ((rightwall.genFlag == 0)) { |
el14moh | 0:6b29f9c29a2a | 339 | rightwall.y = rand() % 27+8; |
el14moh | 0:6b29f9c29a2a | 340 | rightwall.x = 1; |
el14moh | 0:6b29f9c29a2a | 341 | rightwall.genFlag = 1; |
el14moh | 0:6b29f9c29a2a | 342 | } else { |
el14moh | 0:6b29f9c29a2a | 343 | if (rightwall.cooldown > 80) { |
el14moh | 0:6b29f9c29a2a | 344 | rightwall.moveFlag = 1; |
el14moh | 0:6b29f9c29a2a | 345 | rightwall.genFlag = 0; |
el14moh | 0:6b29f9c29a2a | 346 | } else if ((rightwall.random == 1) && (leftwall.cooldown > 60)) { |
el14moh | 0:6b29f9c29a2a | 347 | rightwall.moveFlag = 1; |
el14moh | 0:6b29f9c29a2a | 348 | rightwall.genFlag = 0; |
el14moh | 0:6b29f9c29a2a | 349 | } else { |
el14moh | 0:6b29f9c29a2a | 350 | rightwall.moveFlag = 0; |
el14moh | 0:6b29f9c29a2a | 351 | } |
el14moh | 0:6b29f9c29a2a | 352 | } |
el14moh | 0:6b29f9c29a2a | 353 | } |
el14moh | 0:6b29f9c29a2a | 354 | } |
el14moh | 0:6b29f9c29a2a | 355 | |
el14moh | 0:6b29f9c29a2a | 356 | // DOWN WALL |
el14moh | 0:6b29f9c29a2a | 357 | if (counter > 600) { |
el14moh | 0:6b29f9c29a2a | 358 | if (downwall.moveFlag == 1) { |
el14moh | 0:6b29f9c29a2a | 359 | if (upwall.cooldown > 60) { |
el14moh | 0:6b29f9c29a2a | 360 | if (counter % 2 == 1) { // horizontal walls move half the speed of vertical walls |
el14moh | 0:6b29f9c29a2a | 361 | downwall.y+=1; |
el14moh | 0:6b29f9c29a2a | 362 | } |
el14moh | 0:6b29f9c29a2a | 363 | } |
el14moh | 0:6b29f9c29a2a | 364 | if (downwall.y>44) { |
el14moh | 0:6b29f9c29a2a | 365 | downwall.moveFlag = 0; |
el14moh | 0:6b29f9c29a2a | 366 | } |
el14moh | 0:6b29f9c29a2a | 367 | } else { |
el14moh | 0:6b29f9c29a2a | 368 | if (downwall.genFlag == 0) { |
el14moh | 0:6b29f9c29a2a | 369 | downwall.x = rand() % 63+8; |
el14moh | 0:6b29f9c29a2a | 370 | downwall.y = 1; |
el14moh | 0:6b29f9c29a2a | 371 | downwall.genFlag = 1; |
el14moh | 0:6b29f9c29a2a | 372 | } else { |
el14moh | 0:6b29f9c29a2a | 373 | if (downwall.cooldown > 80) { |
el14moh | 0:6b29f9c29a2a | 374 | downwall.moveFlag = 1; |
el14moh | 0:6b29f9c29a2a | 375 | downwall.genFlag = 0; |
el14moh | 0:6b29f9c29a2a | 376 | } else if ((downwall.random == 1)&&(upwall.cooldown > 60)) { |
el14moh | 0:6b29f9c29a2a | 377 | downwall.moveFlag = 1; |
el14moh | 0:6b29f9c29a2a | 378 | downwall.genFlag = 0; |
el14moh | 0:6b29f9c29a2a | 379 | } else { |
el14moh | 0:6b29f9c29a2a | 380 | downwall.moveFlag = 0; |
el14moh | 0:6b29f9c29a2a | 381 | } |
el14moh | 0:6b29f9c29a2a | 382 | } |
el14moh | 0:6b29f9c29a2a | 383 | } |
el14moh | 0:6b29f9c29a2a | 384 | } |
el14moh | 0:6b29f9c29a2a | 385 | |
el14moh | 0:6b29f9c29a2a | 386 | // UP WALL |
el14moh | 0:6b29f9c29a2a | 387 | if (counter > 1500) { |
el14moh | 0:6b29f9c29a2a | 388 | if (upwall.moveFlag == 1) { |
el14moh | 0:6b29f9c29a2a | 389 | if (downwall.cooldown > 60) { |
el14moh | 0:6b29f9c29a2a | 390 | if (counter % 2 == 1) { // horizontal walls move half the speed of vertical walls |
el14moh | 0:6b29f9c29a2a | 391 | upwall.y-=1; |
el14moh | 0:6b29f9c29a2a | 392 | } |
el14moh | 0:6b29f9c29a2a | 393 | } |
el14moh | 0:6b29f9c29a2a | 394 | if (upwall.y<3) { |
el14moh | 0:6b29f9c29a2a | 395 | upwall.moveFlag = 0; |
el14moh | 0:6b29f9c29a2a | 396 | } |
el14moh | 0:6b29f9c29a2a | 397 | } else { |
el14moh | 0:6b29f9c29a2a | 398 | if (upwall.genFlag == 0) { |
el14moh | 0:6b29f9c29a2a | 399 | upwall.x = rand() % 63+8; |
el14moh | 0:6b29f9c29a2a | 400 | upwall.y = 46; |
el14moh | 0:6b29f9c29a2a | 401 | upwall.genFlag = 1; |
el14moh | 0:6b29f9c29a2a | 402 | } else { |
el14moh | 0:6b29f9c29a2a | 403 | if (upwall.cooldown > 80) { |
el14moh | 0:6b29f9c29a2a | 404 | upwall.moveFlag = 1; |
el14moh | 0:6b29f9c29a2a | 405 | upwall.genFlag = 0; |
el14moh | 0:6b29f9c29a2a | 406 | } else if ((upwall.random == 1)&&(downwall.cooldown > 60)) { |
el14moh | 0:6b29f9c29a2a | 407 | upwall.moveFlag = 1; |
el14moh | 0:6b29f9c29a2a | 408 | upwall.genFlag = 0; |
el14moh | 0:6b29f9c29a2a | 409 | } else { |
el14moh | 0:6b29f9c29a2a | 410 | upwall.moveFlag = 0; |
el14moh | 0:6b29f9c29a2a | 411 | } |
el14moh | 0:6b29f9c29a2a | 412 | } |
el14moh | 0:6b29f9c29a2a | 413 | } |
el14moh | 0:6b29f9c29a2a | 414 | } |
el14moh | 0:6b29f9c29a2a | 415 | } |
el14moh | 0:6b29f9c29a2a | 416 | |
el14moh | 0:6b29f9c29a2a | 417 | void checkCollision() // checks for any collisions (i.e. player has hit a wall or side of the screen) |
el14moh | 0:6b29f9c29a2a | 418 | { |
el14moh | 0:6b29f9c29a2a | 419 | // if floor |
el14moh | 0:6b29f9c29a2a | 420 | if ( j >= 47 - (PLAYERRADIUS+3)) { |
el14moh | 0:6b29f9c29a2a | 421 | j = 47 - (PLAYERRADIUS+3); |
el14moh | 0:6b29f9c29a2a | 422 | } |
el14moh | 0:6b29f9c29a2a | 423 | |
el14moh | 0:6b29f9c29a2a | 424 | // if roof |
el14moh | 0:6b29f9c29a2a | 425 | if ( j <= (PLAYERRADIUS+3)) { |
el14moh | 0:6b29f9c29a2a | 426 | j = (PLAYERRADIUS+3); |
el14moh | 0:6b29f9c29a2a | 427 | } |
el14moh | 0:6b29f9c29a2a | 428 | |
el14moh | 0:6b29f9c29a2a | 429 | // if right wall |
el14moh | 0:6b29f9c29a2a | 430 | if ( i >= 83 - (PLAYERRADIUS+3)) { |
el14moh | 0:6b29f9c29a2a | 431 | i = 83 - (PLAYERRADIUS+3); |
el14moh | 0:6b29f9c29a2a | 432 | } |
el14moh | 0:6b29f9c29a2a | 433 | |
el14moh | 0:6b29f9c29a2a | 434 | // if left wall |
el14moh | 0:6b29f9c29a2a | 435 | if ( i <= (PLAYERRADIUS+3)) { |
el14moh | 0:6b29f9c29a2a | 436 | i = (PLAYERRADIUS+3); |
el14moh | 0:6b29f9c29a2a | 437 | } |
el14moh | 0:6b29f9c29a2a | 438 | |
el14moh | 0:6b29f9c29a2a | 439 | // LEFT WALL |
el14moh | 0:6b29f9c29a2a | 440 | if ((((i - PLAYERRADIUS) <= leftwall.x) && (leftwall.x <= (i + PLAYERRADIUS))) && (j > (leftwall.y+3) || j < (leftwall.y-3))) { |
el14moh | 0:6b29f9c29a2a | 441 | game_over_flag = 1; |
el14moh | 0:6b29f9c29a2a | 442 | } |
el14moh | 0:6b29f9c29a2a | 443 | |
el14moh | 0:6b29f9c29a2a | 444 | // RIGHT WALL |
el14moh | 0:6b29f9c29a2a | 445 | if ((((i - PLAYERRADIUS) <= rightwall.x) && (rightwall.x <= (i + PLAYERRADIUS))) && (j > (rightwall.y+3) || j < (rightwall.y-3))) { |
el14moh | 0:6b29f9c29a2a | 446 | game_over_flag = 1; |
el14moh | 0:6b29f9c29a2a | 447 | } |
el14moh | 0:6b29f9c29a2a | 448 | |
el14moh | 0:6b29f9c29a2a | 449 | // DOWN WALL |
el14moh | 0:6b29f9c29a2a | 450 | if ((((j - PLAYERRADIUS) <= downwall.y) && (downwall.y <= (j - PLAYERRADIUS))) && (i > (downwall.x+9) || i < (downwall.x-9))) { // if player x is 4 or more than wall x, it has missed the gap |
el14moh | 0:6b29f9c29a2a | 451 | game_over_flag = 1; |
el14moh | 0:6b29f9c29a2a | 452 | } |
el14moh | 0:6b29f9c29a2a | 453 | |
el14moh | 0:6b29f9c29a2a | 454 | // UP WALL |
el14moh | 0:6b29f9c29a2a | 455 | if (((j + PLAYERRADIUS) == upwall.y || (j - PLAYERRADIUS) == upwall.y) && (i > (upwall.x+9) || i < (upwall.x-9))) { // if player x is 4 or more than wall x, it has missed the gap |
el14moh | 0:6b29f9c29a2a | 456 | game_over_flag = 1; |
el14moh | 0:6b29f9c29a2a | 457 | } |
el14moh | 0:6b29f9c29a2a | 458 | } |
el14moh | 0:6b29f9c29a2a | 459 | |
el14moh | 0:6b29f9c29a2a | 460 | void updateScreen() // refreshes the screen, redraws player and walls |
el14moh | 0:6b29f9c29a2a | 461 | { |
el14moh | 0:6b29f9c29a2a | 462 | lcd.clear(); |
el14moh | 0:6b29f9c29a2a | 463 | lcd.drawCircle(i,j,PLAYERRADIUS,1); |
el14moh | 0:6b29f9c29a2a | 464 | lcd.refresh(); // update display |
el14moh | 0:6b29f9c29a2a | 465 | |
el14moh | 0:6b29f9c29a2a | 466 | // draw Border |
el14moh | 0:6b29f9c29a2a | 467 | lcd.drawLine(2,2,81,2,1); |
el14moh | 0:6b29f9c29a2a | 468 | lcd.drawLine(2,2,2,45,1); |
el14moh | 0:6b29f9c29a2a | 469 | lcd.drawLine(81,2,81,45,1); |
el14moh | 0:6b29f9c29a2a | 470 | lcd.drawLine(2,45,81,45,1); |
el14moh | 0:6b29f9c29a2a | 471 | |
el14moh | 0:6b29f9c29a2a | 472 | lcd.drawLine(0,0,83,0,1); |
el14moh | 0:6b29f9c29a2a | 473 | lcd.drawLine(0,0,0,47,1); |
el14moh | 0:6b29f9c29a2a | 474 | lcd.drawLine(83,0,83,47,1); |
el14moh | 0:6b29f9c29a2a | 475 | lcd.drawLine(0,47,83,47,1); |
el14moh | 0:6b29f9c29a2a | 476 | |
el14moh | 0:6b29f9c29a2a | 477 | // draw walls |
el14moh | 0:6b29f9c29a2a | 478 | // LEFT WALL |
el14moh | 0:6b29f9c29a2a | 479 | lcd.drawLine(leftwall.x,leftwall.y+5,leftwall.x,44,1); |
el14moh | 0:6b29f9c29a2a | 480 | lcd.drawLine(leftwall.x,leftwall.y-5,leftwall.x,3,1); |
el14moh | 0:6b29f9c29a2a | 481 | lcd.drawLine(leftwall.x+1,leftwall.y+5,leftwall.x+1,44,1); |
el14moh | 0:6b29f9c29a2a | 482 | lcd.drawLine(leftwall.x+1,leftwall.y-5,leftwall.x+1,3,1); |
el14moh | 0:6b29f9c29a2a | 483 | |
el14moh | 0:6b29f9c29a2a | 484 | // RIGHT WALL |
el14moh | 0:6b29f9c29a2a | 485 | if (counter > 200) { |
el14moh | 0:6b29f9c29a2a | 486 | lcd.drawLine(rightwall.x,rightwall.y+5,rightwall.x,44,1); |
el14moh | 0:6b29f9c29a2a | 487 | lcd.drawLine(rightwall.x,rightwall.y-5,rightwall.x,3,1); |
el14moh | 0:6b29f9c29a2a | 488 | lcd.drawLine(rightwall.x-1,rightwall.y+5,rightwall.x-1,44,1); |
el14moh | 0:6b29f9c29a2a | 489 | lcd.drawLine(rightwall.x-1,rightwall.y-5,rightwall.x-1,3,1); |
el14moh | 0:6b29f9c29a2a | 490 | } |
el14moh | 0:6b29f9c29a2a | 491 | |
el14moh | 0:6b29f9c29a2a | 492 | // DOWN WALL |
el14moh | 0:6b29f9c29a2a | 493 | if (counter > 600) { |
el14moh | 0:6b29f9c29a2a | 494 | lcd.drawLine(downwall.x+11,downwall.y,80,downwall.y,1); |
el14moh | 0:6b29f9c29a2a | 495 | lcd.drawLine(downwall.x-11,downwall.y,3,downwall.y,1); |
el14moh | 0:6b29f9c29a2a | 496 | lcd.drawLine(downwall.x+11,downwall.y-1,80,downwall.y-1,1); |
el14moh | 0:6b29f9c29a2a | 497 | lcd.drawLine(downwall.x-11,downwall.y-1,3,downwall.y-1,1); |
el14moh | 0:6b29f9c29a2a | 498 | } |
el14moh | 0:6b29f9c29a2a | 499 | |
el14moh | 0:6b29f9c29a2a | 500 | // UP WALL |
el14moh | 0:6b29f9c29a2a | 501 | if (counter > 1500) { |
el14moh | 0:6b29f9c29a2a | 502 | lcd.drawLine(upwall.x+11,upwall.y,80,upwall.y,1); |
el14moh | 0:6b29f9c29a2a | 503 | lcd.drawLine(upwall.x-11,upwall.y,3,upwall.y,1); |
el14moh | 0:6b29f9c29a2a | 504 | lcd.drawLine(upwall.x+11,upwall.y+1,80,upwall.y+1,1); |
el14moh | 0:6b29f9c29a2a | 505 | lcd.drawLine(upwall.x-11,upwall.y+1,3,upwall.y+1,1); |
el14moh | 0:6b29f9c29a2a | 506 | } |
el14moh | 0:6b29f9c29a2a | 507 | |
el14moh | 0:6b29f9c29a2a | 508 | lcd.refresh(); |
el14moh | 0:6b29f9c29a2a | 509 | } |
el14moh | 0:6b29f9c29a2a | 510 | |
el14moh | 0:6b29f9c29a2a | 511 | void warning () |
el14moh | 0:6b29f9c29a2a | 512 | { |
el14moh | 0:6b29f9c29a2a | 513 | if (counter == 170) { |
el14moh | 0:6b29f9c29a2a | 514 | lcd.inverseMode(); |
el14moh | 0:6b29f9c29a2a | 515 | } else if (counter == 570) { |
el14moh | 0:6b29f9c29a2a | 516 | lcd.inverseMode(); |
el14moh | 0:6b29f9c29a2a | 517 | } else if (counter == 1470) { |
el14moh | 0:6b29f9c29a2a | 518 | lcd.inverseMode(); |
el14moh | 0:6b29f9c29a2a | 519 | } else { |
el14moh | 0:6b29f9c29a2a | 520 | lcd.normalMode(); |
el14moh | 0:6b29f9c29a2a | 521 | } |
el14moh | 0:6b29f9c29a2a | 522 | } |
el14moh | 0:6b29f9c29a2a | 523 | |
el14moh | 0:6b29f9c29a2a | 524 | void game_timer_isr() // sets flag for timer interrupt |
el14moh | 0:6b29f9c29a2a | 525 | { |
el14moh | 0:6b29f9c29a2a | 526 | g_timer_flag = 1; |
el14moh | 0:6b29f9c29a2a | 527 | } |
el14moh | 0:6b29f9c29a2a | 528 | |
el14moh | 0:6b29f9c29a2a | 529 | void initDisplay() // initialises the LCD display |
el14moh | 0:6b29f9c29a2a | 530 | { |
el14moh | 0:6b29f9c29a2a | 531 | lcd.init(); |
el14moh | 0:6b29f9c29a2a | 532 | lcd.normalMode(); |
el14moh | 0:6b29f9c29a2a | 533 | lcd.setBrightness(1.0F-backlight); // brightness pot on PCB is soldered in the wrong direction, (1.0F-backlight) inverts the reading |
el14moh | 0:6b29f9c29a2a | 534 | } |
el14moh | 0:6b29f9c29a2a | 535 | |
el14moh | 0:6b29f9c29a2a | 536 | void calibrateJoystick() // read default positions of the joystick to calibrate later readings |
el14moh | 0:6b29f9c29a2a | 537 | { |
el14moh | 0:6b29f9c29a2a | 538 | button.mode(PullDown); |
el14moh | 0:6b29f9c29a2a | 539 | // must not move during calibration |
el14moh | 0:6b29f9c29a2a | 540 | joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) |
el14moh | 0:6b29f9c29a2a | 541 | joystick.y0 = yPot; |
el14moh | 0:6b29f9c29a2a | 542 | } |
el14moh | 0:6b29f9c29a2a | 543 | void updateJoystick() // reads direction the joystick has been moved |
el14moh | 0:6b29f9c29a2a | 544 | { |
el14moh | 0:6b29f9c29a2a | 545 | // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) |
el14moh | 0:6b29f9c29a2a | 546 | joystick.x = xPot - joystick.x0; |
el14moh | 0:6b29f9c29a2a | 547 | joystick.y = yPot - joystick.y0; |
el14moh | 0:6b29f9c29a2a | 548 | // read button state |
el14moh | 0:6b29f9c29a2a | 549 | joystick.button = button; |
el14moh | 0:6b29f9c29a2a | 550 | |
el14moh | 0:6b29f9c29a2a | 551 | // calculate direction depending on x,y values |
el14moh | 0:6b29f9c29a2a | 552 | // tolerance allows a little lee-way in case joystick not exactly in the stated direction |
el14moh | 0:6b29f9c29a2a | 553 | if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
el14moh | 0:6b29f9c29a2a | 554 | joystick.direction = CENTRE; |
el14moh | 0:6b29f9c29a2a | 555 | } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
el14moh | 0:6b29f9c29a2a | 556 | joystick.direction = UP; |
el14moh | 0:6b29f9c29a2a | 557 | } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
el14moh | 0:6b29f9c29a2a | 558 | joystick.direction = DOWN; |
el14moh | 0:6b29f9c29a2a | 559 | } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
el14moh | 0:6b29f9c29a2a | 560 | joystick.direction = LEFT; |
el14moh | 0:6b29f9c29a2a | 561 | } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
el14moh | 0:6b29f9c29a2a | 562 | joystick.direction = RIGHT; |
el14moh | 0:6b29f9c29a2a | 563 | } else if ( joystick.y > DIRECTION_TOLERANCE && joystick.x > DIRECTION_TOLERANCE) { |
el14moh | 0:6b29f9c29a2a | 564 | joystick.direction = UPLEFT; |
el14moh | 0:6b29f9c29a2a | 565 | } else if ( joystick.y > DIRECTION_TOLERANCE && joystick.x < DIRECTION_TOLERANCE) { |
el14moh | 0:6b29f9c29a2a | 566 | joystick.direction = UPRIGHT; |
el14moh | 0:6b29f9c29a2a | 567 | } else if ( joystick.x > DIRECTION_TOLERANCE && joystick.y < DIRECTION_TOLERANCE) { |
el14moh | 0:6b29f9c29a2a | 568 | joystick.direction = DOWNLEFT; |
el14moh | 0:6b29f9c29a2a | 569 | } else if ( joystick.x < DIRECTION_TOLERANCE && joystick.y < DIRECTION_TOLERANCE) { |
el14moh | 0:6b29f9c29a2a | 570 | joystick.direction = DOWNRIGHT; |
el14moh | 0:6b29f9c29a2a | 571 | } |
el14moh | 0:6b29f9c29a2a | 572 | |
el14moh | 0:6b29f9c29a2a | 573 | else { |
el14moh | 0:6b29f9c29a2a | 574 | joystick.direction = UNKNOWN; |
el14moh | 0:6b29f9c29a2a | 575 | } |
el14moh | 0:6b29f9c29a2a | 576 | |
el14moh | 0:6b29f9c29a2a | 577 | |
el14moh | 0:6b29f9c29a2a | 578 | printFlag = 1; // set flag for printing |
el14moh | 0:6b29f9c29a2a | 579 | } |
el14moh | 0:6b29f9c29a2a | 580 | |
el14moh | 0:6b29f9c29a2a | 581 | void debug() // prints for debug purposes |
el14moh | 0:6b29f9c29a2a | 582 | { |
el14moh | 0:6b29f9c29a2a | 583 | if (printFlag) { // if flag set, clear flag and print joystick values to serial port |
el14moh | 0:6b29f9c29a2a | 584 | printFlag = 0; |
el14moh | 0:6b29f9c29a2a | 585 | /* |
el14moh | 0:6b29f9c29a2a | 586 | if (joystick.direction == UP) |
el14moh | 0:6b29f9c29a2a | 587 | serial.printf(" UP\n"); |
el14moh | 0:6b29f9c29a2a | 588 | if (joystick.direction == DOWN) |
el14moh | 0:6b29f9c29a2a | 589 | serial.printf(" DOWN\n"); |
el14moh | 0:6b29f9c29a2a | 590 | if (joystick.direction == LEFT) |
el14moh | 0:6b29f9c29a2a | 591 | serial.printf(" LEFT\n"); |
el14moh | 0:6b29f9c29a2a | 592 | if (joystick.direction == RIGHT) |
el14moh | 0:6b29f9c29a2a | 593 | serial.printf(" RIGHT\n"); |
el14moh | 0:6b29f9c29a2a | 594 | if (joystick.direction == UPRIGHT) |
el14moh | 0:6b29f9c29a2a | 595 | serial.printf(" UPRIGHT\n"); // Diagonally up + right |
el14moh | 0:6b29f9c29a2a | 596 | if (joystick.direction == UPLEFT) |
el14moh | 0:6b29f9c29a2a | 597 | serial.printf(" UPLEFT\n"); // Diagonally up + left |
el14moh | 0:6b29f9c29a2a | 598 | if (joystick.direction == DOWNRIGHT) |
el14moh | 0:6b29f9c29a2a | 599 | serial.printf(" DOWNRIGHT\n"); // Diagonally down + right |
el14moh | 0:6b29f9c29a2a | 600 | if (joystick.direction == DOWNLEFT) |
el14moh | 0:6b29f9c29a2a | 601 | serial.printf(" DOWNLEFT\n"); // Diagonally down + left |
el14moh | 0:6b29f9c29a2a | 602 | if (joystick.direction == CENTRE) |
el14moh | 0:6b29f9c29a2a | 603 | serial.printf(" CENTRE\n"); |
el14moh | 0:6b29f9c29a2a | 604 | if (joystick.direction == UNKNOWN) |
el14moh | 0:6b29f9c29a2a | 605 | serial.printf(" Unsupported direction\n"); |
el14moh | 0:6b29f9c29a2a | 606 | */ |
el14moh | 0:6b29f9c29a2a | 607 | serial.printf("Left-wall Cooldown = %d \n",leftwall.cooldown); |
el14moh | 0:6b29f9c29a2a | 608 | serial.printf("Right-wall Cooldown = %d \n",rightwall.cooldown); |
el14moh | 0:6b29f9c29a2a | 609 | serial.printf("counter = %d \n",counter); |
el14moh | 0:6b29f9c29a2a | 610 | |
el14moh | 0:6b29f9c29a2a | 611 | } |
el14moh | 0:6b29f9c29a2a | 612 | } |
el14moh | 0:6b29f9c29a2a | 613 | |
el14moh | 0:6b29f9c29a2a | 614 | void init_serial() // sets baud rate for serial |
el14moh | 0:6b29f9c29a2a | 615 | { |
el14moh | 0:6b29f9c29a2a | 616 | // set to highest baud - ensure terminal software matches |
el14moh | 0:6b29f9c29a2a | 617 | serial.baud(115200); |
el14moh | 0:6b29f9c29a2a | 618 | } |
el14moh | 0:6b29f9c29a2a | 619 |