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