Taylor Nichols / Mbed 2 deprecated StrikerShootout

Dependencies:   4DGL-uLCD-SE DebounceIn LSM9DS1_Library_cal SDFileSystem TextLCD mbed-rtos mbed wave_player_appbd

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <time.h>      
00004 #include "mbed.h"
00005 #include "rtos.h"
00006 #include "DebounceIn.h"
00007 #include "TextLCD.h"
00008 #include "LSM9DS1.h"
00009 #include "SDFileSystem.h"
00010 #include "uLCD_4DGL.h"
00011 #include "wave_player.h"
00012 #include "Nav_Switch.h"
00013 
00014 #define WAV_FILE "/sd/music_cropped/Game music.wav"
00015 
00016 #define LOG( ... ) pc.printf( __VA_ARGS__ ); 
00017 
00018 //  LEDs on mbed
00019 BusOut led(LED1,LED2,LED3,LED4);
00020 
00021 //  debugging via pc 
00022 Serial pc( USBTX , USBRX );
00023 
00024 //  SD card
00025 SDFileSystem sd( p5 , p6 , p7 , p8 , "sd" );
00026 
00027 //  text display ( rs , e , d4-d7 )
00028 //TextLCD txt( p22 , p23 , p24 , p25 , p26 , p27 );
00029 
00030 //  LCD ( tx , rx , reset )
00031 uLCD_4DGL lcd( p28 , p27 , p30 ); 
00032 
00033 //  speaker
00034 AnalogOut DACout( p18 );
00035 PwmOut PWMout( p26 );
00036 wave_player waver( & DACout , & PWMout );
00037 
00038 //  pushbutton 
00039 DebounceIn pb( p15 );
00040 
00041 //  IMU ( sda , scl , ... )
00042 LSM9DS1 imu( p9 , p10 , 0xD6 , 0x3C );
00043 
00044 // 
00045 Nav_Switch nav( p21 , p22 , p13 , p12 , p23 );
00046 
00047 class Bullet;
00048 
00049 void music_thread( void const * args ) {
00050     while( 1 ) {
00051         FILE *fp = fopen( WAV_FILE , "r");
00052         if ( fp ) {
00053 //            LOG( ">>> Opened .wav file '%s'.\n" , WAV_FILE )
00054             waver.play( fp );
00055 //            LOG( ">>> Played .wav file '%s'.\n" , WAV_FILE )
00056             fclose( fp );
00057 //            LOG( ">>> Closed .wav file '%s'.\n" , WAV_FILE )
00058         } else {
00059 //            LOG( "ERROR: could not open the .wav file.\n" );     
00060         }    
00061 //        Thread::wait( 120 );
00062     }
00063 }
00064 
00065 class Shield
00066 {
00067     friend Bullet ;
00068     static int const height = 5 ;
00069     static int const color = RED ;
00070 public:
00071     Shield( int x , int dx , int y , int dy ) :
00072         _x( x ) , _dx( dx ) , _y( y ) , _dy( dy ) , _speed( 9 ) , _base( 15 ) , _dbase( 0 )
00073     {
00074     }
00075     void read( )
00076     {
00077         if ( nav.right( ) )
00078         {
00079             _dx = + _speed ;
00080         } else if ( nav.left( ) )
00081         {
00082             _dx = - _speed ;
00083         } else
00084         {
00085             _dx = 0 ;
00086         }
00087     }
00088     void draw( )
00089     {
00090         lcd.filled_rectangle(
00091             _x - _base , _y , 
00092             _x + _base , _y + height,
00093             BLACK
00094         );     
00095         _x += _dx;
00096         if ( _dbase )
00097         {
00098             _base += _dbase ;
00099             if ( _speed <= 1 )
00100             {
00101                 pc.printf("Shield WINS\n") ;
00102             }
00103             else pc.printf("Shield speed: %d\n" , _speed);
00104             _dbase = 0 ;
00105             _x = 128/2 ;
00106         }
00107         else 
00108         {
00109             if ( _x + _base >= 127 )
00110             {
00111                 _x = 127 - _base ;
00112                 _dx = 0 ;
00113             }
00114             else if ( _x - _base <= 0 )
00115             {
00116                 _x = 0 + _base ;
00117                 _dx = 0 ;
00118             }
00119         }
00120         lcd.filled_rectangle(
00121             _x - _base , _y , 
00122             _x + _base , _y + height,
00123             color
00124         );     
00125     }
00126 private:
00127     // 
00128     int _x ;
00129     int _dx ; 
00130     // 
00131     int _y ;
00132     int _dy ;
00133     // 
00134     int _speed ; 
00135     int _base ;
00136     int _dbase ;
00137 };
00138 
00139 class Bullet
00140 {
00141     static int const base = 1 ;
00142     static int const height = 3 ;
00143     static int const color = WHITE ;
00144     
00145 public:
00146     Bullet( int x , int dx , int y , int dy , bool off ) :
00147         _x( x ) , _dx( dx ) , _y( y ) , _dy( dy ) , _off( off )
00148     {
00149     }
00150     bool off( ) const { return _off ; }
00151     void shoot( int x , int dx , int y , int dy )
00152     {
00153         _off = false ;
00154         _x = x ; _dx = dx ; _y = y ; _dy = dy ;
00155     }
00156     void draw( Shield & shield )
00157     {
00158         if ( _off )
00159             return ;
00160         lcd.filled_rectangle(
00161             _x - base , _y - height , 
00162             _x + base , _y ,
00163             BLACK
00164         );     
00165         _x += _dx;
00166         _y += _dy;
00167         if ( _x + base >= 127 )
00168         {
00169             _x = 127 - base ;
00170             _dx = - (_dx / 2);
00171         }
00172         else if ( _x - base <= 0 )
00173         {
00174             _x = 0 + base ;
00175             _dx = - _dx / 2;
00176         }
00177         if ( _y - height <= 0)
00178         {
00179             _off = true ;
00180             lcd.media_init();
00181             lcd.set_sector_address(0x003A,0x7C01);
00182             lcd.display_image(0,0);
00183             Thread::wait(1000);
00184             lcd.filled_rectangle(0 ,0 ,128 ,128,BLACK);
00185             _y = 128 ;
00186             return ;
00187         } 
00188         else if ( _y - height < shield.height )
00189         {
00190             pc.printf("( _y - height < shield.height )\n");
00191             if ( ( _x <= shield._x + shield._base )
00192             && ( _x >= shield._x - shield._base ) )
00193             {
00194                 pc.printf("COLLISION\n");
00195                 // collision
00196                 
00197                 shield._dbase = 5 ;
00198                 shield._speed -= 1 ; 
00199                 _off = true ;
00200                 _y = 127 ;
00201                 if ( shield._base > 126/2 )
00202                 {
00203                     shield._base = 15 ;
00204                     shield._dbase = 0 ;
00205                     shield._speed = 9 ; 
00206                     lcd.media_init();
00207                     lcd.set_sector_address(0x003A,0x7C42);
00208                     lcd.display_image(0,0);
00209                     Thread::wait(1000);
00210                     lcd.filled_rectangle(0 ,0 ,128 ,128,BLACK);     
00211                     return;
00212                     // shield wins
00213 //                    shield._base = 128/2 ;
00214                  }
00215                 return ;
00216 //                shield._x = 128/2 ;                
00217             }
00218         }
00219         lcd.filled_rectangle(
00220             _x - base , _y - height , 
00221             _x + base , _y ,
00222             color
00223         );     
00224     }
00225 private:
00226     int _x , _dx ;
00227     int _y , _dy ;
00228     bool _off ;
00229 };
00230 
00231 class Ship
00232 {    
00233     static int const base = 5 ;
00234     static int const height = 15 ;
00235     static int const color = GREEN ;
00236     
00237 public:
00238     Ship( int x , int dx , int y , int dy ) :
00239         _x( x ) , _dx( dx ) , _y( y ) , _dy( dy ) , _bullet( 0 , 0 , 0 , 0 , true )
00240     {
00241     }
00242     
00243     void read( )
00244     {
00245         if ( ( ! pb.read( ) ) & _bullet.off( ) )
00246         {
00247             _bullet.shoot( _x , _dx , _y - height , -9 ) ;
00248         }
00249         while( ! imu.accelAvailable( ) );
00250         imu.readAccel( );
00251         float ay( imu.calcAccel(imu.ay) );   
00252         _dx -= (int) ( ay * 2.4f ) ;
00253 //        pc.printf("accel: %9f %9f %9f (%d)in Gs\n\r", imu.calcAccel(imu.ax), ay, imu.calcAccel(imu.az),_dx);
00254       
00255     }
00256     void draw( Shield & shield )
00257     {
00258         lcd.triangle(
00259             _x , _y - height ,
00260             _x - base , _y , 
00261             _x + base , _y ,
00262             BLACK
00263         );     
00264         _x += _dx;
00265         if ( _x + base >= 127 )
00266         {
00267             _x = 127 - base ;
00268             _dx = - (_dx / 2);
00269         }
00270         else if ( _x - base <= 0 )
00271         {
00272             _x = 0 + base ;
00273             _dx = - _dx / 2;
00274         }
00275         lcd.triangle(
00276             _x , _y - height ,
00277             _x - base , _y , 
00278             _x + base , _y ,
00279             color
00280         );     
00281         _bullet.draw( shield ) ;
00282     }
00283     
00284 private:
00285     // center of triangle
00286     int _x ;
00287     int _dx ; 
00288     // bottom of triangle
00289     int _y ;
00290     int _dy ; 
00291     Bullet _bullet ;
00292 };
00293 
00294 
00295 
00296 class Game
00297 {
00298     static int const enemy_colors [4];
00299     enum
00300     {
00301         screen_width = 128 , screen_height = 128 ,
00302         enemy_count  = sizeof(enemy_colors) / sizeof(enemy_colors[0])
00303     };
00304         
00305 public:
00306 
00307     Game( ) :
00308         _ship( screen_width/2 , 0 , screen_height-1 , 0 ) ,
00309         _shield( screen_width/2 , 0 , 0 , 0 )
00310     {
00311     }
00312     void read( )
00313     {
00314         _ship.read( );
00315         _shield.read( );
00316     }
00317     void draw( )
00318     {
00319         _ship.draw( _shield );
00320         _shield.draw( );
00321     }
00322     void wait( )
00323     {
00324         Thread::wait( 20 );
00325     } 
00326 private:
00327     Ship _ship ;
00328     Shield _shield ;
00329 //    Enemy _enemy [ enemy_count ] ;
00330 };
00331 int const Game::enemy_colors [4] = {
00332     RED , GREEN , BLUE , RED|GREEN 
00333 };
00334 
00335 //
00336 int main( ) 
00337 {
00338     pc.printf( " -- INIT -- \n" );
00339     srand( time( NULL ) );
00340     pb.set_debounce_us( 1000 );
00341     pb.mode( PullUp );
00342     
00343     if ( ! imu.begin( ) ) {
00344         pc.printf("Failed to communicate with LSM9DS1.\n");
00345     }
00346     imu.calibrate( 1 );
00347 //    imu.calibrateMag( 0 );
00348     
00349     Thread music( music_thread , (void *)"MUSIC" );
00350     
00351     Game game;
00352     
00353     while ( 1 )
00354     {        
00355         game.read( );
00356         game.draw( );
00357         game.wait( );
00358     }
00359     
00360     pc.printf( " -- DONE -- \n" );
00361 }