The wait in mci_WaitForEvent will delay all card transactions.
Fork of EALib by
Adafruit_GFX.cpp@0:0fdadbc3d852, 2013-09-26 (annotated)
- Committer:
- embeddedartists
- Date:
- Thu Sep 26 06:37:02 2013 +0000
- Revision:
- 0:0fdadbc3d852
First version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
embeddedartists | 0:0fdadbc3d852 | 1 | /* |
embeddedartists | 0:0fdadbc3d852 | 2 | This is the core graphics library for all our displays, providing a common |
embeddedartists | 0:0fdadbc3d852 | 3 | set of graphics primitives (points, lines, circles, etc.). It needs to be |
embeddedartists | 0:0fdadbc3d852 | 4 | paired with a hardware-specific library for each display device we carry |
embeddedartists | 0:0fdadbc3d852 | 5 | (to handle the lower-level functions). |
embeddedartists | 0:0fdadbc3d852 | 6 | |
embeddedartists | 0:0fdadbc3d852 | 7 | Adafruit invests time and resources providing this open source code, please |
embeddedartists | 0:0fdadbc3d852 | 8 | support Adafruit & open-source hardware by purchasing products from Adafruit! |
embeddedartists | 0:0fdadbc3d852 | 9 | |
embeddedartists | 0:0fdadbc3d852 | 10 | Copyright (c) 2013 Adafruit Industries. All rights reserved. |
embeddedartists | 0:0fdadbc3d852 | 11 | |
embeddedartists | 0:0fdadbc3d852 | 12 | Redistribution and use in source and binary forms, with or without |
embeddedartists | 0:0fdadbc3d852 | 13 | modification, are permitted provided that the following conditions are met: |
embeddedartists | 0:0fdadbc3d852 | 14 | |
embeddedartists | 0:0fdadbc3d852 | 15 | - Redistributions of source code must retain the above copyright notice, |
embeddedartists | 0:0fdadbc3d852 | 16 | this list of conditions and the following disclaimer. |
embeddedartists | 0:0fdadbc3d852 | 17 | - Redistributions in binary form must reproduce the above copyright notice, |
embeddedartists | 0:0fdadbc3d852 | 18 | this list of conditions and the following disclaimer in the documentation |
embeddedartists | 0:0fdadbc3d852 | 19 | and/or other materials provided with the distribution. |
embeddedartists | 0:0fdadbc3d852 | 20 | |
embeddedartists | 0:0fdadbc3d852 | 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
embeddedartists | 0:0fdadbc3d852 | 22 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
embeddedartists | 0:0fdadbc3d852 | 23 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
embeddedartists | 0:0fdadbc3d852 | 24 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
embeddedartists | 0:0fdadbc3d852 | 25 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
embeddedartists | 0:0fdadbc3d852 | 26 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
embeddedartists | 0:0fdadbc3d852 | 27 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
embeddedartists | 0:0fdadbc3d852 | 28 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
embeddedartists | 0:0fdadbc3d852 | 29 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
embeddedartists | 0:0fdadbc3d852 | 30 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
embeddedartists | 0:0fdadbc3d852 | 31 | POSSIBILITY OF SUCH DAMAGE. |
embeddedartists | 0:0fdadbc3d852 | 32 | */ |
embeddedartists | 0:0fdadbc3d852 | 33 | |
embeddedartists | 0:0fdadbc3d852 | 34 | #include "mbed.h" |
embeddedartists | 0:0fdadbc3d852 | 35 | #include "Adafruit_GFX.h" |
embeddedartists | 0:0fdadbc3d852 | 36 | #include "glcdfont.c" |
embeddedartists | 0:0fdadbc3d852 | 37 | #ifdef __AVR__ |
embeddedartists | 0:0fdadbc3d852 | 38 | #include <avr/pgmspace.h> |
embeddedartists | 0:0fdadbc3d852 | 39 | #else |
embeddedartists | 0:0fdadbc3d852 | 40 | #define pgm_read_byte(addr) (*(const unsigned char *)(addr)) |
embeddedartists | 0:0fdadbc3d852 | 41 | #endif |
embeddedartists | 0:0fdadbc3d852 | 42 | |
embeddedartists | 0:0fdadbc3d852 | 43 | Adafruit_GFX::Adafruit_GFX(int16_t w, int16_t h): |
embeddedartists | 0:0fdadbc3d852 | 44 | WIDTH(w), HEIGHT(h) |
embeddedartists | 0:0fdadbc3d852 | 45 | { |
embeddedartists | 0:0fdadbc3d852 | 46 | _width = WIDTH; |
embeddedartists | 0:0fdadbc3d852 | 47 | _height = HEIGHT; |
embeddedartists | 0:0fdadbc3d852 | 48 | rotation = 0; |
embeddedartists | 0:0fdadbc3d852 | 49 | cursor_y = cursor_x = 0; |
embeddedartists | 0:0fdadbc3d852 | 50 | textsize = 1; |
embeddedartists | 0:0fdadbc3d852 | 51 | textcolor = textbgcolor = 0xFFFF; |
embeddedartists | 0:0fdadbc3d852 | 52 | wrap = true; |
embeddedartists | 0:0fdadbc3d852 | 53 | } |
embeddedartists | 0:0fdadbc3d852 | 54 | |
embeddedartists | 0:0fdadbc3d852 | 55 | // Draw a circle outline |
embeddedartists | 0:0fdadbc3d852 | 56 | void Adafruit_GFX::drawCircle(int16_t x0, int16_t y0, int16_t r, |
embeddedartists | 0:0fdadbc3d852 | 57 | uint16_t color) { |
embeddedartists | 0:0fdadbc3d852 | 58 | int16_t f = 1 - r; |
embeddedartists | 0:0fdadbc3d852 | 59 | int16_t ddF_x = 1; |
embeddedartists | 0:0fdadbc3d852 | 60 | int16_t ddF_y = -2 * r; |
embeddedartists | 0:0fdadbc3d852 | 61 | int16_t x = 0; |
embeddedartists | 0:0fdadbc3d852 | 62 | int16_t y = r; |
embeddedartists | 0:0fdadbc3d852 | 63 | |
embeddedartists | 0:0fdadbc3d852 | 64 | drawPixel(x0 , y0+r, color); |
embeddedartists | 0:0fdadbc3d852 | 65 | drawPixel(x0 , y0-r, color); |
embeddedartists | 0:0fdadbc3d852 | 66 | drawPixel(x0+r, y0 , color); |
embeddedartists | 0:0fdadbc3d852 | 67 | drawPixel(x0-r, y0 , color); |
embeddedartists | 0:0fdadbc3d852 | 68 | |
embeddedartists | 0:0fdadbc3d852 | 69 | while (x<y) { |
embeddedartists | 0:0fdadbc3d852 | 70 | if (f >= 0) { |
embeddedartists | 0:0fdadbc3d852 | 71 | y--; |
embeddedartists | 0:0fdadbc3d852 | 72 | ddF_y += 2; |
embeddedartists | 0:0fdadbc3d852 | 73 | f += ddF_y; |
embeddedartists | 0:0fdadbc3d852 | 74 | } |
embeddedartists | 0:0fdadbc3d852 | 75 | x++; |
embeddedartists | 0:0fdadbc3d852 | 76 | ddF_x += 2; |
embeddedartists | 0:0fdadbc3d852 | 77 | f += ddF_x; |
embeddedartists | 0:0fdadbc3d852 | 78 | |
embeddedartists | 0:0fdadbc3d852 | 79 | drawPixel(x0 + x, y0 + y, color); |
embeddedartists | 0:0fdadbc3d852 | 80 | drawPixel(x0 - x, y0 + y, color); |
embeddedartists | 0:0fdadbc3d852 | 81 | drawPixel(x0 + x, y0 - y, color); |
embeddedartists | 0:0fdadbc3d852 | 82 | drawPixel(x0 - x, y0 - y, color); |
embeddedartists | 0:0fdadbc3d852 | 83 | drawPixel(x0 + y, y0 + x, color); |
embeddedartists | 0:0fdadbc3d852 | 84 | drawPixel(x0 - y, y0 + x, color); |
embeddedartists | 0:0fdadbc3d852 | 85 | drawPixel(x0 + y, y0 - x, color); |
embeddedartists | 0:0fdadbc3d852 | 86 | drawPixel(x0 - y, y0 - x, color); |
embeddedartists | 0:0fdadbc3d852 | 87 | } |
embeddedartists | 0:0fdadbc3d852 | 88 | } |
embeddedartists | 0:0fdadbc3d852 | 89 | |
embeddedartists | 0:0fdadbc3d852 | 90 | void Adafruit_GFX::drawCircleHelper( int16_t x0, int16_t y0, |
embeddedartists | 0:0fdadbc3d852 | 91 | int16_t r, uint8_t cornername, uint16_t color) { |
embeddedartists | 0:0fdadbc3d852 | 92 | int16_t f = 1 - r; |
embeddedartists | 0:0fdadbc3d852 | 93 | int16_t ddF_x = 1; |
embeddedartists | 0:0fdadbc3d852 | 94 | int16_t ddF_y = -2 * r; |
embeddedartists | 0:0fdadbc3d852 | 95 | int16_t x = 0; |
embeddedartists | 0:0fdadbc3d852 | 96 | int16_t y = r; |
embeddedartists | 0:0fdadbc3d852 | 97 | |
embeddedartists | 0:0fdadbc3d852 | 98 | while (x<y) { |
embeddedartists | 0:0fdadbc3d852 | 99 | if (f >= 0) { |
embeddedartists | 0:0fdadbc3d852 | 100 | y--; |
embeddedartists | 0:0fdadbc3d852 | 101 | ddF_y += 2; |
embeddedartists | 0:0fdadbc3d852 | 102 | f += ddF_y; |
embeddedartists | 0:0fdadbc3d852 | 103 | } |
embeddedartists | 0:0fdadbc3d852 | 104 | x++; |
embeddedartists | 0:0fdadbc3d852 | 105 | ddF_x += 2; |
embeddedartists | 0:0fdadbc3d852 | 106 | f += ddF_x; |
embeddedartists | 0:0fdadbc3d852 | 107 | if (cornername & 0x4) { |
embeddedartists | 0:0fdadbc3d852 | 108 | drawPixel(x0 + x, y0 + y, color); |
embeddedartists | 0:0fdadbc3d852 | 109 | drawPixel(x0 + y, y0 + x, color); |
embeddedartists | 0:0fdadbc3d852 | 110 | } |
embeddedartists | 0:0fdadbc3d852 | 111 | if (cornername & 0x2) { |
embeddedartists | 0:0fdadbc3d852 | 112 | drawPixel(x0 + x, y0 - y, color); |
embeddedartists | 0:0fdadbc3d852 | 113 | drawPixel(x0 + y, y0 - x, color); |
embeddedartists | 0:0fdadbc3d852 | 114 | } |
embeddedartists | 0:0fdadbc3d852 | 115 | if (cornername & 0x8) { |
embeddedartists | 0:0fdadbc3d852 | 116 | drawPixel(x0 - y, y0 + x, color); |
embeddedartists | 0:0fdadbc3d852 | 117 | drawPixel(x0 - x, y0 + y, color); |
embeddedartists | 0:0fdadbc3d852 | 118 | } |
embeddedartists | 0:0fdadbc3d852 | 119 | if (cornername & 0x1) { |
embeddedartists | 0:0fdadbc3d852 | 120 | drawPixel(x0 - y, y0 - x, color); |
embeddedartists | 0:0fdadbc3d852 | 121 | drawPixel(x0 - x, y0 - y, color); |
embeddedartists | 0:0fdadbc3d852 | 122 | } |
embeddedartists | 0:0fdadbc3d852 | 123 | } |
embeddedartists | 0:0fdadbc3d852 | 124 | } |
embeddedartists | 0:0fdadbc3d852 | 125 | |
embeddedartists | 0:0fdadbc3d852 | 126 | void Adafruit_GFX::fillCircle(int16_t x0, int16_t y0, int16_t r, |
embeddedartists | 0:0fdadbc3d852 | 127 | uint16_t color) { |
embeddedartists | 0:0fdadbc3d852 | 128 | drawFastVLine(x0, y0-r, 2*r+1, color); |
embeddedartists | 0:0fdadbc3d852 | 129 | fillCircleHelper(x0, y0, r, 3, 0, color); |
embeddedartists | 0:0fdadbc3d852 | 130 | } |
embeddedartists | 0:0fdadbc3d852 | 131 | |
embeddedartists | 0:0fdadbc3d852 | 132 | // Used to do circles and roundrects |
embeddedartists | 0:0fdadbc3d852 | 133 | void Adafruit_GFX::fillCircleHelper(int16_t x0, int16_t y0, int16_t r, |
embeddedartists | 0:0fdadbc3d852 | 134 | uint8_t cornername, int16_t delta, uint16_t color) { |
embeddedartists | 0:0fdadbc3d852 | 135 | |
embeddedartists | 0:0fdadbc3d852 | 136 | int16_t f = 1 - r; |
embeddedartists | 0:0fdadbc3d852 | 137 | int16_t ddF_x = 1; |
embeddedartists | 0:0fdadbc3d852 | 138 | int16_t ddF_y = -2 * r; |
embeddedartists | 0:0fdadbc3d852 | 139 | int16_t x = 0; |
embeddedartists | 0:0fdadbc3d852 | 140 | int16_t y = r; |
embeddedartists | 0:0fdadbc3d852 | 141 | |
embeddedartists | 0:0fdadbc3d852 | 142 | while (x<y) { |
embeddedartists | 0:0fdadbc3d852 | 143 | if (f >= 0) { |
embeddedartists | 0:0fdadbc3d852 | 144 | y--; |
embeddedartists | 0:0fdadbc3d852 | 145 | ddF_y += 2; |
embeddedartists | 0:0fdadbc3d852 | 146 | f += ddF_y; |
embeddedartists | 0:0fdadbc3d852 | 147 | } |
embeddedartists | 0:0fdadbc3d852 | 148 | x++; |
embeddedartists | 0:0fdadbc3d852 | 149 | ddF_x += 2; |
embeddedartists | 0:0fdadbc3d852 | 150 | f += ddF_x; |
embeddedartists | 0:0fdadbc3d852 | 151 | |
embeddedartists | 0:0fdadbc3d852 | 152 | if (cornername & 0x1) { |
embeddedartists | 0:0fdadbc3d852 | 153 | drawFastVLine(x0+x, y0-y, 2*y+1+delta, color); |
embeddedartists | 0:0fdadbc3d852 | 154 | drawFastVLine(x0+y, y0-x, 2*x+1+delta, color); |
embeddedartists | 0:0fdadbc3d852 | 155 | } |
embeddedartists | 0:0fdadbc3d852 | 156 | if (cornername & 0x2) { |
embeddedartists | 0:0fdadbc3d852 | 157 | drawFastVLine(x0-x, y0-y, 2*y+1+delta, color); |
embeddedartists | 0:0fdadbc3d852 | 158 | drawFastVLine(x0-y, y0-x, 2*x+1+delta, color); |
embeddedartists | 0:0fdadbc3d852 | 159 | } |
embeddedartists | 0:0fdadbc3d852 | 160 | } |
embeddedartists | 0:0fdadbc3d852 | 161 | } |
embeddedartists | 0:0fdadbc3d852 | 162 | |
embeddedartists | 0:0fdadbc3d852 | 163 | // Bresenham's algorithm - thx wikpedia |
embeddedartists | 0:0fdadbc3d852 | 164 | void Adafruit_GFX::drawLine(int16_t x0, int16_t y0, |
embeddedartists | 0:0fdadbc3d852 | 165 | int16_t x1, int16_t y1, |
embeddedartists | 0:0fdadbc3d852 | 166 | uint16_t color) { |
embeddedartists | 0:0fdadbc3d852 | 167 | int16_t steep = abs(y1 - y0) > abs(x1 - x0); |
embeddedartists | 0:0fdadbc3d852 | 168 | if (steep) { |
embeddedartists | 0:0fdadbc3d852 | 169 | swap(x0, y0); |
embeddedartists | 0:0fdadbc3d852 | 170 | swap(x1, y1); |
embeddedartists | 0:0fdadbc3d852 | 171 | } |
embeddedartists | 0:0fdadbc3d852 | 172 | |
embeddedartists | 0:0fdadbc3d852 | 173 | if (x0 > x1) { |
embeddedartists | 0:0fdadbc3d852 | 174 | swap(x0, x1); |
embeddedartists | 0:0fdadbc3d852 | 175 | swap(y0, y1); |
embeddedartists | 0:0fdadbc3d852 | 176 | } |
embeddedartists | 0:0fdadbc3d852 | 177 | |
embeddedartists | 0:0fdadbc3d852 | 178 | int16_t dx, dy; |
embeddedartists | 0:0fdadbc3d852 | 179 | dx = x1 - x0; |
embeddedartists | 0:0fdadbc3d852 | 180 | dy = abs(y1 - y0); |
embeddedartists | 0:0fdadbc3d852 | 181 | |
embeddedartists | 0:0fdadbc3d852 | 182 | int16_t err = dx / 2; |
embeddedartists | 0:0fdadbc3d852 | 183 | int16_t ystep; |
embeddedartists | 0:0fdadbc3d852 | 184 | |
embeddedartists | 0:0fdadbc3d852 | 185 | if (y0 < y1) { |
embeddedartists | 0:0fdadbc3d852 | 186 | ystep = 1; |
embeddedartists | 0:0fdadbc3d852 | 187 | } else { |
embeddedartists | 0:0fdadbc3d852 | 188 | ystep = -1; |
embeddedartists | 0:0fdadbc3d852 | 189 | } |
embeddedartists | 0:0fdadbc3d852 | 190 | |
embeddedartists | 0:0fdadbc3d852 | 191 | for (; x0<=x1; x0++) { |
embeddedartists | 0:0fdadbc3d852 | 192 | if (steep) { |
embeddedartists | 0:0fdadbc3d852 | 193 | drawPixel(y0, x0, color); |
embeddedartists | 0:0fdadbc3d852 | 194 | } else { |
embeddedartists | 0:0fdadbc3d852 | 195 | drawPixel(x0, y0, color); |
embeddedartists | 0:0fdadbc3d852 | 196 | } |
embeddedartists | 0:0fdadbc3d852 | 197 | err -= dy; |
embeddedartists | 0:0fdadbc3d852 | 198 | if (err < 0) { |
embeddedartists | 0:0fdadbc3d852 | 199 | y0 += ystep; |
embeddedartists | 0:0fdadbc3d852 | 200 | err += dx; |
embeddedartists | 0:0fdadbc3d852 | 201 | } |
embeddedartists | 0:0fdadbc3d852 | 202 | } |
embeddedartists | 0:0fdadbc3d852 | 203 | } |
embeddedartists | 0:0fdadbc3d852 | 204 | |
embeddedartists | 0:0fdadbc3d852 | 205 | // Draw a rectangle |
embeddedartists | 0:0fdadbc3d852 | 206 | void Adafruit_GFX::drawRect(int16_t x, int16_t y, |
embeddedartists | 0:0fdadbc3d852 | 207 | int16_t w, int16_t h, |
embeddedartists | 0:0fdadbc3d852 | 208 | uint16_t color) { |
embeddedartists | 0:0fdadbc3d852 | 209 | drawFastHLine(x, y, w, color); |
embeddedartists | 0:0fdadbc3d852 | 210 | drawFastHLine(x, y+h-1, w, color); |
embeddedartists | 0:0fdadbc3d852 | 211 | drawFastVLine(x, y, h, color); |
embeddedartists | 0:0fdadbc3d852 | 212 | drawFastVLine(x+w-1, y, h, color); |
embeddedartists | 0:0fdadbc3d852 | 213 | } |
embeddedartists | 0:0fdadbc3d852 | 214 | |
embeddedartists | 0:0fdadbc3d852 | 215 | void Adafruit_GFX::drawFastVLine(int16_t x, int16_t y, |
embeddedartists | 0:0fdadbc3d852 | 216 | int16_t h, uint16_t color) { |
embeddedartists | 0:0fdadbc3d852 | 217 | // Update in subclasses if desired! |
embeddedartists | 0:0fdadbc3d852 | 218 | drawLine(x, y, x, y+h-1, color); |
embeddedartists | 0:0fdadbc3d852 | 219 | } |
embeddedartists | 0:0fdadbc3d852 | 220 | |
embeddedartists | 0:0fdadbc3d852 | 221 | void Adafruit_GFX::drawFastHLine(int16_t x, int16_t y, |
embeddedartists | 0:0fdadbc3d852 | 222 | int16_t w, uint16_t color) { |
embeddedartists | 0:0fdadbc3d852 | 223 | // Update in subclasses if desired! |
embeddedartists | 0:0fdadbc3d852 | 224 | drawLine(x, y, x+w-1, y, color); |
embeddedartists | 0:0fdadbc3d852 | 225 | } |
embeddedartists | 0:0fdadbc3d852 | 226 | |
embeddedartists | 0:0fdadbc3d852 | 227 | void Adafruit_GFX::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, |
embeddedartists | 0:0fdadbc3d852 | 228 | uint16_t color) { |
embeddedartists | 0:0fdadbc3d852 | 229 | // Update in subclasses if desired! |
embeddedartists | 0:0fdadbc3d852 | 230 | for (int16_t i=x; i<x+w; i++) { |
embeddedartists | 0:0fdadbc3d852 | 231 | drawFastVLine(i, y, h, color); |
embeddedartists | 0:0fdadbc3d852 | 232 | } |
embeddedartists | 0:0fdadbc3d852 | 233 | } |
embeddedartists | 0:0fdadbc3d852 | 234 | |
embeddedartists | 0:0fdadbc3d852 | 235 | void Adafruit_GFX::fillScreen(uint16_t color) { |
embeddedartists | 0:0fdadbc3d852 | 236 | fillRect(0, 0, _width, _height, color); |
embeddedartists | 0:0fdadbc3d852 | 237 | } |
embeddedartists | 0:0fdadbc3d852 | 238 | |
embeddedartists | 0:0fdadbc3d852 | 239 | // Draw a rounded rectangle |
embeddedartists | 0:0fdadbc3d852 | 240 | void Adafruit_GFX::drawRoundRect(int16_t x, int16_t y, int16_t w, |
embeddedartists | 0:0fdadbc3d852 | 241 | int16_t h, int16_t r, uint16_t color) { |
embeddedartists | 0:0fdadbc3d852 | 242 | // smarter version |
embeddedartists | 0:0fdadbc3d852 | 243 | drawFastHLine(x+r , y , w-2*r, color); // Top |
embeddedartists | 0:0fdadbc3d852 | 244 | drawFastHLine(x+r , y+h-1, w-2*r, color); // Bottom |
embeddedartists | 0:0fdadbc3d852 | 245 | drawFastVLine(x , y+r , h-2*r, color); // Left |
embeddedartists | 0:0fdadbc3d852 | 246 | drawFastVLine(x+w-1, y+r , h-2*r, color); // Right |
embeddedartists | 0:0fdadbc3d852 | 247 | // draw four corners |
embeddedartists | 0:0fdadbc3d852 | 248 | drawCircleHelper(x+r , y+r , r, 1, color); |
embeddedartists | 0:0fdadbc3d852 | 249 | drawCircleHelper(x+w-r-1, y+r , r, 2, color); |
embeddedartists | 0:0fdadbc3d852 | 250 | drawCircleHelper(x+w-r-1, y+h-r-1, r, 4, color); |
embeddedartists | 0:0fdadbc3d852 | 251 | drawCircleHelper(x+r , y+h-r-1, r, 8, color); |
embeddedartists | 0:0fdadbc3d852 | 252 | } |
embeddedartists | 0:0fdadbc3d852 | 253 | |
embeddedartists | 0:0fdadbc3d852 | 254 | // Fill a rounded rectangle |
embeddedartists | 0:0fdadbc3d852 | 255 | void Adafruit_GFX::fillRoundRect(int16_t x, int16_t y, int16_t w, |
embeddedartists | 0:0fdadbc3d852 | 256 | int16_t h, int16_t r, uint16_t color) { |
embeddedartists | 0:0fdadbc3d852 | 257 | // smarter version |
embeddedartists | 0:0fdadbc3d852 | 258 | fillRect(x+r, y, w-2*r, h, color); |
embeddedartists | 0:0fdadbc3d852 | 259 | |
embeddedartists | 0:0fdadbc3d852 | 260 | // draw four corners |
embeddedartists | 0:0fdadbc3d852 | 261 | fillCircleHelper(x+w-r-1, y+r, r, 1, h-2*r-1, color); |
embeddedartists | 0:0fdadbc3d852 | 262 | fillCircleHelper(x+r , y+r, r, 2, h-2*r-1, color); |
embeddedartists | 0:0fdadbc3d852 | 263 | } |
embeddedartists | 0:0fdadbc3d852 | 264 | |
embeddedartists | 0:0fdadbc3d852 | 265 | // Draw a triangle |
embeddedartists | 0:0fdadbc3d852 | 266 | void Adafruit_GFX::drawTriangle(int16_t x0, int16_t y0, |
embeddedartists | 0:0fdadbc3d852 | 267 | int16_t x1, int16_t y1, |
embeddedartists | 0:0fdadbc3d852 | 268 | int16_t x2, int16_t y2, uint16_t color) { |
embeddedartists | 0:0fdadbc3d852 | 269 | drawLine(x0, y0, x1, y1, color); |
embeddedartists | 0:0fdadbc3d852 | 270 | drawLine(x1, y1, x2, y2, color); |
embeddedartists | 0:0fdadbc3d852 | 271 | drawLine(x2, y2, x0, y0, color); |
embeddedartists | 0:0fdadbc3d852 | 272 | } |
embeddedartists | 0:0fdadbc3d852 | 273 | |
embeddedartists | 0:0fdadbc3d852 | 274 | // Fill a triangle |
embeddedartists | 0:0fdadbc3d852 | 275 | void Adafruit_GFX::fillTriangle ( int16_t x0, int16_t y0, |
embeddedartists | 0:0fdadbc3d852 | 276 | int16_t x1, int16_t y1, |
embeddedartists | 0:0fdadbc3d852 | 277 | int16_t x2, int16_t y2, uint16_t color) { |
embeddedartists | 0:0fdadbc3d852 | 278 | |
embeddedartists | 0:0fdadbc3d852 | 279 | int16_t a, b, y, last; |
embeddedartists | 0:0fdadbc3d852 | 280 | |
embeddedartists | 0:0fdadbc3d852 | 281 | // Sort coordinates by Y order (y2 >= y1 >= y0) |
embeddedartists | 0:0fdadbc3d852 | 282 | if (y0 > y1) { |
embeddedartists | 0:0fdadbc3d852 | 283 | swap(y0, y1); swap(x0, x1); |
embeddedartists | 0:0fdadbc3d852 | 284 | } |
embeddedartists | 0:0fdadbc3d852 | 285 | if (y1 > y2) { |
embeddedartists | 0:0fdadbc3d852 | 286 | swap(y2, y1); swap(x2, x1); |
embeddedartists | 0:0fdadbc3d852 | 287 | } |
embeddedartists | 0:0fdadbc3d852 | 288 | if (y0 > y1) { |
embeddedartists | 0:0fdadbc3d852 | 289 | swap(y0, y1); swap(x0, x1); |
embeddedartists | 0:0fdadbc3d852 | 290 | } |
embeddedartists | 0:0fdadbc3d852 | 291 | |
embeddedartists | 0:0fdadbc3d852 | 292 | if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing |
embeddedartists | 0:0fdadbc3d852 | 293 | a = b = x0; |
embeddedartists | 0:0fdadbc3d852 | 294 | if(x1 < a) a = x1; |
embeddedartists | 0:0fdadbc3d852 | 295 | else if(x1 > b) b = x1; |
embeddedartists | 0:0fdadbc3d852 | 296 | if(x2 < a) a = x2; |
embeddedartists | 0:0fdadbc3d852 | 297 | else if(x2 > b) b = x2; |
embeddedartists | 0:0fdadbc3d852 | 298 | drawFastHLine(a, y0, b-a+1, color); |
embeddedartists | 0:0fdadbc3d852 | 299 | return; |
embeddedartists | 0:0fdadbc3d852 | 300 | } |
embeddedartists | 0:0fdadbc3d852 | 301 | |
embeddedartists | 0:0fdadbc3d852 | 302 | int16_t |
embeddedartists | 0:0fdadbc3d852 | 303 | dx01 = x1 - x0, |
embeddedartists | 0:0fdadbc3d852 | 304 | dy01 = y1 - y0, |
embeddedartists | 0:0fdadbc3d852 | 305 | dx02 = x2 - x0, |
embeddedartists | 0:0fdadbc3d852 | 306 | dy02 = y2 - y0, |
embeddedartists | 0:0fdadbc3d852 | 307 | dx12 = x2 - x1, |
embeddedartists | 0:0fdadbc3d852 | 308 | dy12 = y2 - y1, |
embeddedartists | 0:0fdadbc3d852 | 309 | sa = 0, |
embeddedartists | 0:0fdadbc3d852 | 310 | sb = 0; |
embeddedartists | 0:0fdadbc3d852 | 311 | |
embeddedartists | 0:0fdadbc3d852 | 312 | // For upper part of triangle, find scanline crossings for segments |
embeddedartists | 0:0fdadbc3d852 | 313 | // 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1 |
embeddedartists | 0:0fdadbc3d852 | 314 | // is included here (and second loop will be skipped, avoiding a /0 |
embeddedartists | 0:0fdadbc3d852 | 315 | // error there), otherwise scanline y1 is skipped here and handled |
embeddedartists | 0:0fdadbc3d852 | 316 | // in the second loop...which also avoids a /0 error here if y0=y1 |
embeddedartists | 0:0fdadbc3d852 | 317 | // (flat-topped triangle). |
embeddedartists | 0:0fdadbc3d852 | 318 | if(y1 == y2) last = y1; // Include y1 scanline |
embeddedartists | 0:0fdadbc3d852 | 319 | else last = y1-1; // Skip it |
embeddedartists | 0:0fdadbc3d852 | 320 | |
embeddedartists | 0:0fdadbc3d852 | 321 | for(y=y0; y<=last; y++) { |
embeddedartists | 0:0fdadbc3d852 | 322 | a = x0 + sa / dy01; |
embeddedartists | 0:0fdadbc3d852 | 323 | b = x0 + sb / dy02; |
embeddedartists | 0:0fdadbc3d852 | 324 | sa += dx01; |
embeddedartists | 0:0fdadbc3d852 | 325 | sb += dx02; |
embeddedartists | 0:0fdadbc3d852 | 326 | /* longhand: |
embeddedartists | 0:0fdadbc3d852 | 327 | a = x0 + (x1 - x0) * (y - y0) / (y1 - y0); |
embeddedartists | 0:0fdadbc3d852 | 328 | b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); |
embeddedartists | 0:0fdadbc3d852 | 329 | */ |
embeddedartists | 0:0fdadbc3d852 | 330 | if(a > b) swap(a,b); |
embeddedartists | 0:0fdadbc3d852 | 331 | drawFastHLine(a, y, b-a+1, color); |
embeddedartists | 0:0fdadbc3d852 | 332 | } |
embeddedartists | 0:0fdadbc3d852 | 333 | |
embeddedartists | 0:0fdadbc3d852 | 334 | // For lower part of triangle, find scanline crossings for segments |
embeddedartists | 0:0fdadbc3d852 | 335 | // 0-2 and 1-2. This loop is skipped if y1=y2. |
embeddedartists | 0:0fdadbc3d852 | 336 | sa = dx12 * (y - y1); |
embeddedartists | 0:0fdadbc3d852 | 337 | sb = dx02 * (y - y0); |
embeddedartists | 0:0fdadbc3d852 | 338 | for(; y<=y2; y++) { |
embeddedartists | 0:0fdadbc3d852 | 339 | a = x1 + sa / dy12; |
embeddedartists | 0:0fdadbc3d852 | 340 | b = x0 + sb / dy02; |
embeddedartists | 0:0fdadbc3d852 | 341 | sa += dx12; |
embeddedartists | 0:0fdadbc3d852 | 342 | sb += dx02; |
embeddedartists | 0:0fdadbc3d852 | 343 | /* longhand: |
embeddedartists | 0:0fdadbc3d852 | 344 | a = x1 + (x2 - x1) * (y - y1) / (y2 - y1); |
embeddedartists | 0:0fdadbc3d852 | 345 | b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); |
embeddedartists | 0:0fdadbc3d852 | 346 | */ |
embeddedartists | 0:0fdadbc3d852 | 347 | if(a > b) swap(a,b); |
embeddedartists | 0:0fdadbc3d852 | 348 | drawFastHLine(a, y, b-a+1, color); |
embeddedartists | 0:0fdadbc3d852 | 349 | } |
embeddedartists | 0:0fdadbc3d852 | 350 | } |
embeddedartists | 0:0fdadbc3d852 | 351 | |
embeddedartists | 0:0fdadbc3d852 | 352 | void Adafruit_GFX::drawBitmap(int16_t x, int16_t y, |
embeddedartists | 0:0fdadbc3d852 | 353 | const uint8_t *bitmap, int16_t w, int16_t h, |
embeddedartists | 0:0fdadbc3d852 | 354 | uint16_t color) { |
embeddedartists | 0:0fdadbc3d852 | 355 | |
embeddedartists | 0:0fdadbc3d852 | 356 | int16_t i, j, byteWidth = (w + 7) / 8; |
embeddedartists | 0:0fdadbc3d852 | 357 | |
embeddedartists | 0:0fdadbc3d852 | 358 | for(j=0; j<h; j++) { |
embeddedartists | 0:0fdadbc3d852 | 359 | for(i=0; i<w; i++ ) { |
embeddedartists | 0:0fdadbc3d852 | 360 | if(pgm_read_byte(bitmap + j * byteWidth + i / 8) & (128 >> (i & 7))) { |
embeddedartists | 0:0fdadbc3d852 | 361 | drawPixel(x+i, y+j, color); |
embeddedartists | 0:0fdadbc3d852 | 362 | } |
embeddedartists | 0:0fdadbc3d852 | 363 | } |
embeddedartists | 0:0fdadbc3d852 | 364 | } |
embeddedartists | 0:0fdadbc3d852 | 365 | } |
embeddedartists | 0:0fdadbc3d852 | 366 | |
embeddedartists | 0:0fdadbc3d852 | 367 | |
embeddedartists | 0:0fdadbc3d852 | 368 | size_t Adafruit_GFX::write(uint8_t c) { |
embeddedartists | 0:0fdadbc3d852 | 369 | if (c == '\n') { |
embeddedartists | 0:0fdadbc3d852 | 370 | cursor_y += textsize*8; |
embeddedartists | 0:0fdadbc3d852 | 371 | cursor_x = 0; |
embeddedartists | 0:0fdadbc3d852 | 372 | } else if (c == '\r') { |
embeddedartists | 0:0fdadbc3d852 | 373 | // skip em |
embeddedartists | 0:0fdadbc3d852 | 374 | } else { |
embeddedartists | 0:0fdadbc3d852 | 375 | drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize); |
embeddedartists | 0:0fdadbc3d852 | 376 | cursor_x += textsize*6; |
embeddedartists | 0:0fdadbc3d852 | 377 | if (wrap && (cursor_x > (_width - textsize*6))) { |
embeddedartists | 0:0fdadbc3d852 | 378 | cursor_y += textsize*8; |
embeddedartists | 0:0fdadbc3d852 | 379 | cursor_x = 0; |
embeddedartists | 0:0fdadbc3d852 | 380 | } |
embeddedartists | 0:0fdadbc3d852 | 381 | } |
embeddedartists | 0:0fdadbc3d852 | 382 | |
embeddedartists | 0:0fdadbc3d852 | 383 | return 1; |
embeddedartists | 0:0fdadbc3d852 | 384 | |
embeddedartists | 0:0fdadbc3d852 | 385 | } |
embeddedartists | 0:0fdadbc3d852 | 386 | |
embeddedartists | 0:0fdadbc3d852 | 387 | // Draw a character |
embeddedartists | 0:0fdadbc3d852 | 388 | void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c, |
embeddedartists | 0:0fdadbc3d852 | 389 | uint16_t color, uint16_t bg, uint8_t size) { |
embeddedartists | 0:0fdadbc3d852 | 390 | |
embeddedartists | 0:0fdadbc3d852 | 391 | if((x >= _width) || // Clip right |
embeddedartists | 0:0fdadbc3d852 | 392 | (y >= _height) || // Clip bottom |
embeddedartists | 0:0fdadbc3d852 | 393 | ((x + 6 * size - 1) < 0) || // Clip left |
embeddedartists | 0:0fdadbc3d852 | 394 | ((y + 8 * size - 1) < 0)) // Clip top |
embeddedartists | 0:0fdadbc3d852 | 395 | return; |
embeddedartists | 0:0fdadbc3d852 | 396 | |
embeddedartists | 0:0fdadbc3d852 | 397 | for (int8_t i=0; i<6; i++ ) { |
embeddedartists | 0:0fdadbc3d852 | 398 | uint8_t line; |
embeddedartists | 0:0fdadbc3d852 | 399 | if (i == 5) |
embeddedartists | 0:0fdadbc3d852 | 400 | line = 0x0; |
embeddedartists | 0:0fdadbc3d852 | 401 | else |
embeddedartists | 0:0fdadbc3d852 | 402 | line = pgm_read_byte(font+(c*5)+i); |
embeddedartists | 0:0fdadbc3d852 | 403 | for (int8_t j = 0; j<8; j++) { |
embeddedartists | 0:0fdadbc3d852 | 404 | if (line & 0x1) { |
embeddedartists | 0:0fdadbc3d852 | 405 | if (size == 1) // default size |
embeddedartists | 0:0fdadbc3d852 | 406 | drawPixel(x+i, y+j, color); |
embeddedartists | 0:0fdadbc3d852 | 407 | else { // big size |
embeddedartists | 0:0fdadbc3d852 | 408 | fillRect(x+(i*size), y+(j*size), size, size, color); |
embeddedartists | 0:0fdadbc3d852 | 409 | } |
embeddedartists | 0:0fdadbc3d852 | 410 | } else if (bg != color) { |
embeddedartists | 0:0fdadbc3d852 | 411 | if (size == 1) // default size |
embeddedartists | 0:0fdadbc3d852 | 412 | drawPixel(x+i, y+j, bg); |
embeddedartists | 0:0fdadbc3d852 | 413 | else { // big size |
embeddedartists | 0:0fdadbc3d852 | 414 | fillRect(x+i*size, y+j*size, size, size, bg); |
embeddedartists | 0:0fdadbc3d852 | 415 | } |
embeddedartists | 0:0fdadbc3d852 | 416 | } |
embeddedartists | 0:0fdadbc3d852 | 417 | line >>= 1; |
embeddedartists | 0:0fdadbc3d852 | 418 | } |
embeddedartists | 0:0fdadbc3d852 | 419 | } |
embeddedartists | 0:0fdadbc3d852 | 420 | } |
embeddedartists | 0:0fdadbc3d852 | 421 | |
embeddedartists | 0:0fdadbc3d852 | 422 | void Adafruit_GFX::setCursor(int16_t x, int16_t y) { |
embeddedartists | 0:0fdadbc3d852 | 423 | cursor_x = x; |
embeddedartists | 0:0fdadbc3d852 | 424 | cursor_y = y; |
embeddedartists | 0:0fdadbc3d852 | 425 | } |
embeddedartists | 0:0fdadbc3d852 | 426 | |
embeddedartists | 0:0fdadbc3d852 | 427 | void Adafruit_GFX::setTextSize(uint8_t s) { |
embeddedartists | 0:0fdadbc3d852 | 428 | textsize = (s > 0) ? s : 1; |
embeddedartists | 0:0fdadbc3d852 | 429 | } |
embeddedartists | 0:0fdadbc3d852 | 430 | |
embeddedartists | 0:0fdadbc3d852 | 431 | void Adafruit_GFX::setTextColor(uint16_t c) { |
embeddedartists | 0:0fdadbc3d852 | 432 | // For 'transparent' background, we'll set the bg |
embeddedartists | 0:0fdadbc3d852 | 433 | // to the same as fg instead of using a flag |
embeddedartists | 0:0fdadbc3d852 | 434 | textcolor = textbgcolor = c; |
embeddedartists | 0:0fdadbc3d852 | 435 | } |
embeddedartists | 0:0fdadbc3d852 | 436 | |
embeddedartists | 0:0fdadbc3d852 | 437 | void Adafruit_GFX::setTextColor(uint16_t c, uint16_t b) { |
embeddedartists | 0:0fdadbc3d852 | 438 | textcolor = c; |
embeddedartists | 0:0fdadbc3d852 | 439 | textbgcolor = b; |
embeddedartists | 0:0fdadbc3d852 | 440 | } |
embeddedartists | 0:0fdadbc3d852 | 441 | |
embeddedartists | 0:0fdadbc3d852 | 442 | void Adafruit_GFX::setTextWrap(bool w) { |
embeddedartists | 0:0fdadbc3d852 | 443 | wrap = w; |
embeddedartists | 0:0fdadbc3d852 | 444 | } |
embeddedartists | 0:0fdadbc3d852 | 445 | |
embeddedartists | 0:0fdadbc3d852 | 446 | uint8_t Adafruit_GFX::getRotation(void) { |
embeddedartists | 0:0fdadbc3d852 | 447 | return rotation; |
embeddedartists | 0:0fdadbc3d852 | 448 | } |
embeddedartists | 0:0fdadbc3d852 | 449 | |
embeddedartists | 0:0fdadbc3d852 | 450 | void Adafruit_GFX::setRotation(uint8_t x) { |
embeddedartists | 0:0fdadbc3d852 | 451 | rotation = (x & 3); |
embeddedartists | 0:0fdadbc3d852 | 452 | switch(rotation) { |
embeddedartists | 0:0fdadbc3d852 | 453 | case 0: |
embeddedartists | 0:0fdadbc3d852 | 454 | case 2: |
embeddedartists | 0:0fdadbc3d852 | 455 | _width = WIDTH; |
embeddedartists | 0:0fdadbc3d852 | 456 | _height = HEIGHT; |
embeddedartists | 0:0fdadbc3d852 | 457 | break; |
embeddedartists | 0:0fdadbc3d852 | 458 | case 1: |
embeddedartists | 0:0fdadbc3d852 | 459 | case 3: |
embeddedartists | 0:0fdadbc3d852 | 460 | _width = HEIGHT; |
embeddedartists | 0:0fdadbc3d852 | 461 | _height = WIDTH; |
embeddedartists | 0:0fdadbc3d852 | 462 | break; |
embeddedartists | 0:0fdadbc3d852 | 463 | } |
embeddedartists | 0:0fdadbc3d852 | 464 | } |
embeddedartists | 0:0fdadbc3d852 | 465 | |
embeddedartists | 0:0fdadbc3d852 | 466 | // Return the size of the display (per current rotation) |
embeddedartists | 0:0fdadbc3d852 | 467 | int16_t Adafruit_GFX::width(void) { |
embeddedartists | 0:0fdadbc3d852 | 468 | return _width; |
embeddedartists | 0:0fdadbc3d852 | 469 | } |
embeddedartists | 0:0fdadbc3d852 | 470 | |
embeddedartists | 0:0fdadbc3d852 | 471 | int16_t Adafruit_GFX::height(void) { |
embeddedartists | 0:0fdadbc3d852 | 472 | return _height; |
embeddedartists | 0:0fdadbc3d852 | 473 | } |
embeddedartists | 0:0fdadbc3d852 | 474 | |
embeddedartists | 0:0fdadbc3d852 | 475 | void Adafruit_GFX::invertDisplay(bool i) { |
embeddedartists | 0:0fdadbc3d852 | 476 | // Do nothing, must be subclassed if supported |
embeddedartists | 0:0fdadbc3d852 | 477 | } |
embeddedartists | 0:0fdadbc3d852 | 478 | |
embeddedartists | 0:0fdadbc3d852 | 479 |