Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: ME503_VehicleAssembly
Adafruit_GFX.cpp
00001 /* 00002 This is the core graphics library for all our displays, 00003 providing a common set of graphics primitives (points, lines, circles, etc.). 00004 It needs to be paired with a hardware-specific library for each display device we carry (to handle the lower-level functions). 00005 00006 Adafruit invests time and resources providing this open source code, 00007 please support Adafruit and open-source hardware by purchasing products from Adafruit! 00008 00009 Written by Limor Fried/Ladyada for Adafruit Industries. 00010 BSD license, check license.txt for more information. 00011 All text above must be included in any redistribution. 00012 */ 00013 00014 /* 00015 * Modified by Tatsuki Fukuda 25/5/2014 for use in mbed 00016 */ 00017 00018 #include "Adafruit_GFX.h" 00019 00020 // Draw a circle outline 00021 void Adafruit_GFX::drawCircle(int16_t x0, int16_t y0, int16_t r, 00022 uint16_t color) 00023 { 00024 int16_t f = 1 - r; 00025 int16_t ddF_x = 1; 00026 int16_t ddF_y = -2 * r; 00027 int16_t x = 0; 00028 int16_t y = r; 00029 00030 drawPixel(x0 , y0+r, color); 00031 drawPixel(x0 , y0-r, color); 00032 drawPixel(x0+r, y0 , color); 00033 drawPixel(x0-r, y0 , color); 00034 00035 while (x<y) { 00036 if (f >= 0) { 00037 y--; 00038 ddF_y += 2; 00039 f += ddF_y; 00040 } 00041 x++; 00042 ddF_x += 2; 00043 f += ddF_x; 00044 00045 drawPixel(x0 + x, y0 + y, color); 00046 drawPixel(x0 - x, y0 + y, color); 00047 drawPixel(x0 + x, y0 - y, color); 00048 drawPixel(x0 - x, y0 - y, color); 00049 drawPixel(x0 + y, y0 + x, color); 00050 drawPixel(x0 - y, y0 + x, color); 00051 drawPixel(x0 + y, y0 - x, color); 00052 drawPixel(x0 - y, y0 - x, color); 00053 } 00054 } 00055 00056 void Adafruit_GFX::drawCircleHelper( int16_t x0, int16_t y0, 00057 int16_t r, uint8_t cornername, uint16_t color) 00058 { 00059 int16_t f = 1 - r; 00060 int16_t ddF_x = 1; 00061 int16_t ddF_y = -2 * r; 00062 int16_t x = 0; 00063 int16_t y = r; 00064 00065 while (x<y) { 00066 if (f >= 0) { 00067 y--; 00068 ddF_y += 2; 00069 f += ddF_y; 00070 } 00071 x++; 00072 ddF_x += 2; 00073 f += ddF_x; 00074 if (cornername & 0x4) { 00075 drawPixel(x0 + x, y0 + y, color); 00076 drawPixel(x0 + y, y0 + x, color); 00077 } 00078 if (cornername & 0x2) { 00079 drawPixel(x0 + x, y0 - y, color); 00080 drawPixel(x0 + y, y0 - x, color); 00081 } 00082 if (cornername & 0x8) { 00083 drawPixel(x0 - y, y0 + x, color); 00084 drawPixel(x0 - x, y0 + y, color); 00085 } 00086 if (cornername & 0x1) { 00087 drawPixel(x0 - y, y0 - x, color); 00088 drawPixel(x0 - x, y0 - y, color); 00089 } 00090 } 00091 } 00092 00093 void Adafruit_GFX::fillCircle(int16_t x0, int16_t y0, int16_t r, 00094 uint16_t color) 00095 { 00096 drawFastVLine(x0, y0-r, 2*r+1, color); 00097 fillCircleHelper(x0, y0, r, 3, 0, color); 00098 } 00099 00100 // Used to do circles and roundrects 00101 void Adafruit_GFX::fillCircleHelper(int16_t x0, int16_t y0, int16_t r, 00102 uint8_t cornername, int16_t delta, uint16_t color) 00103 { 00104 00105 int16_t f = 1 - r; 00106 int16_t ddF_x = 1; 00107 int16_t ddF_y = -2 * r; 00108 int16_t x = 0; 00109 int16_t y = r; 00110 00111 while (x<y) { 00112 if (f >= 0) { 00113 y--; 00114 ddF_y += 2; 00115 f += ddF_y; 00116 } 00117 x++; 00118 ddF_x += 2; 00119 f += ddF_x; 00120 00121 if (cornername & 0x1) { 00122 drawFastVLine(x0+x, y0-y, 2*y+1+delta, color); 00123 drawFastVLine(x0+y, y0-x, 2*x+1+delta, color); 00124 } 00125 if (cornername & 0x2) { 00126 drawFastVLine(x0-x, y0-y, 2*y+1+delta, color); 00127 drawFastVLine(x0-y, y0-x, 2*x+1+delta, color); 00128 } 00129 } 00130 } 00131 00132 // Bresenham's algorithm - thx wikpedia 00133 void Adafruit_GFX::drawLine(int16_t x0, int16_t y0, 00134 int16_t x1, int16_t y1, 00135 uint16_t color) 00136 { 00137 int16_t steep = abs(y1 - y0) > abs(x1 - x0); 00138 if (steep) { 00139 swap(x0, y0); 00140 swap(x1, y1); 00141 } 00142 00143 if (x0 > x1) { 00144 swap(x0, x1); 00145 swap(y0, y1); 00146 } 00147 00148 int16_t dx, dy; 00149 dx = x1 - x0; 00150 dy = abs(y1 - y0); 00151 00152 int16_t err = dx / 2; 00153 int16_t ystep; 00154 00155 if (y0 < y1) { 00156 ystep = 1; 00157 } else { 00158 ystep = -1; 00159 } 00160 00161 for (; x0<=x1; x0++) { 00162 if (steep) { 00163 drawPixel(y0, x0, color); 00164 } else { 00165 drawPixel(x0, y0, color); 00166 } 00167 err -= dy; 00168 if (err < 0) { 00169 y0 += ystep; 00170 err += dx; 00171 } 00172 } 00173 } 00174 00175 // Draw a rectangle 00176 void Adafruit_GFX::drawRect(int16_t x, int16_t y, 00177 int16_t w, int16_t h, 00178 uint16_t color) 00179 { 00180 drawFastHLine(x, y, w, color); 00181 drawFastHLine(x, y+h-1, w, color); 00182 drawFastVLine(x, y, h, color); 00183 drawFastVLine(x+w-1, y, h, color); 00184 } 00185 00186 void Adafruit_GFX::drawFastVLine(int16_t x, int16_t y, 00187 int16_t h, uint16_t color) 00188 { 00189 // Update in subclasses if desired! 00190 drawLine(x, y, x, y+h-1, color); 00191 } 00192 00193 void Adafruit_GFX::drawFastHLine(int16_t x, int16_t y, 00194 int16_t w, uint16_t color) 00195 { 00196 // Update in subclasses if desired! 00197 drawLine(x, y, x+w-1, y, color); 00198 } 00199 00200 void Adafruit_GFX::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, 00201 uint16_t color) 00202 { 00203 // Update in subclasses if desired! 00204 for (int16_t i=x; i<x+w; i++) { 00205 drawFastVLine(i, y, h, color); 00206 } 00207 } 00208 00209 void Adafruit_GFX::fillScreen(uint16_t color) 00210 { 00211 fillRect(0, 0, _width, _height, color); 00212 clearTextBuf(0,0,_width,_height); 00213 } 00214 00215 // Draw a rounded rectangle 00216 void Adafruit_GFX::drawRoundRect(int16_t x, int16_t y, int16_t w, 00217 int16_t h, int16_t r, uint16_t color) 00218 { 00219 // smarter version 00220 drawFastHLine(x+r , y , w-2*r, color); // Top 00221 drawFastHLine(x+r , y+h-1, w-2*r, color); // Bottom 00222 drawFastVLine(x , y+r , h-2*r, color); // Left 00223 drawFastVLine(x+w-1, y+r , h-2*r, color); // Right 00224 // draw four corners 00225 drawCircleHelper(x+r , y+r , r, 1, color); 00226 drawCircleHelper(x+w-r-1, y+r , r, 2, color); 00227 drawCircleHelper(x+w-r-1, y+h-r-1, r, 4, color); 00228 drawCircleHelper(x+r , y+h-r-1, r, 8, color); 00229 } 00230 00231 // Fill a rounded rectangle 00232 void Adafruit_GFX::fillRoundRect(int16_t x, int16_t y, int16_t w, 00233 int16_t h, int16_t r, uint16_t color) 00234 { 00235 // smarter version 00236 fillRect(x+r, y, w-2*r, h, color); 00237 00238 // draw four corners 00239 fillCircleHelper(x+w-r-1, y+r, r, 1, h-2*r-1, color); 00240 fillCircleHelper(x+r , y+r, r, 2, h-2*r-1, color); 00241 } 00242 00243 // Draw a triangle 00244 void Adafruit_GFX::drawTriangle(int16_t x0, int16_t y0, 00245 int16_t x1, int16_t y1, 00246 int16_t x2, int16_t y2, uint16_t color) 00247 { 00248 drawLine(x0, y0, x1, y1, color); 00249 drawLine(x1, y1, x2, y2, color); 00250 drawLine(x2, y2, x0, y0, color); 00251 } 00252 00253 // Fill a triangle 00254 void Adafruit_GFX::fillTriangle ( int16_t x0, int16_t y0, 00255 int16_t x1, int16_t y1, 00256 int16_t x2, int16_t y2, uint16_t color) 00257 { 00258 00259 int16_t a, b, y, last; 00260 00261 // Sort coordinates by Y order (y2 >= y1 >= y0) 00262 if (y0 > y1) { 00263 swap(y0, y1); 00264 swap(x0, x1); 00265 } 00266 if (y1 > y2) { 00267 swap(y2, y1); 00268 swap(x2, x1); 00269 } 00270 if (y0 > y1) { 00271 swap(y0, y1); 00272 swap(x0, x1); 00273 } 00274 00275 if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing 00276 a = b = x0; 00277 if(x1 < a) a = x1; 00278 else if(x1 > b) b = x1; 00279 if(x2 < a) a = x2; 00280 else if(x2 > b) b = x2; 00281 drawFastHLine(a, y0, b-a+1, color); 00282 return; 00283 } 00284 00285 int16_t 00286 dx01 = x1 - x0, 00287 dy01 = y1 - y0, 00288 dx02 = x2 - x0, 00289 dy02 = y2 - y0, 00290 dx12 = x2 - x1, 00291 dy12 = y2 - y1, 00292 sa = 0, 00293 sb = 0; 00294 00295 // For upper part of triangle, find scanline crossings for segments 00296 // 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1 00297 // is included here (and second loop will be skipped, avoiding a /0 00298 // error there), otherwise scanline y1 is skipped here and handled 00299 // in the second loop...which also avoids a /0 error here if y0=y1 00300 // (flat-topped triangle). 00301 if(y1 == y2) last = y1; // Include y1 scanline 00302 else last = y1-1; // Skip it 00303 00304 for(y=y0; y<=last; y++) { 00305 a = x0 + sa / dy01; 00306 b = x0 + sb / dy02; 00307 sa += dx01; 00308 sb += dx02; 00309 /* longhand: 00310 a = x0 + (x1 - x0) * (y - y0) / (y1 - y0); 00311 b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); 00312 */ 00313 if(a > b) swap(a,b); 00314 drawFastHLine(a, y, b-a+1, color); 00315 } 00316 00317 // For lower part of triangle, find scanline crossings for segments 00318 // 0-2 and 1-2. This loop is skipped if y1=y2. 00319 sa = dx12 * (y - y1); 00320 sb = dx02 * (y - y0); 00321 for(; y<=y2; y++) { 00322 a = x1 + sa / dy12; 00323 b = x0 + sb / dy02; 00324 sa += dx12; 00325 sb += dx02; 00326 /* longhand: 00327 a = x1 + (x2 - x1) * (y - y1) / (y2 - y1); 00328 b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); 00329 */ 00330 if(a > b) swap(a,b); 00331 drawFastHLine(a, y, b-a+1, color); 00332 } 00333 } 00334 00335 void Adafruit_GFX::drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) 00336 { 00337 int16_t i, j; 00338 for(j=0; j<h; j++) { 00339 for(i=0; i<w; i++ ) { 00340 if(bitmap[i+(j/8)*w]& _BV(j%8) ) 00341 drawPixel(x+i, y+j, color); 00342 } 00343 } 00344 } 00345 00346 size_t Adafruit_GFX::writeChar(uint8_t c) 00347 { 00348 if (c == '\n') { 00349 cursor_y += textsize*8; 00350 cursor_x = 0; 00351 } else if (c == '\r') { 00352 cursor_x = 0; 00353 } else { 00354 drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize); 00355 cursor_x += textsize*6; 00356 if (wrap && (cursor_x > (_width - textsize*6))) { 00357 cursor_y += textsize*8; 00358 cursor_x = 0; 00359 } 00360 } 00361 return 1; 00362 } 00363 00364 // Draw a character 00365 void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, uint8_t size) 00366 { 00367 bool to_print; 00368 if((x >= _width)|| // Clip right 00369 (y >= _height)|| // Clip bottom 00370 ((x + 6 * size - 1) < 0) || // Clip left 00371 ((y + 8 * size - 1) < 0))// Clip top 00372 return; 00373 00374 int x_temp,y_temp; 00375 for (int8_t i=0; i<6; i++ ) { 00376 uint8_t line; 00377 if (i == 5) 00378 line = 0x0; 00379 else 00380 line = font[(c*5)+i]; 00381 for (int8_t j = 0; j<8; j++) { 00382 to_print=line & 0x1; 00383 if (size ==1) { //default size 00384 if (to_print & !textbuf[x+i][y+j]) { 00385 drawPixel(x+i, y+j, color); 00386 textbuf[x+i][y+j]=true; 00387 //fillRect(x+(i*size), y+(j*size), size, size, color); 00388 } else if (!to_print & textbuf[x+i][y+j]) { 00389 drawPixel(x+i, y+j, bg); 00390 textbuf[x+i][y+j]=false; 00391 //fillRect(x+i*size, y+j*size, size, size, bg); 00392 } 00393 } else { 00394 if(to_print) { 00395 for (int8_t ii = 0; ii<size; ii++) { 00396 for (int8_t jj = 0; jj<size; jj++) { 00397 x_temp=x+i*size+ii; 00398 y_temp=y+j*size+jj; 00399 if(!textbuf[x_temp][y_temp]) { 00400 drawPixel(x_temp, y_temp, color); 00401 textbuf[x_temp][y_temp]=true; 00402 } 00403 } 00404 } 00405 } else if(!to_print){ 00406 for (int8_t ii = 0; ii<size; ii++) { 00407 for (int8_t jj = 0; jj<size; jj++) { 00408 x_temp=x+i*size+ii; 00409 y_temp=y+j*size+jj; 00410 if(textbuf[x_temp][y_temp]) { 00411 drawPixel(x_temp, y_temp, bg); 00412 textbuf[x_temp][y_temp]=false; 00413 } 00414 } 00415 } 00416 } 00417 } 00418 line >>= 1; 00419 } 00420 } 00421 } 00422 00423 void Adafruit_GFX::clearTextBuf(int x,int y,int w,int h) { 00424 for (int i=x; i<w; i++ ) { 00425 for (int j=y; j<h; j++ ) { 00426 textbuf[i][j]=false; 00427 } 00428 } 00429 } 00430 00431 void Adafruit_GFX::clearText() { 00432 for (int i=0; i<_rawWidth; i++ ) { 00433 for (int j=0; j<_rawHeight; j++ ) { 00434 if(textbuf[i][j]) { 00435 //Change color of pixel to background color 00436 drawPixel(i, j, textbgcolor); 00437 //Update buffer value 00438 textbuf[i][j]=false; 00439 } 00440 } 00441 } 00442 } 00443 00444 00445 void Adafruit_GFX::invertDisplay(bool i) 00446 { 00447 // Do nothing, must be subclassed if supported 00448 }
Generated on Mon Jul 25 2022 18:08:25 by
1.7.2