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