Adafruit_GFX.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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. #define pgm_read_word(addr) (*(const unsigned short *)(addr))
  37. #endif
  38. #ifndef min
  39. #define min(a,b) (((a) < (b)) ? (a) : (b))
  40. #endif
  41. Adafruit_GFX::Adafruit_GFX(int16_t w, int16_t h):
  42. WIDTH(w), HEIGHT(h)
  43. {
  44. _width = WIDTH;
  45. _height = HEIGHT;
  46. rotation = 0;
  47. cursor_y = cursor_x = 0;
  48. textsize = 1;
  49. textcolor = textbgcolor = 0xFFFF;
  50. wrap = true;
  51. _cp437 = false;
  52. gfxFont = NULL;
  53. }
  54. // Draw a circle outline
  55. void Adafruit_GFX::drawCircle(int16_t x0, int16_t y0, int16_t r,
  56. uint16_t color) {
  57. int16_t f = 1 - r;
  58. int16_t ddF_x = 1;
  59. int16_t ddF_y = -2 * r;
  60. int16_t x = 0;
  61. int16_t y = r;
  62. drawPixel(x0 , y0+r, color);
  63. drawPixel(x0 , y0-r, color);
  64. drawPixel(x0+r, y0 , color);
  65. drawPixel(x0-r, y0 , color);
  66. while (x<y) {
  67. if (f >= 0) {
  68. y--;
  69. ddF_y += 2;
  70. f += ddF_y;
  71. }
  72. x++;
  73. ddF_x += 2;
  74. f += ddF_x;
  75. drawPixel(x0 + x, y0 + y, color);
  76. drawPixel(x0 - x, y0 + y, color);
  77. drawPixel(x0 + x, y0 - y, color);
  78. drawPixel(x0 - x, y0 - y, color);
  79. drawPixel(x0 + y, y0 + x, color);
  80. drawPixel(x0 - y, y0 + x, color);
  81. drawPixel(x0 + y, y0 - x, color);
  82. drawPixel(x0 - y, y0 - x, color);
  83. }
  84. }
  85. void Adafruit_GFX::drawCircleHelper( int16_t x0, int16_t y0,
  86. int16_t r, uint8_t cornername, uint16_t color) {
  87. int16_t f = 1 - r;
  88. int16_t ddF_x = 1;
  89. int16_t ddF_y = -2 * r;
  90. int16_t x = 0;
  91. int16_t y = r;
  92. while (x<y) {
  93. if (f >= 0) {
  94. y--;
  95. ddF_y += 2;
  96. f += ddF_y;
  97. }
  98. x++;
  99. ddF_x += 2;
  100. f += ddF_x;
  101. if (cornername & 0x4) {
  102. drawPixel(x0 + x, y0 + y, color);
  103. drawPixel(x0 + y, y0 + x, color);
  104. }
  105. if (cornername & 0x2) {
  106. drawPixel(x0 + x, y0 - y, color);
  107. drawPixel(x0 + y, y0 - x, color);
  108. }
  109. if (cornername & 0x8) {
  110. drawPixel(x0 - y, y0 + x, color);
  111. drawPixel(x0 - x, y0 + y, color);
  112. }
  113. if (cornername & 0x1) {
  114. drawPixel(x0 - y, y0 - x, color);
  115. drawPixel(x0 - x, y0 - y, color);
  116. }
  117. }
  118. }
  119. void Adafruit_GFX::fillCircle(int16_t x0, int16_t y0, int16_t r,
  120. uint16_t color) {
  121. drawFastVLine(x0, y0-r, 2*r+1, color);
  122. fillCircleHelper(x0, y0, r, 3, 0, color);
  123. }
  124. // Used to do circles and roundrects
  125. void Adafruit_GFX::fillCircleHelper(int16_t x0, int16_t y0, int16_t r,
  126. uint8_t cornername, int16_t delta, uint16_t color) {
  127. int16_t f = 1 - r;
  128. int16_t ddF_x = 1;
  129. int16_t ddF_y = -2 * r;
  130. int16_t x = 0;
  131. int16_t y = r;
  132. while (x<y) {
  133. if (f >= 0) {
  134. y--;
  135. ddF_y += 2;
  136. f += ddF_y;
  137. }
  138. x++;
  139. ddF_x += 2;
  140. f += ddF_x;
  141. if (cornername & 0x1) {
  142. drawFastVLine(x0+x, y0-y, 2*y+1+delta, color);
  143. drawFastVLine(x0+y, y0-x, 2*x+1+delta, color);
  144. }
  145. if (cornername & 0x2) {
  146. drawFastVLine(x0-x, y0-y, 2*y+1+delta, color);
  147. drawFastVLine(x0-y, y0-x, 2*x+1+delta, color);
  148. }
  149. }
  150. }
  151. // Bresenham's algorithm - thx wikpedia
  152. void Adafruit_GFX::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
  153. uint16_t color) {
  154. int16_t steep = abs(y1 - y0) > abs(x1 - x0);
  155. if (steep) {
  156. adagfxswap(x0, y0);
  157. adagfxswap(x1, y1);
  158. }
  159. if (x0 > x1) {
  160. adagfxswap(x0, x1);
  161. adagfxswap(y0, y1);
  162. }
  163. int16_t dx, dy;
  164. dx = x1 - x0;
  165. dy = abs(y1 - y0);
  166. int16_t err = dx / 2;
  167. int16_t ystep;
  168. if (y0 < y1) {
  169. ystep = 1;
  170. } else {
  171. ystep = -1;
  172. }
  173. for (; x0<=x1; x0++) {
  174. if (steep) {
  175. drawPixel(y0, x0, color);
  176. } else {
  177. drawPixel(x0, y0, color);
  178. }
  179. err -= dy;
  180. if (err < 0) {
  181. y0 += ystep;
  182. err += dx;
  183. }
  184. }
  185. }
  186. // Draw a rectangle
  187. void Adafruit_GFX::drawRect(int16_t x, int16_t y, 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, int16_t x2, int16_t y2, uint16_t color) {
  240. drawLine(x0, y0, x1, y1, color);
  241. drawLine(x1, y1, x2, y2, color);
  242. drawLine(x2, y2, x0, y0, color);
  243. }
  244. // Fill a triangle
  245. void Adafruit_GFX::fillTriangle(int16_t x0, int16_t y0,
  246. int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color) {
  247. int16_t a, b, y, last;
  248. // Sort coordinates by Y order (y2 >= y1 >= y0)
  249. if (y0 > y1) {
  250. adagfxswap(y0, y1); adagfxswap(x0, x1);
  251. }
  252. if (y1 > y2) {
  253. adagfxswap(y2, y1); adagfxswap(x2, x1);
  254. }
  255. if (y0 > y1) {
  256. adagfxswap(y0, y1); adagfxswap(x0, x1);
  257. }
  258. if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing
  259. a = b = x0;
  260. if(x1 < a) a = x1;
  261. else if(x1 > b) b = x1;
  262. if(x2 < a) a = x2;
  263. else if(x2 > b) b = x2;
  264. drawFastHLine(a, y0, b-a+1, color);
  265. return;
  266. }
  267. int16_t
  268. dx01 = x1 - x0,
  269. dy01 = y1 - y0,
  270. dx02 = x2 - x0,
  271. dy02 = y2 - y0,
  272. dx12 = x2 - x1,
  273. dy12 = y2 - y1;
  274. int32_t
  275. sa = 0,
  276. sb = 0;
  277. // For upper part of triangle, find scanline crossings for segments
  278. // 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1
  279. // is included here (and second loop will be skipped, avoiding a /0
  280. // error there), otherwise scanline y1 is skipped here and handled
  281. // in the second loop...which also avoids a /0 error here if y0=y1
  282. // (flat-topped triangle).
  283. if(y1 == y2) last = y1; // Include y1 scanline
  284. else last = y1-1; // Skip it
  285. for(y=y0; y<=last; y++) {
  286. a = x0 + sa / dy01;
  287. b = x0 + sb / dy02;
  288. sa += dx01;
  289. sb += dx02;
  290. /* longhand:
  291. a = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
  292. b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
  293. */
  294. if(a > b) adagfxswap(a,b);
  295. drawFastHLine(a, y, b-a+1, color);
  296. }
  297. // For lower part of triangle, find scanline crossings for segments
  298. // 0-2 and 1-2. This loop is skipped if y1=y2.
  299. sa = dx12 * (y - y1);
  300. sb = dx02 * (y - y0);
  301. for(; y<=y2; y++) {
  302. a = x1 + sa / dy12;
  303. b = x0 + sb / dy02;
  304. sa += dx12;
  305. sb += dx02;
  306. /* longhand:
  307. a = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
  308. b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
  309. */
  310. if(a > b) adagfxswap(a,b);
  311. drawFastHLine(a, y, b-a+1, color);
  312. }
  313. }
  314. void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
  315. const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) {
  316. int16_t i, j, byteWidth = (w + 7) / 8;
  317. for(j=0; j<h; j++) {
  318. for(i=0; i<w; i++ ) {
  319. if(pgm_read_byte(bitmap + j * byteWidth + i / 8) & (128 >> (i & 7))) {
  320. drawPixel(x+i, y+j, color);
  321. }
  322. }
  323. }
  324. }
  325. // Draw a 1-bit color bitmap at the specified x, y position from the
  326. // provided bitmap buffer (must be PROGMEM memory) using color as the
  327. // foreground color and bg as the background color.
  328. void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
  329. const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg) {
  330. int16_t i, j, byteWidth = (w + 7) / 8;
  331. for(j=0; j<h; j++) {
  332. for(i=0; i<w; i++ ) {
  333. if(pgm_read_byte(bitmap + j * byteWidth + i / 8) & (128 >> (i & 7))) {
  334. drawPixel(x+i, y+j, color);
  335. }
  336. else {
  337. drawPixel(x+i, y+j, bg);
  338. }
  339. }
  340. }
  341. }
  342. //Draw XBitMap Files (*.xbm), exported from GIMP,
  343. //Usage: Export from GIMP to *.xbm, rename *.xbm to *.c and open in editor.
  344. //C Array can be directly used with this function
  345. void Adafruit_GFX::drawXBitmap(int16_t x, int16_t y,
  346. const uint8_t *bitmap, int16_t w, int16_t h, 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(!gfxFont) { // 'Classic' built-in font
  362. if(c == '\n') {
  363. cursor_y += textsize*8;
  364. cursor_x = 0;
  365. } else if(c == '\r') {
  366. // skip em
  367. } else {
  368. if(wrap && ((cursor_x + textsize * 6) >= _width)) { // Heading off edge?
  369. cursor_x = 0; // Reset x to zero
  370. cursor_y += textsize * 8; // Advance y one line
  371. }
  372. drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
  373. cursor_x += textsize * 6;
  374. }
  375. } else { // Custom font
  376. if(c == '\n') {
  377. cursor_x = 0;
  378. cursor_y += (int16_t)textsize *
  379. (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
  380. } else if(c != '\r') {
  381. uint8_t first = pgm_read_byte(&gfxFont->first);
  382. if((c >= first) && (c <= (uint8_t)pgm_read_byte(&gfxFont->last))) {
  383. uint8_t c2 = c - pgm_read_byte(&gfxFont->first);
  384. GFXglyph *glyph = &(((GFXglyph *)pgm_read_word(&gfxFont->glyph))[c2]);
  385. uint8_t w = pgm_read_byte(&glyph->width),
  386. h = pgm_read_byte(&glyph->height);
  387. if((w > 0) && (h > 0)) { // Is there an associated bitmap?
  388. int16_t xo = (int8_t)pgm_read_byte(&glyph->xOffset); // sic
  389. if(wrap && ((cursor_x + textsize * (xo + w)) >= _width)) {
  390. // Drawing character would go off right edge; wrap to new line
  391. cursor_x = 0;
  392. cursor_y += (int16_t)textsize *
  393. (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
  394. }
  395. drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
  396. }
  397. cursor_x += pgm_read_byte(&glyph->xAdvance) * (int16_t)textsize;
  398. }
  399. }
  400. }
  401. #if ARDUINO >= 100
  402. return 1;
  403. #endif
  404. }
  405. // Draw a character
  406. void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
  407. uint16_t color, uint16_t bg, uint8_t size) {
  408. if(!gfxFont) { // 'Classic' built-in font
  409. if((x >= _width) || // Clip right
  410. (y >= _height) || // Clip bottom
  411. ((x + 6 * size - 1) < 0) || // Clip left
  412. ((y + 8 * size - 1) < 0)) // Clip top
  413. return;
  414. if(!_cp437 && (c >= 176)) c++; // Handle 'classic' charset behavior
  415. for(int8_t i=0; i<6; i++ ) {
  416. uint8_t line;
  417. if(i < 5) line = pgm_read_byte(font+(c*5)+i);
  418. else line = 0x0;
  419. for(int8_t j=0; j<8; j++, line >>= 1) {
  420. if(line & 0x1) {
  421. if(size == 1) drawPixel(x+i, y+j, color);
  422. else fillRect(x+(i*size), y+(j*size), size, size, color);
  423. } else if(bg != color) {
  424. if(size == 1) drawPixel(x+i, y+j, bg);
  425. else fillRect(x+i*size, y+j*size, size, size, bg);
  426. }
  427. }
  428. }
  429. } else { // Custom font
  430. // Character is assumed previously filtered by write() to eliminate
  431. // newlines, returns, non-printable characters, etc. Calling drawChar()
  432. // directly with 'bad' characters of font may cause mayhem!
  433. c -= pgm_read_byte(&gfxFont->first);
  434. GFXglyph *glyph = &(((GFXglyph *)pgm_read_word(&gfxFont->glyph))[c]);
  435. uint8_t *bitmap = (uint8_t *)pgm_read_word(&gfxFont->bitmap);
  436. uint16_t bo = pgm_read_word(&glyph->bitmapOffset);
  437. uint8_t w = pgm_read_byte(&glyph->width),
  438. h = pgm_read_byte(&glyph->height),
  439. xa = pgm_read_byte(&glyph->xAdvance);
  440. int8_t xo = pgm_read_byte(&glyph->xOffset),
  441. yo = pgm_read_byte(&glyph->yOffset);
  442. uint8_t xx, yy, bits, bit=0;
  443. int16_t xo16, yo16;
  444. if(size > 1) {
  445. xo16 = xo;
  446. yo16 = yo;
  447. }
  448. // Todo: Add character clipping here
  449. // NOTE: THERE IS NO 'BACKGROUND' COLOR OPTION ON CUSTOM FONTS.
  450. // THIS IS ON PURPOSE AND BY DESIGN. The background color feature
  451. // has typically been used with the 'classic' font to overwrite old
  452. // screen contents with new data. This ONLY works because the
  453. // characters are a uniform size; it's not a sensible thing to do with
  454. // proportionally-spaced fonts with glyphs of varying sizes (and that
  455. // may overlap). To replace previously-drawn text when using a custom
  456. // font, use the getTextBounds() function to determine the smallest
  457. // rectangle encompassing a string, erase the area with fillRect(),
  458. // then draw new text. This WILL infortunately 'blink' the text, but
  459. // is unavoidable. Drawing 'background' pixels will NOT fix this,
  460. // only creates a new set of problems. Have an idea to work around
  461. // this (a canvas object type for MCUs that can afford the RAM and
  462. // displays supporting setAddrWindow() and pushColors()), but haven't
  463. // implemented this yet.
  464. for(yy=0; yy<h; yy++) {
  465. for(xx=0; xx<w; xx++) {
  466. if(bit == 0) {
  467. bits = pgm_read_byte(&bitmap[bo++]);
  468. bit = 0x80;
  469. }
  470. if(bits & bit) {
  471. if(size == 1) {
  472. drawPixel(x+xo+xx, y+yo+yy, color);
  473. } else {
  474. fillRect(x+(xo16+xx)*size, y+(yo16+yy)*size, size, size, color);
  475. }
  476. }
  477. bit >>= 1;
  478. }
  479. }
  480. } // End classic vs custom font
  481. }
  482. void Adafruit_GFX::setCursor(int16_t x, int16_t y) {
  483. cursor_x = x;
  484. cursor_y = y;
  485. }
  486. int16_t Adafruit_GFX::getCursorX(void) const {
  487. return cursor_x;
  488. }
  489. int16_t Adafruit_GFX::getCursorY(void) const {
  490. return cursor_y;
  491. }
  492. void Adafruit_GFX::setTextSize(uint8_t s) {
  493. textsize = (s > 0) ? s : 1;
  494. }
  495. void Adafruit_GFX::setTextColor(uint16_t c) {
  496. // For 'transparent' background, we'll set the bg
  497. // to the same as fg instead of using a flag
  498. textcolor = textbgcolor = c;
  499. }
  500. void Adafruit_GFX::setTextColor(uint16_t c, uint16_t b) {
  501. textcolor = c;
  502. textbgcolor = b;
  503. }
  504. void Adafruit_GFX::setTextWrap(boolean w) {
  505. wrap = w;
  506. }
  507. uint8_t Adafruit_GFX::getRotation(void) const {
  508. return rotation;
  509. }
  510. void Adafruit_GFX::setRotation(uint8_t x) {
  511. rotation = (x & 3);
  512. switch(rotation) {
  513. case 0:
  514. case 2:
  515. _width = WIDTH;
  516. _height = HEIGHT;
  517. break;
  518. case 1:
  519. case 3:
  520. _width = HEIGHT;
  521. _height = WIDTH;
  522. break;
  523. }
  524. }
  525. // Enable (or disable) Code Page 437-compatible charset.
  526. // There was an error in glcdfont.c for the longest time -- one character
  527. // (#176, the 'light shade' block) was missing -- this threw off the index
  528. // of every character that followed it. But a TON of code has been written
  529. // with the erroneous character indices. By default, the library uses the
  530. // original 'wrong' behavior and old sketches will still work. Pass 'true'
  531. // to this function to use correct CP437 character values in your code.
  532. void Adafruit_GFX::cp437(boolean x) {
  533. _cp437 = x;
  534. }
  535. void Adafruit_GFX::setFont(const GFXfont *f) {
  536. if(f) { // Font struct pointer passed in?
  537. if(!gfxFont) { // And no current font struct?
  538. // Switching from classic to new font behavior.
  539. // Move cursor pos down 6 pixels so it's on baseline.
  540. cursor_y += 6;
  541. }
  542. } else if(gfxFont) { // NULL passed. Current font struct defined?
  543. // Switching from new to classic font behavior.
  544. // Move cursor pos up 6 pixels so it's at top-left of char.
  545. cursor_y -= 6;
  546. }
  547. gfxFont = (GFXfont *)f;
  548. }
  549. // Pass string and a cursor position, returns UL corner and W,H.
  550. void Adafruit_GFX::getTextBounds(char *str, int16_t x, int16_t y,
  551. int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
  552. uint8_t c; // Current character
  553. *x1 = x;
  554. *y1 = y;
  555. *w = *h = 0;
  556. if(gfxFont) {
  557. GFXglyph *glyph;
  558. uint8_t first = pgm_read_byte(&gfxFont->first),
  559. last = pgm_read_byte(&gfxFont->last),
  560. gw, gh, xa;
  561. int8_t xo, yo;
  562. int16_t minx = _width, miny = _height, maxx = -1, maxy = -1,
  563. gx1, gy1, gx2, gy2, ts = (int16_t)textsize,
  564. ya = ts * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
  565. while((c = *str++)) {
  566. if(c != '\n') { // Not a newline
  567. if(c != '\r') { // Not a carriage return, is normal char
  568. if((c >= first) && (c <= last)) { // Char present in current font
  569. c -= first;
  570. glyph = &(((GFXglyph *)pgm_read_word(&gfxFont->glyph))[c]);
  571. gw = pgm_read_byte(&glyph->width);
  572. gh = pgm_read_byte(&glyph->height);
  573. xa = pgm_read_byte(&glyph->xAdvance);
  574. xo = pgm_read_byte(&glyph->xOffset);
  575. yo = pgm_read_byte(&glyph->yOffset);
  576. if(wrap && ((x + (((int16_t)xo + gw) * ts)) >= _width)) {
  577. // Line wrap
  578. x = 0; // Reset x to 0
  579. y += ya; // Advance y by 1 line
  580. }
  581. gx1 = x + xo * ts;
  582. gy1 = y + yo * ts;
  583. gx2 = gx1 + gw * ts - 1;
  584. gy2 = gy1 + gh * ts - 1;
  585. if(gx1 < minx) minx = gx1;
  586. if(gy1 < miny) miny = gy1;
  587. if(gx2 > maxx) maxx = gx2;
  588. if(gy2 > maxy) maxy = gy2;
  589. x += xa * ts;
  590. }
  591. } // Carriage return = do nothing
  592. } else { // Newline
  593. x = 0; // Reset x
  594. y += ya; // Advance y by 1 line
  595. }
  596. }
  597. // End of string
  598. *x1 = minx;
  599. *y1 = miny;
  600. if(maxx >= minx) *w = maxx - minx + 1;
  601. if(maxy >= miny) *h = maxy - miny + 1;
  602. } else { // Default font
  603. uint16_t lineWidth = 0, maxWidth = 0; // Width of current, all lines
  604. while((c = *str++)) {
  605. if(c != '\n') { // Not a newline
  606. if(c != '\r') { // Not a carriage return, is normal char
  607. if(wrap && ((x + textsize * 6) >= _width)) {
  608. x = 0; // Reset x to 0
  609. y += textsize * 8; // Advance y by 1 line
  610. if(lineWidth > maxWidth) maxWidth = lineWidth; // Save widest line
  611. lineWidth = textsize * 6; // First char on new line
  612. } else { // No line wrap, just keep incrementing X
  613. lineWidth += textsize * 6; // Includes interchar x gap
  614. }
  615. } // Carriage return = do nothing
  616. } else { // Newline
  617. x = 0; // Reset x to 0
  618. y += textsize * 8; // Advance y by 1 line
  619. if(lineWidth > maxWidth) maxWidth = lineWidth; // Save widest line
  620. lineWidth = 0; // Reset lineWidth for new line
  621. }
  622. }
  623. // End of string
  624. if(lineWidth) y += textsize * 8; // Add height of last (or only) line
  625. *w = maxWidth - 1; // Don't include last interchar x gap
  626. *h = y - *y1;
  627. } // End classic vs custom font
  628. }
  629. // Same as above, but for PROGMEM strings
  630. void Adafruit_GFX::getTextBounds(const __FlashStringHelper *str,
  631. int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
  632. uint8_t *s = (uint8_t *)str, c;
  633. *x1 = x;
  634. *y1 = y;
  635. *w = *h = 0;
  636. if(gfxFont) {
  637. GFXglyph *glyph;
  638. uint8_t first = pgm_read_byte(&gfxFont->first),
  639. last = pgm_read_byte(&gfxFont->last),
  640. gw, gh, xa;
  641. int8_t xo, yo;
  642. int16_t minx = _width, miny = _height, maxx = -1, maxy = -1,
  643. gx1, gy1, gx2, gy2, ts = (int16_t)textsize,
  644. ya = ts * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
  645. while((c = pgm_read_byte(s++))) {
  646. if(c != '\n') { // Not a newline
  647. if(c != '\r') { // Not a carriage return, is normal char
  648. if((c >= first) && (c <= last)) { // Char present in current font
  649. c -= first;
  650. glyph = &(((GFXglyph *)pgm_read_word(&gfxFont->glyph))[c]);
  651. gw = pgm_read_byte(&glyph->width);
  652. gh = pgm_read_byte(&glyph->height);
  653. xa = pgm_read_byte(&glyph->xAdvance);
  654. xo = pgm_read_byte(&glyph->xOffset);
  655. yo = pgm_read_byte(&glyph->yOffset);
  656. if(wrap && ((x + (((int16_t)xo + gw) * ts)) >= _width)) {
  657. // Line wrap
  658. x = 0; // Reset x to 0
  659. y += ya; // Advance y by 1 line
  660. }
  661. gx1 = x + xo * ts;
  662. gy1 = y + yo * ts;
  663. gx2 = gx1 + gw * ts - 1;
  664. gy2 = gy1 + gh * ts - 1;
  665. if(gx1 < minx) minx = gx1;
  666. if(gy1 < miny) miny = gy1;
  667. if(gx2 > maxx) maxx = gx2;
  668. if(gy2 > maxy) maxy = gy2;
  669. x += xa * ts;
  670. }
  671. } // Carriage return = do nothing
  672. } else { // Newline
  673. x = 0; // Reset x
  674. y += ya; // Advance y by 1 line
  675. }
  676. }
  677. // End of string
  678. *x1 = minx;
  679. *y1 = miny;
  680. if(maxx >= minx) *w = maxx - minx + 1;
  681. if(maxy >= miny) *h = maxy - miny + 1;
  682. } else { // Default font
  683. uint16_t lineWidth = 0, maxWidth = 0; // Width of current, all lines
  684. while((c = pgm_read_byte(s++))) {
  685. if(c != '\n') { // Not a newline
  686. if(c != '\r') { // Not a carriage return, is normal char
  687. if(wrap && ((x + textsize * 6) >= _width)) {
  688. x = 0; // Reset x to 0
  689. y += textsize * 8; // Advance y by 1 line
  690. if(lineWidth > maxWidth) maxWidth = lineWidth; // Save widest line
  691. lineWidth = textsize * 6; // First char on new line
  692. } else { // No line wrap, just keep incrementing X
  693. lineWidth += textsize * 6; // Includes interchar x gap
  694. }
  695. } // Carriage return = do nothing
  696. } else { // Newline
  697. x = 0; // Reset x to 0
  698. y += textsize * 8; // Advance y by 1 line
  699. if(lineWidth > maxWidth) maxWidth = lineWidth; // Save widest line
  700. lineWidth = 0; // Reset lineWidth for new line
  701. }
  702. }
  703. // End of string
  704. if(lineWidth) y += textsize * 8; // Add height of last (or only) line
  705. *w = maxWidth - 1; // Don't include last interchar x gap
  706. *h = y - *y1;
  707. } // End classic vs custom font
  708. }
  709. // Return the size of the display (per current rotation)
  710. int16_t Adafruit_GFX::width(void) const {
  711. return _width;
  712. }
  713. int16_t Adafruit_GFX::height(void) const {
  714. return _height;
  715. }
  716. void Adafruit_GFX::invertDisplay(boolean i) {
  717. // Do nothing, must be subclassed if supported by hardware
  718. }
  719. /***************************************************************************/
  720. // code for the GFX button UI element
  721. Adafruit_GFX_Button::Adafruit_GFX_Button(void) {
  722. _gfx = 0;
  723. }
  724. void Adafruit_GFX_Button::initButton(
  725. Adafruit_GFX *gfx, int16_t x, int16_t y, uint8_t w, uint8_t h,
  726. uint16_t outline, uint16_t fill, uint16_t textcolor,
  727. char *label, uint8_t textsize)
  728. {
  729. _x = x;
  730. _y = y;
  731. _w = w;
  732. _h = h;
  733. _outlinecolor = outline;
  734. _fillcolor = fill;
  735. _textcolor = textcolor;
  736. _textsize = textsize;
  737. _gfx = gfx;
  738. strncpy(_label, label, 9);
  739. _label[9] = 0;
  740. }
  741. void Adafruit_GFX_Button::drawButton(boolean inverted) {
  742. uint16_t fill, outline, text;
  743. if(!inverted) {
  744. fill = _fillcolor;
  745. outline = _outlinecolor;
  746. text = _textcolor;
  747. } else {
  748. fill = _textcolor;
  749. outline = _outlinecolor;
  750. text = _fillcolor;
  751. }
  752. _gfx->fillRoundRect(_x - (_w/2), _y - (_h/2), _w, _h, min(_w,_h)/4, fill);
  753. _gfx->drawRoundRect(_x - (_w/2), _y - (_h/2), _w, _h, min(_w,_h)/4, outline);
  754. _gfx->setCursor(_x - strlen(_label)*3*_textsize, _y-4*_textsize);
  755. _gfx->setTextColor(text);
  756. _gfx->setTextSize(_textsize);
  757. _gfx->print(_label);
  758. }
  759. boolean Adafruit_GFX_Button::contains(int16_t x, int16_t y) {
  760. if ((x < (_x - _w/2)) || (x > (_x + _w/2))) return false;
  761. if ((y < (_y - _h/2)) || (y > (_y + _h/2))) return false;
  762. return true;
  763. }
  764. void Adafruit_GFX_Button::press(boolean p) {
  765. laststate = currstate;
  766. currstate = p;
  767. }
  768. boolean Adafruit_GFX_Button::isPressed() { return currstate; }
  769. boolean Adafruit_GFX_Button::justPressed() { return (currstate && !laststate); }
  770. boolean Adafruit_GFX_Button::justReleased() { return (!currstate && laststate); }