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