WITB2

Dependencies:   mbed 4DGL-uLCD-SE PinDetect

main.cpp

Committer:
Nydrel
Date:
2017-11-08
Revision:
1:21558533e3b9
Parent:
0:5e90b8577fef
Child:
2:9ece21082838

File content as of revision 1:21558533e3b9:

#include "mbed.h"
#include "MMA8452.h"
#include "uLCD_4DGL.h"
#include "PinDetect.h"
#include "Speaker.h"

/*-----Define Statements-----*/

#define ASTEROID_HEIGHT 12
#define ASTEROID_WIDTH 15
#define SPRITE_MAX 15
#define EARTH_WIDTH 10
#define EARTH_HEIGHT 10
#define EXPLOSION1_WIDTH 20
#define SCREEN_MAX 125
#define SCREEN_MIN 1
#define NUM_ASTEROIDS 4
#define Q 0x808000 //OLIVE
#define I 0x008000 //GREEN
#define S 0xC0C0C0 //SILVER
#define C 0x17202A //UFO GLASS
#define D 0x797D7F //DARK GREY
#define L 0x00FF00 //LIME
#define P 0xFF00FF //PINK
#define R 0xF1C40F //YELLOW
#define O 0xF39C12 //ORANGE
#define G 0xAAB7B8 //GREY
#define _ 0x000000 //BLACK
#define X 0xFFFFFF //WHITE
#define B 0x0000FF //BLUE
#define r 0xFF0000 //RED


/*-----Sprites------*/

int spaceship_earth1[EARTH_WIDTH * EARTH_HEIGHT] = {
    _,_,S,S,S,S,S,S,_,_,
    _,S,I,I,I,I,I,I,S,_,
    S,I,I,I,I,I,I,I,I,S,
    S,I,I,I,I,I,I,I,I,S,
    S,I,I,I,I,I,I,I,I,S,
    S,I,I,I,I,I,I,I,I,S,
    S,I,I,I,I,I,I,I,I,S,
    S,I,I,I,I,I,I,I,I,S,
    S,I,I,I,I,I,I,I,S,_,
    _,S,S,S,S,S,S,S,_,_,
};

int earth_blank[EARTH_WIDTH * EARTH_HEIGHT] = {
    _,_,_,_,_,_,_,_,_,_,
    _,_,_,_,_,_,_,_,_,_,
    _,_,_,_,_,_,_,_,_,_,
    _,_,_,_,_,_,_,_,_,_,
    _,_,_,_,_,_,_,_,_,_,
    _,_,_,_,_,_,_,_,_,_,
    _,_,_,_,_,_,_,_,_,_,
    _,_,_,_,_,_,_,_,_,_,
    _,_,_,_,_,_,_,_,_,_,
    _,_,_,_,_,_,_,_,_,_,
};


//Instantiate Accelerometer Object

Serial pc(USBTX,USBRX); //not used in this program
MMA8452 acc(p9, p10, 40000);  //instantiate an acc object!
double x, y, z;

//Instantiate uLCD Object

DigitalOut myled(LED1);
uLCD_4DGL uLCD(p28, p27, p29); // create a global uLCD object
//Speaker mySpeaker(p21);

int factor = 50;
int offset = 63;




/*-----ScreenObject Class------*/

class ScreenObject
{
public:
    //ScreenObject();
    //setter functions
    void setXPos(int x)
    {
        x_pos = x;
    }
    
    void setYPos(int y)
    {
        y_pos = y;
    }

    //getter functions
//    int getXPos();
  //  int getYPos();

    //pure virtual
    virtual void draw() = 0;
    virtual void update() = 0;
protected:
    int x_pos;
    int y_pos;
};


/*-----SpaceShipEarth Class------*/

class SpaceShipEarth: public ScreenObject
{
public:
    SpaceShipEarth() {
        x_pos = 63;
        y_pos = 63;
    }

    void draw() {
        uLCD.BLIT(y_pos, x_pos, ASTEROID_WIDTH, ASTEROID_HEIGHT,spaceship_earth1);
    }

    void update() {
        
        uLCD.BLIT(y_pos, x_pos, ASTEROID_WIDTH, ASTEROID_HEIGHT,earth_blank);
        //uLCD.filled_rectangle(-1*y*factor-3+offsety,-1*x*factor+radius+offsety,-1*y*factor+3+offsetx,-1*x*factor+radius+6+offsety, BLACK);
        
        if(!acc.isXYZReady()) {
            wait(0.02);
        }

        else {
            acc.readXYZGravity(&x,&y,&z); //notice this is passed by reference use pointers
            setXPos(-1*x*factor + offset);
            setYPos(-1*y*factor + offset);
            
            draw();
            
        } //end else
    }

};




//Program Start

int main()
{
    uLCD.baudrate(300000);
    wait(0.2);
    srand(time(0)); // do this srand call here ONLY... no where else in the code!
//ScreenObject * ActiveAsteroids[NUM_ASTEROIDS];
    SpaceShipEarth ship; //instantiate a SpaceShipEarth object


//set parameters -- use these and don't change
    acc.setBitDepth(MMA8452::BIT_DEPTH_12);
    acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G);
    acc.setDataRate(MMA8452::RATE_100);


    while(1) {
        ship.update();

        //ship.draw();
    } //end infinite while loop
} //end main