Adafruit_GFX.cpp 12 KB

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