Working!
Dependencies: 4DGL-uLCD-SE DebounceIn LSM9DS1_Library_cal SDFileSystem TextLCD mbed-rtos mbed wave_player_appbd
main.cpp
- Committer:
- taylornichols
- Date:
- 2016-10-30
- Revision:
- 7:a9b088f640c0
- Parent:
- 6:81ddcbe69054
- Child:
- 8:725b8dfe4e56
File content as of revision 7:a9b088f640c0:
#include <stdio.h> #include <stdlib.h> #include <time.h> #include "mbed.h" #include "rtos.h" #include "DebounceIn.h" #include "TextLCD.h" #include "LSM9DS1.h" #include "SDFileSystem.h" #include "uLCD_4DGL.h" #include "wave_player.h" #include "Nav_Switch.h" // LEDs on mbed BusOut led(LED1,LED2,LED3,LED4); // debugging via pc Serial pc( USBTX , USBRX ); // SD card SDFileSystem sd( p5 , p6 , p7 , p8 , "sd" ); // text display ( rs , e , d4-d7 ) //TextLCD txt( p22 , p23 , p24 , p25 , p26 , p27 ); // LCD ( tx , rx , reset ) uLCD_4DGL lcd( p28 , p27 , p30 ); // speaker AnalogOut DACout( p18 ); PwmOut PWMout( p26 ); wave_player waver( & DACout , & PWMout ); // pushbutton DebounceIn pb( p15 ); // IMU ( sda , scl , ... ) LSM9DS1 imu( p9 , p10 , 0xD6 , 0x3C ); // Nav_Switch nav( p21 , p22 , p13 , p12 , p23 ); class Game ; class Bullet { friend Game ; static int const base = 1 ; static int const height = 3 ; static int const color = WHITE ; public: Bullet( int x , int dx , int y , int dy , bool off ) : _x( x ) , _dx( dx ) , _y( y ) , _dy( dy ) , _off( off ) { } bool off( ) const { return _off ; } void shoot( int x , int dx , int y , int dy ) { _off = false ; _x = x ; _dx = dx ; _y = y ; _dy = dy ; } void draw( ) { lcd.filled_rectangle( _x - base , _y - height , _x + base , _y , BLACK ); if ( _off ) return ; _x += _dx; _y += _dy; if ( _x + base >= 127 ) { _x = 127 - base ; _dx = - (_dx / 2); } else if ( _x - base <= 0 ) { _x = 0 + base ; _dx = - _dx / 2; } if ( _y - height <= 0 ) { _off = true ; return ; } lcd.filled_rectangle( _x - base , _y - height , _x + base , _y , color ); } private: int _x , _dx ; int _y , _dy ; bool _off ; }; class Ship { friend Game ; static int const base = 5 ; static int const height = 15 ; static int const color = GREEN ; public: Ship( int x , int dx , int y , int dy ) : _x( x ) , _dx( dx ) , _y( y ) , _dy( dy ) , _bullet( 0 , 0 , 0 , 0 , true ) { } void read( ) { if ( ( ! pb.read( ) ) & _bullet.off( ) ) { _bullet.shoot( _x , _dx , _y - height , -5 ) ; } while( ! imu.accelAvailable( ) ); imu.readAccel( ); float ay( imu.calcAccel(imu.ay) ); _dx -= (int) ( ay * 5.4f ) ; // pc.printf("accel: %9f %9f %9f (%d)in Gs\n\r", imu.calcAccel(imu.ax), ay, imu.calcAccel(imu.az),_dx); } void draw( ) { lcd.triangle( _x , _y - height , _x - base , _y , _x + base , _y , BLACK ); _x += _dx; if ( _x + base >= 127 ) { _x = 127 - base ; _dx = - (_dx / 2); } else if ( _x - base <= 0 ) { _x = 0 + base ; _dx = - _dx / 2; } lcd.triangle( _x , _y - height , _x - base , _y , _x + base , _y , color ); _bullet.draw( ) ; } private: // center of triangle int _x ; int _dx ; // bottom of triangle int _y ; int _dy ; Bullet _bullet ; }; class Shield { friend Game ; static int const base = 17 ; static int const height = 5 ; static int const color = RED ; Shield( int x , int dx , int y , int dy ) : _x( x ) , _dx( dx ) , _y( y ) , _dy( dy ) { } void draw( ) { lcd.filled_rectangle( _x - base , _y , _x + base , _y + height, BLACK ); _x += _dx; if ( _x + base >= 127 ) { _x = 127 - base ; _dx = - (_dx / 2); } else if ( _x - base <= 0 ) { _x = 0 + base ; _dx = - _dx / 2; } lcd.filled_rectangle( _x - base , _y , _x + base , _y + height, color ); } private: // int _x ; int _dx ; // int _y ; int _dy ; }; class Game { static int const enemy_colors [4]; enum { screen_width = 128 , screen_height = 128 , enemy_count = sizeof(enemy_colors) / sizeof(enemy_colors[0]) }; public: Game( ) : _ship( screen_width/2 , 0 , screen_height-1 , 0 ) , _shield( screen_width/2 , 0 , 0 , 0 ) { } void read( ) { _ship.read( ); } void draw( ) { _ship.draw( ); _shield.draw( ); } void wait( ) { Thread::wait( 100 ); } private: Ship _ship ; Shield _shield ; // Enemy _enemy [ enemy_count ] ; }; int const Game::enemy_colors [4] = { RED , GREEN , BLUE , RED|GREEN }; // int main( ) { pc.printf( " -- INIT -- \n" ); srand( time( NULL ) ); pb.set_debounce_us( 1000 ); pb.mode( PullUp ); if ( ! imu.begin( ) ) { pc.printf("Failed to communicate with LSM9DS1.\n"); } imu.calibrate( 1 ); // imu.calibrateMag( 0 ); Game game; while ( 1 ) { game.read( ); game.draw( ); game.wait( ); } pc.printf( " -- DONE -- \n" ); }