Production Test Program (PTP) for the LPC4088 Experiment Base Board
Dependencies: EALib I2S LM75B SDFileSystem mbed
BubbleDemo.cpp@0:0d5190d379d3, 2014-08-25 (annotated)
- Committer:
- embeddedartists
- Date:
- Mon Aug 25 13:15:27 2014 +0000
- Revision:
- 0:0d5190d379d3
Baseline for PTP (production test program) for the LPC4088 QSB + Experiment Base Board.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
embeddedartists | 0:0d5190d379d3 | 1 | /****************************************************************************** |
embeddedartists | 0:0d5190d379d3 | 2 | * Includes |
embeddedartists | 0:0d5190d379d3 | 3 | *****************************************************************************/ |
embeddedartists | 0:0d5190d379d3 | 4 | |
embeddedartists | 0:0d5190d379d3 | 5 | #include "mbed.h" |
embeddedartists | 0:0d5190d379d3 | 6 | |
embeddedartists | 0:0d5190d379d3 | 7 | #include "BubbleDemo.h" |
embeddedartists | 0:0d5190d379d3 | 8 | #include "DmTftBase.h" |
embeddedartists | 0:0d5190d379d3 | 9 | #include "wchar.h" |
embeddedartists | 0:0d5190d379d3 | 10 | |
embeddedartists | 0:0d5190d379d3 | 11 | /****************************************************************************** |
embeddedartists | 0:0d5190d379d3 | 12 | * Typedefs and defines |
embeddedartists | 0:0d5190d379d3 | 13 | *****************************************************************************/ |
embeddedartists | 0:0d5190d379d3 | 14 | |
embeddedartists | 0:0d5190d379d3 | 15 | #define PI 3.1415926535897932384626433832795 |
embeddedartists | 0:0d5190d379d3 | 16 | |
embeddedartists | 0:0d5190d379d3 | 17 | /* Red color mask, 565 mode */ |
embeddedartists | 0:0d5190d379d3 | 18 | #define REDMASK 0xF800 |
embeddedartists | 0:0d5190d379d3 | 19 | /* Red shift value, 565 mode */ |
embeddedartists | 0:0d5190d379d3 | 20 | #define REDSHIFT 11 |
embeddedartists | 0:0d5190d379d3 | 21 | /* Green color mask, 565 mode */ |
embeddedartists | 0:0d5190d379d3 | 22 | #define GREENMASK 0x07E0 |
embeddedartists | 0:0d5190d379d3 | 23 | /* Green shift value, 565 mode */ |
embeddedartists | 0:0d5190d379d3 | 24 | #define GREENSHIFT 5 |
embeddedartists | 0:0d5190d379d3 | 25 | /* Blue color mask, 565 mode */ |
embeddedartists | 0:0d5190d379d3 | 26 | #define BLUEMASK 0x001F |
embeddedartists | 0:0d5190d379d3 | 27 | /* Blue shift value, 565 mode */ |
embeddedartists | 0:0d5190d379d3 | 28 | #define BLUESHIFT 0 |
embeddedartists | 0:0d5190d379d3 | 29 | |
embeddedartists | 0:0d5190d379d3 | 30 | /* Number of colors in 565 mode */ |
embeddedartists | 0:0d5190d379d3 | 31 | #define NUM_COLORS 65536 |
embeddedartists | 0:0d5190d379d3 | 32 | /* Number of red colors in 565 mode */ |
embeddedartists | 0:0d5190d379d3 | 33 | #define RED_COLORS 0x20 |
embeddedartists | 0:0d5190d379d3 | 34 | /* Number of green colors in 565 mode */ |
embeddedartists | 0:0d5190d379d3 | 35 | #define GREEN_COLORS 0x40 |
embeddedartists | 0:0d5190d379d3 | 36 | /* Number of blue colors in 565 mode */ |
embeddedartists | 0:0d5190d379d3 | 37 | #define BLUE_COLORS 0x20 |
embeddedartists | 0:0d5190d379d3 | 38 | |
embeddedartists | 0:0d5190d379d3 | 39 | /****************************************************************************** |
embeddedartists | 0:0d5190d379d3 | 40 | * Local variables |
embeddedartists | 0:0d5190d379d3 | 41 | *****************************************************************************/ |
embeddedartists | 0:0d5190d379d3 | 42 | |
embeddedartists | 0:0d5190d379d3 | 43 | |
embeddedartists | 0:0d5190d379d3 | 44 | /****************************************************************************** |
embeddedartists | 0:0d5190d379d3 | 45 | * External variables |
embeddedartists | 0:0d5190d379d3 | 46 | *****************************************************************************/ |
embeddedartists | 0:0d5190d379d3 | 47 | |
embeddedartists | 0:0d5190d379d3 | 48 | |
embeddedartists | 0:0d5190d379d3 | 49 | /****************************************************************************** |
embeddedartists | 0:0d5190d379d3 | 50 | * Local functions |
embeddedartists | 0:0d5190d379d3 | 51 | *****************************************************************************/ |
embeddedartists | 0:0d5190d379d3 | 52 | |
embeddedartists | 0:0d5190d379d3 | 53 | |
embeddedartists | 0:0d5190d379d3 | 54 | void BubbleDemo::initialize() { |
embeddedartists | 0:0d5190d379d3 | 55 | float radian; |
embeddedartists | 0:0d5190d379d3 | 56 | const float phase1 = 2 * PI / 3; |
embeddedartists | 0:0d5190d379d3 | 57 | const float phase2 = 4 * PI / 3; |
embeddedartists | 0:0d5190d379d3 | 58 | |
embeddedartists | 0:0d5190d379d3 | 59 | for(i = 0; i < NumBalls; i++) { |
embeddedartists | 0:0d5190d379d3 | 60 | x[i] = this->windowX / 2; |
embeddedartists | 0:0d5190d379d3 | 61 | y[i] = this->windowY / 2; |
embeddedartists | 0:0d5190d379d3 | 62 | r[i] = i * 2 + 10; |
embeddedartists | 0:0d5190d379d3 | 63 | |
embeddedartists | 0:0d5190d379d3 | 64 | oldX[i] = x[i]; |
embeddedartists | 0:0d5190d379d3 | 65 | oldY[i] = y[i]; |
embeddedartists | 0:0d5190d379d3 | 66 | |
embeddedartists | 0:0d5190d379d3 | 67 | velX[i] = 1; |
embeddedartists | 0:0d5190d379d3 | 68 | velY[i] = 1; |
embeddedartists | 0:0d5190d379d3 | 69 | |
embeddedartists | 0:0d5190d379d3 | 70 | radian = i * 2 * PI / NumBalls; |
embeddedartists | 0:0d5190d379d3 | 71 | red[i] = cos(radian) * RED_COLORS / 2 + (RED_COLORS / 2 - 1); |
embeddedartists | 0:0d5190d379d3 | 72 | green[i] = cos(radian + phase2) * GREEN_COLORS / 2 + (GREEN_COLORS / 2 - 1); |
embeddedartists | 0:0d5190d379d3 | 73 | blue[i] = cos(radian + phase1) * BLUE_COLORS / 2 + (BLUE_COLORS / 2 - 1); |
embeddedartists | 0:0d5190d379d3 | 74 | } |
embeddedartists | 0:0d5190d379d3 | 75 | } |
embeddedartists | 0:0d5190d379d3 | 76 | |
embeddedartists | 0:0d5190d379d3 | 77 | void BubbleDemo::collision() { |
embeddedartists | 0:0d5190d379d3 | 78 | float disX = x[j] - x[i]; |
embeddedartists | 0:0d5190d379d3 | 79 | float disY = y[j] - y[i]; |
embeddedartists | 0:0d5190d379d3 | 80 | float d2 = disX * disX + disY * disY; |
embeddedartists | 0:0d5190d379d3 | 81 | |
embeddedartists | 0:0d5190d379d3 | 82 | if(d2 != 0) { |
embeddedartists | 0:0d5190d379d3 | 83 | float rij = r[i] + r[j]; |
embeddedartists | 0:0d5190d379d3 | 84 | float rij2 = rij * rij; |
embeddedartists | 0:0d5190d379d3 | 85 | |
embeddedartists | 0:0d5190d379d3 | 86 | if(d2 < rij2) { |
embeddedartists | 0:0d5190d379d3 | 87 | float ii = (disX * velX[i] + disY * velY[i]) / d2; |
embeddedartists | 0:0d5190d379d3 | 88 | float ji = (disX * velY[i] - disY * velX[i]) / d2; |
embeddedartists | 0:0d5190d379d3 | 89 | float ij = (disX * velX[j] + disY * velY[j]) / d2; |
embeddedartists | 0:0d5190d379d3 | 90 | float jj = (disX * velY[j] - disY * velX[j]) / d2; |
embeddedartists | 0:0d5190d379d3 | 91 | float ratio = rij / sqrt(d2); |
embeddedartists | 0:0d5190d379d3 | 92 | |
embeddedartists | 0:0d5190d379d3 | 93 | velX[i] = ij * disX - ii * disY; |
embeddedartists | 0:0d5190d379d3 | 94 | velY[i] = ij * disY + ii * disX; |
embeddedartists | 0:0d5190d379d3 | 95 | velX[j] = ji * disX - jj * disY; |
embeddedartists | 0:0d5190d379d3 | 96 | velY[j] = ji * disY + jj * disX; |
embeddedartists | 0:0d5190d379d3 | 97 | |
embeddedartists | 0:0d5190d379d3 | 98 | disX *= (ratio - 1) / 2; |
embeddedartists | 0:0d5190d379d3 | 99 | disY *= (ratio - 1) / 2; |
embeddedartists | 0:0d5190d379d3 | 100 | |
embeddedartists | 0:0d5190d379d3 | 101 | x[j] += disX; |
embeddedartists | 0:0d5190d379d3 | 102 | y[j] += disY; |
embeddedartists | 0:0d5190d379d3 | 103 | x[i] -= disX; |
embeddedartists | 0:0d5190d379d3 | 104 | y[i] -= disY; |
embeddedartists | 0:0d5190d379d3 | 105 | } |
embeddedartists | 0:0d5190d379d3 | 106 | } |
embeddedartists | 0:0d5190d379d3 | 107 | } |
embeddedartists | 0:0d5190d379d3 | 108 | |
embeddedartists | 0:0d5190d379d3 | 109 | void BubbleDemo::borders() { |
embeddedartists | 0:0d5190d379d3 | 110 | if(x[i] >= this->windowX - r[i] - 1) { |
embeddedartists | 0:0d5190d379d3 | 111 | x[i] = this->windowX - r[i] - 1; |
embeddedartists | 0:0d5190d379d3 | 112 | velX[i] = -velX[i]; |
embeddedartists | 0:0d5190d379d3 | 113 | } else if(x[i] <= r[i]) { |
embeddedartists | 0:0d5190d379d3 | 114 | x[i] = r[i]; |
embeddedartists | 0:0d5190d379d3 | 115 | velX[i] = -velX[i]; |
embeddedartists | 0:0d5190d379d3 | 116 | } |
embeddedartists | 0:0d5190d379d3 | 117 | |
embeddedartists | 0:0d5190d379d3 | 118 | if(y[i] >= this->windowY - r[i] - 1) { |
embeddedartists | 0:0d5190d379d3 | 119 | y[i] = this->windowY - r[i] - 1; |
embeddedartists | 0:0d5190d379d3 | 120 | velY[i] = -velY[i]; |
embeddedartists | 0:0d5190d379d3 | 121 | } else if(y[i] <= r[i]) { |
embeddedartists | 0:0d5190d379d3 | 122 | y[i] = r[i]; |
embeddedartists | 0:0d5190d379d3 | 123 | velY[i] = -velY[i]; |
embeddedartists | 0:0d5190d379d3 | 124 | } |
embeddedartists | 0:0d5190d379d3 | 125 | } |
embeddedartists | 0:0d5190d379d3 | 126 | |
embeddedartists | 0:0d5190d379d3 | 127 | void BubbleDemo::draw() { |
embeddedartists | 0:0d5190d379d3 | 128 | tft->drawCircle(oldX[i], oldY[i], r[i], BLACK); |
embeddedartists | 0:0d5190d379d3 | 129 | tft->drawCircle(x[i], y[i], r[i], (red[i] << REDSHIFT) + (green[i] << GREENSHIFT) + (blue[i] << BLUESHIFT)); |
embeddedartists | 0:0d5190d379d3 | 130 | |
embeddedartists | 0:0d5190d379d3 | 131 | oldX[i] = x[i]; |
embeddedartists | 0:0d5190d379d3 | 132 | oldY[i] = y[i]; |
embeddedartists | 0:0d5190d379d3 | 133 | } |
embeddedartists | 0:0d5190d379d3 | 134 | |
embeddedartists | 0:0d5190d379d3 | 135 | |
embeddedartists | 0:0d5190d379d3 | 136 | /****************************************************************************** |
embeddedartists | 0:0d5190d379d3 | 137 | * Public functions |
embeddedartists | 0:0d5190d379d3 | 138 | *****************************************************************************/ |
embeddedartists | 0:0d5190d379d3 | 139 | |
embeddedartists | 0:0d5190d379d3 | 140 | BubbleDemo::BubbleDemo(DmTftBase* display, uint16_t dispWidth, uint16_t dispHeight) { |
embeddedartists | 0:0d5190d379d3 | 141 | |
embeddedartists | 0:0d5190d379d3 | 142 | this->windowX = dispWidth; |
embeddedartists | 0:0d5190d379d3 | 143 | this->windowY = dispHeight; |
embeddedartists | 0:0d5190d379d3 | 144 | tft = display; |
embeddedartists | 0:0d5190d379d3 | 145 | |
embeddedartists | 0:0d5190d379d3 | 146 | initialize(); |
embeddedartists | 0:0d5190d379d3 | 147 | } |
embeddedartists | 0:0d5190d379d3 | 148 | |
embeddedartists | 0:0d5190d379d3 | 149 | void BubbleDemo::run(uint32_t loops, uint32_t delayMs) { |
embeddedartists | 0:0d5190d379d3 | 150 | |
embeddedartists | 0:0d5190d379d3 | 151 | tft->clearScreen(); |
embeddedartists | 0:0d5190d379d3 | 152 | |
embeddedartists | 0:0d5190d379d3 | 153 | for(int32_t n=0;n<loops;n++) { |
embeddedartists | 0:0d5190d379d3 | 154 | |
embeddedartists | 0:0d5190d379d3 | 155 | for(i = 0; i < NumBalls; i++) { |
embeddedartists | 0:0d5190d379d3 | 156 | x[i] += velX[i]; |
embeddedartists | 0:0d5190d379d3 | 157 | y[i] += velY[i]; |
embeddedartists | 0:0d5190d379d3 | 158 | |
embeddedartists | 0:0d5190d379d3 | 159 | for(j = i + 1; j < NumBalls; j++) |
embeddedartists | 0:0d5190d379d3 | 160 | collision(); |
embeddedartists | 0:0d5190d379d3 | 161 | |
embeddedartists | 0:0d5190d379d3 | 162 | borders(); |
embeddedartists | 0:0d5190d379d3 | 163 | |
embeddedartists | 0:0d5190d379d3 | 164 | if((int)x[i] != (int)oldX[i] || (int)y[i] != (int)oldY[i]) |
embeddedartists | 0:0d5190d379d3 | 165 | draw(); |
embeddedartists | 0:0d5190d379d3 | 166 | } |
embeddedartists | 0:0d5190d379d3 | 167 | |
embeddedartists | 0:0d5190d379d3 | 168 | wait_ms(delayMs); |
embeddedartists | 0:0d5190d379d3 | 169 | } |
embeddedartists | 0:0d5190d379d3 | 170 | } |
embeddedartists | 0:0d5190d379d3 | 171 |