Adafruit_GFX.cpp 4.6 KB

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