Adafruit_GFX.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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(uint8_t x0, uint8_t y0, uint8_t r,
  18. uint8_t color) {
  19. int8_t f = 1 - r;
  20. int8_t ddF_x = 1;
  21. int8_t ddF_y = -2 * r;
  22. int8_t x = 0;
  23. int8_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::fillCircle(uint8_t x0, uint8_t y0, uint8_t r,
  48. uint8_t color) {
  49. int8_t f = 1 - r;
  50. int8_t ddF_x = 1;
  51. int8_t ddF_y = -2 * r;
  52. int8_t x = 0;
  53. int8_t y = r;
  54. drawFastVLine(x0, y0-r, r*2+1, color);
  55. while (x<y) {
  56. if (f >= 0) {
  57. y--;
  58. ddF_y += 2;
  59. f += ddF_y;
  60. }
  61. x++;
  62. ddF_x += 2;
  63. f += ddF_x;
  64. drawFastVLine(x0+x, y0-y, y*2+1, color);
  65. drawFastVLine(x0-x, y0-y, y*2+1, color);
  66. drawFastVLine(x0+y, y0-x, x*2+1, color);
  67. drawFastVLine(x0-y, y0-x, x*2+1, color);
  68. }
  69. }
  70. // bresenham's algorithm - thx wikpedia
  71. void Adafruit_GFX::drawLine(int16_t x0, int16_t y0,
  72. int16_t x1, int16_t y1,
  73. uint8_t color) {
  74. int16_t steep = abs(y1 - y0) > abs(x1 - x0);
  75. if (steep) {
  76. swap(x0, y0);
  77. swap(x1, y1);
  78. }
  79. if (x0 > x1) {
  80. swap(x0, x1);
  81. swap(y0, y1);
  82. }
  83. int16_t dx, dy;
  84. dx = x1 - x0;
  85. dy = abs(y1 - y0);
  86. int16_t err = dx / 2;
  87. int16_t ystep;
  88. if (y0 < y1) {
  89. ystep = 1;
  90. } else {
  91. ystep = -1;
  92. }
  93. for (; x0<=x1; x0++) {
  94. if (steep) {
  95. drawPixel(y0, x0, color);
  96. } else {
  97. drawPixel(x0, y0, color);
  98. }
  99. err -= dy;
  100. if (err < 0) {
  101. y0 += ystep;
  102. err += dx;
  103. }
  104. }
  105. }
  106. // draw a rectangle
  107. void Adafruit_GFX::drawRect(uint8_t x, uint8_t y,
  108. uint8_t w, uint8_t h,
  109. uint8_t color) {
  110. // stupidest version - update in subclasses if desired!
  111. for (uint8_t i=x; i<x+w; i++) {
  112. drawPixel(i, y, color);
  113. drawPixel(i, y+h-1, color);
  114. }
  115. drawFastVLine(x, y, h, color);
  116. drawFastVLine(x+w-1, y, h, color);
  117. }
  118. void Adafruit_GFX::drawFastVLine(uint8_t x, uint8_t y,
  119. uint8_t h, uint8_t color) {
  120. // stupidest version - update in subclasses if desired!
  121. for (uint8_t j=y; j<y+h; j++) {
  122. drawPixel(x, j, color);
  123. }
  124. }
  125. void Adafruit_GFX::fillRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h,
  126. uint8_t color) {
  127. // stupidest version - update in subclasses if desired!
  128. for (uint8_t i=x; i<x+w; i++) {
  129. drawFastVLine(i, y, h, color);
  130. }
  131. }
  132. void Adafruit_GFX::drawBitmap(uint8_t x, uint8_t y,
  133. const uint8_t *bitmap, uint8_t w, uint8_t h,
  134. uint8_t color) {
  135. for (uint8_t j=0; j<h; j++) {
  136. for (uint8_t i=0; i<w; i++ ) {
  137. if (pgm_read_byte(bitmap + i + (j/8)*w) & _BV(j%8)) {
  138. drawPixel(x+i, y+j, color);
  139. }
  140. }
  141. }
  142. }
  143. #if ARDUINO >= 100
  144. size_t Adafruit_GFX::write(uint8_t c) {
  145. #else
  146. void Adafruit_GFX::write(uint8_t c) {
  147. #endif
  148. if (c == '\n') {
  149. cursor_y += textsize*8;
  150. cursor_x = 0;
  151. } else if (c == '\r') {
  152. // skip em
  153. } else {
  154. drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
  155. cursor_x += textsize*6;
  156. }
  157. #if ARDUINO >= 100
  158. return 1;
  159. #endif
  160. }
  161. // draw a character
  162. void Adafruit_GFX::drawChar(uint8_t x, uint8_t y, char c,
  163. uint16_t color, uint16_t bg, uint8_t size) {
  164. for (uint8_t i=0; i<6; i++ ) {
  165. uint8_t line;
  166. if (i == 5)
  167. line = 0x0;
  168. else
  169. line = pgm_read_byte(font+(c*5)+i);
  170. for (uint8_t j = 0; j<8; j++) {
  171. if (line & 0x1) {
  172. if (size == 1) // default size
  173. drawPixel(x+i, y+j, color);
  174. else { // big size
  175. fillRect(x+(i*size), y+(j*size), size, size, color);
  176. }
  177. } else if (bg != color) {
  178. if (size == 1) // default size
  179. drawPixel(x+i, y+j, bg);
  180. else { // big size
  181. fillRect(x+i*size, y+j*size, size, size, bg);
  182. }
  183. }
  184. line >>= 1;
  185. }
  186. }
  187. }
  188. void Adafruit_GFX::setCursor(uint16_t x, uint16_t y) {
  189. cursor_x = x;
  190. cursor_y = y;
  191. }
  192. void Adafruit_GFX::setTextSize(uint8_t s) {
  193. textsize = (s > 0) ? s : 1;
  194. }
  195. void Adafruit_GFX::setTextColor(uint16_t c) {
  196. textcolor = c;
  197. textbgcolor = c;
  198. // for 'transparent' background, we'll set the bg
  199. // to the same as fg instead of using a flag
  200. }
  201. void Adafruit_GFX::setTextColor(uint16_t c, uint16_t b) {
  202. textcolor = c;
  203. textbgcolor = b;
  204. }