Adafruit_GFX.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. This is the core graphics library for all our displays, providing a common
  3. set of graphics primitives (points, lines, circles, etc.). It needs to be
  4. paired with a hardware-specific library for each display device we carry
  5. (to handle the lower-level functions).
  6. Adafruit invests time and resources providing this open source code, please
  7. support Adafruit & open-source hardware by purchasing products from Adafruit!
  8. Copyright (c) 2013 Adafruit Industries. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. - Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. - Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  20. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "Adafruit_GFX.h"
  29. #include "glcdfont.c"
  30. #ifdef __AVR__
  31. #include <avr/pgmspace.h>
  32. #else
  33. #define pgm_read_byte(addr) (*(const unsigned char *)(addr))
  34. #endif
  35. Adafruit_GFX::Adafruit_GFX(int16_t w, int16_t h):
  36. WIDTH(w), HEIGHT(h)
  37. {
  38. _width = WIDTH;
  39. _height = HEIGHT;
  40. rotation = 0;
  41. cursor_y = cursor_x = 0;
  42. textsize = 1;
  43. textcolor = textbgcolor = 0xFFFF;
  44. wrap = true;
  45. }
  46. // Draw a circle outline
  47. void Adafruit_GFX::drawCircle(int16_t x0, int16_t y0, int16_t r,
  48. 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. drawPixel(x0 , y0+r, color);
  55. drawPixel(x0 , y0-r, color);
  56. drawPixel(x0+r, y0 , color);
  57. drawPixel(x0-r, y0 , color);
  58. while (x<y) {
  59. if (f >= 0) {
  60. y--;
  61. ddF_y += 2;
  62. f += ddF_y;
  63. }
  64. x++;
  65. ddF_x += 2;
  66. f += ddF_x;
  67. drawPixel(x0 + x, y0 + y, color);
  68. drawPixel(x0 - x, y0 + y, color);
  69. drawPixel(x0 + x, y0 - y, color);
  70. drawPixel(x0 - x, y0 - y, color);
  71. drawPixel(x0 + y, y0 + x, color);
  72. drawPixel(x0 - y, y0 + x, color);
  73. drawPixel(x0 + y, y0 - x, color);
  74. drawPixel(x0 - y, y0 - x, color);
  75. }
  76. }
  77. void Adafruit_GFX::drawCircleHelper( int16_t x0, int16_t y0,
  78. int16_t r, uint8_t cornername, uint16_t color) {
  79. int16_t f = 1 - r;
  80. int16_t ddF_x = 1;
  81. int16_t ddF_y = -2 * r;
  82. int16_t x = 0;
  83. int16_t y = r;
  84. while (x<y) {
  85. if (f >= 0) {
  86. y--;
  87. ddF_y += 2;
  88. f += ddF_y;
  89. }
  90. x++;
  91. ddF_x += 2;
  92. f += ddF_x;
  93. if (cornername & 0x4) {
  94. drawPixel(x0 + x, y0 + y, color);
  95. drawPixel(x0 + y, y0 + x, color);
  96. }
  97. if (cornername & 0x2) {
  98. drawPixel(x0 + x, y0 - y, color);
  99. drawPixel(x0 + y, y0 - x, color);
  100. }
  101. if (cornername & 0x8) {
  102. drawPixel(x0 - y, y0 + x, color);
  103. drawPixel(x0 - x, y0 + y, color);
  104. }
  105. if (cornername & 0x1) {
  106. drawPixel(x0 - y, y0 - x, color);
  107. drawPixel(x0 - x, y0 - y, color);
  108. }
  109. }
  110. }
  111. void Adafruit_GFX::fillCircle(int16_t x0, int16_t y0, int16_t r,
  112. uint16_t color) {
  113. drawFastVLine(x0, y0-r, 2*r+1, color);
  114. fillCircleHelper(x0, y0, r, 3, 0, color);
  115. }
  116. // Used to do circles and roundrects
  117. void Adafruit_GFX::fillCircleHelper(int16_t x0, int16_t y0, int16_t r,
  118. uint8_t cornername, int16_t delta, uint16_t color) {
  119. int16_t f = 1 - r;
  120. int16_t ddF_x = 1;
  121. int16_t ddF_y = -2 * r;
  122. int16_t x = 0;
  123. int16_t y = r;
  124. while (x<y) {
  125. if (f >= 0) {
  126. y--;
  127. ddF_y += 2;
  128. f += ddF_y;
  129. }
  130. x++;
  131. ddF_x += 2;
  132. f += ddF_x;
  133. if (cornername & 0x1) {
  134. drawFastVLine(x0+x, y0-y, 2*y+1+delta, color);
  135. drawFastVLine(x0+y, y0-x, 2*x+1+delta, color);
  136. }
  137. if (cornername & 0x2) {
  138. drawFastVLine(x0-x, y0-y, 2*y+1+delta, color);
  139. drawFastVLine(x0-y, y0-x, 2*x+1+delta, color);
  140. }
  141. }
  142. }
  143. // Bresenham's algorithm - thx wikpedia
  144. void Adafruit_GFX::drawLine(int16_t x0, int16_t y0,
  145. int16_t x1, int16_t y1,
  146. uint16_t color) {
  147. int16_t steep = abs(y1 - y0) > abs(x1 - x0);
  148. if (steep) {
  149. swap(x0, y0);
  150. swap(x1, y1);
  151. }
  152. if (x0 > x1) {
  153. swap(x0, x1);
  154. swap(y0, y1);
  155. }
  156. int16_t dx, dy;
  157. dx = x1 - x0;
  158. dy = abs(y1 - y0);
  159. int16_t err = dx / 2;
  160. int16_t ystep;
  161. if (y0 < y1) {
  162. ystep = 1;
  163. } else {
  164. ystep = -1;
  165. }
  166. for (; x0<=x1; x0++) {
  167. if (steep) {
  168. drawPixel(y0, x0, color);
  169. } else {
  170. drawPixel(x0, y0, color);
  171. }
  172. err -= dy;
  173. if (err < 0) {
  174. y0 += ystep;
  175. err += dx;
  176. }
  177. }
  178. }
  179. // Draw a rectangle
  180. void Adafruit_GFX::drawRect(int16_t x, int16_t y,
  181. int16_t w, int16_t h,
  182. uint16_t color) {
  183. drawFastHLine(x, y, w, color);
  184. drawFastHLine(x, y+h-1, w, color);
  185. drawFastVLine(x, y, h, color);
  186. drawFastVLine(x+w-1, y, h, color);
  187. }
  188. void Adafruit_GFX::drawFastVLine(int16_t x, int16_t y,
  189. int16_t h, uint16_t color) {
  190. // Update in subclasses if desired!
  191. drawLine(x, y, x, y+h-1, color);
  192. }
  193. void Adafruit_GFX::drawFastHLine(int16_t x, int16_t y,
  194. int16_t w, uint16_t color) {
  195. // Update in subclasses if desired!
  196. drawLine(x, y, x+w-1, y, color);
  197. }
  198. void Adafruit_GFX::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
  199. uint16_t color) {
  200. // Update in subclasses if desired!
  201. for (int16_t i=x; i<x+w; i++) {
  202. drawFastVLine(i, y, h, color);
  203. }
  204. }
  205. void Adafruit_GFX::fillScreen(uint16_t color) {
  206. fillRect(0, 0, _width, _height, color);
  207. }
  208. // Draw a rounded rectangle
  209. void Adafruit_GFX::drawRoundRect(int16_t x, int16_t y, int16_t w,
  210. int16_t h, int16_t r, uint16_t color) {
  211. // smarter version
  212. drawFastHLine(x+r , y , w-2*r, color); // Top
  213. drawFastHLine(x+r , y+h-1, w-2*r, color); // Bottom
  214. drawFastVLine(x , y+r , h-2*r, color); // Left
  215. drawFastVLine(x+w-1, y+r , h-2*r, color); // Right
  216. // draw four corners
  217. drawCircleHelper(x+r , y+r , r, 1, color);
  218. drawCircleHelper(x+w-r-1, y+r , r, 2, color);
  219. drawCircleHelper(x+w-r-1, y+h-r-1, r, 4, color);
  220. drawCircleHelper(x+r , y+h-r-1, r, 8, color);
  221. }
  222. // Fill a rounded rectangle
  223. void Adafruit_GFX::fillRoundRect(int16_t x, int16_t y, int16_t w,
  224. int16_t h, int16_t r, uint16_t color) {
  225. // smarter version
  226. fillRect(x+r, y, w-2*r, h, color);
  227. // draw four corners
  228. fillCircleHelper(x+w-r-1, y+r, r, 1, h-2*r-1, color);
  229. fillCircleHelper(x+r , y+r, r, 2, h-2*r-1, color);
  230. }
  231. // Draw a triangle
  232. void Adafruit_GFX::drawTriangle(int16_t x0, int16_t y0,
  233. int16_t x1, int16_t y1,
  234. int16_t x2, int16_t y2, uint16_t color) {
  235. drawLine(x0, y0, x1, y1, color);
  236. drawLine(x1, y1, x2, y2, color);
  237. drawLine(x2, y2, x0, y0, color);
  238. }
  239. // Fill a triangle
  240. void Adafruit_GFX::fillTriangle ( int16_t x0, int16_t y0,
  241. int16_t x1, int16_t y1,
  242. int16_t x2, int16_t y2, uint16_t color) {
  243. int16_t a, b, y, last;
  244. // Sort coordinates by Y order (y2 >= y1 >= y0)
  245. if (y0 > y1) {
  246. swap(y0, y1); swap(x0, x1);
  247. }
  248. if (y1 > y2) {
  249. swap(y2, y1); swap(x2, x1);
  250. }
  251. if (y0 > y1) {
  252. swap(y0, y1); swap(x0, x1);
  253. }
  254. if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing
  255. a = b = x0;
  256. if(x1 < a) a = x1;
  257. else if(x1 > b) b = x1;
  258. if(x2 < a) a = x2;
  259. else if(x2 > b) b = x2;
  260. drawFastHLine(a, y0, b-a+1, color);
  261. return;
  262. }
  263. int16_t
  264. dx01 = x1 - x0,
  265. dy01 = y1 - y0,
  266. dx02 = x2 - x0,
  267. dy02 = y2 - y0,
  268. dx12 = x2 - x1,
  269. dy12 = y2 - y1;
  270. int32_t
  271. sa = 0,
  272. sb = 0;
  273. // For upper part of triangle, find scanline crossings for segments
  274. // 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1
  275. // is included here (and second loop will be skipped, avoiding a /0
  276. // error there), otherwise scanline y1 is skipped here and handled
  277. // in the second loop...which also avoids a /0 error here if y0=y1
  278. // (flat-topped triangle).
  279. if(y1 == y2) last = y1; // Include y1 scanline
  280. else last = y1-1; // Skip it
  281. for(y=y0; y<=last; y++) {
  282. a = x0 + sa / dy01;
  283. b = x0 + sb / dy02;
  284. sa += dx01;
  285. sb += dx02;
  286. /* longhand:
  287. a = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
  288. b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
  289. */
  290. if(a > b) swap(a,b);
  291. drawFastHLine(a, y, b-a+1, color);
  292. }
  293. // For lower part of triangle, find scanline crossings for segments
  294. // 0-2 and 1-2. This loop is skipped if y1=y2.
  295. sa = dx12 * (y - y1);
  296. sb = dx02 * (y - y0);
  297. for(; y<=y2; y++) {
  298. a = x1 + sa / dy12;
  299. b = x0 + sb / dy02;
  300. sa += dx12;
  301. sb += dx02;
  302. /* longhand:
  303. a = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
  304. b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
  305. */
  306. if(a > b) swap(a,b);
  307. drawFastHLine(a, y, b-a+1, color);
  308. }
  309. }
  310. void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
  311. const uint8_t *bitmap, int16_t w, int16_t h,
  312. uint16_t color) {
  313. int16_t i, j, byteWidth = (w + 7) / 8;
  314. for(j=0; j<h; j++) {
  315. for(i=0; i<w; i++ ) {
  316. if(pgm_read_byte(bitmap + j * byteWidth + i / 8) & (128 >> (i & 7))) {
  317. drawPixel(x+i, y+j, color);
  318. }
  319. }
  320. }
  321. }
  322. // Draw a 1-bit color bitmap at the specified x, y position from the
  323. // provided bitmap buffer (must be PROGMEM memory) using color as the
  324. // foreground color and bg as the background color.
  325. void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
  326. const uint8_t *bitmap, int16_t w, int16_t h,
  327. uint16_t color, uint16_t bg) {
  328. int16_t i, j, byteWidth = (w + 7) / 8;
  329. for(j=0; j<h; j++) {
  330. for(i=0; i<w; i++ ) {
  331. if(pgm_read_byte(bitmap + j * byteWidth + i / 8) & (128 >> (i & 7))) {
  332. drawPixel(x+i, y+j, color);
  333. }
  334. else {
  335. drawPixel(x+i, y+j, bg);
  336. }
  337. }
  338. }
  339. }
  340. //Draw XBitMap Files (*.xbm), exported from GIMP,
  341. //Usage: Export from GIMP to *.xbm, rename *.xbm to *.c and open in editor.
  342. //C Array can be directly used with this function
  343. void Adafruit_GFX::drawXBitmap(int16_t x, int16_t y,
  344. const uint8_t *bitmap, int16_t w, int16_t h,
  345. uint16_t color) {
  346. int16_t i, j, byteWidth = (w + 7) / 8;
  347. for(j=0; j<h; j++) {
  348. for(i=0; i<w; i++ ) {
  349. if(pgm_read_byte(bitmap + j * byteWidth + i / 8) & (1 << (i % 8))) {
  350. drawPixel(x+i, y+j, color);
  351. }
  352. }
  353. }
  354. }
  355. #if ARDUINO >= 100
  356. size_t Adafruit_GFX::write(uint8_t c) {
  357. #else
  358. void Adafruit_GFX::write(uint8_t c) {
  359. #endif
  360. if (c == '\n') {
  361. cursor_y += textsize*8;
  362. cursor_x = 0;
  363. } else if (c == '\r') {
  364. // skip em
  365. } else {
  366. drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
  367. cursor_x += textsize*6;
  368. if (wrap && (cursor_x > (_width - textsize*6))) {
  369. cursor_y += textsize*8;
  370. cursor_x = 0;
  371. }
  372. }
  373. #if ARDUINO >= 100
  374. return 1;
  375. #endif
  376. }
  377. // Draw a character
  378. void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
  379. uint16_t color, uint16_t bg, uint8_t size) {
  380. if((x >= _width) || // Clip right
  381. (y >= _height) || // Clip bottom
  382. ((x + 6 * size - 1) < 0) || // Clip left
  383. ((y + 8 * size - 1) < 0)) // Clip top
  384. return;
  385. for (int8_t i=0; i<6; i++ ) {
  386. uint8_t line;
  387. if (i == 5)
  388. line = 0x0;
  389. else
  390. line = pgm_read_byte(font+(c*5)+i);
  391. for (int8_t j = 0; j<8; j++) {
  392. if (line & 0x1) {
  393. if (size == 1) // default size
  394. drawPixel(x+i, y+j, color);
  395. else { // big size
  396. fillRect(x+(i*size), y+(j*size), size, size, color);
  397. }
  398. } else if (bg != color) {
  399. if (size == 1) // default size
  400. drawPixel(x+i, y+j, bg);
  401. else { // big size
  402. fillRect(x+i*size, y+j*size, size, size, bg);
  403. }
  404. }
  405. line >>= 1;
  406. }
  407. }
  408. }
  409. void Adafruit_GFX::setCursor(int16_t x, int16_t y) {
  410. cursor_x = x;
  411. cursor_y = y;
  412. }
  413. int16_t Adafruit_GFX::getCursorX(void) const {
  414. return cursor_x;
  415. }
  416. int16_t Adafruit_GFX::getCursorY(void) const {
  417. return cursor_y;
  418. }
  419. void Adafruit_GFX::setTextSize(uint8_t s) {
  420. textsize = (s > 0) ? s : 1;
  421. }
  422. void Adafruit_GFX::setTextColor(uint16_t c) {
  423. // For 'transparent' background, we'll set the bg
  424. // to the same as fg instead of using a flag
  425. textcolor = textbgcolor = c;
  426. }
  427. void Adafruit_GFX::setTextColor(uint16_t c, uint16_t b) {
  428. textcolor = c;
  429. textbgcolor = b;
  430. }
  431. void Adafruit_GFX::setTextWrap(boolean w) {
  432. wrap = w;
  433. }
  434. uint8_t Adafruit_GFX::getRotation(void) const {
  435. return rotation;
  436. }
  437. void Adafruit_GFX::setRotation(uint8_t x) {
  438. rotation = (x & 3);
  439. switch(rotation) {
  440. case 0:
  441. case 2:
  442. _width = WIDTH;
  443. _height = HEIGHT;
  444. break;
  445. case 1:
  446. case 3:
  447. _width = HEIGHT;
  448. _height = WIDTH;
  449. break;
  450. }
  451. }
  452. // Return the size of the display (per current rotation)
  453. int16_t Adafruit_GFX::width(void) const {
  454. return _width;
  455. }
  456. int16_t Adafruit_GFX::height(void) const {
  457. return _height;
  458. }
  459. void Adafruit_GFX::invertDisplay(boolean i) {
  460. // Do nothing, must be subclassed if supported
  461. }
  462. /***************************************************************************/
  463. // code for the GFX button UI element
  464. Adafruit_GFX_Button::Adafruit_GFX_Button(void) {
  465. _gfx = 0;
  466. }
  467. void Adafruit_GFX_Button::initButton(Adafruit_GFX *gfx,
  468. int16_t x, int16_t y,
  469. uint8_t w, uint8_t h,
  470. uint16_t outline, uint16_t fill,
  471. uint16_t textcolor,
  472. char *label, uint8_t textsize)
  473. {
  474. _x = x;
  475. _y = y;
  476. _w = w;
  477. _h = h;
  478. _outlinecolor = outline;
  479. _fillcolor = fill;
  480. _textcolor = textcolor;
  481. _textsize = textsize;
  482. _gfx = gfx;
  483. strncpy(_label, label, 9);
  484. _label[9] = 0;
  485. }
  486. void Adafruit_GFX_Button::drawButton(boolean inverted) {
  487. uint16_t fill, outline, text;
  488. if (! inverted) {
  489. fill = _fillcolor;
  490. outline = _outlinecolor;
  491. text = _textcolor;
  492. } else {
  493. fill = _textcolor;
  494. outline = _outlinecolor;
  495. text = _fillcolor;
  496. }
  497. _gfx->fillRoundRect(_x - (_w/2), _y - (_h/2), _w, _h, min(_w,_h)/4, fill);
  498. _gfx->drawRoundRect(_x - (_w/2), _y - (_h/2), _w, _h, min(_w,_h)/4, outline);
  499. _gfx->setCursor(_x - strlen(_label)*3*_textsize, _y-4*_textsize);
  500. _gfx->setTextColor(text);
  501. _gfx->setTextSize(_textsize);
  502. _gfx->print(_label);
  503. }
  504. boolean Adafruit_GFX_Button::contains(int16_t x, int16_t y) {
  505. if ((x < (_x - _w/2)) || (x > (_x + _w/2))) return false;
  506. if ((y < (_y - _h)) || (y > (_y + _h/2))) return false;
  507. return true;
  508. }
  509. void Adafruit_GFX_Button::press(boolean p) {
  510. laststate = currstate;
  511. currstate = p;
  512. }
  513. boolean Adafruit_GFX_Button::isPressed() { return currstate; }
  514. boolean Adafruit_GFX_Button::justPressed() { return (currstate && !laststate); }
  515. boolean Adafruit_GFX_Button::justReleased() { return (!currstate && laststate); }