Working!

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

Committer:
taylornichols
Date:
Sun Oct 30 03:57:00 2016 +0000
Revision:
6:81ddcbe69054
Parent:
5:bc2247ee09b9
Child:
7:a9b088f640c0
Bullet & Ship WORKING

Who changed what in which revision?

UserRevisionLine numberNew contents of line
taylornichols 5:bc2247ee09b9 1 #include <stdio.h>
taylornichols 5:bc2247ee09b9 2 #include <stdlib.h>
taylornichols 5:bc2247ee09b9 3 #include <time.h>
taylornichols 6:81ddcbe69054 4 #include "mbed.h"
taylornichols 6:81ddcbe69054 5 #include "rtos.h"
taylornichols 6:81ddcbe69054 6 #include "DebounceIn.h"
taylornichols 6:81ddcbe69054 7 #include "TextLCD.h"
taylornichols 6:81ddcbe69054 8 #include "LSM9DS1.h"
taylornichols 6:81ddcbe69054 9 #include "SDFileSystem.h"
taylornichols 6:81ddcbe69054 10 #include "uLCD_4DGL.h"
taylornichols 6:81ddcbe69054 11 #include "wave_player.h"
taylornichols 0:f29cfb036e34 12
taylornichols 6:81ddcbe69054 13 // LEDs on mbed
taylornichols 6:81ddcbe69054 14 DigitalOut led1( LED1 );
taylornichols 6:81ddcbe69054 15 DigitalOut led2( LED2 );
taylornichols 6:81ddcbe69054 16 DigitalOut led3( LED3 );
taylornichols 6:81ddcbe69054 17 DigitalOut led4( LED4 );
taylornichols 6:81ddcbe69054 18
taylornichols 6:81ddcbe69054 19 // debugging via pc
taylornichols 6:81ddcbe69054 20 Serial pc( USBTX , USBRX );
taylornichols 6:81ddcbe69054 21
taylornichols 6:81ddcbe69054 22 // SD card
taylornichols 6:81ddcbe69054 23 SDFileSystem sd( p5 , p6 , p7 , p8 , "sd" );
taylornichols 6:81ddcbe69054 24
taylornichols 6:81ddcbe69054 25 // text display ( rs , e , d4-d7 )
taylornichols 6:81ddcbe69054 26 //TextLCD txt( p22 , p23 , p24 , p25 , p26 , p27 );
taylornichols 0:f29cfb036e34 27
taylornichols 6:81ddcbe69054 28 // LCD ( tx , rx , reset )
taylornichols 6:81ddcbe69054 29 uLCD_4DGL lcd( p28 , p27 , p30 );
taylornichols 6:81ddcbe69054 30
taylornichols 6:81ddcbe69054 31 // speaker
taylornichols 6:81ddcbe69054 32 AnalogOut DACout( p18 );
taylornichols 6:81ddcbe69054 33 PwmOut PWMout( p26 );
taylornichols 6:81ddcbe69054 34 wave_player waver( & DACout , & PWMout );
taylornichols 6:81ddcbe69054 35
taylornichols 6:81ddcbe69054 36 // pushbutton
taylornichols 6:81ddcbe69054 37 DebounceIn pb( p15 );
taylornichols 6:81ddcbe69054 38
taylornichols 6:81ddcbe69054 39 // IMU ( sda , scl , ... )
taylornichols 6:81ddcbe69054 40 LSM9DS1 imu( p9 , p10 , 0xD6 , 0x3C );
taylornichols 6:81ddcbe69054 41
taylornichols 6:81ddcbe69054 42 class Bullet
taylornichols 5:bc2247ee09b9 43 {
taylornichols 5:bc2247ee09b9 44 enum
taylornichols 5:bc2247ee09b9 45 {
taylornichols 6:81ddcbe69054 46 base = 1 , height = 3
taylornichols 5:bc2247ee09b9 47 };
taylornichols 6:81ddcbe69054 48 public:
taylornichols 6:81ddcbe69054 49 Bullet( int x , int dx , int y , int dy , bool off ) :
taylornichols 6:81ddcbe69054 50 _x( x ) , _dx( dx ) , _y( y ) , _dy( dy ) , _off( off )
taylornichols 6:81ddcbe69054 51 {
taylornichols 6:81ddcbe69054 52 }
taylornichols 6:81ddcbe69054 53 bool off( ) const { return _off ; }
taylornichols 6:81ddcbe69054 54 void shoot( int x , int dx , int y , int dy )
taylornichols 6:81ddcbe69054 55 {
taylornichols 6:81ddcbe69054 56 _off = false ;
taylornichols 6:81ddcbe69054 57 _x = x ; _dx = dx ; _y = y ; _dy = dy ;
taylornichols 6:81ddcbe69054 58 }
taylornichols 6:81ddcbe69054 59 void draw( )
taylornichols 6:81ddcbe69054 60 {
taylornichols 6:81ddcbe69054 61 lcd.filled_rectangle(
taylornichols 6:81ddcbe69054 62 _x - base , _y - height ,
taylornichols 6:81ddcbe69054 63 _x + base , _y ,
taylornichols 6:81ddcbe69054 64 BLACK
taylornichols 6:81ddcbe69054 65 );
taylornichols 6:81ddcbe69054 66 if ( _off )
taylornichols 6:81ddcbe69054 67 return ;
taylornichols 6:81ddcbe69054 68 _x += _dx;
taylornichols 6:81ddcbe69054 69 _y += _dy;
taylornichols 6:81ddcbe69054 70 if ( _x + base >= 127 )
taylornichols 6:81ddcbe69054 71 {
taylornichols 6:81ddcbe69054 72 _x = 127 - base ;
taylornichols 6:81ddcbe69054 73 _dx = - (_dx / 2);
taylornichols 6:81ddcbe69054 74 }
taylornichols 6:81ddcbe69054 75 else if ( _x - base <= 0 )
taylornichols 6:81ddcbe69054 76 {
taylornichols 6:81ddcbe69054 77 _x = 0 + base ;
taylornichols 6:81ddcbe69054 78 _dx = - _dx / 2;
taylornichols 6:81ddcbe69054 79 }
taylornichols 6:81ddcbe69054 80 if ( _y - height <= 0 )
taylornichols 6:81ddcbe69054 81 {
taylornichols 6:81ddcbe69054 82 _off = true ;
taylornichols 6:81ddcbe69054 83 return ;
taylornichols 6:81ddcbe69054 84 }
taylornichols 6:81ddcbe69054 85 lcd.filled_rectangle(
taylornichols 6:81ddcbe69054 86 _x - base , _y - height ,
taylornichols 6:81ddcbe69054 87 _x + base , _y ,
taylornichols 6:81ddcbe69054 88 WHITE
taylornichols 6:81ddcbe69054 89 );
taylornichols 6:81ddcbe69054 90 }
taylornichols 6:81ddcbe69054 91 private:
taylornichols 6:81ddcbe69054 92 int _x , _dx ;
taylornichols 6:81ddcbe69054 93 int _y , _dy ;
taylornichols 6:81ddcbe69054 94 bool _off ;
taylornichols 6:81ddcbe69054 95 };
taylornichols 6:81ddcbe69054 96
taylornichols 6:81ddcbe69054 97 class Ship
taylornichols 6:81ddcbe69054 98 {
taylornichols 6:81ddcbe69054 99 static int const base = 5 ;
taylornichols 6:81ddcbe69054 100 static int const height = 15 ;
taylornichols 6:81ddcbe69054 101 static int const color = WHITE ;
taylornichols 5:bc2247ee09b9 102
taylornichols 5:bc2247ee09b9 103 public:
taylornichols 6:81ddcbe69054 104 Ship( int x , int dx , int y , int dy ) :
taylornichols 6:81ddcbe69054 105 _x( x ) , _dx( dx ) , _y( y ) , _dy( dy ) , _bullet( 0 , 0 , 0 , 0 , true )
taylornichols 5:bc2247ee09b9 106 {
taylornichols 5:bc2247ee09b9 107 }
taylornichols 5:bc2247ee09b9 108
taylornichols 6:81ddcbe69054 109 void read( )
taylornichols 6:81ddcbe69054 110 {
taylornichols 6:81ddcbe69054 111 if ( ( ! pb.read( ) ) & _bullet.off( ) )
taylornichols 6:81ddcbe69054 112 {
taylornichols 6:81ddcbe69054 113 _bullet.shoot( _x , _dx , _y - height , -17 ) ;
taylornichols 6:81ddcbe69054 114 }
taylornichols 6:81ddcbe69054 115 while( ! imu.accelAvailable( ) );
taylornichols 6:81ddcbe69054 116 imu.readAccel( );
taylornichols 6:81ddcbe69054 117 float ay( imu.calcAccel(imu.ay) );
taylornichols 6:81ddcbe69054 118 _dx -= (int) ( ay * 5.4f ) ;
taylornichols 6:81ddcbe69054 119 // pc.printf("accel: %9f %9f %9f (%d)in Gs\n\r", imu.calcAccel(imu.ax), ay, imu.calcAccel(imu.az),_dx);
taylornichols 6:81ddcbe69054 120
taylornichols 6:81ddcbe69054 121 }
taylornichols 5:bc2247ee09b9 122 void draw( )
taylornichols 5:bc2247ee09b9 123 {
taylornichols 5:bc2247ee09b9 124 lcd.triangle(
taylornichols 5:bc2247ee09b9 125 _x , _y - height ,
taylornichols 5:bc2247ee09b9 126 _x - base , _y ,
taylornichols 5:bc2247ee09b9 127 _x + base , _y ,
taylornichols 6:81ddcbe69054 128 BLACK
taylornichols 6:81ddcbe69054 129 );
taylornichols 6:81ddcbe69054 130 _x += _dx;
taylornichols 6:81ddcbe69054 131 if ( _x + base >= 127 )
taylornichols 6:81ddcbe69054 132 {
taylornichols 6:81ddcbe69054 133 _x = 127 - base ;
taylornichols 6:81ddcbe69054 134 _dx = - (_dx / 2);
taylornichols 6:81ddcbe69054 135 }
taylornichols 6:81ddcbe69054 136 else if ( _x - base <= 0 )
taylornichols 6:81ddcbe69054 137 {
taylornichols 6:81ddcbe69054 138 _x = 0 + base ;
taylornichols 6:81ddcbe69054 139 _dx = - _dx / 2;
taylornichols 6:81ddcbe69054 140 }
taylornichols 6:81ddcbe69054 141 lcd.triangle(
taylornichols 6:81ddcbe69054 142 _x , _y - height ,
taylornichols 6:81ddcbe69054 143 _x - base , _y ,
taylornichols 6:81ddcbe69054 144 _x + base , _y ,
taylornichols 5:bc2247ee09b9 145 color
taylornichols 5:bc2247ee09b9 146 );
taylornichols 6:81ddcbe69054 147 _bullet.draw( ) ;
taylornichols 5:bc2247ee09b9 148 }
taylornichols 5:bc2247ee09b9 149
taylornichols 5:bc2247ee09b9 150 private:
taylornichols 6:81ddcbe69054 151 // center of triangle
taylornichols 6:81ddcbe69054 152 int _x ;
taylornichols 6:81ddcbe69054 153 int _dx ;
taylornichols 6:81ddcbe69054 154 // bottom of triangle
taylornichols 6:81ddcbe69054 155 int _y ;
taylornichols 6:81ddcbe69054 156 int _dy ;
taylornichols 6:81ddcbe69054 157 Bullet _bullet ;
taylornichols 5:bc2247ee09b9 158 };
taylornichols 2:047c9c4b8db3 159
taylornichols 2:047c9c4b8db3 160
taylornichols 5:bc2247ee09b9 161 class Enemy
taylornichols 5:bc2247ee09b9 162 {
taylornichols 5:bc2247ee09b9 163 enum
taylornichols 5:bc2247ee09b9 164 {
taylornichols 5:bc2247ee09b9 165 width = 16, height = 10
taylornichols 5:bc2247ee09b9 166 };
taylornichols 6:81ddcbe69054 167 Enemy( int x , int dx , int y , int dy , int color ) :
taylornichols 5:bc2247ee09b9 168 _x( x ) , _dx( dx ) , _y( y ) , _dy( dy ) , _color( color )
taylornichols 5:bc2247ee09b9 169 {
taylornichols 5:bc2247ee09b9 170 }
taylornichols 5:bc2247ee09b9 171 private:
taylornichols 6:81ddcbe69054 172 //
taylornichols 6:81ddcbe69054 173 int _x ;
taylornichols 6:81ddcbe69054 174 int _dx ;
taylornichols 6:81ddcbe69054 175 //
taylornichols 6:81ddcbe69054 176 int _y ;
taylornichols 6:81ddcbe69054 177 int _dy ;
taylornichols 6:81ddcbe69054 178 //
taylornichols 6:81ddcbe69054 179 int const _color ;
taylornichols 5:bc2247ee09b9 180 };
taylornichols 2:047c9c4b8db3 181
taylornichols 5:bc2247ee09b9 182 class Game
taylornichols 5:bc2247ee09b9 183 {
taylornichols 5:bc2247ee09b9 184 static int const enemy_colors [4];
taylornichols 5:bc2247ee09b9 185 enum
taylornichols 5:bc2247ee09b9 186 {
taylornichols 5:bc2247ee09b9 187 screen_width = 128 , screen_height = 128 ,
taylornichols 5:bc2247ee09b9 188 enemy_count = sizeof(enemy_colors) / sizeof(enemy_colors[0])
taylornichols 5:bc2247ee09b9 189 };
taylornichols 5:bc2247ee09b9 190
taylornichols 5:bc2247ee09b9 191 public:
taylornichols 2:047c9c4b8db3 192
taylornichols 5:bc2247ee09b9 193 Game( ) :
taylornichols 5:bc2247ee09b9 194 _ship( screen_width/2 , 0 , screen_height-1 , 0 )
taylornichols 5:bc2247ee09b9 195 {
taylornichols 5:bc2247ee09b9 196 }
taylornichols 5:bc2247ee09b9 197 void read( )
taylornichols 5:bc2247ee09b9 198 {
taylornichols 6:81ddcbe69054 199 _ship.read( );
taylornichols 5:bc2247ee09b9 200 }
taylornichols 5:bc2247ee09b9 201 void draw( )
taylornichols 5:bc2247ee09b9 202 {
taylornichols 5:bc2247ee09b9 203 _ship.draw( );
taylornichols 5:bc2247ee09b9 204 }
taylornichols 5:bc2247ee09b9 205 void wait( )
taylornichols 5:bc2247ee09b9 206 {
taylornichols 5:bc2247ee09b9 207 Thread::wait( 100 );
taylornichols 5:bc2247ee09b9 208 }
taylornichols 5:bc2247ee09b9 209 private:
taylornichols 5:bc2247ee09b9 210 Ship _ship ;
taylornichols 5:bc2247ee09b9 211 // Enemy _enemy [ enemy_count ] ;
taylornichols 5:bc2247ee09b9 212 };
taylornichols 5:bc2247ee09b9 213 int const Game::enemy_colors [4] = {
taylornichols 5:bc2247ee09b9 214 RED , GREEN , BLUE , RED|GREEN
taylornichols 5:bc2247ee09b9 215 };
taylornichols 2:047c9c4b8db3 216
taylornichols 5:bc2247ee09b9 217 //
taylornichols 1:d1d12f229b9a 218 int main( )
taylornichols 1:d1d12f229b9a 219 {
taylornichols 3:1688f7a77ed8 220 pc.printf( " -- INIT -- \n" );
taylornichols 5:bc2247ee09b9 221 srand( time( NULL ) );
taylornichols 5:bc2247ee09b9 222 pb.set_debounce_us( 1000 );
taylornichols 5:bc2247ee09b9 223 pb.mode( PullUp );
taylornichols 5:bc2247ee09b9 224
taylornichols 6:81ddcbe69054 225 if ( ! imu.begin( ) ) {
taylornichols 6:81ddcbe69054 226 pc.printf("Failed to communicate with LSM9DS1.\n");
taylornichols 6:81ddcbe69054 227 }
taylornichols 6:81ddcbe69054 228 imu.calibrate( 1 );
taylornichols 6:81ddcbe69054 229 // imu.calibrateMag( 0 );
taylornichols 6:81ddcbe69054 230
taylornichols 5:bc2247ee09b9 231 Game game;
taylornichols 5:bc2247ee09b9 232
taylornichols 5:bc2247ee09b9 233 while ( 1 )
taylornichols 5:bc2247ee09b9 234 {
taylornichols 5:bc2247ee09b9 235 game.read( );
taylornichols 5:bc2247ee09b9 236 game.draw( );
taylornichols 5:bc2247ee09b9 237 game.wait( );
taylornichols 5:bc2247ee09b9 238 }
taylornichols 2:047c9c4b8db3 239
taylornichols 3:1688f7a77ed8 240 pc.printf( " -- DONE -- \n" );
taylornichols 0:f29cfb036e34 241 }