Adafruit_GFX.cpp 26 KB

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