Port of the Adafruit_GFX library for use in MBED. This is the core graphics library for all Adafruit's displays, providing a common set of graphics primitives (points, lines, circles, strings,bitmaps. etc). Complete details found @ https://github.com/adafruit/Adafruit-GFX-Library Required by Adafruit_PCD8544 Driver. BSD license.
Dependents: ili9341_test ece495_firmware Adafruit-64x32-PWM-Demo Pong_ILI9163C
Adafruit_GFX.cpp@0:50ff23c02d57, 2014-11-02 (annotated)
- Committer:
- infotech1
- Date:
- Sun Nov 02 07:06:22 2014 +0000
- Revision:
- 0:50ff23c02d57
Initial port of Adafruit GFX library for MBED
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
infotech1 | 0:50ff23c02d57 | 1 | /* |
infotech1 | 0:50ff23c02d57 | 2 | This is the core graphics library for all our displays, providing a common |
infotech1 | 0:50ff23c02d57 | 3 | set of graphics primitives (points, lines, circles, etc.). It needs to be |
infotech1 | 0:50ff23c02d57 | 4 | paired with a hardware-specific library for each display device we carry |
infotech1 | 0:50ff23c02d57 | 5 | (to handle the lower-level functions). |
infotech1 | 0:50ff23c02d57 | 6 | |
infotech1 | 0:50ff23c02d57 | 7 | Adafruit invests time and resources providing this open source code, please |
infotech1 | 0:50ff23c02d57 | 8 | support Adafruit & open-source hardware by purchasing products from Adafruit! |
infotech1 | 0:50ff23c02d57 | 9 | |
infotech1 | 0:50ff23c02d57 | 10 | Copyright (c) 2013 Adafruit Industries. All rights reserved. |
infotech1 | 0:50ff23c02d57 | 11 | |
infotech1 | 0:50ff23c02d57 | 12 | Redistribution and use in source and binary forms, with or without |
infotech1 | 0:50ff23c02d57 | 13 | modification, are permitted provided that the following conditions are met: |
infotech1 | 0:50ff23c02d57 | 14 | |
infotech1 | 0:50ff23c02d57 | 15 | - Redistributions of source code must retain the above copyright notice, |
infotech1 | 0:50ff23c02d57 | 16 | this list of conditions and the following disclaimer. |
infotech1 | 0:50ff23c02d57 | 17 | - Redistributions in binary form must reproduce the above copyright notice, |
infotech1 | 0:50ff23c02d57 | 18 | this list of conditions and the following disclaimer in the documentation |
infotech1 | 0:50ff23c02d57 | 19 | and/or other materials provided with the distribution. |
infotech1 | 0:50ff23c02d57 | 20 | |
infotech1 | 0:50ff23c02d57 | 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
infotech1 | 0:50ff23c02d57 | 22 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
infotech1 | 0:50ff23c02d57 | 23 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
infotech1 | 0:50ff23c02d57 | 24 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
infotech1 | 0:50ff23c02d57 | 25 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
infotech1 | 0:50ff23c02d57 | 26 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
infotech1 | 0:50ff23c02d57 | 27 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
infotech1 | 0:50ff23c02d57 | 28 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
infotech1 | 0:50ff23c02d57 | 29 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
infotech1 | 0:50ff23c02d57 | 30 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
infotech1 | 0:50ff23c02d57 | 31 | POSSIBILITY OF SUCH DAMAGE.*/ |
infotech1 | 0:50ff23c02d57 | 32 | |
infotech1 | 0:50ff23c02d57 | 33 | /*Modified for MBED usage and tested with STM32F411RE on a Nucleo board. |
infotech1 | 0:50ff23c02d57 | 34 | Embedded Print methods from Arduino Print.Cpp/Print.h |
infotech1 | 0:50ff23c02d57 | 35 | |
infotech1 | 0:50ff23c02d57 | 36 | by James Kidd 2014 |
infotech1 | 0:50ff23c02d57 | 37 | * */ |
infotech1 | 0:50ff23c02d57 | 38 | |
infotech1 | 0:50ff23c02d57 | 39 | #include <stdint.h> |
infotech1 | 0:50ff23c02d57 | 40 | #include "Adafruit_GFX.h" |
infotech1 | 0:50ff23c02d57 | 41 | #include "glcdfont.c" |
infotech1 | 0:50ff23c02d57 | 42 | #include <math.h> |
infotech1 | 0:50ff23c02d57 | 43 | #include <stdlib.h> |
infotech1 | 0:50ff23c02d57 | 44 | #include <stddef.h> |
infotech1 | 0:50ff23c02d57 | 45 | #define pgm_read_byte(addr) (*(const unsigned char *)(addr)) |
infotech1 | 0:50ff23c02d57 | 46 | |
infotech1 | 0:50ff23c02d57 | 47 | |
infotech1 | 0:50ff23c02d57 | 48 | Adafruit_GFX::Adafruit_GFX(int16_t w, int16_t h): |
infotech1 | 0:50ff23c02d57 | 49 | WIDTH(w), HEIGHT(h) |
infotech1 | 0:50ff23c02d57 | 50 | { |
infotech1 | 0:50ff23c02d57 | 51 | _width = WIDTH; |
infotech1 | 0:50ff23c02d57 | 52 | _height = HEIGHT; |
infotech1 | 0:50ff23c02d57 | 53 | rotation = 0; |
infotech1 | 0:50ff23c02d57 | 54 | cursor_y = cursor_x = 0; |
infotech1 | 0:50ff23c02d57 | 55 | textsize = 1; |
infotech1 | 0:50ff23c02d57 | 56 | textcolor = textbgcolor = 0xFFFF; |
infotech1 | 0:50ff23c02d57 | 57 | wrap = true; |
infotech1 | 0:50ff23c02d57 | 58 | } |
infotech1 | 0:50ff23c02d57 | 59 | |
infotech1 | 0:50ff23c02d57 | 60 | // Draw a circle outline |
infotech1 | 0:50ff23c02d57 | 61 | void Adafruit_GFX::drawCircle(int16_t x0, int16_t y0, int16_t r, |
infotech1 | 0:50ff23c02d57 | 62 | uint16_t color) { |
infotech1 | 0:50ff23c02d57 | 63 | int16_t f = 1 - r; |
infotech1 | 0:50ff23c02d57 | 64 | int16_t ddF_x = 1; |
infotech1 | 0:50ff23c02d57 | 65 | int16_t ddF_y = -2 * r; |
infotech1 | 0:50ff23c02d57 | 66 | int16_t x = 0; |
infotech1 | 0:50ff23c02d57 | 67 | int16_t y = r; |
infotech1 | 0:50ff23c02d57 | 68 | |
infotech1 | 0:50ff23c02d57 | 69 | drawPixel(x0 , y0+r, color); |
infotech1 | 0:50ff23c02d57 | 70 | drawPixel(x0 , y0-r, color); |
infotech1 | 0:50ff23c02d57 | 71 | drawPixel(x0+r, y0 , color); |
infotech1 | 0:50ff23c02d57 | 72 | drawPixel(x0-r, y0 , color); |
infotech1 | 0:50ff23c02d57 | 73 | |
infotech1 | 0:50ff23c02d57 | 74 | while (x<y) { |
infotech1 | 0:50ff23c02d57 | 75 | if (f >= 0) { |
infotech1 | 0:50ff23c02d57 | 76 | y--; |
infotech1 | 0:50ff23c02d57 | 77 | ddF_y += 2; |
infotech1 | 0:50ff23c02d57 | 78 | f += ddF_y; |
infotech1 | 0:50ff23c02d57 | 79 | } |
infotech1 | 0:50ff23c02d57 | 80 | x++; |
infotech1 | 0:50ff23c02d57 | 81 | ddF_x += 2; |
infotech1 | 0:50ff23c02d57 | 82 | f += ddF_x; |
infotech1 | 0:50ff23c02d57 | 83 | |
infotech1 | 0:50ff23c02d57 | 84 | drawPixel(x0 + x, y0 + y, color); |
infotech1 | 0:50ff23c02d57 | 85 | drawPixel(x0 - x, y0 + y, color); |
infotech1 | 0:50ff23c02d57 | 86 | drawPixel(x0 + x, y0 - y, color); |
infotech1 | 0:50ff23c02d57 | 87 | drawPixel(x0 - x, y0 - y, color); |
infotech1 | 0:50ff23c02d57 | 88 | drawPixel(x0 + y, y0 + x, color); |
infotech1 | 0:50ff23c02d57 | 89 | drawPixel(x0 - y, y0 + x, color); |
infotech1 | 0:50ff23c02d57 | 90 | drawPixel(x0 + y, y0 - x, color); |
infotech1 | 0:50ff23c02d57 | 91 | drawPixel(x0 - y, y0 - x, color); |
infotech1 | 0:50ff23c02d57 | 92 | } |
infotech1 | 0:50ff23c02d57 | 93 | } |
infotech1 | 0:50ff23c02d57 | 94 | |
infotech1 | 0:50ff23c02d57 | 95 | void Adafruit_GFX::drawCircleHelper( int16_t x0, int16_t y0, |
infotech1 | 0:50ff23c02d57 | 96 | int16_t r, uint8_t cornername, uint16_t color) { |
infotech1 | 0:50ff23c02d57 | 97 | int16_t f = 1 - r; |
infotech1 | 0:50ff23c02d57 | 98 | int16_t ddF_x = 1; |
infotech1 | 0:50ff23c02d57 | 99 | int16_t ddF_y = -2 * r; |
infotech1 | 0:50ff23c02d57 | 100 | int16_t x = 0; |
infotech1 | 0:50ff23c02d57 | 101 | int16_t y = r; |
infotech1 | 0:50ff23c02d57 | 102 | |
infotech1 | 0:50ff23c02d57 | 103 | while (x<y) { |
infotech1 | 0:50ff23c02d57 | 104 | if (f >= 0) { |
infotech1 | 0:50ff23c02d57 | 105 | y--; |
infotech1 | 0:50ff23c02d57 | 106 | ddF_y += 2; |
infotech1 | 0:50ff23c02d57 | 107 | f += ddF_y; |
infotech1 | 0:50ff23c02d57 | 108 | } |
infotech1 | 0:50ff23c02d57 | 109 | x++; |
infotech1 | 0:50ff23c02d57 | 110 | ddF_x += 2; |
infotech1 | 0:50ff23c02d57 | 111 | f += ddF_x; |
infotech1 | 0:50ff23c02d57 | 112 | if (cornername & 0x4) { |
infotech1 | 0:50ff23c02d57 | 113 | drawPixel(x0 + x, y0 + y, color); |
infotech1 | 0:50ff23c02d57 | 114 | drawPixel(x0 + y, y0 + x, color); |
infotech1 | 0:50ff23c02d57 | 115 | } |
infotech1 | 0:50ff23c02d57 | 116 | if (cornername & 0x2) { |
infotech1 | 0:50ff23c02d57 | 117 | drawPixel(x0 + x, y0 - y, color); |
infotech1 | 0:50ff23c02d57 | 118 | drawPixel(x0 + y, y0 - x, color); |
infotech1 | 0:50ff23c02d57 | 119 | } |
infotech1 | 0:50ff23c02d57 | 120 | if (cornername & 0x8) { |
infotech1 | 0:50ff23c02d57 | 121 | drawPixel(x0 - y, y0 + x, color); |
infotech1 | 0:50ff23c02d57 | 122 | drawPixel(x0 - x, y0 + y, color); |
infotech1 | 0:50ff23c02d57 | 123 | } |
infotech1 | 0:50ff23c02d57 | 124 | if (cornername & 0x1) { |
infotech1 | 0:50ff23c02d57 | 125 | drawPixel(x0 - y, y0 - x, color); |
infotech1 | 0:50ff23c02d57 | 126 | drawPixel(x0 - x, y0 - y, color); |
infotech1 | 0:50ff23c02d57 | 127 | } |
infotech1 | 0:50ff23c02d57 | 128 | } |
infotech1 | 0:50ff23c02d57 | 129 | } |
infotech1 | 0:50ff23c02d57 | 130 | |
infotech1 | 0:50ff23c02d57 | 131 | void Adafruit_GFX::fillCircle(int16_t x0, int16_t y0, int16_t r, |
infotech1 | 0:50ff23c02d57 | 132 | uint16_t color) { |
infotech1 | 0:50ff23c02d57 | 133 | drawFastVLine(x0, y0-r, 2*r+1, color); |
infotech1 | 0:50ff23c02d57 | 134 | fillCircleHelper(x0, y0, r, 3, 0, color); |
infotech1 | 0:50ff23c02d57 | 135 | } |
infotech1 | 0:50ff23c02d57 | 136 | |
infotech1 | 0:50ff23c02d57 | 137 | // Used to do circles and roundrects |
infotech1 | 0:50ff23c02d57 | 138 | void Adafruit_GFX::fillCircleHelper(int16_t x0, int16_t y0, int16_t r, |
infotech1 | 0:50ff23c02d57 | 139 | uint8_t cornername, int16_t delta, uint16_t color) { |
infotech1 | 0:50ff23c02d57 | 140 | |
infotech1 | 0:50ff23c02d57 | 141 | int16_t f = 1 - r; |
infotech1 | 0:50ff23c02d57 | 142 | int16_t ddF_x = 1; |
infotech1 | 0:50ff23c02d57 | 143 | int16_t ddF_y = -2 * r; |
infotech1 | 0:50ff23c02d57 | 144 | int16_t x = 0; |
infotech1 | 0:50ff23c02d57 | 145 | int16_t y = r; |
infotech1 | 0:50ff23c02d57 | 146 | |
infotech1 | 0:50ff23c02d57 | 147 | while (x<y) { |
infotech1 | 0:50ff23c02d57 | 148 | if (f >= 0) { |
infotech1 | 0:50ff23c02d57 | 149 | y--; |
infotech1 | 0:50ff23c02d57 | 150 | ddF_y += 2; |
infotech1 | 0:50ff23c02d57 | 151 | f += ddF_y; |
infotech1 | 0:50ff23c02d57 | 152 | } |
infotech1 | 0:50ff23c02d57 | 153 | x++; |
infotech1 | 0:50ff23c02d57 | 154 | ddF_x += 2; |
infotech1 | 0:50ff23c02d57 | 155 | f += ddF_x; |
infotech1 | 0:50ff23c02d57 | 156 | |
infotech1 | 0:50ff23c02d57 | 157 | if (cornername & 0x1) { |
infotech1 | 0:50ff23c02d57 | 158 | drawFastVLine(x0+x, y0-y, 2*y+1+delta, color); |
infotech1 | 0:50ff23c02d57 | 159 | drawFastVLine(x0+y, y0-x, 2*x+1+delta, color); |
infotech1 | 0:50ff23c02d57 | 160 | } |
infotech1 | 0:50ff23c02d57 | 161 | if (cornername & 0x2) { |
infotech1 | 0:50ff23c02d57 | 162 | drawFastVLine(x0-x, y0-y, 2*y+1+delta, color); |
infotech1 | 0:50ff23c02d57 | 163 | drawFastVLine(x0-y, y0-x, 2*x+1+delta, color); |
infotech1 | 0:50ff23c02d57 | 164 | } |
infotech1 | 0:50ff23c02d57 | 165 | } |
infotech1 | 0:50ff23c02d57 | 166 | } |
infotech1 | 0:50ff23c02d57 | 167 | |
infotech1 | 0:50ff23c02d57 | 168 | // Bresenham's algorithm - thx wikpedia |
infotech1 | 0:50ff23c02d57 | 169 | void Adafruit_GFX::drawLine(int16_t x0, int16_t y0, |
infotech1 | 0:50ff23c02d57 | 170 | int16_t x1, int16_t y1, |
infotech1 | 0:50ff23c02d57 | 171 | uint16_t color) { |
infotech1 | 0:50ff23c02d57 | 172 | int16_t steep = abs(y1 - y0) > abs(x1 - x0); |
infotech1 | 0:50ff23c02d57 | 173 | if (steep) { |
infotech1 | 0:50ff23c02d57 | 174 | swap(x0, y0); |
infotech1 | 0:50ff23c02d57 | 175 | swap(x1, y1); |
infotech1 | 0:50ff23c02d57 | 176 | } |
infotech1 | 0:50ff23c02d57 | 177 | |
infotech1 | 0:50ff23c02d57 | 178 | if (x0 > x1) { |
infotech1 | 0:50ff23c02d57 | 179 | swap(x0, x1); |
infotech1 | 0:50ff23c02d57 | 180 | swap(y0, y1); |
infotech1 | 0:50ff23c02d57 | 181 | } |
infotech1 | 0:50ff23c02d57 | 182 | |
infotech1 | 0:50ff23c02d57 | 183 | int16_t dx, dy; |
infotech1 | 0:50ff23c02d57 | 184 | dx = x1 - x0; |
infotech1 | 0:50ff23c02d57 | 185 | dy = abs(y1 - y0); |
infotech1 | 0:50ff23c02d57 | 186 | |
infotech1 | 0:50ff23c02d57 | 187 | int16_t err = dx / 2; |
infotech1 | 0:50ff23c02d57 | 188 | int16_t ystep; |
infotech1 | 0:50ff23c02d57 | 189 | |
infotech1 | 0:50ff23c02d57 | 190 | if (y0 < y1) { |
infotech1 | 0:50ff23c02d57 | 191 | ystep = 1; |
infotech1 | 0:50ff23c02d57 | 192 | } else { |
infotech1 | 0:50ff23c02d57 | 193 | ystep = -1; |
infotech1 | 0:50ff23c02d57 | 194 | } |
infotech1 | 0:50ff23c02d57 | 195 | |
infotech1 | 0:50ff23c02d57 | 196 | for (; x0<=x1; x0++) { |
infotech1 | 0:50ff23c02d57 | 197 | if (steep) { |
infotech1 | 0:50ff23c02d57 | 198 | drawPixel(y0, x0, color); |
infotech1 | 0:50ff23c02d57 | 199 | } else { |
infotech1 | 0:50ff23c02d57 | 200 | drawPixel(x0, y0, color); |
infotech1 | 0:50ff23c02d57 | 201 | } |
infotech1 | 0:50ff23c02d57 | 202 | err -= dy; |
infotech1 | 0:50ff23c02d57 | 203 | if (err < 0) { |
infotech1 | 0:50ff23c02d57 | 204 | y0 += ystep; |
infotech1 | 0:50ff23c02d57 | 205 | err += dx; |
infotech1 | 0:50ff23c02d57 | 206 | } |
infotech1 | 0:50ff23c02d57 | 207 | } |
infotech1 | 0:50ff23c02d57 | 208 | } |
infotech1 | 0:50ff23c02d57 | 209 | |
infotech1 | 0:50ff23c02d57 | 210 | // Draw a rectangle |
infotech1 | 0:50ff23c02d57 | 211 | void Adafruit_GFX::drawRect(int16_t x, int16_t y, |
infotech1 | 0:50ff23c02d57 | 212 | int16_t w, int16_t h, |
infotech1 | 0:50ff23c02d57 | 213 | uint16_t color) { |
infotech1 | 0:50ff23c02d57 | 214 | drawFastHLine(x, y, w, color); |
infotech1 | 0:50ff23c02d57 | 215 | drawFastHLine(x, y+h-1, w, color); |
infotech1 | 0:50ff23c02d57 | 216 | drawFastVLine(x, y, h, color); |
infotech1 | 0:50ff23c02d57 | 217 | drawFastVLine(x+w-1, y, h, color); |
infotech1 | 0:50ff23c02d57 | 218 | } |
infotech1 | 0:50ff23c02d57 | 219 | |
infotech1 | 0:50ff23c02d57 | 220 | void Adafruit_GFX::drawFastVLine(int16_t x, int16_t y, |
infotech1 | 0:50ff23c02d57 | 221 | int16_t h, uint16_t color) { |
infotech1 | 0:50ff23c02d57 | 222 | // Update in subclasses if desired! |
infotech1 | 0:50ff23c02d57 | 223 | drawLine(x, y, x, y+h-1, color); |
infotech1 | 0:50ff23c02d57 | 224 | } |
infotech1 | 0:50ff23c02d57 | 225 | |
infotech1 | 0:50ff23c02d57 | 226 | void Adafruit_GFX::drawFastHLine(int16_t x, int16_t y, |
infotech1 | 0:50ff23c02d57 | 227 | int16_t w, uint16_t color) { |
infotech1 | 0:50ff23c02d57 | 228 | // Update in subclasses if desired! |
infotech1 | 0:50ff23c02d57 | 229 | drawLine(x, y, x+w-1, y, color); |
infotech1 | 0:50ff23c02d57 | 230 | } |
infotech1 | 0:50ff23c02d57 | 231 | |
infotech1 | 0:50ff23c02d57 | 232 | void Adafruit_GFX::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, |
infotech1 | 0:50ff23c02d57 | 233 | uint16_t color) { |
infotech1 | 0:50ff23c02d57 | 234 | // Update in subclasses if desired! |
infotech1 | 0:50ff23c02d57 | 235 | for (int16_t i=x; i<x+w; i++) { |
infotech1 | 0:50ff23c02d57 | 236 | drawFastVLine(i, y, h, color); |
infotech1 | 0:50ff23c02d57 | 237 | } |
infotech1 | 0:50ff23c02d57 | 238 | } |
infotech1 | 0:50ff23c02d57 | 239 | |
infotech1 | 0:50ff23c02d57 | 240 | void Adafruit_GFX::fillScreen(uint16_t color) { |
infotech1 | 0:50ff23c02d57 | 241 | fillRect(0, 0, _width, _height, color); |
infotech1 | 0:50ff23c02d57 | 242 | } |
infotech1 | 0:50ff23c02d57 | 243 | |
infotech1 | 0:50ff23c02d57 | 244 | // Draw a rounded rectangle |
infotech1 | 0:50ff23c02d57 | 245 | void Adafruit_GFX::drawRoundRect(int16_t x, int16_t y, int16_t w, |
infotech1 | 0:50ff23c02d57 | 246 | int16_t h, int16_t r, uint16_t color) { |
infotech1 | 0:50ff23c02d57 | 247 | // smarter version |
infotech1 | 0:50ff23c02d57 | 248 | drawFastHLine(x+r , y , w-2*r, color); // Top |
infotech1 | 0:50ff23c02d57 | 249 | drawFastHLine(x+r , y+h-1, w-2*r, color); // Bottom |
infotech1 | 0:50ff23c02d57 | 250 | drawFastVLine(x , y+r , h-2*r, color); // Left |
infotech1 | 0:50ff23c02d57 | 251 | drawFastVLine(x+w-1, y+r , h-2*r, color); // Right |
infotech1 | 0:50ff23c02d57 | 252 | // draw four corners |
infotech1 | 0:50ff23c02d57 | 253 | drawCircleHelper(x+r , y+r , r, 1, color); |
infotech1 | 0:50ff23c02d57 | 254 | drawCircleHelper(x+w-r-1, y+r , r, 2, color); |
infotech1 | 0:50ff23c02d57 | 255 | drawCircleHelper(x+w-r-1, y+h-r-1, r, 4, color); |
infotech1 | 0:50ff23c02d57 | 256 | drawCircleHelper(x+r , y+h-r-1, r, 8, color); |
infotech1 | 0:50ff23c02d57 | 257 | } |
infotech1 | 0:50ff23c02d57 | 258 | |
infotech1 | 0:50ff23c02d57 | 259 | // Fill a rounded rectangle |
infotech1 | 0:50ff23c02d57 | 260 | void Adafruit_GFX::fillRoundRect(int16_t x, int16_t y, int16_t w, |
infotech1 | 0:50ff23c02d57 | 261 | int16_t h, int16_t r, uint16_t color) { |
infotech1 | 0:50ff23c02d57 | 262 | // smarter version |
infotech1 | 0:50ff23c02d57 | 263 | fillRect(x+r, y, w-2*r, h, color); |
infotech1 | 0:50ff23c02d57 | 264 | |
infotech1 | 0:50ff23c02d57 | 265 | // draw four corners |
infotech1 | 0:50ff23c02d57 | 266 | fillCircleHelper(x+w-r-1, y+r, r, 1, h-2*r-1, color); |
infotech1 | 0:50ff23c02d57 | 267 | fillCircleHelper(x+r , y+r, r, 2, h-2*r-1, color); |
infotech1 | 0:50ff23c02d57 | 268 | } |
infotech1 | 0:50ff23c02d57 | 269 | |
infotech1 | 0:50ff23c02d57 | 270 | // Draw a triangle |
infotech1 | 0:50ff23c02d57 | 271 | void Adafruit_GFX::drawTriangle(int16_t x0, int16_t y0, |
infotech1 | 0:50ff23c02d57 | 272 | int16_t x1, int16_t y1, |
infotech1 | 0:50ff23c02d57 | 273 | int16_t x2, int16_t y2, uint16_t color) { |
infotech1 | 0:50ff23c02d57 | 274 | drawLine(x0, y0, x1, y1, color); |
infotech1 | 0:50ff23c02d57 | 275 | drawLine(x1, y1, x2, y2, color); |
infotech1 | 0:50ff23c02d57 | 276 | drawLine(x2, y2, x0, y0, color); |
infotech1 | 0:50ff23c02d57 | 277 | } |
infotech1 | 0:50ff23c02d57 | 278 | |
infotech1 | 0:50ff23c02d57 | 279 | // Fill a triangle |
infotech1 | 0:50ff23c02d57 | 280 | void Adafruit_GFX::fillTriangle ( int16_t x0, int16_t y0, |
infotech1 | 0:50ff23c02d57 | 281 | int16_t x1, int16_t y1, |
infotech1 | 0:50ff23c02d57 | 282 | int16_t x2, int16_t y2, uint16_t color) { |
infotech1 | 0:50ff23c02d57 | 283 | |
infotech1 | 0:50ff23c02d57 | 284 | int16_t a, b, y, last; |
infotech1 | 0:50ff23c02d57 | 285 | |
infotech1 | 0:50ff23c02d57 | 286 | // Sort coordinates by Y order (y2 >= y1 >= y0) |
infotech1 | 0:50ff23c02d57 | 287 | if (y0 > y1) { |
infotech1 | 0:50ff23c02d57 | 288 | swap(y0, y1); swap(x0, x1); |
infotech1 | 0:50ff23c02d57 | 289 | } |
infotech1 | 0:50ff23c02d57 | 290 | if (y1 > y2) { |
infotech1 | 0:50ff23c02d57 | 291 | swap(y2, y1); swap(x2, x1); |
infotech1 | 0:50ff23c02d57 | 292 | } |
infotech1 | 0:50ff23c02d57 | 293 | if (y0 > y1) { |
infotech1 | 0:50ff23c02d57 | 294 | swap(y0, y1); swap(x0, x1); |
infotech1 | 0:50ff23c02d57 | 295 | } |
infotech1 | 0:50ff23c02d57 | 296 | |
infotech1 | 0:50ff23c02d57 | 297 | if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing |
infotech1 | 0:50ff23c02d57 | 298 | a = b = x0; |
infotech1 | 0:50ff23c02d57 | 299 | if(x1 < a) a = x1; |
infotech1 | 0:50ff23c02d57 | 300 | else if(x1 > b) b = x1; |
infotech1 | 0:50ff23c02d57 | 301 | if(x2 < a) a = x2; |
infotech1 | 0:50ff23c02d57 | 302 | else if(x2 > b) b = x2; |
infotech1 | 0:50ff23c02d57 | 303 | drawFastHLine(a, y0, b-a+1, color); |
infotech1 | 0:50ff23c02d57 | 304 | return; |
infotech1 | 0:50ff23c02d57 | 305 | } |
infotech1 | 0:50ff23c02d57 | 306 | |
infotech1 | 0:50ff23c02d57 | 307 | int16_t |
infotech1 | 0:50ff23c02d57 | 308 | dx01 = x1 - x0, |
infotech1 | 0:50ff23c02d57 | 309 | dy01 = y1 - y0, |
infotech1 | 0:50ff23c02d57 | 310 | dx02 = x2 - x0, |
infotech1 | 0:50ff23c02d57 | 311 | dy02 = y2 - y0, |
infotech1 | 0:50ff23c02d57 | 312 | dx12 = x2 - x1, |
infotech1 | 0:50ff23c02d57 | 313 | dy12 = y2 - y1; |
infotech1 | 0:50ff23c02d57 | 314 | int32_t |
infotech1 | 0:50ff23c02d57 | 315 | sa = 0, |
infotech1 | 0:50ff23c02d57 | 316 | sb = 0; |
infotech1 | 0:50ff23c02d57 | 317 | |
infotech1 | 0:50ff23c02d57 | 318 | // For upper part of triangle, find scanline crossings for segments |
infotech1 | 0:50ff23c02d57 | 319 | // 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1 |
infotech1 | 0:50ff23c02d57 | 320 | // is included here (and second loop will be skipped, avoiding a /0 |
infotech1 | 0:50ff23c02d57 | 321 | // error there), otherwise scanline y1 is skipped here and handled |
infotech1 | 0:50ff23c02d57 | 322 | // in the second loop...which also avoids a /0 error here if y0=y1 |
infotech1 | 0:50ff23c02d57 | 323 | // (flat-topped triangle). |
infotech1 | 0:50ff23c02d57 | 324 | if(y1 == y2) last = y1; // Include y1 scanline |
infotech1 | 0:50ff23c02d57 | 325 | else last = y1-1; // Skip it |
infotech1 | 0:50ff23c02d57 | 326 | |
infotech1 | 0:50ff23c02d57 | 327 | for(y=y0; y<=last; y++) { |
infotech1 | 0:50ff23c02d57 | 328 | a = x0 + sa / dy01; |
infotech1 | 0:50ff23c02d57 | 329 | b = x0 + sb / dy02; |
infotech1 | 0:50ff23c02d57 | 330 | sa += dx01; |
infotech1 | 0:50ff23c02d57 | 331 | sb += dx02; |
infotech1 | 0:50ff23c02d57 | 332 | /* longhand: |
infotech1 | 0:50ff23c02d57 | 333 | a = x0 + (x1 - x0) * (y - y0) / (y1 - y0); |
infotech1 | 0:50ff23c02d57 | 334 | b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); |
infotech1 | 0:50ff23c02d57 | 335 | */ |
infotech1 | 0:50ff23c02d57 | 336 | if(a > b) swap(a,b); |
infotech1 | 0:50ff23c02d57 | 337 | drawFastHLine(a, y, b-a+1, color); |
infotech1 | 0:50ff23c02d57 | 338 | } |
infotech1 | 0:50ff23c02d57 | 339 | |
infotech1 | 0:50ff23c02d57 | 340 | // For lower part of triangle, find scanline crossings for segments |
infotech1 | 0:50ff23c02d57 | 341 | // 0-2 and 1-2. This loop is skipped if y1=y2. |
infotech1 | 0:50ff23c02d57 | 342 | sa = dx12 * (y - y1); |
infotech1 | 0:50ff23c02d57 | 343 | sb = dx02 * (y - y0); |
infotech1 | 0:50ff23c02d57 | 344 | for(; y<=y2; y++) { |
infotech1 | 0:50ff23c02d57 | 345 | a = x1 + sa / dy12; |
infotech1 | 0:50ff23c02d57 | 346 | b = x0 + sb / dy02; |
infotech1 | 0:50ff23c02d57 | 347 | sa += dx12; |
infotech1 | 0:50ff23c02d57 | 348 | sb += dx02; |
infotech1 | 0:50ff23c02d57 | 349 | /* longhand: |
infotech1 | 0:50ff23c02d57 | 350 | a = x1 + (x2 - x1) * (y - y1) / (y2 - y1); |
infotech1 | 0:50ff23c02d57 | 351 | b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); |
infotech1 | 0:50ff23c02d57 | 352 | */ |
infotech1 | 0:50ff23c02d57 | 353 | if(a > b) swap(a,b); |
infotech1 | 0:50ff23c02d57 | 354 | drawFastHLine(a, y, b-a+1, color); |
infotech1 | 0:50ff23c02d57 | 355 | } |
infotech1 | 0:50ff23c02d57 | 356 | } |
infotech1 | 0:50ff23c02d57 | 357 | |
infotech1 | 0:50ff23c02d57 | 358 | void Adafruit_GFX::drawBitmap(int16_t x, int16_t y, |
infotech1 | 0:50ff23c02d57 | 359 | const uint8_t *bitmap, int16_t w, int16_t h, |
infotech1 | 0:50ff23c02d57 | 360 | uint16_t color) { |
infotech1 | 0:50ff23c02d57 | 361 | |
infotech1 | 0:50ff23c02d57 | 362 | int16_t i, j, byteWidth = (w + 7) / 8; |
infotech1 | 0:50ff23c02d57 | 363 | |
infotech1 | 0:50ff23c02d57 | 364 | for(j=0; j<h; j++) { |
infotech1 | 0:50ff23c02d57 | 365 | for(i=0; i<w; i++ ) { |
infotech1 | 0:50ff23c02d57 | 366 | if(pgm_read_byte(bitmap + j * byteWidth + i / 8) & (128 >> (i & 7))) { |
infotech1 | 0:50ff23c02d57 | 367 | drawPixel(x+i, y+j, color); |
infotech1 | 0:50ff23c02d57 | 368 | } |
infotech1 | 0:50ff23c02d57 | 369 | } |
infotech1 | 0:50ff23c02d57 | 370 | } |
infotech1 | 0:50ff23c02d57 | 371 | } |
infotech1 | 0:50ff23c02d57 | 372 | |
infotech1 | 0:50ff23c02d57 | 373 | // Draw a 1-bit color bitmap at the specified x, y position from the |
infotech1 | 0:50ff23c02d57 | 374 | // provided bitmap buffer (must be PROGMEM memory) using color as the |
infotech1 | 0:50ff23c02d57 | 375 | // foreground color and bg as the background color. |
infotech1 | 0:50ff23c02d57 | 376 | void Adafruit_GFX::drawBitmap(int16_t x, int16_t y, |
infotech1 | 0:50ff23c02d57 | 377 | const uint8_t *bitmap, int16_t w, int16_t h, |
infotech1 | 0:50ff23c02d57 | 378 | uint16_t color, uint16_t bg) { |
infotech1 | 0:50ff23c02d57 | 379 | |
infotech1 | 0:50ff23c02d57 | 380 | int16_t i, j, byteWidth = (w + 7) / 8; |
infotech1 | 0:50ff23c02d57 | 381 | |
infotech1 | 0:50ff23c02d57 | 382 | for(j=0; j<h; j++) { |
infotech1 | 0:50ff23c02d57 | 383 | for(i=0; i<w; i++ ) { |
infotech1 | 0:50ff23c02d57 | 384 | if(pgm_read_byte(bitmap + j * byteWidth + i / 8) & (128 >> (i & 7))) { |
infotech1 | 0:50ff23c02d57 | 385 | drawPixel(x+i, y+j, color); |
infotech1 | 0:50ff23c02d57 | 386 | } |
infotech1 | 0:50ff23c02d57 | 387 | else { |
infotech1 | 0:50ff23c02d57 | 388 | drawPixel(x+i, y+j, bg); |
infotech1 | 0:50ff23c02d57 | 389 | } |
infotech1 | 0:50ff23c02d57 | 390 | } |
infotech1 | 0:50ff23c02d57 | 391 | } |
infotech1 | 0:50ff23c02d57 | 392 | } |
infotech1 | 0:50ff23c02d57 | 393 | |
infotech1 | 0:50ff23c02d57 | 394 | //Draw XBitMap Files (*.xbm), exported from GIMP, |
infotech1 | 0:50ff23c02d57 | 395 | //Usage: Export from GIMP to *.xbm, rename *.xbm to *.c and open in editor. |
infotech1 | 0:50ff23c02d57 | 396 | //C Array can be directly used with this function |
infotech1 | 0:50ff23c02d57 | 397 | void Adafruit_GFX::drawXBitmap(int16_t x, int16_t y, |
infotech1 | 0:50ff23c02d57 | 398 | const uint8_t *bitmap, int16_t w, int16_t h, |
infotech1 | 0:50ff23c02d57 | 399 | uint16_t color) { |
infotech1 | 0:50ff23c02d57 | 400 | |
infotech1 | 0:50ff23c02d57 | 401 | int16_t i, j, byteWidth = (w + 7) / 8; |
infotech1 | 0:50ff23c02d57 | 402 | |
infotech1 | 0:50ff23c02d57 | 403 | for(j=0; j<h; j++) { |
infotech1 | 0:50ff23c02d57 | 404 | for(i=0; i<w; i++ ) { |
infotech1 | 0:50ff23c02d57 | 405 | if(pgm_read_byte(bitmap + j * byteWidth + i / 8) & (1 << (i % 8))) { |
infotech1 | 0:50ff23c02d57 | 406 | drawPixel(x+i, y+j, color); |
infotech1 | 0:50ff23c02d57 | 407 | } |
infotech1 | 0:50ff23c02d57 | 408 | } |
infotech1 | 0:50ff23c02d57 | 409 | } |
infotech1 | 0:50ff23c02d57 | 410 | } |
infotech1 | 0:50ff23c02d57 | 411 | |
infotech1 | 0:50ff23c02d57 | 412 | |
infotech1 | 0:50ff23c02d57 | 413 | uint8_t Adafruit_GFX::write(uint8_t c) { |
infotech1 | 0:50ff23c02d57 | 414 | |
infotech1 | 0:50ff23c02d57 | 415 | if (c == '\n') { |
infotech1 | 0:50ff23c02d57 | 416 | cursor_y += textsize*8; |
infotech1 | 0:50ff23c02d57 | 417 | cursor_x = 0; |
infotech1 | 0:50ff23c02d57 | 418 | } else if (c == '\r') { |
infotech1 | 0:50ff23c02d57 | 419 | // skip em |
infotech1 | 0:50ff23c02d57 | 420 | } else { |
infotech1 | 0:50ff23c02d57 | 421 | drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize); |
infotech1 | 0:50ff23c02d57 | 422 | cursor_x += textsize*6; |
infotech1 | 0:50ff23c02d57 | 423 | if (wrap && (cursor_x > (_width - textsize*6))) { |
infotech1 | 0:50ff23c02d57 | 424 | cursor_y += textsize*8; |
infotech1 | 0:50ff23c02d57 | 425 | cursor_x = 0; |
infotech1 | 0:50ff23c02d57 | 426 | } |
infotech1 | 0:50ff23c02d57 | 427 | } |
infotech1 | 0:50ff23c02d57 | 428 | |
infotech1 | 0:50ff23c02d57 | 429 | return 1; |
infotech1 | 0:50ff23c02d57 | 430 | |
infotech1 | 0:50ff23c02d57 | 431 | } |
infotech1 | 0:50ff23c02d57 | 432 | |
infotech1 | 0:50ff23c02d57 | 433 | // Draw a character |
infotech1 | 0:50ff23c02d57 | 434 | void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c, |
infotech1 | 0:50ff23c02d57 | 435 | uint16_t color, uint16_t bg, uint8_t size) { |
infotech1 | 0:50ff23c02d57 | 436 | |
infotech1 | 0:50ff23c02d57 | 437 | if((x >= _width) || // Clip right |
infotech1 | 0:50ff23c02d57 | 438 | (y >= _height) || // Clip bottom |
infotech1 | 0:50ff23c02d57 | 439 | ((x + 6 * size - 1) < 0) || // Clip left |
infotech1 | 0:50ff23c02d57 | 440 | ((y + 8 * size - 1) < 0)) // Clip top |
infotech1 | 0:50ff23c02d57 | 441 | return; |
infotech1 | 0:50ff23c02d57 | 442 | |
infotech1 | 0:50ff23c02d57 | 443 | for (int8_t i=0; i<6; i++ ) { |
infotech1 | 0:50ff23c02d57 | 444 | uint8_t line; |
infotech1 | 0:50ff23c02d57 | 445 | if (i == 5) |
infotech1 | 0:50ff23c02d57 | 446 | line = 0x0; |
infotech1 | 0:50ff23c02d57 | 447 | else |
infotech1 | 0:50ff23c02d57 | 448 | line = pgm_read_byte(font+(c*5)+i); |
infotech1 | 0:50ff23c02d57 | 449 | for (int8_t j = 0; j<8; j++) { |
infotech1 | 0:50ff23c02d57 | 450 | if (line & 0x1) { |
infotech1 | 0:50ff23c02d57 | 451 | if (size == 1) // default size |
infotech1 | 0:50ff23c02d57 | 452 | drawPixel(x+i, y+j, color); |
infotech1 | 0:50ff23c02d57 | 453 | else { // big size |
infotech1 | 0:50ff23c02d57 | 454 | fillRect(x+(i*size), y+(j*size), size, size, color); |
infotech1 | 0:50ff23c02d57 | 455 | } |
infotech1 | 0:50ff23c02d57 | 456 | } else if (bg != color) { |
infotech1 | 0:50ff23c02d57 | 457 | if (size == 1) // default size |
infotech1 | 0:50ff23c02d57 | 458 | drawPixel(x+i, y+j, bg); |
infotech1 | 0:50ff23c02d57 | 459 | else { // big size |
infotech1 | 0:50ff23c02d57 | 460 | fillRect(x+i*size, y+j*size, size, size, bg); |
infotech1 | 0:50ff23c02d57 | 461 | } |
infotech1 | 0:50ff23c02d57 | 462 | } |
infotech1 | 0:50ff23c02d57 | 463 | line >>= 1; |
infotech1 | 0:50ff23c02d57 | 464 | } |
infotech1 | 0:50ff23c02d57 | 465 | } |
infotech1 | 0:50ff23c02d57 | 466 | } |
infotech1 | 0:50ff23c02d57 | 467 | |
infotech1 | 0:50ff23c02d57 | 468 | void Adafruit_GFX::setCursor(int16_t x, int16_t y) { |
infotech1 | 0:50ff23c02d57 | 469 | cursor_x = x; |
infotech1 | 0:50ff23c02d57 | 470 | cursor_y = y; |
infotech1 | 0:50ff23c02d57 | 471 | } |
infotech1 | 0:50ff23c02d57 | 472 | |
infotech1 | 0:50ff23c02d57 | 473 | void Adafruit_GFX::setTextSize(uint8_t s) { |
infotech1 | 0:50ff23c02d57 | 474 | textsize = (s > 0) ? s : 1; |
infotech1 | 0:50ff23c02d57 | 475 | } |
infotech1 | 0:50ff23c02d57 | 476 | |
infotech1 | 0:50ff23c02d57 | 477 | void Adafruit_GFX::setTextColor(uint16_t c) { |
infotech1 | 0:50ff23c02d57 | 478 | // For 'transparent' background, we'll set the bg |
infotech1 | 0:50ff23c02d57 | 479 | // to the same as fg instead of using a flag |
infotech1 | 0:50ff23c02d57 | 480 | textcolor = textbgcolor = c; |
infotech1 | 0:50ff23c02d57 | 481 | } |
infotech1 | 0:50ff23c02d57 | 482 | |
infotech1 | 0:50ff23c02d57 | 483 | void Adafruit_GFX::setTextColor(uint16_t c, uint16_t b) { |
infotech1 | 0:50ff23c02d57 | 484 | textcolor = c; |
infotech1 | 0:50ff23c02d57 | 485 | textbgcolor = b; |
infotech1 | 0:50ff23c02d57 | 486 | } |
infotech1 | 0:50ff23c02d57 | 487 | |
infotech1 | 0:50ff23c02d57 | 488 | void Adafruit_GFX::setTextWrap(bool w) { |
infotech1 | 0:50ff23c02d57 | 489 | wrap = w; |
infotech1 | 0:50ff23c02d57 | 490 | } |
infotech1 | 0:50ff23c02d57 | 491 | |
infotech1 | 0:50ff23c02d57 | 492 | uint8_t Adafruit_GFX::getRotation(void) const { |
infotech1 | 0:50ff23c02d57 | 493 | return rotation; |
infotech1 | 0:50ff23c02d57 | 494 | } |
infotech1 | 0:50ff23c02d57 | 495 | |
infotech1 | 0:50ff23c02d57 | 496 | void Adafruit_GFX::setRotation(uint8_t x) { |
infotech1 | 0:50ff23c02d57 | 497 | rotation = (x & 3); |
infotech1 | 0:50ff23c02d57 | 498 | switch(rotation) { |
infotech1 | 0:50ff23c02d57 | 499 | case 0: |
infotech1 | 0:50ff23c02d57 | 500 | case 2: |
infotech1 | 0:50ff23c02d57 | 501 | _width = WIDTH; |
infotech1 | 0:50ff23c02d57 | 502 | _height = HEIGHT; |
infotech1 | 0:50ff23c02d57 | 503 | break; |
infotech1 | 0:50ff23c02d57 | 504 | case 1: |
infotech1 | 0:50ff23c02d57 | 505 | case 3: |
infotech1 | 0:50ff23c02d57 | 506 | _width = HEIGHT; |
infotech1 | 0:50ff23c02d57 | 507 | _height = WIDTH; |
infotech1 | 0:50ff23c02d57 | 508 | break; |
infotech1 | 0:50ff23c02d57 | 509 | } |
infotech1 | 0:50ff23c02d57 | 510 | } |
infotech1 | 0:50ff23c02d57 | 511 | |
infotech1 | 0:50ff23c02d57 | 512 | // Return the size of the display (per current rotation) |
infotech1 | 0:50ff23c02d57 | 513 | int16_t Adafruit_GFX::width(void) const { |
infotech1 | 0:50ff23c02d57 | 514 | return _width; |
infotech1 | 0:50ff23c02d57 | 515 | } |
infotech1 | 0:50ff23c02d57 | 516 | |
infotech1 | 0:50ff23c02d57 | 517 | int16_t Adafruit_GFX::height(void) const { |
infotech1 | 0:50ff23c02d57 | 518 | return _height; |
infotech1 | 0:50ff23c02d57 | 519 | } |
infotech1 | 0:50ff23c02d57 | 520 | |
infotech1 | 0:50ff23c02d57 | 521 | void Adafruit_GFX::invertDisplay(bool i) { |
infotech1 | 0:50ff23c02d57 | 522 | // Do nothing, must be subclassed if supported |
infotech1 | 0:50ff23c02d57 | 523 | } |
infotech1 | 0:50ff23c02d57 | 524 | |
infotech1 | 0:50ff23c02d57 | 525 | |
infotech1 | 0:50ff23c02d57 | 526 | //Methods from Print.cpp Arduino |
infotech1 | 0:50ff23c02d57 | 527 | |
infotech1 | 0:50ff23c02d57 | 528 | uint8_t Adafruit_GFX::write(const uint8_t *buffer, uint8_t size) |
infotech1 | 0:50ff23c02d57 | 529 | { |
infotech1 | 0:50ff23c02d57 | 530 | uint8_t n = 0; |
infotech1 | 0:50ff23c02d57 | 531 | while (size--) { |
infotech1 | 0:50ff23c02d57 | 532 | n += write(*buffer++); |
infotech1 | 0:50ff23c02d57 | 533 | } |
infotech1 | 0:50ff23c02d57 | 534 | return n; |
infotech1 | 0:50ff23c02d57 | 535 | } |
infotech1 | 0:50ff23c02d57 | 536 | |
infotech1 | 0:50ff23c02d57 | 537 | uint8_t Adafruit_GFX::print(const char str[]) |
infotech1 | 0:50ff23c02d57 | 538 | { |
infotech1 | 0:50ff23c02d57 | 539 | return write(str); |
infotech1 | 0:50ff23c02d57 | 540 | } |
infotech1 | 0:50ff23c02d57 | 541 | |
infotech1 | 0:50ff23c02d57 | 542 | uint8_t Adafruit_GFX::print(char c) |
infotech1 | 0:50ff23c02d57 | 543 | { |
infotech1 | 0:50ff23c02d57 | 544 | return write(c); |
infotech1 | 0:50ff23c02d57 | 545 | } |
infotech1 | 0:50ff23c02d57 | 546 | |
infotech1 | 0:50ff23c02d57 | 547 | uint8_t Adafruit_GFX::print(unsigned char b, int base) |
infotech1 | 0:50ff23c02d57 | 548 | { |
infotech1 | 0:50ff23c02d57 | 549 | return print((unsigned long) b, base); |
infotech1 | 0:50ff23c02d57 | 550 | } |
infotech1 | 0:50ff23c02d57 | 551 | |
infotech1 | 0:50ff23c02d57 | 552 | uint8_t Adafruit_GFX::print(int n, int base) |
infotech1 | 0:50ff23c02d57 | 553 | { |
infotech1 | 0:50ff23c02d57 | 554 | return print((long) n, base); |
infotech1 | 0:50ff23c02d57 | 555 | } |
infotech1 | 0:50ff23c02d57 | 556 | |
infotech1 | 0:50ff23c02d57 | 557 | uint8_t Adafruit_GFX::print(unsigned int n, int base) |
infotech1 | 0:50ff23c02d57 | 558 | { |
infotech1 | 0:50ff23c02d57 | 559 | return print((unsigned long) n, base); |
infotech1 | 0:50ff23c02d57 | 560 | } |
infotech1 | 0:50ff23c02d57 | 561 | |
infotech1 | 0:50ff23c02d57 | 562 | uint8_t Adafruit_GFX::print(long n, int base) |
infotech1 | 0:50ff23c02d57 | 563 | { |
infotech1 | 0:50ff23c02d57 | 564 | if (base == 0) { |
infotech1 | 0:50ff23c02d57 | 565 | return write(n); |
infotech1 | 0:50ff23c02d57 | 566 | } else if (base == 10) { |
infotech1 | 0:50ff23c02d57 | 567 | if (n < 0) { |
infotech1 | 0:50ff23c02d57 | 568 | int t = print('-'); |
infotech1 | 0:50ff23c02d57 | 569 | n = -n; |
infotech1 | 0:50ff23c02d57 | 570 | return printNumber(n, 10) + t; |
infotech1 | 0:50ff23c02d57 | 571 | } |
infotech1 | 0:50ff23c02d57 | 572 | return printNumber(n, 10); |
infotech1 | 0:50ff23c02d57 | 573 | } else { |
infotech1 | 0:50ff23c02d57 | 574 | return printNumber(n, base); |
infotech1 | 0:50ff23c02d57 | 575 | } |
infotech1 | 0:50ff23c02d57 | 576 | } |
infotech1 | 0:50ff23c02d57 | 577 | |
infotech1 | 0:50ff23c02d57 | 578 | uint8_t Adafruit_GFX::print(unsigned long n, int base) |
infotech1 | 0:50ff23c02d57 | 579 | { |
infotech1 | 0:50ff23c02d57 | 580 | if (base == 0) return write(n); |
infotech1 | 0:50ff23c02d57 | 581 | else return printNumber(n, base); |
infotech1 | 0:50ff23c02d57 | 582 | } |
infotech1 | 0:50ff23c02d57 | 583 | |
infotech1 | 0:50ff23c02d57 | 584 | uint8_t Adafruit_GFX::print(double n, int digits) |
infotech1 | 0:50ff23c02d57 | 585 | { |
infotech1 | 0:50ff23c02d57 | 586 | return printFloat(n, digits); |
infotech1 | 0:50ff23c02d57 | 587 | } |
infotech1 | 0:50ff23c02d57 | 588 | |
infotech1 | 0:50ff23c02d57 | 589 | |
infotech1 | 0:50ff23c02d57 | 590 | |
infotech1 | 0:50ff23c02d57 | 591 | |
infotech1 | 0:50ff23c02d57 | 592 | |
infotech1 | 0:50ff23c02d57 | 593 | uint8_t Adafruit_GFX::println(void) |
infotech1 | 0:50ff23c02d57 | 594 | { |
infotech1 | 0:50ff23c02d57 | 595 | size_t n = print('\r'); |
infotech1 | 0:50ff23c02d57 | 596 | n += print('\n'); |
infotech1 | 0:50ff23c02d57 | 597 | return n; |
infotech1 | 0:50ff23c02d57 | 598 | } |
infotech1 | 0:50ff23c02d57 | 599 | |
infotech1 | 0:50ff23c02d57 | 600 | |
infotech1 | 0:50ff23c02d57 | 601 | |
infotech1 | 0:50ff23c02d57 | 602 | uint8_t Adafruit_GFX::println(const char c[]) |
infotech1 | 0:50ff23c02d57 | 603 | { |
infotech1 | 0:50ff23c02d57 | 604 | size_t n = print(c); |
infotech1 | 0:50ff23c02d57 | 605 | n += println(); |
infotech1 | 0:50ff23c02d57 | 606 | return n; |
infotech1 | 0:50ff23c02d57 | 607 | } |
infotech1 | 0:50ff23c02d57 | 608 | |
infotech1 | 0:50ff23c02d57 | 609 | uint8_t Adafruit_GFX::println(char c) |
infotech1 | 0:50ff23c02d57 | 610 | { |
infotech1 | 0:50ff23c02d57 | 611 | size_t n = print(c); |
infotech1 | 0:50ff23c02d57 | 612 | n += println(); |
infotech1 | 0:50ff23c02d57 | 613 | return n; |
infotech1 | 0:50ff23c02d57 | 614 | } |
infotech1 | 0:50ff23c02d57 | 615 | |
infotech1 | 0:50ff23c02d57 | 616 | uint8_t Adafruit_GFX::println(unsigned char b, int numBase) |
infotech1 | 0:50ff23c02d57 | 617 | { |
infotech1 | 0:50ff23c02d57 | 618 | size_t n = print(b, numBase); |
infotech1 | 0:50ff23c02d57 | 619 | n += println(); |
infotech1 | 0:50ff23c02d57 | 620 | return n; |
infotech1 | 0:50ff23c02d57 | 621 | } |
infotech1 | 0:50ff23c02d57 | 622 | |
infotech1 | 0:50ff23c02d57 | 623 | uint8_t Adafruit_GFX::println(int num, int base) |
infotech1 | 0:50ff23c02d57 | 624 | { |
infotech1 | 0:50ff23c02d57 | 625 | size_t n = print(num, base); |
infotech1 | 0:50ff23c02d57 | 626 | n += println(); |
infotech1 | 0:50ff23c02d57 | 627 | return n; |
infotech1 | 0:50ff23c02d57 | 628 | } |
infotech1 | 0:50ff23c02d57 | 629 | |
infotech1 | 0:50ff23c02d57 | 630 | uint8_t Adafruit_GFX::println(unsigned int num, int base) |
infotech1 | 0:50ff23c02d57 | 631 | { |
infotech1 | 0:50ff23c02d57 | 632 | size_t n = print(num, base); |
infotech1 | 0:50ff23c02d57 | 633 | n += println(); |
infotech1 | 0:50ff23c02d57 | 634 | return n; |
infotech1 | 0:50ff23c02d57 | 635 | } |
infotech1 | 0:50ff23c02d57 | 636 | |
infotech1 | 0:50ff23c02d57 | 637 | uint8_t Adafruit_GFX::println(long num, int base) |
infotech1 | 0:50ff23c02d57 | 638 | { |
infotech1 | 0:50ff23c02d57 | 639 | size_t n = print(num, base); |
infotech1 | 0:50ff23c02d57 | 640 | n += println(); |
infotech1 | 0:50ff23c02d57 | 641 | return n; |
infotech1 | 0:50ff23c02d57 | 642 | } |
infotech1 | 0:50ff23c02d57 | 643 | |
infotech1 | 0:50ff23c02d57 | 644 | uint8_t Adafruit_GFX::println(unsigned long num, int base) |
infotech1 | 0:50ff23c02d57 | 645 | { |
infotech1 | 0:50ff23c02d57 | 646 | size_t n = print(num, base); |
infotech1 | 0:50ff23c02d57 | 647 | n += println(); |
infotech1 | 0:50ff23c02d57 | 648 | return n; |
infotech1 | 0:50ff23c02d57 | 649 | } |
infotech1 | 0:50ff23c02d57 | 650 | |
infotech1 | 0:50ff23c02d57 | 651 | uint8_t Adafruit_GFX::println(double num, int digits) |
infotech1 | 0:50ff23c02d57 | 652 | { |
infotech1 | 0:50ff23c02d57 | 653 | size_t n = print(num, digits); |
infotech1 | 0:50ff23c02d57 | 654 | n += println(); |
infotech1 | 0:50ff23c02d57 | 655 | return n; |
infotech1 | 0:50ff23c02d57 | 656 | } |
infotech1 | 0:50ff23c02d57 | 657 | |
infotech1 | 0:50ff23c02d57 | 658 | |
infotech1 | 0:50ff23c02d57 | 659 | |
infotech1 | 0:50ff23c02d57 | 660 | // Private Methods ///////////////////////////////////////////////////////////// |
infotech1 | 0:50ff23c02d57 | 661 | |
infotech1 | 0:50ff23c02d57 | 662 | uint8_t Adafruit_GFX::printNumber(unsigned long n, uint8_t base) { |
infotech1 | 0:50ff23c02d57 | 663 | char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte. |
infotech1 | 0:50ff23c02d57 | 664 | char *str = &buf[sizeof(buf) - 1]; |
infotech1 | 0:50ff23c02d57 | 665 | |
infotech1 | 0:50ff23c02d57 | 666 | *str = '\0'; |
infotech1 | 0:50ff23c02d57 | 667 | |
infotech1 | 0:50ff23c02d57 | 668 | // prevent crash if called with base == 1 |
infotech1 | 0:50ff23c02d57 | 669 | if (base < 2) base = 10; |
infotech1 | 0:50ff23c02d57 | 670 | |
infotech1 | 0:50ff23c02d57 | 671 | do { |
infotech1 | 0:50ff23c02d57 | 672 | unsigned long m = n; |
infotech1 | 0:50ff23c02d57 | 673 | n /= base; |
infotech1 | 0:50ff23c02d57 | 674 | char c = m - base * n; |
infotech1 | 0:50ff23c02d57 | 675 | *--str = c < 10 ? c + '0' : c + 'A' - 10; |
infotech1 | 0:50ff23c02d57 | 676 | } while(n); |
infotech1 | 0:50ff23c02d57 | 677 | |
infotech1 | 0:50ff23c02d57 | 678 | return write(str); |
infotech1 | 0:50ff23c02d57 | 679 | } |
infotech1 | 0:50ff23c02d57 | 680 | |
infotech1 | 0:50ff23c02d57 | 681 | uint8_t Adafruit_GFX::printFloat(double number, uint8_t digits) |
infotech1 | 0:50ff23c02d57 | 682 | { |
infotech1 | 0:50ff23c02d57 | 683 | uint8_t n = 0; |
infotech1 | 0:50ff23c02d57 | 684 | |
infotech1 | 0:50ff23c02d57 | 685 | if (isnan(number)) return print("nan"); |
infotech1 | 0:50ff23c02d57 | 686 | if (isinf(number)) return print("inf"); |
infotech1 | 0:50ff23c02d57 | 687 | if (number > 4294967040.0) return print ("ovf"); // constant determined empirically |
infotech1 | 0:50ff23c02d57 | 688 | if (number <-4294967040.0) return print ("ovf"); // constant determined empirically |
infotech1 | 0:50ff23c02d57 | 689 | |
infotech1 | 0:50ff23c02d57 | 690 | // Handle negative numbers |
infotech1 | 0:50ff23c02d57 | 691 | if (number < 0.0) |
infotech1 | 0:50ff23c02d57 | 692 | { |
infotech1 | 0:50ff23c02d57 | 693 | n += print('-'); |
infotech1 | 0:50ff23c02d57 | 694 | number = -number; |
infotech1 | 0:50ff23c02d57 | 695 | } |
infotech1 | 0:50ff23c02d57 | 696 | |
infotech1 | 0:50ff23c02d57 | 697 | // Round correctly so that print(1.999, 2) prints as "2.00" |
infotech1 | 0:50ff23c02d57 | 698 | double rounding = 0.5; |
infotech1 | 0:50ff23c02d57 | 699 | for (uint8_t i=0; i<digits; ++i) |
infotech1 | 0:50ff23c02d57 | 700 | rounding /= 10.0; |
infotech1 | 0:50ff23c02d57 | 701 | |
infotech1 | 0:50ff23c02d57 | 702 | number += rounding; |
infotech1 | 0:50ff23c02d57 | 703 | |
infotech1 | 0:50ff23c02d57 | 704 | // Extract the integer part of the number and print it |
infotech1 | 0:50ff23c02d57 | 705 | unsigned long int_part = (unsigned long)number; |
infotech1 | 0:50ff23c02d57 | 706 | double remainder = number - (double)int_part; |
infotech1 | 0:50ff23c02d57 | 707 | n += print(int_part); |
infotech1 | 0:50ff23c02d57 | 708 | |
infotech1 | 0:50ff23c02d57 | 709 | // Print the decimal point, but only if there are digits beyond |
infotech1 | 0:50ff23c02d57 | 710 | if (digits > 0) { |
infotech1 | 0:50ff23c02d57 | 711 | n += print("."); |
infotech1 | 0:50ff23c02d57 | 712 | } |
infotech1 | 0:50ff23c02d57 | 713 | |
infotech1 | 0:50ff23c02d57 | 714 | // Extract digits from the remainder one at a time |
infotech1 | 0:50ff23c02d57 | 715 | while (digits-- > 0) |
infotech1 | 0:50ff23c02d57 | 716 | { |
infotech1 | 0:50ff23c02d57 | 717 | remainder *= 10.0; |
infotech1 | 0:50ff23c02d57 | 718 | int toPrint = int(remainder); |
infotech1 | 0:50ff23c02d57 | 719 | n += print(toPrint); |
infotech1 | 0:50ff23c02d57 | 720 | remainder -= toPrint; |
infotech1 | 0:50ff23c02d57 | 721 | } |
infotech1 | 0:50ff23c02d57 | 722 | |
infotech1 | 0:50ff23c02d57 | 723 | return n; |
infotech1 | 0:50ff23c02d57 | 724 | } |
infotech1 | 0:50ff23c02d57 | 725 | |
infotech1 | 0:50ff23c02d57 | 726 |