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