WITB2
Dependencies: mbed 4DGL-uLCD-SE PinDetect
Diff: main.cpp
- Revision:
- 1:21558533e3b9
- Parent:
- 0:5e90b8577fef
- Child:
- 2:9ece21082838
diff -r 5e90b8577fef -r 21558533e3b9 main.cpp --- a/main.cpp Tue Sep 19 23:10:09 2017 +0000 +++ b/main.cpp Wed Nov 08 20:19:16 2017 +0000 @@ -1,74 +1,173 @@ #include "mbed.h" #include "MMA8452.h" - #include "uLCD_4DGL.h" - #include "PinDetect.h" - #include "Speaker.h" - - - uLCD_4DGL uLCD(p28, p27, p29); // create a global uLCD object - +#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; -Speaker mySpeaker(p21); +//Instantiate uLCD Object -PinDetect pb1(p15); -PinDetect pb2(p16); +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------*/ -int main() { - - // you can play around with the parameters to see the response - int radius = 10; - int offsetx = 63; - int offsety = 63; - double factor = 50; - double music_factor = 200; - bool MusicOn = false; - - //set the push buttons that control sounds - pb1.mode(PullUp); - pb2.mode(PullUp); - //I will not use interupts like in jazzy tunes - - - double x = 0, y = 0, z = 0; - - MMA8452 acc(p9, p10, 40000); //instantiate an acc 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) { - - uLCD.circle(-1*y*factor+offsety, -1*x*factor+offsetx, radius, BLACK); - - if(!acc.isXYZReady()) - { - wait(0.01); - } - else - { - - acc.readXYZGravity(&x,&y,&z); //notice this is passed by reference use pointers - - uLCD.circle(-1*y*factor+offsety, -1*x*factor+offsetx, radius, WHITE); - - if (MusicOn) - mySpeaker.PlayNote(440.0+x*music_factor,0.25+0.2*y,0.05); - - if (pb1 == false) - MusicOn = true; - - if (pb2 == false) - MusicOn = false; - - - // You can uncomment this line to see the values coming off the MMA8452 - //uLCD.printf("\n(%.2f,%.2f,%.2f) \n", x,y,z); - - } //end else - - } //end infinite while loop -} // end main \ No newline at end of file +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 + +