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@0:61128d697217, 2016-05-06 (annotated)
- Committer:
- bonnyngangu
- Date:
- Fri May 06 17:03:41 2016 +0000
- Revision:
- 0:61128d697217
- Child:
- 1:fe4ac8f10309
Updating joystick direction to affect paddle movement.
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 | 0:61128d697217 | 3 | #include "Joystick.h" |
bonnyngangu | 0:61128d697217 | 4 | #include "SDFileSystem.h" |
bonnyngangu | 0:61128d697217 | 5 | |
bonnyngangu | 0:61128d697217 | 6 | #define BALLRADIUS 2 |
bonnyngangu | 0:61128d697217 | 7 | |
bonnyngangu | 0:61128d697217 | 8 | |
bonnyngangu | 0:61128d697217 | 9 | // VCC, SCE, RST, D/C, MOSI, SCLK, LED |
bonnyngangu | 0:61128d697217 | 10 | N5110 lcd (PTE26 , PTA0 , PTC4 , PTD0 , PTD2 , PTD1 , PTC3); |
bonnyngangu | 0:61128d697217 | 11 | // Can also power (VCC) directly from VOUT (3.3 V) - |
bonnyngangu | 0:61128d697217 | 12 | // Can give better performance due to current limitation from GPIO pin |
bonnyngangu | 0:61128d697217 | 13 | |
bonnyngangu | 0:61128d697217 | 14 | InterruptIn interrupt_button(PTB18); // interrupt in button. |
bonnyngangu | 0:61128d697217 | 15 | PwmOut buzzer(PTA2); // buzzer connection to PwmOut. |
bonnyngangu | 0:61128d697217 | 16 | |
bonnyngangu | 0:61128d697217 | 17 | // Connections to SD card holder on K64F (SPI interface) |
bonnyngangu | 0:61128d697217 | 18 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS |
bonnyngangu | 0:61128d697217 | 19 | |
bonnyngangu | 0:61128d697217 | 20 | void delete_file(char filename[]); |
bonnyngangu | 0:61128d697217 | 21 | |
bonnyngangu | 0:61128d697217 | 22 | // function prototypes |
bonnyngangu | 0:61128d697217 | 23 | void init_ball(); |
bonnyngangu | 0:61128d697217 | 24 | void game_timer_isr(); |
bonnyngangu | 0:61128d697217 | 25 | void BallMove(); |
bonnyngangu | 0:61128d697217 | 26 | void redraw_screen(); |
bonnyngangu | 0:61128d697217 | 27 | void update_physics_engine(); |
bonnyngangu | 0:61128d697217 | 28 | void check_collisions(); |
bonnyngangu | 0:61128d697217 | 29 | void Squash_Court(); |
bonnyngangu | 0:61128d697217 | 30 | void getPaddle(); |
bonnyngangu | 0:61128d697217 | 31 | void PaddleMove(); |
bonnyngangu | 0:61128d697217 | 32 | void updateJoystick(); |
bonnyngangu | 0:61128d697217 | 33 | |
bonnyngangu | 0:61128d697217 | 34 | //bool wall = false; |
bonnyngangu | 0:61128d697217 | 35 | |
bonnyngangu | 0:61128d697217 | 36 | // Can give better performance due to current limitation from GPIO pin |
bonnyngangu | 0:61128d697217 | 37 | Ticker game_timer; |
bonnyngangu | 0:61128d697217 | 38 | |
bonnyngangu | 0:61128d697217 | 39 | // struct used to store 2D vectors |
bonnyngangu | 0:61128d697217 | 40 | typedef struct vector_t vector_t; |
bonnyngangu | 0:61128d697217 | 41 | struct vector_t { |
bonnyngangu | 0:61128d697217 | 42 | float x; |
bonnyngangu | 0:61128d697217 | 43 | float y; |
bonnyngangu | 0:61128d697217 | 44 | }; |
bonnyngangu | 0:61128d697217 | 45 | |
bonnyngangu | 0:61128d697217 | 46 | vector_t pos; // ball position |
bonnyngangu | 0:61128d697217 | 47 | vector_t vel; // ball velocity |
bonnyngangu | 0:61128d697217 | 48 | vector_t acc; // ball acceleration |
bonnyngangu | 0:61128d697217 | 49 | |
bonnyngangu | 0:61128d697217 | 50 | float refresh_rate = 25.0; // how often to update display (Hz) |
bonnyngangu | 0:61128d697217 | 51 | 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 | 52 | volatile int g_timer_flag = 0; |
bonnyngangu | 0:61128d697217 | 53 | |
bonnyngangu | 0:61128d697217 | 54 | int scoring = 0;// setting the value of starting score. |
bonnyngangu | 0:61128d697217 | 55 | int Lhand = 0;// value of the left hand side on y axis. |
bonnyngangu | 0:61128d697217 | 56 | int Rhand = 0;// value of the right hand side on y axis. |
bonnyngangu | 0:61128d697217 | 57 | int m = 42 ; |
bonnyngangu | 0:61128d697217 | 58 | int a; |
bonnyngangu | 0:61128d697217 | 59 | int width;// left hand side width. |
bonnyngangu | 0:61128d697217 | 60 | int width2;/// right hand side width. |
bonnyngangu | 0:61128d697217 | 61 | |
bonnyngangu | 0:61128d697217 | 62 | |
bonnyngangu | 0:61128d697217 | 63 | int main() |
bonnyngangu | 0:61128d697217 | 64 | { |
bonnyngangu | 0:61128d697217 | 65 | |
bonnyngangu | 0:61128d697217 | 66 | wait(1.0); // short delay for power settling |
bonnyngangu | 0:61128d697217 | 67 | lcd.init(); // initialising the display |
bonnyngangu | 0:61128d697217 | 68 | |
bonnyngangu | 0:61128d697217 | 69 | lcd.printString(" SQUASH!",15,2); |
bonnyngangu | 0:61128d697217 | 70 | lcd.inverseMode(); // invert colours |
bonnyngangu | 0:61128d697217 | 71 | lcd.drawLine(41,0,0,23,1); |
bonnyngangu | 0:61128d697217 | 72 | lcd.drawLine(41,0,83,23,1); |
bonnyngangu | 0:61128d697217 | 73 | lcd.drawLine(0,23,41,47,1); |
bonnyngangu | 0:61128d697217 | 74 | lcd.drawLine(83,23,41,47,1); |
bonnyngangu | 0:61128d697217 | 75 | lcd.refresh(); |
bonnyngangu | 0:61128d697217 | 76 | wait(5.0); |
bonnyngangu | 0:61128d697217 | 77 | lcd.clear(); |
bonnyngangu | 0:61128d697217 | 78 | lcd.printString("Game starting",5,2); |
bonnyngangu | 0:61128d697217 | 79 | lcd.normalMode(); // normal colour mode |
bonnyngangu | 0:61128d697217 | 80 | lcd.setBrightness(0.5); // put LED backlight on 50% |
bonnyngangu | 0:61128d697217 | 81 | |
bonnyngangu | 0:61128d697217 | 82 | buzzer.period_ms(2.0); |
bonnyngangu | 0:61128d697217 | 83 | buzzer.pulsewidth_ms(1.9); |
bonnyngangu | 0:61128d697217 | 84 | getPaddle(); |
bonnyngangu | 0:61128d697217 | 85 | wait(3.0); |
bonnyngangu | 0:61128d697217 | 86 | |
bonnyngangu | 0:61128d697217 | 87 | calibrateJoystick(); // get centred values of joystick |
bonnyngangu | 0:61128d697217 | 88 | pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second |
bonnyngangu | 0:61128d697217 | 89 | |
bonnyngangu | 0:61128d697217 | 90 | init_ball(); |
bonnyngangu | 0:61128d697217 | 91 | // setup ticker |
bonnyngangu | 0:61128d697217 | 92 | game_timer.attach(&game_timer_isr,g_dt); |
bonnyngangu | 0:61128d697217 | 93 | |
bonnyngangu | 0:61128d697217 | 94 | redraw_screen(); // draw initial screen |
bonnyngangu | 0:61128d697217 | 95 | |
bonnyngangu | 0:61128d697217 | 96 | while (1) { |
bonnyngangu | 0:61128d697217 | 97 | |
bonnyngangu | 0:61128d697217 | 98 | Squash_Court(); |
bonnyngangu | 0:61128d697217 | 99 | BallMove(); |
bonnyngangu | 0:61128d697217 | 100 | PaddleMove(); |
bonnyngangu | 0:61128d697217 | 101 | } |
bonnyngangu | 0:61128d697217 | 102 | } |
bonnyngangu | 0:61128d697217 | 103 | |
bonnyngangu | 0:61128d697217 | 104 | void getPaddle() // setting paddle moving |
bonnyngangu | 0:61128d697217 | 105 | { |
bonnyngangu | 0:61128d697217 | 106 | int m = 42 ; // set paddle into origin 42 on X axis. |
bonnyngangu | 0:61128d697217 | 107 | |
bonnyngangu | 0:61128d697217 | 108 | // drawing the paddle. |
bonnyngangu | 0:61128d697217 | 109 | lcd.drawLine(m-6,45,m+6,45,1); |
bonnyngangu | 0:61128d697217 | 110 | lcd.refresh(); |
bonnyngangu | 0:61128d697217 | 111 | } |
bonnyngangu | 0:61128d697217 | 112 | |
bonnyngangu | 0:61128d697217 | 113 | |
bonnyngangu | 0:61128d697217 | 114 | void PaddleMove() |
bonnyngangu | 0:61128d697217 | 115 | { |
bonnyngangu | 0:61128d697217 | 116 | // setting the joystick control. |
bonnyngangu | 0:61128d697217 | 117 | if (joystick.direction == RIGHT && m > 4) { |
bonnyngangu | 0:61128d697217 | 118 | //scoring + = 1;// one pixel move for 1 score. |
bonnyngangu | 0:61128d697217 | 119 | lcd.drawLine(m-6,45,m+6,45,1); |
bonnyngangu | 0:61128d697217 | 120 | m-=1;// moving a pixel a time. |
bonnyngangu | 0:61128d697217 | 121 | lcd.refresh(); |
bonnyngangu | 0:61128d697217 | 122 | } |
bonnyngangu | 0:61128d697217 | 123 | |
bonnyngangu | 0:61128d697217 | 124 | if (joystick.direction == LEFT && m < 78) { |
bonnyngangu | 0:61128d697217 | 125 | //scoring + = 1;// one pixel move for 1 score. |
bonnyngangu | 0:61128d697217 | 126 | lcd.drawLine(m-6,45,m+6,45,1); |
bonnyngangu | 0:61128d697217 | 127 | m+=1;// moving a pixel a time. |
bonnyngangu | 0:61128d697217 | 128 | |
bonnyngangu | 0:61128d697217 | 129 | lcd.drawLine(m-6,45,m+6,45,1); |
bonnyngangu | 0:61128d697217 | 130 | lcd.refresh(); |
bonnyngangu | 0:61128d697217 | 131 | } |
bonnyngangu | 0:61128d697217 | 132 | } |
bonnyngangu | 0:61128d697217 | 133 | |
bonnyngangu | 0:61128d697217 | 134 | void Squash_Court() |
bonnyngangu | 0:61128d697217 | 135 | { |
bonnyngangu | 0:61128d697217 | 136 | |
bonnyngangu | 0:61128d697217 | 137 | lcd.drawRect(0,0,83,47,0); // transparent, just outline |
bonnyngangu | 0:61128d697217 | 138 | lcd.refresh(); // need to refresh screen after drawing rects |
bonnyngangu | 0:61128d697217 | 139 | |
bonnyngangu | 0:61128d697217 | 140 | for (int i = 0; i < 84; i++) { |
bonnyngangu | 0:61128d697217 | 141 | |
bonnyngangu | 0:61128d697217 | 142 | lcd.setPixel(i,24);// at y = 24 pixels (centre) |
bonnyngangu | 0:61128d697217 | 143 | } |
bonnyngangu | 0:61128d697217 | 144 | |
bonnyngangu | 0:61128d697217 | 145 | for (int i = 0; i < 84; i++) { |
bonnyngangu | 0:61128d697217 | 146 | |
bonnyngangu | 0:61128d697217 | 147 | lcd.setPixel(i,16);// // at y = 14 pixels |
bonnyngangu | 0:61128d697217 | 148 | } |
bonnyngangu | 0:61128d697217 | 149 | |
bonnyngangu | 0:61128d697217 | 150 | // need to refresh display after setting pixels |
bonnyngangu | 0:61128d697217 | 151 | lcd.refresh(); |
bonnyngangu | 0:61128d697217 | 152 | |
bonnyngangu | 0:61128d697217 | 153 | } |
bonnyngangu | 0:61128d697217 | 154 | |
bonnyngangu | 0:61128d697217 | 155 | int SD_card() |
bonnyngangu | 0:61128d697217 | 156 | { |
bonnyngangu | 0:61128d697217 | 157 | serial.baud(115200); // full-speed! |
bonnyngangu | 0:61128d697217 | 158 | serial.printf("#### SD Card Example #####\n"); |
bonnyngangu | 0:61128d697217 | 159 | FILE *fp; // this is our file pointer |
bonnyngangu | 0:61128d697217 | 160 | wait(1); |
bonnyngangu | 0:61128d697217 | 161 | |
bonnyngangu | 0:61128d697217 | 162 | // open file for writing ('w') - creates file if it doesn't exist and overwrites |
bonnyngangu | 0:61128d697217 | 163 | // if it does. If you wish to add a score onto a list, then you can |
bonnyngangu | 0:61128d697217 | 164 | // append instead 'a'. This will open the file if it exists and start |
bonnyngangu | 0:61128d697217 | 165 | // writing at the end. It will create the file if it doesn't exist. |
bonnyngangu | 0:61128d697217 | 166 | fp = fopen("/sd/topscore.txt", "w"); |
bonnyngangu | 0:61128d697217 | 167 | int top_score = 56; // random example |
bonnyngangu | 0:61128d697217 | 168 | |
bonnyngangu | 0:61128d697217 | 169 | if (fp == NULL) { // if it can't open the file then print error message |
bonnyngangu | 0:61128d697217 | 170 | serial.printf("Error! Unable to open file!\n"); |
bonnyngangu | 0:61128d697217 | 171 | } else { // opened file so can write |
bonnyngangu | 0:61128d697217 | 172 | serial.printf("Writing to file...."); |
bonnyngangu | 0:61128d697217 | 173 | fprintf(fp, "%d",top_score); // ensure data type matches |
bonnyngangu | 0:61128d697217 | 174 | serial.printf("Done.\n"); |
bonnyngangu | 0:61128d697217 | 175 | fclose(fp); // ensure you close the file after writing |
bonnyngangu | 0:61128d697217 | 176 | } |
bonnyngangu | 0:61128d697217 | 177 | |
bonnyngangu | 0:61128d697217 | 178 | // now open file for reading |
bonnyngangu | 0:61128d697217 | 179 | fp = fopen("/sd/topscore.txt", "r"); |
bonnyngangu | 0:61128d697217 | 180 | int stored_top_score = -1; // -1 to demonstrate it has changed after reading |
bonnyngangu | 0:61128d697217 | 181 | |
bonnyngangu | 0:61128d697217 | 182 | if (fp == NULL) { // if it can't open the file then print error message |
bonnyngangu | 0:61128d697217 | 183 | serial.printf("Error! Unable to open file!\n"); |
bonnyngangu | 0:61128d697217 | 184 | } else { // opened file so can write |
bonnyngangu | 0:61128d697217 | 185 | fscanf(fp, "%d",&stored_top_score); // ensure data type matches - note address operator (&) |
bonnyngangu | 0:61128d697217 | 186 | serial.printf("Read %d from file.\n",stored_top_score); |
bonnyngangu | 0:61128d697217 | 187 | fclose(fp); // ensure you close the file after reading |
bonnyngangu | 0:61128d697217 | 188 | } |
bonnyngangu | 0:61128d697217 | 189 | |
bonnyngangu | 0:61128d697217 | 190 | // for this example, I'll create some numbers to write to file in a big list |
bonnyngangu | 0:61128d697217 | 191 | // a data logger for example will usually append to a file - at a reading |
bonnyngangu | 0:61128d697217 | 192 | // at the end rather than creating a new file |
bonnyngangu | 0:61128d697217 | 193 | fp = fopen("/sd/test.txt", "a"); |
bonnyngangu | 0:61128d697217 | 194 | |
bonnyngangu | 0:61128d697217 | 195 | if (fp == NULL) { // if it can't open the file then print error message |
bonnyngangu | 0:61128d697217 | 196 | serial.printf("Error! Unable to open file!\n"); |
bonnyngangu | 0:61128d697217 | 197 | } else { // opened file so can write |
bonnyngangu | 0:61128d697217 | 198 | serial.printf("Writing to file...."); |
bonnyngangu | 0:61128d697217 | 199 | for(int i = 1; i <= 50; i++) { |
bonnyngangu | 0:61128d697217 | 200 | float dummy = 1000.0F/i; // dummy variable |
bonnyngangu | 0:61128d697217 | 201 | fprintf(fp, "%d,%f\n",i,dummy); // print formatted string to file (CSV) |
bonnyngangu | 0:61128d697217 | 202 | } |
bonnyngangu | 0:61128d697217 | 203 | serial.printf("Done.\n"); |
bonnyngangu | 0:61128d697217 | 204 | fclose(fp); // ensure you close the file after writing |
bonnyngangu | 0:61128d697217 | 205 | } |
bonnyngangu | 0:61128d697217 | 206 | |
bonnyngangu | 0:61128d697217 | 207 | // you can comment out the writing example to check that the writing has |
bonnyngangu | 0:61128d697217 | 208 | // worked - when you run it after commenting, it should still open the |
bonnyngangu | 0:61128d697217 | 209 | // file that exists on the SD card - assuming you didn't delete it! |
bonnyngangu | 0:61128d697217 | 210 | |
bonnyngangu | 0:61128d697217 | 211 | // now open file for reading...note the 'r' |
bonnyngangu | 0:61128d697217 | 212 | fp = fopen("/sd/test.txt", "r"); |
bonnyngangu | 0:61128d697217 | 213 | if (fp == NULL) { // if it can't open the file then print error message |
bonnyngangu | 0:61128d697217 | 214 | serial.printf("Error! Unable to open file!\n"); |
bonnyngangu | 0:61128d697217 | 215 | } else { |
bonnyngangu | 0:61128d697217 | 216 | serial.printf("Reading file....\n"); |
bonnyngangu | 0:61128d697217 | 217 | int i; // create suitable variables to store the data in the file |
bonnyngangu | 0:61128d697217 | 218 | float value; |
bonnyngangu | 0:61128d697217 | 219 | |
bonnyngangu | 0:61128d697217 | 220 | // in this example, we keep reading (using fscanf) until we reach |
bonnyngangu | 0:61128d697217 | 221 | // the 'end of file'. Note we use the address operator & to write |
bonnyngangu | 0:61128d697217 | 222 | // to the variables. Also the format of the string must match what |
bonnyngangu | 0:61128d697217 | 223 | // is in the file |
bonnyngangu | 0:61128d697217 | 224 | while (fscanf(fp, "%d,%f", &i, &value) != EOF) { |
bonnyngangu | 0:61128d697217 | 225 | serial.printf("%d,%f\n",i,value); |
bonnyngangu | 0:61128d697217 | 226 | } |
bonnyngangu | 0:61128d697217 | 227 | serial.printf("Done.\n"); |
bonnyngangu | 0:61128d697217 | 228 | fclose(fp); // ensure you close the file after reading |
bonnyngangu | 0:61128d697217 | 229 | } |
bonnyngangu | 0:61128d697217 | 230 | |
bonnyngangu | 0:61128d697217 | 231 | // the previous example just read the values into variables and printed to |
bonnyngangu | 0:61128d697217 | 232 | // serial, we'll now read files into an array. |
bonnyngangu | 0:61128d697217 | 233 | |
bonnyngangu | 0:61128d697217 | 234 | // now open file for reading...note the 'r' |
bonnyngangu | 0:61128d697217 | 235 | fp = fopen("/sd/test.txt", "r"); |
bonnyngangu | 0:61128d697217 | 236 | |
bonnyngangu | 0:61128d697217 | 237 | int n=0; // going to store the number of lines in the file |
bonnyngangu | 0:61128d697217 | 238 | int *index_array; // pointers to create dynamic arrays later |
bonnyngangu | 0:61128d697217 | 239 | float *value_array; // note memory will be in heap rather than on the stack |
bonnyngangu | 0:61128d697217 | 240 | |
bonnyngangu | 0:61128d697217 | 241 | if (fp == NULL) { // if it can't open the file then print error message |
bonnyngangu | 0:61128d697217 | 242 | serial.printf("Error! Unable to open file!\n"); |
bonnyngangu | 0:61128d697217 | 243 | } else { |
bonnyngangu | 0:61128d697217 | 244 | serial.printf("Counting lines in file....\n"); |
bonnyngangu | 0:61128d697217 | 245 | //Since we may not know the |
bonnyngangu | 0:61128d697217 | 246 | // number of lines in the files ahead of time, we'll first count them |
bonnyngangu | 0:61128d697217 | 247 | // * means scan but don't save |
bonnyngangu | 0:61128d697217 | 248 | while (fscanf(fp, "%*d,%*f") != EOF) { |
bonnyngangu | 0:61128d697217 | 249 | n++; // increment counter when read a line |
bonnyngangu | 0:61128d697217 | 250 | } |
bonnyngangu | 0:61128d697217 | 251 | |
bonnyngangu | 0:61128d697217 | 252 | |
bonnyngangu | 0:61128d697217 | 253 | serial.printf("Read %d lines\n",n); |
bonnyngangu | 0:61128d697217 | 254 | serial.printf("Creating dynamic arrays...\n"); |
bonnyngangu | 0:61128d697217 | 255 | // calloc creates an array and initilises to 0 |
bonnyngangu | 0:61128d697217 | 256 | // malloc returns unitialised array - diffrent syntax |
bonnyngangu | 0:61128d697217 | 257 | index_array = (int *)calloc(n, sizeof (int)); |
bonnyngangu | 0:61128d697217 | 258 | value_array = (float *)calloc(n, sizeof (float)); |
bonnyngangu | 0:61128d697217 | 259 | |
bonnyngangu | 0:61128d697217 | 260 | int i=0; |
bonnyngangu | 0:61128d697217 | 261 | rewind(fp); // 'scrolled' to end of file, so go back to beginning |
bonnyngangu | 0:61128d697217 | 262 | serial.printf("Reading into arrays...\n"); |
bonnyngangu | 0:61128d697217 | 263 | while (fscanf(fp, "%d,%f",&index_array[i],&value_array[i]) != EOF) { |
bonnyngangu | 0:61128d697217 | 264 | i++; // read data into array and increment index |
bonnyngangu | 0:61128d697217 | 265 | } |
bonnyngangu | 0:61128d697217 | 266 | serial.printf("Done.\n"); |
bonnyngangu | 0:61128d697217 | 267 | fclose(fp); // ensure you close the file after reading |
bonnyngangu | 0:61128d697217 | 268 | } |
bonnyngangu | 0:61128d697217 | 269 | |
bonnyngangu | 0:61128d697217 | 270 | // we should now have the data in the arrays, will print to serial to check |
bonnyngangu | 0:61128d697217 | 271 | for(int i=0; i<n ; i++) { |
bonnyngangu | 0:61128d697217 | 272 | serial.printf("[%d] %d,%f\n",i,index_array[i],value_array[i]); |
bonnyngangu | 0:61128d697217 | 273 | } |
bonnyngangu | 0:61128d697217 | 274 | |
bonnyngangu | 0:61128d697217 | 275 | serial.printf("End of SD card example\n"); |
bonnyngangu | 0:61128d697217 | 276 | |
bonnyngangu | 0:61128d697217 | 277 | return 0; |
bonnyngangu | 0:61128d697217 | 278 | |
bonnyngangu | 0:61128d697217 | 279 | } |
bonnyngangu | 0:61128d697217 | 280 | |
bonnyngangu | 0:61128d697217 | 281 | void delete_file(char filename[]) |
bonnyngangu | 0:61128d697217 | 282 | { |
bonnyngangu | 0:61128d697217 | 283 | serial.printf("Deleting file '%s'...",filename); |
bonnyngangu | 0:61128d697217 | 284 | FILE *fp = fopen(filename, "r"); // try and open file |
bonnyngangu | 0:61128d697217 | 285 | if (fp != NULL) { // if it does open... |
bonnyngangu | 0:61128d697217 | 286 | fclose(fp); // close it |
bonnyngangu | 0:61128d697217 | 287 | remove(filename); // and then delete |
bonnyngangu | 0:61128d697217 | 288 | serial.printf("Done!\n"); |
bonnyngangu | 0:61128d697217 | 289 | } |
bonnyngangu | 0:61128d697217 | 290 | // if we can't open it, it doesn't exist and so we can't delete it |
bonnyngangu | 0:61128d697217 | 291 | } |
bonnyngangu | 0:61128d697217 | 292 | |
bonnyngangu | 0:61128d697217 | 293 | void BallMove() |
bonnyngangu | 0:61128d697217 | 294 | { |
bonnyngangu | 0:61128d697217 | 295 | if ( g_timer_flag ) { // ticker interrupt |
bonnyngangu | 0:61128d697217 | 296 | g_timer_flag = 0; // clear flag |
bonnyngangu | 0:61128d697217 | 297 | update_physics_engine(); |
bonnyngangu | 0:61128d697217 | 298 | check_collisions(); |
bonnyngangu | 0:61128d697217 | 299 | redraw_screen(); |
bonnyngangu | 0:61128d697217 | 300 | } |
bonnyngangu | 0:61128d697217 | 301 | } |
bonnyngangu | 0:61128d697217 | 302 | |
bonnyngangu | 0:61128d697217 | 303 | void redraw_screen() |
bonnyngangu | 0:61128d697217 | 304 | { |
bonnyngangu | 0:61128d697217 | 305 | lcd.clear(); |
bonnyngangu | 0:61128d697217 | 306 | lcd.drawCircle(pos.x,pos.y,BALLRADIUS,1); // x,y,radius,black fill |
bonnyngangu | 0:61128d697217 | 307 | lcd.refresh(); // update display |
bonnyngangu | 0:61128d697217 | 308 | } |
bonnyngangu | 0:61128d697217 | 309 | |
bonnyngangu | 0:61128d697217 | 310 | void check_collisions() |
bonnyngangu | 0:61128d697217 | 311 | { |
bonnyngangu | 0:61128d697217 | 312 | // see if ball has hit the floor (subtract the radius since the position is the centre of the ball) |
bonnyngangu | 0:61128d697217 | 313 | if ( pos.y >= 47 - BALLRADIUS ) { |
bonnyngangu | 0:61128d697217 | 314 | pos.y = 47 - BALLRADIUS; // need to force this or else ball can end up going 'underground' |
bonnyngangu | 0:61128d697217 | 315 | vel.y = -1.0 * vel.y; // y velocity is reflected and dampened |
bonnyngangu | 0:61128d697217 | 316 | // y accleration is still gravity |
bonnyngangu | 0:61128d697217 | 317 | } |
bonnyngangu | 0:61128d697217 | 318 | |
bonnyngangu | 0:61128d697217 | 319 | else if ( pos.y >= 45 - BALLRADIUS) { |
bonnyngangu | 0:61128d697217 | 320 | pos.y = 45 - BALLRADIUS; // need to force this or else ball can end up going 'underground' |
bonnyngangu | 0:61128d697217 | 321 | vel.y = -4.0 * vel.y; // y velocity is reflected and dampened |
bonnyngangu | 0:61128d697217 | 322 | // y accleration is still gravity |
bonnyngangu | 0:61128d697217 | 323 | } |
bonnyngangu | 0:61128d697217 | 324 | |
bonnyngangu | 0:61128d697217 | 325 | // has ball gone off the right-hand side? |
bonnyngangu | 0:61128d697217 | 326 | if ( pos.x >= 83 - BALLRADIUS ) { |
bonnyngangu | 0:61128d697217 | 327 | pos.x = 83 - BALLRADIUS; // need to force this or else ball can end up going off screen |
bonnyngangu | 0:61128d697217 | 328 | vel.x = -1.5 * vel.x; // reflect and damp velocity |
bonnyngangu | 0:61128d697217 | 329 | acc.x = -acc.x; // reflect accleration |
bonnyngangu | 0:61128d697217 | 330 | } |
bonnyngangu | 0:61128d697217 | 331 | |
bonnyngangu | 0:61128d697217 | 332 | // what about the left? |
bonnyngangu | 0:61128d697217 | 333 | if ( pos.x <= BALLRADIUS ) { |
bonnyngangu | 0:61128d697217 | 334 | pos.x = BALLRADIUS; // need to force this or else ball can end up going off screen |
bonnyngangu | 0:61128d697217 | 335 | vel.x = -0.5 * vel.x; // reflect and damp velocity |
bonnyngangu | 0:61128d697217 | 336 | acc.x = -acc.x; // reflect accleration |
bonnyngangu | 0:61128d697217 | 337 | } |
bonnyngangu | 0:61128d697217 | 338 | |
bonnyngangu | 0:61128d697217 | 339 | } |
bonnyngangu | 0:61128d697217 | 340 | |
bonnyngangu | 0:61128d697217 | 341 | void update_physics_engine() |
bonnyngangu | 0:61128d697217 | 342 | { |
bonnyngangu | 0:61128d697217 | 343 | // from Newton's Laws |
bonnyngangu | 0:61128d697217 | 344 | |
bonnyngangu | 0:61128d697217 | 345 | acc.x = 0.9F*acc.x; // reduce a little due to air friction |
bonnyngangu | 0:61128d697217 | 346 | |
bonnyngangu | 0:61128d697217 | 347 | // calc new velocity (assume 'unit' time) |
bonnyngangu | 0:61128d697217 | 348 | vel.x = vel.x + acc.x; // * g_gt; |
bonnyngangu | 0:61128d697217 | 349 | vel.y = vel.y + acc.y; // * g_gt; |
bonnyngangu | 0:61128d697217 | 350 | |
bonnyngangu | 0:61128d697217 | 351 | // calc new position (assume 'unit' time) |
bonnyngangu | 0:61128d697217 | 352 | pos.x = pos.x + vel.x;// * g_gt; |
bonnyngangu | 0:61128d697217 | 353 | pos.y = pos.y + vel.y;// * g_dt; |
bonnyngangu | 0:61128d697217 | 354 | |
bonnyngangu | 0:61128d697217 | 355 | // should really multiply the above by the time-step, |
bonnyngangu | 0:61128d697217 | 356 | // but since the pixel can only be a integer value, |
bonnyngangu | 0:61128d697217 | 357 | // it makes the motion a little 'jumpy'. |
bonnyngangu | 0:61128d697217 | 358 | |
bonnyngangu | 0:61128d697217 | 359 | } |
bonnyngangu | 0:61128d697217 | 360 | |
bonnyngangu | 0:61128d697217 | 361 | void init_ball() |
bonnyngangu | 0:61128d697217 | 362 | { |
bonnyngangu | 0:61128d697217 | 363 | // initial position (top-left) |
bonnyngangu | 0:61128d697217 | 364 | pos.x = BALLRADIUS; |
bonnyngangu | 0:61128d697217 | 365 | pos.y = BALLRADIUS; |
bonnyngangu | 0:61128d697217 | 366 | // initial velocity - still |
bonnyngangu | 0:61128d697217 | 367 | vel.x = 0.0; |
bonnyngangu | 0:61128d697217 | 368 | vel.y = 0.0; |
bonnyngangu | 0:61128d697217 | 369 | // initial acceleration - gravity and a bit of push to right |
bonnyngangu | 0:61128d697217 | 370 | acc.x = 0.5; |
bonnyngangu | 0:61128d697217 | 371 | 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 | 372 | // should be 9.8, but can play with value to get a 'nice' ball movement |
bonnyngangu | 0:61128d697217 | 373 | } |
bonnyngangu | 0:61128d697217 | 374 | |
bonnyngangu | 0:61128d697217 | 375 | void game_timer_isr() |
bonnyngangu | 0:61128d697217 | 376 | { |
bonnyngangu | 0:61128d697217 | 377 | g_timer_flag = 1; |
bonnyngangu | 0:61128d697217 | 378 | } |