this hurts

Dependencies:   FFT

Committer:
annieluo2
Date:
Wed Dec 02 18:02:03 2020 +0000
Revision:
0:d6c9b09b4042
boo

Who changed what in which revision?

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