Microduino

Dependencies:   mbed

Fork of Io_moon by Li Weiyi

Committer:
lixianyu
Date:
Thu Jun 23 11:16:14 2016 +0000
Revision:
0:740c1eb2df13
* AM2321?????????2s????i2c?????; * SimpleTimer??bug?????????????????????????; * Blynk??bug??????????????; * ?????????

Who changed what in which revision?

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