Adafruit_GFX.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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(w), HEIGHT(h)
  37. {
  38. _width = WIDTH;
  39. _height = HEIGHT;
  40. rotation = 0;
  41. cursor_y = cursor_x = 0;
  42. textsize = 1;
  43. textcolor = textbgcolor = 0xFFFF;
  44. wrap = true;
  45. }
  46. // Draw a circle outline
  47. void Adafruit_GFX::drawCircle(int16_t x0, int16_t y0, int16_t r,
  48. uint16_t color) {
  49. int16_t f = 1 - r;
  50. int16_t ddF_x = 1;
  51. int16_t ddF_y = -2 * r;
  52. int16_t x = 0;
  53. int16_t y = r;
  54. drawPixel(x0 , y0+r, color);
  55. drawPixel(x0 , y0-r, color);
  56. drawPixel(x0+r, y0 , color);
  57. drawPixel(x0-r, y0 , color);
  58. while (x<y) {
  59. if (f >= 0) {
  60. y--;
  61. ddF_y += 2;
  62. f += ddF_y;
  63. }
  64. x++;
  65. ddF_x += 2;
  66. f += ddF_x;
  67. drawPixel(x0 + x, y0 + y, color);
  68. drawPixel(x0 - x, y0 + y, color);
  69. drawPixel(x0 + x, y0 - y, color);
  70. drawPixel(x0 - x, y0 - y, color);
  71. drawPixel(x0 + y, y0 + x, color);
  72. drawPixel(x0 - y, y0 + x, color);
  73. drawPixel(x0 + y, y0 - x, color);
  74. drawPixel(x0 - y, y0 - x, color);
  75. }
  76. }
  77. void Adafruit_GFX::drawCircleHelper( int16_t x0, int16_t y0,
  78. int16_t r, uint8_t cornername, uint16_t color) {
  79. int16_t f = 1 - r;
  80. int16_t ddF_x = 1;
  81. int16_t ddF_y = -2 * r;
  82. int16_t x = 0;
  83. int16_t y = r;
  84. while (x<y) {
  85. if (f >= 0) {
  86. y--;
  87. ddF_y += 2;
  88. f += ddF_y;
  89. }
  90. x++;
  91. ddF_x += 2;
  92. f += ddF_x;
  93. if (cornername & 0x4) {
  94. drawPixel(x0 + x, y0 + y, color);
  95. drawPixel(x0 + y, y0 + x, color);
  96. }
  97. if (cornername & 0x2) {
  98. drawPixel(x0 + x, y0 - y, color);
  99. drawPixel(x0 + y, y0 - x, color);
  100. }
  101. if (cornername & 0x8) {
  102. drawPixel(x0 - y, y0 + x, color);
  103. drawPixel(x0 - x, y0 + y, color);
  104. }
  105. if (cornername & 0x1) {
  106. drawPixel(x0 - y, y0 - x, color);
  107. drawPixel(x0 - x, y0 - y, color);
  108. }
  109. }
  110. }
  111. void Adafruit_GFX::fillCircle(int16_t x0, int16_t y0, int16_t r,
  112. uint16_t color) {
  113. drawFastVLine(x0, y0-r, 2*r+1, color);
  114. fillCircleHelper(x0, y0, r, 3, 0, color);
  115. }
  116. // Used to do circles and roundrects
  117. void Adafruit_GFX::fillCircleHelper(int16_t x0, int16_t y0, int16_t r,
  118. uint8_t cornername, int16_t delta, uint16_t color) {
  119. int16_t f = 1 - r;
  120. int16_t ddF_x = 1;
  121. int16_t ddF_y = -2 * r;
  122. int16_t x = 0;
  123. int16_t y = r;
  124. while (x<y) {
  125. if (f >= 0) {
  126. y--;
  127. ddF_y += 2;
  128. f += ddF_y;
  129. }
  130. x++;
  131. ddF_x += 2;
  132. f += ddF_x;
  133. if (cornername & 0x1) {
  134. drawFastVLine(x0+x, y0-y, 2*y+1+delta, color);
  135. drawFastVLine(x0+y, y0-x, 2*x+1+delta, color);
  136. }
  137. if (cornername & 0x2) {
  138. drawFastVLine(x0-x, y0-y, 2*y+1+delta, color);
  139. drawFastVLine(x0-y, y0-x, 2*x+1+delta, color);
  140. }
  141. }
  142. }
  143. // Bresenham's algorithm - thx wikpedia
  144. void Adafruit_GFX::drawLine(int16_t x0, int16_t y0,
  145. int16_t x1, int16_t y1,
  146. uint16_t color) {
  147. int16_t steep = abs(y1 - y0) > abs(x1 - x0);
  148. if (steep) {
  149. swap(x0, y0);
  150. swap(x1, y1);
  151. }
  152. if (x0 > x1) {
  153. swap(x0, x1);
  154. swap(y0, y1);
  155. }
  156. int16_t dx, dy;
  157. dx = x1 - x0;
  158. dy = abs(y1 - y0);
  159. int16_t err = dx / 2;
  160. int16_t ystep;
  161. if (y0 < y1) {
  162. ystep = 1;
  163. } else {
  164. ystep = -1;
  165. }
  166. for (; x0<=x1; x0++) {
  167. if (steep) {
  168. drawPixel(y0, x0, color);
  169. } else {
  170. drawPixel(x0, y0, color);
  171. }
  172. err -= dy;
  173. if (err < 0) {
  174. y0 += ystep;
  175. err += dx;
  176. }
  177. }
  178. }
  179. // Draw a rectangle
  180. void Adafruit_GFX::drawRect(int16_t x, int16_t y,
  181. int16_t w, int16_t h,
  182. uint16_t color) {
  183. drawFastHLine(x, y, w, color);
  184. drawFastHLine(x, y+h-1, w, color);
  185. drawFastVLine(x, y, h, color);
  186. drawFastVLine(x+w-1, y, h, color);
  187. }
  188. void Adafruit_GFX::drawFastVLine(int16_t x, int16_t y,
  189. int16_t h, uint16_t color) {
  190. // Update in subclasses if desired!
  191. drawLine(x, y, x, y+h-1, color);
  192. }
  193. void Adafruit_GFX::drawFastHLine(int16_t x, int16_t y,
  194. int16_t w, uint16_t color) {
  195. // Update in subclasses if desired!
  196. drawLine(x, y, x+w-1, y, color);
  197. }
  198. void Adafruit_GFX::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
  199. uint16_t color) {
  200. // Update in subclasses if desired!
  201. for (int16_t i=x; i<x+w; i++) {
  202. drawFastVLine(i, y, h, color);
  203. }
  204. }
  205. void Adafruit_GFX::fillScreen(uint16_t color) {
  206. fillRect(0, 0, _width, _height, color);
  207. }
  208. // Draw a rounded rectangle
  209. void Adafruit_GFX::drawRoundRect(int16_t x, int16_t y, int16_t w,
  210. int16_t h, int16_t r, uint16_t color) {
  211. // smarter version
  212. drawFastHLine(x+r , y , w-2*r, color); // Top
  213. drawFastHLine(x+r , y+h-1, w-2*r, color); // Bottom
  214. drawFastVLine(x , y+r , h-2*r, color); // Left
  215. drawFastVLine(x+w-1, y+r , h-2*r, color); // Right
  216. // draw four corners
  217. drawCircleHelper(x+r , y+r , r, 1, color);
  218. drawCircleHelper(x+w-r-1, y+r , r, 2, color);
  219. drawCircleHelper(x+w-r-1, y+h-r-1, r, 4, color);
  220. drawCircleHelper(x+r , y+h-r-1, r, 8, color);
  221. }
  222. // Fill a rounded rectangle
  223. void Adafruit_GFX::fillRoundRect(int16_t x, int16_t y, int16_t w,
  224. int16_t h, int16_t r, uint16_t color) {
  225. // smarter version
  226. fillRect(x+r, y, w-2*r, h, color);
  227. // draw four corners
  228. fillCircleHelper(x+w-r-1, y+r, r, 1, h-2*r-1, color);
  229. fillCircleHelper(x+r , y+r, r, 2, h-2*r-1, color);
  230. }
  231. // Draw a triangle
  232. void Adafruit_GFX::drawTriangle(int16_t x0, int16_t y0,
  233. int16_t x1, int16_t y1,
  234. int16_t x2, int16_t y2, uint16_t color) {
  235. drawLine(x0, y0, x1, y1, color);
  236. drawLine(x1, y1, x2, y2, color);
  237. drawLine(x2, y2, x0, y0, color);
  238. }
  239. // Fill a triangle
  240. void Adafruit_GFX::fillTriangle ( int16_t x0, int16_t y0,
  241. int16_t x1, int16_t y1,
  242. int16_t x2, int16_t y2, uint16_t color) {
  243. int16_t a, b, y, last;
  244. // Sort coordinates by Y order (y2 >= y1 >= y0)
  245. if (y0 > y1) {
  246. swap(y0, y1); swap(x0, x1);
  247. }
  248. if (y1 > y2) {
  249. swap(y2, y1); swap(x2, x1);
  250. }
  251. if (y0 > y1) {
  252. swap(y0, y1); swap(x0, x1);
  253. }
  254. if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing
  255. a = b = x0;
  256. if(x1 < a) a = x1;
  257. else if(x1 > b) b = x1;
  258. if(x2 < a) a = x2;
  259. else if(x2 > b) b = x2;
  260. drawFastHLine(a, y0, b-a+1, color);
  261. return;
  262. }
  263. int16_t
  264. dx01 = x1 - x0,
  265. dy01 = y1 - y0,
  266. dx02 = x2 - x0,
  267. dy02 = y2 - y0,
  268. dx12 = x2 - x1,
  269. dy12 = y2 - y1,
  270. sa = 0,
  271. sb = 0;
  272. // For upper part of triangle, find scanline crossings for segments
  273. // 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1
  274. // is included here (and second loop will be skipped, avoiding a /0
  275. // error there), otherwise scanline y1 is skipped here and handled
  276. // in the second loop...which also avoids a /0 error here if y0=y1
  277. // (flat-topped triangle).
  278. if(y1 == y2) last = y1; // Include y1 scanline
  279. else last = y1-1; // Skip it
  280. for(y=y0; y<=last; y++) {
  281. a = x0 + sa / dy01;
  282. b = x0 + sb / dy02;
  283. sa += dx01;
  284. sb += dx02;
  285. /* longhand:
  286. a = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
  287. b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
  288. */
  289. if(a > b) swap(a,b);
  290. drawFastHLine(a, y, b-a+1, color);
  291. }
  292. // For lower part of triangle, find scanline crossings for segments
  293. // 0-2 and 1-2. This loop is skipped if y1=y2.
  294. sa = dx12 * (y - y1);
  295. sb = dx02 * (y - y0);
  296. for(; y<=y2; y++) {
  297. a = x1 + sa / dy12;
  298. b = x0 + sb / dy02;
  299. sa += dx12;
  300. sb += dx02;
  301. /* longhand:
  302. a = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
  303. b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
  304. */
  305. if(a > b) swap(a,b);
  306. drawFastHLine(a, y, b-a+1, color);
  307. }
  308. }
  309. void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
  310. const uint8_t *bitmap, int16_t w, int16_t h,
  311. uint16_t color) {
  312. int16_t i, j, byteWidth = (w + 7) / 8;
  313. for(j=0; j<h; j++) {
  314. for(i=0; i<w; i++ ) {
  315. if(pgm_read_byte(bitmap + j * byteWidth + i / 8) & (128 >> (i & 7))) {
  316. drawPixel(x+i, y+j, color);
  317. }
  318. }
  319. }
  320. }
  321. #if ARDUINO >= 100
  322. size_t Adafruit_GFX::write(uint8_t c) {
  323. #else
  324. void Adafruit_GFX::write(uint8_t c) {
  325. #endif
  326. if (c == '\n') {
  327. cursor_y += textsize*8;
  328. cursor_x = 0;
  329. } else if (c == '\r') {
  330. // skip em
  331. } else {
  332. drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
  333. cursor_x += textsize*6;
  334. if (wrap && (cursor_x > (_width - textsize*6))) {
  335. cursor_y += textsize*8;
  336. cursor_x = 0;
  337. }
  338. }
  339. #if ARDUINO >= 100
  340. return 1;
  341. #endif
  342. }
  343. // Draw a character
  344. void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
  345. uint16_t color, uint16_t bg, uint8_t size) {
  346. if((x >= _width) || // Clip right
  347. (y >= _height) || // Clip bottom
  348. ((x + 6 * size - 1) < 0) || // Clip left
  349. ((y + 8 * size - 1) < 0)) // Clip top
  350. return;
  351. for (int8_t i=0; i<6; i++ ) {
  352. uint8_t line;
  353. if (i == 5)
  354. line = 0x0;
  355. else
  356. line = pgm_read_byte(font+(c*5)+i);
  357. for (int8_t j = 0; j<8; j++) {
  358. if (line & 0x1) {
  359. if (size == 1) // default size
  360. drawPixel(x+i, y+j, color);
  361. else { // big size
  362. fillRect(x+(i*size), y+(j*size), size, size, color);
  363. }
  364. } else if (bg != color) {
  365. if (size == 1) // default size
  366. drawPixel(x+i, y+j, bg);
  367. else { // big size
  368. fillRect(x+i*size, y+j*size, size, size, bg);
  369. }
  370. }
  371. line >>= 1;
  372. }
  373. }
  374. }
  375. void Adafruit_GFX::setCursor(int16_t x, int16_t y) {
  376. cursor_x = x;
  377. cursor_y = y;
  378. }
  379. void Adafruit_GFX::setTextSize(uint8_t s) {
  380. textsize = (s > 0) ? s : 1;
  381. }
  382. void Adafruit_GFX::setTextColor(uint16_t c) {
  383. // For 'transparent' background, we'll set the bg
  384. // to the same as fg instead of using a flag
  385. textcolor = textbgcolor = c;
  386. }
  387. void Adafruit_GFX::setTextColor(uint16_t c, uint16_t b) {
  388. textcolor = c;
  389. textbgcolor = b;
  390. }
  391. void Adafruit_GFX::setTextWrap(boolean w) {
  392. wrap = w;
  393. }
  394. uint8_t Adafruit_GFX::getRotation(void) {
  395. return rotation;
  396. }
  397. void Adafruit_GFX::setRotation(uint8_t x) {
  398. rotation = (x & 3);
  399. switch(rotation) {
  400. case 0:
  401. case 2:
  402. _width = WIDTH;
  403. _height = HEIGHT;
  404. break;
  405. case 1:
  406. case 3:
  407. _width = HEIGHT;
  408. _height = WIDTH;
  409. break;
  410. }
  411. }
  412. // Return the size of the display (per current rotation)
  413. int16_t Adafruit_GFX::width(void) {
  414. return _width;
  415. }
  416. int16_t Adafruit_GFX::height(void) {
  417. return _height;
  418. }
  419. void Adafruit_GFX::invertDisplay(boolean i) {
  420. // Do nothing, must be subclassed if supported
  421. }