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