My ELEC2645 joystick project Tetris Game NAME: JIANWEI CHEN SID: 200879849

Dependencies:   N5110 SDFileSystem mbed

Committer:
cjw851102
Date:
Mon Apr 25 11:01:19 2016 +0000
Revision:
1:2a758565f691
Parent:
0:12a1972fa0d0
Child:
2:f427089e2bfa
left right finish

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cjw851102 0:12a1972fa0d0 1 #include "mbed.h"
cjw851102 0:12a1972fa0d0 2 #include "Patterns.h"
cjw851102 0:12a1972fa0d0 3 #include "N5110.h"
cjw851102 0:12a1972fa0d0 4
cjw851102 1:2a758565f691 5 #define DIRECTION_TOLERANCE 0.05
cjw851102 1:2a758565f691 6
cjw851102 0:12a1972fa0d0 7 //VCC, SCE, RST, D/C, MOSI,SCLK, LED
cjw851102 0:12a1972fa0d0 8 N5110 lcd(PTE26,PTA0,PTC4,PTD0,PTD2,PTD1,PTC3);
cjw851102 1:2a758565f691 9 AnalogIn yPot(PTB2);
cjw851102 1:2a758565f691 10 AnalogIn xPot(PTB3);
cjw851102 1:2a758565f691 11 DigitalIn button(PTB11);
cjw851102 0:12a1972fa0d0 12 Patterns patterns;
cjw851102 1:2a758565f691 13
cjw851102 1:2a758565f691 14 Ticker game;
cjw851102 1:2a758565f691 15 void game_isr();
cjw851102 1:2a758565f691 16 volatile int g_game_flag = 0;
cjw851102 1:2a758565f691 17
cjw851102 0:12a1972fa0d0 18 void drawPattern(int type,int rotation,int x,int y,int fill); // draw pattern at (x,y), fill = 0 white, fill = 1 black
cjw851102 0:12a1972fa0d0 19
cjw851102 0:12a1972fa0d0 20 void left_collisionDetect();
cjw851102 0:12a1972fa0d0 21 int left_collision_flag = 0;
cjw851102 0:12a1972fa0d0 22
cjw851102 0:12a1972fa0d0 23 void right_collisionDetect();
cjw851102 0:12a1972fa0d0 24 int right_collision_flag = 0;
cjw851102 0:12a1972fa0d0 25
cjw851102 1:2a758565f691 26 int left_boundary[6][6];
cjw851102 0:12a1972fa0d0 27
cjw851102 0:12a1972fa0d0 28 void bottom_collisionDetect();
cjw851102 0:12a1972fa0d0 29 int bottom_yMax = 47;
cjw851102 0:12a1972fa0d0 30 int bottom_collision_flag = 0;
cjw851102 0:12a1972fa0d0 31
cjw851102 0:12a1972fa0d0 32 struct Position {
cjw851102 0:12a1972fa0d0 33 float x;
cjw851102 0:12a1972fa0d0 34 float y;
cjw851102 0:12a1972fa0d0 35 int type;
cjw851102 0:12a1972fa0d0 36 int rotation;
cjw851102 0:12a1972fa0d0 37 };
cjw851102 0:12a1972fa0d0 38 typedef struct Position position;
cjw851102 0:12a1972fa0d0 39
cjw851102 1:2a758565f691 40 position pos;
cjw851102 0:12a1972fa0d0 41
cjw851102 0:12a1972fa0d0 42 void init_game();
cjw851102 1:2a758565f691 43 int typeArray[100];
cjw851102 1:2a758565f691 44 int rotationArray[100];
cjw851102 0:12a1972fa0d0 45
cjw851102 0:12a1972fa0d0 46 int pattern_buffer[6][6];
cjw851102 0:12a1972fa0d0 47 void get_pattern(int type, int rotatin);
cjw851102 0:12a1972fa0d0 48
cjw851102 0:12a1972fa0d0 49 void scan();
cjw851102 0:12a1972fa0d0 50 int buffer[84][48];
cjw851102 0:12a1972fa0d0 51
cjw851102 1:2a758565f691 52 // create enumerated type (0,1,2,3 etc. for direction)
cjw851102 1:2a758565f691 53 // could be extended for diagonals etc.
cjw851102 1:2a758565f691 54 enum DirectionName {
cjw851102 1:2a758565f691 55 UP,
cjw851102 1:2a758565f691 56 DOWN,
cjw851102 1:2a758565f691 57 LEFT,
cjw851102 1:2a758565f691 58 RIGHT,
cjw851102 1:2a758565f691 59 CENTRE,
cjw851102 1:2a758565f691 60 UNKNOWN
cjw851102 1:2a758565f691 61 };
cjw851102 1:2a758565f691 62 // struct for Joystick
cjw851102 1:2a758565f691 63 typedef struct JoyStick Joystick;
cjw851102 1:2a758565f691 64 struct JoyStick {
cjw851102 1:2a758565f691 65 float x; // current x value
cjw851102 1:2a758565f691 66 float x0; // 'centred' x value
cjw851102 1:2a758565f691 67 float y; // current y value
cjw851102 1:2a758565f691 68 float y0; // 'centred' y value
cjw851102 1:2a758565f691 69 int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
cjw851102 1:2a758565f691 70 DirectionName direction; // current direction
cjw851102 1:2a758565f691 71 };
cjw851102 1:2a758565f691 72 // create struct variable
cjw851102 1:2a758565f691 73 Joystick joystick;
cjw851102 1:2a758565f691 74 void calibrateJoystick();
cjw851102 1:2a758565f691 75 void updateJoystick();
cjw851102 1:2a758565f691 76
cjw851102 0:12a1972fa0d0 77 int main()
cjw851102 0:12a1972fa0d0 78 {
cjw851102 0:12a1972fa0d0 79 wait(2.0); // short delay for power to settle
cjw851102 0:12a1972fa0d0 80 lcd.init();
cjw851102 0:12a1972fa0d0 81 lcd.normalMode(); // normal colour mode
cjw851102 1:2a758565f691 82 lcd.setBrightness(1.0); // put LED backlight on 100%
cjw851102 0:12a1972fa0d0 83 init_game();
cjw851102 1:2a758565f691 84 game.attach(&game_isr,0.4);
cjw851102 1:2a758565f691 85 lcd.refresh();
cjw851102 0:12a1972fa0d0 86
cjw851102 1:2a758565f691 87 int xOld;
cjw851102 1:2a758565f691 88 calibrateJoystick();
cjw851102 0:12a1972fa0d0 89
cjw851102 1:2a758565f691 90 int typeCount = 10;
cjw851102 1:2a758565f691 91 int rotationCount = 5;
cjw851102 1:2a758565f691 92
cjw851102 0:12a1972fa0d0 93 while (1) {
cjw851102 0:12a1972fa0d0 94
cjw851102 1:2a758565f691 95 if(g_game_flag==1) {
cjw851102 1:2a758565f691 96 g_game_flag = 0;
cjw851102 1:2a758565f691 97 pos.type = typeArray[typeCount];
cjw851102 1:2a758565f691 98 pos.rotation = rotationArray[rotationCount];
cjw851102 1:2a758565f691 99 if (pos.y >= -6) { // clear previous pattern
cjw851102 1:2a758565f691 100 drawPattern(pos.type,pos.rotation,xOld,pos.y-1,0);
cjw851102 1:2a758565f691 101 }
cjw851102 1:2a758565f691 102 updateJoystick();
cjw851102 0:12a1972fa0d0 103
cjw851102 1:2a758565f691 104 switch(joystick.direction) {
cjw851102 1:2a758565f691 105 case UP:
cjw851102 1:2a758565f691 106 pos.x +=2;
cjw851102 1:2a758565f691 107 drawPattern(pos.type,pos.rotation,pos.x,pos.y,1);
cjw851102 1:2a758565f691 108 break;
cjw851102 1:2a758565f691 109 case DOWN:
cjw851102 1:2a758565f691 110 pos.y +=4;
cjw851102 1:2a758565f691 111 drawPattern(pos.type,pos.rotation,pos.x,pos.y,1);
cjw851102 1:2a758565f691 112 break;
cjw851102 1:2a758565f691 113 case RIGHT:
cjw851102 1:2a758565f691 114 right_collisionDetect();
cjw851102 1:2a758565f691 115 /*
cjw851102 1:2a758565f691 116 for (int i=0; i<=5; i++) {
cjw851102 1:2a758565f691 117 for (int j=0; j<=5; j++) {
cjw851102 1:2a758565f691 118 if(right_boundary[i][j] ==1) {
cjw851102 1:2a758565f691 119 lcd.setPixel(i,j);
cjw851102 1:2a758565f691 120 }
cjw851102 1:2a758565f691 121 }
cjw851102 1:2a758565f691 122 }
cjw851102 1:2a758565f691 123 */
cjw851102 1:2a758565f691 124 if( right_collision_flag == 0) {
cjw851102 1:2a758565f691 125 pos.x +=2;
cjw851102 1:2a758565f691 126 drawPattern(pos.type,pos.rotation,pos.x,pos.y,1);
cjw851102 1:2a758565f691 127 } else {
cjw851102 1:2a758565f691 128 drawPattern(pos.type,pos.rotation,pos.x,pos.y,1);
cjw851102 1:2a758565f691 129 }
cjw851102 1:2a758565f691 130 break;
cjw851102 1:2a758565f691 131 case LEFT:
cjw851102 1:2a758565f691 132 left_collisionDetect();
cjw851102 1:2a758565f691 133 if( left_collision_flag == 0) {
cjw851102 1:2a758565f691 134 pos.x -=2;
cjw851102 1:2a758565f691 135 drawPattern(pos.type,pos.rotation,pos.x,pos.y,1);
cjw851102 1:2a758565f691 136 // lcd.printString("0",42,5);
cjw851102 1:2a758565f691 137 } else {
cjw851102 1:2a758565f691 138 drawPattern(pos.type,pos.rotation,pos.x,pos.y,1);
cjw851102 1:2a758565f691 139 // lcd.printString("1",42,5);
cjw851102 1:2a758565f691 140 }
cjw851102 1:2a758565f691 141 break;
cjw851102 1:2a758565f691 142 case CENTRE:
cjw851102 1:2a758565f691 143 drawPattern(pos.type,pos.rotation,pos.x,pos.y,1);
cjw851102 1:2a758565f691 144 break;
cjw851102 1:2a758565f691 145 case UNKNOWN:
cjw851102 1:2a758565f691 146 drawPattern(pos.type,pos.rotation,pos.x,pos.y,1);
cjw851102 1:2a758565f691 147 break;
cjw851102 1:2a758565f691 148 }
cjw851102 1:2a758565f691 149
cjw851102 1:2a758565f691 150 xOld = pos.x;
cjw851102 1:2a758565f691 151 if (pos.y<42) {
cjw851102 1:2a758565f691 152 pos.y++;
cjw851102 1:2a758565f691 153 } else {
cjw851102 1:2a758565f691 154 pos.y=42;
cjw851102 1:2a758565f691 155 }
cjw851102 1:2a758565f691 156 }
cjw851102 1:2a758565f691 157
cjw851102 1:2a758565f691 158 lcd.refresh();
cjw851102 0:12a1972fa0d0 159 }
cjw851102 0:12a1972fa0d0 160
cjw851102 0:12a1972fa0d0 161 }
cjw851102 0:12a1972fa0d0 162
cjw851102 0:12a1972fa0d0 163 void init_game()
cjw851102 0:12a1972fa0d0 164 {
cjw851102 1:2a758565f691 165 pos.x = 10;
cjw851102 1:2a758565f691 166 pos.y = -6;
cjw851102 1:2a758565f691 167
cjw851102 1:2a758565f691 168 for(int i=0; i<=99; i++) {
cjw851102 1:2a758565f691 169 typeArray[i] = rand()%7;
cjw851102 1:2a758565f691 170 rotationArray[i] = rand()%5;
cjw851102 1:2a758565f691 171 }
cjw851102 1:2a758565f691 172
cjw851102 1:2a758565f691 173 lcd.drawLine(30,0,30,47,1);
cjw851102 1:2a758565f691 174 lcd.printString("Level:",42,0);
cjw851102 1:2a758565f691 175 lcd.printString("easy",42,1);
cjw851102 1:2a758565f691 176 lcd.printString("Score:",42,2);
cjw851102 1:2a758565f691 177 lcd.printString("100",42,3);
cjw851102 1:2a758565f691 178 lcd.printString("Next:",42,4);
cjw851102 1:2a758565f691 179
cjw851102 0:12a1972fa0d0 180 }
cjw851102 0:12a1972fa0d0 181
cjw851102 0:12a1972fa0d0 182 void get_pattern(int type, int rotation)
cjw851102 0:12a1972fa0d0 183 {
cjw851102 0:12a1972fa0d0 184 for(int i=0; i<=5; i++) {
cjw851102 0:12a1972fa0d0 185 for(int j=0; j<=5; j++) {
cjw851102 0:12a1972fa0d0 186 pattern_buffer[i][j] = patterns.getPatterns(type,rotation,j,i); // return pattern[type][rotation][y][x];
cjw851102 0:12a1972fa0d0 187 }
cjw851102 0:12a1972fa0d0 188 }
cjw851102 0:12a1972fa0d0 189 }
cjw851102 0:12a1972fa0d0 190
cjw851102 0:12a1972fa0d0 191 void scan()
cjw851102 0:12a1972fa0d0 192 {
cjw851102 0:12a1972fa0d0 193 for (int i=0; i<=83; i++) {
cjw851102 0:12a1972fa0d0 194 for (int j=0; j<=47; j++) {
cjw851102 0:12a1972fa0d0 195 if(lcd.getPixel(i,j)) {
cjw851102 0:12a1972fa0d0 196 buffer[i][j] = 1;
cjw851102 0:12a1972fa0d0 197 } else {
cjw851102 0:12a1972fa0d0 198 buffer[i][j] = 0;
cjw851102 0:12a1972fa0d0 199 }
cjw851102 0:12a1972fa0d0 200 }
cjw851102 0:12a1972fa0d0 201 }
cjw851102 0:12a1972fa0d0 202 }
cjw851102 0:12a1972fa0d0 203
cjw851102 0:12a1972fa0d0 204
cjw851102 0:12a1972fa0d0 205 void drawPattern(int type,int rotation,int x,int y,int fill)
cjw851102 0:12a1972fa0d0 206 {
cjw851102 0:12a1972fa0d0 207 get_pattern(type,rotation);
cjw851102 0:12a1972fa0d0 208 for(int i=x; i <= x+5; i++) { // (x,y) is the left top point of a 6*6 square
cjw851102 0:12a1972fa0d0 209 for(int j=y; j <= y+5; j++) {
cjw851102 1:2a758565f691 210 if (j>=0) {
cjw851102 1:2a758565f691 211 if(pattern_buffer[i-x][j-y]==1) {
cjw851102 1:2a758565f691 212 if (fill==0) {
cjw851102 1:2a758565f691 213 if (j<=47 && i>=0 && i <= 29) {
cjw851102 1:2a758565f691 214 lcd.clearPixel(i,j);
cjw851102 1:2a758565f691 215 }
cjw851102 1:2a758565f691 216 } else if (fill==1) {
cjw851102 1:2a758565f691 217 if (j<=47 && i>=0 && i <= 29) {
cjw851102 1:2a758565f691 218 lcd.setPixel(i,j);
cjw851102 1:2a758565f691 219 }
cjw851102 1:2a758565f691 220 }
cjw851102 0:12a1972fa0d0 221 }
cjw851102 0:12a1972fa0d0 222 }
cjw851102 0:12a1972fa0d0 223 }
cjw851102 0:12a1972fa0d0 224 }
cjw851102 0:12a1972fa0d0 225 }
cjw851102 0:12a1972fa0d0 226
cjw851102 0:12a1972fa0d0 227 void left_collisionDetect()
cjw851102 0:12a1972fa0d0 228 {
cjw851102 0:12a1972fa0d0 229 scan();
cjw851102 1:2a758565f691 230 get_pattern(pos.type,pos.rotation);
cjw851102 1:2a758565f691 231 // int left_boundary[6][6];
cjw851102 0:12a1972fa0d0 232 // get the left boundary pattern
cjw851102 1:2a758565f691 233 for (int j=0; j<=5; j++) { //
cjw851102 1:2a758565f691 234 for (int i=0; i<=5; i++) {
cjw851102 0:12a1972fa0d0 235 if (pattern_buffer[i][j]==1) {
cjw851102 0:12a1972fa0d0 236 left_boundary[i][j]=1;
cjw851102 1:2a758565f691 237 for(int k=i+1; k<=5; k++) {
cjw851102 0:12a1972fa0d0 238 left_boundary[k][j] = 0;
cjw851102 0:12a1972fa0d0 239 }
cjw851102 1:2a758565f691 240 break;
cjw851102 0:12a1972fa0d0 241 } else {
cjw851102 0:12a1972fa0d0 242 left_boundary[i][j]=0;
cjw851102 0:12a1972fa0d0 243 }
cjw851102 0:12a1972fa0d0 244 }
cjw851102 0:12a1972fa0d0 245 }
cjw851102 0:12a1972fa0d0 246
cjw851102 0:12a1972fa0d0 247 //check left collision
cjw851102 1:2a758565f691 248 int x = pos.x;
cjw851102 1:2a758565f691 249 int y = pos.y;
cjw851102 1:2a758565f691 250 if (x<0) {
cjw851102 1:2a758565f691 251 left_collision_flag = 1;
cjw851102 1:2a758565f691 252 } else {
cjw851102 1:2a758565f691 253 for(int i=x; i <= x+5; i++) { // (x,y) is the left top point of a 6*6 square
cjw851102 1:2a758565f691 254 for(int j=y; j <= y+5; j++) {
cjw851102 1:2a758565f691 255 if(left_boundary[i-x][j-y]==1) {
cjw851102 1:2a758565f691 256 if(i == 0) {
cjw851102 1:2a758565f691 257 left_collision_flag = 1;
cjw851102 1:2a758565f691 258 break;
cjw851102 1:2a758565f691 259 } else if (buffer[i-1][j]) {
cjw851102 1:2a758565f691 260 left_collision_flag = 1;
cjw851102 1:2a758565f691 261 break;
cjw851102 1:2a758565f691 262 } else {
cjw851102 1:2a758565f691 263 left_collision_flag = 0;
cjw851102 0:12a1972fa0d0 264 }
cjw851102 0:12a1972fa0d0 265 }
cjw851102 1:2a758565f691 266 if (left_collision_flag == 1) {
cjw851102 1:2a758565f691 267 break;
cjw851102 1:2a758565f691 268 }
cjw851102 0:12a1972fa0d0 269 }
cjw851102 0:12a1972fa0d0 270 }
cjw851102 0:12a1972fa0d0 271 }
cjw851102 0:12a1972fa0d0 272 }
cjw851102 0:12a1972fa0d0 273
cjw851102 1:2a758565f691 274
cjw851102 0:12a1972fa0d0 275 void right_collisionDetect()
cjw851102 0:12a1972fa0d0 276 {
cjw851102 0:12a1972fa0d0 277 scan();
cjw851102 0:12a1972fa0d0 278 int right_boundary[6][6];
cjw851102 1:2a758565f691 279 get_pattern(pos.type,pos.rotation);
cjw851102 0:12a1972fa0d0 280 // get the left boundary pattern
cjw851102 1:2a758565f691 281 for (int j=0; j<=5; j++) {
cjw851102 1:2a758565f691 282 for (int i=5; i>=0; i--) {
cjw851102 0:12a1972fa0d0 283 if (pattern_buffer[i][j]==1) {
cjw851102 0:12a1972fa0d0 284 right_boundary[i][j]=1;
cjw851102 0:12a1972fa0d0 285 for(int k=i-1; k>=0; k--) {
cjw851102 0:12a1972fa0d0 286 right_boundary[k][j] = 0;
cjw851102 0:12a1972fa0d0 287 }
cjw851102 1:2a758565f691 288 break;
cjw851102 0:12a1972fa0d0 289 } else {
cjw851102 0:12a1972fa0d0 290 right_boundary[i][j]=0;
cjw851102 0:12a1972fa0d0 291 }
cjw851102 0:12a1972fa0d0 292 }
cjw851102 0:12a1972fa0d0 293 }
cjw851102 0:12a1972fa0d0 294
cjw851102 0:12a1972fa0d0 295 //check left collision
cjw851102 1:2a758565f691 296 int x = pos.x;
cjw851102 1:2a758565f691 297 int y = pos.y;
cjw851102 0:12a1972fa0d0 298 for(int i = x; i <= x+5; i++) { // (x,y) is the left top point of a 6*6 square
cjw851102 0:12a1972fa0d0 299 for(int j=y; j <= y+5; j++) {
cjw851102 0:12a1972fa0d0 300 if(right_boundary[i-x][j-y]==1) {
cjw851102 1:2a758565f691 301 if(j>=0) {
cjw851102 1:2a758565f691 302 if(i >= 29) {
cjw851102 1:2a758565f691 303 right_collision_flag = 1;
cjw851102 1:2a758565f691 304 break;
cjw851102 1:2a758565f691 305 } else if (buffer[i+1][j] == 1) {
cjw851102 1:2a758565f691 306 right_collision_flag = 1;
cjw851102 1:2a758565f691 307 break;
cjw851102 1:2a758565f691 308 } else {
cjw851102 1:2a758565f691 309 right_collision_flag = 0;
cjw851102 0:12a1972fa0d0 310 }
cjw851102 0:12a1972fa0d0 311 }
cjw851102 0:12a1972fa0d0 312 }
cjw851102 1:2a758565f691 313 if (right_collision_flag == 1) {
cjw851102 1:2a758565f691 314 break;
cjw851102 1:2a758565f691 315 }
cjw851102 0:12a1972fa0d0 316 }
cjw851102 0:12a1972fa0d0 317
cjw851102 0:12a1972fa0d0 318 }
cjw851102 0:12a1972fa0d0 319 }
cjw851102 0:12a1972fa0d0 320
cjw851102 0:12a1972fa0d0 321 void bottom_collisionDetect()
cjw851102 0:12a1972fa0d0 322 {
cjw851102 0:12a1972fa0d0 323 scan();
cjw851102 1:2a758565f691 324 get_pattern(pos.type,pos.rotation);
cjw851102 0:12a1972fa0d0 325 int bot_boundary[6][6];
cjw851102 0:12a1972fa0d0 326 // get the left boundary pattern
cjw851102 1:2a758565f691 327 for (int i=0; i<=5; i++) {
cjw851102 1:2a758565f691 328 for (int j=5; j>=0; j--) {
cjw851102 0:12a1972fa0d0 329 if (pattern_buffer[i][j]==1) {
cjw851102 0:12a1972fa0d0 330 bot_boundary[i][j]=1;
cjw851102 0:12a1972fa0d0 331 for(int k=i-1; k>=0; k--) {
cjw851102 0:12a1972fa0d0 332 bot_boundary[i][k] = 0;
cjw851102 0:12a1972fa0d0 333 }
cjw851102 0:12a1972fa0d0 334 } else {
cjw851102 0:12a1972fa0d0 335 bot_boundary[i][j]=0;
cjw851102 0:12a1972fa0d0 336 }
cjw851102 0:12a1972fa0d0 337 }
cjw851102 0:12a1972fa0d0 338 }
cjw851102 0:12a1972fa0d0 339
cjw851102 0:12a1972fa0d0 340 //check left collision
cjw851102 1:2a758565f691 341 int x = pos.x;
cjw851102 1:2a758565f691 342 int y = pos.y;
cjw851102 0:12a1972fa0d0 343 for(int i = x; i <= x+5; i++) { // (x,y) is the left top point of a 6*6 square
cjw851102 0:12a1972fa0d0 344 for(int j=y; j <= y+5; j++) {
cjw851102 0:12a1972fa0d0 345 if(bot_boundary[i-x][j-y]==1) {
cjw851102 0:12a1972fa0d0 346 if(j == 47) {
cjw851102 0:12a1972fa0d0 347 bottom_collision_flag = 1;
cjw851102 0:12a1972fa0d0 348 bottom_yMax = 47;
cjw851102 0:12a1972fa0d0 349 } else if (buffer[i][j+1] == 1) {
cjw851102 0:12a1972fa0d0 350 right_collision_flag = 1;
cjw851102 0:12a1972fa0d0 351 if(j< bottom_yMax) {
cjw851102 0:12a1972fa0d0 352 bottom_yMax = j;
cjw851102 0:12a1972fa0d0 353 }
cjw851102 1:2a758565f691 354 } else {
cjw851102 1:2a758565f691 355 right_collision_flag = 0;
cjw851102 0:12a1972fa0d0 356 }
cjw851102 0:12a1972fa0d0 357 }
cjw851102 0:12a1972fa0d0 358 }
cjw851102 0:12a1972fa0d0 359
cjw851102 0:12a1972fa0d0 360 }
cjw851102 1:2a758565f691 361 }
cjw851102 1:2a758565f691 362
cjw851102 1:2a758565f691 363 void game_isr()
cjw851102 1:2a758565f691 364 {
cjw851102 1:2a758565f691 365 g_game_flag = 1;
cjw851102 1:2a758565f691 366 }
cjw851102 1:2a758565f691 367
cjw851102 1:2a758565f691 368 // read default positions of the joystick to calibrate later readings
cjw851102 1:2a758565f691 369 void calibrateJoystick()
cjw851102 1:2a758565f691 370 {
cjw851102 1:2a758565f691 371 button.mode(PullDown);
cjw851102 1:2a758565f691 372 // must not move during calibration
cjw851102 1:2a758565f691 373 joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
cjw851102 1:2a758565f691 374 joystick.y0 = yPot;
cjw851102 1:2a758565f691 375 }
cjw851102 1:2a758565f691 376
cjw851102 1:2a758565f691 377 void updateJoystick()
cjw851102 1:2a758565f691 378 {
cjw851102 1:2a758565f691 379 // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
cjw851102 1:2a758565f691 380 joystick.x = xPot - joystick.x0;
cjw851102 1:2a758565f691 381 joystick.y = yPot - joystick.y0;
cjw851102 1:2a758565f691 382 // read button state
cjw851102 1:2a758565f691 383 joystick.button = button;
cjw851102 1:2a758565f691 384
cjw851102 1:2a758565f691 385 // calculate direction depending on x,y values
cjw851102 1:2a758565f691 386 // tolerance allows a little lee-way in case joystick not exactly in the stated direction
cjw851102 1:2a758565f691 387 if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
cjw851102 1:2a758565f691 388 joystick.direction = CENTRE;
cjw851102 1:2a758565f691 389 } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
cjw851102 1:2a758565f691 390 joystick.direction = UP;
cjw851102 1:2a758565f691 391 } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
cjw851102 1:2a758565f691 392 joystick.direction = DOWN;
cjw851102 1:2a758565f691 393 } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
cjw851102 1:2a758565f691 394 joystick.direction = RIGHT;
cjw851102 1:2a758565f691 395 } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
cjw851102 1:2a758565f691 396 joystick.direction = LEFT;
cjw851102 1:2a758565f691 397 } else {
cjw851102 1:2a758565f691 398 joystick.direction = UNKNOWN;
cjw851102 1:2a758565f691 399 }
cjw851102 1:2a758565f691 400
cjw851102 0:12a1972fa0d0 401 }