1
Adafruit_GFX.cpp@17:c0c589ffe9bf, 2022-06-17 (annotated)
- Committer:
- hisi
- Date:
- Fri Jun 17 02:24:45 2022 +0000
- Revision:
- 17:c0c589ffe9bf
- Parent:
- 14:edb3c36aa1a7
11
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nkhorman | 0:c3dcd4c4983a | 1 | /*********************************** |
hisi | 17:c0c589ffe9bf | 2 | This is a our graphics core library, for all our displays. |
nkhorman | 0:c3dcd4c4983a | 3 | We'll be adapting all the |
hisi | 17:c0c589ffe9bf | 4 | existing libaries to use this core to make updating, support |
nkhorman | 0:c3dcd4c4983a | 5 | and upgrading easier! |
nkhorman | 0:c3dcd4c4983a | 6 | |
hisi | 17:c0c589ffe9bf | 7 | Adafruit invests time and resources providing this open source code, |
hisi | 17:c0c589ffe9bf | 8 | please support Adafruit and open-source hardware by purchasing |
nkhorman | 0:c3dcd4c4983a | 9 | products from Adafruit! |
nkhorman | 0:c3dcd4c4983a | 10 | |
hisi | 17:c0c589ffe9bf | 11 | Written by Limor Fried/Ladyada for Adafruit Industries. |
nkhorman | 0:c3dcd4c4983a | 12 | BSD license, check license.txt for more information |
nkhorman | 0:c3dcd4c4983a | 13 | All text above must be included in any redistribution |
nkhorman | 0:c3dcd4c4983a | 14 | ****************************************/ |
nkhorman | 0:c3dcd4c4983a | 15 | |
nkhorman | 0:c3dcd4c4983a | 16 | /* |
nkhorman | 9:ddb97c9850a2 | 17 | * Modified by Neal Horman 7/14/2012 for use in mbed |
nkhorman | 0:c3dcd4c4983a | 18 | */ |
nkhorman | 0:c3dcd4c4983a | 19 | |
nkhorman | 0:c3dcd4c4983a | 20 | #include "mbed.h" |
nkhorman | 0:c3dcd4c4983a | 21 | |
nkhorman | 0:c3dcd4c4983a | 22 | #include "Adafruit_GFX.h" |
nkhorman | 0:c3dcd4c4983a | 23 | #include "glcdfont.h" |
nkhorman | 0:c3dcd4c4983a | 24 | |
nkhorman | 9:ddb97c9850a2 | 25 | #if defined(GFX_WANT_ABSTRACTS) |
nkhorman | 0:c3dcd4c4983a | 26 | // draw a circle outline |
nkhorman | 0:c3dcd4c4983a | 27 | void Adafruit_GFX::drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color) |
nkhorman | 0:c3dcd4c4983a | 28 | { |
nkhorman | 0:c3dcd4c4983a | 29 | int16_t f = 1 - r; |
nkhorman | 0:c3dcd4c4983a | 30 | int16_t ddF_x = 1; |
nkhorman | 0:c3dcd4c4983a | 31 | int16_t ddF_y = -2 * r; |
nkhorman | 0:c3dcd4c4983a | 32 | int16_t x = 0; |
nkhorman | 0:c3dcd4c4983a | 33 | int16_t y = r; |
hisi | 17:c0c589ffe9bf | 34 | |
nkhorman | 0:c3dcd4c4983a | 35 | drawPixel(x0, y0+r, color); |
nkhorman | 0:c3dcd4c4983a | 36 | drawPixel(x0, y0-r, color); |
nkhorman | 0:c3dcd4c4983a | 37 | drawPixel(x0+r, y0, color); |
nkhorman | 0:c3dcd4c4983a | 38 | drawPixel(x0-r, y0, color); |
hisi | 17:c0c589ffe9bf | 39 | |
hisi | 17:c0c589ffe9bf | 40 | while (x<y) { |
hisi | 17:c0c589ffe9bf | 41 | if (f >= 0) { |
nkhorman | 0:c3dcd4c4983a | 42 | y--; |
nkhorman | 0:c3dcd4c4983a | 43 | ddF_y += 2; |
nkhorman | 0:c3dcd4c4983a | 44 | f += ddF_y; |
nkhorman | 0:c3dcd4c4983a | 45 | } |
nkhorman | 0:c3dcd4c4983a | 46 | x++; |
nkhorman | 0:c3dcd4c4983a | 47 | ddF_x += 2; |
nkhorman | 0:c3dcd4c4983a | 48 | f += ddF_x; |
hisi | 17:c0c589ffe9bf | 49 | |
nkhorman | 0:c3dcd4c4983a | 50 | drawPixel(x0 + x, y0 + y, color); |
nkhorman | 0:c3dcd4c4983a | 51 | drawPixel(x0 - x, y0 + y, color); |
nkhorman | 0:c3dcd4c4983a | 52 | drawPixel(x0 + x, y0 - y, color); |
nkhorman | 0:c3dcd4c4983a | 53 | drawPixel(x0 - x, y0 - y, color); |
nkhorman | 0:c3dcd4c4983a | 54 | drawPixel(x0 + y, y0 + x, color); |
nkhorman | 0:c3dcd4c4983a | 55 | drawPixel(x0 - y, y0 + x, color); |
nkhorman | 0:c3dcd4c4983a | 56 | drawPixel(x0 + y, y0 - x, color); |
nkhorman | 0:c3dcd4c4983a | 57 | drawPixel(x0 - y, y0 - x, color); |
nkhorman | 0:c3dcd4c4983a | 58 | } |
nkhorman | 0:c3dcd4c4983a | 59 | } |
nkhorman | 0:c3dcd4c4983a | 60 | |
nkhorman | 0:c3dcd4c4983a | 61 | void Adafruit_GFX::drawCircleHelper( int16_t x0, int16_t y0, int16_t r, uint8_t cornername, uint16_t color) |
nkhorman | 0:c3dcd4c4983a | 62 | { |
nkhorman | 0:c3dcd4c4983a | 63 | int16_t f = 1 - r; |
nkhorman | 0:c3dcd4c4983a | 64 | int16_t ddF_x = 1; |
nkhorman | 0:c3dcd4c4983a | 65 | int16_t ddF_y = -2 * r; |
nkhorman | 0:c3dcd4c4983a | 66 | int16_t x = 0; |
nkhorman | 0:c3dcd4c4983a | 67 | int16_t y = r; |
hisi | 17:c0c589ffe9bf | 68 | |
hisi | 17:c0c589ffe9bf | 69 | while (x<y) { |
hisi | 17:c0c589ffe9bf | 70 | if (f >= 0) { |
nkhorman | 0:c3dcd4c4983a | 71 | y--; |
nkhorman | 0:c3dcd4c4983a | 72 | ddF_y += 2; |
nkhorman | 0:c3dcd4c4983a | 73 | f += ddF_y; |
nkhorman | 0:c3dcd4c4983a | 74 | } |
nkhorman | 0:c3dcd4c4983a | 75 | x++; |
nkhorman | 0:c3dcd4c4983a | 76 | ddF_x += 2; |
nkhorman | 0:c3dcd4c4983a | 77 | f += ddF_x; |
hisi | 17:c0c589ffe9bf | 78 | |
hisi | 17:c0c589ffe9bf | 79 | if (cornername & 0x4) { |
nkhorman | 0:c3dcd4c4983a | 80 | drawPixel(x0 + x, y0 + y, color); |
nkhorman | 0:c3dcd4c4983a | 81 | drawPixel(x0 + y, y0 + x, color); |
hisi | 17:c0c589ffe9bf | 82 | } |
nkhorman | 0:c3dcd4c4983a | 83 | |
hisi | 17:c0c589ffe9bf | 84 | if (cornername & 0x2) { |
nkhorman | 0:c3dcd4c4983a | 85 | drawPixel(x0 + x, y0 - y, color); |
nkhorman | 0:c3dcd4c4983a | 86 | drawPixel(x0 + y, y0 - x, color); |
nkhorman | 0:c3dcd4c4983a | 87 | } |
nkhorman | 0:c3dcd4c4983a | 88 | |
hisi | 17:c0c589ffe9bf | 89 | if (cornername & 0x8) { |
nkhorman | 0:c3dcd4c4983a | 90 | drawPixel(x0 - y, y0 + x, color); |
nkhorman | 0:c3dcd4c4983a | 91 | drawPixel(x0 - x, y0 + y, color); |
nkhorman | 0:c3dcd4c4983a | 92 | } |
hisi | 17:c0c589ffe9bf | 93 | |
hisi | 17:c0c589ffe9bf | 94 | if (cornername & 0x1) { |
nkhorman | 0:c3dcd4c4983a | 95 | drawPixel(x0 - y, y0 - x, color); |
nkhorman | 0:c3dcd4c4983a | 96 | drawPixel(x0 - x, y0 - y, color); |
nkhorman | 0:c3dcd4c4983a | 97 | } |
nkhorman | 0:c3dcd4c4983a | 98 | } |
nkhorman | 0:c3dcd4c4983a | 99 | } |
nkhorman | 0:c3dcd4c4983a | 100 | |
nkhorman | 0:c3dcd4c4983a | 101 | void Adafruit_GFX::fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color) |
nkhorman | 0:c3dcd4c4983a | 102 | { |
nkhorman | 0:c3dcd4c4983a | 103 | drawFastVLine(x0, y0-r, 2*r+1, color); |
nkhorman | 0:c3dcd4c4983a | 104 | fillCircleHelper(x0, y0, r, 3, 0, color); |
nkhorman | 0:c3dcd4c4983a | 105 | } |
nkhorman | 0:c3dcd4c4983a | 106 | |
nkhorman | 0:c3dcd4c4983a | 107 | // used to do circles and roundrects! |
nkhorman | 0:c3dcd4c4983a | 108 | void Adafruit_GFX::fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, uint16_t color) |
nkhorman | 0:c3dcd4c4983a | 109 | { |
nkhorman | 0:c3dcd4c4983a | 110 | int16_t f = 1 - r; |
nkhorman | 0:c3dcd4c4983a | 111 | int16_t ddF_x = 1; |
nkhorman | 0:c3dcd4c4983a | 112 | int16_t ddF_y = -2 * r; |
nkhorman | 0:c3dcd4c4983a | 113 | int16_t x = 0; |
nkhorman | 0:c3dcd4c4983a | 114 | int16_t y = r; |
hisi | 17:c0c589ffe9bf | 115 | |
hisi | 17:c0c589ffe9bf | 116 | while (x<y) { |
hisi | 17:c0c589ffe9bf | 117 | if (f >= 0) { |
nkhorman | 0:c3dcd4c4983a | 118 | y--; |
nkhorman | 0:c3dcd4c4983a | 119 | ddF_y += 2; |
nkhorman | 0:c3dcd4c4983a | 120 | f += ddF_y; |
nkhorman | 0:c3dcd4c4983a | 121 | } |
nkhorman | 0:c3dcd4c4983a | 122 | x++; |
nkhorman | 0:c3dcd4c4983a | 123 | ddF_x += 2; |
nkhorman | 0:c3dcd4c4983a | 124 | f += ddF_x; |
hisi | 17:c0c589ffe9bf | 125 | |
hisi | 17:c0c589ffe9bf | 126 | if (cornername & 0x1) { |
nkhorman | 0:c3dcd4c4983a | 127 | drawFastVLine(x0+x, y0-y, 2*y+1+delta, color); |
nkhorman | 0:c3dcd4c4983a | 128 | drawFastVLine(x0+y, y0-x, 2*x+1+delta, color); |
nkhorman | 0:c3dcd4c4983a | 129 | } |
nkhorman | 0:c3dcd4c4983a | 130 | |
hisi | 17:c0c589ffe9bf | 131 | if (cornername & 0x2) { |
nkhorman | 0:c3dcd4c4983a | 132 | drawFastVLine(x0-x, y0-y, 2*y+1+delta, color); |
nkhorman | 0:c3dcd4c4983a | 133 | drawFastVLine(x0-y, y0-x, 2*x+1+delta, color); |
nkhorman | 0:c3dcd4c4983a | 134 | } |
nkhorman | 0:c3dcd4c4983a | 135 | } |
nkhorman | 0:c3dcd4c4983a | 136 | } |
nkhorman | 9:ddb97c9850a2 | 137 | #endif |
nkhorman | 0:c3dcd4c4983a | 138 | |
nkhorman | 9:ddb97c9850a2 | 139 | #if defined(GFX_WANT_ABSTRACTS) || defined(GFX_SIZEABLE_TEXT) |
nkhorman | 0:c3dcd4c4983a | 140 | // bresenham's algorithm - thx wikpedia |
nkhorman | 0:c3dcd4c4983a | 141 | void Adafruit_GFX::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color) |
nkhorman | 0:c3dcd4c4983a | 142 | { |
nkhorman | 0:c3dcd4c4983a | 143 | int16_t steep = abs(y1 - y0) > abs(x1 - x0); |
hisi | 17:c0c589ffe9bf | 144 | |
hisi | 17:c0c589ffe9bf | 145 | if (steep) { |
nkhorman | 0:c3dcd4c4983a | 146 | swap(x0, y0); |
nkhorman | 0:c3dcd4c4983a | 147 | swap(x1, y1); |
nkhorman | 0:c3dcd4c4983a | 148 | } |
hisi | 17:c0c589ffe9bf | 149 | |
hisi | 17:c0c589ffe9bf | 150 | if (x0 > x1) { |
nkhorman | 0:c3dcd4c4983a | 151 | swap(x0, x1); |
nkhorman | 0:c3dcd4c4983a | 152 | swap(y0, y1); |
nkhorman | 0:c3dcd4c4983a | 153 | } |
hisi | 17:c0c589ffe9bf | 154 | |
nkhorman | 0:c3dcd4c4983a | 155 | int16_t dx, dy; |
nkhorman | 0:c3dcd4c4983a | 156 | dx = x1 - x0; |
nkhorman | 0:c3dcd4c4983a | 157 | dy = abs(y1 - y0); |
hisi | 17:c0c589ffe9bf | 158 | |
nkhorman | 0:c3dcd4c4983a | 159 | int16_t err = dx / 2; |
nkhorman | 0:c3dcd4c4983a | 160 | int16_t ystep; |
hisi | 17:c0c589ffe9bf | 161 | |
nkhorman | 0:c3dcd4c4983a | 162 | if (y0 < y1) |
nkhorman | 0:c3dcd4c4983a | 163 | ystep = 1; |
nkhorman | 0:c3dcd4c4983a | 164 | else |
nkhorman | 0:c3dcd4c4983a | 165 | ystep = -1; |
hisi | 17:c0c589ffe9bf | 166 | |
hisi | 17:c0c589ffe9bf | 167 | for (; x0<=x1; x0++) { |
nkhorman | 0:c3dcd4c4983a | 168 | if (steep) |
nkhorman | 0:c3dcd4c4983a | 169 | drawPixel(y0, x0, color); |
nkhorman | 0:c3dcd4c4983a | 170 | else |
nkhorman | 0:c3dcd4c4983a | 171 | drawPixel(x0, y0, color); |
nkhorman | 0:c3dcd4c4983a | 172 | |
nkhorman | 0:c3dcd4c4983a | 173 | err -= dy; |
hisi | 17:c0c589ffe9bf | 174 | if (err < 0) { |
nkhorman | 0:c3dcd4c4983a | 175 | y0 += ystep; |
nkhorman | 0:c3dcd4c4983a | 176 | err += dx; |
nkhorman | 0:c3dcd4c4983a | 177 | } |
nkhorman | 0:c3dcd4c4983a | 178 | } |
nkhorman | 0:c3dcd4c4983a | 179 | } |
nkhorman | 0:c3dcd4c4983a | 180 | |
nkhorman | 9:ddb97c9850a2 | 181 | void Adafruit_GFX::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) |
nkhorman | 9:ddb97c9850a2 | 182 | { |
nkhorman | 9:ddb97c9850a2 | 183 | // stupidest version - update in subclasses if desired! |
nkhorman | 9:ddb97c9850a2 | 184 | drawLine(x, y, x, y+h-1, color); |
nkhorman | 9:ddb97c9850a2 | 185 | } |
nkhorman | 0:c3dcd4c4983a | 186 | |
nkhorman | 9:ddb97c9850a2 | 187 | void Adafruit_GFX::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) |
nkhorman | 9:ddb97c9850a2 | 188 | { |
nkhorman | 9:ddb97c9850a2 | 189 | // stupidest version - update in subclasses if desired! |
nkhorman | 9:ddb97c9850a2 | 190 | for (int16_t i=x; i<x+w; i++) |
hisi | 17:c0c589ffe9bf | 191 | drawFastVLine(i, y, h, color); |
nkhorman | 9:ddb97c9850a2 | 192 | } |
nkhorman | 9:ddb97c9850a2 | 193 | #endif |
nkhorman | 9:ddb97c9850a2 | 194 | |
nkhorman | 9:ddb97c9850a2 | 195 | #if defined(GFX_WANT_ABSTRACTS) |
nkhorman | 0:c3dcd4c4983a | 196 | // draw a rectangle |
nkhorman | 0:c3dcd4c4983a | 197 | void Adafruit_GFX::drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) |
nkhorman | 0:c3dcd4c4983a | 198 | { |
nkhorman | 0:c3dcd4c4983a | 199 | drawFastHLine(x, y, w, color); |
nkhorman | 0:c3dcd4c4983a | 200 | drawFastHLine(x, y+h-1, w, color); |
nkhorman | 0:c3dcd4c4983a | 201 | drawFastVLine(x, y, h, color); |
nkhorman | 0:c3dcd4c4983a | 202 | drawFastVLine(x+w-1, y, h, color); |
nkhorman | 0:c3dcd4c4983a | 203 | } |
nkhorman | 0:c3dcd4c4983a | 204 | |
nkhorman | 0:c3dcd4c4983a | 205 | void Adafruit_GFX::drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color) |
nkhorman | 0:c3dcd4c4983a | 206 | { |
nkhorman | 0:c3dcd4c4983a | 207 | // stupidest version - update in subclasses if desired! |
nkhorman | 0:c3dcd4c4983a | 208 | drawLine(x, y, x+w-1, y, color); |
nkhorman | 0:c3dcd4c4983a | 209 | } |
nkhorman | 0:c3dcd4c4983a | 210 | |
nkhorman | 0:c3dcd4c4983a | 211 | void Adafruit_GFX::fillScreen(uint16_t color) |
nkhorman | 0:c3dcd4c4983a | 212 | { |
nkhorman | 0:c3dcd4c4983a | 213 | fillRect(0, 0, _width, _height, color); |
nkhorman | 0:c3dcd4c4983a | 214 | } |
nkhorman | 0:c3dcd4c4983a | 215 | |
nkhorman | 0:c3dcd4c4983a | 216 | // draw a rounded rectangle! |
nkhorman | 0:c3dcd4c4983a | 217 | void Adafruit_GFX::drawRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, uint16_t color) |
nkhorman | 0:c3dcd4c4983a | 218 | { |
nkhorman | 0:c3dcd4c4983a | 219 | // smarter version |
hisi | 17:c0c589ffe9bf | 220 | drawFastHLine(x+r, y, w-2*r, color); // Top |
hisi | 17:c0c589ffe9bf | 221 | drawFastHLine(x+r, y+h-1, w-2*r, color); // Bottom |
hisi | 17:c0c589ffe9bf | 222 | drawFastVLine( x, y+r, h-2*r, color); // Left |
hisi | 17:c0c589ffe9bf | 223 | drawFastVLine( x+w-1, y+r, h-2*r, color); // Right |
nkhorman | 0:c3dcd4c4983a | 224 | // draw four corners |
hisi | 17:c0c589ffe9bf | 225 | drawCircleHelper(x+r, y+r, r, 1, color); |
hisi | 17:c0c589ffe9bf | 226 | drawCircleHelper(x+w-r-1, y+r, r, 2, color); |
nkhorman | 0:c3dcd4c4983a | 227 | drawCircleHelper(x+w-r-1, y+h-r-1, r, 4, color); |
hisi | 17:c0c589ffe9bf | 228 | drawCircleHelper(x+r, y+h-r-1, r, 8, color); |
nkhorman | 0:c3dcd4c4983a | 229 | } |
nkhorman | 0:c3dcd4c4983a | 230 | |
nkhorman | 0:c3dcd4c4983a | 231 | // fill a rounded rectangle! |
nkhorman | 0:c3dcd4c4983a | 232 | void Adafruit_GFX::fillRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, uint16_t color) |
nkhorman | 0:c3dcd4c4983a | 233 | { |
nkhorman | 0:c3dcd4c4983a | 234 | // smarter version |
nkhorman | 0:c3dcd4c4983a | 235 | fillRect(x+r, y, w-2*r, h, color); |
hisi | 17:c0c589ffe9bf | 236 | |
nkhorman | 0:c3dcd4c4983a | 237 | // draw four corners |
nkhorman | 0:c3dcd4c4983a | 238 | fillCircleHelper(x+w-r-1, y+r, r, 1, h-2*r-1, color); |
hisi | 17:c0c589ffe9bf | 239 | fillCircleHelper(x+r, y+r, r, 2, h-2*r-1, color); |
nkhorman | 0:c3dcd4c4983a | 240 | } |
nkhorman | 0:c3dcd4c4983a | 241 | |
nkhorman | 0:c3dcd4c4983a | 242 | // draw a triangle! |
nkhorman | 0:c3dcd4c4983a | 243 | void Adafruit_GFX::drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color) |
nkhorman | 0:c3dcd4c4983a | 244 | { |
nkhorman | 0:c3dcd4c4983a | 245 | drawLine(x0, y0, x1, y1, color); |
nkhorman | 0:c3dcd4c4983a | 246 | drawLine(x1, y1, x2, y2, color); |
nkhorman | 0:c3dcd4c4983a | 247 | drawLine(x2, y2, x0, y0, color); |
nkhorman | 0:c3dcd4c4983a | 248 | } |
nkhorman | 0:c3dcd4c4983a | 249 | |
nkhorman | 0:c3dcd4c4983a | 250 | // fill a triangle! |
nkhorman | 0:c3dcd4c4983a | 251 | void Adafruit_GFX::fillTriangle ( int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color) |
nkhorman | 0:c3dcd4c4983a | 252 | { |
nkhorman | 0:c3dcd4c4983a | 253 | int16_t a, b, y, last; |
hisi | 17:c0c589ffe9bf | 254 | |
nkhorman | 0:c3dcd4c4983a | 255 | // Sort coordinates by Y order (y2 >= y1 >= y0) |
nkhorman | 0:c3dcd4c4983a | 256 | if (y0 > y1) |
hisi | 17:c0c589ffe9bf | 257 | swap(y0, y1); |
hisi | 17:c0c589ffe9bf | 258 | swap(x0, x1); |
nkhorman | 0:c3dcd4c4983a | 259 | |
nkhorman | 0:c3dcd4c4983a | 260 | if (y1 > y2) |
hisi | 17:c0c589ffe9bf | 261 | swap(y2, y1); |
hisi | 17:c0c589ffe9bf | 262 | swap(x2, x1); |
nkhorman | 0:c3dcd4c4983a | 263 | |
nkhorman | 0:c3dcd4c4983a | 264 | if (y0 > y1) |
hisi | 17:c0c589ffe9bf | 265 | swap(y0, y1); |
hisi | 17:c0c589ffe9bf | 266 | swap(x0, x1); |
nkhorman | 0:c3dcd4c4983a | 267 | |
hisi | 17:c0c589ffe9bf | 268 | |
hisi | 17:c0c589ffe9bf | 269 | if(y0 == y2) { |
hisi | 17:c0c589ffe9bf | 270 | // Handle awkward all-on-same-line case as its own thing |
nkhorman | 0:c3dcd4c4983a | 271 | a = b = x0; |
nkhorman | 0:c3dcd4c4983a | 272 | if(x1 < a) |
nkhorman | 0:c3dcd4c4983a | 273 | a = x1; |
nkhorman | 0:c3dcd4c4983a | 274 | else if(x1 > b) |
nkhorman | 0:c3dcd4c4983a | 275 | b = x1; |
hisi | 17:c0c589ffe9bf | 276 | |
nkhorman | 0:c3dcd4c4983a | 277 | if(x2 < a) |
nkhorman | 0:c3dcd4c4983a | 278 | a = x2; |
nkhorman | 0:c3dcd4c4983a | 279 | else if(x2 > b) b = x2; |
hisi | 17:c0c589ffe9bf | 280 | drawFastHLine(a, y0, b-a+1, color); |
nkhorman | 0:c3dcd4c4983a | 281 | return; |
nkhorman | 0:c3dcd4c4983a | 282 | } |
nkhorman | 0:c3dcd4c4983a | 283 | |
nkhorman | 0:c3dcd4c4983a | 284 | int16_t |
hisi | 17:c0c589ffe9bf | 285 | dx01 = x1 - x0, |
hisi | 17:c0c589ffe9bf | 286 | dy01 = y1 - y0, |
hisi | 17:c0c589ffe9bf | 287 | dx02 = x2 - x0, |
hisi | 17:c0c589ffe9bf | 288 | dy02 = y2 - y0, |
hisi | 17:c0c589ffe9bf | 289 | dx12 = x2 - x1, |
hisi | 17:c0c589ffe9bf | 290 | dy12 = y2 - y1, |
hisi | 17:c0c589ffe9bf | 291 | sa = 0, |
hisi | 17:c0c589ffe9bf | 292 | sb = 0; |
nkhorman | 0:c3dcd4c4983a | 293 | |
nkhorman | 0:c3dcd4c4983a | 294 | // For upper part of triangle, find scanline crossings for segments |
nkhorman | 0:c3dcd4c4983a | 295 | // 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1 |
nkhorman | 0:c3dcd4c4983a | 296 | // is included here (and second loop will be skipped, avoiding a /0 |
nkhorman | 0:c3dcd4c4983a | 297 | // error there), otherwise scanline y1 is skipped here and handled |
nkhorman | 0:c3dcd4c4983a | 298 | // in the second loop...which also avoids a /0 error here if y0=y1 |
nkhorman | 0:c3dcd4c4983a | 299 | // (flat-topped triangle). |
nkhorman | 0:c3dcd4c4983a | 300 | if(y1 == y2) |
nkhorman | 0:c3dcd4c4983a | 301 | last = y1; // Include y1 scanline |
nkhorman | 0:c3dcd4c4983a | 302 | else |
nkhorman | 0:c3dcd4c4983a | 303 | last = y1-1; // Skip it |
nkhorman | 0:c3dcd4c4983a | 304 | |
hisi | 17:c0c589ffe9bf | 305 | for(y=y0; y<=last; y++) { |
nkhorman | 0:c3dcd4c4983a | 306 | a = x0 + sa / dy01; |
nkhorman | 0:c3dcd4c4983a | 307 | b = x0 + sb / dy02; |
nkhorman | 0:c3dcd4c4983a | 308 | sa += dx01; |
nkhorman | 0:c3dcd4c4983a | 309 | sb += dx02; |
nkhorman | 0:c3dcd4c4983a | 310 | /* longhand: |
nkhorman | 0:c3dcd4c4983a | 311 | a = x0 + (x1 - x0) * (y - y0) / (y1 - y0); |
nkhorman | 0:c3dcd4c4983a | 312 | b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); |
nkhorman | 0:c3dcd4c4983a | 313 | */ |
nkhorman | 0:c3dcd4c4983a | 314 | if(a > b) |
nkhorman | 0:c3dcd4c4983a | 315 | swap(a,b); |
nkhorman | 0:c3dcd4c4983a | 316 | drawFastHLine(a, y, b-a+1, color); |
nkhorman | 0:c3dcd4c4983a | 317 | } |
nkhorman | 0:c3dcd4c4983a | 318 | |
nkhorman | 0:c3dcd4c4983a | 319 | // For lower part of triangle, find scanline crossings for segments |
nkhorman | 0:c3dcd4c4983a | 320 | // 0-2 and 1-2. This loop is skipped if y1=y2. |
nkhorman | 0:c3dcd4c4983a | 321 | sa = dx12 * (y - y1); |
nkhorman | 0:c3dcd4c4983a | 322 | sb = dx02 * (y - y0); |
hisi | 17:c0c589ffe9bf | 323 | for(; y<=y2; y++) { |
nkhorman | 0:c3dcd4c4983a | 324 | a = x1 + sa / dy12; |
nkhorman | 0:c3dcd4c4983a | 325 | b = x0 + sb / dy02; |
nkhorman | 0:c3dcd4c4983a | 326 | sa += dx12; |
nkhorman | 0:c3dcd4c4983a | 327 | sb += dx02; |
nkhorman | 0:c3dcd4c4983a | 328 | /* longhand: |
nkhorman | 0:c3dcd4c4983a | 329 | a = x1 + (x2 - x1) * (y - y1) / (y2 - y1); |
nkhorman | 0:c3dcd4c4983a | 330 | b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); |
nkhorman | 0:c3dcd4c4983a | 331 | */ |
nkhorman | 0:c3dcd4c4983a | 332 | if(a > b) |
nkhorman | 0:c3dcd4c4983a | 333 | swap(a,b); |
nkhorman | 0:c3dcd4c4983a | 334 | drawFastHLine(a, y, b-a+1, color); |
nkhorman | 0:c3dcd4c4983a | 335 | } |
nkhorman | 0:c3dcd4c4983a | 336 | } |
nkhorman | 0:c3dcd4c4983a | 337 | |
nkhorman | 0:c3dcd4c4983a | 338 | void Adafruit_GFX::drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) |
nkhorman | 0:c3dcd4c4983a | 339 | { |
hisi | 17:c0c589ffe9bf | 340 | for (int16_t j=0; j<h; j++) { |
hisi | 17:c0c589ffe9bf | 341 | for (int16_t i=0; i<w; i++ ) { |
nkhorman | 0:c3dcd4c4983a | 342 | if (bitmap[i + (j/8)*w] & _BV(j%8)) |
nkhorman | 0:c3dcd4c4983a | 343 | drawPixel(x+i, y+j, color); |
nkhorman | 0:c3dcd4c4983a | 344 | } |
nkhorman | 0:c3dcd4c4983a | 345 | } |
nkhorman | 0:c3dcd4c4983a | 346 | } |
nkhorman | 14:edb3c36aa1a7 | 347 | #endif |
nkhorman | 0:c3dcd4c4983a | 348 | |
nkhorman | 0:c3dcd4c4983a | 349 | size_t Adafruit_GFX::writeChar(uint8_t c) |
nkhorman | 0:c3dcd4c4983a | 350 | { |
hisi | 17:c0c589ffe9bf | 351 | if (c == '\n') { |
nkhorman | 0:c3dcd4c4983a | 352 | cursor_y += textsize*8; |
nkhorman | 0:c3dcd4c4983a | 353 | cursor_x = 0; |
hisi | 17:c0c589ffe9bf | 354 | } else if (c == '\r') |
nkhorman | 0:c3dcd4c4983a | 355 | cursor_x = 0; |
hisi | 17:c0c589ffe9bf | 356 | else { |
nkhorman | 0:c3dcd4c4983a | 357 | drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize); |
nkhorman | 0:c3dcd4c4983a | 358 | cursor_x += textsize*6; |
hisi | 17:c0c589ffe9bf | 359 | if (wrap && (cursor_x > (_width - textsize*6))) { |
nkhorman | 0:c3dcd4c4983a | 360 | cursor_y += textsize*8; |
nkhorman | 0:c3dcd4c4983a | 361 | cursor_x = 0; |
nkhorman | 0:c3dcd4c4983a | 362 | } |
nkhorman | 0:c3dcd4c4983a | 363 | } |
nkhorman | 0:c3dcd4c4983a | 364 | return 1; |
nkhorman | 0:c3dcd4c4983a | 365 | } |
nkhorman | 0:c3dcd4c4983a | 366 | |
nkhorman | 0:c3dcd4c4983a | 367 | // draw a character |
nkhorman | 0:c3dcd4c4983a | 368 | void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, uint8_t size) |
nkhorman | 0:c3dcd4c4983a | 369 | { |
nkhorman | 0:c3dcd4c4983a | 370 | if( |
nkhorman | 0:c3dcd4c4983a | 371 | (x >= _width) || // Clip right |
nkhorman | 0:c3dcd4c4983a | 372 | (y >= _height) || // Clip bottom |
nkhorman | 0:c3dcd4c4983a | 373 | ((x + 5 * size - 1) < 0) || // Clip left |
nkhorman | 0:c3dcd4c4983a | 374 | ((y + 8 * size - 1) < 0) // Clip top |
hisi | 17:c0c589ffe9bf | 375 | ) |
hisi | 17:c0c589ffe9bf | 376 | return; |
hisi | 17:c0c589ffe9bf | 377 | |
hisi | 17:c0c589ffe9bf | 378 | for (int8_t i=0; i<6; i++ ) { |
nkhorman | 0:c3dcd4c4983a | 379 | uint8_t line = 0; |
nkhorman | 0:c3dcd4c4983a | 380 | |
hisi | 17:c0c589ffe9bf | 381 | if (i == 5) |
nkhorman | 0:c3dcd4c4983a | 382 | line = 0x0; |
hisi | 17:c0c589ffe9bf | 383 | else |
nkhorman | 0:c3dcd4c4983a | 384 | line = font[(c*5)+i]; |
hisi | 17:c0c589ffe9bf | 385 | |
hisi | 17:c0c589ffe9bf | 386 | for (int8_t j = 0; j<8; j++) { |
hisi | 17:c0c589ffe9bf | 387 | if (line & 0x1) { |
nkhorman | 9:ddb97c9850a2 | 388 | #if defined(GFX_WANT_ABSTRACTS) || defined(GFX_SIZEABLE_TEXT) |
nkhorman | 0:c3dcd4c4983a | 389 | if (size == 1) // default size |
nkhorman | 0:c3dcd4c4983a | 390 | drawPixel(x+i, y+j, color); |
nkhorman | 0:c3dcd4c4983a | 391 | else // big size |
nkhorman | 0:c3dcd4c4983a | 392 | fillRect(x+(i*size), y+(j*size), size, size, color); |
nkhorman | 9:ddb97c9850a2 | 393 | #else |
nkhorman | 9:ddb97c9850a2 | 394 | drawPixel(x+i, y+j, color); |
nkhorman | 0:c3dcd4c4983a | 395 | #endif |
hisi | 17:c0c589ffe9bf | 396 | } else if (bg != color) { |
nkhorman | 9:ddb97c9850a2 | 397 | #if defined(GFX_WANT_ABSTRACTS) || defined(GFX_SIZEABLE_TEXT) |
nkhorman | 0:c3dcd4c4983a | 398 | if (size == 1) // default size |
nkhorman | 0:c3dcd4c4983a | 399 | drawPixel(x+i, y+j, bg); |
nkhorman | 0:c3dcd4c4983a | 400 | else // big size |
nkhorman | 0:c3dcd4c4983a | 401 | fillRect(x+i*size, y+j*size, size, size, bg); |
nkhorman | 9:ddb97c9850a2 | 402 | #else |
nkhorman | 9:ddb97c9850a2 | 403 | drawPixel(x+i, y+j, bg); |
nkhorman | 0:c3dcd4c4983a | 404 | #endif |
nkhorman | 0:c3dcd4c4983a | 405 | } |
nkhorman | 0:c3dcd4c4983a | 406 | line >>= 1; |
nkhorman | 0:c3dcd4c4983a | 407 | } |
nkhorman | 0:c3dcd4c4983a | 408 | } |
nkhorman | 0:c3dcd4c4983a | 409 | } |
nkhorman | 9:ddb97c9850a2 | 410 | |
nkhorman | 0:c3dcd4c4983a | 411 | void Adafruit_GFX::setRotation(uint8_t x) |
nkhorman | 0:c3dcd4c4983a | 412 | { |
nkhorman | 0:c3dcd4c4983a | 413 | x %= 4; // cant be higher than 3 |
nkhorman | 0:c3dcd4c4983a | 414 | rotation = x; |
hisi | 17:c0c589ffe9bf | 415 | switch (x) { |
nkhorman | 0:c3dcd4c4983a | 416 | case 0: |
nkhorman | 0:c3dcd4c4983a | 417 | case 2: |
nkhorman | 0:c3dcd4c4983a | 418 | _width = _rawWidth; |
nkhorman | 0:c3dcd4c4983a | 419 | _height = _rawHeight; |
nkhorman | 0:c3dcd4c4983a | 420 | break; |
nkhorman | 0:c3dcd4c4983a | 421 | case 1: |
nkhorman | 0:c3dcd4c4983a | 422 | case 3: |
nkhorman | 0:c3dcd4c4983a | 423 | _width = _rawHeight; |
nkhorman | 0:c3dcd4c4983a | 424 | _height = _rawWidth; |
nkhorman | 0:c3dcd4c4983a | 425 | break; |
nkhorman | 0:c3dcd4c4983a | 426 | } |
nkhorman | 0:c3dcd4c4983a | 427 | } |
hisi | 17:c0c589ffe9bf | 428 | |
hisi | 17:c0c589ffe9bf | 429 | void Adafruit_GFX::drawX11BitMap(const uint8_t bitmap[],uint16_t bitMapWidth,uint16_t bitMapSize,uint16_t posX,uint16_t posY) |
hisi | 17:c0c589ffe9bf | 430 | { |
hisi | 17:c0c589ffe9bf | 431 | int16_t x1 = posX; |
hisi | 17:c0c589ffe9bf | 432 | int16_t y1 = posY; |
hisi | 17:c0c589ffe9bf | 433 | for(unsigned int i=0; i<bitMapSize; i++) { |
hisi | 17:c0c589ffe9bf | 434 | uint8_t val = bitmap[i]; |
hisi | 17:c0c589ffe9bf | 435 | for(int j=0; j<8; j++) { |
hisi | 17:c0c589ffe9bf | 436 | uint16_t pixColor; |
hisi | 17:c0c589ffe9bf | 437 | if(val>>j & 0x01) |
hisi | 17:c0c589ffe9bf | 438 | pixColor = 1; |
hisi | 17:c0c589ffe9bf | 439 | else |
hisi | 17:c0c589ffe9bf | 440 | pixColor = 0; |
hisi | 17:c0c589ffe9bf | 441 | drawPixel(x1,y1, pixColor); |
hisi | 17:c0c589ffe9bf | 442 | x1 = x1 + 1; |
hisi | 17:c0c589ffe9bf | 443 | if(x1 == posX + bitMapWidth) { |
hisi | 17:c0c589ffe9bf | 444 | x1 = posX; |
hisi | 17:c0c589ffe9bf | 445 | y1 = y1 + 1; |
hisi | 17:c0c589ffe9bf | 446 | } |
hisi | 17:c0c589ffe9bf | 447 | } |
hisi | 17:c0c589ffe9bf | 448 | } |
hisi | 17:c0c589ffe9bf | 449 | } |