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

Dependencies:   EALib EthernetInterface mbed-rtos mbed

Committer:
embeddedartists
Date:
Mon Jul 21 09:42:16 2014 +0000
Revision:
0:833824a40ced
Very basic example of using display and networking at the same time.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 0:833824a40ced 1 /******************************************************************************
embeddedartists 0:833824a40ced 2 * Includes
embeddedartists 0:833824a40ced 3 *****************************************************************************/
embeddedartists 0:833824a40ced 4
embeddedartists 0:833824a40ced 5 #include "mbed.h"
embeddedartists 0:833824a40ced 6
embeddedartists 0:833824a40ced 7 #include "LcdController.h"
embeddedartists 0:833824a40ced 8 #include "EaLcdBoard.h"
embeddedartists 0:833824a40ced 9 #include "BubbleDemo.h"
embeddedartists 0:833824a40ced 10
embeddedartists 0:833824a40ced 11 #include "wchar.h"
embeddedartists 0:833824a40ced 12
embeddedartists 0:833824a40ced 13 /******************************************************************************
embeddedartists 0:833824a40ced 14 * Typedefs and defines
embeddedartists 0:833824a40ced 15 *****************************************************************************/
embeddedartists 0:833824a40ced 16
embeddedartists 0:833824a40ced 17 #define PI 3.1415926535897932384626433832795
embeddedartists 0:833824a40ced 18
embeddedartists 0:833824a40ced 19 /* Red color mask, 565 mode */
embeddedartists 0:833824a40ced 20 #define REDMASK 0xF800
embeddedartists 0:833824a40ced 21 /* Red shift value, 565 mode */
embeddedartists 0:833824a40ced 22 #define REDSHIFT 11
embeddedartists 0:833824a40ced 23 /* Green color mask, 565 mode */
embeddedartists 0:833824a40ced 24 #define GREENMASK 0x07E0
embeddedartists 0:833824a40ced 25 /* Green shift value, 565 mode */
embeddedartists 0:833824a40ced 26 #define GREENSHIFT 5
embeddedartists 0:833824a40ced 27 /* Blue color mask, 565 mode */
embeddedartists 0:833824a40ced 28 #define BLUEMASK 0x001F
embeddedartists 0:833824a40ced 29 /* Blue shift value, 565 mode */
embeddedartists 0:833824a40ced 30 #define BLUESHIFT 0
embeddedartists 0:833824a40ced 31
embeddedartists 0:833824a40ced 32 /* Number of colors in 565 mode */
embeddedartists 0:833824a40ced 33 #define NUM_COLORS 65536
embeddedartists 0:833824a40ced 34 /* Number of red colors in 565 mode */
embeddedartists 0:833824a40ced 35 #define RED_COLORS 0x20
embeddedartists 0:833824a40ced 36 /* Number of green colors in 565 mode */
embeddedartists 0:833824a40ced 37 #define GREEN_COLORS 0x40
embeddedartists 0:833824a40ced 38 /* Number of blue colors in 565 mode */
embeddedartists 0:833824a40ced 39 #define BLUE_COLORS 0x20
embeddedartists 0:833824a40ced 40
embeddedartists 0:833824a40ced 41 /******************************************************************************
embeddedartists 0:833824a40ced 42 * Local variables
embeddedartists 0:833824a40ced 43 *****************************************************************************/
embeddedartists 0:833824a40ced 44
embeddedartists 0:833824a40ced 45
embeddedartists 0:833824a40ced 46 /******************************************************************************
embeddedartists 0:833824a40ced 47 * External variables
embeddedartists 0:833824a40ced 48 *****************************************************************************/
embeddedartists 0:833824a40ced 49 extern EaLcdBoard lcdBoard;
embeddedartists 0:833824a40ced 50 extern bool abortTest;
embeddedartists 0:833824a40ced 51
embeddedartists 0:833824a40ced 52 /******************************************************************************
embeddedartists 0:833824a40ced 53 * Local functions
embeddedartists 0:833824a40ced 54 *****************************************************************************/
embeddedartists 0:833824a40ced 55
embeddedartists 0:833824a40ced 56
embeddedartists 0:833824a40ced 57 void BubbleDemo::initialize() {
embeddedartists 0:833824a40ced 58 float radian;
embeddedartists 0:833824a40ced 59 const float phase1 = 2 * PI / 3;
embeddedartists 0:833824a40ced 60 const float phase2 = 4 * PI / 3;
embeddedartists 0:833824a40ced 61
embeddedartists 0:833824a40ced 62 for(i = 0; i < NumBalls; i++) {
embeddedartists 0:833824a40ced 63 x[i] = this->windowX / 2;
embeddedartists 0:833824a40ced 64 y[i] = this->windowY / 2;
embeddedartists 0:833824a40ced 65 r[i] = i * 2 + 10;
embeddedartists 0:833824a40ced 66
embeddedartists 0:833824a40ced 67 oldX[i] = x[i];
embeddedartists 0:833824a40ced 68 oldY[i] = y[i];
embeddedartists 0:833824a40ced 69
embeddedartists 0:833824a40ced 70 velX[i] = 1;
embeddedartists 0:833824a40ced 71 velY[i] = 1;
embeddedartists 0:833824a40ced 72
embeddedartists 0:833824a40ced 73 radian = i * 2 * PI / NumBalls;
embeddedartists 0:833824a40ced 74 red[i] = cos(radian) * RED_COLORS / 2 + (RED_COLORS / 2 - 1);
embeddedartists 0:833824a40ced 75 green[i] = cos(radian + phase2) * GREEN_COLORS / 2 + (GREEN_COLORS / 2 - 1);
embeddedartists 0:833824a40ced 76 blue[i] = cos(radian + phase1) * BLUE_COLORS / 2 + (BLUE_COLORS / 2 - 1);
embeddedartists 0:833824a40ced 77 }
embeddedartists 0:833824a40ced 78 }
embeddedartists 0:833824a40ced 79
embeddedartists 0:833824a40ced 80 void BubbleDemo::collision() {
embeddedartists 0:833824a40ced 81 float disX = x[j] - x[i];
embeddedartists 0:833824a40ced 82 float disY = y[j] - y[i];
embeddedartists 0:833824a40ced 83 float d2 = disX * disX + disY * disY;
embeddedartists 0:833824a40ced 84
embeddedartists 0:833824a40ced 85 if(d2 != 0) {
embeddedartists 0:833824a40ced 86 float rij = r[i] + r[j];
embeddedartists 0:833824a40ced 87 float rij2 = rij * rij;
embeddedartists 0:833824a40ced 88
embeddedartists 0:833824a40ced 89 if(d2 < rij2) {
embeddedartists 0:833824a40ced 90 float ii = (disX * velX[i] + disY * velY[i]) / d2;
embeddedartists 0:833824a40ced 91 float ji = (disX * velY[i] - disY * velX[i]) / d2;
embeddedartists 0:833824a40ced 92 float ij = (disX * velX[j] + disY * velY[j]) / d2;
embeddedartists 0:833824a40ced 93 float jj = (disX * velY[j] - disY * velX[j]) / d2;
embeddedartists 0:833824a40ced 94 float ratio = rij / sqrt(d2);
embeddedartists 0:833824a40ced 95
embeddedartists 0:833824a40ced 96 velX[i] = ij * disX - ii * disY;
embeddedartists 0:833824a40ced 97 velY[i] = ij * disY + ii * disX;
embeddedartists 0:833824a40ced 98 velX[j] = ji * disX - jj * disY;
embeddedartists 0:833824a40ced 99 velY[j] = ji * disY + jj * disX;
embeddedartists 0:833824a40ced 100
embeddedartists 0:833824a40ced 101 disX *= (ratio - 1) / 2;
embeddedartists 0:833824a40ced 102 disY *= (ratio - 1) / 2;
embeddedartists 0:833824a40ced 103
embeddedartists 0:833824a40ced 104 x[j] += disX;
embeddedartists 0:833824a40ced 105 y[j] += disY;
embeddedartists 0:833824a40ced 106 x[i] -= disX;
embeddedartists 0:833824a40ced 107 y[i] -= disY;
embeddedartists 0:833824a40ced 108 }
embeddedartists 0:833824a40ced 109 }
embeddedartists 0:833824a40ced 110 }
embeddedartists 0:833824a40ced 111
embeddedartists 0:833824a40ced 112 void BubbleDemo::borders() {
embeddedartists 0:833824a40ced 113 if(x[i] >= this->windowX - r[i] - 1) {
embeddedartists 0:833824a40ced 114 x[i] = this->windowX - r[i] - 1;
embeddedartists 0:833824a40ced 115 velX[i] = -velX[i];
embeddedartists 0:833824a40ced 116 } else if(x[i] <= r[i]) {
embeddedartists 0:833824a40ced 117 x[i] = r[i];
embeddedartists 0:833824a40ced 118 velX[i] = -velX[i];
embeddedartists 0:833824a40ced 119 }
embeddedartists 0:833824a40ced 120
embeddedartists 0:833824a40ced 121 if(y[i] >= this->windowY - r[i] - 1) {
embeddedartists 0:833824a40ced 122 y[i] = this->windowY - r[i] - 1;
embeddedartists 0:833824a40ced 123 velY[i] = -velY[i];
embeddedartists 0:833824a40ced 124 } else if(y[i] <= r[i]) {
embeddedartists 0:833824a40ced 125 y[i] = r[i];
embeddedartists 0:833824a40ced 126 velY[i] = -velY[i];
embeddedartists 0:833824a40ced 127 }
embeddedartists 0:833824a40ced 128 }
embeddedartists 0:833824a40ced 129
embeddedartists 0:833824a40ced 130 void BubbleDemo::draw() {
embeddedartists 0:833824a40ced 131 graphics.put_circle( oldX[i], oldY[i], 0, r[i], 0 );
embeddedartists 0:833824a40ced 132 graphics.put_circle( x[i], y[i], (red[i] << REDSHIFT) + (green[i] << GREENSHIFT) + (blue[i] << BLUESHIFT), r[i], 0);
embeddedartists 0:833824a40ced 133
embeddedartists 0:833824a40ced 134 oldX[i] = x[i];
embeddedartists 0:833824a40ced 135 oldY[i] = y[i];
embeddedartists 0:833824a40ced 136 }
embeddedartists 0:833824a40ced 137
embeddedartists 0:833824a40ced 138
embeddedartists 0:833824a40ced 139 /******************************************************************************
embeddedartists 0:833824a40ced 140 * Public functions
embeddedartists 0:833824a40ced 141 *****************************************************************************/
embeddedartists 0:833824a40ced 142
embeddedartists 0:833824a40ced 143 BubbleDemo::BubbleDemo(uint8_t *pFrameBuf, uint16_t dispWidth, uint16_t dispHeight)
embeddedartists 0:833824a40ced 144 : graphics((uint16_t *)pFrameBuf, dispWidth, dispHeight) {
embeddedartists 0:833824a40ced 145
embeddedartists 0:833824a40ced 146 this->windowX = dispWidth;
embeddedartists 0:833824a40ced 147 this->windowY = dispHeight;
embeddedartists 0:833824a40ced 148 this->pFrmBuf = (uint16_t *)pFrameBuf;
embeddedartists 0:833824a40ced 149 this->pFrmBuf1 = (uint16_t *)pFrameBuf;
embeddedartists 0:833824a40ced 150 this->pFrmBuf2 = (uint16_t *)((uint32_t)pFrameBuf + dispWidth*dispHeight*2);
embeddedartists 0:833824a40ced 151 this->pFrmBuf3 = (uint16_t *)((uint32_t)pFrameBuf + dispWidth*dispHeight*4);
embeddedartists 0:833824a40ced 152
embeddedartists 0:833824a40ced 153 initialize();
embeddedartists 0:833824a40ced 154 }
embeddedartists 0:833824a40ced 155
embeddedartists 0:833824a40ced 156 void BubbleDemo::run(uint32_t loops, uint32_t delayMs) {
embeddedartists 0:833824a40ced 157
embeddedartists 0:833824a40ced 158 printf("BubbleDemo, %d loops, %dms delay\n", loops, delayMs);
embeddedartists 0:833824a40ced 159
embeddedartists 0:833824a40ced 160 //update framebuffer
embeddedartists 0:833824a40ced 161 graphics.setFrameBuffer(this->pFrmBuf);
embeddedartists 0:833824a40ced 162 lcdBoard.setFrameBuffer((uint32_t)this->pFrmBuf);
embeddedartists 0:833824a40ced 163 memset((void*)(pFrmBuf), 0, this->windowX * this->windowY * 2);
embeddedartists 0:833824a40ced 164
embeddedartists 0:833824a40ced 165 for(int32_t n=0;n<loops;n++) {
embeddedartists 0:833824a40ced 166
embeddedartists 0:833824a40ced 167 for(i = 0; i < NumBalls; i++) {
embeddedartists 0:833824a40ced 168 x[i] += velX[i];
embeddedartists 0:833824a40ced 169 y[i] += velY[i];
embeddedartists 0:833824a40ced 170
embeddedartists 0:833824a40ced 171 for(j = i + 1; j < NumBalls; j++)
embeddedartists 0:833824a40ced 172 collision();
embeddedartists 0:833824a40ced 173
embeddedartists 0:833824a40ced 174 borders();
embeddedartists 0:833824a40ced 175
embeddedartists 0:833824a40ced 176 if((int)x[i] != (int)oldX[i] || (int)y[i] != (int)oldY[i])
embeddedartists 0:833824a40ced 177 draw();
embeddedartists 0:833824a40ced 178 }
embeddedartists 0:833824a40ced 179 if (abortTest) {
embeddedartists 0:833824a40ced 180 break;
embeddedartists 0:833824a40ced 181 }
embeddedartists 0:833824a40ced 182
embeddedartists 0:833824a40ced 183 wait_ms(delayMs);
embeddedartists 0:833824a40ced 184 }
embeddedartists 0:833824a40ced 185 memset((void*)(pFrmBuf), 0, this->windowX * this->windowY * 2);
embeddedartists 0:833824a40ced 186 }
embeddedartists 0:833824a40ced 187