Adafruit-GFX porting for mbed
Dependents: RGB_matrix_Panel RGB_matrix_Panel_modif RGB_matrix_Panel Pmod_OLEDrgb_ALS1_K64F ... more
Adafruit_GFX.cpp@0:3e9c32359703, 2014-05-23 (annotated)
- Committer:
- lelect
- Date:
- Fri May 23 15:05:48 2014 +0000
- Revision:
- 0:3e9c32359703
- Child:
- 1:c2715acb7466
Initial Commit; No compile error
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lelect | 0:3e9c32359703 | 1 | /* |
lelect | 0:3e9c32359703 | 2 | This is the core graphics library for all our displays, providing a common |
lelect | 0:3e9c32359703 | 3 | set of graphics primitives (points, lines, circles, etc.). It needs to be |
lelect | 0:3e9c32359703 | 4 | paired with a hardware-specific library for each display device we carry |
lelect | 0:3e9c32359703 | 5 | (to handle the lower-level functions). |
lelect | 0:3e9c32359703 | 6 | |
lelect | 0:3e9c32359703 | 7 | Adafruit invests time and resources providing this open source code, please |
lelect | 0:3e9c32359703 | 8 | support Adafruit & open-source hardware by purchasing products from Adafruit! |
lelect | 0:3e9c32359703 | 9 | |
lelect | 0:3e9c32359703 | 10 | Copyright (c) 2013 Adafruit Industries. All rights reserved. |
lelect | 0:3e9c32359703 | 11 | |
lelect | 0:3e9c32359703 | 12 | Redistribution and use in source and binary forms, with or without |
lelect | 0:3e9c32359703 | 13 | modification, are permitted provided that the following conditions are met: |
lelect | 0:3e9c32359703 | 14 | |
lelect | 0:3e9c32359703 | 15 | - Redistributions of source code must retain the above copyright notice, |
lelect | 0:3e9c32359703 | 16 | this list of conditions and the following disclaimer. |
lelect | 0:3e9c32359703 | 17 | - Redistributions in binary form must reproduce the above copyright notice, |
lelect | 0:3e9c32359703 | 18 | this list of conditions and the following disclaimer in the documentation |
lelect | 0:3e9c32359703 | 19 | and/or other materials provided with the distribution. |
lelect | 0:3e9c32359703 | 20 | |
lelect | 0:3e9c32359703 | 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
lelect | 0:3e9c32359703 | 22 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
lelect | 0:3e9c32359703 | 23 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
lelect | 0:3e9c32359703 | 24 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
lelect | 0:3e9c32359703 | 25 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
lelect | 0:3e9c32359703 | 26 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
lelect | 0:3e9c32359703 | 27 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
lelect | 0:3e9c32359703 | 28 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
lelect | 0:3e9c32359703 | 29 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
lelect | 0:3e9c32359703 | 30 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
lelect | 0:3e9c32359703 | 31 | POSSIBILITY OF SUCH DAMAGE. |
lelect | 0:3e9c32359703 | 32 | */ |
lelect | 0:3e9c32359703 | 33 | |
lelect | 0:3e9c32359703 | 34 | #include "mbed.h" |
lelect | 0:3e9c32359703 | 35 | #include "Adafruit_GFX.h" |
lelect | 0:3e9c32359703 | 36 | #include "glcdfont.h" |
lelect | 0:3e9c32359703 | 37 | |
lelect | 0:3e9c32359703 | 38 | |
lelect | 0:3e9c32359703 | 39 | // Draw a circle outline |
lelect | 0:3e9c32359703 | 40 | void Adafruit_GFX::drawCircle(int16_t x0, int16_t y0, int16_t r, |
lelect | 0:3e9c32359703 | 41 | uint16_t color) |
lelect | 0:3e9c32359703 | 42 | { |
lelect | 0:3e9c32359703 | 43 | int16_t f = 1 - r; |
lelect | 0:3e9c32359703 | 44 | int16_t ddF_x = 1; |
lelect | 0:3e9c32359703 | 45 | int16_t ddF_y = -2 * r; |
lelect | 0:3e9c32359703 | 46 | int16_t x = 0; |
lelect | 0:3e9c32359703 | 47 | int16_t y = r; |
lelect | 0:3e9c32359703 | 48 | |
lelect | 0:3e9c32359703 | 49 | drawPixel(x0 , y0+r, color); |
lelect | 0:3e9c32359703 | 50 | drawPixel(x0 , y0-r, color); |
lelect | 0:3e9c32359703 | 51 | drawPixel(x0+r, y0 , color); |
lelect | 0:3e9c32359703 | 52 | drawPixel(x0-r, y0 , color); |
lelect | 0:3e9c32359703 | 53 | |
lelect | 0:3e9c32359703 | 54 | while (x<y) { |
lelect | 0:3e9c32359703 | 55 | if (f >= 0) { |
lelect | 0:3e9c32359703 | 56 | y--; |
lelect | 0:3e9c32359703 | 57 | ddF_y += 2; |
lelect | 0:3e9c32359703 | 58 | f += ddF_y; |
lelect | 0:3e9c32359703 | 59 | } |
lelect | 0:3e9c32359703 | 60 | x++; |
lelect | 0:3e9c32359703 | 61 | ddF_x += 2; |
lelect | 0:3e9c32359703 | 62 | f += ddF_x; |
lelect | 0:3e9c32359703 | 63 | |
lelect | 0:3e9c32359703 | 64 | drawPixel(x0 + x, y0 + y, color); |
lelect | 0:3e9c32359703 | 65 | drawPixel(x0 - x, y0 + y, color); |
lelect | 0:3e9c32359703 | 66 | drawPixel(x0 + x, y0 - y, color); |
lelect | 0:3e9c32359703 | 67 | drawPixel(x0 - x, y0 - y, color); |
lelect | 0:3e9c32359703 | 68 | drawPixel(x0 + y, y0 + x, color); |
lelect | 0:3e9c32359703 | 69 | drawPixel(x0 - y, y0 + x, color); |
lelect | 0:3e9c32359703 | 70 | drawPixel(x0 + y, y0 - x, color); |
lelect | 0:3e9c32359703 | 71 | drawPixel(x0 - y, y0 - x, color); |
lelect | 0:3e9c32359703 | 72 | } |
lelect | 0:3e9c32359703 | 73 | } |
lelect | 0:3e9c32359703 | 74 | |
lelect | 0:3e9c32359703 | 75 | void Adafruit_GFX::drawCircleHelper( int16_t x0, int16_t y0, |
lelect | 0:3e9c32359703 | 76 | int16_t r, uint8_t cornername, uint16_t color) |
lelect | 0:3e9c32359703 | 77 | { |
lelect | 0:3e9c32359703 | 78 | int16_t f = 1 - r; |
lelect | 0:3e9c32359703 | 79 | int16_t ddF_x = 1; |
lelect | 0:3e9c32359703 | 80 | int16_t ddF_y = -2 * r; |
lelect | 0:3e9c32359703 | 81 | int16_t x = 0; |
lelect | 0:3e9c32359703 | 82 | int16_t y = r; |
lelect | 0:3e9c32359703 | 83 | |
lelect | 0:3e9c32359703 | 84 | while (x<y) { |
lelect | 0:3e9c32359703 | 85 | if (f >= 0) { |
lelect | 0:3e9c32359703 | 86 | y--; |
lelect | 0:3e9c32359703 | 87 | ddF_y += 2; |
lelect | 0:3e9c32359703 | 88 | f += ddF_y; |
lelect | 0:3e9c32359703 | 89 | } |
lelect | 0:3e9c32359703 | 90 | x++; |
lelect | 0:3e9c32359703 | 91 | ddF_x += 2; |
lelect | 0:3e9c32359703 | 92 | f += ddF_x; |
lelect | 0:3e9c32359703 | 93 | if (cornername & 0x4) { |
lelect | 0:3e9c32359703 | 94 | drawPixel(x0 + x, y0 + y, color); |
lelect | 0:3e9c32359703 | 95 | drawPixel(x0 + y, y0 + x, color); |
lelect | 0:3e9c32359703 | 96 | } |
lelect | 0:3e9c32359703 | 97 | if (cornername & 0x2) { |
lelect | 0:3e9c32359703 | 98 | drawPixel(x0 + x, y0 - y, color); |
lelect | 0:3e9c32359703 | 99 | drawPixel(x0 + y, y0 - x, color); |
lelect | 0:3e9c32359703 | 100 | } |
lelect | 0:3e9c32359703 | 101 | if (cornername & 0x8) { |
lelect | 0:3e9c32359703 | 102 | drawPixel(x0 - y, y0 + x, color); |
lelect | 0:3e9c32359703 | 103 | drawPixel(x0 - x, y0 + y, color); |
lelect | 0:3e9c32359703 | 104 | } |
lelect | 0:3e9c32359703 | 105 | if (cornername & 0x1) { |
lelect | 0:3e9c32359703 | 106 | drawPixel(x0 - y, y0 - x, color); |
lelect | 0:3e9c32359703 | 107 | drawPixel(x0 - x, y0 - y, color); |
lelect | 0:3e9c32359703 | 108 | } |
lelect | 0:3e9c32359703 | 109 | } |
lelect | 0:3e9c32359703 | 110 | } |
lelect | 0:3e9c32359703 | 111 | |
lelect | 0:3e9c32359703 | 112 | void Adafruit_GFX::fillCircle(int16_t x0, int16_t y0, int16_t r, |
lelect | 0:3e9c32359703 | 113 | uint16_t color) |
lelect | 0:3e9c32359703 | 114 | { |
lelect | 0:3e9c32359703 | 115 | drawFastVLine(x0, y0-r, 2*r+1, color); |
lelect | 0:3e9c32359703 | 116 | fillCircleHelper(x0, y0, r, 3, 0, color); |
lelect | 0:3e9c32359703 | 117 | } |
lelect | 0:3e9c32359703 | 118 | |
lelect | 0:3e9c32359703 | 119 | // Used to do circles and roundrects |
lelect | 0:3e9c32359703 | 120 | void Adafruit_GFX::fillCircleHelper(int16_t x0, int16_t y0, int16_t r, |
lelect | 0:3e9c32359703 | 121 | uint8_t cornername, int16_t delta, uint16_t color) |
lelect | 0:3e9c32359703 | 122 | { |
lelect | 0:3e9c32359703 | 123 | |
lelect | 0:3e9c32359703 | 124 | int16_t f = 1 - r; |
lelect | 0:3e9c32359703 | 125 | int16_t ddF_x = 1; |
lelect | 0:3e9c32359703 | 126 | int16_t ddF_y = -2 * r; |
lelect | 0:3e9c32359703 | 127 | int16_t x = 0; |
lelect | 0:3e9c32359703 | 128 | int16_t y = r; |
lelect | 0:3e9c32359703 | 129 | |
lelect | 0:3e9c32359703 | 130 | while (x<y) { |
lelect | 0:3e9c32359703 | 131 | if (f >= 0) { |
lelect | 0:3e9c32359703 | 132 | y--; |
lelect | 0:3e9c32359703 | 133 | ddF_y += 2; |
lelect | 0:3e9c32359703 | 134 | f += ddF_y; |
lelect | 0:3e9c32359703 | 135 | } |
lelect | 0:3e9c32359703 | 136 | x++; |
lelect | 0:3e9c32359703 | 137 | ddF_x += 2; |
lelect | 0:3e9c32359703 | 138 | f += ddF_x; |
lelect | 0:3e9c32359703 | 139 | |
lelect | 0:3e9c32359703 | 140 | if (cornername & 0x1) { |
lelect | 0:3e9c32359703 | 141 | drawFastVLine(x0+x, y0-y, 2*y+1+delta, color); |
lelect | 0:3e9c32359703 | 142 | drawFastVLine(x0+y, y0-x, 2*x+1+delta, color); |
lelect | 0:3e9c32359703 | 143 | } |
lelect | 0:3e9c32359703 | 144 | if (cornername & 0x2) { |
lelect | 0:3e9c32359703 | 145 | drawFastVLine(x0-x, y0-y, 2*y+1+delta, color); |
lelect | 0:3e9c32359703 | 146 | drawFastVLine(x0-y, y0-x, 2*x+1+delta, color); |
lelect | 0:3e9c32359703 | 147 | } |
lelect | 0:3e9c32359703 | 148 | } |
lelect | 0:3e9c32359703 | 149 | } |
lelect | 0:3e9c32359703 | 150 | |
lelect | 0:3e9c32359703 | 151 | // Bresenham's algorithm - thx wikpedia |
lelect | 0:3e9c32359703 | 152 | void Adafruit_GFX::drawLine(int16_t x0, int16_t y0, |
lelect | 0:3e9c32359703 | 153 | int16_t x1, int16_t y1, |
lelect | 0:3e9c32359703 | 154 | uint16_t color) |
lelect | 0:3e9c32359703 | 155 | { |
lelect | 0:3e9c32359703 | 156 | int16_t steep = abs(y1 - y0) > abs(x1 - x0); |
lelect | 0:3e9c32359703 | 157 | if (steep) { |
lelect | 0:3e9c32359703 | 158 | swap(x0, y0); |
lelect | 0:3e9c32359703 | 159 | swap(x1, y1); |
lelect | 0:3e9c32359703 | 160 | } |
lelect | 0:3e9c32359703 | 161 | |
lelect | 0:3e9c32359703 | 162 | if (x0 > x1) { |
lelect | 0:3e9c32359703 | 163 | swap(x0, x1); |
lelect | 0:3e9c32359703 | 164 | swap(y0, y1); |
lelect | 0:3e9c32359703 | 165 | } |
lelect | 0:3e9c32359703 | 166 | |
lelect | 0:3e9c32359703 | 167 | int16_t dx, dy; |
lelect | 0:3e9c32359703 | 168 | dx = x1 - x0; |
lelect | 0:3e9c32359703 | 169 | dy = abs(y1 - y0); |
lelect | 0:3e9c32359703 | 170 | |
lelect | 0:3e9c32359703 | 171 | int16_t err = dx / 2; |
lelect | 0:3e9c32359703 | 172 | int16_t ystep; |
lelect | 0:3e9c32359703 | 173 | |
lelect | 0:3e9c32359703 | 174 | if (y0 < y1) { |
lelect | 0:3e9c32359703 | 175 | ystep = 1; |
lelect | 0:3e9c32359703 | 176 | } else { |
lelect | 0:3e9c32359703 | 177 | ystep = -1; |
lelect | 0:3e9c32359703 | 178 | } |
lelect | 0:3e9c32359703 | 179 | |
lelect | 0:3e9c32359703 | 180 | for (; x0<=x1; x0++) { |
lelect | 0:3e9c32359703 | 181 | if (steep) { |
lelect | 0:3e9c32359703 | 182 | drawPixel(y0, x0, color); |
lelect | 0:3e9c32359703 | 183 | } else { |
lelect | 0:3e9c32359703 | 184 | drawPixel(x0, y0, color); |
lelect | 0:3e9c32359703 | 185 | } |
lelect | 0:3e9c32359703 | 186 | err -= dy; |
lelect | 0:3e9c32359703 | 187 | if (err < 0) { |
lelect | 0:3e9c32359703 | 188 | y0 += ystep; |
lelect | 0:3e9c32359703 | 189 | err += dx; |
lelect | 0:3e9c32359703 | 190 | } |
lelect | 0:3e9c32359703 | 191 | } |
lelect | 0:3e9c32359703 | 192 | } |
lelect | 0:3e9c32359703 | 193 | |
lelect | 0:3e9c32359703 | 194 | // Draw a rectangle |
lelect | 0:3e9c32359703 | 195 | void Adafruit_GFX::drawRect(int16_t x, int16_t y, |
lelect | 0:3e9c32359703 | 196 | int16_t w, int16_t h, |
lelect | 0:3e9c32359703 | 197 | uint16_t color) |
lelect | 0:3e9c32359703 | 198 | { |
lelect | 0:3e9c32359703 | 199 | drawFastHLine(x, y, w, color); |
lelect | 0:3e9c32359703 | 200 | drawFastHLine(x, y+h-1, w, color); |
lelect | 0:3e9c32359703 | 201 | drawFastVLine(x, y, h, color); |
lelect | 0:3e9c32359703 | 202 | drawFastVLine(x+w-1, y, h, color); |
lelect | 0:3e9c32359703 | 203 | } |
lelect | 0:3e9c32359703 | 204 | |
lelect | 0:3e9c32359703 | 205 | void Adafruit_GFX::drawFastVLine(int16_t x, int16_t y, |
lelect | 0:3e9c32359703 | 206 | int16_t h, uint16_t color) |
lelect | 0:3e9c32359703 | 207 | { |
lelect | 0:3e9c32359703 | 208 | // Update in subclasses if desired! |
lelect | 0:3e9c32359703 | 209 | drawLine(x, y, x, y+h-1, color); |
lelect | 0:3e9c32359703 | 210 | } |
lelect | 0:3e9c32359703 | 211 | |
lelect | 0:3e9c32359703 | 212 | void Adafruit_GFX::drawFastHLine(int16_t x, int16_t y, |
lelect | 0:3e9c32359703 | 213 | int16_t w, uint16_t color) |
lelect | 0:3e9c32359703 | 214 | { |
lelect | 0:3e9c32359703 | 215 | // Update in subclasses if desired! |
lelect | 0:3e9c32359703 | 216 | drawLine(x, y, x+w-1, y, color); |
lelect | 0:3e9c32359703 | 217 | } |
lelect | 0:3e9c32359703 | 218 | |
lelect | 0:3e9c32359703 | 219 | void Adafruit_GFX::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, |
lelect | 0:3e9c32359703 | 220 | uint16_t color) |
lelect | 0:3e9c32359703 | 221 | { |
lelect | 0:3e9c32359703 | 222 | // Update in subclasses if desired! |
lelect | 0:3e9c32359703 | 223 | for (int16_t i=x; i<x+w; i++) { |
lelect | 0:3e9c32359703 | 224 | drawFastVLine(i, y, h, color); |
lelect | 0:3e9c32359703 | 225 | } |
lelect | 0:3e9c32359703 | 226 | } |
lelect | 0:3e9c32359703 | 227 | |
lelect | 0:3e9c32359703 | 228 | void Adafruit_GFX::fillScreen(uint16_t color) |
lelect | 0:3e9c32359703 | 229 | { |
lelect | 0:3e9c32359703 | 230 | fillRect(0, 0, _width, _height, color); |
lelect | 0:3e9c32359703 | 231 | } |
lelect | 0:3e9c32359703 | 232 | |
lelect | 0:3e9c32359703 | 233 | // Draw a rounded rectangle |
lelect | 0:3e9c32359703 | 234 | void Adafruit_GFX::drawRoundRect(int16_t x, int16_t y, int16_t w, |
lelect | 0:3e9c32359703 | 235 | int16_t h, int16_t r, uint16_t color) |
lelect | 0:3e9c32359703 | 236 | { |
lelect | 0:3e9c32359703 | 237 | // smarter version |
lelect | 0:3e9c32359703 | 238 | drawFastHLine(x+r , y , w-2*r, color); // Top |
lelect | 0:3e9c32359703 | 239 | drawFastHLine(x+r , y+h-1, w-2*r, color); // Bottom |
lelect | 0:3e9c32359703 | 240 | drawFastVLine(x , y+r , h-2*r, color); // Left |
lelect | 0:3e9c32359703 | 241 | drawFastVLine(x+w-1, y+r , h-2*r, color); // Right |
lelect | 0:3e9c32359703 | 242 | // draw four corners |
lelect | 0:3e9c32359703 | 243 | drawCircleHelper(x+r , y+r , r, 1, color); |
lelect | 0:3e9c32359703 | 244 | drawCircleHelper(x+w-r-1, y+r , r, 2, color); |
lelect | 0:3e9c32359703 | 245 | drawCircleHelper(x+w-r-1, y+h-r-1, r, 4, color); |
lelect | 0:3e9c32359703 | 246 | drawCircleHelper(x+r , y+h-r-1, r, 8, color); |
lelect | 0:3e9c32359703 | 247 | } |
lelect | 0:3e9c32359703 | 248 | |
lelect | 0:3e9c32359703 | 249 | // Fill a rounded rectangle |
lelect | 0:3e9c32359703 | 250 | void Adafruit_GFX::fillRoundRect(int16_t x, int16_t y, int16_t w, |
lelect | 0:3e9c32359703 | 251 | int16_t h, int16_t r, uint16_t color) |
lelect | 0:3e9c32359703 | 252 | { |
lelect | 0:3e9c32359703 | 253 | // smarter version |
lelect | 0:3e9c32359703 | 254 | fillRect(x+r, y, w-2*r, h, color); |
lelect | 0:3e9c32359703 | 255 | |
lelect | 0:3e9c32359703 | 256 | // draw four corners |
lelect | 0:3e9c32359703 | 257 | fillCircleHelper(x+w-r-1, y+r, r, 1, h-2*r-1, color); |
lelect | 0:3e9c32359703 | 258 | fillCircleHelper(x+r , y+r, r, 2, h-2*r-1, color); |
lelect | 0:3e9c32359703 | 259 | } |
lelect | 0:3e9c32359703 | 260 | |
lelect | 0:3e9c32359703 | 261 | // Draw a triangle |
lelect | 0:3e9c32359703 | 262 | void Adafruit_GFX::drawTriangle(int16_t x0, int16_t y0, |
lelect | 0:3e9c32359703 | 263 | int16_t x1, int16_t y1, |
lelect | 0:3e9c32359703 | 264 | int16_t x2, int16_t y2, uint16_t color) |
lelect | 0:3e9c32359703 | 265 | { |
lelect | 0:3e9c32359703 | 266 | drawLine(x0, y0, x1, y1, color); |
lelect | 0:3e9c32359703 | 267 | drawLine(x1, y1, x2, y2, color); |
lelect | 0:3e9c32359703 | 268 | drawLine(x2, y2, x0, y0, color); |
lelect | 0:3e9c32359703 | 269 | } |
lelect | 0:3e9c32359703 | 270 | |
lelect | 0:3e9c32359703 | 271 | // Fill a triangle |
lelect | 0:3e9c32359703 | 272 | void Adafruit_GFX::fillTriangle ( int16_t x0, int16_t y0, |
lelect | 0:3e9c32359703 | 273 | int16_t x1, int16_t y1, |
lelect | 0:3e9c32359703 | 274 | int16_t x2, int16_t y2, uint16_t color) |
lelect | 0:3e9c32359703 | 275 | { |
lelect | 0:3e9c32359703 | 276 | |
lelect | 0:3e9c32359703 | 277 | int16_t a, b, y, last; |
lelect | 0:3e9c32359703 | 278 | |
lelect | 0:3e9c32359703 | 279 | // Sort coordinates by Y order (y2 >= y1 >= y0) |
lelect | 0:3e9c32359703 | 280 | if (y0 > y1) { |
lelect | 0:3e9c32359703 | 281 | swap(y0, y1); |
lelect | 0:3e9c32359703 | 282 | swap(x0, x1); |
lelect | 0:3e9c32359703 | 283 | } |
lelect | 0:3e9c32359703 | 284 | if (y1 > y2) { |
lelect | 0:3e9c32359703 | 285 | swap(y2, y1); |
lelect | 0:3e9c32359703 | 286 | swap(x2, x1); |
lelect | 0:3e9c32359703 | 287 | } |
lelect | 0:3e9c32359703 | 288 | if (y0 > y1) { |
lelect | 0:3e9c32359703 | 289 | swap(y0, y1); |
lelect | 0:3e9c32359703 | 290 | swap(x0, x1); |
lelect | 0:3e9c32359703 | 291 | } |
lelect | 0:3e9c32359703 | 292 | |
lelect | 0:3e9c32359703 | 293 | if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing |
lelect | 0:3e9c32359703 | 294 | a = b = x0; |
lelect | 0:3e9c32359703 | 295 | if(x1 < a) a = x1; |
lelect | 0:3e9c32359703 | 296 | else if(x1 > b) b = x1; |
lelect | 0:3e9c32359703 | 297 | if(x2 < a) a = x2; |
lelect | 0:3e9c32359703 | 298 | else if(x2 > b) b = x2; |
lelect | 0:3e9c32359703 | 299 | drawFastHLine(a, y0, b-a+1, color); |
lelect | 0:3e9c32359703 | 300 | return; |
lelect | 0:3e9c32359703 | 301 | } |
lelect | 0:3e9c32359703 | 302 | |
lelect | 0:3e9c32359703 | 303 | int16_t |
lelect | 0:3e9c32359703 | 304 | dx01 = x1 - x0, |
lelect | 0:3e9c32359703 | 305 | dy01 = y1 - y0, |
lelect | 0:3e9c32359703 | 306 | dx02 = x2 - x0, |
lelect | 0:3e9c32359703 | 307 | dy02 = y2 - y0, |
lelect | 0:3e9c32359703 | 308 | dx12 = x2 - x1, |
lelect | 0:3e9c32359703 | 309 | dy12 = y2 - y1, |
lelect | 0:3e9c32359703 | 310 | sa = 0, |
lelect | 0:3e9c32359703 | 311 | sb = 0; |
lelect | 0:3e9c32359703 | 312 | |
lelect | 0:3e9c32359703 | 313 | // For upper part of triangle, find scanline crossings for segments |
lelect | 0:3e9c32359703 | 314 | // 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1 |
lelect | 0:3e9c32359703 | 315 | // is included here (and second loop will be skipped, avoiding a /0 |
lelect | 0:3e9c32359703 | 316 | // error there), otherwise scanline y1 is skipped here and handled |
lelect | 0:3e9c32359703 | 317 | // in the second loop...which also avoids a /0 error here if y0=y1 |
lelect | 0:3e9c32359703 | 318 | // (flat-topped triangle). |
lelect | 0:3e9c32359703 | 319 | if(y1 == y2) last = y1; // Include y1 scanline |
lelect | 0:3e9c32359703 | 320 | else last = y1-1; // Skip it |
lelect | 0:3e9c32359703 | 321 | |
lelect | 0:3e9c32359703 | 322 | for(y=y0; y<=last; y++) { |
lelect | 0:3e9c32359703 | 323 | a = x0 + sa / dy01; |
lelect | 0:3e9c32359703 | 324 | b = x0 + sb / dy02; |
lelect | 0:3e9c32359703 | 325 | sa += dx01; |
lelect | 0:3e9c32359703 | 326 | sb += dx02; |
lelect | 0:3e9c32359703 | 327 | /* longhand: |
lelect | 0:3e9c32359703 | 328 | a = x0 + (x1 - x0) * (y - y0) / (y1 - y0); |
lelect | 0:3e9c32359703 | 329 | b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); |
lelect | 0:3e9c32359703 | 330 | */ |
lelect | 0:3e9c32359703 | 331 | if(a > b) swap(a,b); |
lelect | 0:3e9c32359703 | 332 | drawFastHLine(a, y, b-a+1, color); |
lelect | 0:3e9c32359703 | 333 | } |
lelect | 0:3e9c32359703 | 334 | |
lelect | 0:3e9c32359703 | 335 | // For lower part of triangle, find scanline crossings for segments |
lelect | 0:3e9c32359703 | 336 | // 0-2 and 1-2. This loop is skipped if y1=y2. |
lelect | 0:3e9c32359703 | 337 | sa = dx12 * (y - y1); |
lelect | 0:3e9c32359703 | 338 | sb = dx02 * (y - y0); |
lelect | 0:3e9c32359703 | 339 | for(; y<=y2; y++) { |
lelect | 0:3e9c32359703 | 340 | a = x1 + sa / dy12; |
lelect | 0:3e9c32359703 | 341 | b = x0 + sb / dy02; |
lelect | 0:3e9c32359703 | 342 | sa += dx12; |
lelect | 0:3e9c32359703 | 343 | sb += dx02; |
lelect | 0:3e9c32359703 | 344 | /* longhand: |
lelect | 0:3e9c32359703 | 345 | a = x1 + (x2 - x1) * (y - y1) / (y2 - y1); |
lelect | 0:3e9c32359703 | 346 | b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); |
lelect | 0:3e9c32359703 | 347 | */ |
lelect | 0:3e9c32359703 | 348 | if(a > b) swap(a,b); |
lelect | 0:3e9c32359703 | 349 | drawFastHLine(a, y, b-a+1, color); |
lelect | 0:3e9c32359703 | 350 | } |
lelect | 0:3e9c32359703 | 351 | } |
lelect | 0:3e9c32359703 | 352 | |
lelect | 0:3e9c32359703 | 353 | void Adafruit_GFX::drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) |
lelect | 0:3e9c32359703 | 354 | { |
lelect | 0:3e9c32359703 | 355 | int16_t i, j; |
lelect | 0:3e9c32359703 | 356 | for(j=0; j<h; j++) { |
lelect | 0:3e9c32359703 | 357 | for(i=0; i<w; i++ ) { |
lelect | 0:3e9c32359703 | 358 | if(bitmap[i+(j/8)*w]& _BV(j%8) ) |
lelect | 0:3e9c32359703 | 359 | drawPixel(x+i, y+j, color); |
lelect | 0:3e9c32359703 | 360 | } |
lelect | 0:3e9c32359703 | 361 | } |
lelect | 0:3e9c32359703 | 362 | } |
lelect | 0:3e9c32359703 | 363 | |
lelect | 0:3e9c32359703 | 364 | size_t Adafruit_GFX::writeChar(uint8_t c) |
lelect | 0:3e9c32359703 | 365 | { |
lelect | 0:3e9c32359703 | 366 | if (c == '\n') { |
lelect | 0:3e9c32359703 | 367 | cursor_y += textsize*8; |
lelect | 0:3e9c32359703 | 368 | cursor_x = 0; |
lelect | 0:3e9c32359703 | 369 | } else if (c == '\r') { |
lelect | 0:3e9c32359703 | 370 | cursor_x = 0; |
lelect | 0:3e9c32359703 | 371 | } else { |
lelect | 0:3e9c32359703 | 372 | drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize); |
lelect | 0:3e9c32359703 | 373 | cursor_x += textsize*6; |
lelect | 0:3e9c32359703 | 374 | if (wrap && (cursor_x > (_width - textsize*6))) { |
lelect | 0:3e9c32359703 | 375 | cursor_y += textsize*8; |
lelect | 0:3e9c32359703 | 376 | cursor_x = 0; |
lelect | 0:3e9c32359703 | 377 | } |
lelect | 0:3e9c32359703 | 378 | } |
lelect | 0:3e9c32359703 | 379 | return 1; |
lelect | 0:3e9c32359703 | 380 | } |
lelect | 0:3e9c32359703 | 381 | |
lelect | 0:3e9c32359703 | 382 | // Draw a character |
lelect | 0:3e9c32359703 | 383 | void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, uint8_t size) |
lelect | 0:3e9c32359703 | 384 | { |
lelect | 0:3e9c32359703 | 385 | if((x >= _width)|| // Clip right |
lelect | 0:3e9c32359703 | 386 | (y >= _height)|| // Clip bottom |
lelect | 0:3e9c32359703 | 387 | ((x + 6 * size - 1) < 0) || // Clip left |
lelect | 0:3e9c32359703 | 388 | ((y + 8 * size - 1) < 0))// Clip top |
lelect | 0:3e9c32359703 | 389 | return; |
lelect | 0:3e9c32359703 | 390 | |
lelect | 0:3e9c32359703 | 391 | for (int8_t i=0; i<6; i++ ) { |
lelect | 0:3e9c32359703 | 392 | uint8_t line; |
lelect | 0:3e9c32359703 | 393 | if (i == 5) |
lelect | 0:3e9c32359703 | 394 | line = 0x0; |
lelect | 0:3e9c32359703 | 395 | else |
lelect | 0:3e9c32359703 | 396 | line = font[(c*5)+i]; |
lelect | 0:3e9c32359703 | 397 | for (int8_t j = 0; j<8; j++) { |
lelect | 0:3e9c32359703 | 398 | if (line & 0x1) { |
lelect | 0:3e9c32359703 | 399 | if (size == 1) // default size |
lelect | 0:3e9c32359703 | 400 | drawPixel(x+i, y+j, color); |
lelect | 0:3e9c32359703 | 401 | else { // big size |
lelect | 0:3e9c32359703 | 402 | fillRect(x+(i*size), y+(j*size), size, size, color); |
lelect | 0:3e9c32359703 | 403 | } |
lelect | 0:3e9c32359703 | 404 | } else if (bg != color) { |
lelect | 0:3e9c32359703 | 405 | if (size == 1) // default size |
lelect | 0:3e9c32359703 | 406 | drawPixel(x+i, y+j, bg); |
lelect | 0:3e9c32359703 | 407 | else { // big size |
lelect | 0:3e9c32359703 | 408 | fillRect(x+i*size, y+j*size, size, size, bg); |
lelect | 0:3e9c32359703 | 409 | } |
lelect | 0:3e9c32359703 | 410 | } |
lelect | 0:3e9c32359703 | 411 | line >>= 1; |
lelect | 0:3e9c32359703 | 412 | } |
lelect | 0:3e9c32359703 | 413 | } |
lelect | 0:3e9c32359703 | 414 | } |
lelect | 0:3e9c32359703 | 415 | |
lelect | 0:3e9c32359703 | 416 | |
lelect | 0:3e9c32359703 | 417 | void Adafruit_GFX::invertDisplay(bool i) |
lelect | 0:3e9c32359703 | 418 | { |
lelect | 0:3e9c32359703 | 419 | // Do nothing, must be subclassed if supported |
lelect | 0:3e9c32359703 | 420 | } |