A derived version of the BSD licensed Adafrut GFX library for the SSD1306 controller for an OLED 128x32 or 128x64 display using SPI or I2C.
Fork of Adafruit_GFX by
Adafruit_GFX.cpp@9:ddb97c9850a2, 2014-10-19 (annotated)
- Committer:
- nkhorman
- Date:
- Sun Oct 19 20:55:27 2014 +0000
- Revision:
- 9:ddb97c9850a2
- Parent:
- 0:c3dcd4c4983a
- Child:
- 14:edb3c36aa1a7
c++'ify the SPI vs I2C driver portions, instead of compile time defines.; The SSD1306 driver can now be instantiated multiple times, supporting simultaneous displays of differing dimensions.
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 | 9:ddb97c9850a2 | 17 | * Modified by Neal Horman 7/14/2012 for use in mbed |
nkhorman | 0:c3dcd4c4983a | 18 | */ |
nkhorman | 0:c3dcd4c4983a | 19 | |
nkhorman | 0:c3dcd4c4983a | 20 | #include "mbed.h" |
nkhorman | 0:c3dcd4c4983a | 21 | |
nkhorman | 0:c3dcd4c4983a | 22 | #include "Adafruit_GFX.h" |
nkhorman | 0:c3dcd4c4983a | 23 | #include "glcdfont.h" |
nkhorman | 0:c3dcd4c4983a | 24 | |
nkhorman | 9:ddb97c9850a2 | 25 | #if defined(GFX_WANT_ABSTRACTS) |
nkhorman | 0:c3dcd4c4983a | 26 | // draw a circle outline |
nkhorman | 0:c3dcd4c4983a | 27 | void Adafruit_GFX::drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color) |
nkhorman | 0:c3dcd4c4983a | 28 | { |
nkhorman | 0:c3dcd4c4983a | 29 | int16_t f = 1 - r; |
nkhorman | 0:c3dcd4c4983a | 30 | int16_t ddF_x = 1; |
nkhorman | 0:c3dcd4c4983a | 31 | int16_t ddF_y = -2 * r; |
nkhorman | 0:c3dcd4c4983a | 32 | int16_t x = 0; |
nkhorman | 0:c3dcd4c4983a | 33 | int16_t y = r; |
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 | 9:ddb97c9850a2 | 149 | #endif |
nkhorman | 0:c3dcd4c4983a | 150 | |
nkhorman | 9:ddb97c9850a2 | 151 | #if defined(GFX_WANT_ABSTRACTS) || defined(GFX_SIZEABLE_TEXT) |
nkhorman | 0:c3dcd4c4983a | 152 | // bresenham's algorithm - thx wikpedia |
nkhorman | 0:c3dcd4c4983a | 153 | void Adafruit_GFX::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color) |
nkhorman | 0:c3dcd4c4983a | 154 | { |
nkhorman | 0:c3dcd4c4983a | 155 | int16_t steep = abs(y1 - y0) > abs(x1 - x0); |
nkhorman | 0:c3dcd4c4983a | 156 | |
nkhorman | 0:c3dcd4c4983a | 157 | if (steep) |
nkhorman | 0:c3dcd4c4983a | 158 | { |
nkhorman | 0:c3dcd4c4983a | 159 | swap(x0, y0); |
nkhorman | 0:c3dcd4c4983a | 160 | swap(x1, y1); |
nkhorman | 0:c3dcd4c4983a | 161 | } |
nkhorman | 0:c3dcd4c4983a | 162 | |
nkhorman | 0:c3dcd4c4983a | 163 | if (x0 > x1) |
nkhorman | 0:c3dcd4c4983a | 164 | { |
nkhorman | 0:c3dcd4c4983a | 165 | swap(x0, x1); |
nkhorman | 0:c3dcd4c4983a | 166 | swap(y0, y1); |
nkhorman | 0:c3dcd4c4983a | 167 | } |
nkhorman | 0:c3dcd4c4983a | 168 | |
nkhorman | 0:c3dcd4c4983a | 169 | int16_t dx, dy; |
nkhorman | 0:c3dcd4c4983a | 170 | dx = x1 - x0; |
nkhorman | 0:c3dcd4c4983a | 171 | dy = abs(y1 - y0); |
nkhorman | 0:c3dcd4c4983a | 172 | |
nkhorman | 0:c3dcd4c4983a | 173 | int16_t err = dx / 2; |
nkhorman | 0:c3dcd4c4983a | 174 | int16_t ystep; |
nkhorman | 0:c3dcd4c4983a | 175 | |
nkhorman | 0:c3dcd4c4983a | 176 | if (y0 < y1) |
nkhorman | 0:c3dcd4c4983a | 177 | ystep = 1; |
nkhorman | 0:c3dcd4c4983a | 178 | else |
nkhorman | 0:c3dcd4c4983a | 179 | ystep = -1; |
nkhorman | 0:c3dcd4c4983a | 180 | |
nkhorman | 0:c3dcd4c4983a | 181 | for (; x0<=x1; x0++) |
nkhorman | 0:c3dcd4c4983a | 182 | { |
nkhorman | 0:c3dcd4c4983a | 183 | if (steep) |
nkhorman | 0:c3dcd4c4983a | 184 | drawPixel(y0, x0, color); |
nkhorman | 0:c3dcd4c4983a | 185 | else |
nkhorman | 0:c3dcd4c4983a | 186 | drawPixel(x0, y0, color); |
nkhorman | 0:c3dcd4c4983a | 187 | |
nkhorman | 0:c3dcd4c4983a | 188 | err -= dy; |
nkhorman | 0:c3dcd4c4983a | 189 | if (err < 0) |
nkhorman | 0:c3dcd4c4983a | 190 | { |
nkhorman | 0:c3dcd4c4983a | 191 | y0 += ystep; |
nkhorman | 0:c3dcd4c4983a | 192 | err += dx; |
nkhorman | 0:c3dcd4c4983a | 193 | } |
nkhorman | 0:c3dcd4c4983a | 194 | } |
nkhorman | 0:c3dcd4c4983a | 195 | } |
nkhorman | 0:c3dcd4c4983a | 196 | |
nkhorman | 9:ddb97c9850a2 | 197 | void Adafruit_GFX::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) |
nkhorman | 9:ddb97c9850a2 | 198 | { |
nkhorman | 9:ddb97c9850a2 | 199 | // stupidest version - update in subclasses if desired! |
nkhorman | 9:ddb97c9850a2 | 200 | drawLine(x, y, x, y+h-1, color); |
nkhorman | 9:ddb97c9850a2 | 201 | } |
nkhorman | 0:c3dcd4c4983a | 202 | |
nkhorman | 9:ddb97c9850a2 | 203 | void Adafruit_GFX::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) |
nkhorman | 9:ddb97c9850a2 | 204 | { |
nkhorman | 9:ddb97c9850a2 | 205 | // stupidest version - update in subclasses if desired! |
nkhorman | 9:ddb97c9850a2 | 206 | for (int16_t i=x; i<x+w; i++) |
nkhorman | 9:ddb97c9850a2 | 207 | drawFastVLine(i, y, h, color); |
nkhorman | 9:ddb97c9850a2 | 208 | } |
nkhorman | 9:ddb97c9850a2 | 209 | #endif |
nkhorman | 9:ddb97c9850a2 | 210 | |
nkhorman | 9:ddb97c9850a2 | 211 | #if defined(GFX_WANT_ABSTRACTS) |
nkhorman | 0:c3dcd4c4983a | 212 | // draw a rectangle |
nkhorman | 0:c3dcd4c4983a | 213 | void Adafruit_GFX::drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) |
nkhorman | 0:c3dcd4c4983a | 214 | { |
nkhorman | 0:c3dcd4c4983a | 215 | drawFastHLine(x, y, w, color); |
nkhorman | 0:c3dcd4c4983a | 216 | drawFastHLine(x, y+h-1, w, color); |
nkhorman | 0:c3dcd4c4983a | 217 | drawFastVLine(x, y, h, color); |
nkhorman | 0:c3dcd4c4983a | 218 | drawFastVLine(x+w-1, y, h, color); |
nkhorman | 0:c3dcd4c4983a | 219 | } |
nkhorman | 0:c3dcd4c4983a | 220 | |
nkhorman | 0:c3dcd4c4983a | 221 | void Adafruit_GFX::drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color) |
nkhorman | 0:c3dcd4c4983a | 222 | { |
nkhorman | 0:c3dcd4c4983a | 223 | // stupidest version - update in subclasses if desired! |
nkhorman | 0:c3dcd4c4983a | 224 | drawLine(x, y, x+w-1, y, color); |
nkhorman | 0:c3dcd4c4983a | 225 | } |
nkhorman | 0:c3dcd4c4983a | 226 | |
nkhorman | 0:c3dcd4c4983a | 227 | void Adafruit_GFX::fillScreen(uint16_t color) |
nkhorman | 0:c3dcd4c4983a | 228 | { |
nkhorman | 0:c3dcd4c4983a | 229 | fillRect(0, 0, _width, _height, color); |
nkhorman | 0:c3dcd4c4983a | 230 | } |
nkhorman | 0:c3dcd4c4983a | 231 | |
nkhorman | 0:c3dcd4c4983a | 232 | // draw a rounded rectangle! |
nkhorman | 0:c3dcd4c4983a | 233 | 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 | 234 | { |
nkhorman | 0:c3dcd4c4983a | 235 | // smarter version |
nkhorman | 0:c3dcd4c4983a | 236 | drawFastHLine(x+r , y , w-2*r, color); // Top |
nkhorman | 0:c3dcd4c4983a | 237 | drawFastHLine(x+r , y+h-1, w-2*r, color); // Bottom |
nkhorman | 0:c3dcd4c4983a | 238 | drawFastVLine( x , y+r , h-2*r, color); // Left |
nkhorman | 0:c3dcd4c4983a | 239 | drawFastVLine( x+w-1, y+r , h-2*r, color); // Right |
nkhorman | 0:c3dcd4c4983a | 240 | // draw four corners |
nkhorman | 0:c3dcd4c4983a | 241 | drawCircleHelper(x+r , y+r , r, 1, color); |
nkhorman | 0:c3dcd4c4983a | 242 | drawCircleHelper(x+w-r-1, y+r , r, 2, color); |
nkhorman | 0:c3dcd4c4983a | 243 | drawCircleHelper(x+w-r-1, y+h-r-1, r, 4, color); |
nkhorman | 0:c3dcd4c4983a | 244 | drawCircleHelper(x+r , y+h-r-1, r, 8, color); |
nkhorman | 0:c3dcd4c4983a | 245 | } |
nkhorman | 0:c3dcd4c4983a | 246 | |
nkhorman | 0:c3dcd4c4983a | 247 | // fill a rounded rectangle! |
nkhorman | 0:c3dcd4c4983a | 248 | 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 | 249 | { |
nkhorman | 0:c3dcd4c4983a | 250 | // smarter version |
nkhorman | 0:c3dcd4c4983a | 251 | fillRect(x+r, y, w-2*r, h, color); |
nkhorman | 0:c3dcd4c4983a | 252 | |
nkhorman | 0:c3dcd4c4983a | 253 | // draw four corners |
nkhorman | 0:c3dcd4c4983a | 254 | fillCircleHelper(x+w-r-1, y+r, r, 1, h-2*r-1, color); |
nkhorman | 0:c3dcd4c4983a | 255 | fillCircleHelper(x+r , y+r, r, 2, h-2*r-1, color); |
nkhorman | 0:c3dcd4c4983a | 256 | } |
nkhorman | 0:c3dcd4c4983a | 257 | |
nkhorman | 0:c3dcd4c4983a | 258 | // draw a triangle! |
nkhorman | 0:c3dcd4c4983a | 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) |
nkhorman | 0:c3dcd4c4983a | 260 | { |
nkhorman | 0:c3dcd4c4983a | 261 | drawLine(x0, y0, x1, y1, color); |
nkhorman | 0:c3dcd4c4983a | 262 | drawLine(x1, y1, x2, y2, color); |
nkhorman | 0:c3dcd4c4983a | 263 | drawLine(x2, y2, x0, y0, color); |
nkhorman | 0:c3dcd4c4983a | 264 | } |
nkhorman | 0:c3dcd4c4983a | 265 | |
nkhorman | 0:c3dcd4c4983a | 266 | // fill a triangle! |
nkhorman | 0:c3dcd4c4983a | 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) |
nkhorman | 0:c3dcd4c4983a | 268 | { |
nkhorman | 0:c3dcd4c4983a | 269 | int16_t a, b, y, last; |
nkhorman | 0:c3dcd4c4983a | 270 | |
nkhorman | 0:c3dcd4c4983a | 271 | // Sort coordinates by Y order (y2 >= y1 >= y0) |
nkhorman | 0:c3dcd4c4983a | 272 | if (y0 > y1) |
nkhorman | 0:c3dcd4c4983a | 273 | swap(y0, y1); swap(x0, x1); |
nkhorman | 0:c3dcd4c4983a | 274 | |
nkhorman | 0:c3dcd4c4983a | 275 | if (y1 > y2) |
nkhorman | 0:c3dcd4c4983a | 276 | swap(y2, y1); swap(x2, x1); |
nkhorman | 0:c3dcd4c4983a | 277 | |
nkhorman | 0:c3dcd4c4983a | 278 | if (y0 > y1) |
nkhorman | 0:c3dcd4c4983a | 279 | swap(y0, y1); swap(x0, x1); |
nkhorman | 0:c3dcd4c4983a | 280 | |
nkhorman | 0:c3dcd4c4983a | 281 | |
nkhorman | 0:c3dcd4c4983a | 282 | if(y0 == y2) |
nkhorman | 0:c3dcd4c4983a | 283 | { // Handle awkward all-on-same-line case as its own thing |
nkhorman | 0:c3dcd4c4983a | 284 | a = b = x0; |
nkhorman | 0:c3dcd4c4983a | 285 | if(x1 < a) |
nkhorman | 0:c3dcd4c4983a | 286 | a = x1; |
nkhorman | 0:c3dcd4c4983a | 287 | else if(x1 > b) |
nkhorman | 0:c3dcd4c4983a | 288 | b = x1; |
nkhorman | 0:c3dcd4c4983a | 289 | |
nkhorman | 0:c3dcd4c4983a | 290 | if(x2 < a) |
nkhorman | 0:c3dcd4c4983a | 291 | a = x2; |
nkhorman | 0:c3dcd4c4983a | 292 | else if(x2 > b) b = x2; |
nkhorman | 0:c3dcd4c4983a | 293 | drawFastHLine(a, y0, b-a+1, color); |
nkhorman | 0:c3dcd4c4983a | 294 | return; |
nkhorman | 0:c3dcd4c4983a | 295 | } |
nkhorman | 0:c3dcd4c4983a | 296 | |
nkhorman | 0:c3dcd4c4983a | 297 | int16_t |
nkhorman | 0:c3dcd4c4983a | 298 | dx01 = x1 - x0, |
nkhorman | 0:c3dcd4c4983a | 299 | dy01 = y1 - y0, |
nkhorman | 0:c3dcd4c4983a | 300 | dx02 = x2 - x0, |
nkhorman | 0:c3dcd4c4983a | 301 | dy02 = y2 - y0, |
nkhorman | 0:c3dcd4c4983a | 302 | dx12 = x2 - x1, |
nkhorman | 0:c3dcd4c4983a | 303 | dy12 = y2 - y1, |
nkhorman | 0:c3dcd4c4983a | 304 | sa = 0, |
nkhorman | 0:c3dcd4c4983a | 305 | sb = 0; |
nkhorman | 0:c3dcd4c4983a | 306 | |
nkhorman | 0:c3dcd4c4983a | 307 | // For upper part of triangle, find scanline crossings for segments |
nkhorman | 0:c3dcd4c4983a | 308 | // 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1 |
nkhorman | 0:c3dcd4c4983a | 309 | // is included here (and second loop will be skipped, avoiding a /0 |
nkhorman | 0:c3dcd4c4983a | 310 | // error there), otherwise scanline y1 is skipped here and handled |
nkhorman | 0:c3dcd4c4983a | 311 | // in the second loop...which also avoids a /0 error here if y0=y1 |
nkhorman | 0:c3dcd4c4983a | 312 | // (flat-topped triangle). |
nkhorman | 0:c3dcd4c4983a | 313 | if(y1 == y2) |
nkhorman | 0:c3dcd4c4983a | 314 | last = y1; // Include y1 scanline |
nkhorman | 0:c3dcd4c4983a | 315 | else |
nkhorman | 0:c3dcd4c4983a | 316 | last = y1-1; // Skip it |
nkhorman | 0:c3dcd4c4983a | 317 | |
nkhorman | 0:c3dcd4c4983a | 318 | for(y=y0; y<=last; y++) |
nkhorman | 0:c3dcd4c4983a | 319 | { |
nkhorman | 0:c3dcd4c4983a | 320 | a = x0 + sa / dy01; |
nkhorman | 0:c3dcd4c4983a | 321 | b = x0 + sb / dy02; |
nkhorman | 0:c3dcd4c4983a | 322 | sa += dx01; |
nkhorman | 0:c3dcd4c4983a | 323 | sb += dx02; |
nkhorman | 0:c3dcd4c4983a | 324 | /* longhand: |
nkhorman | 0:c3dcd4c4983a | 325 | a = x0 + (x1 - x0) * (y - y0) / (y1 - y0); |
nkhorman | 0:c3dcd4c4983a | 326 | b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); |
nkhorman | 0:c3dcd4c4983a | 327 | */ |
nkhorman | 0:c3dcd4c4983a | 328 | if(a > b) |
nkhorman | 0:c3dcd4c4983a | 329 | swap(a,b); |
nkhorman | 0:c3dcd4c4983a | 330 | drawFastHLine(a, y, b-a+1, color); |
nkhorman | 0:c3dcd4c4983a | 331 | } |
nkhorman | 0:c3dcd4c4983a | 332 | |
nkhorman | 0:c3dcd4c4983a | 333 | // For lower part of triangle, find scanline crossings for segments |
nkhorman | 0:c3dcd4c4983a | 334 | // 0-2 and 1-2. This loop is skipped if y1=y2. |
nkhorman | 0:c3dcd4c4983a | 335 | sa = dx12 * (y - y1); |
nkhorman | 0:c3dcd4c4983a | 336 | sb = dx02 * (y - y0); |
nkhorman | 0:c3dcd4c4983a | 337 | for(; y<=y2; y++) |
nkhorman | 0:c3dcd4c4983a | 338 | { |
nkhorman | 0:c3dcd4c4983a | 339 | a = x1 + sa / dy12; |
nkhorman | 0:c3dcd4c4983a | 340 | b = x0 + sb / dy02; |
nkhorman | 0:c3dcd4c4983a | 341 | sa += dx12; |
nkhorman | 0:c3dcd4c4983a | 342 | sb += dx02; |
nkhorman | 0:c3dcd4c4983a | 343 | /* longhand: |
nkhorman | 0:c3dcd4c4983a | 344 | a = x1 + (x2 - x1) * (y - y1) / (y2 - y1); |
nkhorman | 0:c3dcd4c4983a | 345 | b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); |
nkhorman | 0:c3dcd4c4983a | 346 | */ |
nkhorman | 0:c3dcd4c4983a | 347 | if(a > b) |
nkhorman | 0:c3dcd4c4983a | 348 | swap(a,b); |
nkhorman | 0:c3dcd4c4983a | 349 | drawFastHLine(a, y, b-a+1, color); |
nkhorman | 0:c3dcd4c4983a | 350 | } |
nkhorman | 0:c3dcd4c4983a | 351 | } |
nkhorman | 0:c3dcd4c4983a | 352 | #endif |
nkhorman | 0:c3dcd4c4983a | 353 | |
nkhorman | 0:c3dcd4c4983a | 354 | 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 | 355 | { |
nkhorman | 0:c3dcd4c4983a | 356 | for (int16_t j=0; j<h; j++) |
nkhorman | 0:c3dcd4c4983a | 357 | { |
nkhorman | 0:c3dcd4c4983a | 358 | for (int16_t i=0; i<w; i++ ) |
nkhorman | 0:c3dcd4c4983a | 359 | { |
nkhorman | 0:c3dcd4c4983a | 360 | if (bitmap[i + (j/8)*w] & _BV(j%8)) |
nkhorman | 0:c3dcd4c4983a | 361 | drawPixel(x+i, y+j, color); |
nkhorman | 0:c3dcd4c4983a | 362 | } |
nkhorman | 0:c3dcd4c4983a | 363 | } |
nkhorman | 0:c3dcd4c4983a | 364 | } |
nkhorman | 0:c3dcd4c4983a | 365 | |
nkhorman | 0:c3dcd4c4983a | 366 | size_t Adafruit_GFX::writeChar(uint8_t c) |
nkhorman | 0:c3dcd4c4983a | 367 | { |
nkhorman | 0:c3dcd4c4983a | 368 | if (c == '\n') |
nkhorman | 0:c3dcd4c4983a | 369 | { |
nkhorman | 0:c3dcd4c4983a | 370 | cursor_y += textsize*8; |
nkhorman | 0:c3dcd4c4983a | 371 | cursor_x = 0; |
nkhorman | 0:c3dcd4c4983a | 372 | } |
nkhorman | 0:c3dcd4c4983a | 373 | else if (c == '\r') |
nkhorman | 0:c3dcd4c4983a | 374 | cursor_x = 0; |
nkhorman | 0:c3dcd4c4983a | 375 | else |
nkhorman | 0:c3dcd4c4983a | 376 | { |
nkhorman | 0:c3dcd4c4983a | 377 | drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize); |
nkhorman | 0:c3dcd4c4983a | 378 | cursor_x += textsize*6; |
nkhorman | 0:c3dcd4c4983a | 379 | if (wrap && (cursor_x > (_width - textsize*6))) |
nkhorman | 0:c3dcd4c4983a | 380 | { |
nkhorman | 0:c3dcd4c4983a | 381 | cursor_y += textsize*8; |
nkhorman | 0:c3dcd4c4983a | 382 | cursor_x = 0; |
nkhorman | 0:c3dcd4c4983a | 383 | } |
nkhorman | 0:c3dcd4c4983a | 384 | } |
nkhorman | 0:c3dcd4c4983a | 385 | return 1; |
nkhorman | 0:c3dcd4c4983a | 386 | } |
nkhorman | 0:c3dcd4c4983a | 387 | |
nkhorman | 0:c3dcd4c4983a | 388 | // draw a character |
nkhorman | 0:c3dcd4c4983a | 389 | 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 | 390 | { |
nkhorman | 0:c3dcd4c4983a | 391 | if( |
nkhorman | 0:c3dcd4c4983a | 392 | (x >= _width) || // Clip right |
nkhorman | 0:c3dcd4c4983a | 393 | (y >= _height) || // Clip bottom |
nkhorman | 0:c3dcd4c4983a | 394 | ((x + 5 * size - 1) < 0) || // Clip left |
nkhorman | 0:c3dcd4c4983a | 395 | ((y + 8 * size - 1) < 0) // Clip top |
nkhorman | 0:c3dcd4c4983a | 396 | ) |
nkhorman | 0:c3dcd4c4983a | 397 | return; |
nkhorman | 0:c3dcd4c4983a | 398 | |
nkhorman | 0:c3dcd4c4983a | 399 | for (int8_t i=0; i<6; i++ ) |
nkhorman | 0:c3dcd4c4983a | 400 | { |
nkhorman | 0:c3dcd4c4983a | 401 | uint8_t line = 0; |
nkhorman | 0:c3dcd4c4983a | 402 | |
nkhorman | 0:c3dcd4c4983a | 403 | if (i == 5) |
nkhorman | 0:c3dcd4c4983a | 404 | line = 0x0; |
nkhorman | 0:c3dcd4c4983a | 405 | else |
nkhorman | 0:c3dcd4c4983a | 406 | line = font[(c*5)+i]; |
nkhorman | 0:c3dcd4c4983a | 407 | |
nkhorman | 0:c3dcd4c4983a | 408 | for (int8_t j = 0; j<8; j++) |
nkhorman | 0:c3dcd4c4983a | 409 | { |
nkhorman | 0:c3dcd4c4983a | 410 | if (line & 0x1) |
nkhorman | 0:c3dcd4c4983a | 411 | { |
nkhorman | 9:ddb97c9850a2 | 412 | #if defined(GFX_WANT_ABSTRACTS) || defined(GFX_SIZEABLE_TEXT) |
nkhorman | 0:c3dcd4c4983a | 413 | if (size == 1) // default size |
nkhorman | 0:c3dcd4c4983a | 414 | drawPixel(x+i, y+j, color); |
nkhorman | 0:c3dcd4c4983a | 415 | else // big size |
nkhorman | 0:c3dcd4c4983a | 416 | fillRect(x+(i*size), y+(j*size), size, size, color); |
nkhorman | 9:ddb97c9850a2 | 417 | #else |
nkhorman | 9:ddb97c9850a2 | 418 | drawPixel(x+i, y+j, color); |
nkhorman | 0:c3dcd4c4983a | 419 | #endif |
nkhorman | 0:c3dcd4c4983a | 420 | } |
nkhorman | 0:c3dcd4c4983a | 421 | else if (bg != color) |
nkhorman | 0:c3dcd4c4983a | 422 | { |
nkhorman | 9:ddb97c9850a2 | 423 | #if defined(GFX_WANT_ABSTRACTS) || defined(GFX_SIZEABLE_TEXT) |
nkhorman | 0:c3dcd4c4983a | 424 | if (size == 1) // default size |
nkhorman | 0:c3dcd4c4983a | 425 | drawPixel(x+i, y+j, bg); |
nkhorman | 0:c3dcd4c4983a | 426 | else // big size |
nkhorman | 0:c3dcd4c4983a | 427 | fillRect(x+i*size, y+j*size, size, size, bg); |
nkhorman | 9:ddb97c9850a2 | 428 | #else |
nkhorman | 9:ddb97c9850a2 | 429 | drawPixel(x+i, y+j, bg); |
nkhorman | 0:c3dcd4c4983a | 430 | #endif |
nkhorman | 0:c3dcd4c4983a | 431 | } |
nkhorman | 0:c3dcd4c4983a | 432 | line >>= 1; |
nkhorman | 0:c3dcd4c4983a | 433 | } |
nkhorman | 0:c3dcd4c4983a | 434 | } |
nkhorman | 0:c3dcd4c4983a | 435 | } |
nkhorman | 9:ddb97c9850a2 | 436 | |
nkhorman | 0:c3dcd4c4983a | 437 | void Adafruit_GFX::setRotation(uint8_t x) |
nkhorman | 0:c3dcd4c4983a | 438 | { |
nkhorman | 0:c3dcd4c4983a | 439 | x %= 4; // cant be higher than 3 |
nkhorman | 0:c3dcd4c4983a | 440 | rotation = x; |
nkhorman | 0:c3dcd4c4983a | 441 | switch (x) |
nkhorman | 0:c3dcd4c4983a | 442 | { |
nkhorman | 0:c3dcd4c4983a | 443 | case 0: |
nkhorman | 0:c3dcd4c4983a | 444 | case 2: |
nkhorman | 0:c3dcd4c4983a | 445 | _width = _rawWidth; |
nkhorman | 0:c3dcd4c4983a | 446 | _height = _rawHeight; |
nkhorman | 0:c3dcd4c4983a | 447 | break; |
nkhorman | 0:c3dcd4c4983a | 448 | case 1: |
nkhorman | 0:c3dcd4c4983a | 449 | case 3: |
nkhorman | 0:c3dcd4c4983a | 450 | _width = _rawHeight; |
nkhorman | 0:c3dcd4c4983a | 451 | _height = _rawWidth; |
nkhorman | 0:c3dcd4c4983a | 452 | break; |
nkhorman | 0:c3dcd4c4983a | 453 | } |
nkhorman | 0:c3dcd4c4983a | 454 | } |