The present code implements a single player squash game, using joystick to move paddle right or left. And checks the current temperature inside the device.
Dependencies: 2645_I2C_TMP102 2645_Physics_Engine_Example Paddle mbed
main.cpp@2:c1b442f26087, 2016-05-09 (annotated)
- Committer:
- bonnyngangu
- Date:
- Mon May 09 09:17:47 2016 +0000
- Revision:
- 2:c1b442f26087
- Parent:
- 1:fe4ac8f10309
- Child:
- 4:1d38d6b09e84
The present code implements a single player squash game, using joystick to move paddle right or left. And checks the current temperature inside the device.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bonnyngangu | 0:61128d697217 | 1 | #include "mbed.h" |
bonnyngangu | 0:61128d697217 | 2 | #include "N5110.h" |
bonnyngangu | 1:fe4ac8f10309 | 3 | #include "game.h" |
bonnyngangu | 2:c1b442f26087 | 4 | #include "main.h" |
bonnyngangu | 2:c1b442f26087 | 5 | |
bonnyngangu | 0:61128d697217 | 6 | |
bonnyngangu | 0:61128d697217 | 7 | #define BALLRADIUS 2 |
bonnyngangu | 0:61128d697217 | 8 | |
bonnyngangu | 0:61128d697217 | 9 | |
bonnyngangu | 0:61128d697217 | 10 | // VCC, SCE, RST, D/C, MOSI, SCLK, LED |
bonnyngangu | 0:61128d697217 | 11 | N5110 lcd (PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3); |
bonnyngangu | 0:61128d697217 | 12 | // Can also power (VCC) directly from VOUT (3.3 V) - |
bonnyngangu | 0:61128d697217 | 13 | // Can give better performance due to current limitation from GPIO pin |
bonnyngangu | 0:61128d697217 | 14 | |
bonnyngangu | 1:fe4ac8f10309 | 15 | InterruptIn interrupt_button(PTB18); // interrupting the sleep mode. |
bonnyngangu | 0:61128d697217 | 16 | PwmOut buzzer(PTA2); // buzzer connection to PwmOut. |
bonnyngangu | 0:61128d697217 | 17 | |
bonnyngangu | 1:fe4ac8f10309 | 18 | // connections for joystick |
bonnyngangu | 1:fe4ac8f10309 | 19 | DigitalIn button(PTB10); |
bonnyngangu | 1:fe4ac8f10309 | 20 | AnalogIn xPot(PTB2); |
bonnyngangu | 1:fe4ac8f10309 | 21 | AnalogIn yPot(PTB3); |
bonnyngangu | 1:fe4ac8f10309 | 22 | |
bonnyngangu | 0:61128d697217 | 23 | void delete_file(char filename[]); |
bonnyngangu | 0:61128d697217 | 24 | |
bonnyngangu | 0:61128d697217 | 25 | // function prototypes |
bonnyngangu | 0:61128d697217 | 26 | void init_ball(); |
bonnyngangu | 0:61128d697217 | 27 | void game_timer_isr(); |
bonnyngangu | 0:61128d697217 | 28 | void BallMove(); |
bonnyngangu | 0:61128d697217 | 29 | void redraw_screen(); |
bonnyngangu | 0:61128d697217 | 30 | void update_physics_engine(); |
bonnyngangu | 0:61128d697217 | 31 | void check_collisions(); |
bonnyngangu | 0:61128d697217 | 32 | void Squash_Court(); |
bonnyngangu | 0:61128d697217 | 33 | void getPaddle(); |
bonnyngangu | 0:61128d697217 | 34 | void PaddleMove(); |
bonnyngangu | 0:61128d697217 | 35 | void updateJoystick(); |
bonnyngangu | 1:fe4ac8f10309 | 36 | void drawCircle(int x0,int y0,int radius,int fill); |
bonnyngangu | 2:c1b442f26087 | 37 | void Temp_reading(); |
bonnyngangu | 0:61128d697217 | 38 | |
bonnyngangu | 0:61128d697217 | 39 | // Can give better performance due to current limitation from GPIO pin |
bonnyngangu | 0:61128d697217 | 40 | Ticker game_timer; |
bonnyngangu | 0:61128d697217 | 41 | |
bonnyngangu | 0:61128d697217 | 42 | // struct used to store 2D vectors |
bonnyngangu | 0:61128d697217 | 43 | typedef struct vector_t vector_t; |
bonnyngangu | 0:61128d697217 | 44 | struct vector_t { |
bonnyngangu | 0:61128d697217 | 45 | float x; |
bonnyngangu | 0:61128d697217 | 46 | float y; |
bonnyngangu | 0:61128d697217 | 47 | }; |
bonnyngangu | 0:61128d697217 | 48 | |
bonnyngangu | 0:61128d697217 | 49 | vector_t pos; // ball position |
bonnyngangu | 0:61128d697217 | 50 | vector_t vel; // ball velocity |
bonnyngangu | 0:61128d697217 | 51 | vector_t acc; // ball acceleration |
bonnyngangu | 0:61128d697217 | 52 | |
bonnyngangu | 0:61128d697217 | 53 | float refresh_rate = 25.0; // how often to update display (Hz) |
bonnyngangu | 0:61128d697217 | 54 | float g_dt = 2.0F/refresh_rate; // global to store time step (F makes it a float, gets rid of compiler warning) |
bonnyngangu | 0:61128d697217 | 55 | volatile int g_timer_flag = 0; |
bonnyngangu | 0:61128d697217 | 56 | |
bonnyngangu | 0:61128d697217 | 57 | int scoring = 0;// setting the value of starting score. |
bonnyngangu | 1:fe4ac8f10309 | 58 | int m = 42; |
bonnyngangu | 0:61128d697217 | 59 | |
bonnyngangu | 0:61128d697217 | 60 | int main() |
bonnyngangu | 0:61128d697217 | 61 | { |
bonnyngangu | 0:61128d697217 | 62 | |
bonnyngangu | 0:61128d697217 | 63 | wait(1.0); // short delay for power settling |
bonnyngangu | 0:61128d697217 | 64 | lcd.init(); // initialising the display |
bonnyngangu | 1:fe4ac8f10309 | 65 | |
bonnyngangu | 1:fe4ac8f10309 | 66 | // creating introduction screen |
bonnyngangu | 0:61128d697217 | 67 | lcd.printString(" SQUASH!",15,2); |
bonnyngangu | 0:61128d697217 | 68 | lcd.inverseMode(); // invert colours |
bonnyngangu | 0:61128d697217 | 69 | lcd.drawLine(41,0,0,23,1); |
bonnyngangu | 0:61128d697217 | 70 | lcd.drawLine(41,0,83,23,1); |
bonnyngangu | 0:61128d697217 | 71 | lcd.drawLine(0,23,41,47,1); |
bonnyngangu | 0:61128d697217 | 72 | lcd.drawLine(83,23,41,47,1); |
bonnyngangu | 0:61128d697217 | 73 | lcd.refresh(); |
bonnyngangu | 0:61128d697217 | 74 | wait(5.0); |
bonnyngangu | 0:61128d697217 | 75 | lcd.clear(); |
bonnyngangu | 1:fe4ac8f10309 | 76 | |
bonnyngangu | 0:61128d697217 | 77 | lcd.printString("Game starting",5,2); |
bonnyngangu | 0:61128d697217 | 78 | lcd.normalMode(); // normal colour mode |
bonnyngangu | 0:61128d697217 | 79 | lcd.setBrightness(0.5); // put LED backlight on 50% |
bonnyngangu | 0:61128d697217 | 80 | |
bonnyngangu | 1:fe4ac8f10309 | 81 | buzzer.period_ms(1.5); |
bonnyngangu | 0:61128d697217 | 82 | buzzer.pulsewidth_ms(1.9); |
bonnyngangu | 0:61128d697217 | 83 | getPaddle(); |
bonnyngangu | 0:61128d697217 | 84 | wait(3.0); |
bonnyngangu | 2:c1b442f26087 | 85 | lcd.clear(); |
bonnyngangu | 2:c1b442f26087 | 86 | |
bonnyngangu | 2:c1b442f26087 | 87 | // checking the heat on the regulator |
bonnyngangu | 2:c1b442f26087 | 88 | Temp_reading(); |
bonnyngangu | 2:c1b442f26087 | 89 | wait(3.0); |
bonnyngangu | 2:c1b442f26087 | 90 | |
bonnyngangu | 1:fe4ac8f10309 | 91 | init_ball(); |
bonnyngangu | 0:61128d697217 | 92 | |
bonnyngangu | 0:61128d697217 | 93 | // setup ticker |
bonnyngangu | 0:61128d697217 | 94 | game_timer.attach(&game_timer_isr,g_dt); |
bonnyngangu | 0:61128d697217 | 95 | |
bonnyngangu | 0:61128d697217 | 96 | redraw_screen(); // draw initial screen |
bonnyngangu | 1:fe4ac8f10309 | 97 | |
bonnyngangu | 0:61128d697217 | 98 | while (1) { |
bonnyngangu | 0:61128d697217 | 99 | |
bonnyngangu | 1:fe4ac8f10309 | 100 | Squash_Court(); // draw squash court |
bonnyngangu | 1:fe4ac8f10309 | 101 | |
bonnyngangu | 1:fe4ac8f10309 | 102 | // setting the joystick control. |
bonnyngangu | 1:fe4ac8f10309 | 103 | if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
bonnyngangu | 1:fe4ac8f10309 | 104 | joystick.direction = CENTRE; |
bonnyngangu | 1:fe4ac8f10309 | 105 | int m = 42; |
bonnyngangu | 1:fe4ac8f10309 | 106 | lcd.drawLine(m-6,45,m+6,45,1); |
bonnyngangu | 1:fe4ac8f10309 | 107 | lcd.refresh(); |
bonnyngangu | 1:fe4ac8f10309 | 108 | wait(0.05); |
bonnyngangu | 1:fe4ac8f10309 | 109 | lcd.clear(); |
bonnyngangu | 1:fe4ac8f10309 | 110 | } |
bonnyngangu | 1:fe4ac8f10309 | 111 | |
bonnyngangu | 1:fe4ac8f10309 | 112 | else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
bonnyngangu | 1:fe4ac8f10309 | 113 | joystick.direction = LEFT; |
bonnyngangu | 1:fe4ac8f10309 | 114 | int m = 18; |
bonnyngangu | 1:fe4ac8f10309 | 115 | lcd.drawLine(m-6,45,m+6,45,1); |
bonnyngangu | 1:fe4ac8f10309 | 116 | lcd.refresh(); |
bonnyngangu | 1:fe4ac8f10309 | 117 | wait(0.05); |
bonnyngangu | 1:fe4ac8f10309 | 118 | lcd.clear(); |
bonnyngangu | 1:fe4ac8f10309 | 119 | } |
bonnyngangu | 1:fe4ac8f10309 | 120 | |
bonnyngangu | 1:fe4ac8f10309 | 121 | else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
bonnyngangu | 1:fe4ac8f10309 | 122 | joystick.direction = RIGHT; |
bonnyngangu | 1:fe4ac8f10309 | 123 | int m = 66; |
bonnyngangu | 1:fe4ac8f10309 | 124 | lcd.drawLine(m-6,45,m+6,45,1); |
bonnyngangu | 1:fe4ac8f10309 | 125 | //m-=1;// moving a pixel a time. |
bonnyngangu | 1:fe4ac8f10309 | 126 | lcd.refresh(); |
bonnyngangu | 1:fe4ac8f10309 | 127 | wait(0.05); |
bonnyngangu | 1:fe4ac8f10309 | 128 | lcd.clear(); |
bonnyngangu | 1:fe4ac8f10309 | 129 | } |
bonnyngangu | 2:c1b442f26087 | 130 | |
bonnyngangu | 0:61128d697217 | 131 | BallMove(); |
bonnyngangu | 0:61128d697217 | 132 | } |
bonnyngangu | 0:61128d697217 | 133 | } |
bonnyngangu | 0:61128d697217 | 134 | |
bonnyngangu | 0:61128d697217 | 135 | void getPaddle() // setting paddle moving |
bonnyngangu | 0:61128d697217 | 136 | { |
bonnyngangu | 0:61128d697217 | 137 | int m = 42 ; // set paddle into origin 42 on X axis. |
bonnyngangu | 0:61128d697217 | 138 | |
bonnyngangu | 1:fe4ac8f10309 | 139 | // draw a circle x,y,radius,black fill |
bonnyngangu | 1:fe4ac8f10309 | 140 | lcd.drawCircle(42,40,2,1); |
bonnyngangu | 1:fe4ac8f10309 | 141 | |
bonnyngangu | 0:61128d697217 | 142 | // drawing the paddle. |
bonnyngangu | 0:61128d697217 | 143 | lcd.drawLine(m-6,45,m+6,45,1); |
bonnyngangu | 0:61128d697217 | 144 | lcd.refresh(); |
bonnyngangu | 0:61128d697217 | 145 | } |
bonnyngangu | 0:61128d697217 | 146 | |
bonnyngangu | 0:61128d697217 | 147 | void Squash_Court() |
bonnyngangu | 0:61128d697217 | 148 | { |
bonnyngangu | 0:61128d697217 | 149 | |
bonnyngangu | 0:61128d697217 | 150 | lcd.drawRect(0,0,83,47,0); // transparent, just outline |
bonnyngangu | 0:61128d697217 | 151 | lcd.refresh(); // need to refresh screen after drawing rects |
bonnyngangu | 0:61128d697217 | 152 | |
bonnyngangu | 0:61128d697217 | 153 | for (int i = 0; i < 84; i++) { |
bonnyngangu | 0:61128d697217 | 154 | |
bonnyngangu | 0:61128d697217 | 155 | lcd.setPixel(i,24);// at y = 24 pixels (centre) |
bonnyngangu | 0:61128d697217 | 156 | } |
bonnyngangu | 0:61128d697217 | 157 | |
bonnyngangu | 0:61128d697217 | 158 | for (int i = 0; i < 84; i++) { |
bonnyngangu | 0:61128d697217 | 159 | |
bonnyngangu | 0:61128d697217 | 160 | lcd.setPixel(i,16);// // at y = 14 pixels |
bonnyngangu | 0:61128d697217 | 161 | } |
bonnyngangu | 0:61128d697217 | 162 | |
bonnyngangu | 0:61128d697217 | 163 | // need to refresh display after setting pixels |
bonnyngangu | 0:61128d697217 | 164 | lcd.refresh(); |
bonnyngangu | 0:61128d697217 | 165 | |
bonnyngangu | 0:61128d697217 | 166 | } |
bonnyngangu | 0:61128d697217 | 167 | |
bonnyngangu | 0:61128d697217 | 168 | void BallMove() |
bonnyngangu | 0:61128d697217 | 169 | { |
bonnyngangu | 0:61128d697217 | 170 | if ( g_timer_flag ) { // ticker interrupt |
bonnyngangu | 0:61128d697217 | 171 | g_timer_flag = 0; // clear flag |
bonnyngangu | 0:61128d697217 | 172 | update_physics_engine(); |
bonnyngangu | 0:61128d697217 | 173 | check_collisions(); |
bonnyngangu | 0:61128d697217 | 174 | redraw_screen(); |
bonnyngangu | 0:61128d697217 | 175 | } |
bonnyngangu | 0:61128d697217 | 176 | } |
bonnyngangu | 0:61128d697217 | 177 | |
bonnyngangu | 0:61128d697217 | 178 | void redraw_screen() |
bonnyngangu | 0:61128d697217 | 179 | { |
bonnyngangu | 0:61128d697217 | 180 | lcd.clear(); |
bonnyngangu | 0:61128d697217 | 181 | lcd.drawCircle(pos.x,pos.y,BALLRADIUS,1); // x,y,radius,black fill |
bonnyngangu | 0:61128d697217 | 182 | lcd.refresh(); // update display |
bonnyngangu | 0:61128d697217 | 183 | } |
bonnyngangu | 0:61128d697217 | 184 | |
bonnyngangu | 0:61128d697217 | 185 | void check_collisions() |
bonnyngangu | 0:61128d697217 | 186 | { |
bonnyngangu | 0:61128d697217 | 187 | // see if ball has hit the floor (subtract the radius since the position is the centre of the ball) |
bonnyngangu | 0:61128d697217 | 188 | if ( pos.y >= 47 - BALLRADIUS ) { |
bonnyngangu | 0:61128d697217 | 189 | pos.y = 47 - BALLRADIUS; // need to force this or else ball can end up going 'underground' |
bonnyngangu | 0:61128d697217 | 190 | vel.y = -1.0 * vel.y; // y velocity is reflected and dampened |
bonnyngangu | 0:61128d697217 | 191 | // y accleration is still gravity |
bonnyngangu | 0:61128d697217 | 192 | } |
bonnyngangu | 0:61128d697217 | 193 | |
bonnyngangu | 0:61128d697217 | 194 | else if ( pos.y >= 45 - BALLRADIUS) { |
bonnyngangu | 0:61128d697217 | 195 | pos.y = 45 - BALLRADIUS; // need to force this or else ball can end up going 'underground' |
bonnyngangu | 0:61128d697217 | 196 | vel.y = -4.0 * vel.y; // y velocity is reflected and dampened |
bonnyngangu | 0:61128d697217 | 197 | // y accleration is still gravity |
bonnyngangu | 0:61128d697217 | 198 | } |
bonnyngangu | 0:61128d697217 | 199 | |
bonnyngangu | 0:61128d697217 | 200 | // has ball gone off the right-hand side? |
bonnyngangu | 0:61128d697217 | 201 | if ( pos.x >= 83 - BALLRADIUS ) { |
bonnyngangu | 0:61128d697217 | 202 | pos.x = 83 - BALLRADIUS; // need to force this or else ball can end up going off screen |
bonnyngangu | 0:61128d697217 | 203 | vel.x = -1.5 * vel.x; // reflect and damp velocity |
bonnyngangu | 0:61128d697217 | 204 | acc.x = -acc.x; // reflect accleration |
bonnyngangu | 0:61128d697217 | 205 | } |
bonnyngangu | 0:61128d697217 | 206 | |
bonnyngangu | 0:61128d697217 | 207 | // what about the left? |
bonnyngangu | 0:61128d697217 | 208 | if ( pos.x <= BALLRADIUS ) { |
bonnyngangu | 0:61128d697217 | 209 | pos.x = BALLRADIUS; // need to force this or else ball can end up going off screen |
bonnyngangu | 0:61128d697217 | 210 | vel.x = -0.5 * vel.x; // reflect and damp velocity |
bonnyngangu | 0:61128d697217 | 211 | acc.x = -acc.x; // reflect accleration |
bonnyngangu | 0:61128d697217 | 212 | } |
bonnyngangu | 0:61128d697217 | 213 | } |
bonnyngangu | 0:61128d697217 | 214 | |
bonnyngangu | 0:61128d697217 | 215 | void update_physics_engine() |
bonnyngangu | 0:61128d697217 | 216 | { |
bonnyngangu | 0:61128d697217 | 217 | // from Newton's Laws |
bonnyngangu | 0:61128d697217 | 218 | |
bonnyngangu | 0:61128d697217 | 219 | acc.x = 0.9F*acc.x; // reduce a little due to air friction |
bonnyngangu | 0:61128d697217 | 220 | |
bonnyngangu | 0:61128d697217 | 221 | // calc new velocity (assume 'unit' time) |
bonnyngangu | 0:61128d697217 | 222 | vel.x = vel.x + acc.x; // * g_gt; |
bonnyngangu | 0:61128d697217 | 223 | vel.y = vel.y + acc.y; // * g_gt; |
bonnyngangu | 0:61128d697217 | 224 | |
bonnyngangu | 0:61128d697217 | 225 | // calc new position (assume 'unit' time) |
bonnyngangu | 0:61128d697217 | 226 | pos.x = pos.x + vel.x;// * g_gt; |
bonnyngangu | 0:61128d697217 | 227 | pos.y = pos.y + vel.y;// * g_dt; |
bonnyngangu | 0:61128d697217 | 228 | |
bonnyngangu | 0:61128d697217 | 229 | // should really multiply the above by the time-step, |
bonnyngangu | 0:61128d697217 | 230 | // but since the pixel can only be a integer value, |
bonnyngangu | 0:61128d697217 | 231 | // it makes the motion a little 'jumpy'. |
bonnyngangu | 0:61128d697217 | 232 | } |
bonnyngangu | 0:61128d697217 | 233 | |
bonnyngangu | 0:61128d697217 | 234 | void init_ball() |
bonnyngangu | 0:61128d697217 | 235 | { |
bonnyngangu | 0:61128d697217 | 236 | // initial position (top-left) |
bonnyngangu | 0:61128d697217 | 237 | pos.x = BALLRADIUS; |
bonnyngangu | 0:61128d697217 | 238 | pos.y = BALLRADIUS; |
bonnyngangu | 0:61128d697217 | 239 | // initial velocity - still |
bonnyngangu | 0:61128d697217 | 240 | vel.x = 0.0; |
bonnyngangu | 0:61128d697217 | 241 | vel.y = 0.0; |
bonnyngangu | 0:61128d697217 | 242 | // initial acceleration - gravity and a bit of push to right |
bonnyngangu | 0:61128d697217 | 243 | acc.x = 0.5; |
bonnyngangu | 0:61128d697217 | 244 | acc.y = 1.0; // +ve so ball accelerates to bottom of display (top of screen is y=0, bottom is y=47) |
bonnyngangu | 0:61128d697217 | 245 | // should be 9.8, but can play with value to get a 'nice' ball movement |
bonnyngangu | 0:61128d697217 | 246 | } |
bonnyngangu | 0:61128d697217 | 247 | |
bonnyngangu | 1:fe4ac8f10309 | 248 | void calibrateJoystick() |
bonnyngangu | 1:fe4ac8f10309 | 249 | { |
bonnyngangu | 1:fe4ac8f10309 | 250 | button.mode(PullDown); |
bonnyngangu | 1:fe4ac8f10309 | 251 | // must not move during calibration |
bonnyngangu | 1:fe4ac8f10309 | 252 | joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) |
bonnyngangu | 1:fe4ac8f10309 | 253 | joystick.y0 = yPot; |
bonnyngangu | 1:fe4ac8f10309 | 254 | } |
bonnyngangu | 1:fe4ac8f10309 | 255 | void updateJoystick() |
bonnyngangu | 1:fe4ac8f10309 | 256 | { |
bonnyngangu | 1:fe4ac8f10309 | 257 | // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) |
bonnyngangu | 1:fe4ac8f10309 | 258 | joystick.x = xPot - joystick.x0; |
bonnyngangu | 1:fe4ac8f10309 | 259 | joystick.y = yPot - joystick.y0; |
bonnyngangu | 1:fe4ac8f10309 | 260 | // read button state |
bonnyngangu | 1:fe4ac8f10309 | 261 | joystick.buttonjoystick = button; |
bonnyngangu | 1:fe4ac8f10309 | 262 | |
bonnyngangu | 1:fe4ac8f10309 | 263 | // calculate direction depending on x,y values |
bonnyngangu | 1:fe4ac8f10309 | 264 | // tolerance allows a little lee-way in case joystick not exactly in the stated direction |
bonnyngangu | 1:fe4ac8f10309 | 265 | if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
bonnyngangu | 1:fe4ac8f10309 | 266 | joystick.direction = CENTRE; |
bonnyngangu | 1:fe4ac8f10309 | 267 | } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
bonnyngangu | 1:fe4ac8f10309 | 268 | joystick.direction = UP; |
bonnyngangu | 1:fe4ac8f10309 | 269 | } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { |
bonnyngangu | 1:fe4ac8f10309 | 270 | joystick.direction = DOWN; |
bonnyngangu | 1:fe4ac8f10309 | 271 | } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
bonnyngangu | 1:fe4ac8f10309 | 272 | joystick.direction = RIGHT; |
bonnyngangu | 1:fe4ac8f10309 | 273 | } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { |
bonnyngangu | 1:fe4ac8f10309 | 274 | joystick.direction = LEFT; |
bonnyngangu | 1:fe4ac8f10309 | 275 | } else { |
bonnyngangu | 1:fe4ac8f10309 | 276 | joystick.direction = UNKNOWN; |
bonnyngangu | 1:fe4ac8f10309 | 277 | } |
bonnyngangu | 1:fe4ac8f10309 | 278 | |
bonnyngangu | 1:fe4ac8f10309 | 279 | // set flag for printing |
bonnyngangu | 1:fe4ac8f10309 | 280 | printFlag = 1; |
bonnyngangu | 1:fe4ac8f10309 | 281 | } |
bonnyngangu | 1:fe4ac8f10309 | 282 | |
bonnyngangu | 2:c1b442f26087 | 283 | void Temp_reading() |
bonnyngangu | 2:c1b442f26087 | 284 | { |
bonnyngangu | 2:c1b442f26087 | 285 | //lcd.init(); // initialising the display |
bonnyngangu | 2:c1b442f26087 | 286 | // initialise the board and serial port |
bonnyngangu | 2:c1b442f26087 | 287 | init_K64F(); |
bonnyngangu | 2:c1b442f26087 | 288 | init_serial(); |
bonnyngangu | 2:c1b442f26087 | 289 | // call the sensor init method using dot syntax |
bonnyngangu | 2:c1b442f26087 | 290 | tmp102.init(); |
bonnyngangu | 2:c1b442f26087 | 291 | |
bonnyngangu | 2:c1b442f26087 | 292 | |
bonnyngangu | 2:c1b442f26087 | 293 | // can directly print strings at specified co-ordinates |
bonnyngangu | 2:c1b442f26087 | 294 | lcd.printString("Current Temp",0,0); |
bonnyngangu | 2:c1b442f26087 | 295 | |
bonnyngangu | 2:c1b442f26087 | 296 | // read temperature and print over serial port |
bonnyngangu | 2:c1b442f26087 | 297 | float T = tmp102.get_temperature(); |
bonnyngangu | 2:c1b442f26087 | 298 | pc.printf("T = %f K\n",T); |
bonnyngangu | 2:c1b442f26087 | 299 | |
bonnyngangu | 2:c1b442f26087 | 300 | char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) |
bonnyngangu | 2:c1b442f26087 | 301 | // so can display a string of a maximum 14 characters in length |
bonnyngangu | 2:c1b442f26087 | 302 | // or create formatted strings - ensure they aren't more than 14 characters long |
bonnyngangu | 2:c1b442f26087 | 303 | int length = sprintf(buffer,"T = %.2f C",T); // print formatted data to buffer |
bonnyngangu | 2:c1b442f26087 | 304 | // it is important the format specifier ensures the length will fit in the buffer |
bonnyngangu | 2:c1b442f26087 | 305 | if (length <= 14) // if string will fit on display |
bonnyngangu | 2:c1b442f26087 | 306 | lcd.printString(buffer,0,2); |
bonnyngangu | 2:c1b442f26087 | 307 | |
bonnyngangu | 2:c1b442f26087 | 308 | // small delay - 1s to match the update rate of the sensor (1 Hz) |
bonnyngangu | 2:c1b442f26087 | 309 | wait(1.0); |
bonnyngangu | 2:c1b442f26087 | 310 | |
bonnyngangu | 2:c1b442f26087 | 311 | if(T<27){ |
bonnyngangu | 2:c1b442f26087 | 312 | |
bonnyngangu | 2:c1b442f26087 | 313 | g_led = 0; |
bonnyngangu | 2:c1b442f26087 | 314 | lcd.printString("Fan OFF",0,4); |
bonnyngangu | 2:c1b442f26087 | 315 | } |
bonnyngangu | 2:c1b442f26087 | 316 | |
bonnyngangu | 2:c1b442f26087 | 317 | if (T>=30){ |
bonnyngangu | 2:c1b442f26087 | 318 | |
bonnyngangu | 2:c1b442f26087 | 319 | r_led = 0; |
bonnyngangu | 2:c1b442f26087 | 320 | lcd.printString("Fan ON ",0,4); |
bonnyngangu | 2:c1b442f26087 | 321 | } |
bonnyngangu | 2:c1b442f26087 | 322 | } |
bonnyngangu | 2:c1b442f26087 | 323 | |
bonnyngangu | 2:c1b442f26087 | 324 | |
bonnyngangu | 0:61128d697217 | 325 | void game_timer_isr() |
bonnyngangu | 0:61128d697217 | 326 | { |
bonnyngangu | 0:61128d697217 | 327 | g_timer_flag = 1; |
bonnyngangu | 1:fe4ac8f10309 | 328 | } |