Adafruit_GFX.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. _cp437 = false;
  46. }
  47. // Draw a circle outline
  48. void Adafruit_GFX::drawCircle(int16_t x0, int16_t y0, int16_t r,
  49. uint16_t color) {
  50. int16_t f = 1 - r;
  51. int16_t ddF_x = 1;
  52. int16_t ddF_y = -2 * r;
  53. int16_t x = 0;
  54. int16_t y = r;
  55. drawPixel(x0 , y0+r, color);
  56. drawPixel(x0 , y0-r, color);
  57. drawPixel(x0+r, y0 , color);
  58. drawPixel(x0-r, y0 , color);
  59. while (x<y) {
  60. if (f >= 0) {
  61. y--;
  62. ddF_y += 2;
  63. f += ddF_y;
  64. }
  65. x++;
  66. ddF_x += 2;
  67. f += ddF_x;
  68. drawPixel(x0 + x, y0 + y, color);
  69. drawPixel(x0 - x, y0 + y, color);
  70. drawPixel(x0 + x, y0 - y, color);
  71. drawPixel(x0 - x, y0 - y, color);
  72. drawPixel(x0 + y, y0 + x, color);
  73. drawPixel(x0 - y, y0 + x, color);
  74. drawPixel(x0 + y, y0 - x, color);
  75. drawPixel(x0 - y, y0 - x, color);
  76. }
  77. }
  78. void Adafruit_GFX::drawCircleHelper( int16_t x0, int16_t y0,
  79. int16_t r, uint8_t cornername, uint16_t color) {
  80. int16_t f = 1 - r;
  81. int16_t ddF_x = 1;
  82. int16_t ddF_y = -2 * r;
  83. int16_t x = 0;
  84. int16_t y = r;
  85. while (x<y) {
  86. if (f >= 0) {
  87. y--;
  88. ddF_y += 2;
  89. f += ddF_y;
  90. }
  91. x++;
  92. ddF_x += 2;
  93. f += ddF_x;
  94. if (cornername & 0x4) {
  95. drawPixel(x0 + x, y0 + y, color);
  96. drawPixel(x0 + y, y0 + x, color);
  97. }
  98. if (cornername & 0x2) {
  99. drawPixel(x0 + x, y0 - y, color);
  100. drawPixel(x0 + y, y0 - x, color);
  101. }
  102. if (cornername & 0x8) {
  103. drawPixel(x0 - y, y0 + x, color);
  104. drawPixel(x0 - x, y0 + y, color);
  105. }
  106. if (cornername & 0x1) {
  107. drawPixel(x0 - y, y0 - x, color);
  108. drawPixel(x0 - x, y0 - y, color);
  109. }
  110. }
  111. }
  112. void Adafruit_GFX::fillCircle(int16_t x0, int16_t y0, int16_t r,
  113. uint16_t color) {
  114. drawFastVLine(x0, y0-r, 2*r+1, color);
  115. fillCircleHelper(x0, y0, r, 3, 0, color);
  116. }
  117. // Used to do circles and roundrects
  118. void Adafruit_GFX::fillCircleHelper(int16_t x0, int16_t y0, int16_t r,
  119. uint8_t cornername, int16_t delta, uint16_t color) {
  120. int16_t f = 1 - r;
  121. int16_t ddF_x = 1;
  122. int16_t ddF_y = -2 * r;
  123. int16_t x = 0;
  124. int16_t y = r;
  125. while (x<y) {
  126. if (f >= 0) {
  127. y--;
  128. ddF_y += 2;
  129. f += ddF_y;
  130. }
  131. x++;
  132. ddF_x += 2;
  133. f += ddF_x;
  134. if (cornername & 0x1) {
  135. drawFastVLine(x0+x, y0-y, 2*y+1+delta, color);
  136. drawFastVLine(x0+y, y0-x, 2*x+1+delta, color);
  137. }
  138. if (cornername & 0x2) {
  139. drawFastVLine(x0-x, y0-y, 2*y+1+delta, color);
  140. drawFastVLine(x0-y, y0-x, 2*x+1+delta, color);
  141. }
  142. }
  143. }
  144. // Bresenham's algorithm - thx wikpedia
  145. void Adafruit_GFX::drawLine(int16_t x0, int16_t y0,
  146. int16_t x1, int16_t y1,
  147. uint16_t color) {
  148. int16_t steep = abs(y1 - y0) > abs(x1 - x0);
  149. if (steep) {
  150. swap(x0, y0);
  151. swap(x1, y1);
  152. }
  153. if (x0 > x1) {
  154. swap(x0, x1);
  155. swap(y0, y1);
  156. }
  157. int16_t dx, dy;
  158. dx = x1 - x0;
  159. dy = abs(y1 - y0);
  160. int16_t err = dx / 2;
  161. int16_t ystep;
  162. if (y0 < y1) {
  163. ystep = 1;
  164. } else {
  165. ystep = -1;
  166. }
  167. for (; x0<=x1; x0++) {
  168. if (steep) {
  169. drawPixel(y0, x0, color);
  170. } else {
  171. drawPixel(x0, y0, color);
  172. }
  173. err -= dy;
  174. if (err < 0) {
  175. y0 += ystep;
  176. err += dx;
  177. }
  178. }
  179. }
  180. // Draw a rectangle
  181. void Adafruit_GFX::drawRect(int16_t x, int16_t y,
  182. int16_t w, int16_t h,
  183. uint16_t color) {
  184. drawFastHLine(x, y, w, color);
  185. drawFastHLine(x, y+h-1, w, color);
  186. drawFastVLine(x, y, h, color);
  187. drawFastVLine(x+w-1, y, h, color);
  188. }
  189. void Adafruit_GFX::drawFastVLine(int16_t x, int16_t y,
  190. int16_t h, uint16_t color) {
  191. // Update in subclasses if desired!
  192. drawLine(x, y, x, y+h-1, color);
  193. }
  194. void Adafruit_GFX::drawFastHLine(int16_t x, int16_t y,
  195. int16_t w, uint16_t color) {
  196. // Update in subclasses if desired!
  197. drawLine(x, y, x+w-1, y, color);
  198. }
  199. void Adafruit_GFX::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
  200. uint16_t color) {
  201. // Update in subclasses if desired!
  202. for (int16_t i=x; i<x+w; i++) {
  203. drawFastVLine(i, y, h, color);
  204. }
  205. }
  206. void Adafruit_GFX::fillScreen(uint16_t color) {
  207. fillRect(0, 0, _width, _height, color);
  208. }
  209. // Draw a rounded rectangle
  210. void Adafruit_GFX::drawRoundRect(int16_t x, int16_t y, int16_t w,
  211. int16_t h, int16_t r, uint16_t color) {
  212. // smarter version
  213. drawFastHLine(x+r , y , w-2*r, color); // Top
  214. drawFastHLine(x+r , y+h-1, w-2*r, color); // Bottom
  215. drawFastVLine(x , y+r , h-2*r, color); // Left
  216. drawFastVLine(x+w-1, y+r , h-2*r, color); // Right
  217. // draw four corners
  218. drawCircleHelper(x+r , y+r , r, 1, color);
  219. drawCircleHelper(x+w-r-1, y+r , r, 2, color);
  220. drawCircleHelper(x+w-r-1, y+h-r-1, r, 4, color);
  221. drawCircleHelper(x+r , y+h-r-1, r, 8, color);
  222. }
  223. // Fill a rounded rectangle
  224. void Adafruit_GFX::fillRoundRect(int16_t x, int16_t y, int16_t w,
  225. int16_t h, int16_t r, uint16_t color) {
  226. // smarter version
  227. fillRect(x+r, y, w-2*r, h, color);
  228. // draw four corners
  229. fillCircleHelper(x+w-r-1, y+r, r, 1, h-2*r-1, color);
  230. fillCircleHelper(x+r , y+r, r, 2, h-2*r-1, color);
  231. }
  232. // Draw a triangle
  233. void Adafruit_GFX::drawTriangle(int16_t x0, int16_t y0,
  234. int16_t x1, int16_t y1,
  235. int16_t x2, int16_t y2, uint16_t color) {
  236. drawLine(x0, y0, x1, y1, color);
  237. drawLine(x1, y1, x2, y2, color);
  238. drawLine(x2, y2, x0, y0, color);
  239. }
  240. // Fill a triangle
  241. void Adafruit_GFX::fillTriangle ( int16_t x0, int16_t y0,
  242. int16_t x1, int16_t y1,
  243. int16_t x2, int16_t y2, uint16_t color) {
  244. int16_t a, b, y, last;
  245. // Sort coordinates by Y order (y2 >= y1 >= y0)
  246. if (y0 > y1) {
  247. swap(y0, y1); swap(x0, x1);
  248. }
  249. if (y1 > y2) {
  250. swap(y2, y1); swap(x2, x1);
  251. }
  252. if (y0 > y1) {
  253. swap(y0, y1); swap(x0, x1);
  254. }
  255. if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing
  256. a = b = x0;
  257. if(x1 < a) a = x1;
  258. else if(x1 > b) b = x1;
  259. if(x2 < a) a = x2;
  260. else if(x2 > b) b = x2;
  261. drawFastHLine(a, y0, b-a+1, color);
  262. return;
  263. }
  264. int16_t
  265. dx01 = x1 - x0,
  266. dy01 = y1 - y0,
  267. dx02 = x2 - x0,
  268. dy02 = y2 - y0,
  269. dx12 = x2 - x1,
  270. dy12 = y2 - y1;
  271. int32_t
  272. sa = 0,
  273. sb = 0;
  274. // For upper part of triangle, find scanline crossings for segments
  275. // 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1
  276. // is included here (and second loop will be skipped, avoiding a /0
  277. // error there), otherwise scanline y1 is skipped here and handled
  278. // in the second loop...which also avoids a /0 error here if y0=y1
  279. // (flat-topped triangle).
  280. if(y1 == y2) last = y1; // Include y1 scanline
  281. else last = y1-1; // Skip it
  282. for(y=y0; y<=last; y++) {
  283. a = x0 + sa / dy01;
  284. b = x0 + sb / dy02;
  285. sa += dx01;
  286. sb += dx02;
  287. /* longhand:
  288. a = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
  289. b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
  290. */
  291. if(a > b) swap(a,b);
  292. drawFastHLine(a, y, b-a+1, color);
  293. }
  294. // For lower part of triangle, find scanline crossings for segments
  295. // 0-2 and 1-2. This loop is skipped if y1=y2.
  296. sa = dx12 * (y - y1);
  297. sb = dx02 * (y - y0);
  298. for(; y<=y2; y++) {
  299. a = x1 + sa / dy12;
  300. b = x0 + sb / dy02;
  301. sa += dx12;
  302. sb += dx02;
  303. /* longhand:
  304. a = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
  305. b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
  306. */
  307. if(a > b) swap(a,b);
  308. drawFastHLine(a, y, b-a+1, color);
  309. }
  310. }
  311. void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
  312. const uint8_t *bitmap, int16_t w, int16_t h,
  313. uint16_t color) {
  314. int16_t i, j, byteWidth = (w + 7) / 8;
  315. for(j=0; j<h; j++) {
  316. for(i=0; i<w; i++ ) {
  317. if(pgm_read_byte(bitmap + j * byteWidth + i / 8) & (128 >> (i & 7))) {
  318. drawPixel(x+i, y+j, color);
  319. }
  320. }
  321. }
  322. }
  323. // Draw a 1-bit color bitmap at the specified x, y position from the
  324. // provided bitmap buffer (must be PROGMEM memory) using color as the
  325. // foreground color and bg as the background color.
  326. void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
  327. const uint8_t *bitmap, int16_t w, int16_t h,
  328. uint16_t color, uint16_t bg) {
  329. int16_t i, j, byteWidth = (w + 7) / 8;
  330. for(j=0; j<h; j++) {
  331. for(i=0; i<w; i++ ) {
  332. if(pgm_read_byte(bitmap + j * byteWidth + i / 8) & (128 >> (i & 7))) {
  333. drawPixel(x+i, y+j, color);
  334. }
  335. else {
  336. drawPixel(x+i, y+j, bg);
  337. }
  338. }
  339. }
  340. }
  341. //Draw XBitMap Files (*.xbm), exported from GIMP,
  342. //Usage: Export from GIMP to *.xbm, rename *.xbm to *.c and open in editor.
  343. //C Array can be directly used with this function
  344. void Adafruit_GFX::drawXBitmap(int16_t x, int16_t y,
  345. const uint8_t *bitmap, int16_t w, int16_t h,
  346. uint16_t color) {
  347. int16_t i, j, byteWidth = (w + 7) / 8;
  348. for(j=0; j<h; j++) {
  349. for(i=0; i<w; i++ ) {
  350. if(pgm_read_byte(bitmap + j * byteWidth + i / 8) & (1 << (i % 8))) {
  351. drawPixel(x+i, y+j, color);
  352. }
  353. }
  354. }
  355. }
  356. #if ARDUINO >= 100
  357. size_t Adafruit_GFX::write(uint8_t c) {
  358. #else
  359. void Adafruit_GFX::write(uint8_t c) {
  360. #endif
  361. if (c == '\n') {
  362. cursor_y += textsize*8;
  363. cursor_x = 0;
  364. } else if (c == '\r') {
  365. // skip em
  366. } else {
  367. drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
  368. cursor_x += textsize*6;
  369. if (wrap && (cursor_x > (_width - textsize*6))) {
  370. cursor_y += textsize*8;
  371. cursor_x = 0;
  372. }
  373. }
  374. #if ARDUINO >= 100
  375. return 1;
  376. #endif
  377. }
  378. // Draw a character
  379. void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
  380. uint16_t color, uint16_t bg, uint8_t size) {
  381. if((x >= _width) || // Clip right
  382. (y >= _height) || // Clip bottom
  383. ((x + 6 * size - 1) < 0) || // Clip left
  384. ((y + 8 * size - 1) < 0)) // Clip top
  385. return;
  386. if(!_cp437 && (c >= 176)) c++; // Handle 'classic' charset behavior
  387. for (int8_t i=0; i<6; i++ ) {
  388. uint8_t line;
  389. if (i == 5)
  390. line = 0x0;
  391. else
  392. line = pgm_read_byte(font+(c*5)+i);
  393. for (int8_t j = 0; j<8; j++) {
  394. if (line & 0x1) {
  395. if (size == 1) // default size
  396. drawPixel(x+i, y+j, color);
  397. else { // big size
  398. fillRect(x+(i*size), y+(j*size), size, size, color);
  399. }
  400. } else if (bg != color) {
  401. if (size == 1) // default size
  402. drawPixel(x+i, y+j, bg);
  403. else { // big size
  404. fillRect(x+i*size, y+j*size, size, size, bg);
  405. }
  406. }
  407. line >>= 1;
  408. }
  409. }
  410. }
  411. void Adafruit_GFX::setCursor(int16_t x, int16_t y) {
  412. cursor_x = x;
  413. cursor_y = y;
  414. }
  415. int16_t Adafruit_GFX::getCursorX(void) const {
  416. return cursor_x;
  417. }
  418. int16_t Adafruit_GFX::getCursorY(void) const {
  419. return cursor_y;
  420. }
  421. void Adafruit_GFX::setTextSize(uint8_t s) {
  422. textsize = (s > 0) ? s : 1;
  423. }
  424. void Adafruit_GFX::setTextColor(uint16_t c) {
  425. // For 'transparent' background, we'll set the bg
  426. // to the same as fg instead of using a flag
  427. textcolor = textbgcolor = c;
  428. }
  429. void Adafruit_GFX::setTextColor(uint16_t c, uint16_t b) {
  430. textcolor = c;
  431. textbgcolor = b;
  432. }
  433. void Adafruit_GFX::setTextWrap(boolean w) {
  434. wrap = w;
  435. }
  436. uint8_t Adafruit_GFX::getRotation(void) const {
  437. return rotation;
  438. }
  439. void Adafruit_GFX::setRotation(uint8_t x) {
  440. rotation = (x & 3);
  441. switch(rotation) {
  442. case 0:
  443. case 2:
  444. _width = WIDTH;
  445. _height = HEIGHT;
  446. break;
  447. case 1:
  448. case 3:
  449. _width = HEIGHT;
  450. _height = WIDTH;
  451. break;
  452. }
  453. }
  454. // Enable (or disable) Code Page 437-compatible charset.
  455. // There was an error in glcdfont.c for the longest time -- one character
  456. // (#176, the 'light shade' block) was missing -- this threw off the index
  457. // of every character that followed it. But a TON of code has been written
  458. // with the erroneous character indices. By default, the library uses the
  459. // original 'wrong' behavior and old sketches will still work. Pass 'true'
  460. // to this function to use correct CP437 character values in your code.
  461. void Adafruit_GFX::cp437(boolean x) {
  462. _cp437 = x;
  463. }
  464. // Return the size of the display (per current rotation)
  465. int16_t Adafruit_GFX::width(void) const {
  466. return _width;
  467. }
  468. int16_t Adafruit_GFX::height(void) const {
  469. return _height;
  470. }
  471. void Adafruit_GFX::invertDisplay(boolean i) {
  472. // Do nothing, must be subclassed if supported
  473. }
  474. /***************************************************************************/
  475. // code for the GFX button UI element
  476. Adafruit_GFX_Button::Adafruit_GFX_Button(void) {
  477. _gfx = 0;
  478. }
  479. void Adafruit_GFX_Button::initButton(Adafruit_GFX *gfx,
  480. int16_t x, int16_t y,
  481. uint8_t w, uint8_t h,
  482. uint16_t outline, uint16_t fill,
  483. uint16_t textcolor,
  484. char *label, uint8_t textsize)
  485. {
  486. _x = x;
  487. _y = y;
  488. _w = w;
  489. _h = h;
  490. _outlinecolor = outline;
  491. _fillcolor = fill;
  492. _textcolor = textcolor;
  493. _textsize = textsize;
  494. _gfx = gfx;
  495. strncpy(_label, label, 9);
  496. _label[9] = 0;
  497. }
  498. void Adafruit_GFX_Button::drawButton(boolean inverted) {
  499. uint16_t fill, outline, text;
  500. if (! inverted) {
  501. fill = _fillcolor;
  502. outline = _outlinecolor;
  503. text = _textcolor;
  504. } else {
  505. fill = _textcolor;
  506. outline = _outlinecolor;
  507. text = _fillcolor;
  508. }
  509. _gfx->fillRoundRect(_x - (_w/2), _y - (_h/2), _w, _h, min(_w,_h)/4, fill);
  510. _gfx->drawRoundRect(_x - (_w/2), _y - (_h/2), _w, _h, min(_w,_h)/4, outline);
  511. _gfx->setCursor(_x - strlen(_label)*3*_textsize, _y-4*_textsize);
  512. _gfx->setTextColor(text);
  513. _gfx->setTextSize(_textsize);
  514. _gfx->print(_label);
  515. }
  516. boolean Adafruit_GFX_Button::contains(int16_t x, int16_t y) {
  517. if ((x < (_x - _w/2)) || (x > (_x + _w/2))) return false;
  518. if ((y < (_y - _h)) || (y > (_y + _h/2))) return false;
  519. return true;
  520. }
  521. void Adafruit_GFX_Button::press(boolean p) {
  522. laststate = currstate;
  523. currstate = p;
  524. }
  525. boolean Adafruit_GFX_Button::isPressed() { return currstate; }
  526. boolean Adafruit_GFX_Button::justPressed() { return (currstate && !laststate); }
  527. boolean Adafruit_GFX_Button::justReleased() { return (!currstate && laststate); }