ILI9340 Library based on the Arduino version from Adafruit. It has been tested with a custom STM32F103C8 board.

Porting of the ILI9340 Library from Adafruit. It has been tested on a custom board based on a STM32F103C8 microcontroller.

Please, see the Wiki page on how to use the library

Committer:
gmoralis
Date:
Wed Dec 17 07:57:28 2014 +0000
Revision:
2:effcedd42f1b
Parent:
0:0bf2453a67ba
Added glcdfont.c which I forgot to include in the initial release

Who changed what in which revision?

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