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