Adafruit_GFX.cpp 16 KB

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