Graphical demo for the LPC4088 Experiment Base Board with one of the Display Expansion Kits. This demo shows a number of bubbles bouncing around.
Graphics.cpp@0:4839ec6c350d, 2014-10-03 (annotated)
- Committer:
- embeddedartists
- Date:
- Fri Oct 03 13:09:11 2014 +0000
- Revision:
- 0:4839ec6c350d
First version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
embeddedartists | 0:4839ec6c350d | 1 | |
embeddedartists | 0:4839ec6c350d | 2 | #include "mbed.h" |
embeddedartists | 0:4839ec6c350d | 3 | #include "Graphics.h" |
embeddedartists | 0:4839ec6c350d | 4 | |
embeddedartists | 0:4839ec6c350d | 5 | |
embeddedartists | 0:4839ec6c350d | 6 | Graphics::Graphics(uint16_t *pFrmBuf, uint16_t dispWidth, uint16_t dispHeight) |
embeddedartists | 0:4839ec6c350d | 7 | { |
embeddedartists | 0:4839ec6c350d | 8 | this->windowX = dispWidth; |
embeddedartists | 0:4839ec6c350d | 9 | this->windowY = dispHeight; |
embeddedartists | 0:4839ec6c350d | 10 | this->pFrmBuf = pFrmBuf; |
embeddedartists | 0:4839ec6c350d | 11 | } |
embeddedartists | 0:4839ec6c350d | 12 | |
embeddedartists | 0:4839ec6c350d | 13 | void Graphics::setFrameBuffer( uint16_t *pFrmBuf ) |
embeddedartists | 0:4839ec6c350d | 14 | { |
embeddedartists | 0:4839ec6c350d | 15 | this->pFrmBuf = pFrmBuf; |
embeddedartists | 0:4839ec6c350d | 16 | } |
embeddedartists | 0:4839ec6c350d | 17 | |
embeddedartists | 0:4839ec6c350d | 18 | int32_t Graphics::abs(int32_t v1) const |
embeddedartists | 0:4839ec6c350d | 19 | { |
embeddedartists | 0:4839ec6c350d | 20 | if (v1 > 0) |
embeddedartists | 0:4839ec6c350d | 21 | return v1; |
embeddedartists | 0:4839ec6c350d | 22 | |
embeddedartists | 0:4839ec6c350d | 23 | return -v1; |
embeddedartists | 0:4839ec6c350d | 24 | } |
embeddedartists | 0:4839ec6c350d | 25 | |
embeddedartists | 0:4839ec6c350d | 26 | /*********************************************************************** |
embeddedartists | 0:4839ec6c350d | 27 | * |
embeddedartists | 0:4839ec6c350d | 28 | * Function: swim_put_line_raw |
embeddedartists | 0:4839ec6c350d | 29 | * |
embeddedartists | 0:4839ec6c350d | 30 | * Purpose: Draw a line on the physical display |
embeddedartists | 0:4839ec6c350d | 31 | * |
embeddedartists | 0:4839ec6c350d | 32 | * Processing: |
embeddedartists | 0:4839ec6c350d | 33 | * See function. |
embeddedartists | 0:4839ec6c350d | 34 | * |
embeddedartists | 0:4839ec6c350d | 35 | * Parameters: |
embeddedartists | 0:4839ec6c350d | 36 | * win : Window identifier |
embeddedartists | 0:4839ec6c350d | 37 | * x1 : Physical X position of X line start |
embeddedartists | 0:4839ec6c350d | 38 | * y1 : Physical Y position of Y line start |
embeddedartists | 0:4839ec6c350d | 39 | * x2 : Physical X position of X line end |
embeddedartists | 0:4839ec6c350d | 40 | * y2 : Physical Y position of Y line end |
embeddedartists | 0:4839ec6c350d | 41 | * |
embeddedartists | 0:4839ec6c350d | 42 | * Outputs: None |
embeddedartists | 0:4839ec6c350d | 43 | * |
embeddedartists | 0:4839ec6c350d | 44 | * Returns: Nothing |
embeddedartists | 0:4839ec6c350d | 45 | * |
embeddedartists | 0:4839ec6c350d | 46 | * Notes: None |
embeddedartists | 0:4839ec6c350d | 47 | * |
embeddedartists | 0:4839ec6c350d | 48 | **********************************************************************/ |
embeddedartists | 0:4839ec6c350d | 49 | void Graphics::put_line(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int16_t color) |
embeddedartists | 0:4839ec6c350d | 50 | { |
embeddedartists | 0:4839ec6c350d | 51 | int32_t e2, sx, sy, dx, dy, err; |
embeddedartists | 0:4839ec6c350d | 52 | |
embeddedartists | 0:4839ec6c350d | 53 | /* calculate delta_x and delta_y */ |
embeddedartists | 0:4839ec6c350d | 54 | dx = abs(x2 - x1); |
embeddedartists | 0:4839ec6c350d | 55 | dy = abs(y2 - y1); |
embeddedartists | 0:4839ec6c350d | 56 | |
embeddedartists | 0:4839ec6c350d | 57 | /* set the direction for the step for both x and y, and |
embeddedartists | 0:4839ec6c350d | 58 | initialize the error */ |
embeddedartists | 0:4839ec6c350d | 59 | if (x1 < x2) |
embeddedartists | 0:4839ec6c350d | 60 | sx = 1; |
embeddedartists | 0:4839ec6c350d | 61 | else |
embeddedartists | 0:4839ec6c350d | 62 | sx = -1; |
embeddedartists | 0:4839ec6c350d | 63 | |
embeddedartists | 0:4839ec6c350d | 64 | if (y1 < y2) |
embeddedartists | 0:4839ec6c350d | 65 | sy = 1; |
embeddedartists | 0:4839ec6c350d | 66 | else |
embeddedartists | 0:4839ec6c350d | 67 | sy = -1; |
embeddedartists | 0:4839ec6c350d | 68 | |
embeddedartists | 0:4839ec6c350d | 69 | err = dx - dy; |
embeddedartists | 0:4839ec6c350d | 70 | |
embeddedartists | 0:4839ec6c350d | 71 | while (1) |
embeddedartists | 0:4839ec6c350d | 72 | { |
embeddedartists | 0:4839ec6c350d | 73 | if ((x1 >= 0) && (x1 < this->windowX) && |
embeddedartists | 0:4839ec6c350d | 74 | (y1 >= 0) && (y1 < this->windowY)) |
embeddedartists | 0:4839ec6c350d | 75 | this->pFrmBuf[x1 + (this->windowX*y1)] = color; |
embeddedartists | 0:4839ec6c350d | 76 | |
embeddedartists | 0:4839ec6c350d | 77 | if ((x1 == x2) && (y1 == y2)) |
embeddedartists | 0:4839ec6c350d | 78 | return; |
embeddedartists | 0:4839ec6c350d | 79 | |
embeddedartists | 0:4839ec6c350d | 80 | e2 = 2 * err; |
embeddedartists | 0:4839ec6c350d | 81 | if (e2 > -dy) |
embeddedartists | 0:4839ec6c350d | 82 | { |
embeddedartists | 0:4839ec6c350d | 83 | err -= dy; |
embeddedartists | 0:4839ec6c350d | 84 | x1 += sx; |
embeddedartists | 0:4839ec6c350d | 85 | } |
embeddedartists | 0:4839ec6c350d | 86 | if (e2 < dx) |
embeddedartists | 0:4839ec6c350d | 87 | { |
embeddedartists | 0:4839ec6c350d | 88 | err += dx; |
embeddedartists | 0:4839ec6c350d | 89 | y1 += sy; |
embeddedartists | 0:4839ec6c350d | 90 | } |
embeddedartists | 0:4839ec6c350d | 91 | } |
embeddedartists | 0:4839ec6c350d | 92 | } |
embeddedartists | 0:4839ec6c350d | 93 | |
embeddedartists | 0:4839ec6c350d | 94 | /*********************************************************************** |
embeddedartists | 0:4839ec6c350d | 95 | * |
embeddedartists | 0:4839ec6c350d | 96 | * Function: plot4points |
embeddedartists | 0:4839ec6c350d | 97 | * |
embeddedartists | 0:4839ec6c350d | 98 | * Purpose: |
embeddedartists | 0:4839ec6c350d | 99 | * |
embeddedartists | 0:4839ec6c350d | 100 | * Processing: |
embeddedartists | 0:4839ec6c350d | 101 | * See function. |
embeddedartists | 0:4839ec6c350d | 102 | * |
embeddedartists | 0:4839ec6c350d | 103 | * Parameters: |
embeddedartists | 0:4839ec6c350d | 104 | * win : Window identifier |
embeddedartists | 0:4839ec6c350d | 105 | * cx : |
embeddedartists | 0:4839ec6c350d | 106 | * cy : |
embeddedartists | 0:4839ec6c350d | 107 | * x : |
embeddedartists | 0:4839ec6c350d | 108 | * y : |
embeddedartists | 0:4839ec6c350d | 109 | * Filled : |
embeddedartists | 0:4839ec6c350d | 110 | * |
embeddedartists | 0:4839ec6c350d | 111 | * Outputs: None |
embeddedartists | 0:4839ec6c350d | 112 | * |
embeddedartists | 0:4839ec6c350d | 113 | * Returns: Nothing |
embeddedartists | 0:4839ec6c350d | 114 | * |
embeddedartists | 0:4839ec6c350d | 115 | * Notes: None |
embeddedartists | 0:4839ec6c350d | 116 | * |
embeddedartists | 0:4839ec6c350d | 117 | **********************************************************************/ |
embeddedartists | 0:4839ec6c350d | 118 | void Graphics::plot4points( int32_t cx, int32_t cy, int32_t x, int32_t y, int16_t color, int32_t Filled ) |
embeddedartists | 0:4839ec6c350d | 119 | { |
embeddedartists | 0:4839ec6c350d | 120 | int16_t x0, x1, y0, y1; |
embeddedartists | 0:4839ec6c350d | 121 | |
embeddedartists | 0:4839ec6c350d | 122 | y0 = cy + y; |
embeddedartists | 0:4839ec6c350d | 123 | y1 = cy - y; |
embeddedartists | 0:4839ec6c350d | 124 | if( Filled ) |
embeddedartists | 0:4839ec6c350d | 125 | { |
embeddedartists | 0:4839ec6c350d | 126 | for( x0=cx - x; x0<=cx + x; x0++ ) |
embeddedartists | 0:4839ec6c350d | 127 | { |
embeddedartists | 0:4839ec6c350d | 128 | if ((x0>=0) && (x0<this->windowX) && (y0>=0) && (y0<this->windowY)) |
embeddedartists | 0:4839ec6c350d | 129 | this->pFrmBuf[x0 + (this->windowX*y0)] = color; |
embeddedartists | 0:4839ec6c350d | 130 | if ((x0>=0) && (x0<this->windowX) && (y1>=0) && (y1<this->windowY)) |
embeddedartists | 0:4839ec6c350d | 131 | this->pFrmBuf[x0 + (this->windowX*y1)] = color; |
embeddedartists | 0:4839ec6c350d | 132 | } |
embeddedartists | 0:4839ec6c350d | 133 | } |
embeddedartists | 0:4839ec6c350d | 134 | else |
embeddedartists | 0:4839ec6c350d | 135 | { |
embeddedartists | 0:4839ec6c350d | 136 | x0 = cx + x; |
embeddedartists | 0:4839ec6c350d | 137 | x1 = cx - x; |
embeddedartists | 0:4839ec6c350d | 138 | if ((x0>=0) && (x0<this->windowX) && (y0>=0) && (y0<this->windowY)) |
embeddedartists | 0:4839ec6c350d | 139 | this->pFrmBuf[x0 + (this->windowX*y0)] = color; |
embeddedartists | 0:4839ec6c350d | 140 | if ((x != 0) && |
embeddedartists | 0:4839ec6c350d | 141 | (x1>=0) && (x1<this->windowX) && (y0>=0) && (y0<this->windowY)) |
embeddedartists | 0:4839ec6c350d | 142 | this->pFrmBuf[x1 + (this->windowX*y0)] = color; |
embeddedartists | 0:4839ec6c350d | 143 | if ((y != 0) && |
embeddedartists | 0:4839ec6c350d | 144 | (x0>=0) && (x0<this->windowX) && (y1>=0) && (y1<this->windowY)) |
embeddedartists | 0:4839ec6c350d | 145 | this->pFrmBuf[x0 + (this->windowX*y1)] = color; |
embeddedartists | 0:4839ec6c350d | 146 | if ((x != 0 && y != 0) && |
embeddedartists | 0:4839ec6c350d | 147 | (x1>=0) && (x1<this->windowX) && (y1>=0) && (y1<this->windowY)) |
embeddedartists | 0:4839ec6c350d | 148 | this->pFrmBuf[x1 + (this->windowX*y1)] = color; |
embeddedartists | 0:4839ec6c350d | 149 | } |
embeddedartists | 0:4839ec6c350d | 150 | } |
embeddedartists | 0:4839ec6c350d | 151 | |
embeddedartists | 0:4839ec6c350d | 152 | /*********************************************************************** |
embeddedartists | 0:4839ec6c350d | 153 | * |
embeddedartists | 0:4839ec6c350d | 154 | * Function: plot8points |
embeddedartists | 0:4839ec6c350d | 155 | * |
embeddedartists | 0:4839ec6c350d | 156 | * Purpose: |
embeddedartists | 0:4839ec6c350d | 157 | * |
embeddedartists | 0:4839ec6c350d | 158 | * Processing: |
embeddedartists | 0:4839ec6c350d | 159 | * See function. |
embeddedartists | 0:4839ec6c350d | 160 | * |
embeddedartists | 0:4839ec6c350d | 161 | * Parameters: |
embeddedartists | 0:4839ec6c350d | 162 | * win : Window identifier |
embeddedartists | 0:4839ec6c350d | 163 | * cx : |
embeddedartists | 0:4839ec6c350d | 164 | * cy : |
embeddedartists | 0:4839ec6c350d | 165 | * x : |
embeddedartists | 0:4839ec6c350d | 166 | * y : |
embeddedartists | 0:4839ec6c350d | 167 | * Filled : |
embeddedartists | 0:4839ec6c350d | 168 | * |
embeddedartists | 0:4839ec6c350d | 169 | * Outputs: None |
embeddedartists | 0:4839ec6c350d | 170 | * |
embeddedartists | 0:4839ec6c350d | 171 | * Returns: Nothing |
embeddedartists | 0:4839ec6c350d | 172 | * |
embeddedartists | 0:4839ec6c350d | 173 | * Notes: None |
embeddedartists | 0:4839ec6c350d | 174 | * |
embeddedartists | 0:4839ec6c350d | 175 | **********************************************************************/ |
embeddedartists | 0:4839ec6c350d | 176 | void Graphics::plot8points( int32_t cx, int32_t cy, int32_t x, int32_t y, int16_t color, int32_t Filled ) |
embeddedartists | 0:4839ec6c350d | 177 | { |
embeddedartists | 0:4839ec6c350d | 178 | plot4points( cx, cy, x, y, color, Filled ); |
embeddedartists | 0:4839ec6c350d | 179 | if (x != y) |
embeddedartists | 0:4839ec6c350d | 180 | plot4points( cx, cy, y, x, color, Filled ); |
embeddedartists | 0:4839ec6c350d | 181 | } |
embeddedartists | 0:4839ec6c350d | 182 | |
embeddedartists | 0:4839ec6c350d | 183 | /*********************************************************************** |
embeddedartists | 0:4839ec6c350d | 184 | * |
embeddedartists | 0:4839ec6c350d | 185 | * Function: swim_put_circle |
embeddedartists | 0:4839ec6c350d | 186 | * |
embeddedartists | 0:4839ec6c350d | 187 | * Purpose: |
embeddedartists | 0:4839ec6c350d | 188 | * |
embeddedartists | 0:4839ec6c350d | 189 | * Processing: |
embeddedartists | 0:4839ec6c350d | 190 | * See function. |
embeddedartists | 0:4839ec6c350d | 191 | * |
embeddedartists | 0:4839ec6c350d | 192 | * Parameters: |
embeddedartists | 0:4839ec6c350d | 193 | * win : Window identifier |
embeddedartists | 0:4839ec6c350d | 194 | * cx : |
embeddedartists | 0:4839ec6c350d | 195 | * cy : |
embeddedartists | 0:4839ec6c350d | 196 | * radius : |
embeddedartists | 0:4839ec6c350d | 197 | * Filled : |
embeddedartists | 0:4839ec6c350d | 198 | * |
embeddedartists | 0:4839ec6c350d | 199 | * Outputs: None |
embeddedartists | 0:4839ec6c350d | 200 | * |
embeddedartists | 0:4839ec6c350d | 201 | * Returns: Nothing |
embeddedartists | 0:4839ec6c350d | 202 | * |
embeddedartists | 0:4839ec6c350d | 203 | * Notes: None |
embeddedartists | 0:4839ec6c350d | 204 | * |
embeddedartists | 0:4839ec6c350d | 205 | **********************************************************************/ |
embeddedartists | 0:4839ec6c350d | 206 | void Graphics::put_circle( int32_t cx, int32_t cy, int16_t color, int32_t radius, int32_t Filled ) |
embeddedartists | 0:4839ec6c350d | 207 | { |
embeddedartists | 0:4839ec6c350d | 208 | int32_t Error = -radius; |
embeddedartists | 0:4839ec6c350d | 209 | int16_t x = radius; |
embeddedartists | 0:4839ec6c350d | 210 | int16_t y = 0; |
embeddedartists | 0:4839ec6c350d | 211 | |
embeddedartists | 0:4839ec6c350d | 212 | while( x >= y ) |
embeddedartists | 0:4839ec6c350d | 213 | { |
embeddedartists | 0:4839ec6c350d | 214 | plot8points( cx, cy, x, y, color, Filled ); |
embeddedartists | 0:4839ec6c350d | 215 | |
embeddedartists | 0:4839ec6c350d | 216 | Error += y; |
embeddedartists | 0:4839ec6c350d | 217 | ++y; |
embeddedartists | 0:4839ec6c350d | 218 | Error += y; |
embeddedartists | 0:4839ec6c350d | 219 | |
embeddedartists | 0:4839ec6c350d | 220 | if( Error >= 0 ) |
embeddedartists | 0:4839ec6c350d | 221 | { |
embeddedartists | 0:4839ec6c350d | 222 | --x; |
embeddedartists | 0:4839ec6c350d | 223 | Error -= x; |
embeddedartists | 0:4839ec6c350d | 224 | Error -= x; |
embeddedartists | 0:4839ec6c350d | 225 | } |
embeddedartists | 0:4839ec6c350d | 226 | } |
embeddedartists | 0:4839ec6c350d | 227 | } |
embeddedartists | 0:4839ec6c350d | 228 | |
embeddedartists | 0:4839ec6c350d | 229 | void Graphics::put_dot( int32_t cx, int32_t cy, int16_t color ) |
embeddedartists | 0:4839ec6c350d | 230 | { |
embeddedartists | 0:4839ec6c350d | 231 | int size = 1; |
embeddedartists | 0:4839ec6c350d | 232 | for (int y1 = cy - size; y1 <= (cy + size); y1++) { |
embeddedartists | 0:4839ec6c350d | 233 | for (int x1 = cx - size; x1 <= (cx + size); x1++) { |
embeddedartists | 0:4839ec6c350d | 234 | if ((x1 >= 0) && (x1 < this->windowX) && (y1 >= 0) && (y1 < this->windowY)) { |
embeddedartists | 0:4839ec6c350d | 235 | this->pFrmBuf[x1 + (this->windowX*y1)] = color; |
embeddedartists | 0:4839ec6c350d | 236 | } |
embeddedartists | 0:4839ec6c350d | 237 | } |
embeddedartists | 0:4839ec6c350d | 238 | } |
embeddedartists | 0:4839ec6c350d | 239 | } |
embeddedartists | 0:4839ec6c350d | 240 | |
embeddedartists | 0:4839ec6c350d | 241 |