Please run it on your NUCLEO-L152

Dependencies:   mbed

Committer:
davidprentice
Date:
Wed Sep 18 10:38:19 2019 +0000
Revision:
1:d88d2ad55fac
Parent:
0:b608c7f02f80
Added messages to Serial Terminal (9600 baud)

Who changed what in which revision?

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