Adafruit OLED Libary

Dependents:   SSD1306_LCD-HelloWorld WizFi250-Geolocation_NTP Tweeting_Machine_HelloWorld_WIZwiki-W7500 display_irita ... more

Fork of Adafruit_GFX by Neal Horman

Committer:
kaizen
Date:
Tue Jun 23 09:49:01 2015 +0000
Revision:
18:3112550cc6a3
Parent:
9:ddb97c9850a2
Fixed display function bug

Who changed what in which revision?

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