Tilt the IMU to stay on the platforms!

Dependencies:   4DGL-uLCD-SE LSM9DS1_Library_cal SDFileSystem mbed wave_player

Fork of uDoodleJump by Arthur Bui

Committer:
abui7
Date:
Wed Mar 16 22:15:00 2016 +0000
Revision:
1:0012059a579a
Parent:
0:78b8bc1cd230
uDoodleJump

Who changed what in which revision?

UserRevisionLine numberNew contents of line
abui7 0:78b8bc1cd230 1 #include "mbed.h"
abui7 0:78b8bc1cd230 2 #include "uLCD_4DGL.h"
abui7 0:78b8bc1cd230 3 #include "LSM9DS1.h"
abui7 0:78b8bc1cd230 4 #include "SongPlayer.h"
abui7 0:78b8bc1cd230 5 #include "SDFileSystem.h"
abui7 0:78b8bc1cd230 6 #include "wave_player.h"
abui7 0:78b8bc1cd230 7
abui7 0:78b8bc1cd230 8 uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin;
abui7 0:78b8bc1cd230 9 SDFileSystem sd(p5, p6, p7, p8, "sd"); // DI, DO, SCK, CS
abui7 0:78b8bc1cd230 10 DigitalIn dummy1(p18); // sets p18 to high Z
abui7 0:78b8bc1cd230 11
abui7 0:78b8bc1cd230 12 float note[2]= {1168.0,0.0};
abui7 0:78b8bc1cd230 13 float duration[2]= {0.2,0.0};
abui7 0:78b8bc1cd230 14
abui7 0:78b8bc1cd230 15 int main()
abui7 0:78b8bc1cd230 16 {
abui7 0:78b8bc1cd230 17 SongPlayer mySpeaker(p21); // PwmOut
abui7 0:78b8bc1cd230 18 LSM9DS1 IMU(p9, p10, 0xD6, 0x3C);
abui7 0:78b8bc1cd230 19 uLCD.background_color(WHITE);
abui7 0:78b8bc1cd230 20 uLCD.cls();
abui7 0:78b8bc1cd230 21 IMU.begin();
abui7 0:78b8bc1cd230 22 if (!IMU.begin()) uLCD.printf("Failed to communicate with LSM9DS1.\n");
abui7 0:78b8bc1cd230 23 int radius=2;
abui7 0:78b8bc1cd230 24 int x=66;
abui7 0:78b8bc1cd230 25 int y=10;
abui7 0:78b8bc1cd230 26 int dir=1;
abui7 0:78b8bc1cd230 27 int u=1;
abui7 0:78b8bc1cd230 28 int bounce_pos=0;
abui7 0:78b8bc1cd230 29 int rect_x1 = floor(105*(rand()/(float(RAND_MAX))));
abui7 0:78b8bc1cd230 30 int rect_x2 = floor(52*(rand()/(float(RAND_MAX))));
abui7 0:78b8bc1cd230 31 int rect_x3 = floor(26*(rand()/(float(RAND_MAX))));
abui7 0:78b8bc1cd230 32 int rect_x4 = floor(13*(rand()/(float(RAND_MAX))));
abui7 0:78b8bc1cd230 33 int rect_y1 = 90;
abui7 0:78b8bc1cd230 34 int rect_y2 = 65;
abui7 0:78b8bc1cd230 35 int rect_y3 = 40;
abui7 0:78b8bc1cd230 36 int rect_y4 = 10;
abui7 0:78b8bc1cd230 37
abui7 0:78b8bc1cd230 38 // Initialize first several platforms
abui7 0:78b8bc1cd230 39 uLCD.filled_rectangle(rect_x1,rect_y1,rect_x1+20,rect_y1+4,GREEN);
abui7 0:78b8bc1cd230 40 uLCD.filled_rectangle(rect_x2,rect_y2,rect_x2+20,rect_y2+4,GREEN);
abui7 0:78b8bc1cd230 41 uLCD.filled_rectangle(rect_x3,rect_y3,rect_x3+20,rect_y3+4,GREEN);
abui7 0:78b8bc1cd230 42 uLCD.filled_rectangle(rect_x4,rect_y4,rect_x4+20,rect_y4+4,GREEN);
abui7 0:78b8bc1cd230 43 while(1) {
abui7 0:78b8bc1cd230 44 wait(0.005);
abui7 0:78b8bc1cd230 45
abui7 0:78b8bc1cd230 46 //erase old ball location
abui7 0:78b8bc1cd230 47 uLCD.filled_circle(x, y, radius, WHITE);
abui7 0:78b8bc1cd230 48
abui7 0:78b8bc1cd230 49 // read in tilt
abui7 0:78b8bc1cd230 50 IMU.readAccel();
abui7 0:78b8bc1cd230 51
abui7 0:78b8bc1cd230 52 // update and draw new ball
abui7 0:78b8bc1cd230 53 x = 66 + 63*IMU.calcAccel(IMU.ax);
abui7 0:78b8bc1cd230 54 if( y<(bounce_pos-36) ) {
abui7 0:78b8bc1cd230 55 dir = -dir;
abui7 0:78b8bc1cd230 56 }
abui7 0:78b8bc1cd230 57 u = abs(floor((y-bounce_pos + 36)/9.0)) + 1;
abui7 0:78b8bc1cd230 58 y = y + dir*u; // u (very roughly) models gravity
abui7 0:78b8bc1cd230 59 uLCD.filled_circle(x, y, radius, RED);
abui7 0:78b8bc1cd230 60
abui7 0:78b8bc1cd230 61 // Bounce event
abui7 0:78b8bc1cd230 62 if( ((x>=rect_x1 && x<=(rect_x1+20) && y>=(rect_y1-1) && y<=(rect_y1+4)) ||
abui7 0:78b8bc1cd230 63 (x>=rect_x2 && x<=(rect_x2+20) && y>=(rect_y2-1) && y<=(rect_y2+4)) ||
abui7 0:78b8bc1cd230 64 (x>=rect_x3 && x<=(rect_x3+20) && y>=(rect_y3-1) && y<=(rect_y3+4)) ||
abui7 0:78b8bc1cd230 65 (x>=rect_x4 && x<=(rect_x4+20) && y>=(rect_y4-1) && y<=(rect_y4+4))) && dir == 1 ) {
abui7 0:78b8bc1cd230 66 mySpeaker.PlaySong(note,duration);
abui7 0:78b8bc1cd230 67 bounce_pos = y;
abui7 0:78b8bc1cd230 68 dir = -dir;
abui7 0:78b8bc1cd230 69 // Erase old platforms
abui7 0:78b8bc1cd230 70 uLCD.filled_rectangle(rect_x1,rect_y1,rect_x1+20,rect_y1+4,WHITE);
abui7 0:78b8bc1cd230 71 uLCD.filled_rectangle(rect_x2,rect_y2,rect_x2+20,rect_y2+4,WHITE);
abui7 0:78b8bc1cd230 72 uLCD.filled_rectangle(rect_x3,rect_y3,rect_x3+20,rect_y3+4,WHITE);
abui7 0:78b8bc1cd230 73 uLCD.filled_rectangle(rect_x4,rect_y4,rect_x4+20,rect_y4+4,WHITE);
abui7 0:78b8bc1cd230 74 // Update and draw new platforms
abui7 0:78b8bc1cd230 75 rect_y1 += 20;
abui7 0:78b8bc1cd230 76 rect_y2 += 20;
abui7 0:78b8bc1cd230 77 rect_y3 += 20;
abui7 0:78b8bc1cd230 78 rect_y4 += 20;
abui7 0:78b8bc1cd230 79 uLCD.filled_rectangle(rect_x1,rect_y1,rect_x1+20,rect_y1+4,GREEN);
abui7 0:78b8bc1cd230 80 uLCD.filled_rectangle(rect_x2,rect_y2,rect_x2+20,rect_y2+4,GREEN);
abui7 0:78b8bc1cd230 81 uLCD.filled_rectangle(rect_x3,rect_y3,rect_x3+20,rect_y3+4,GREEN);
abui7 0:78b8bc1cd230 82 uLCD.filled_rectangle(rect_x4,rect_y4,rect_x4+20,rect_y4+4,GREEN);
abui7 0:78b8bc1cd230 83 }
abui7 0:78b8bc1cd230 84
abui7 0:78b8bc1cd230 85 // Recycle platforms as they come off screen
abui7 0:78b8bc1cd230 86 if( rect_y1 >128) {
abui7 0:78b8bc1cd230 87 rect_y1 = floor(15*(rand()/(float(RAND_MAX))));
abui7 0:78b8bc1cd230 88 rect_x1 = floor(105*(rand()/(float(RAND_MAX))));
abui7 0:78b8bc1cd230 89 uLCD.filled_rectangle(rect_x1,rect_y1,rect_x1+20,rect_y1+4,GREEN);
abui7 0:78b8bc1cd230 90 }
abui7 0:78b8bc1cd230 91 if( rect_y2 >128) {
abui7 0:78b8bc1cd230 92 rect_y2 = floor(15*(rand()/(float(RAND_MAX))));
abui7 0:78b8bc1cd230 93 rect_x2 = floor(105*(rand()/(float(RAND_MAX))));
abui7 0:78b8bc1cd230 94 uLCD.filled_rectangle(rect_x2,rect_y2,rect_x2+20,rect_y2+4,GREEN);
abui7 0:78b8bc1cd230 95 }
abui7 0:78b8bc1cd230 96
abui7 0:78b8bc1cd230 97 if( rect_y3 >128) {
abui7 0:78b8bc1cd230 98 rect_y3 = floor(15*(rand()/(float(RAND_MAX))));
abui7 0:78b8bc1cd230 99 rect_x3 = floor(105*(rand()/(float(RAND_MAX))));
abui7 0:78b8bc1cd230 100 uLCD.filled_rectangle(rect_x3,rect_y3,rect_x3+20,rect_y3+4,GREEN);
abui7 0:78b8bc1cd230 101 }
abui7 0:78b8bc1cd230 102 if( rect_y4 >128) {
abui7 0:78b8bc1cd230 103 rect_y4 = floor(15*(rand()/(float(RAND_MAX))));
abui7 0:78b8bc1cd230 104 rect_x4 = floor(105*(rand()/(float(RAND_MAX))));
abui7 0:78b8bc1cd230 105 uLCD.filled_rectangle(rect_x4,rect_y4,rect_x4+20,rect_y4+4,GREEN);
abui7 0:78b8bc1cd230 106 }
abui7 0:78b8bc1cd230 107
abui7 0:78b8bc1cd230 108 // Ball falls off screen
abui7 0:78b8bc1cd230 109 if(y>128) break;
abui7 0:78b8bc1cd230 110
abui7 0:78b8bc1cd230 111 }
abui7 0:78b8bc1cd230 112
abui7 0:78b8bc1cd230 113 // print GAME OVER
abui7 0:78b8bc1cd230 114 uLCD.cls();
abui7 0:78b8bc1cd230 115 uLCD.color(BLUE);
abui7 0:78b8bc1cd230 116 uLCD.textbackground_color(WHITE);
abui7 0:78b8bc1cd230 117 uLCD.locate(4,4);
abui7 0:78b8bc1cd230 118 uLCD.text_width(2);
abui7 0:78b8bc1cd230 119 uLCD.text_height(2);
abui7 0:78b8bc1cd230 120 uLCD.printf("GAME");
abui7 0:78b8bc1cd230 121 uLCD.locate(2,4);
abui7 0:78b8bc1cd230 122 uLCD.printf("OVER%\n");
abui7 0:78b8bc1cd230 123 // play GAME OVER sound
abui7 0:78b8bc1cd230 124 FILE *wave_file;
abui7 0:78b8bc1cd230 125 DigitalIn dummy2(p21); // set p21 to high Z
abui7 0:78b8bc1cd230 126 AnalogOut DACout(p18); // AnalogOut
abui7 0:78b8bc1cd230 127 wave_player waver(&DACout);
abui7 0:78b8bc1cd230 128 wave_file=fopen("/sd/wavfiles/BOMB.wav","r");
abui7 0:78b8bc1cd230 129 waver.play(wave_file);
abui7 0:78b8bc1cd230 130 fclose(wave_file);
abui7 0:78b8bc1cd230 131 wait(500);
abui7 0:78b8bc1cd230 132 }