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