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@10:67e13dfaa10d, 2015-05-10 (annotated)
- Committer:
- el14rd
- Date:
- Sun May 10 20:48:51 2015 +0000
- Revision:
- 10:67e13dfaa10d
- Parent:
- 9:c8ea9b4af873
Final version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el14rd | 10:67e13dfaa10d | 1 | /** |
el14rd | 10:67e13dfaa10d | 2 | @file main.cpp |
el14rd | 10:67e13dfaa10d | 3 | @brief Program implementation |
el14rd | 10:67e13dfaa10d | 4 | @brief cpp file containing total program include main function, functions prototypes, defines and global variables. |
el14rd | 10:67e13dfaa10d | 5 | @brief hand-held joystick snake gmae |
el14rd | 10:67e13dfaa10d | 6 | @author Renhao Dai |
el14rd | 10:67e13dfaa10d | 7 | @date MAY 2015 |
el14rd | 10:67e13dfaa10d | 8 | */ |
eencae | 0:026fa541af7a | 9 | #include "mbed.h" |
el14rd | 1:f682aeb462f1 | 10 | #include "N5110.h" |
eencae | 0:026fa541af7a | 11 | #define DIRECTION_TOLERANCE 0.05 |
eencae | 0:026fa541af7a | 12 | |
el14rd | 10:67e13dfaa10d | 13 | /** |
el14rd | 10:67e13dfaa10d | 14 | @namespace lcd |
el14rd | 10:67e13dfaa10d | 15 | @brief GPIO output for LCD |
el14rd | 10:67e13dfaa10d | 16 | */ //pins in |
el14rd | 1:f682aeb462f1 | 17 | N5110 lcd(p7,p8,p9,p10,p11,p13,p26); |
el14rd | 10:67e13dfaa10d | 18 | PwmOut blacklight(p26); |
el14rd | 10:67e13dfaa10d | 19 | |
el14rd | 10:67e13dfaa10d | 20 | /** |
el14rd | 10:67e13dfaa10d | 21 | @namespace joystick |
el14rd | 10:67e13dfaa10d | 22 | @brief GPIO input for joystick |
el14rd | 10:67e13dfaa10d | 23 | */ |
el14rd | 8:b01f638d23b4 | 24 | InterruptIn button(p17); |
el14rd | 9:c8ea9b4af873 | 25 | AnalogIn xPot(p15); // connections for joystick |
el14rd | 1:f682aeb462f1 | 26 | AnalogIn yPot(p16); |
el14rd | 10:67e13dfaa10d | 27 | |
el14rd | 10:67e13dfaa10d | 28 | /** |
el14rd | 10:67e13dfaa10d | 29 | @namespace buttonA |
el14rd | 10:67e13dfaa10d | 30 | @brief GPIO input for button |
el14rd | 10:67e13dfaa10d | 31 | */ |
el14rd | 10:67e13dfaa10d | 32 | DigitalIn buttonA(p19); |
el14rd | 10:67e13dfaa10d | 33 | |
el14rd | 10:67e13dfaa10d | 34 | /** |
el14rd | 10:67e13dfaa10d | 35 | @namespace pot |
el14rd | 10:67e13dfaa10d | 36 | @brief GPIO input for potentiometer |
el14rd | 10:67e13dfaa10d | 37 | */ |
el14rd | 10:67e13dfaa10d | 38 | AnalogIn pot(p20); |
el14rd | 10:67e13dfaa10d | 39 | |
el14rd | 10:67e13dfaa10d | 40 | /** |
el14rd | 10:67e13dfaa10d | 41 | @namespace buzzer |
el14rd | 10:67e13dfaa10d | 42 | @brief GPIO output for buzzer |
el14rd | 10:67e13dfaa10d | 43 | */ |
el14rd | 4:41acda9d68c7 | 44 | PwmOut buzzer(p21); |
el14rd | 10:67e13dfaa10d | 45 | |
el14rd | 10:67e13dfaa10d | 46 | /** |
el14rd | 10:67e13dfaa10d | 47 | @namespace led |
el14rd | 10:67e13dfaa10d | 48 | @brief GPIO output for LED |
el14rd | 10:67e13dfaa10d | 49 | */ |
el14rd | 4:41acda9d68c7 | 50 | PwmOut led1(p24); |
el14rd | 4:41acda9d68c7 | 51 | PwmOut led2(p23); |
el14rd | 8:b01f638d23b4 | 52 | BusOut leds(LED4,LED3,LED2,LED1); |
el14rd | 4:41acda9d68c7 | 53 | |
el14rd | 10:67e13dfaa10d | 54 | float ain; /*!< variable in potentiometer */ |
el14rd | 10:67e13dfaa10d | 55 | int i; /*!< variable in coordinate of snake */ |
el14rd | 1:f682aeb462f1 | 56 | int j; |
el14rd | 10:67e13dfaa10d | 57 | int X; /*!< variable in coordinate of nut */ |
el14rd | 2:d7b17623ba26 | 58 | int Y; |
el14rd | 10:67e13dfaa10d | 59 | char buffer[14]; /*!< array for character */ |
el14rd | 1:f682aeb462f1 | 60 | |
el14rd | 10:67e13dfaa10d | 61 | int snakeX[100]; /*!< array for x axis coordinate */ |
el14rd | 10:67e13dfaa10d | 62 | int snakeY[100]; /*!< array for y axis coordinate */ |
eencae | 0:026fa541af7a | 63 | |
eencae | 0:026fa541af7a | 64 | // timer to regularly read the joystick |
eencae | 0:026fa541af7a | 65 | Ticker pollJoystick; |
eencae | 0:026fa541af7a | 66 | // Serial for debug |
eencae | 0:026fa541af7a | 67 | Serial serial(USBTX,USBRX); |
eencae | 0:026fa541af7a | 68 | |
eencae | 0:026fa541af7a | 69 | // create enumerated type (0,1,2,3 etc. for direction) |
eencae | 0:026fa541af7a | 70 | // could be extended for diagonals etc. |
eencae | 0:026fa541af7a | 71 | enum DirectionName { |
eencae | 0:026fa541af7a | 72 | UP, |
eencae | 0:026fa541af7a | 73 | DOWN, |
eencae | 0:026fa541af7a | 74 | LEFT, |
eencae | 0:026fa541af7a | 75 | RIGHT, |
eencae | 0:026fa541af7a | 76 | CENTRE, |
eencae | 0:026fa541af7a | 77 | UNKNOWN |
eencae | 0:026fa541af7a | 78 | }; |
eencae | 0:026fa541af7a | 79 | |
el14rd | 10:67e13dfaa10d | 80 | /// struct for Joystick |
eencae | 0:026fa541af7a | 81 | typedef struct JoyStick Joystick; |
eencae | 0:026fa541af7a | 82 | struct JoyStick { |
el14rd | 10:67e13dfaa10d | 83 | float x; /// current x value |
el14rd | 10:67e13dfaa10d | 84 | float x0; /// 'centred' x value |
el14rd | 10:67e13dfaa10d | 85 | float y; /// current y value |
el14rd | 10:67e13dfaa10d | 86 | float y0; /// 'centred' y value |
el14rd | 10:67e13dfaa10d | 87 | int button; /// button state (assume pull-down used, so 1 = pressed, 0 = unpressed) |
el14rd | 10:67e13dfaa10d | 88 | DirectionName direction; /// current direction |
eencae | 0:026fa541af7a | 89 | }; |
el14rd | 10:67e13dfaa10d | 90 | /// create struct variable |
eencae | 0:026fa541af7a | 91 | Joystick joystick; |
eencae | 0:026fa541af7a | 92 | |
el14rd | 10:67e13dfaa10d | 93 | int pause = 0; /*!< pause flag set in ISR */ |
el14rd | 10:67e13dfaa10d | 94 | int buttonFlag = 0; /*!< button flag set in ISR */ |
el14rd | 10:67e13dfaa10d | 95 | int printFlag = 0; /*!< print flag set in ISR */ |
el14rd | 10:67e13dfaa10d | 96 | int nutFlag =1; /*!< nut flag set in ISR */ |
el14rd | 10:67e13dfaa10d | 97 | int lifeFlag =0; /*!< lifet flag set in ISR */ |
el14rd | 10:67e13dfaa10d | 98 | float frequency[]= {659,554,659,554,550,494,554,587,494,659,554,440}; /*!< array for the tone frequency */ |
el14rd | 10:67e13dfaa10d | 99 | float beat[]= {1,1,1,1,1,0.5,0.5,1,1,1,1,2}; /*!< array for time span */ |
el14rd | 1:f682aeb462f1 | 100 | |
el14rd | 10:67e13dfaa10d | 101 | /// struct for nut |
el14rd | 1:f682aeb462f1 | 102 | typedef struct Nut Nut; |
el14rd | 1:f682aeb462f1 | 103 | struct Nut { |
el14rd | 10:67e13dfaa10d | 104 | int X; /// coordinate in x axis |
el14rd | 10:67e13dfaa10d | 105 | int Y; /// coordinate in y axis |
el14rd | 1:f682aeb462f1 | 106 | }; |
el14rd | 10:67e13dfaa10d | 107 | /// create struct variable |
el14rd | 1:f682aeb462f1 | 108 | Nut nut; |
eencae | 0:026fa541af7a | 109 | |
el14rd | 10:67e13dfaa10d | 110 | /// struct for snake |
el14rd | 1:f682aeb462f1 | 111 | typedef struct Snake Snake; |
el14rd | 1:f682aeb462f1 | 112 | struct Snake { |
el14rd | 10:67e13dfaa10d | 113 | int snakeX[100]; /// array in x axis |
el14rd | 10:67e13dfaa10d | 114 | int snakeY[100]; /// array in y axis |
el14rd | 10:67e13dfaa10d | 115 | int node; /// number of node |
el14rd | 10:67e13dfaa10d | 116 | DirectionName direction; /// direction virable |
el14rd | 1:f682aeb462f1 | 117 | }; |
el14rd | 10:67e13dfaa10d | 118 | /// create struct variable |
el14rd | 1:f682aeb462f1 | 119 | Snake snake; |
eencae | 0:026fa541af7a | 120 | |
el14rd | 10:67e13dfaa10d | 121 | /// definition for snake direction |
el14rd | 1:f682aeb462f1 | 122 | DirectionName snakeDirection; |
el14rd | 1:f682aeb462f1 | 123 | |
el14rd | 10:67e13dfaa10d | 124 | |
el14rd | 2:d7b17623ba26 | 125 | void initNut() |
el14rd | 10:67e13dfaa10d | 126 | { ///set the coordinate of nut randomly |
el14rd | 3:04ff8cd27dd1 | 127 | nut.X=rand()%84; |
el14rd | 3:04ff8cd27dd1 | 128 | nut.Y=rand()%48; |
el14rd | 8:b01f638d23b4 | 129 | } |
el14rd | 8:b01f638d23b4 | 130 | |
el14rd | 2:d7b17623ba26 | 131 | void drawNut() |
el14rd | 10:67e13dfaa10d | 132 | { ///draw nut |
el14rd | 8:b01f638d23b4 | 133 | if(nutFlag==1) { |
el14rd | 8:b01f638d23b4 | 134 | lcd.drawCircle(nut.X,nut.Y,1,1); |
el14rd | 3:04ff8cd27dd1 | 135 | } |
el14rd | 2:d7b17623ba26 | 136 | } |
eencae | 0:026fa541af7a | 137 | |
el14rd | 1:f682aeb462f1 | 138 | void moveSnake() |
el14rd | 1:f682aeb462f1 | 139 | { |
el14rd | 8:b01f638d23b4 | 140 | |
el14rd | 8:b01f638d23b4 | 141 | if(joystick.direction == CENTRE) { |
el14rd | 10:67e13dfaa10d | 142 | ///does not make any changes |
el14rd | 4:41acda9d68c7 | 143 | joystick.direction = CENTRE; |
el14rd | 10:67e13dfaa10d | 144 | } else if(joystick.direction == UNKNOWN) { |
el14rd | 10:67e13dfaa10d | 145 | //does not make any changes |
el14rd | 8:b01f638d23b4 | 146 | joystick.direction = UNKNOWN; |
el14rd | 10:67e13dfaa10d | 147 | |
el14rd | 8:b01f638d23b4 | 148 | } else if(joystick.direction != UNKNOWN&&joystick.direction != CENTRE) { |
el14rd | 2:d7b17623ba26 | 149 | |
el14rd | 4:41acda9d68c7 | 150 | if((joystick.direction == DOWN)&&(snake.direction!=LEFT)) { |
el14rd | 10:67e13dfaa10d | 151 | ///sent rightward direction to snake.direction |
el14rd | 2:d7b17623ba26 | 152 | snake.direction= RIGHT; |
el14rd | 8:b01f638d23b4 | 153 | |
el14rd | 4:41acda9d68c7 | 154 | } else if((joystick.direction == UP)&&(snake.direction!=RIGHT)) { |
el14rd | 10:67e13dfaa10d | 155 | ///sent leftward direction to snake.direction |
el14rd | 2:d7b17623ba26 | 156 | snake.direction=LEFT; |
el14rd | 10:67e13dfaa10d | 157 | |
el14rd | 4:41acda9d68c7 | 158 | } else if((joystick.direction == LEFT)&&(snake.direction!=DOWN)) { |
el14rd | 10:67e13dfaa10d | 159 | ///sent upward direction to snake.direction |
el14rd | 5:d8a06e7c54fb | 160 | snake.direction=UP; |
el14rd | 10:67e13dfaa10d | 161 | |
el14rd | 4:41acda9d68c7 | 162 | } else if((joystick.direction == RIGHT)&&(snake.direction!=UP)) { |
el14rd | 10:67e13dfaa10d | 163 | ///sent downward direction to snake.direction |
el14rd | 5:d8a06e7c54fb | 164 | snake.direction=DOWN; |
el14rd | 10:67e13dfaa10d | 165 | |
eencae | 0:026fa541af7a | 166 | } |
eencae | 0:026fa541af7a | 167 | |
eencae | 0:026fa541af7a | 168 | } |
el14rd | 8:b01f638d23b4 | 169 | |
el14rd | 2:d7b17623ba26 | 170 | } |
el14rd | 2:d7b17623ba26 | 171 | |
el14rd | 2:d7b17623ba26 | 172 | void initSnake() |
el14rd | 2:d7b17623ba26 | 173 | { |
el14rd | 10:67e13dfaa10d | 174 | ///define initial coordinate of snake |
el14rd | 10:67e13dfaa10d | 175 | snake.snakeX[0] = 16; ///coordinate of head |
el14rd | 2:d7b17623ba26 | 176 | snake.snakeY[0] = 10; |
el14rd | 5:d8a06e7c54fb | 177 | snake.snakeX[1] = 14; |
el14rd | 2:d7b17623ba26 | 178 | snake.snakeY[1] = 10; |
el14rd | 4:41acda9d68c7 | 179 | snake.snakeX[2] = 12; |
el14rd | 2:d7b17623ba26 | 180 | snake.snakeY[2] = 10; |
el14rd | 5:d8a06e7c54fb | 181 | snake.snakeX[3] = 10; |
el14rd | 2:d7b17623ba26 | 182 | snake.snakeY[3] = 10; |
el14rd | 10:67e13dfaa10d | 183 | snake.node = 5; ///node of snake |
el14rd | 10:67e13dfaa10d | 184 | snake.direction = RIGHT;///initialize the direction of snake |
el14rd | 8:b01f638d23b4 | 185 | |
el14rd | 5:d8a06e7c54fb | 186 | } |
el14rd | 8:b01f638d23b4 | 187 | |
el14rd | 8:b01f638d23b4 | 188 | |
el14rd | 1:f682aeb462f1 | 189 | void drawSnake() |
el14rd | 8:b01f638d23b4 | 190 | { |
el14rd | 10:67e13dfaa10d | 191 | ///each node is a circle with 1 pixel radius |
el14rd | 8:b01f638d23b4 | 192 | for(i=0; i<snake.node-1; i++) { |
el14rd | 5:d8a06e7c54fb | 193 | lcd.drawCircle(snake.snakeX[i],snake.snakeY[i],1,1); |
el14rd | 8:b01f638d23b4 | 194 | } |
el14rd | 8:b01f638d23b4 | 195 | |
el14rd | 1:f682aeb462f1 | 196 | } |
el14rd | 1:f682aeb462f1 | 197 | |
el14rd | 1:f682aeb462f1 | 198 | void startGmae() |
el14rd | 1:f682aeb462f1 | 199 | { |
el14rd | 10:67e13dfaa10d | 200 | ///occasion that snake eats nut |
el14rd | 10:67e13dfaa10d | 201 | if((snake.snakeX[0]==nut.X)&&(snake.snakeY[0]==nut.Y)) { |
el14rd | 8:b01f638d23b4 | 202 | snake.node+=1; |
el14rd | 10:67e13dfaa10d | 203 | nutFlag=0; /// new nut appear |
el14rd | 10:67e13dfaa10d | 204 | buzzer.period(1/frequency[1]); /// set PWM period |
el14rd | 10:67e13dfaa10d | 205 | buzzer=0.1; /// set duty cycle |
el14rd | 3:04ff8cd27dd1 | 206 | } |
el14rd | 8:b01f638d23b4 | 207 | |
el14rd | 8:b01f638d23b4 | 208 | if((snake.snakeX[0]!=nut.X)||(snake.snakeY[0]!=nut.Y)) { |
el14rd | 10:67e13dfaa10d | 209 | ///if snake gets nut, play a beep |
el14rd | 8:b01f638d23b4 | 210 | buzzer=0; |
el14rd | 8:b01f638d23b4 | 211 | } |
el14rd | 8:b01f638d23b4 | 212 | |
el14rd | 10:67e13dfaa10d | 213 | if(nutFlag==0) { |
el14rd | 10:67e13dfaa10d | 214 | ///reset nut randomly |
el14rd | 1:f682aeb462f1 | 215 | nut.X = rand()%83; |
el14rd | 1:f682aeb462f1 | 216 | nut.Y = rand()%47; |
el14rd | 3:04ff8cd27dd1 | 217 | nutFlag = 1; |
el14rd | 1:f682aeb462f1 | 218 | } |
el14rd | 2:d7b17623ba26 | 219 | |
el14rd | 8:b01f638d23b4 | 220 | |
el14rd | 10:67e13dfaa10d | 221 | for(i=3; i<snake.node; i++) { |
el14rd | 10:67e13dfaa10d | 222 | ///if snake collide itself snake die |
el14rd | 8:b01f638d23b4 | 223 | if(snake.snakeX[i]==snake.snakeX[0] && snake.snakeY[i]==snake.snakeY[0]) { |
el14rd | 8:b01f638d23b4 | 224 | lifeFlag=1; |
el14rd | 7:69ae0585d11a | 225 | } |
el14rd | 8:b01f638d23b4 | 226 | } |
el14rd | 8:b01f638d23b4 | 227 | |
el14rd | 8:b01f638d23b4 | 228 | |
el14rd | 10:67e13dfaa10d | 229 | for(i=snake.node-1; i>0; i--) { |
el14rd | 10:67e13dfaa10d | 230 | ///move snake |
el14rd | 8:b01f638d23b4 | 231 | if(snake.snakeX[0]>1||snake.snakeX[0]<83||snake.snakeY[0]>1||snake.snakeY[0]<47) { |
el14rd | 8:b01f638d23b4 | 232 | snake.snakeX[i]=snake.snakeX[i-1]; |
el14rd | 8:b01f638d23b4 | 233 | snake.snakeY[i]=snake.snakeY[i-1]; |
el14rd | 8:b01f638d23b4 | 234 | } else { |
el14rd | 8:b01f638d23b4 | 235 | snake.snakeX[i]=snake.snakeX[i]; |
el14rd | 8:b01f638d23b4 | 236 | snake.snakeY[i]=snake.snakeY[i]; |
el14rd | 8:b01f638d23b4 | 237 | } |
el14rd | 8:b01f638d23b4 | 238 | } |
el14rd | 8:b01f638d23b4 | 239 | |
el14rd | 1:f682aeb462f1 | 240 | |
el14rd | 1:f682aeb462f1 | 241 | if(snake.snakeX[0]<1||snake.snakeX[0]>83||snake.snakeY[0]<1||snake.snakeY[0]>47) { |
el14rd | 10:67e13dfaa10d | 242 | /// snake collide the corrider snake die |
el14rd | 10:67e13dfaa10d | 243 | lifeFlag=1; |
el14rd | 1:f682aeb462f1 | 244 | } |
el14rd | 8:b01f638d23b4 | 245 | |
el14rd | 1:f682aeb462f1 | 246 | } |
el14rd | 1:f682aeb462f1 | 247 | |
el14rd | 8:b01f638d23b4 | 248 | void printScore() |
el14rd | 10:67e13dfaa10d | 249 | { ///print characters on map at coordiante 0,0 |
el14rd | 6:290173d4a22d | 250 | lcd.printString("Your Score is",0,0); |
el14rd | 10:67e13dfaa10d | 251 | ///print score |
el14rd | 10:67e13dfaa10d | 252 | int score=snake.node-4; |
el14rd | 6:290173d4a22d | 253 | int length = sprintf(buffer," SCORE=%2d ",score); |
el14rd | 8:b01f638d23b4 | 254 | lcd.printString(buffer,0,2 ); |
el14rd | 10:67e13dfaa10d | 255 | ///print characters on map at coordiante 0,4 |
el14rd | 6:290173d4a22d | 256 | lcd.printString("Press to start",0,4); |
el14rd | 8:b01f638d23b4 | 257 | } |
el14rd | 7:69ae0585d11a | 258 | |
el14rd | 8:b01f638d23b4 | 259 | void buttonPressed() |
el14rd | 10:67e13dfaa10d | 260 | { ///button Flag set |
el14rd | 8:b01f638d23b4 | 261 | buttonFlag =1; |
el14rd | 7:69ae0585d11a | 262 | } |
el14rd | 7:69ae0585d11a | 263 | |
el14rd | 8:b01f638d23b4 | 264 | void snakeModel() |
el14rd | 10:67e13dfaa10d | 265 | { ///set moving snake model |
el14rd | 8:b01f638d23b4 | 266 | for(int i=0; i<25; i+=5) { |
el14rd | 8:b01f638d23b4 | 267 | lcd.clear(); |
el14rd | 8:b01f638d23b4 | 268 | lcd.printString("Press to start",1,1); |
el14rd | 8:b01f638d23b4 | 269 | |
el14rd | 8:b01f638d23b4 | 270 | lcd.drawRect(44-i,21,1,1,1); |
el14rd | 8:b01f638d23b4 | 271 | lcd.drawRect(46-i,21,1,1,1); |
el14rd | 8:b01f638d23b4 | 272 | lcd.drawRect(48-i,21,1,1,1); |
el14rd | 8:b01f638d23b4 | 273 | |
el14rd | 8:b01f638d23b4 | 274 | lcd.drawRect(42-i,23,1,1,1); |
el14rd | 8:b01f638d23b4 | 275 | lcd.drawRect(44-i,23,1,1,1); |
el14rd | 8:b01f638d23b4 | 276 | lcd.drawRect(46-i,23,1,1,1); |
el14rd | 8:b01f638d23b4 | 277 | lcd.drawRect(48-i,23,1,1,1); |
el14rd | 8:b01f638d23b4 | 278 | |
el14rd | 8:b01f638d23b4 | 279 | lcd.drawRect(40-i,25,1,1,1); |
el14rd | 8:b01f638d23b4 | 280 | lcd.drawRect(42-i,25,1,1,1); |
el14rd | 8:b01f638d23b4 | 281 | lcd.drawRect(44-i,25,1,1,1); |
el14rd | 8:b01f638d23b4 | 282 | lcd.drawRect(46-i,25,1,1,1); |
el14rd | 8:b01f638d23b4 | 283 | lcd.drawRect(48-i,25,1,1,1); |
el14rd | 8:b01f638d23b4 | 284 | lcd.drawRect(50-i,25,1,1,1); |
el14rd | 8:b01f638d23b4 | 285 | |
el14rd | 8:b01f638d23b4 | 286 | lcd.drawRect(40-i,27,1,1,1); |
el14rd | 8:b01f638d23b4 | 287 | lcd.drawRect(42-i,27,1,1,1); |
el14rd | 8:b01f638d23b4 | 288 | lcd.drawRect(44-i,27,1,1,1); |
el14rd | 8:b01f638d23b4 | 289 | lcd.drawRect(46-i,27,1,1,1); |
el14rd | 8:b01f638d23b4 | 290 | lcd.drawRect(48-i,27,1,1,1); |
el14rd | 10:67e13dfaa10d | 291 | lcd.drawRect(50-i,27,1,1,1); ///head |
el14rd | 8:b01f638d23b4 | 292 | |
el14rd | 10:67e13dfaa10d | 293 | lcd.drawRect(38-i,29,1,1,0); ///tougue |
el14rd | 8:b01f638d23b4 | 294 | lcd.drawRect(40-i,31,1,1,0); |
el14rd | 8:b01f638d23b4 | 295 | |
el14rd | 8:b01f638d23b4 | 296 | lcd.drawRect(46-i,29,1,1,1); |
el14rd | 8:b01f638d23b4 | 297 | lcd.drawRect(48-i,29,1,1,1); |
el14rd | 8:b01f638d23b4 | 298 | lcd.drawRect(50-i,29,1,1,1); |
el14rd | 8:b01f638d23b4 | 299 | |
el14rd | 8:b01f638d23b4 | 300 | lcd.drawRect(46-i,31,1,1,1); |
el14rd | 8:b01f638d23b4 | 301 | lcd.drawRect(48-i,31,1,1,1); |
el14rd | 8:b01f638d23b4 | 302 | lcd.drawRect(50-i,31,1,1,1); |
el14rd | 8:b01f638d23b4 | 303 | |
el14rd | 8:b01f638d23b4 | 304 | lcd.drawRect(44-i,33,1,1,1); |
el14rd | 8:b01f638d23b4 | 305 | lcd.drawRect(46-i,33,1,1,1); |
el14rd | 8:b01f638d23b4 | 306 | lcd.drawRect(48-i,33,1,1,1); |
el14rd | 8:b01f638d23b4 | 307 | |
el14rd | 8:b01f638d23b4 | 308 | lcd.drawRect(44-i,35,1,1,1); |
el14rd | 8:b01f638d23b4 | 309 | lcd.drawRect(46-i,35,1,1,1); |
el14rd | 8:b01f638d23b4 | 310 | lcd.drawRect(48-i,35,1,1,1); |
el14rd | 8:b01f638d23b4 | 311 | |
el14rd | 8:b01f638d23b4 | 312 | lcd.drawRect(42-i,37,1,1,1); |
el14rd | 8:b01f638d23b4 | 313 | lcd.drawRect(44-i,37,1,1,1); |
el14rd | 8:b01f638d23b4 | 314 | lcd.drawRect(46-i,37,1,1,1); |
el14rd | 6:290173d4a22d | 315 | |
el14rd | 8:b01f638d23b4 | 316 | lcd.drawRect(42-i,39,1,1,1); |
el14rd | 8:b01f638d23b4 | 317 | lcd.drawRect(44-i,39,1,1,1); |
el14rd | 8:b01f638d23b4 | 318 | lcd.drawRect(46-i,39,1,1,1); |
el14rd | 8:b01f638d23b4 | 319 | |
el14rd | 8:b01f638d23b4 | 320 | lcd.drawRect(44-i,41,1,1,1); |
el14rd | 8:b01f638d23b4 | 321 | lcd.drawRect(46-i,41,1,1,1); |
el14rd | 8:b01f638d23b4 | 322 | lcd.drawRect(48-i,41,1,1,1); |
el14rd | 10:67e13dfaa10d | 323 | lcd.drawRect(50-i,41,1,1,1);///neck |
el14rd | 8:b01f638d23b4 | 324 | |
el14rd | 10:67e13dfaa10d | 325 | lcd.drawRect(80-i,41,1,1,1);///tail |
el14rd | 8:b01f638d23b4 | 326 | lcd.drawRect(82-i,41,1,1,1); |
el14rd | 8:b01f638d23b4 | 327 | lcd.drawRect(84-i,41,1,1,1); |
el14rd | 8:b01f638d23b4 | 328 | |
el14rd | 8:b01f638d23b4 | 329 | lcd.drawRect(82-i,39,1,1,1); |
el14rd | 8:b01f638d23b4 | 330 | lcd.drawRect(84-i,39,1,1,1); |
el14rd | 8:b01f638d23b4 | 331 | |
el14rd | 8:b01f638d23b4 | 332 | lcd.drawRect(82-i,37,1,1,1); |
el14rd | 8:b01f638d23b4 | 333 | |
el14rd | 10:67e13dfaa10d | 334 | lcd.drawRect(80-i,35,1,1,1);///tail |
el14rd | 8:b01f638d23b4 | 335 | |
el14rd | 8:b01f638d23b4 | 336 | lcd.drawRect(46-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 337 | lcd.drawRect(48-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 338 | lcd.drawRect(50-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 339 | lcd.drawRect(52-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 340 | lcd.drawRect(54-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 341 | lcd.drawRect(56-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 342 | lcd.drawRect(58-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 343 | lcd.drawRect(60-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 344 | lcd.drawRect(62-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 345 | lcd.drawRect(64-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 346 | lcd.drawRect(66-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 347 | lcd.drawRect(68-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 348 | lcd.drawRect(70-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 349 | lcd.drawRect(72-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 350 | lcd.drawRect(74-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 351 | lcd.drawRect(76-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 352 | lcd.drawRect(78-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 353 | lcd.drawRect(80-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 354 | lcd.drawRect(82-i,43,1,1,1); |
el14rd | 8:b01f638d23b4 | 355 | |
el14rd | 8:b01f638d23b4 | 356 | lcd.drawRect(48-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 357 | lcd.drawRect(50-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 358 | lcd.drawRect(52-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 359 | lcd.drawRect(54-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 360 | lcd.drawRect(56-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 361 | lcd.drawRect(58-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 362 | lcd.drawRect(60-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 363 | lcd.drawRect(62-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 364 | lcd.drawRect(64-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 365 | lcd.drawRect(66-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 366 | lcd.drawRect(68-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 367 | lcd.drawRect(70-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 368 | lcd.drawRect(72-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 369 | lcd.drawRect(74-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 370 | lcd.drawRect(76-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 371 | lcd.drawRect(78-i,45,1,1,1); |
el14rd | 8:b01f638d23b4 | 372 | lcd.drawRect(80-i,45,1,1,1); |
el14rd | 7:69ae0585d11a | 373 | wait(0.5); |
el14rd | 8:b01f638d23b4 | 374 | //lcd.refresh(); |
el14rd | 7:69ae0585d11a | 375 | } |
el14rd | 8:b01f638d23b4 | 376 | |
el14rd | 8:b01f638d23b4 | 377 | } |
el14rd | 1:f682aeb462f1 | 378 | |
el14rd | 10:67e13dfaa10d | 379 | /// read default positions of the joystick to calibrate later readings |
eencae | 0:026fa541af7a | 380 | void calibrateJoystick() |
eencae | 0:026fa541af7a | 381 | { |
el14rd | 10:67e13dfaa10d | 382 | button.mode(PullDown); ///must not move during calibration |
el14rd | 10:67e13dfaa10d | 383 | joystick.x0 = xPot; ///initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) |
eencae | 0:026fa541af7a | 384 | joystick.y0 = yPot; |
eencae | 0:026fa541af7a | 385 | } |
el14rd | 1:f682aeb462f1 | 386 | |
el14rd | 2:d7b17623ba26 | 387 | |
eencae | 0:026fa541af7a | 388 | void updateJoystick() |
eencae | 0:026fa541af7a | 389 | { |
el14rd | 10:67e13dfaa10d | 390 | /// read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) |
eencae | 0:026fa541af7a | 391 | joystick.x = xPot - joystick.x0; |
eencae | 0:026fa541af7a | 392 | joystick.y = yPot - joystick.y0; |
el14rd | 10:67e13dfaa10d | 393 | /// read button state |
eencae | 0:026fa541af7a | 394 | joystick.button = button; |
el14rd | 10:67e13dfaa10d | 395 | /// calculate direction depending on x,y values |
el14rd | 10:67e13dfaa10d | 396 | /// tolerance allows a little lee-way in case joystick not exactly in the stated direction |
eencae | 0:026fa541af7a | 397 | if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 398 | joystick.direction = CENTRE; |
eencae | 0:026fa541af7a | 399 | } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 400 | joystick.direction = UP; |
eencae | 0:026fa541af7a | 401 | } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 402 | joystick.direction = DOWN; |
eencae | 0:026fa541af7a | 403 | } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 404 | joystick.direction = RIGHT; |
eencae | 0:026fa541af7a | 405 | } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
eencae | 0:026fa541af7a | 406 | joystick.direction = LEFT; |
eencae | 0:026fa541af7a | 407 | } else { |
eencae | 0:026fa541af7a | 408 | joystick.direction = UNKNOWN; |
eencae | 0:026fa541af7a | 409 | } |
el14rd | 10:67e13dfaa10d | 410 | /// set flag for printing |
eencae | 0:026fa541af7a | 411 | printFlag = 1; |
el14rd | 1:f682aeb462f1 | 412 | } |
el14rd | 1:f682aeb462f1 | 413 | |
el14rd | 1:f682aeb462f1 | 414 | void updateSnake() |
el14rd | 1:f682aeb462f1 | 415 | { |
el14rd | 10:67e13dfaa10d | 416 | /// get direction of joystick and update snake |
el14rd | 4:41acda9d68c7 | 417 | if (snake.direction == LEFT) { |
el14rd | 7:69ae0585d11a | 418 | snake.snakeX[0]-=1; |
el14rd | 10:67e13dfaa10d | 419 | ///will stop at the top edge |
el14rd | 4:41acda9d68c7 | 420 | if(snake.snakeX[0]<1) { |
el14rd | 4:41acda9d68c7 | 421 | snake.snakeX[0]=1; |
el14rd | 10:67e13dfaa10d | 422 | } |
el14rd | 1:f682aeb462f1 | 423 | } |
el14rd | 4:41acda9d68c7 | 424 | if (snake.direction == RIGHT) { |
el14rd | 7:69ae0585d11a | 425 | snake.snakeX[0]+=1; |
el14rd | 10:67e13dfaa10d | 426 | ///will stop at the bottom edge |
el14rd | 4:41acda9d68c7 | 427 | if(snake.snakeX[0]>83) { |
el14rd | 4:41acda9d68c7 | 428 | snake.snakeX[0]=83; |
el14rd | 10:67e13dfaa10d | 429 | } |
el14rd | 1:f682aeb462f1 | 430 | } |
el14rd | 4:41acda9d68c7 | 431 | if (snake.direction == UP) { |
el14rd | 7:69ae0585d11a | 432 | snake.snakeY[0]-=1; |
el14rd | 10:67e13dfaa10d | 433 | ///will stop at the left edge |
el14rd | 4:41acda9d68c7 | 434 | if(snake.snakeY[0]>47) { |
el14rd | 4:41acda9d68c7 | 435 | snake.snakeY[0]=47; |
el14rd | 10:67e13dfaa10d | 436 | } |
el14rd | 1:f682aeb462f1 | 437 | } |
el14rd | 4:41acda9d68c7 | 438 | if (snake.direction == DOWN) { |
el14rd | 7:69ae0585d11a | 439 | snake.snakeY[0]+=1; |
el14rd | 10:67e13dfaa10d | 440 | ///will stop at the right edge |
el14rd | 4:41acda9d68c7 | 441 | if(snake.snakeY[0]<1) { |
el14rd | 4:41acda9d68c7 | 442 | snake.snakeY[0]=1; |
el14rd | 10:67e13dfaa10d | 443 | } |
el14rd | 1:f682aeb462f1 | 444 | } |
el14rd | 8:b01f638d23b4 | 445 | if(snake.direction == CENTRE) { |
el14rd | 4:41acda9d68c7 | 446 | snake.snakeX[0]+=1; |
el14rd | 8:b01f638d23b4 | 447 | } |
el14rd | 8:b01f638d23b4 | 448 | if(snake.direction == UNKNOWN) { |
el14rd | 5:d8a06e7c54fb | 449 | snake.snakeX[0]+=1; |
el14rd | 8:b01f638d23b4 | 450 | } |
el14rd | 1:f682aeb462f1 | 451 | } |
el14rd | 1:f682aeb462f1 | 452 | |
el14rd | 1:f682aeb462f1 | 453 | |
el14rd | 10:67e13dfaa10d | 454 | ///main code |
el14rd | 1:f682aeb462f1 | 455 | int main() |
el14rd | 1:f682aeb462f1 | 456 | { |
el14rd | 10:67e13dfaa10d | 457 | /// function prototypes |
el14rd | 10:67e13dfaa10d | 458 | lcd.init(); /// initialize the lcd |
el14rd | 10:67e13dfaa10d | 459 | lcd.setBrightness(0.5); /// put LED backlight on 50% |
el14rd | 10:67e13dfaa10d | 460 | calibrateJoystick(); /// get centred values of joystick |
el14rd | 10:67e13dfaa10d | 461 | pollJoystick.attach(&updateJoystick,1.0/10.0); /// read joystick 10 times per second |
el14rd | 10:67e13dfaa10d | 462 | lcd.refresh(); /// refresh the screen |
el14rd | 10:67e13dfaa10d | 463 | initNut(); /// create a nut |
el14rd | 10:67e13dfaa10d | 464 | initSnake(); /// create a snake |
el14rd | 10:67e13dfaa10d | 465 | button.rise(&buttonPressed); ///event generated on rising edge |
el14rd | 10:67e13dfaa10d | 466 | buttonA.mode(PullDown); /// |
el14rd | 10:67e13dfaa10d | 467 | |
el14rd | 10:67e13dfaa10d | 468 | ///infinite while loop to execute the game |
el14rd | 1:f682aeb462f1 | 469 | while(1) { |
el14rd | 10:67e13dfaa10d | 470 | |
el14rd | 10:67e13dfaa10d | 471 | if(buttonFlag==0) { ///joystick button unpressed |
el14rd | 8:b01f638d23b4 | 472 | lcd.printString("Press to start",1,1); |
el14rd | 8:b01f638d23b4 | 473 | snakeModel(); |
el14rd | 10:67e13dfaa10d | 474 | led1=1; ///red LED illuminate |
el14rd | 8:b01f638d23b4 | 475 | } else { |
el14rd | 10:67e13dfaa10d | 476 | |
el14rd | 10:67e13dfaa10d | 477 | led1=0; ///red LED off |
el14rd | 10:67e13dfaa10d | 478 | led2=1; ///green LED on |
el14rd | 10:67e13dfaa10d | 479 | leds = 0; /// all status LED off |
el14rd | 8:b01f638d23b4 | 480 | |
el14rd | 10:67e13dfaa10d | 481 | if(buttonA) { ///if button pressed, toggle pause |
el14rd | 8:b01f638d23b4 | 482 | wait_ms(50); |
el14rd | 10:67e13dfaa10d | 483 | pause = !pause; ///pause the game |
el14rd | 8:b01f638d23b4 | 484 | leds = 15; |
el14rd | 10:67e13dfaa10d | 485 | led2=0; /// green LED off |
el14rd | 10:67e13dfaa10d | 486 | led1=1; /// red LED on |
el14rd | 8:b01f638d23b4 | 487 | } |
el14rd | 8:b01f638d23b4 | 488 | |
el14rd | 10:67e13dfaa10d | 489 | while(pause) {///if pause==1 |
el14rd | 8:b01f638d23b4 | 490 | leds = 3; |
el14rd | 10:67e13dfaa10d | 491 | if(buttonA) { /// if button pressed, toggle pause |
el14rd | 8:b01f638d23b4 | 492 | wait_ms(50); |
el14rd | 10:67e13dfaa10d | 493 | pause = 0; ///set pause to 0 |
el14rd | 10:67e13dfaa10d | 494 | leds = 1; /// one status LED on |
el14rd | 10:67e13dfaa10d | 495 | led2=1; /// green LED on |
el14rd | 10:67e13dfaa10d | 496 | led1=0; /// red LED off |
el14rd | 8:b01f638d23b4 | 497 | |
el14rd | 8:b01f638d23b4 | 498 | } |
el14rd | 8:b01f638d23b4 | 499 | } |
el14rd | 8:b01f638d23b4 | 500 | } |
el14rd | 1:f682aeb462f1 | 501 | lcd.clear(); |
el14rd | 5:d8a06e7c54fb | 502 | lifeFlag=0; |
el14rd | 1:f682aeb462f1 | 503 | updateSnake(); |
el14rd | 1:f682aeb462f1 | 504 | drawSnake(); |
el14rd | 2:d7b17623ba26 | 505 | drawNut(); |
el14rd | 1:f682aeb462f1 | 506 | moveSnake(); |
el14rd | 1:f682aeb462f1 | 507 | startGmae(); |
el14rd | 8:b01f638d23b4 | 508 | lcd.refresh(); |
el14rd | 8:b01f638d23b4 | 509 | wait(0.08); |
el14rd | 10:67e13dfaa10d | 510 | if(lifeFlag==1) { ///snake die |
el14rd | 10:67e13dfaa10d | 511 | led1=1; |
el14rd | 10:67e13dfaa10d | 512 | led2=0; |
el14rd | 8:b01f638d23b4 | 513 | lcd.clear(); |
el14rd | 8:b01f638d23b4 | 514 | lcd.refresh(); |
el14rd | 8:b01f638d23b4 | 515 | printScore(); |
el14rd | 8:b01f638d23b4 | 516 | for(int i=0; i<=11; i++) { |
el14rd | 10:67e13dfaa10d | 517 | buzzer.period(1/(frequency[i])); /// set PWM period |
el14rd | 10:67e13dfaa10d | 518 | buzzer=0.1; /// set duty cycle |
el14rd | 8:b01f638d23b4 | 519 | wait(0.5*beat[i]); |
el14rd | 8:b01f638d23b4 | 520 | } |
el14rd | 10:67e13dfaa10d | 521 | buzzer=0; ///close buzzer |
el14rd | 8:b01f638d23b4 | 522 | } |
el14rd | 10:67e13dfaa10d | 523 | |
el14rd | 10:67e13dfaa10d | 524 | ain = pot.read(); /// read value of potentiometer |
el14rd | 9:c8ea9b4af873 | 525 | lcd.setBrightness(ain); |
el14rd | 10:67e13dfaa10d | 526 | lcd.refresh(); |
el14rd | 8:b01f638d23b4 | 527 | } |
el14rd | 1:f682aeb462f1 | 528 | } |
el14rd | 1:f682aeb462f1 | 529 |