Adafruit_GFX.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. void Adafruit_GFX::drawBitmap(uint16_t x, uint16_t y,
  206. const uint8_t *bitmap, uint16_t w, uint16_t h,
  207. uint16_t color) {
  208. for (uint16_t j=0; j<h; j++) {
  209. for (uint16_t i=0; i<w; i++ ) {
  210. if (pgm_read_byte(bitmap + i + (j/8)*w) & _BV(j%8)) {
  211. drawPixel(x+i, y+j, color);
  212. }
  213. }
  214. }
  215. }
  216. #if ARDUINO >= 100
  217. size_t Adafruit_GFX::write(uint8_t c) {
  218. #else
  219. void Adafruit_GFX::write(uint8_t c) {
  220. #endif
  221. if (c == '\n') {
  222. cursor_y += textsize*8;
  223. cursor_x = 0;
  224. } else if (c == '\r') {
  225. // skip em
  226. } else {
  227. drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
  228. cursor_x += textsize*6;
  229. }
  230. #if ARDUINO >= 100
  231. return 1;
  232. #endif
  233. }
  234. // draw a character
  235. void Adafruit_GFX::drawChar(uint16_t x, uint16_t y, char c,
  236. uint16_t color, uint16_t bg, uint8_t size) {
  237. for (uint8_t i=0; i<6; i++ ) {
  238. uint8_t line;
  239. if (i == 5)
  240. line = 0x0;
  241. else
  242. line = pgm_read_byte(font+(c*5)+i);
  243. for (uint8_t j = 0; j<8; j++) {
  244. if (line & 0x1) {
  245. if (size == 1) // default size
  246. drawPixel(x+i, y+j, color);
  247. else { // big size
  248. fillRect(x+(i*size), y+(j*size), size, size, color);
  249. }
  250. } else if (bg != color) {
  251. if (size == 1) // default size
  252. drawPixel(x+i, y+j, bg);
  253. else { // big size
  254. fillRect(x+i*size, y+j*size, size, size, bg);
  255. }
  256. }
  257. line >>= 1;
  258. }
  259. }
  260. }
  261. void Adafruit_GFX::setCursor(uint16_t x, uint16_t y) {
  262. cursor_x = x;
  263. cursor_y = y;
  264. }
  265. void Adafruit_GFX::setTextSize(uint8_t s) {
  266. textsize = (s > 0) ? s : 1;
  267. }
  268. void Adafruit_GFX::setTextColor(uint16_t c) {
  269. textcolor = c;
  270. textbgcolor = c;
  271. // for 'transparent' background, we'll set the bg
  272. // to the same as fg instead of using a flag
  273. }
  274. void Adafruit_GFX::setTextColor(uint16_t c, uint16_t b) {
  275. textcolor = c;
  276. textbgcolor = b;
  277. }