![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Bouncing Balls - MBED + SmartGPU2 board
main.cpp@0:34f2953983ab, 2013-07-10 (annotated)
- Committer:
- emmanuelchio
- Date:
- Wed Jul 10 03:26:11 2013 +0000
- Revision:
- 0:34f2953983ab
- Child:
- 1:a6f748930179
Bouncing Balls - MBED + SmartGPU2 board
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emmanuelchio | 0:34f2953983ab | 1 | /**************************************************************************************/ |
emmanuelchio | 0:34f2953983ab | 2 | /*SMARTGPU2 intelligent embedded graphics processor unit |
emmanuelchio | 0:34f2953983ab | 3 | those examples are for use the SMARTGPU2 with the mbed microcontoller, just connect tx,rx,and reset |
emmanuelchio | 0:34f2953983ab | 4 | Board: |
emmanuelchio | 0:34f2953983ab | 5 | http://vizictechnologies.com/#/smart-gpu-2/4577779046 |
emmanuelchio | 0:34f2953983ab | 6 | |
emmanuelchio | 0:34f2953983ab | 7 | www.vizictechnologies.com |
emmanuelchio | 0:34f2953983ab | 8 | Vizic Technologies copyright 2013 */ |
emmanuelchio | 0:34f2953983ab | 9 | /**************************************************************************************/ |
emmanuelchio | 0:34f2953983ab | 10 | /**************************************************************************************/ |
emmanuelchio | 0:34f2953983ab | 11 | |
emmanuelchio | 0:34f2953983ab | 12 | #include "mbed.h" |
emmanuelchio | 0:34f2953983ab | 13 | #include "SMARTGPU2.h" |
emmanuelchio | 0:34f2953983ab | 14 | |
emmanuelchio | 0:34f2953983ab | 15 | // defines for balls |
emmanuelchio | 0:34f2953983ab | 16 | #define RADIUSBALL1 15 //ball1 size |
emmanuelchio | 0:34f2953983ab | 17 | #define COLOURBALL1 GREEN //ball1 colour |
emmanuelchio | 0:34f2953983ab | 18 | #define RADIUSBALL2 8 //ball2 size |
emmanuelchio | 0:34f2953983ab | 19 | #define COLOURBALL2 YELLOW //ball2 colour |
emmanuelchio | 0:34f2953983ab | 20 | #define RADIUSBALL3 11 //ball3 size |
emmanuelchio | 0:34f2953983ab | 21 | #define COLOURBALL3 RED //ball3 colour |
emmanuelchio | 0:34f2953983ab | 22 | #define RADIUSBALL4 18 //ball4 size |
emmanuelchio | 0:34f2953983ab | 23 | #define COLOURBALL4 BLUE //ball4 colour |
emmanuelchio | 0:34f2953983ab | 24 | |
emmanuelchio | 0:34f2953983ab | 25 | //variables used by move ball methods |
emmanuelchio | 0:34f2953983ab | 26 | int speedBall1=3; //ball1 moving speed - amount of pixels that ball move each time |
emmanuelchio | 0:34f2953983ab | 27 | int speedBall2=4; //ball2 moving speed - amount of pixels that ball move each time |
emmanuelchio | 0:34f2953983ab | 28 | int speedBall3=8; //ball3 moving speed - amount of pixels that ball move each time |
emmanuelchio | 0:34f2953983ab | 29 | int speedBall4=6; //ball4 moving speed - amount of pixels that ball move each time |
emmanuelchio | 0:34f2953983ab | 30 | int dirx1=1; //xball1 initial positive direction |
emmanuelchio | 0:34f2953983ab | 31 | int diry1=1; //yball1 initial positive direction |
emmanuelchio | 0:34f2953983ab | 32 | int xBall1=300; //x initial position of ball1 |
emmanuelchio | 0:34f2953983ab | 33 | int yBall1=100; //y initial position of ball1 |
emmanuelchio | 0:34f2953983ab | 34 | int dirx2=-1; //xball2 initial negative direction |
emmanuelchio | 0:34f2953983ab | 35 | int diry2=-1; //yball2 initial negative direction |
emmanuelchio | 0:34f2953983ab | 36 | int xBall2=30; //x initial position of ball2 |
emmanuelchio | 0:34f2953983ab | 37 | int yBall2=80; //y initial position of ball2 |
emmanuelchio | 0:34f2953983ab | 38 | int dirx3=-1; //xball3 initial negative direction |
emmanuelchio | 0:34f2953983ab | 39 | int diry3=1; //yball3 initial negative direction |
emmanuelchio | 0:34f2953983ab | 40 | int xBall3=60; //x initial position of ball3 |
emmanuelchio | 0:34f2953983ab | 41 | int yBall3=15; //y initial position of ball3 |
emmanuelchio | 0:34f2953983ab | 42 | int dirx4=1; //xball4 initial negative direction |
emmanuelchio | 0:34f2953983ab | 43 | int diry4=-1; //yball4 initial negative direction |
emmanuelchio | 0:34f2953983ab | 44 | int xBall4=150; //x initial position of ball4 |
emmanuelchio | 0:34f2953983ab | 45 | int yBall4=80; //y initial position of ball4 |
emmanuelchio | 0:34f2953983ab | 46 | |
emmanuelchio | 0:34f2953983ab | 47 | SMARTGPU2 lcd(TXPIN,RXPIN,RESETPIN); //create our object called "lcd" |
emmanuelchio | 0:34f2953983ab | 48 | |
emmanuelchio | 0:34f2953983ab | 49 | /***************************************************/ |
emmanuelchio | 0:34f2953983ab | 50 | //Function that updates the current position of the ball1 |
emmanuelchio | 0:34f2953983ab | 51 | void moveBall1(){ |
emmanuelchio | 0:34f2953983ab | 52 | lcd.drawCircle(xBall1,yBall1,RADIUSBALL1,BLACK,UNFILL); // Erase previous ball position |
emmanuelchio | 0:34f2953983ab | 53 | xBall1+=(dirx1*speedBall1); // Calculate new x coordinate for ball1 |
emmanuelchio | 0:34f2953983ab | 54 | yBall1+=(diry1*speedBall1); // Calculate new y coordinate for ball1 |
emmanuelchio | 0:34f2953983ab | 55 | lcd.drawCircle(xBall1,yBall1,RADIUSBALL1,COLOURBALL1,UNFILL); // Draw new ball position |
emmanuelchio | 0:34f2953983ab | 56 | if((xBall1+speedBall1+RADIUSBALL1)>318 | (xBall1-speedBall1-RADIUSBALL1)<=1){ // if ball reaches the left or right corner, we invert moving direction |
emmanuelchio | 0:34f2953983ab | 57 | dirx1= dirx1*(-1); // Invert the moving direction by multiplying by -1 |
emmanuelchio | 0:34f2953983ab | 58 | } |
emmanuelchio | 0:34f2953983ab | 59 | if((yBall1+speedBall1+RADIUSBALL1)>238 | (yBall1-speedBall1-RADIUSBALL1)<=1){ // if ball reaches the top or bottom corner, we invert moving direction |
emmanuelchio | 0:34f2953983ab | 60 | diry1= diry1*(-1); // Invert the moving direction by multiplying by -1 |
emmanuelchio | 0:34f2953983ab | 61 | } |
emmanuelchio | 0:34f2953983ab | 62 | } |
emmanuelchio | 0:34f2953983ab | 63 | |
emmanuelchio | 0:34f2953983ab | 64 | /***************************************************/ |
emmanuelchio | 0:34f2953983ab | 65 | //Function that updates the current position of the ball2 |
emmanuelchio | 0:34f2953983ab | 66 | void moveBall2(){ |
emmanuelchio | 0:34f2953983ab | 67 | lcd.drawCircle(xBall2,yBall2,RADIUSBALL2,BLACK,FILL); // Erase previous ball position |
emmanuelchio | 0:34f2953983ab | 68 | xBall2+=(dirx2*speedBall2); // Calculate new x coordinate for ball2 |
emmanuelchio | 0:34f2953983ab | 69 | yBall2+=(diry2*speedBall2); // Calculate new y coordinate for ball2 |
emmanuelchio | 0:34f2953983ab | 70 | lcd.drawCircle(xBall2,yBall2,RADIUSBALL2,COLOURBALL2,FILL); // Draw new ball position |
emmanuelchio | 0:34f2953983ab | 71 | if((xBall2+speedBall2+RADIUSBALL2)>318 | (xBall2-speedBall2-RADIUSBALL2)<=1){ // if ball reaches the left or right corner, we invert moving direction |
emmanuelchio | 0:34f2953983ab | 72 | dirx2= dirx2*(-1); // Invert the moving direction by multiplying by -1 |
emmanuelchio | 0:34f2953983ab | 73 | } |
emmanuelchio | 0:34f2953983ab | 74 | if((yBall2+speedBall2+RADIUSBALL2)>238 | (yBall2-speedBall2-RADIUSBALL2)<=1){ // if ball reaches the top or bottom corner, we invert moving direction |
emmanuelchio | 0:34f2953983ab | 75 | diry2= diry2*(-1); // Invert the moving direction by multiplying by -1 |
emmanuelchio | 0:34f2953983ab | 76 | } |
emmanuelchio | 0:34f2953983ab | 77 | } |
emmanuelchio | 0:34f2953983ab | 78 | |
emmanuelchio | 0:34f2953983ab | 79 | /***************************************************/ |
emmanuelchio | 0:34f2953983ab | 80 | //Function that updates the current position of the ball3 |
emmanuelchio | 0:34f2953983ab | 81 | void moveBall3(){ |
emmanuelchio | 0:34f2953983ab | 82 | lcd.drawCircle(xBall3,yBall3,RADIUSBALL3,BLACK,FILL); // Erase previous ball position |
emmanuelchio | 0:34f2953983ab | 83 | xBall3+=(dirx3*speedBall3); // Calculate new x coordinate for ball3 |
emmanuelchio | 0:34f2953983ab | 84 | yBall3+=(diry3*speedBall3); // Calculate new y coordinate for ball3 |
emmanuelchio | 0:34f2953983ab | 85 | lcd.drawCircle(xBall3,yBall3,RADIUSBALL3,COLOURBALL3,FILL); // Draw new ball position |
emmanuelchio | 0:34f2953983ab | 86 | if((xBall3+speedBall3+RADIUSBALL3)>318 | (xBall3-speedBall3-RADIUSBALL3)<=1){ // if ball reaches the left or right corner, we invert moving direction |
emmanuelchio | 0:34f2953983ab | 87 | dirx3= dirx3*(-1); // Invert the moving direction by multiplying by -1 |
emmanuelchio | 0:34f2953983ab | 88 | } |
emmanuelchio | 0:34f2953983ab | 89 | if((yBall3+speedBall3+RADIUSBALL3)>238 | (yBall3-speedBall3-RADIUSBALL3)<=1){ // if ball reaches the top or bottom corner, we invert moving direction |
emmanuelchio | 0:34f2953983ab | 90 | diry3= diry3*(-1); // Invert the moving direction by multiplying by -1 |
emmanuelchio | 0:34f2953983ab | 91 | } |
emmanuelchio | 0:34f2953983ab | 92 | } |
emmanuelchio | 0:34f2953983ab | 93 | |
emmanuelchio | 0:34f2953983ab | 94 | /***************************************************/ |
emmanuelchio | 0:34f2953983ab | 95 | //Function that updates the current position of the ball4 |
emmanuelchio | 0:34f2953983ab | 96 | void moveBall4(){ |
emmanuelchio | 0:34f2953983ab | 97 | lcd.drawCircle(xBall4,yBall4,RADIUSBALL4,BLACK,UNFILL); // Erase previous ball position |
emmanuelchio | 0:34f2953983ab | 98 | xBall4+=(dirx4*speedBall4); // Calculate new x coordinate for ball4 |
emmanuelchio | 0:34f2953983ab | 99 | yBall4+=(diry4*speedBall4); // Calculate new y coordinate for ball4 |
emmanuelchio | 0:34f2953983ab | 100 | lcd.drawCircle(xBall4,yBall4,RADIUSBALL4,COLOURBALL4,UNFILL); // Draw new ball position |
emmanuelchio | 0:34f2953983ab | 101 | if((xBall4+speedBall4+RADIUSBALL4)>318 | (xBall4-speedBall4-RADIUSBALL4)<=1){ // if ball reaches the left or right corner, we invert moving direction |
emmanuelchio | 0:34f2953983ab | 102 | dirx4= dirx4*(-1); // Invert the moving direction by multiplying by -1 |
emmanuelchio | 0:34f2953983ab | 103 | } |
emmanuelchio | 0:34f2953983ab | 104 | if((yBall4+speedBall4+RADIUSBALL4)>238 | (yBall4-speedBall4-RADIUSBALL4)<=1){ // if ball reaches the top or bottom corner, we invert moving direction |
emmanuelchio | 0:34f2953983ab | 105 | diry4= diry4*(-1); // Invert the moving direction by multiplying by -1 |
emmanuelchio | 0:34f2953983ab | 106 | } |
emmanuelchio | 0:34f2953983ab | 107 | } |
emmanuelchio | 0:34f2953983ab | 108 | |
emmanuelchio | 0:34f2953983ab | 109 | /***************************************************/ |
emmanuelchio | 0:34f2953983ab | 110 | /***************************************************/ |
emmanuelchio | 0:34f2953983ab | 111 | void initializeSmartGPU2(void){ //Initialize SMARTGPU2 Board |
emmanuelchio | 0:34f2953983ab | 112 | lcd.reset(); //physically reset SMARTGPU2 |
emmanuelchio | 0:34f2953983ab | 113 | lcd.start(); //initialize the SMARTGPU2 processor |
emmanuelchio | 0:34f2953983ab | 114 | } |
emmanuelchio | 0:34f2953983ab | 115 | |
emmanuelchio | 0:34f2953983ab | 116 | /***************************************************/ |
emmanuelchio | 0:34f2953983ab | 117 | /***************************************************/ |
emmanuelchio | 0:34f2953983ab | 118 | /***************************************************/ |
emmanuelchio | 0:34f2953983ab | 119 | /***************************************************/ |
emmanuelchio | 0:34f2953983ab | 120 | int main() { |
emmanuelchio | 0:34f2953983ab | 121 | |
emmanuelchio | 0:34f2953983ab | 122 | initializeSmartGPU2(); //Init communication with SmartGPU2 board |
emmanuelchio | 0:34f2953983ab | 123 | |
emmanuelchio | 0:34f2953983ab | 124 | lcd.baudChange(BAUD7); //set a fast baud! for fast drawing |
emmanuelchio | 0:34f2953983ab | 125 | lcd.drawRectangle(0,0,319,239,MAGENTA,UNFILL); //draw corners |
emmanuelchio | 0:34f2953983ab | 126 | |
emmanuelchio | 0:34f2953983ab | 127 | while(1){ // Loop forever |
emmanuelchio | 0:34f2953983ab | 128 | moveBall1(); // move ball1 |
emmanuelchio | 0:34f2953983ab | 129 | moveBall2(); // move ball2 |
emmanuelchio | 0:34f2953983ab | 130 | moveBall3(); // move ball3 |
emmanuelchio | 0:34f2953983ab | 131 | moveBall4(); // move ball4 |
emmanuelchio | 0:34f2953983ab | 132 | wait_ms(15); // wait a little |
emmanuelchio | 0:34f2953983ab | 133 | } |
emmanuelchio | 0:34f2953983ab | 134 | } |