A first port of the excellent Adafruit GFX library

Dependents:   Adafruit_GFX

Committer:
SomeRandomBloke
Date:
Tue Mar 26 23:08:53 2013 +0000
Revision:
0:08cfbae05724
Child:
1:e67555532f16
New commit

Who changed what in which revision?

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