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.cpp@2:d7b17623ba26, 2015-04-30 (annotated)
- Committer:
- el14rd
- Date:
- Thu Apr 30 15:54:35 2015 +0000
- Revision:
- 2:d7b17623ba26
- Parent:
- 1:f682aeb462f1
- Child:
- 3:04ff8cd27dd1
snake move version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 0:026fa541af7a | 1 | #include "mbed.h" |
el14rd | 1:f682aeb462f1 | 2 | #include "N5110.h" |
eencae | 0:026fa541af7a | 3 | #define DIRECTION_TOLERANCE 0.05 |
eencae | 0:026fa541af7a | 4 | |
el14rd | 1:f682aeb462f1 | 5 | |
el14rd | 1:f682aeb462f1 | 6 | //pins in |
el14rd | 1:f682aeb462f1 | 7 | N5110 lcd(p7,p8,p9,p10,p11,p13,p26); |
el14rd | 1:f682aeb462f1 | 8 | |
eencae | 0:026fa541af7a | 9 | // connections for joystick |
el14rd | 1:f682aeb462f1 | 10 | DigitalIn button(p19); |
el14rd | 1:f682aeb462f1 | 11 | AnalogIn xPot(p15); |
el14rd | 1:f682aeb462f1 | 12 | AnalogIn yPot(p16); |
el14rd | 2:d7b17623ba26 | 13 | //InterruptIn button(p19); |
el14rd | 1:f682aeb462f1 | 14 | int i; |
el14rd | 1:f682aeb462f1 | 15 | int j; |
el14rd | 2:d7b17623ba26 | 16 | int X; |
el14rd | 2:d7b17623ba26 | 17 | int Y; |
el14rd | 1:f682aeb462f1 | 18 | |
el14rd | 1:f682aeb462f1 | 19 | int snakeX[100]; |
el14rd | 1:f682aeb462f1 | 20 | int snakeY[100]; |
eencae | 0:026fa541af7a | 21 | |
eencae | 0:026fa541af7a | 22 | // timer to regularly read the joystick |
eencae | 0:026fa541af7a | 23 | Ticker pollJoystick; |
eencae | 0:026fa541af7a | 24 | // Serial for debug |
eencae | 0:026fa541af7a | 25 | Serial serial(USBTX,USBRX); |
eencae | 0:026fa541af7a | 26 | |
eencae | 0:026fa541af7a | 27 | // create enumerated type (0,1,2,3 etc. for direction) |
eencae | 0:026fa541af7a | 28 | // could be extended for diagonals etc. |
eencae | 0:026fa541af7a | 29 | enum DirectionName { |
eencae | 0:026fa541af7a | 30 | UP, |
eencae | 0:026fa541af7a | 31 | DOWN, |
eencae | 0:026fa541af7a | 32 | LEFT, |
eencae | 0:026fa541af7a | 33 | RIGHT, |
eencae | 0:026fa541af7a | 34 | CENTRE, |
eencae | 0:026fa541af7a | 35 | UNKNOWN |
eencae | 0:026fa541af7a | 36 | }; |
eencae | 0:026fa541af7a | 37 | |
eencae | 0:026fa541af7a | 38 | // struct for Joystick |
eencae | 0:026fa541af7a | 39 | typedef struct JoyStick Joystick; |
eencae | 0:026fa541af7a | 40 | struct JoyStick { |
eencae | 0:026fa541af7a | 41 | float x; // current x value |
eencae | 0:026fa541af7a | 42 | float x0; // 'centred' x value |
eencae | 0:026fa541af7a | 43 | float y; // current y value |
eencae | 0:026fa541af7a | 44 | float y0; // 'centred' y value |
eencae | 0:026fa541af7a | 45 | int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed) |
eencae | 0:026fa541af7a | 46 | DirectionName direction; // current direction |
eencae | 0:026fa541af7a | 47 | }; |
eencae | 0:026fa541af7a | 48 | // create struct variable |
eencae | 0:026fa541af7a | 49 | Joystick joystick; |
eencae | 0:026fa541af7a | 50 | |
eencae | 0:026fa541af7a | 51 | int printFlag = 0; |
el14rd | 2:d7b17623ba26 | 52 | //int buttonFlag = 0; |
eencae | 0:026fa541af7a | 53 | // function prototypes |
el14rd | 1:f682aeb462f1 | 54 | //void setPixel(int x, int y); |
el14rd | 1:f682aeb462f1 | 55 | //void clearPixel(int x, int y); |
el14rd | 1:f682aeb462f1 | 56 | |
eencae | 0:026fa541af7a | 57 | |
el14rd | 1:f682aeb462f1 | 58 | typedef struct Nut Nut; |
el14rd | 1:f682aeb462f1 | 59 | struct Nut { |
el14rd | 1:f682aeb462f1 | 60 | int X; |
el14rd | 1:f682aeb462f1 | 61 | int Y; |
el14rd | 1:f682aeb462f1 | 62 | int yes; |
el14rd | 1:f682aeb462f1 | 63 | }; |
el14rd | 1:f682aeb462f1 | 64 | Nut nut; |
eencae | 0:026fa541af7a | 65 | |
el14rd | 1:f682aeb462f1 | 66 | typedef struct Snake Snake; |
el14rd | 1:f682aeb462f1 | 67 | struct Snake { |
el14rd | 1:f682aeb462f1 | 68 | int snakeX[100]; |
el14rd | 1:f682aeb462f1 | 69 | int snakeY[100]; |
el14rd | 1:f682aeb462f1 | 70 | int node; |
el14rd | 1:f682aeb462f1 | 71 | int life; |
el14rd | 1:f682aeb462f1 | 72 | DirectionName direction; |
el14rd | 1:f682aeb462f1 | 73 | }; |
el14rd | 1:f682aeb462f1 | 74 | Snake snake; |
eencae | 0:026fa541af7a | 75 | |
el14rd | 1:f682aeb462f1 | 76 | DirectionName snakeDirection; |
el14rd | 1:f682aeb462f1 | 77 | |
el14rd | 2:d7b17623ba26 | 78 | void initNut() |
el14rd | 2:d7b17623ba26 | 79 | { |
el14rd | 2:d7b17623ba26 | 80 | nut.X=40; |
el14rd | 2:d7b17623ba26 | 81 | nut.Y=20; |
el14rd | 2:d7b17623ba26 | 82 | nut.yes=0; |
el14rd | 2:d7b17623ba26 | 83 | } |
el14rd | 2:d7b17623ba26 | 84 | void drawNut() |
el14rd | 2:d7b17623ba26 | 85 | { |
el14rd | 2:d7b17623ba26 | 86 | int X = rand()%84; |
el14rd | 2:d7b17623ba26 | 87 | int Y = rand()%48; |
el14rd | 2:d7b17623ba26 | 88 | lcd.drawCircle(X,Y,1,1); |
el14rd | 2:d7b17623ba26 | 89 | } |
eencae | 0:026fa541af7a | 90 | |
el14rd | 1:f682aeb462f1 | 91 | void moveSnake() |
el14rd | 1:f682aeb462f1 | 92 | { |
el14rd | 2:d7b17623ba26 | 93 | snake.snakeX[0]+=1; |
el14rd | 2:d7b17623ba26 | 94 | wait(0.2); |
el14rd | 2:d7b17623ba26 | 95 | |
el14rd | 2:d7b17623ba26 | 96 | /* |
el14rd | 1:f682aeb462f1 | 97 | if(joystick.direction != UNKNOWN&&joystick.direction != CENTRE) { |
el14rd | 2:d7b17623ba26 | 98 | |
el14rd | 1:f682aeb462f1 | 99 | if(joystick.direction == RIGHT&&snake.direction!=LEFT) { |
el14rd | 2:d7b17623ba26 | 100 | snake.direction= RIGHT; |
el14rd | 2:d7b17623ba26 | 101 | snake.snakeX[0]+=1; |
el14rd | 1:f682aeb462f1 | 102 | |
el14rd | 1:f682aeb462f1 | 103 | } else if(joystick.direction == LEFT&&snake.direction!=RIGHT) { |
el14rd | 2:d7b17623ba26 | 104 | snake.direction=LEFT; |
el14rd | 2:d7b17623ba26 | 105 | snake.snakeX[0]-=1; |
el14rd | 1:f682aeb462f1 | 106 | } else if(joystick.direction == UP&&snake.direction!=DOWN) { |
el14rd | 2:d7b17623ba26 | 107 | snake.direction=DOWN; |
el14rd | 1:f682aeb462f1 | 108 | snake.snakeY[0]-=1; |
el14rd | 1:f682aeb462f1 | 109 | } else if(joystick.direction == DOWN&&snake.direction!=UP) { |
el14rd | 2:d7b17623ba26 | 110 | snake.direction=UP; |
el14rd | 1:f682aeb462f1 | 111 | snake.snakeY[0]+=1; |
eencae | 0:026fa541af7a | 112 | } |
eencae | 0:026fa541af7a | 113 | |
eencae | 0:026fa541af7a | 114 | } |
el14rd | 2:d7b17623ba26 | 115 | */ |
el14rd | 2:d7b17623ba26 | 116 | } |
el14rd | 2:d7b17623ba26 | 117 | |
el14rd | 2:d7b17623ba26 | 118 | void initSnake() |
el14rd | 2:d7b17623ba26 | 119 | { |
el14rd | 2:d7b17623ba26 | 120 | |
el14rd | 2:d7b17623ba26 | 121 | snake.snakeX[0] = 10; //coordinate of head |
el14rd | 2:d7b17623ba26 | 122 | snake.snakeY[0] = 10; |
el14rd | 2:d7b17623ba26 | 123 | snake.snakeX[1] = 12; |
el14rd | 2:d7b17623ba26 | 124 | snake.snakeY[1] = 10; |
el14rd | 2:d7b17623ba26 | 125 | snake.snakeX[2] = 14; |
el14rd | 2:d7b17623ba26 | 126 | snake.snakeY[2] = 10; |
el14rd | 2:d7b17623ba26 | 127 | snake.snakeX[3] = 16; |
el14rd | 2:d7b17623ba26 | 128 | snake.snakeY[3] = 10; |
el14rd | 2:d7b17623ba26 | 129 | snake.node = 4; //node of snake |
el14rd | 2:d7b17623ba26 | 130 | |
eencae | 0:026fa541af7a | 131 | } |
eencae | 0:026fa541af7a | 132 | |
el14rd | 1:f682aeb462f1 | 133 | void drawSnake() |
el14rd | 1:f682aeb462f1 | 134 | { |
el14rd | 1:f682aeb462f1 | 135 | |
el14rd | 2:d7b17623ba26 | 136 | for(i=0; i<snake.node; i++) { |
el14rd | 1:f682aeb462f1 | 137 | |
el14rd | 2:d7b17623ba26 | 138 | lcd.drawRect(snake.snakeX[i],snake.snakeY[i],1,1,1); |
el14rd | 2:d7b17623ba26 | 139 | //lcd.setPixel(snake.snakeX[i],snake.snakeY[i]); |
el14rd | 1:f682aeb462f1 | 140 | } |
el14rd | 1:f682aeb462f1 | 141 | } |
el14rd | 1:f682aeb462f1 | 142 | |
el14rd | 1:f682aeb462f1 | 143 | /* |
el14rd | 1:f682aeb462f1 | 144 | */ |
el14rd | 1:f682aeb462f1 | 145 | |
el14rd | 1:f682aeb462f1 | 146 | void startGmae() |
el14rd | 1:f682aeb462f1 | 147 | { |
el14rd | 2:d7b17623ba26 | 148 | //lcd.drawRect(snake.snakeX[0]+10,snake.snakeY[0]+10,10,10,1); |
el14rd | 2:d7b17623ba26 | 149 | nut.yes = 0; |
el14rd | 1:f682aeb462f1 | 150 | snake.life = 0; |
el14rd | 2:d7b17623ba26 | 151 | //snake.direction=LEFT; |
el14rd | 1:f682aeb462f1 | 152 | |
el14rd | 1:f682aeb462f1 | 153 | //if(joystick.direction == UNKNOWN) |
el14rd | 1:f682aeb462f1 | 154 | //{ |
el14rd | 1:f682aeb462f1 | 155 | |
el14rd | 2:d7b17623ba26 | 156 | if(nut.yes==0) { //set nut randomly |
el14rd | 1:f682aeb462f1 | 157 | nut.X = rand()%83; |
el14rd | 1:f682aeb462f1 | 158 | nut.Y = rand()%47; |
el14rd | 2:d7b17623ba26 | 159 | nut.yes = 1; |
el14rd | 2:d7b17623ba26 | 160 | drawNut(); |
el14rd | 1:f682aeb462f1 | 161 | } |
el14rd | 2:d7b17623ba26 | 162 | |
el14rd | 1:f682aeb462f1 | 163 | |
el14rd | 1:f682aeb462f1 | 164 | for(i=3; i<snake.node; i++) { //if snake collide itself snake die |
el14rd | 1:f682aeb462f1 | 165 | if(snake.snakeX[i]==snake.snakeX[0] && snake.snakeY[i]==snake.snakeY[0]) { |
el14rd | 1:f682aeb462f1 | 166 | snake.life=1; |
el14rd | 1:f682aeb462f1 | 167 | // break; |
el14rd | 1:f682aeb462f1 | 168 | } |
el14rd | 1:f682aeb462f1 | 169 | } |
el14rd | 1:f682aeb462f1 | 170 | |
el14rd | 1:f682aeb462f1 | 171 | for(i=snake.node-1; i>0; i--) { //move snake |
el14rd | 1:f682aeb462f1 | 172 | snake.snakeX[i]=snake.snakeX[i-1]; |
el14rd | 1:f682aeb462f1 | 173 | snake.snakeY[i]=snake.snakeY[i-1]; |
el14rd | 1:f682aeb462f1 | 174 | } |
el14rd | 1:f682aeb462f1 | 175 | |
el14rd | 1:f682aeb462f1 | 176 | if(snake.snakeX[0]<1||snake.snakeX[0]>83||snake.snakeY[0]<1||snake.snakeY[0]>47) { |
el14rd | 1:f682aeb462f1 | 177 | snake.life=1; // snake collide the corrider snake die |
el14rd | 1:f682aeb462f1 | 178 | //break; |
el14rd | 1:f682aeb462f1 | 179 | } |
el14rd | 1:f682aeb462f1 | 180 | |
el14rd | 1:f682aeb462f1 | 181 | |
el14rd | 1:f682aeb462f1 | 182 | if(snake.life==1) { |
el14rd | 1:f682aeb462f1 | 183 | //break; |
el14rd | 1:f682aeb462f1 | 184 | } |
el14rd | 1:f682aeb462f1 | 185 | if(snake.snakeX[0]==nut.X && snake.snakeY[0]==nut.Y) { // if snake got nut |
el14rd | 1:f682aeb462f1 | 186 | snake.node++; |
el14rd | 1:f682aeb462f1 | 187 | nut.yes=1; // new nut appear |
el14rd | 1:f682aeb462f1 | 188 | } |
el14rd | 1:f682aeb462f1 | 189 | //} |
el14rd | 1:f682aeb462f1 | 190 | |
el14rd | 1:f682aeb462f1 | 191 | } |
el14rd | 1:f682aeb462f1 | 192 | |
el14rd | 1:f682aeb462f1 | 193 | |
el14rd | 1:f682aeb462f1 | 194 | //while(joystick.direction == UNKNOWN) { |
el14rd | 1:f682aeb462f1 | 195 | |
el14rd | 1:f682aeb462f1 | 196 | |
el14rd | 1:f682aeb462f1 | 197 | //switch(snake.direction) |
el14rd | 1:f682aeb462f1 | 198 | //{ |
el14rd | 1:f682aeb462f1 | 199 | // case RIGHT:snake.X[0]+=1; |
el14rd | 1:f682aeb462f1 | 200 | // break; |
el14rd | 1:f682aeb462f1 | 201 | // case LEFT:snake.X[0]-=1; |
el14rd | 1:f682aeb462f1 | 202 | // break; |
el14rd | 1:f682aeb462f1 | 203 | // case UP:snake.Y[0]-=1; |
el14rd | 1:f682aeb462f1 | 204 | // break; |
el14rd | 1:f682aeb462f1 | 205 | // case DOWN:snake.Y[0]+=1; |
el14rd | 1:f682aeb462f1 | 206 | // break; |
el14rd | 1:f682aeb462f1 | 207 | // } |
el14rd | 1:f682aeb462f1 | 208 | |
el14rd | 1:f682aeb462f1 | 209 | //end of while(joystick.direction = UNKNOWN) |
el14rd | 1:f682aeb462f1 | 210 | |
el14rd | 1:f682aeb462f1 | 211 | |
el14rd | 1:f682aeb462f1 | 212 | // if(joystick.direction == UP&&snake.direction!=DOWN) { |
el14rd | 1:f682aeb462f1 | 213 | // snake.direction=UP; |
el14rd | 1:f682aeb462f1 | 214 | // } else if(joystick.direction == RIGHT&&snake.direction!=LEFT) { |
el14rd | 1:f682aeb462f1 | 215 | // snake.direction= RIGHT; |
el14rd | 1:f682aeb462f1 | 216 | // } else if(joystick.direction == LEFT&&snake.direction!=RIGHT) { |
el14rd | 1:f682aeb462f1 | 217 | // snake.direction=LEFT; |
el14rd | 1:f682aeb462f1 | 218 | // } else(joystick.direction == DOWN&&snake.direction!=UP) { |
el14rd | 1:f682aeb462f1 | 219 | // snake.direction=DOWN; |
el14rd | 1:f682aeb462f1 | 220 | // } |
el14rd | 1:f682aeb462f1 | 221 | |
el14rd | 1:f682aeb462f1 | 222 | |
el14rd | 1:f682aeb462f1 | 223 | |
eencae | 0:026fa541af7a | 224 | // read default positions of the joystick to calibrate later readings |
eencae | 0:026fa541af7a | 225 | void calibrateJoystick() |
eencae | 0:026fa541af7a | 226 | { |
eencae | 0:026fa541af7a | 227 | button.mode(PullDown); |
eencae | 0:026fa541af7a | 228 | // must not move during calibration |
eencae | 0:026fa541af7a | 229 | joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) |
eencae | 0:026fa541af7a | 230 | joystick.y0 = yPot; |
eencae | 0:026fa541af7a | 231 | } |
el14rd | 1:f682aeb462f1 | 232 | |
el14rd | 2:d7b17623ba26 | 233 | |
eencae | 0:026fa541af7a | 234 | void updateJoystick() |
eencae | 0:026fa541af7a | 235 | { |
eencae | 0:026fa541af7a | 236 | // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) |
eencae | 0:026fa541af7a | 237 | joystick.x = xPot - joystick.x0; |
eencae | 0:026fa541af7a | 238 | joystick.y = yPot - joystick.y0; |
eencae | 0:026fa541af7a | 239 | // read button state |
eencae | 0:026fa541af7a | 240 | joystick.button = button; |
eencae | 0:026fa541af7a | 241 | |
eencae | 0:026fa541af7a | 242 | // calculate direction depending on x,y values |
eencae | 0:026fa541af7a | 243 | // tolerance allows a little lee-way in case joystick not exactly in the stated direction |
eencae | 0:026fa541af7a | 244 | if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 245 | joystick.direction = CENTRE; |
eencae | 0:026fa541af7a | 246 | } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 247 | joystick.direction = UP; |
el14rd | 1:f682aeb462f1 | 248 | //snake.direction = UP; |
eencae | 0:026fa541af7a | 249 | } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 250 | joystick.direction = DOWN; |
eencae | 0:026fa541af7a | 251 | } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 252 | joystick.direction = RIGHT; |
eencae | 0:026fa541af7a | 253 | } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 254 | joystick.direction = LEFT; |
eencae | 0:026fa541af7a | 255 | } else { |
eencae | 0:026fa541af7a | 256 | joystick.direction = UNKNOWN; |
eencae | 0:026fa541af7a | 257 | } |
eencae | 0:026fa541af7a | 258 | // set flag for printing |
eencae | 0:026fa541af7a | 259 | printFlag = 1; |
el14rd | 1:f682aeb462f1 | 260 | } |
el14rd | 1:f682aeb462f1 | 261 | |
el14rd | 1:f682aeb462f1 | 262 | void updateSnake() |
el14rd | 1:f682aeb462f1 | 263 | { |
el14rd | 1:f682aeb462f1 | 264 | // get direction of joystick and update snake |
el14rd | 1:f682aeb462f1 | 265 | if (joystick.direction == UP) { |
el14rd | 2:d7b17623ba26 | 266 | snake.snakeX[0]--; |
el14rd | 1:f682aeb462f1 | 267 | if(snake.snakeY[0]<1) { |
el14rd | 1:f682aeb462f1 | 268 | snake.snakeY[0]=1; |
el14rd | 1:f682aeb462f1 | 269 | }//will stop at the top edge |
el14rd | 1:f682aeb462f1 | 270 | |
el14rd | 1:f682aeb462f1 | 271 | } |
el14rd | 1:f682aeb462f1 | 272 | if (joystick.direction == DOWN) { |
el14rd | 2:d7b17623ba26 | 273 | snake.snakeX[0]++; |
el14rd | 1:f682aeb462f1 | 274 | if(snake.snakeY[0]>43) { |
el14rd | 1:f682aeb462f1 | 275 | snake.snakeY[0]=43; |
el14rd | 1:f682aeb462f1 | 276 | }//will stop at the bottom edge |
el14rd | 1:f682aeb462f1 | 277 | } |
el14rd | 1:f682aeb462f1 | 278 | if (joystick.direction == LEFT) { |
el14rd | 2:d7b17623ba26 | 279 | snake.snakeY[0]--; |
el14rd | 1:f682aeb462f1 | 280 | if(snake.snakeX[0]<1) { |
el14rd | 1:f682aeb462f1 | 281 | snake.snakeX[0]=1; |
el14rd | 1:f682aeb462f1 | 282 | }//will stop at the left edge |
el14rd | 1:f682aeb462f1 | 283 | |
el14rd | 1:f682aeb462f1 | 284 | } |
el14rd | 1:f682aeb462f1 | 285 | if (joystick.direction == RIGHT) { |
el14rd | 2:d7b17623ba26 | 286 | snake.snakeY[0]++; |
el14rd | 1:f682aeb462f1 | 287 | if(snake.snakeX[0]>87) { |
el14rd | 1:f682aeb462f1 | 288 | snake.snakeX[0]=87; |
el14rd | 1:f682aeb462f1 | 289 | }//will stop at the right edge |
el14rd | 1:f682aeb462f1 | 290 | |
el14rd | 1:f682aeb462f1 | 291 | } |
el14rd | 1:f682aeb462f1 | 292 | |
el14rd | 1:f682aeb462f1 | 293 | } |
el14rd | 1:f682aeb462f1 | 294 | |
el14rd | 1:f682aeb462f1 | 295 | |
el14rd | 2:d7b17623ba26 | 296 | |
el14rd | 1:f682aeb462f1 | 297 | //main code |
el14rd | 1:f682aeb462f1 | 298 | int main() |
el14rd | 1:f682aeb462f1 | 299 | { |
el14rd | 1:f682aeb462f1 | 300 | lcd.init(); |
el14rd | 1:f682aeb462f1 | 301 | lcd.setBrightness(0.5); // put LED backlight on 50% |
el14rd | 1:f682aeb462f1 | 302 | lcd.printString("press to start",0,0); |
el14rd | 1:f682aeb462f1 | 303 | calibrateJoystick(); // get centred values of joystick |
el14rd | 1:f682aeb462f1 | 304 | pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second |
el14rd | 1:f682aeb462f1 | 305 | lcd.refresh(); |
el14rd | 2:d7b17623ba26 | 306 | initNut(); |
el14rd | 2:d7b17623ba26 | 307 | initSnake(); |
el14rd | 1:f682aeb462f1 | 308 | |
el14rd | 1:f682aeb462f1 | 309 | //for(i=20,i<83,i++){ |
el14rd | 1:f682aeb462f1 | 310 | // i=i; |
el14rd | 1:f682aeb462f1 | 311 | // j=j; |
el14rd | 1:f682aeb462f1 | 312 | // wait(0.5); |
el14rd | 1:f682aeb462f1 | 313 | // } |
el14rd | 1:f682aeb462f1 | 314 | |
el14rd | 1:f682aeb462f1 | 315 | //infinite while loop |
el14rd | 1:f682aeb462f1 | 316 | while(1) { |
el14rd | 2:d7b17623ba26 | 317 | |
el14rd | 2:d7b17623ba26 | 318 | if (printFlag) { // if flag set, clear flag and print joystick values to serial port |
el14rd | 2:d7b17623ba26 | 319 | printFlag = 0; |
el14rd | 2:d7b17623ba26 | 320 | serial.printf("x = %f y = %f button = %d ",joystick.x,joystick.y,joystick.button); |
el14rd | 2:d7b17623ba26 | 321 | |
el14rd | 2:d7b17623ba26 | 322 | // check joystick direction |
el14rd | 2:d7b17623ba26 | 323 | if (joystick.direction == UP) |
el14rd | 2:d7b17623ba26 | 324 | serial.printf(" UP\n"); |
el14rd | 2:d7b17623ba26 | 325 | if (joystick.direction == DOWN) |
el14rd | 2:d7b17623ba26 | 326 | serial.printf(" DOWN\n"); |
el14rd | 2:d7b17623ba26 | 327 | if (joystick.direction == LEFT) |
el14rd | 2:d7b17623ba26 | 328 | serial.printf(" LEFT\n"); |
el14rd | 2:d7b17623ba26 | 329 | if (joystick.direction == RIGHT) |
el14rd | 2:d7b17623ba26 | 330 | serial.printf(" RIGHT\n"); |
el14rd | 2:d7b17623ba26 | 331 | if (joystick.direction == CENTRE) |
el14rd | 2:d7b17623ba26 | 332 | serial.printf(" CENTRE\n"); |
el14rd | 2:d7b17623ba26 | 333 | if (joystick.direction == UNKNOWN) |
el14rd | 2:d7b17623ba26 | 334 | serial.printf(" Unsupported direction\n"); |
el14rd | 2:d7b17623ba26 | 335 | } |
el14rd | 1:f682aeb462f1 | 336 | lcd.clear(); |
el14rd | 1:f682aeb462f1 | 337 | updateSnake(); |
el14rd | 1:f682aeb462f1 | 338 | drawSnake(); |
el14rd | 2:d7b17623ba26 | 339 | drawNut(); |
el14rd | 1:f682aeb462f1 | 340 | moveSnake(); |
el14rd | 1:f682aeb462f1 | 341 | startGmae(); |
el14rd | 1:f682aeb462f1 | 342 | lcd.refresh(); |
el14rd | 2:d7b17623ba26 | 343 | wait(0.5); |
el14rd | 2:d7b17623ba26 | 344 | |
el14rd | 2:d7b17623ba26 | 345 | serial.printf(" hi"); |
el14rd | 1:f682aeb462f1 | 346 | } |
el14rd | 2:d7b17623ba26 | 347 | //wait(0.5); |
el14rd | 2:d7b17623ba26 | 348 | //if(snake.life==1) { |
el14rd | 2:d7b17623ba26 | 349 | // lcd.printString("lost",24,42); |
el14rd | 2:d7b17623ba26 | 350 | // break; |
el14rd | 2:d7b17623ba26 | 351 | |
el14rd | 1:f682aeb462f1 | 352 | |
el14rd | 1:f682aeb462f1 | 353 | } |
el14rd | 1:f682aeb462f1 | 354 |