Adafruit_GFX.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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. // Draw a 1-bit image (bitmap) at the specified (x,y) position from the
  315. // provided bitmap buffer (must be PROGMEM memory) using the specified
  316. // foreground color (unset bits are transparent).
  317. void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
  318. const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) {
  319. int16_t i, j, byteWidth = (w + 7) / 8;
  320. uint8_t byte;
  321. for(j=0; j<h; j++) {
  322. for(i=0; i<w; i++) {
  323. if(i & 7) byte <<= 1;
  324. else byte = pgm_read_byte(bitmap + j * byteWidth + i / 8);
  325. if(byte & 0x80) drawPixel(x+i, y+j, color);
  326. }
  327. }
  328. }
  329. // Draw a 1-bit image (bitmap) at the specified (x,y) position from the
  330. // provided bitmap buffer (must be PROGMEM memory) using the specified
  331. // foreground (for set bits) and background (for clear bits) colors.
  332. void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
  333. const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg) {
  334. int16_t i, j, byteWidth = (w + 7) / 8;
  335. uint8_t byte;
  336. for(j=0; j<h; j++) {
  337. for(i=0; i<w; i++ ) {
  338. if(i & 7) byte <<= 1;
  339. else byte = pgm_read_byte(bitmap + j * byteWidth + i / 8);
  340. if(byte & 0x80) drawPixel(x+i, y+j, color);
  341. else drawPixel(x+i, y+j, bg);
  342. }
  343. }
  344. }
  345. // drawBitmap() variant for RAM-resident (not PROGMEM) bitmaps.
  346. void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
  347. uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) {
  348. int16_t i, j, byteWidth = (w + 7) / 8;
  349. uint8_t byte;
  350. for(j=0; j<h; j++) {
  351. for(i=0; i<w; i++ ) {
  352. if(i & 7) byte <<= 1;
  353. else byte = bitmap[j * byteWidth + i / 8];
  354. if(byte & 0x80) drawPixel(x+i, y+j, color);
  355. }
  356. }
  357. }
  358. // drawBitmap() variant w/background for RAM-resident (not PROGMEM) bitmaps.
  359. void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
  360. uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg) {
  361. int16_t i, j, byteWidth = (w + 7) / 8;
  362. uint8_t byte;
  363. for(j=0; j<h; j++) {
  364. for(i=0; i<w; i++ ) {
  365. if(i & 7) byte <<= 1;
  366. else byte = bitmap[j * byteWidth + i / 8];
  367. if(byte & 0x80) drawPixel(x+i, y+j, color);
  368. else drawPixel(x+i, y+j, bg);
  369. }
  370. }
  371. }
  372. //Draw XBitMap Files (*.xbm), exported from GIMP,
  373. //Usage: Export from GIMP to *.xbm, rename *.xbm to *.c and open in editor.
  374. //C Array can be directly used with this function
  375. void Adafruit_GFX::drawXBitmap(int16_t x, int16_t y,
  376. const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) {
  377. int16_t i, j, byteWidth = (w + 7) / 8;
  378. uint8_t byte;
  379. for(j=0; j<h; j++) {
  380. for(i=0; i<w; i++ ) {
  381. if(i & 7) byte >>= 1;
  382. else byte = pgm_read_byte(bitmap + j * byteWidth + i / 8);
  383. if(byte & 0x01) drawPixel(x+i, y+j, color);
  384. }
  385. }
  386. }
  387. #if ARDUINO >= 100
  388. size_t Adafruit_GFX::write(uint8_t c) {
  389. #else
  390. void Adafruit_GFX::write(uint8_t c) {
  391. #endif
  392. if(!gfxFont) { // 'Classic' built-in font
  393. if(c == '\n') {
  394. cursor_y += textsize*8;
  395. cursor_x = 0;
  396. } else if(c == '\r') {
  397. // skip em
  398. } else {
  399. if(wrap && ((cursor_x + textsize * 6) >= _width)) { // Heading off edge?
  400. cursor_x = 0; // Reset x to zero
  401. cursor_y += textsize * 8; // Advance y one line
  402. }
  403. drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
  404. cursor_x += textsize * 6;
  405. }
  406. } else { // Custom font
  407. if(c == '\n') {
  408. cursor_x = 0;
  409. cursor_y += (int16_t)textsize *
  410. (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
  411. } else if(c != '\r') {
  412. uint8_t first = pgm_read_byte(&gfxFont->first);
  413. if((c >= first) && (c <= (uint8_t)pgm_read_byte(&gfxFont->last))) {
  414. uint8_t c2 = c - pgm_read_byte(&gfxFont->first);
  415. GFXglyph *glyph = &(((GFXglyph *)pgm_read_word(&gfxFont->glyph))[c2]);
  416. uint8_t w = pgm_read_byte(&glyph->width),
  417. h = pgm_read_byte(&glyph->height);
  418. if((w > 0) && (h > 0)) { // Is there an associated bitmap?
  419. int16_t xo = (int8_t)pgm_read_byte(&glyph->xOffset); // sic
  420. if(wrap && ((cursor_x + textsize * (xo + w)) >= _width)) {
  421. // Drawing character would go off right edge; wrap to new line
  422. cursor_x = 0;
  423. cursor_y += (int16_t)textsize *
  424. (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
  425. }
  426. drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
  427. }
  428. cursor_x += pgm_read_byte(&glyph->xAdvance) * (int16_t)textsize;
  429. }
  430. }
  431. }
  432. #if ARDUINO >= 100
  433. return 1;
  434. #endif
  435. }
  436. // Draw a character
  437. void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
  438. uint16_t color, uint16_t bg, uint8_t size) {
  439. if(!gfxFont) { // 'Classic' built-in font
  440. if((x >= _width) || // Clip right
  441. (y >= _height) || // Clip bottom
  442. ((x + 6 * size - 1) < 0) || // Clip left
  443. ((y + 8 * size - 1) < 0)) // Clip top
  444. return;
  445. if(!_cp437 && (c >= 176)) c++; // Handle 'classic' charset behavior
  446. for(int8_t i=0; i<6; i++ ) {
  447. uint8_t line;
  448. if(i < 5) line = pgm_read_byte(font+(c*5)+i);
  449. else line = 0x0;
  450. for(int8_t j=0; j<8; j++, line >>= 1) {
  451. if(line & 0x1) {
  452. if(size == 1) drawPixel(x+i, y+j, color);
  453. else fillRect(x+(i*size), y+(j*size), size, size, color);
  454. } else if(bg != color) {
  455. if(size == 1) drawPixel(x+i, y+j, bg);
  456. else fillRect(x+i*size, y+j*size, size, size, bg);
  457. }
  458. }
  459. }
  460. } else { // Custom font
  461. // Character is assumed previously filtered by write() to eliminate
  462. // newlines, returns, non-printable characters, etc. Calling drawChar()
  463. // directly with 'bad' characters of font may cause mayhem!
  464. c -= pgm_read_byte(&gfxFont->first);
  465. GFXglyph *glyph = &(((GFXglyph *)pgm_read_word(&gfxFont->glyph))[c]);
  466. uint8_t *bitmap = (uint8_t *)pgm_read_word(&gfxFont->bitmap);
  467. uint16_t bo = pgm_read_word(&glyph->bitmapOffset);
  468. uint8_t w = pgm_read_byte(&glyph->width),
  469. h = pgm_read_byte(&glyph->height),
  470. xa = pgm_read_byte(&glyph->xAdvance);
  471. int8_t xo = pgm_read_byte(&glyph->xOffset),
  472. yo = pgm_read_byte(&glyph->yOffset);
  473. uint8_t xx, yy, bits, bit = 0;
  474. int16_t xo16, yo16;
  475. if(size > 1) {
  476. xo16 = xo;
  477. yo16 = yo;
  478. }
  479. // Todo: Add character clipping here
  480. // NOTE: THERE IS NO 'BACKGROUND' COLOR OPTION ON CUSTOM FONTS.
  481. // THIS IS ON PURPOSE AND BY DESIGN. The background color feature
  482. // has typically been used with the 'classic' font to overwrite old
  483. // screen contents with new data. This ONLY works because the
  484. // characters are a uniform size; it's not a sensible thing to do with
  485. // proportionally-spaced fonts with glyphs of varying sizes (and that
  486. // may overlap). To replace previously-drawn text when using a custom
  487. // font, use the getTextBounds() function to determine the smallest
  488. // rectangle encompassing a string, erase the area with fillRect(),
  489. // then draw new text. This WILL infortunately 'blink' the text, but
  490. // is unavoidable. Drawing 'background' pixels will NOT fix this,
  491. // only creates a new set of problems. Have an idea to work around
  492. // this (a canvas object type for MCUs that can afford the RAM and
  493. // displays supporting setAddrWindow() and pushColors()), but haven't
  494. // implemented this yet.
  495. for(yy=0; yy<h; yy++) {
  496. for(xx=0; xx<w; xx++) {
  497. if(!(bit++ & 7)) {
  498. bits = pgm_read_byte(&bitmap[bo++]);
  499. }
  500. if(bits & 0x80) {
  501. if(size == 1) {
  502. drawPixel(x+xo+xx, y+yo+yy, color);
  503. } else {
  504. fillRect(x+(xo16+xx)*size, y+(yo16+yy)*size, size, size, color);
  505. }
  506. }
  507. bits <<= 1;
  508. }
  509. }
  510. } // End classic vs custom font
  511. }
  512. void Adafruit_GFX::setCursor(int16_t x, int16_t y) {
  513. cursor_x = x;
  514. cursor_y = y;
  515. }
  516. int16_t Adafruit_GFX::getCursorX(void) const {
  517. return cursor_x;
  518. }
  519. int16_t Adafruit_GFX::getCursorY(void) const {
  520. return cursor_y;
  521. }
  522. void Adafruit_GFX::setTextSize(uint8_t s) {
  523. textsize = (s > 0) ? s : 1;
  524. }
  525. void Adafruit_GFX::setTextColor(uint16_t c) {
  526. // For 'transparent' background, we'll set the bg
  527. // to the same as fg instead of using a flag
  528. textcolor = textbgcolor = c;
  529. }
  530. void Adafruit_GFX::setTextColor(uint16_t c, uint16_t b) {
  531. textcolor = c;
  532. textbgcolor = b;
  533. }
  534. void Adafruit_GFX::setTextWrap(boolean w) {
  535. wrap = w;
  536. }
  537. uint8_t Adafruit_GFX::getRotation(void) const {
  538. return rotation;
  539. }
  540. void Adafruit_GFX::setRotation(uint8_t x) {
  541. rotation = (x & 3);
  542. switch(rotation) {
  543. case 0:
  544. case 2:
  545. _width = WIDTH;
  546. _height = HEIGHT;
  547. break;
  548. case 1:
  549. case 3:
  550. _width = HEIGHT;
  551. _height = WIDTH;
  552. break;
  553. }
  554. }
  555. // Enable (or disable) Code Page 437-compatible charset.
  556. // There was an error in glcdfont.c for the longest time -- one character
  557. // (#176, the 'light shade' block) was missing -- this threw off the index
  558. // of every character that followed it. But a TON of code has been written
  559. // with the erroneous character indices. By default, the library uses the
  560. // original 'wrong' behavior and old sketches will still work. Pass 'true'
  561. // to this function to use correct CP437 character values in your code.
  562. void Adafruit_GFX::cp437(boolean x) {
  563. _cp437 = x;
  564. }
  565. void Adafruit_GFX::setFont(const GFXfont *f) {
  566. if(f) { // Font struct pointer passed in?
  567. if(!gfxFont) { // And no current font struct?
  568. // Switching from classic to new font behavior.
  569. // Move cursor pos down 6 pixels so it's on baseline.
  570. cursor_y += 6;
  571. }
  572. } else if(gfxFont) { // NULL passed. Current font struct defined?
  573. // Switching from new to classic font behavior.
  574. // Move cursor pos up 6 pixels so it's at top-left of char.
  575. cursor_y -= 6;
  576. }
  577. gfxFont = (GFXfont *)f;
  578. }
  579. // Pass string and a cursor position, returns UL corner and W,H.
  580. void Adafruit_GFX::getTextBounds(char *str, int16_t x, int16_t y,
  581. int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
  582. uint8_t c; // Current character
  583. *x1 = x;
  584. *y1 = y;
  585. *w = *h = 0;
  586. if(gfxFont) {
  587. GFXglyph *glyph;
  588. uint8_t first = pgm_read_byte(&gfxFont->first),
  589. last = pgm_read_byte(&gfxFont->last),
  590. gw, gh, xa;
  591. int8_t xo, yo;
  592. int16_t minx = _width, miny = _height, maxx = -1, maxy = -1,
  593. gx1, gy1, gx2, gy2, ts = (int16_t)textsize,
  594. ya = ts * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
  595. while((c = *str++)) {
  596. if(c != '\n') { // Not a newline
  597. if(c != '\r') { // Not a carriage return, is normal char
  598. if((c >= first) && (c <= last)) { // Char present in current font
  599. c -= first;
  600. glyph = &(((GFXglyph *)pgm_read_word(&gfxFont->glyph))[c]);
  601. gw = pgm_read_byte(&glyph->width);
  602. gh = pgm_read_byte(&glyph->height);
  603. xa = pgm_read_byte(&glyph->xAdvance);
  604. xo = pgm_read_byte(&glyph->xOffset);
  605. yo = pgm_read_byte(&glyph->yOffset);
  606. if(wrap && ((x + (((int16_t)xo + gw) * ts)) >= _width)) {
  607. // Line wrap
  608. x = 0; // Reset x to 0
  609. y += ya; // Advance y by 1 line
  610. }
  611. gx1 = x + xo * ts;
  612. gy1 = y + yo * ts;
  613. gx2 = gx1 + gw * ts - 1;
  614. gy2 = gy1 + gh * ts - 1;
  615. if(gx1 < minx) minx = gx1;
  616. if(gy1 < miny) miny = gy1;
  617. if(gx2 > maxx) maxx = gx2;
  618. if(gy2 > maxy) maxy = gy2;
  619. x += xa * ts;
  620. }
  621. } // Carriage return = do nothing
  622. } else { // Newline
  623. x = 0; // Reset x
  624. y += ya; // Advance y by 1 line
  625. }
  626. }
  627. // End of string
  628. *x1 = minx;
  629. *y1 = miny;
  630. if(maxx >= minx) *w = maxx - minx + 1;
  631. if(maxy >= miny) *h = maxy - miny + 1;
  632. } else { // Default font
  633. uint16_t lineWidth = 0, maxWidth = 0; // Width of current, all lines
  634. while((c = *str++)) {
  635. if(c != '\n') { // Not a newline
  636. if(c != '\r') { // Not a carriage return, is normal char
  637. if(wrap && ((x + textsize * 6) >= _width)) {
  638. x = 0; // Reset x to 0
  639. y += textsize * 8; // Advance y by 1 line
  640. if(lineWidth > maxWidth) maxWidth = lineWidth; // Save widest line
  641. lineWidth = textsize * 6; // First char on new line
  642. } else { // No line wrap, just keep incrementing X
  643. lineWidth += textsize * 6; // Includes interchar x gap
  644. }
  645. } // Carriage return = do nothing
  646. } else { // Newline
  647. x = 0; // Reset x to 0
  648. y += textsize * 8; // Advance y by 1 line
  649. if(lineWidth > maxWidth) maxWidth = lineWidth; // Save widest line
  650. lineWidth = 0; // Reset lineWidth for new line
  651. }
  652. }
  653. // End of string
  654. if(lineWidth) y += textsize * 8; // Add height of last (or only) line
  655. if(lineWidth > maxWidth) maxWidth = lineWidth; // Is the last or only line the widest?
  656. *w = maxWidth - 1; // Don't include last interchar x gap
  657. *h = y - *y1;
  658. } // End classic vs custom font
  659. }
  660. // Same as above, but for PROGMEM strings
  661. void Adafruit_GFX::getTextBounds(const __FlashStringHelper *str,
  662. int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
  663. uint8_t *s = (uint8_t *)str, c;
  664. *x1 = x;
  665. *y1 = y;
  666. *w = *h = 0;
  667. if(gfxFont) {
  668. GFXglyph *glyph;
  669. uint8_t first = pgm_read_byte(&gfxFont->first),
  670. last = pgm_read_byte(&gfxFont->last),
  671. gw, gh, xa;
  672. int8_t xo, yo;
  673. int16_t minx = _width, miny = _height, maxx = -1, maxy = -1,
  674. gx1, gy1, gx2, gy2, ts = (int16_t)textsize,
  675. ya = ts * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
  676. while((c = pgm_read_byte(s++))) {
  677. if(c != '\n') { // Not a newline
  678. if(c != '\r') { // Not a carriage return, is normal char
  679. if((c >= first) && (c <= last)) { // Char present in current font
  680. c -= first;
  681. glyph = &(((GFXglyph *)pgm_read_word(&gfxFont->glyph))[c]);
  682. gw = pgm_read_byte(&glyph->width);
  683. gh = pgm_read_byte(&glyph->height);
  684. xa = pgm_read_byte(&glyph->xAdvance);
  685. xo = pgm_read_byte(&glyph->xOffset);
  686. yo = pgm_read_byte(&glyph->yOffset);
  687. if(wrap && ((x + (((int16_t)xo + gw) * ts)) >= _width)) {
  688. // Line wrap
  689. x = 0; // Reset x to 0
  690. y += ya; // Advance y by 1 line
  691. }
  692. gx1 = x + xo * ts;
  693. gy1 = y + yo * ts;
  694. gx2 = gx1 + gw * ts - 1;
  695. gy2 = gy1 + gh * ts - 1;
  696. if(gx1 < minx) minx = gx1;
  697. if(gy1 < miny) miny = gy1;
  698. if(gx2 > maxx) maxx = gx2;
  699. if(gy2 > maxy) maxy = gy2;
  700. x += xa * ts;
  701. }
  702. } // Carriage return = do nothing
  703. } else { // Newline
  704. x = 0; // Reset x
  705. y += ya; // Advance y by 1 line
  706. }
  707. }
  708. // End of string
  709. *x1 = minx;
  710. *y1 = miny;
  711. if(maxx >= minx) *w = maxx - minx + 1;
  712. if(maxy >= miny) *h = maxy - miny + 1;
  713. } else { // Default font
  714. uint16_t lineWidth = 0, maxWidth = 0; // Width of current, all lines
  715. while((c = pgm_read_byte(s++))) {
  716. if(c != '\n') { // Not a newline
  717. if(c != '\r') { // Not a carriage return, is normal char
  718. if(wrap && ((x + textsize * 6) >= _width)) {
  719. x = 0; // Reset x to 0
  720. y += textsize * 8; // Advance y by 1 line
  721. if(lineWidth > maxWidth) maxWidth = lineWidth; // Save widest line
  722. lineWidth = textsize * 6; // First char on new line
  723. } else { // No line wrap, just keep incrementing X
  724. lineWidth += textsize * 6; // Includes interchar x gap
  725. }
  726. } // Carriage return = do nothing
  727. } else { // Newline
  728. x = 0; // Reset x to 0
  729. y += textsize * 8; // Advance y by 1 line
  730. if(lineWidth > maxWidth) maxWidth = lineWidth; // Save widest line
  731. lineWidth = 0; // Reset lineWidth for new line
  732. }
  733. }
  734. // End of string
  735. if(lineWidth) y += textsize * 8; // Add height of last (or only) line
  736. if(lineWidth > maxWidth) maxWidth = lineWidth; // Is the last or only line the widest?
  737. *w = maxWidth - 1; // Don't include last interchar x gap
  738. *h = y - *y1;
  739. } // End classic vs custom font
  740. }
  741. // Return the size of the display (per current rotation)
  742. int16_t Adafruit_GFX::width(void) const {
  743. return _width;
  744. }
  745. int16_t Adafruit_GFX::height(void) const {
  746. return _height;
  747. }
  748. void Adafruit_GFX::invertDisplay(boolean i) {
  749. // Do nothing, must be subclassed if supported by hardware
  750. }
  751. /***************************************************************************/
  752. // code for the GFX button UI element
  753. Adafruit_GFX_Button::Adafruit_GFX_Button(void) {
  754. _gfx = 0;
  755. }
  756. void Adafruit_GFX_Button::initButton(
  757. Adafruit_GFX *gfx, int16_t x, int16_t y, uint8_t w, uint8_t h,
  758. uint16_t outline, uint16_t fill, uint16_t textcolor,
  759. char *label, uint8_t textsize)
  760. {
  761. _x = x;
  762. _y = y;
  763. _w = w;
  764. _h = h;
  765. _outlinecolor = outline;
  766. _fillcolor = fill;
  767. _textcolor = textcolor;
  768. _textsize = textsize;
  769. _gfx = gfx;
  770. strncpy(_label, label, 9);
  771. _label[9] = 0;
  772. }
  773. void Adafruit_GFX_Button::drawButton(boolean inverted) {
  774. uint16_t fill, outline, text;
  775. if(!inverted) {
  776. fill = _fillcolor;
  777. outline = _outlinecolor;
  778. text = _textcolor;
  779. } else {
  780. fill = _textcolor;
  781. outline = _outlinecolor;
  782. text = _fillcolor;
  783. }
  784. _gfx->fillRoundRect(_x - (_w/2), _y - (_h/2), _w, _h, min(_w,_h)/4, fill);
  785. _gfx->drawRoundRect(_x - (_w/2), _y - (_h/2), _w, _h, min(_w,_h)/4, outline);
  786. _gfx->setCursor(_x - strlen(_label)*3*_textsize, _y-4*_textsize);
  787. _gfx->setTextColor(text);
  788. _gfx->setTextSize(_textsize);
  789. _gfx->print(_label);
  790. }
  791. boolean Adafruit_GFX_Button::contains(int16_t x, int16_t y) {
  792. if ((x < (_x - _w/2)) || (x > (_x + _w/2))) return false;
  793. if ((y < (_y - _h/2)) || (y > (_y + _h/2))) return false;
  794. return true;
  795. }
  796. void Adafruit_GFX_Button::press(boolean p) {
  797. laststate = currstate;
  798. currstate = p;
  799. }
  800. boolean Adafruit_GFX_Button::isPressed() { return currstate; }
  801. boolean Adafruit_GFX_Button::justPressed() { return (currstate && !laststate); }
  802. boolean Adafruit_GFX_Button::justReleased() { return (!currstate && laststate); }
  803. // -------------------------------------------------------------------------
  804. // GFXcanvas1 and GFXcanvas16 (currently a WIP, don't get too comfy with the
  805. // implementation) provide 1- and 16-bit offscreen canvases, the address of
  806. // which can be passed to drawBitmap() or pushColors() (the latter appears
  807. // to only be in Adafruit_TFTLCD at this time). This is here mostly to
  808. // help with the recently-added proportionally-spaced fonts; adds a way to
  809. // refresh a section of the screen without a massive flickering clear-and-
  810. // redraw...but maybe you'll find other uses too. VERY RAM-intensive, since
  811. // the buffer is in MCU memory and not the display driver...GXFcanvas1 might
  812. // be minimally useful on an Uno-class board, but this and GFXcanvas16 are
  813. // much more likely to require at least a Mega or various recent ARM-type
  814. // boards (recomment, as the text+bitmap draw can be pokey). GFXcanvas1
  815. // requires 1 bit per pixel (rounded up to nearest byte per scanline),
  816. // GFXcanvas16 requires 2 bytes per pixel (no scanline pad).
  817. // NOT EXTENSIVELY TESTED YET. MAY CONTAIN WORST BUGS KNOWN TO HUMANKIND.
  818. GFXcanvas1::GFXcanvas1(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
  819. uint16_t bytes = ((w + 7) / 8) * h;
  820. if((buffer = (uint8_t *)malloc(bytes))) {
  821. memset(buffer, 0, bytes);
  822. }
  823. }
  824. GFXcanvas1::~GFXcanvas1(void) {
  825. if(buffer) free(buffer);
  826. }
  827. uint8_t* GFXcanvas1::getBuffer(void) {
  828. return buffer;
  829. }
  830. void GFXcanvas1::drawPixel(int16_t x, int16_t y, uint16_t color) {
  831. // Bitmask tables of 0x80>>X and ~(0x80>>X), because X>>Y is slow on AVR
  832. static const uint8_t PROGMEM
  833. GFXsetBit[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 },
  834. GFXclrBit[] = { 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0xFE };
  835. if(buffer) {
  836. if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
  837. int16_t t;
  838. switch(rotation) {
  839. case 1:
  840. t = x;
  841. x = WIDTH - 1 - y;
  842. y = t;
  843. break;
  844. case 2:
  845. x = WIDTH - 1 - x;
  846. y = HEIGHT - 1 - y;
  847. break;
  848. case 3:
  849. t = x;
  850. x = y;
  851. y = HEIGHT - 1 - t;
  852. break;
  853. }
  854. uint8_t *ptr = &buffer[(x / 8) + y * ((WIDTH + 7) / 8)];
  855. if(color) *ptr |= pgm_read_byte(&GFXsetBit[x & 7]);
  856. else *ptr &= pgm_read_byte(&GFXclrBit[x & 7]);
  857. }
  858. }
  859. void GFXcanvas1::fillScreen(uint16_t color) {
  860. if(buffer) {
  861. uint16_t bytes = ((WIDTH + 7) / 8) * HEIGHT;
  862. memset(buffer, color ? 0xFF : 0x00, bytes);
  863. }
  864. }
  865. GFXcanvas16::GFXcanvas16(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
  866. uint16_t bytes = w * h * 2;
  867. if((buffer = (uint16_t *)malloc(bytes))) {
  868. memset(buffer, 0, bytes);
  869. }
  870. }
  871. GFXcanvas16::~GFXcanvas16(void) {
  872. if(buffer) free(buffer);
  873. }
  874. uint16_t* GFXcanvas16::getBuffer(void) {
  875. return buffer;
  876. }
  877. void GFXcanvas16::drawPixel(int16_t x, int16_t y, uint16_t color) {
  878. if(buffer) {
  879. if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
  880. int16_t t;
  881. switch(rotation) {
  882. case 1:
  883. t = x;
  884. x = WIDTH - 1 - y;
  885. y = t;
  886. break;
  887. case 2:
  888. x = WIDTH - 1 - x;
  889. y = HEIGHT - 1 - y;
  890. break;
  891. case 3:
  892. t = x;
  893. x = y;
  894. y = HEIGHT - 1 - t;
  895. break;
  896. }
  897. buffer[x + y * WIDTH] = color;
  898. }
  899. }
  900. void GFXcanvas16::fillScreen(uint16_t color) {
  901. if(buffer) {
  902. uint8_t hi = color >> 8, lo = color & 0xFF;
  903. if(hi == lo) {
  904. memset(buffer, lo, WIDTH * HEIGHT * 2);
  905. } else {
  906. uint16_t i, pixels = WIDTH * HEIGHT;
  907. for(i=0; i<pixels; i++) buffer[i] = color;
  908. }
  909. }
  910. }