A simple example using a display and networking at the same time.

Dependencies:   EALib EthernetInterface mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BubbleDemo.cpp Source File

BubbleDemo.cpp

00001 /******************************************************************************
00002  * Includes
00003  *****************************************************************************/
00004 
00005 #include "mbed.h"
00006 
00007 #include "LcdController.h"
00008 #include "EaLcdBoard.h"
00009 #include "BubbleDemo.h"
00010 
00011 #include "wchar.h"
00012 
00013 /******************************************************************************
00014  * Typedefs and defines
00015  *****************************************************************************/
00016 
00017 #define PI 3.1415926535897932384626433832795
00018 
00019 /* Red color mask, 565 mode */
00020 #define REDMASK       0xF800
00021 /* Red shift value, 565 mode */
00022 #define REDSHIFT      11
00023 /* Green color mask, 565 mode */
00024 #define GREENMASK     0x07E0
00025 /* Green shift value, 565 mode */
00026 #define GREENSHIFT    5
00027 /* Blue color mask, 565 mode */
00028 #define BLUEMASK      0x001F
00029 /* Blue shift value, 565 mode */
00030 #define BLUESHIFT     0
00031 
00032 /* Number of colors in 565 mode */
00033 #define NUM_COLORS    65536
00034 /* Number of red colors in 565 mode */
00035 #define RED_COLORS    0x20
00036 /* Number of green colors in 565 mode */
00037 #define GREEN_COLORS  0x40
00038 /* Number of blue colors in 565 mode */
00039 #define BLUE_COLORS   0x20
00040 
00041 /******************************************************************************
00042  * Local variables
00043  *****************************************************************************/
00044 
00045 
00046 /******************************************************************************
00047  * External variables
00048  *****************************************************************************/
00049 extern EaLcdBoard lcdBoard;
00050 extern bool abortTest;
00051 
00052 /******************************************************************************
00053  * Local functions
00054  *****************************************************************************/
00055 
00056 
00057 void BubbleDemo::initialize() {
00058  float radian;
00059  const float phase1 = 2 * PI / 3;
00060  const float phase2 = 4 * PI / 3;
00061 
00062  for(i = 0; i < NumBalls; i++) {
00063   x[i] = this->windowX / 2;
00064   y[i] = this->windowY / 2;
00065   r[i] = i * 2 + 10;
00066 
00067   oldX[i] = x[i];
00068   oldY[i] = y[i];
00069 
00070   velX[i] = 1;
00071   velY[i] = 1;
00072 
00073   radian = i * 2 * PI / NumBalls;
00074   red[i] = cos(radian) * RED_COLORS / 2 + (RED_COLORS / 2 - 1);
00075   green[i] = cos(radian + phase2) * GREEN_COLORS / 2 + (GREEN_COLORS / 2 - 1);
00076   blue[i] = cos(radian + phase1) * BLUE_COLORS / 2 + (BLUE_COLORS / 2 - 1);
00077  }
00078 }
00079 
00080 void BubbleDemo::collision() {
00081  float disX = x[j] - x[i];
00082  float disY = y[j] - y[i];
00083  float d2 = disX * disX + disY * disY;
00084 
00085  if(d2 != 0) {
00086   float rij = r[i] + r[j];
00087   float rij2 = rij * rij;
00088 
00089   if(d2 < rij2) {
00090    float ii = (disX * velX[i] + disY * velY[i]) / d2;
00091    float ji = (disX * velY[i] - disY * velX[i]) / d2;
00092    float ij = (disX * velX[j] + disY * velY[j]) / d2;
00093    float jj = (disX * velY[j] - disY * velX[j]) / d2;
00094    float ratio = rij / sqrt(d2);
00095 
00096    velX[i] = ij * disX - ii * disY;
00097    velY[i] = ij * disY + ii * disX;
00098    velX[j] = ji * disX - jj * disY;
00099    velY[j] = ji * disY + jj * disX;
00100 
00101    disX *= (ratio - 1) / 2;
00102    disY *= (ratio - 1) / 2;
00103 
00104    x[j] += disX;
00105    y[j] += disY;
00106    x[i] -= disX;
00107    y[i] -= disY;
00108   }
00109  }
00110 }
00111 
00112 void BubbleDemo::borders() {
00113  if(x[i] >= this->windowX - r[i] - 1) {
00114   x[i] = this->windowX - r[i] - 1;
00115   velX[i] = -velX[i];
00116  } else if(x[i] <= r[i]) {
00117   x[i] = r[i];
00118   velX[i] = -velX[i];
00119  }
00120 
00121  if(y[i] >= this->windowY - r[i] - 1) {
00122   y[i] = this->windowY - r[i] - 1;
00123   velY[i] = -velY[i];
00124  } else if(y[i] <= r[i]) {
00125   y[i] = r[i];
00126   velY[i] = -velY[i];
00127  }
00128 }
00129 
00130 void BubbleDemo::draw() {
00131  graphics.put_circle( oldX[i], oldY[i], 0, r[i], 0 );
00132  graphics.put_circle( x[i], y[i], (red[i] << REDSHIFT) + (green[i] << GREENSHIFT) + (blue[i] << BLUESHIFT), r[i], 0);
00133 
00134  oldX[i] = x[i];
00135  oldY[i] = y[i];
00136 }
00137 
00138 
00139 /******************************************************************************
00140  * Public functions
00141  *****************************************************************************/
00142  
00143 BubbleDemo::BubbleDemo(uint8_t *pFrameBuf, uint16_t dispWidth, uint16_t dispHeight) 
00144     : graphics((uint16_t *)pFrameBuf, dispWidth, dispHeight) {
00145 
00146     this->windowX = dispWidth;
00147     this->windowY = dispHeight;
00148     this->pFrmBuf  = (uint16_t *)pFrameBuf;
00149     this->pFrmBuf1 = (uint16_t *)pFrameBuf;
00150     this->pFrmBuf2 = (uint16_t *)((uint32_t)pFrameBuf + dispWidth*dispHeight*2);
00151     this->pFrmBuf3 = (uint16_t *)((uint32_t)pFrameBuf + dispWidth*dispHeight*4);
00152 
00153     initialize();
00154 }
00155 
00156 void BubbleDemo::run(uint32_t loops, uint32_t delayMs) {
00157   
00158   printf("BubbleDemo, %d loops, %dms delay\n", loops, delayMs);
00159 
00160     //update framebuffer
00161     graphics.setFrameBuffer(this->pFrmBuf);
00162     lcdBoard.setFrameBuffer((uint32_t)this->pFrmBuf);
00163     memset((void*)(pFrmBuf), 0, this->windowX * this->windowY * 2);
00164 
00165     for(int32_t n=0;n<loops;n++) {
00166 
00167         for(i = 0; i < NumBalls; i++) {
00168             x[i] += velX[i];
00169             y[i] += velY[i];
00170             
00171             for(j = i + 1; j < NumBalls; j++)
00172                 collision();
00173                 
00174             borders();
00175             
00176             if((int)x[i] != (int)oldX[i] || (int)y[i] != (int)oldY[i])
00177                 draw();
00178         }
00179         if (abortTest) {
00180             break;
00181         }
00182 
00183         wait_ms(delayMs);
00184     }
00185     memset((void*)(pFrmBuf), 0, this->windowX * this->windowY * 2);
00186 }
00187