Working!

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

Revision:
6:81ddcbe69054
Parent:
5:bc2247ee09b9
Child:
7:a9b088f640c0
--- a/main.cpp	Sun Oct 30 02:55:24 2016 +0000
+++ b/main.cpp	Sun Oct 30 03:57:00 2016 +0000
@@ -1,48 +1,162 @@
 #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 "init.cpp"
+//  LEDs on mbed
+DigitalOut led1( LED1 );
+DigitalOut led2( LED2 );
+DigitalOut led3( LED3 );
+DigitalOut led4( 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 );
 
-class Ship
+//  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 );
+
+class Bullet
 {
     enum
     {
-        base = 5 , height = 15 , color = WHITE
+        base = 1 , height = 3
     };
+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 ,
+            WHITE
+        );     
+    }
+private:
+    int _x , _dx ;
+    int _y , _dy ;
+    bool _off ;
+};
+
+class Ship
+{
+    static int const base = 5 ;
+    static int const height = 15 ;
+    static int const color = WHITE ;
     
 public:
-    Ship( int unsigned x , int unsigned dx , int unsigned y , int unsigned dy ) :
-        _x( x ) , _dx( dx ) , _y( y ) , _dy( dy ) 
+    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 , -17 ) ;
+        }
+        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:
-    int unsigned _x , _dx ; // center of triangle
-    int unsigned _y , _dy ; // bottom of triangle
+    // center of triangle
+    int _x ;
+    int _dx ; 
+    // bottom of triangle
+    int _y ;
+    int _dy ; 
+    Bullet _bullet ;
 };
 
-class Bullet
-{
-    enum
-    {
-        width = 2 , height = 3
-    };
-    
-private:
-    int unsigned _x , _dx ;
-    int unsigned _y , _dy ;
-};
 
 class Enemy
 {
@@ -50,14 +164,19 @@
     {
         width = 16, height = 10
     };
-    Enemy( int unsigned x , int unsigned dx , int unsigned y , int unsigned dy , int color ) :
+    Enemy( int x , int dx , int y , int dy , int color ) :
         _x( x ) , _dx( dx ) , _y( y ) , _dy( dy ) , _color( color )
     {
     }
 private:
-    int unsigned _x , _dx ;
-    int unsigned _y , _dy ;
-    int unsigned const _color ;
+    // 
+    int _x ;
+    int _dx ; 
+    // 
+    int _y ;
+    int _dy ; 
+    //
+    int const _color ;
 };
 
 class Game
@@ -77,6 +196,7 @@
     }
     void read( )
     {
+        _ship.read( );
     }
     void draw( )
     {
@@ -102,6 +222,12 @@
     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 )