Adafruit_GFX.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. Serial.println('[]');
  154. drawFastHLine(x, y, w, color);
  155. drawFastHLine(x, y+h-1, w, color);
  156. drawFastVLine(x, y, h, color);
  157. drawFastVLine(x+w-1, y, h, color);
  158. }
  159. void Adafruit_GFX::drawFastVLine(uint16_t x, uint16_t y,
  160. uint16_t h, uint16_t color) {
  161. // stupidest version - update in subclasses if desired!
  162. for (uint16_t j=y; j<y+h; j++) {
  163. drawPixel(x, j, color);
  164. }
  165. }
  166. void Adafruit_GFX::drawFastHLine(uint16_t x, uint16_t y,
  167. uint16_t w, uint16_t color) {
  168. // stupidest version - update in subclasses if desired!
  169. for (uint16_t i=x; i<x+w; i++) {
  170. drawPixel(i, y, color);
  171. }
  172. }
  173. void Adafruit_GFX::fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h,
  174. uint16_t color) {
  175. // stupidest version - update in subclasses if desired!
  176. for (uint16_t i=x; i<x+w; i++) {
  177. drawFastVLine(i, y, h, color);
  178. }
  179. }
  180. // draw a rounded rectangle!
  181. void Adafruit_GFX::drawRoundRect(uint16_t x, uint16_t y, uint16_t w,
  182. uint16_t h, uint16_t r, uint16_t color) {
  183. // smarter version
  184. drawFastHLine(x+r , y , w-2*r, color); // Top
  185. drawFastHLine(x+r , y+h-1, w-2*r, color); // Bottom
  186. drawFastVLine( x , y+r , h-2*r, color); // Left
  187. drawFastVLine( x+w-1, y+r , h-2*r, color); // Right
  188. // draw four corners
  189. drawCircleHelper(x+r , y+r , r, 1, color);
  190. drawCircleHelper(x+w-r-1, y+r , r, 2, color);
  191. drawCircleHelper(x+w-r-1, y+h-r-1, r, 4, color);
  192. drawCircleHelper(x+r , y+h-r-1, r, 8, color);
  193. }
  194. // fill a rounded rectangle!
  195. void Adafruit_GFX::fillRoundRect(uint16_t x, uint16_t y, uint16_t w,
  196. uint16_t h, uint16_t r, uint16_t color) {
  197. // smarter version
  198. fillRect(x+r, y, w-2*r, h, color);
  199. // draw four corners
  200. fillCircleHelper(x+w-r-1, y+r, r, 1, h-2*r-1, color);
  201. fillCircleHelper(x+r , y+r, r, 2, h-2*r-1, color);
  202. }
  203. // draw a triangle!
  204. void Adafruit_GFX::drawTriangle(uint16_t x0, uint16_t y0,
  205. uint16_t x1, uint16_t y1,
  206. uint16_t x2, uint16_t y2, uint16_t color) {
  207. drawLine(x0, y0, x1, y1, color);
  208. drawLine(x1, y1, x2, y2, color);
  209. drawLine(x2, y2, x0, y0, color);
  210. }
  211. // fill a triangle!
  212. void Adafruit_GFX::fillTriangle ( uint16_t x0, uint16_t y0,
  213. uint16_t x1, uint16_t y1,
  214. uint16_t x2, uint16_t y2, uint16_t color) {
  215. int16_t a, b, y, last;
  216. // Sort coordinates by Y order (y2 >= y1 >= y0)
  217. if (y0 > y1) {
  218. swap(y0, y1); swap(x0, x1);
  219. }
  220. if (y1 > y2) {
  221. swap(y2, y1); swap(x2, x1);
  222. }
  223. if (y0 > y1) {
  224. swap(y0, y1); swap(x0, x1);
  225. }
  226. if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing
  227. a = b = x0;
  228. if(x1 < a) a = x1;
  229. else if(x1 > b) b = x1;
  230. if(x2 < a) a = x2;
  231. else if(x2 > b) b = x2;
  232. drawFastHLine(a, y0, b-a+1, color);
  233. return;
  234. }
  235. int16_t
  236. dx01 = x1 - x0,
  237. dy01 = y1 - y0,
  238. dx02 = x2 - x0,
  239. dy02 = y2 - y0,
  240. dx12 = x2 - x1,
  241. dy12 = y2 - y1,
  242. sa = 0,
  243. sb = 0;
  244. // For upper part of triangle, find scanline crossings for segments
  245. // 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1
  246. // is included here (and second loop will be skipped, avoiding a /0
  247. // error there), otherwise scanline y1 is skipped here and handled
  248. // in the second loop...which also avoids a /0 error here if y0=y1
  249. // (flat-topped triangle).
  250. if(y1 == y2) last = y1; // Include y1 scanline
  251. else last = y1-1; // Skip it
  252. for(y=y0; y<=last; y++) {
  253. a = x0 + sa / dy01;
  254. b = x0 + sb / dy02;
  255. sa += dx01;
  256. sb += dx02;
  257. /* longhand:
  258. a = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
  259. b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
  260. */
  261. if(a > b) swap(a,b);
  262. drawFastHLine(a, y, b-a+1, color);
  263. }
  264. // For lower part of triangle, find scanline crossings for segments
  265. // 0-2 and 1-2. This loop is skipped if y1=y2.
  266. sa = dx12 * (y - y1);
  267. sb = dx02 * (y - y0);
  268. for(; y<=y2; y++) {
  269. a = x1 + sa / dy12;
  270. b = x0 + sb / dy02;
  271. sa += dx12;
  272. sb += dx02;
  273. /* longhand:
  274. a = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
  275. b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
  276. */
  277. if(a > b) swap(a,b);
  278. drawFastHLine(a, y, b-a+1, color);
  279. }
  280. }
  281. void Adafruit_GFX::drawBitmap(uint16_t x, uint16_t y,
  282. const uint8_t *bitmap, uint16_t w, uint16_t h,
  283. uint16_t color) {
  284. for (uint16_t j=0; j<h; j++) {
  285. for (uint16_t i=0; i<w; i++ ) {
  286. if (pgm_read_byte(bitmap + i + (j/8)*w) & _BV(j%8)) {
  287. drawPixel(x+i, y+j, color);
  288. }
  289. }
  290. }
  291. }
  292. #if ARDUINO >= 100
  293. size_t Adafruit_GFX::write(uint8_t c) {
  294. #else
  295. void Adafruit_GFX::write(uint8_t c) {
  296. #endif
  297. if (c == '\n') {
  298. cursor_y += textsize*8;
  299. cursor_x = 0;
  300. } else if (c == '\r') {
  301. // skip em
  302. } else {
  303. drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
  304. cursor_x += textsize*6;
  305. if (cursor_x > (width() - textsize*6)) {
  306. cursor_y += textsize*8;
  307. cursor_x = 0;
  308. }
  309. }
  310. #if ARDUINO >= 100
  311. return 1;
  312. #endif
  313. }
  314. // draw a character
  315. void Adafruit_GFX::drawChar(uint16_t x, uint16_t y, char c,
  316. uint16_t color, uint16_t bg, uint8_t size) {
  317. for (uint8_t i=0; i<6; i++ ) {
  318. uint8_t line;
  319. if (i == 5)
  320. line = 0x0;
  321. else
  322. line = pgm_read_byte(font+(c*5)+i);
  323. for (uint8_t j = 0; j<8; j++) {
  324. if (line & 0x1) {
  325. if (size == 1) // default size
  326. drawPixel(x+i, y+j, color);
  327. else { // big size
  328. fillRect(x+(i*size), y+(j*size), size, size, color);
  329. }
  330. } else if (bg != color) {
  331. if (size == 1) // default size
  332. drawPixel(x+i, y+j, bg);
  333. else { // big size
  334. fillRect(x+i*size, y+j*size, size, size, bg);
  335. }
  336. }
  337. line >>= 1;
  338. }
  339. }
  340. }
  341. void Adafruit_GFX::setCursor(uint16_t x, uint16_t y) {
  342. cursor_x = x;
  343. cursor_y = y;
  344. }
  345. void Adafruit_GFX::setTextSize(uint8_t s) {
  346. textsize = (s > 0) ? s : 1;
  347. }
  348. void Adafruit_GFX::setTextColor(uint16_t c) {
  349. textcolor = c;
  350. textbgcolor = c;
  351. // for 'transparent' background, we'll set the bg
  352. // to the same as fg instead of using a flag
  353. }
  354. void Adafruit_GFX::setTextColor(uint16_t c, uint16_t b) {
  355. textcolor = c;
  356. textbgcolor = b;
  357. }