Rocket threading between missiles game, using touch keypad, audio amp, sd card, ulcd, ulcd sd graphics

Dependencies:   4DGL-uLCD-SE MPR121_Demo SDFileSystem mbed-rtos mbed wave_player

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "SDFileSystem.h"
00004 #include "wave_player.h"
00005 #include "uLCD_4DGL.h"
00006 #include "mpr121.h"
00007 
00008 Mutex lcd_mutex;
00009 
00010 uLCD_4DGL uLCD(p9, p10, p12); // uLCD-144-G2 screen, (serial tx, serial rx, reset pin)
00011 int currentShipX;
00012 float x;
00013 AnalogOut DACout(p18);
00014 wave_player waver(&DACout);
00015 
00016 
00017 //8 missiles 
00018 int missile0 [1][2]; // 1 row 2 col
00019 int missile1 [1][2];
00020 int missile2 [1][2];
00021 int missile3 [1][2];
00022 
00023 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
00024 
00025 // Create the interrupt receiver object on pin 26
00026 InterruptIn interrupt(p26);
00027 // Setup the i2c bus on pins 9 and 10
00028 I2C i2c(p28, p27);
00029 // Setup the Mpr121:
00030 // constructor(i2c object, i2c address of the mpr121)
00031 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
00032 
00033 void fallInterrupt();
00034 void gameover();
00035 void moveShip(void const *args);
00036 void moveMissile0(void const *args);
00037 //void sd_sound(void const *args);
00038 int getRandNum();
00039 
00040 void moveShip(void const *args){
00041         
00042     currentShipX = 64;
00043     lcd_mutex.lock();
00044     uLCD.media_init();
00045     uLCD.set_sector_address(0x0040, 0x4083);
00046     uLCD.display_image(currentShipX, 112);
00047     lcd_mutex.unlock();
00048     
00049     while(1) {
00050         
00051         lcd_mutex.lock();
00052         uLCD.filled_rectangle(currentShipX - 16, 112 , currentShipX , 128 ,WHITE);
00053         uLCD.filled_rectangle(currentShipX + 16, 112 ,currentShipX + 32 , 128 ,WHITE);
00054         uLCD.media_init();
00055         uLCD.set_sector_address(0x0040, 0x4083);
00056         uLCD.display_image(currentShipX, 112);
00057         uLCD.filled_rectangle(currentShipX - 16, 112 , currentShipX , 128 ,WHITE);
00058         uLCD.filled_rectangle(currentShipX + 16, 112 ,currentShipX + 32 , 128 ,WHITE);
00059         lcd_mutex.unlock();
00060         
00061     }
00062     
00063         
00064 }
00065 
00066 //Move Missile 0
00067 void moveMissile0(void const *args) {
00068         Thread::wait(1000);
00069         
00070         missile0[0][0] = getRandNum(); //x coordinate
00071         missile0[0][1] = 0;  //y coordinate
00072         
00073         //draw Missile
00074         lcd_mutex.lock();
00075         uLCD.media_init();
00076         uLCD.set_sector_address(0x0040, 0x4081);
00077         uLCD.display_image(missile0[0][0], missile0[0][1]);
00078         lcd_mutex.unlock();
00079 
00080         while(1) {
00081              
00082              lcd_mutex.lock();
00083              uLCD.filled_rectangle(missile0[0][0], missile0[0][1] ,missile0[0][0]+16 , missile0[0][1]+16 ,WHITE);
00084              uLCD.media_init();
00085              uLCD.set_sector_address(0x0040, 0x4081);
00086              //Reset missile
00087              if (missile0[0][1] == 112) {
00088                 missile0[0][1] = -16;
00089                 missile0[0][0] = getRandNum();    
00090              }
00091              uLCD.display_image(missile0[0][0], missile0[0][1]+16);//Move missile down
00092              lcd_mutex.unlock();
00093              missile0[0][1] = missile0[0][1]+16;//Update new y value
00094              
00095         }
00096 
00097 }
00098 
00099 //Move Missile 1
00100 void moveMissile1(void const *args) {
00101         Thread::wait(2000);
00102         
00103         missile1[0][0] = getRandNum(); //x coordinate
00104         missile1[0][1] = 0;  //y coordinate
00105         
00106         //draw Missile
00107         lcd_mutex.lock();
00108         uLCD.media_init();
00109         uLCD.set_sector_address(0x0040, 0x4081);
00110         uLCD.display_image(missile1[0][0], missile1[0][1]);
00111         lcd_mutex.unlock();
00112 
00113         while(1) {
00114              
00115              lcd_mutex.lock();
00116              uLCD.filled_rectangle(missile1[0][0], missile1[0][1] ,missile1[0][0]+16 , missile1[0][1]+16 ,WHITE);
00117              uLCD.media_init();
00118              uLCD.set_sector_address(0x0040, 0x4081);
00119              //Reset missile
00120              if (missile1[0][1] == 112) {
00121                 missile1[0][1] = -16;
00122                 missile1[0][0] = getRandNum();    
00123              }
00124              uLCD.display_image(missile1[0][0], missile1[0][1]+16);//Move missile down
00125              lcd_mutex.unlock();
00126              missile1[0][1] = missile1[0][1]+16;//Update new y value
00127              
00128         }
00129 
00130 }
00131 
00132 //Move Missile 2
00133 void moveMissile2(void const *args) {
00134         Thread::wait(3000);
00135         
00136         missile2[0][0] = getRandNum(); //x coordinate
00137         missile2[0][1] = 0;  //y coordinate
00138         
00139         //draw Missile
00140         lcd_mutex.lock();
00141         uLCD.media_init();
00142         uLCD.set_sector_address(0x0040, 0x4081);
00143         uLCD.display_image(missile2[0][0], missile2[0][1]);
00144         lcd_mutex.unlock();
00145 
00146         while(1) {
00147              
00148              lcd_mutex.lock();
00149              uLCD.filled_rectangle(missile2[0][0], missile2[0][1] ,missile2[0][0]+16 , missile2[0][1]+16 ,WHITE);
00150              uLCD.media_init();
00151              uLCD.set_sector_address(0x0040, 0x4081);
00152              //Reset missile
00153              if (missile2[0][1] == 112) {
00154                 missile2[0][1] = -16;
00155                 missile2[0][0] = getRandNum();    
00156              }
00157              uLCD.display_image(missile2[0][0], missile2[0][1]+16);//Move missile down
00158              lcd_mutex.unlock();
00159              missile2[0][1] = missile2[0][1]+16;//Update new y value
00160              
00161         }
00162 
00163 }
00164 
00165 //Move Missile 3
00166 void moveMissile3(void const *args) {
00167         Thread::wait(4000);
00168         
00169         missile3[0][0] = getRandNum(); //x coordinate
00170         missile3[0][1] = 0;  //y coordinate
00171         
00172         //draw Missile
00173         lcd_mutex.lock();
00174         uLCD.media_init();
00175         uLCD.set_sector_address(0x0040, 0x4081);
00176         uLCD.display_image(missile3[0][0], missile3[0][1]);
00177         lcd_mutex.unlock();
00178 
00179         while(1) {
00180              
00181              lcd_mutex.lock();
00182              uLCD.filled_rectangle(missile3[0][0], missile3[0][1] ,missile3[0][0]+16 , missile3[0][1]+16 ,WHITE);
00183              uLCD.media_init();
00184              uLCD.set_sector_address(0x0040, 0x4081);
00185              //Reset missile
00186              if (missile3[0][1] == 112) {
00187                 missile3[0][1] = -16;
00188                 missile3[0][0] = getRandNum();    
00189              }
00190              uLCD.display_image(missile3[0][0], missile3[0][1]+16);//Move missile down
00191              lcd_mutex.unlock();
00192              missile3[0][1] = missile3[0][1]+16;//Update new y value
00193              
00194         }
00195 
00196 }
00197 
00198 
00199 // Key hit/release interrupt routine
00200 void fallInterrupt() {
00201   int key_code=0;
00202   int i=0;
00203   int value=mpr121.read(0x00);
00204   value +=mpr121.read(0x01)<<8;
00205   // LED demo mod
00206   i=0;
00207   // puts key number out to LEDs for demo
00208   for (i=0; i<12; i++) {
00209   if (((value>>i)&0x01)==1) key_code=i+1;
00210   }
00211   
00212   if (key_code == 0x4 && currentShipX != 16) {
00213         currentShipX = currentShipX - 16;
00214    }
00215   if (key_code == 0x3 && currentShipX != 96) {
00216         currentShipX = currentShipX + 16;
00217    }      
00218 }
00219 
00220 inline float random_number()
00221 {
00222     return (rand()/(float(RAND_MAX)));
00223 }
00224 
00225 int getRandNum() {
00226         
00227     x = random_number();
00228     x = x * 6;
00229     x = x+1;
00230     
00231     return (((int)x) * 16);
00232     
00233 }
00234 
00235 
00236 void gameover() {
00237     
00238     uLCD.background_color(WHITE);
00239     uLCD.cls();
00240     uLCD.background_color(WHITE);
00241     uLCD.textbackground_color(WHITE);
00242     
00243     uLCD.media_init();
00244     uLCD.set_sector_address(0x0040, 0x4089);
00245     uLCD.display_video(0, 0);
00246     
00247     uLCD.background_color(WHITE);
00248     uLCD.cls();
00249     uLCD.background_color(WHITE);
00250     uLCD.textbackground_color(WHITE);
00251     
00252     uLCD.printf("Gameover");
00253     FILE *wave_file;
00254     wave_file=fopen("/sd/gameover.wav","r");
00255     waver.play(wave_file);
00256     fclose(wave_file);    
00257 }
00258 
00259 int main() {
00260     
00261     interrupt.fall(&fallInterrupt);
00262     interrupt.mode(PullUp);
00263     
00264     uLCD.baudrate(3000000);
00265         
00266     uLCD.background_color(WHITE);
00267     uLCD.cls();
00268     uLCD.background_color(WHITE);
00269     uLCD.textbackground_color(WHITE);
00270     
00271     Thread thread0(moveShip);
00272     Thread thread1(moveMissile0);
00273     Thread thread2(moveMissile1);
00274     Thread thread3(moveMissile2);
00275     Thread thread4(moveMissile3);    
00276     bool keepGoing = true;
00277 
00278       
00279     while(1) {
00280         
00281         if (((currentShipX == missile0[0][0] && missile0[0][1] == 112)|| 
00282             (currentShipX == missile1[0][0] && missile1[0][1]== 112) ||
00283             (currentShipX == missile2[0][0] && missile2[0][1]== 112) || 
00284             (currentShipX == missile3[0][0] && missile3[0][1]== 112)) && 
00285             keepGoing) {
00286                 keepGoing = false;
00287                 thread0.terminate();
00288                 thread1.terminate();
00289                 thread2.terminate();
00290                 thread3.terminate();
00291                 thread4.terminate();
00292                 gameover();
00293         }
00294 
00295     }
00296 }