Adafruit_GFX.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /***********************************
  2. This is a our graphics core library, for all our displays.
  3. We'll be adapting all the
  4. existing libaries to use this core to make updating, support
  5. and upgrading easier!
  6. Adafruit invests time and resources providing this open source code,
  7. please support Adafruit and open-source hardware by purchasing
  8. products from Adafruit!
  9. Written by Limor Fried/Ladyada for Adafruit Industries.
  10. BSD license, check license.txt for more information
  11. All text above must be included in any redistribution
  12. ****************************************/
  13. #include "Adafruit_GFX.h"
  14. #include "glcdfont.c"
  15. #include <avr/pgmspace.h>
  16. // draw a circle outline
  17. void Adafruit_GFX::drawCircle(uint16_t x0, uint16_t y0, uint16_t r,
  18. uint16_t color) {
  19. int16_t f = 1 - r;
  20. int16_t ddF_x = 1;
  21. int16_t ddF_y = -2 * r;
  22. int16_t x = 0;
  23. int16_t y = r;
  24. drawPixel(x0, y0+r, color);
  25. drawPixel(x0, y0-r, color);
  26. drawPixel(x0+r, y0, color);
  27. drawPixel(x0-r, y0, color);
  28. while (x<y) {
  29. if (f >= 0) {
  30. y--;
  31. ddF_y += 2;
  32. f += ddF_y;
  33. }
  34. x++;
  35. ddF_x += 2;
  36. f += ddF_x;
  37. drawPixel(x0 + x, y0 + y, color);
  38. drawPixel(x0 - x, y0 + y, color);
  39. drawPixel(x0 + x, y0 - y, color);
  40. drawPixel(x0 - x, y0 - y, color);
  41. drawPixel(x0 + y, y0 + x, color);
  42. drawPixel(x0 - y, y0 + x, color);
  43. drawPixel(x0 + y, y0 - x, color);
  44. drawPixel(x0 - y, y0 - x, color);
  45. }
  46. }
  47. void Adafruit_GFX::drawCircleHelper(uint16_t x0, uint16_t y0,
  48. uint16_t r, uint8_t cornername, 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. while (x<y) {
  55. if (f >= 0) {
  56. y--;
  57. ddF_y += 2;
  58. f += ddF_y;
  59. }
  60. x++;
  61. ddF_x += 2;
  62. f += ddF_x;
  63. if (cornername & 0x4) {
  64. drawPixel(x0 + x, y0 + y, color);
  65. drawPixel(x0 + y, y0 + x, color);
  66. }
  67. if (cornername & 0x2) {
  68. drawPixel(x0 + x, y0 - y, color);
  69. drawPixel(x0 + y, y0 - x, color);
  70. }
  71. if (cornername & 0x8) {
  72. drawPixel(x0 - y, y0 + x, color);
  73. drawPixel(x0 - x, y0 + y, color);
  74. }
  75. if (cornername & 0x1) {
  76. drawPixel(x0 - y, y0 - x, color);
  77. drawPixel(x0 - x, y0 - y, color);
  78. }
  79. }
  80. }
  81. void Adafruit_GFX::fillCircle(uint16_t x0, uint16_t y0, uint16_t r,
  82. uint16_t color) {
  83. drawFastVLine(x0, y0-r, 2*r+1, color);
  84. fillCircleHelper(x0, y0, r, 3, 0, color);
  85. }
  86. // used to do circles and roundrects!
  87. void Adafruit_GFX::fillCircleHelper(uint16_t x0, uint16_t y0, uint16_t r,
  88. uint8_t cornername, uint16_t delta, uint16_t color) {
  89. int16_t f = 1 - r;
  90. int16_t ddF_x = 1;
  91. int16_t ddF_y = -2 * r;
  92. int16_t x = 0;
  93. int16_t y = r;
  94. while (x<y) {
  95. if (f >= 0) {
  96. y--;
  97. ddF_y += 2;
  98. f += ddF_y;
  99. }
  100. x++;
  101. ddF_x += 2;
  102. f += ddF_x;
  103. if (cornername & 0x1) {
  104. drawFastVLine(x0+x, y0-y, 2*y+1+delta, color);
  105. drawFastVLine(x0+y, y0-x, 2*x+1+delta, color);
  106. }
  107. if (cornername & 0x2) {
  108. drawFastVLine(x0-x, y0-y, 2*y+1+delta, color);
  109. drawFastVLine(x0-y, y0-x, 2*x+1+delta, color);
  110. }
  111. }
  112. }
  113. // bresenham's algorithm - thx wikpedia
  114. void Adafruit_GFX::drawLine(int16_t x0, int16_t y0,
  115. int16_t x1, int16_t y1,
  116. uint16_t color) {
  117. int16_t steep = abs(y1 - y0) > abs(x1 - x0);
  118. if (steep) {
  119. swap(x0, y0);
  120. swap(x1, y1);
  121. }
  122. if (x0 > x1) {
  123. swap(x0, x1);
  124. swap(y0, y1);
  125. }
  126. int16_t dx, dy;
  127. dx = x1 - x0;
  128. dy = abs(y1 - y0);
  129. int16_t err = dx / 2;
  130. int16_t ystep;
  131. if (y0 < y1) {
  132. ystep = 1;
  133. } else {
  134. ystep = -1;
  135. }
  136. for (; x0<=x1; x0++) {
  137. if (steep) {
  138. drawPixel(y0, x0, color);
  139. } else {
  140. drawPixel(x0, y0, color);
  141. }
  142. err -= dy;
  143. if (err < 0) {
  144. y0 += ystep;
  145. err += dx;
  146. }
  147. }
  148. }
  149. // draw a rectangle
  150. void Adafruit_GFX::drawRect(uint16_t x, uint16_t y,
  151. uint16_t w, uint16_t h,
  152. uint16_t color) {
  153. // stupidest version - update in subclasses if desired!
  154. for (uint16_t i=x; i<x+w; i++) {
  155. drawPixel(i, y, color);
  156. drawPixel(i, y+h-1, color);
  157. }
  158. drawFastVLine(x, y, h, color);
  159. drawFastVLine(x+w-1, y, h, color);
  160. }
  161. void Adafruit_GFX::drawFastVLine(uint16_t x, uint16_t y,
  162. uint16_t h, uint16_t color) {
  163. // stupidest version - update in subclasses if desired!
  164. for (uint16_t j=y; j<y+h; j++) {
  165. drawPixel(x, j, color);
  166. }
  167. }
  168. void Adafruit_GFX::drawFastHLine(uint16_t x, uint16_t y,
  169. uint16_t w, uint16_t color) {
  170. // stupidest version - update in subclasses if desired!
  171. for (uint16_t i=x; i<x+w; i++) {
  172. drawPixel(i, y, color);
  173. }
  174. }
  175. void Adafruit_GFX::fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h,
  176. uint16_t color) {
  177. // stupidest version - update in subclasses if desired!
  178. for (uint16_t i=x; i<x+w; i++) {
  179. drawFastVLine(i, y, h, color);
  180. }
  181. }
  182. // draw a rounded rectangle!
  183. void Adafruit_GFX::drawRoundRect(uint16_t x, uint16_t y, uint16_t w,
  184. uint16_t h, uint16_t r, uint16_t color) {
  185. // smarter version
  186. drawFastHLine(x+r , y , w-2*r, color); // Top
  187. drawFastHLine(x+r , y+h-1, w-2*r, color); // Bottom
  188. drawFastVLine( x , y+r , h-2*r, color); // Left
  189. drawFastVLine( x+w-1, y+r , h-2*r, color); // Right
  190. // draw four corners
  191. drawCircleHelper(x+r , y+r , r, 1, color);
  192. drawCircleHelper(x+w-r-1, y+r , r, 2, color);
  193. drawCircleHelper(x+w-r-1, y+h-r-1, r, 4, color);
  194. drawCircleHelper(x+r , y+h-r-1, r, 8, color);
  195. }
  196. // fill a rounded rectangle!
  197. void Adafruit_GFX::fillRoundRect(uint16_t x, uint16_t y, uint16_t w,
  198. uint16_t h, uint16_t r, uint16_t color) {
  199. // smarter version
  200. fillRect(x+r, y, w-2*r, h, color);
  201. // draw four corners
  202. fillCircleHelper(x+w-r-1, y+r, r, 1, h-2*r-1, color);
  203. fillCircleHelper(x+r , y+r, r, 2, h-2*r-1, color);
  204. }
  205. // draw a triangle!
  206. void Adafruit_GFX::drawTriangle(uint16_t x0, uint16_t y0,
  207. uint16_t x1, uint16_t y1,
  208. uint16_t x2, uint16_t y2, uint16_t color) {
  209. drawLine(x0, y0, x1, y1, color);
  210. drawLine(x1, y1, x2, y2, color);
  211. drawLine(x2, y2, x0, y0, color);
  212. }
  213. // fill a triangle!
  214. void Adafruit_GFX::fillTriangle ( uint16_t x0, uint16_t y0,
  215. uint16_t x1, uint16_t y1,
  216. uint16_t x2, uint16_t y2, uint16_t color) {
  217. int16_t a, b, y, last;
  218. // Sort coordinates by Y order (y2 >= y1 >= y0)
  219. if (y0 > y1) {
  220. swap(y0, y1); swap(x0, x1);
  221. }
  222. if (y1 > y2) {
  223. swap(y2, y1); swap(x2, x1);
  224. }
  225. if (y0 > y1) {
  226. swap(y0, y1); swap(x0, x1);
  227. }
  228. if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing
  229. a = b = x0;
  230. if(x1 < a) a = x1;
  231. else if(x1 > b) b = x1;
  232. if(x2 < a) a = x2;
  233. else if(x2 > b) b = x2;
  234. drawFastHLine(a, y0, b-a+1, color);
  235. return;
  236. }
  237. int16_t
  238. dx01 = x1 - x0,
  239. dy01 = y1 - y0,
  240. dx02 = x2 - x0,
  241. dy02 = y2 - y0,
  242. dx12 = x2 - x1,
  243. dy12 = y2 - y1,
  244. sa = 0,
  245. sb = 0;
  246. // For upper part of triangle, find scanline crossings for segments
  247. // 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1
  248. // is included here (and second loop will be skipped, avoiding a /0
  249. // error there), otherwise scanline y1 is skipped here and handled
  250. // in the second loop...which also avoids a /0 error here if y0=y1
  251. // (flat-topped triangle).
  252. if(y1 == y2) last = y1; // Include y1 scanline
  253. else last = y1-1; // Skip it
  254. for(y=y0; y<=last; y++) {
  255. a = x0 + sa / dy01;
  256. b = x0 + sb / dy02;
  257. sa += dx01;
  258. sb += dx02;
  259. /* longhand:
  260. a = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
  261. b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
  262. */
  263. if(a > b) swap(a,b);
  264. drawFastHLine(a, y, b-a+1, color);
  265. }
  266. // For lower part of triangle, find scanline crossings for segments
  267. // 0-2 and 1-2. This loop is skipped if y1=y2.
  268. sa = dx12 * (y - y1);
  269. sb = dx02 * (y - y0);
  270. for(; y<=y2; y++) {
  271. a = x1 + sa / dy12;
  272. b = x0 + sb / dy02;
  273. sa += dx12;
  274. sb += dx02;
  275. /* longhand:
  276. a = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
  277. b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
  278. */
  279. if(a > b) swap(a,b);
  280. drawFastHLine(a, y, b-a+1, color);
  281. }
  282. }
  283. void Adafruit_GFX::drawBitmap(uint16_t x, uint16_t y,
  284. const uint8_t *bitmap, uint16_t w, uint16_t h,
  285. uint16_t color) {
  286. for (uint16_t j=0; j<h; j++) {
  287. for (uint16_t i=0; i<w; i++ ) {
  288. if (pgm_read_byte(bitmap + i + (j/8)*w) & _BV(j%8)) {
  289. drawPixel(x+i, y+j, color);
  290. }
  291. }
  292. }
  293. }
  294. #if ARDUINO >= 100
  295. size_t Adafruit_GFX::write(uint8_t c) {
  296. #else
  297. void Adafruit_GFX::write(uint8_t c) {
  298. #endif
  299. if (c == '\n') {
  300. cursor_y += textsize*8;
  301. cursor_x = 0;
  302. } else if (c == '\r') {
  303. // skip em
  304. } else {
  305. drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
  306. cursor_x += textsize*6;
  307. }
  308. #if ARDUINO >= 100
  309. return 1;
  310. #endif
  311. }
  312. // draw a character
  313. void Adafruit_GFX::drawChar(uint16_t x, uint16_t y, char c,
  314. uint16_t color, uint16_t bg, uint8_t size) {
  315. for (uint8_t i=0; i<6; i++ ) {
  316. uint8_t line;
  317. if (i == 5)
  318. line = 0x0;
  319. else
  320. line = pgm_read_byte(font+(c*5)+i);
  321. for (uint8_t j = 0; j<8; j++) {
  322. if (line & 0x1) {
  323. if (size == 1) // default size
  324. drawPixel(x+i, y+j, color);
  325. else { // big size
  326. fillRect(x+(i*size), y+(j*size), size, size, color);
  327. }
  328. } else if (bg != color) {
  329. if (size == 1) // default size
  330. drawPixel(x+i, y+j, bg);
  331. else { // big size
  332. fillRect(x+i*size, y+j*size, size, size, bg);
  333. }
  334. }
  335. line >>= 1;
  336. }
  337. }
  338. }
  339. void Adafruit_GFX::setCursor(uint16_t x, uint16_t y) {
  340. cursor_x = x;
  341. cursor_y = y;
  342. }
  343. void Adafruit_GFX::setTextSize(uint8_t s) {
  344. textsize = (s > 0) ? s : 1;
  345. }
  346. void Adafruit_GFX::setTextColor(uint16_t c) {
  347. textcolor = c;
  348. textbgcolor = c;
  349. // for 'transparent' background, we'll set the bg
  350. // to the same as fg instead of using a flag
  351. }
  352. void Adafruit_GFX::setTextColor(uint16_t c, uint16_t b) {
  353. textcolor = c;
  354. textbgcolor = b;
  355. }