Adafruit_GFX.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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. #define pgm_read_pointer(addr) ((void *)pgm_read_word(addr))
  33. #elif defined(ESP8266)
  34. #include <pgmspace.h>
  35. #define pgm_read_pointer(addr) ((void *)pgm_read_dword(addr))
  36. #else
  37. #define pgm_read_byte(addr) (*(const unsigned char *)(addr))
  38. #define pgm_read_word(addr) (*(const unsigned short *)(addr))
  39. #define pgm_read_pointer(addr) ((void *)pgm_read_word(addr))
  40. #endif
  41. #ifndef min
  42. #define min(a,b) (((a) < (b)) ? (a) : (b))
  43. #endif
  44. #ifndef _swap_int16_t
  45. #define _swap_int16_t(a, b) { int16_t t = a; a = b; b = t; }
  46. #endif
  47. Adafruit_GFX::Adafruit_GFX(int16_t w, int16_t h):
  48. WIDTH(w), HEIGHT(h)
  49. {
  50. _width = WIDTH;
  51. _height = HEIGHT;
  52. rotation = 0;
  53. cursor_y = cursor_x = 0;
  54. textsize = 1;
  55. textcolor = textbgcolor = 0xFFFF;
  56. wrap = true;
  57. _cp437 = false;
  58. gfxFont = NULL;
  59. }
  60. // Draw a circle outline
  61. void Adafruit_GFX::drawCircle(int16_t x0, int16_t y0, int16_t r,
  62. uint16_t color) {
  63. int16_t f = 1 - r;
  64. int16_t ddF_x = 1;
  65. int16_t ddF_y = -2 * r;
  66. int16_t x = 0;
  67. int16_t y = r;
  68. drawPixel(x0 , y0+r, color);
  69. drawPixel(x0 , y0-r, color);
  70. drawPixel(x0+r, y0 , color);
  71. drawPixel(x0-r, y0 , color);
  72. while (x<y) {
  73. if (f >= 0) {
  74. y--;
  75. ddF_y += 2;
  76. f += ddF_y;
  77. }
  78. x++;
  79. ddF_x += 2;
  80. f += ddF_x;
  81. drawPixel(x0 + x, y0 + y, color);
  82. drawPixel(x0 - x, y0 + y, color);
  83. drawPixel(x0 + x, y0 - y, color);
  84. drawPixel(x0 - x, y0 - y, color);
  85. drawPixel(x0 + y, y0 + x, color);
  86. drawPixel(x0 - y, y0 + x, color);
  87. drawPixel(x0 + y, y0 - x, color);
  88. drawPixel(x0 - y, y0 - x, color);
  89. }
  90. }
  91. void Adafruit_GFX::drawCircleHelper( int16_t x0, int16_t y0,
  92. int16_t r, uint8_t cornername, uint16_t color) {
  93. int16_t f = 1 - r;
  94. int16_t ddF_x = 1;
  95. int16_t ddF_y = -2 * r;
  96. int16_t x = 0;
  97. int16_t y = r;
  98. while (x<y) {
  99. if (f >= 0) {
  100. y--;
  101. ddF_y += 2;
  102. f += ddF_y;
  103. }
  104. x++;
  105. ddF_x += 2;
  106. f += ddF_x;
  107. if (cornername & 0x4) {
  108. drawPixel(x0 + x, y0 + y, color);
  109. drawPixel(x0 + y, y0 + x, color);
  110. }
  111. if (cornername & 0x2) {
  112. drawPixel(x0 + x, y0 - y, color);
  113. drawPixel(x0 + y, y0 - x, color);
  114. }
  115. if (cornername & 0x8) {
  116. drawPixel(x0 - y, y0 + x, color);
  117. drawPixel(x0 - x, y0 + y, color);
  118. }
  119. if (cornername & 0x1) {
  120. drawPixel(x0 - y, y0 - x, color);
  121. drawPixel(x0 - x, y0 - y, color);
  122. }
  123. }
  124. }
  125. void Adafruit_GFX::fillCircle(int16_t x0, int16_t y0, int16_t r,
  126. uint16_t color) {
  127. drawFastVLine(x0, y0-r, 2*r+1, color);
  128. fillCircleHelper(x0, y0, r, 3, 0, color);
  129. }
  130. // Used to do circles and roundrects
  131. void Adafruit_GFX::fillCircleHelper(int16_t x0, int16_t y0, int16_t r,
  132. uint8_t cornername, int16_t delta, uint16_t color) {
  133. int16_t f = 1 - r;
  134. int16_t ddF_x = 1;
  135. int16_t ddF_y = -2 * r;
  136. int16_t x = 0;
  137. int16_t y = r;
  138. while (x<y) {
  139. if (f >= 0) {
  140. y--;
  141. ddF_y += 2;
  142. f += ddF_y;
  143. }
  144. x++;
  145. ddF_x += 2;
  146. f += ddF_x;
  147. if (cornername & 0x1) {
  148. drawFastVLine(x0+x, y0-y, 2*y+1+delta, color);
  149. drawFastVLine(x0+y, y0-x, 2*x+1+delta, color);
  150. }
  151. if (cornername & 0x2) {
  152. drawFastVLine(x0-x, y0-y, 2*y+1+delta, color);
  153. drawFastVLine(x0-y, y0-x, 2*x+1+delta, color);
  154. }
  155. }
  156. }
  157. // Bresenham's algorithm - thx wikpedia
  158. void Adafruit_GFX::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
  159. uint16_t color) {
  160. int16_t steep = abs(y1 - y0) > abs(x1 - x0);
  161. if (steep) {
  162. _swap_int16_t(x0, y0);
  163. _swap_int16_t(x1, y1);
  164. }
  165. if (x0 > x1) {
  166. _swap_int16_t(x0, x1);
  167. _swap_int16_t(y0, y1);
  168. }
  169. int16_t dx, dy;
  170. dx = x1 - x0;
  171. dy = abs(y1 - y0);
  172. int16_t err = dx / 2;
  173. int16_t ystep;
  174. if (y0 < y1) {
  175. ystep = 1;
  176. } else {
  177. ystep = -1;
  178. }
  179. for (; x0<=x1; x0++) {
  180. if (steep) {
  181. drawPixel(y0, x0, color);
  182. } else {
  183. drawPixel(x0, y0, color);
  184. }
  185. err -= dy;
  186. if (err < 0) {
  187. y0 += ystep;
  188. err += dx;
  189. }
  190. }
  191. }
  192. // Draw a rectangle
  193. void Adafruit_GFX::drawRect(int16_t x, int16_t y, int16_t w, int16_t h,
  194. uint16_t color) {
  195. drawFastHLine(x, y, w, color);
  196. drawFastHLine(x, y+h-1, w, color);
  197. drawFastVLine(x, y, h, color);
  198. drawFastVLine(x+w-1, y, h, color);
  199. }
  200. void Adafruit_GFX::drawFastVLine(int16_t x, int16_t y,
  201. int16_t h, uint16_t color) {
  202. // Update in subclasses if desired!
  203. drawLine(x, y, x, y+h-1, color);
  204. }
  205. void Adafruit_GFX::drawFastHLine(int16_t x, int16_t y,
  206. int16_t w, uint16_t color) {
  207. // Update in subclasses if desired!
  208. drawLine(x, y, x+w-1, y, color);
  209. }
  210. void Adafruit_GFX::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
  211. uint16_t color) {
  212. // Update in subclasses if desired!
  213. for (int16_t i=x; i<x+w; i++) {
  214. drawFastVLine(i, y, h, color);
  215. }
  216. }
  217. void Adafruit_GFX::fillScreen(uint16_t color) {
  218. fillRect(0, 0, _width, _height, color);
  219. }
  220. // Draw a rounded rectangle
  221. void Adafruit_GFX::drawRoundRect(int16_t x, int16_t y, int16_t w,
  222. int16_t h, int16_t r, uint16_t color) {
  223. // smarter version
  224. drawFastHLine(x+r , y , w-2*r, color); // Top
  225. drawFastHLine(x+r , y+h-1, w-2*r, color); // Bottom
  226. drawFastVLine(x , y+r , h-2*r, color); // Left
  227. drawFastVLine(x+w-1, y+r , h-2*r, color); // Right
  228. // draw four corners
  229. drawCircleHelper(x+r , y+r , r, 1, color);
  230. drawCircleHelper(x+w-r-1, y+r , r, 2, color);
  231. drawCircleHelper(x+w-r-1, y+h-r-1, r, 4, color);
  232. drawCircleHelper(x+r , y+h-r-1, r, 8, color);
  233. }
  234. // Fill a rounded rectangle
  235. void Adafruit_GFX::fillRoundRect(int16_t x, int16_t y, int16_t w,
  236. int16_t h, int16_t r, uint16_t color) {
  237. // smarter version
  238. fillRect(x+r, y, w-2*r, h, color);
  239. // draw four corners
  240. fillCircleHelper(x+w-r-1, y+r, r, 1, h-2*r-1, color);
  241. fillCircleHelper(x+r , y+r, r, 2, h-2*r-1, color);
  242. }
  243. // Draw a triangle
  244. void Adafruit_GFX::drawTriangle(int16_t x0, int16_t y0,
  245. int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color) {
  246. drawLine(x0, y0, x1, y1, color);
  247. drawLine(x1, y1, x2, y2, color);
  248. drawLine(x2, y2, x0, y0, color);
  249. }
  250. // Fill a triangle
  251. void Adafruit_GFX::fillTriangle(int16_t x0, int16_t y0,
  252. int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color) {
  253. int16_t a, b, y, last;
  254. // Sort coordinates by Y order (y2 >= y1 >= y0)
  255. if (y0 > y1) {
  256. _swap_int16_t(y0, y1); _swap_int16_t(x0, x1);
  257. }
  258. if (y1 > y2) {
  259. _swap_int16_t(y2, y1); _swap_int16_t(x2, x1);
  260. }
  261. if (y0 > y1) {
  262. _swap_int16_t(y0, y1); _swap_int16_t(x0, x1);
  263. }
  264. if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing
  265. a = b = x0;
  266. if(x1 < a) a = x1;
  267. else if(x1 > b) b = x1;
  268. if(x2 < a) a = x2;
  269. else if(x2 > b) b = x2;
  270. drawFastHLine(a, y0, b-a+1, color);
  271. return;
  272. }
  273. int16_t
  274. dx01 = x1 - x0,
  275. dy01 = y1 - y0,
  276. dx02 = x2 - x0,
  277. dy02 = y2 - y0,
  278. dx12 = x2 - x1,
  279. dy12 = y2 - y1;
  280. int32_t
  281. sa = 0,
  282. sb = 0;
  283. // For upper part of triangle, find scanline crossings for segments
  284. // 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1
  285. // is included here (and second loop will be skipped, avoiding a /0
  286. // error there), otherwise scanline y1 is skipped here and handled
  287. // in the second loop...which also avoids a /0 error here if y0=y1
  288. // (flat-topped triangle).
  289. if(y1 == y2) last = y1; // Include y1 scanline
  290. else last = y1-1; // Skip it
  291. for(y=y0; y<=last; y++) {
  292. a = x0 + sa / dy01;
  293. b = x0 + sb / dy02;
  294. sa += dx01;
  295. sb += dx02;
  296. /* longhand:
  297. a = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
  298. b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
  299. */
  300. if(a > b) _swap_int16_t(a,b);
  301. drawFastHLine(a, y, b-a+1, color);
  302. }
  303. // For lower part of triangle, find scanline crossings for segments
  304. // 0-2 and 1-2. This loop is skipped if y1=y2.
  305. sa = dx12 * (y - y1);
  306. sb = dx02 * (y - y0);
  307. for(; y<=y2; y++) {
  308. a = x1 + sa / dy12;
  309. b = x0 + sb / dy02;
  310. sa += dx12;
  311. sb += dx02;
  312. /* longhand:
  313. a = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
  314. b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
  315. */
  316. if(a > b) _swap_int16_t(a,b);
  317. drawFastHLine(a, y, b-a+1, color);
  318. }
  319. }
  320. // Draw a 1-bit image (bitmap) at the specified (x,y) position from the
  321. // provided bitmap buffer (must be PROGMEM memory) using the specified
  322. // foreground color (unset bits are transparent).
  323. void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
  324. const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) {
  325. int16_t i, j, byteWidth = (w + 7) / 8;
  326. uint8_t byte;
  327. for(j=0; j<h; j++) {
  328. for(i=0; i<w; i++) {
  329. if(i & 7) byte <<= 1;
  330. else byte = pgm_read_byte(bitmap + j * byteWidth + i / 8);
  331. if(byte & 0x80) drawPixel(x+i, y+j, color);
  332. }
  333. }
  334. }
  335. // Draw a 1-bit image (bitmap) at the specified (x,y) position from the
  336. // provided bitmap buffer (must be PROGMEM memory) using the specified
  337. // foreground (for set bits) and background (for clear bits) colors.
  338. void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
  339. const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg) {
  340. int16_t i, j, byteWidth = (w + 7) / 8;
  341. uint8_t byte;
  342. for(j=0; j<h; j++) {
  343. for(i=0; i<w; i++ ) {
  344. if(i & 7) byte <<= 1;
  345. else byte = pgm_read_byte(bitmap + j * byteWidth + i / 8);
  346. if(byte & 0x80) drawPixel(x+i, y+j, color);
  347. else drawPixel(x+i, y+j, bg);
  348. }
  349. }
  350. }
  351. // drawBitmap() variant for RAM-resident (not PROGMEM) bitmaps.
  352. void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
  353. uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) {
  354. int16_t i, j, byteWidth = (w + 7) / 8;
  355. uint8_t byte;
  356. for(j=0; j<h; j++) {
  357. for(i=0; i<w; i++ ) {
  358. if(i & 7) byte <<= 1;
  359. else byte = bitmap[j * byteWidth + i / 8];
  360. if(byte & 0x80) drawPixel(x+i, y+j, color);
  361. }
  362. }
  363. }
  364. // drawBitmap() variant w/background for RAM-resident (not PROGMEM) bitmaps.
  365. void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
  366. uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg) {
  367. int16_t i, j, byteWidth = (w + 7) / 8;
  368. uint8_t byte;
  369. for(j=0; j<h; j++) {
  370. for(i=0; i<w; i++ ) {
  371. if(i & 7) byte <<= 1;
  372. else byte = bitmap[j * byteWidth + i / 8];
  373. if(byte & 0x80) drawPixel(x+i, y+j, color);
  374. else drawPixel(x+i, y+j, bg);
  375. }
  376. }
  377. }
  378. //Draw XBitMap Files (*.xbm), exported from GIMP,
  379. //Usage: Export from GIMP to *.xbm, rename *.xbm to *.c and open in editor.
  380. //C Array can be directly used with this function
  381. void Adafruit_GFX::drawXBitmap(int16_t x, int16_t y,
  382. const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) {
  383. int16_t i, j, byteWidth = (w + 7) / 8;
  384. uint8_t byte;
  385. for(j=0; j<h; j++) {
  386. for(i=0; i<w; i++ ) {
  387. if(i & 7) byte >>= 1;
  388. else byte = pgm_read_byte(bitmap + j * byteWidth + i / 8);
  389. if(byte & 0x01) drawPixel(x+i, y+j, color);
  390. }
  391. }
  392. }
  393. #if ARDUINO >= 100
  394. size_t Adafruit_GFX::write(uint8_t c) {
  395. #else
  396. void Adafruit_GFX::write(uint8_t c) {
  397. #endif
  398. if(!gfxFont) { // 'Classic' built-in font
  399. if(c == '\n') {
  400. cursor_y += textsize*8;
  401. cursor_x = 0;
  402. } else if(c == '\r') {
  403. // skip em
  404. } else {
  405. if(wrap && ((cursor_x + textsize * 6) >= _width)) { // Heading off edge?
  406. cursor_x = 0; // Reset x to zero
  407. cursor_y += textsize * 8; // Advance y one line
  408. }
  409. drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
  410. cursor_x += textsize * 6;
  411. }
  412. } else { // Custom font
  413. if(c == '\n') {
  414. cursor_x = 0;
  415. cursor_y += (int16_t)textsize *
  416. (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
  417. } else if(c != '\r') {
  418. uint8_t first = pgm_read_byte(&gfxFont->first);
  419. if((c >= first) && (c <= (uint8_t)pgm_read_byte(&gfxFont->last))) {
  420. uint8_t c2 = c - pgm_read_byte(&gfxFont->first);
  421. GFXglyph *glyph = &(((GFXglyph *)pgm_read_pointer(&gfxFont->glyph))[c2]);
  422. uint8_t w = pgm_read_byte(&glyph->width),
  423. h = pgm_read_byte(&glyph->height);
  424. if((w > 0) && (h > 0)) { // Is there an associated bitmap?
  425. int16_t xo = (int8_t)pgm_read_byte(&glyph->xOffset); // sic
  426. if(wrap && ((cursor_x + textsize * (xo + w)) >= _width)) {
  427. // Drawing character would go off right edge; wrap to new line
  428. cursor_x = 0;
  429. cursor_y += (int16_t)textsize *
  430. (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
  431. }
  432. drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
  433. }
  434. cursor_x += pgm_read_byte(&glyph->xAdvance) * (int16_t)textsize;
  435. }
  436. }
  437. }
  438. #if ARDUINO >= 100
  439. return 1;
  440. #endif
  441. }
  442. // Draw a character
  443. void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
  444. uint16_t color, uint16_t bg, uint8_t size) {
  445. if(!gfxFont) { // 'Classic' built-in font
  446. if((x >= _width) || // Clip right
  447. (y >= _height) || // Clip bottom
  448. ((x + 6 * size - 1) < 0) || // Clip left
  449. ((y + 8 * size - 1) < 0)) // Clip top
  450. return;
  451. if(!_cp437 && (c >= 176)) c++; // Handle 'classic' charset behavior
  452. for(int8_t i=0; i<6; i++ ) {
  453. uint8_t line;
  454. if(i < 5) line = pgm_read_byte(font+(c*5)+i);
  455. else line = 0x0;
  456. for(int8_t j=0; j<8; j++, line >>= 1) {
  457. if(line & 0x1) {
  458. if(size == 1) drawPixel(x+i, y+j, color);
  459. else fillRect(x+(i*size), y+(j*size), size, size, color);
  460. } else if(bg != color) {
  461. if(size == 1) drawPixel(x+i, y+j, bg);
  462. else fillRect(x+i*size, y+j*size, size, size, bg);
  463. }
  464. }
  465. }
  466. } else { // Custom font
  467. // Character is assumed previously filtered by write() to eliminate
  468. // newlines, returns, non-printable characters, etc. Calling drawChar()
  469. // directly with 'bad' characters of font may cause mayhem!
  470. c -= pgm_read_byte(&gfxFont->first);
  471. GFXglyph *glyph = &(((GFXglyph *)pgm_read_pointer(&gfxFont->glyph))[c]);
  472. uint8_t *bitmap = (uint8_t *)pgm_read_pointer(&gfxFont->bitmap);
  473. uint16_t bo = pgm_read_word(&glyph->bitmapOffset);
  474. uint8_t w = pgm_read_byte(&glyph->width),
  475. h = pgm_read_byte(&glyph->height),
  476. xa = pgm_read_byte(&glyph->xAdvance);
  477. int8_t xo = pgm_read_byte(&glyph->xOffset),
  478. yo = pgm_read_byte(&glyph->yOffset);
  479. uint8_t xx, yy, bits, bit = 0;
  480. int16_t xo16, yo16;
  481. if(size > 1) {
  482. xo16 = xo;
  483. yo16 = yo;
  484. }
  485. // Todo: Add character clipping here
  486. // NOTE: THERE IS NO 'BACKGROUND' COLOR OPTION ON CUSTOM FONTS.
  487. // THIS IS ON PURPOSE AND BY DESIGN. The background color feature
  488. // has typically been used with the 'classic' font to overwrite old
  489. // screen contents with new data. This ONLY works because the
  490. // characters are a uniform size; it's not a sensible thing to do with
  491. // proportionally-spaced fonts with glyphs of varying sizes (and that
  492. // may overlap). To replace previously-drawn text when using a custom
  493. // font, use the getTextBounds() function to determine the smallest
  494. // rectangle encompassing a string, erase the area with fillRect(),
  495. // then draw new text. This WILL infortunately 'blink' the text, but
  496. // is unavoidable. Drawing 'background' pixels will NOT fix this,
  497. // only creates a new set of problems. Have an idea to work around
  498. // this (a canvas object type for MCUs that can afford the RAM and
  499. // displays supporting setAddrWindow() and pushColors()), but haven't
  500. // implemented this yet.
  501. for(yy=0; yy<h; yy++) {
  502. for(xx=0; xx<w; xx++) {
  503. if(!(bit++ & 7)) {
  504. bits = pgm_read_byte(&bitmap[bo++]);
  505. }
  506. if(bits & 0x80) {
  507. if(size == 1) {
  508. drawPixel(x+xo+xx, y+yo+yy, color);
  509. } else {
  510. fillRect(x+(xo16+xx)*size, y+(yo16+yy)*size, size, size, color);
  511. }
  512. }
  513. bits <<= 1;
  514. }
  515. }
  516. } // End classic vs custom font
  517. }
  518. void Adafruit_GFX::setCursor(int16_t x, int16_t y) {
  519. cursor_x = x;
  520. cursor_y = y;
  521. }
  522. int16_t Adafruit_GFX::getCursorX(void) const {
  523. return cursor_x;
  524. }
  525. int16_t Adafruit_GFX::getCursorY(void) const {
  526. return cursor_y;
  527. }
  528. void Adafruit_GFX::setTextSize(uint8_t s) {
  529. textsize = (s > 0) ? s : 1;
  530. }
  531. void Adafruit_GFX::setTextColor(uint16_t c) {
  532. // For 'transparent' background, we'll set the bg
  533. // to the same as fg instead of using a flag
  534. textcolor = textbgcolor = c;
  535. }
  536. void Adafruit_GFX::setTextColor(uint16_t c, uint16_t b) {
  537. textcolor = c;
  538. textbgcolor = b;
  539. }
  540. void Adafruit_GFX::setTextWrap(boolean w) {
  541. wrap = w;
  542. }
  543. uint8_t Adafruit_GFX::getRotation(void) const {
  544. return rotation;
  545. }
  546. void Adafruit_GFX::setRotation(uint8_t x) {
  547. rotation = (x & 3);
  548. switch(rotation) {
  549. case 0:
  550. case 2:
  551. _width = WIDTH;
  552. _height = HEIGHT;
  553. break;
  554. case 1:
  555. case 3:
  556. _width = HEIGHT;
  557. _height = WIDTH;
  558. break;
  559. }
  560. }
  561. // Enable (or disable) Code Page 437-compatible charset.
  562. // There was an error in glcdfont.c for the longest time -- one character
  563. // (#176, the 'light shade' block) was missing -- this threw off the index
  564. // of every character that followed it. But a TON of code has been written
  565. // with the erroneous character indices. By default, the library uses the
  566. // original 'wrong' behavior and old sketches will still work. Pass 'true'
  567. // to this function to use correct CP437 character values in your code.
  568. void Adafruit_GFX::cp437(boolean x) {
  569. _cp437 = x;
  570. }
  571. void Adafruit_GFX::setFont(const GFXfont *f) {
  572. if(f) { // Font struct pointer passed in?
  573. if(!gfxFont) { // And no current font struct?
  574. // Switching from classic to new font behavior.
  575. // Move cursor pos down 6 pixels so it's on baseline.
  576. cursor_y += 6;
  577. }
  578. } else if(gfxFont) { // NULL passed. Current font struct defined?
  579. // Switching from new to classic font behavior.
  580. // Move cursor pos up 6 pixels so it's at top-left of char.
  581. cursor_y -= 6;
  582. }
  583. gfxFont = (GFXfont *)f;
  584. }
  585. // Pass string and a cursor position, returns UL corner and W,H.
  586. void Adafruit_GFX::getTextBounds(char *str, int16_t x, int16_t y,
  587. int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
  588. uint8_t c; // Current character
  589. *x1 = x;
  590. *y1 = y;
  591. *w = *h = 0;
  592. if(gfxFont) {
  593. GFXglyph *glyph;
  594. uint8_t first = pgm_read_byte(&gfxFont->first),
  595. last = pgm_read_byte(&gfxFont->last),
  596. gw, gh, xa;
  597. int8_t xo, yo;
  598. int16_t minx = _width, miny = _height, maxx = -1, maxy = -1,
  599. gx1, gy1, gx2, gy2, ts = (int16_t)textsize,
  600. ya = ts * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
  601. while((c = *str++)) {
  602. if(c != '\n') { // Not a newline
  603. if(c != '\r') { // Not a carriage return, is normal char
  604. if((c >= first) && (c <= last)) { // Char present in current font
  605. c -= first;
  606. glyph = &(((GFXglyph *)pgm_read_pointer(&gfxFont->glyph))[c]);
  607. gw = pgm_read_byte(&glyph->width);
  608. gh = pgm_read_byte(&glyph->height);
  609. xa = pgm_read_byte(&glyph->xAdvance);
  610. xo = pgm_read_byte(&glyph->xOffset);
  611. yo = pgm_read_byte(&glyph->yOffset);
  612. if(wrap && ((x + (((int16_t)xo + gw) * ts)) >= _width)) {
  613. // Line wrap
  614. x = 0; // Reset x to 0
  615. y += ya; // Advance y by 1 line
  616. }
  617. gx1 = x + xo * ts;
  618. gy1 = y + yo * ts;
  619. gx2 = gx1 + gw * ts - 1;
  620. gy2 = gy1 + gh * ts - 1;
  621. if(gx1 < minx) minx = gx1;
  622. if(gy1 < miny) miny = gy1;
  623. if(gx2 > maxx) maxx = gx2;
  624. if(gy2 > maxy) maxy = gy2;
  625. x += xa * ts;
  626. }
  627. } // Carriage return = do nothing
  628. } else { // Newline
  629. x = 0; // Reset x
  630. y += ya; // Advance y by 1 line
  631. }
  632. }
  633. // End of string
  634. *x1 = minx;
  635. *y1 = miny;
  636. if(maxx >= minx) *w = maxx - minx + 1;
  637. if(maxy >= miny) *h = maxy - miny + 1;
  638. } else { // Default font
  639. uint16_t lineWidth = 0, maxWidth = 0; // Width of current, all lines
  640. while((c = *str++)) {
  641. if(c != '\n') { // Not a newline
  642. if(c != '\r') { // Not a carriage return, is normal char
  643. if(wrap && ((x + textsize * 6) >= _width)) {
  644. x = 0; // Reset x to 0
  645. y += textsize * 8; // Advance y by 1 line
  646. if(lineWidth > maxWidth) maxWidth = lineWidth; // Save widest line
  647. lineWidth = textsize * 6; // First char on new line
  648. } else { // No line wrap, just keep incrementing X
  649. lineWidth += textsize * 6; // Includes interchar x gap
  650. }
  651. } // Carriage return = do nothing
  652. } else { // Newline
  653. x = 0; // Reset x to 0
  654. y += textsize * 8; // Advance y by 1 line
  655. if(lineWidth > maxWidth) maxWidth = lineWidth; // Save widest line
  656. lineWidth = 0; // Reset lineWidth for new line
  657. }
  658. }
  659. // End of string
  660. if(lineWidth) y += textsize * 8; // Add height of last (or only) line
  661. *w = maxWidth - 1; // Don't include last interchar x gap
  662. *h = y - *y1;
  663. } // End classic vs custom font
  664. }
  665. // Same as above, but for PROGMEM strings
  666. void Adafruit_GFX::getTextBounds(const __FlashStringHelper *str,
  667. int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
  668. uint8_t *s = (uint8_t *)str, c;
  669. *x1 = x;
  670. *y1 = y;
  671. *w = *h = 0;
  672. if(gfxFont) {
  673. GFXglyph *glyph;
  674. uint8_t first = pgm_read_byte(&gfxFont->first),
  675. last = pgm_read_byte(&gfxFont->last),
  676. gw, gh, xa;
  677. int8_t xo, yo;
  678. int16_t minx = _width, miny = _height, maxx = -1, maxy = -1,
  679. gx1, gy1, gx2, gy2, ts = (int16_t)textsize,
  680. ya = ts * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
  681. while((c = pgm_read_byte(s++))) {
  682. if(c != '\n') { // Not a newline
  683. if(c != '\r') { // Not a carriage return, is normal char
  684. if((c >= first) && (c <= last)) { // Char present in current font
  685. c -= first;
  686. glyph = &(((GFXglyph *)pgm_read_pointer(&gfxFont->glyph))[c]);
  687. gw = pgm_read_byte(&glyph->width);
  688. gh = pgm_read_byte(&glyph->height);
  689. xa = pgm_read_byte(&glyph->xAdvance);
  690. xo = pgm_read_byte(&glyph->xOffset);
  691. yo = pgm_read_byte(&glyph->yOffset);
  692. if(wrap && ((x + (((int16_t)xo + gw) * ts)) >= _width)) {
  693. // Line wrap
  694. x = 0; // Reset x to 0
  695. y += ya; // Advance y by 1 line
  696. }
  697. gx1 = x + xo * ts;
  698. gy1 = y + yo * ts;
  699. gx2 = gx1 + gw * ts - 1;
  700. gy2 = gy1 + gh * ts - 1;
  701. if(gx1 < minx) minx = gx1;
  702. if(gy1 < miny) miny = gy1;
  703. if(gx2 > maxx) maxx = gx2;
  704. if(gy2 > maxy) maxy = gy2;
  705. x += xa * ts;
  706. }
  707. } // Carriage return = do nothing
  708. } else { // Newline
  709. x = 0; // Reset x
  710. y += ya; // Advance y by 1 line
  711. }
  712. }
  713. // End of string
  714. *x1 = minx;
  715. *y1 = miny;
  716. if(maxx >= minx) *w = maxx - minx + 1;
  717. if(maxy >= miny) *h = maxy - miny + 1;
  718. } else { // Default font
  719. uint16_t lineWidth = 0, maxWidth = 0; // Width of current, all lines
  720. while((c = pgm_read_byte(s++))) {
  721. if(c != '\n') { // Not a newline
  722. if(c != '\r') { // Not a carriage return, is normal char
  723. if(wrap && ((x + textsize * 6) >= _width)) {
  724. x = 0; // Reset x to 0
  725. y += textsize * 8; // Advance y by 1 line
  726. if(lineWidth > maxWidth) maxWidth = lineWidth; // Save widest line
  727. lineWidth = textsize * 6; // First char on new line
  728. } else { // No line wrap, just keep incrementing X
  729. lineWidth += textsize * 6; // Includes interchar x gap
  730. }
  731. } // Carriage return = do nothing
  732. } else { // Newline
  733. x = 0; // Reset x to 0
  734. y += textsize * 8; // Advance y by 1 line
  735. if(lineWidth > maxWidth) maxWidth = lineWidth; // Save widest line
  736. lineWidth = 0; // Reset lineWidth for new line
  737. }
  738. }
  739. // End of string
  740. if(lineWidth) y += textsize * 8; // Add height of last (or only) line
  741. *w = maxWidth - 1; // Don't include last interchar x gap
  742. *h = y - *y1;
  743. } // End classic vs custom font
  744. }
  745. // Return the size of the display (per current rotation)
  746. int16_t Adafruit_GFX::width(void) const {
  747. return _width;
  748. }
  749. int16_t Adafruit_GFX::height(void) const {
  750. return _height;
  751. }
  752. void Adafruit_GFX::invertDisplay(boolean i) {
  753. // Do nothing, must be subclassed if supported by hardware
  754. }
  755. /***************************************************************************/
  756. // code for the GFX button UI element
  757. Adafruit_GFX_Button::Adafruit_GFX_Button(void) {
  758. _gfx = 0;
  759. }
  760. void Adafruit_GFX_Button::initButton(
  761. Adafruit_GFX *gfx, int16_t x, int16_t y, uint8_t w, uint8_t h,
  762. uint16_t outline, uint16_t fill, uint16_t textcolor,
  763. char *label, uint8_t textsize)
  764. {
  765. _x = x;
  766. _y = y;
  767. _w = w;
  768. _h = h;
  769. _outlinecolor = outline;
  770. _fillcolor = fill;
  771. _textcolor = textcolor;
  772. _textsize = textsize;
  773. _gfx = gfx;
  774. strncpy(_label, label, 9);
  775. _label[9] = 0;
  776. }
  777. void Adafruit_GFX_Button::drawButton(boolean inverted) {
  778. uint16_t fill, outline, text;
  779. if(!inverted) {
  780. fill = _fillcolor;
  781. outline = _outlinecolor;
  782. text = _textcolor;
  783. } else {
  784. fill = _textcolor;
  785. outline = _outlinecolor;
  786. text = _fillcolor;
  787. }
  788. _gfx->fillRoundRect(_x - (_w/2), _y - (_h/2), _w, _h, min(_w,_h)/4, fill);
  789. _gfx->drawRoundRect(_x - (_w/2), _y - (_h/2), _w, _h, min(_w,_h)/4, outline);
  790. _gfx->setCursor(_x - strlen(_label)*3*_textsize, _y-4*_textsize);
  791. _gfx->setTextColor(text);
  792. _gfx->setTextSize(_textsize);
  793. _gfx->print(_label);
  794. }
  795. boolean Adafruit_GFX_Button::contains(int16_t x, int16_t y) {
  796. if ((x < (_x - _w/2)) || (x > (_x + _w/2))) return false;
  797. if ((y < (_y - _h/2)) || (y > (_y + _h/2))) return false;
  798. return true;
  799. }
  800. void Adafruit_GFX_Button::press(boolean p) {
  801. laststate = currstate;
  802. currstate = p;
  803. }
  804. boolean Adafruit_GFX_Button::isPressed() { return currstate; }
  805. boolean Adafruit_GFX_Button::justPressed() { return (currstate && !laststate); }
  806. boolean Adafruit_GFX_Button::justReleased() { return (!currstate && laststate); }
  807. // -------------------------------------------------------------------------
  808. // GFXcanvas1 and GFXcanvas16 (currently a WIP, don't get too comfy with the
  809. // implementation) provide 1- and 16-bit offscreen canvases, the address of
  810. // which can be passed to drawBitmap() or pushColors() (the latter appears
  811. // to only be in Adafruit_TFTLCD at this time). This is here mostly to
  812. // help with the recently-added proportionally-spaced fonts; adds a way to
  813. // refresh a section of the screen without a massive flickering clear-and-
  814. // redraw...but maybe you'll find other uses too. VERY RAM-intensive, since
  815. // the buffer is in MCU memory and not the display driver...GXFcanvas1 might
  816. // be minimally useful on an Uno-class board, but this and GFXcanvas16 are
  817. // much more likely to require at least a Mega or various recent ARM-type
  818. // boards (recomment, as the text+bitmap draw can be pokey). GFXcanvas1
  819. // requires 1 bit per pixel (rounded up to nearest byte per scanline),
  820. // GFXcanvas16 requires 2 bytes per pixel (no scanline pad).
  821. // NOT EXTENSIVELY TESTED YET. MAY CONTAIN WORST BUGS KNOWN TO HUMANKIND.
  822. GFXcanvas1::GFXcanvas1(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
  823. uint16_t bytes = ((w + 7) / 8) * h;
  824. if((buffer = (uint8_t *)malloc(bytes))) {
  825. memset(buffer, 0, bytes);
  826. }
  827. }
  828. GFXcanvas1::~GFXcanvas1(void) {
  829. if(buffer) free(buffer);
  830. }
  831. uint8_t* GFXcanvas1::getBuffer(void) {
  832. return buffer;
  833. }
  834. void GFXcanvas1::drawPixel(int16_t x, int16_t y, uint16_t color) {
  835. // Bitmask tables of 0x80>>X and ~(0x80>>X), because X>>Y is slow on AVR
  836. static const uint8_t PROGMEM
  837. GFXsetBit[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 },
  838. GFXclrBit[] = { 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0xFE };
  839. if(buffer) {
  840. if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
  841. int16_t t;
  842. switch(rotation) {
  843. case 1:
  844. t = x;
  845. x = WIDTH - 1 - y;
  846. y = t;
  847. break;
  848. case 2:
  849. x = WIDTH - 1 - x;
  850. y = HEIGHT - 1 - y;
  851. break;
  852. case 3:
  853. t = x;
  854. x = y;
  855. y = HEIGHT - 1 - t;
  856. break;
  857. }
  858. uint8_t *ptr = &buffer[(x / 8) + y * ((WIDTH + 7) / 8)];
  859. if(color) *ptr |= pgm_read_byte(&GFXsetBit[x & 7]);
  860. else *ptr &= pgm_read_byte(&GFXclrBit[x & 7]);
  861. }
  862. }
  863. void GFXcanvas1::fillScreen(uint16_t color) {
  864. if(buffer) {
  865. uint16_t bytes = ((WIDTH + 7) / 8) * HEIGHT;
  866. memset(buffer, color ? 0xFF : 0x00, bytes);
  867. }
  868. }
  869. GFXcanvas16::GFXcanvas16(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
  870. uint16_t bytes = w * h * 2;
  871. if((buffer = (uint16_t *)malloc(bytes))) {
  872. memset(buffer, 0, bytes);
  873. }
  874. }
  875. GFXcanvas16::~GFXcanvas16(void) {
  876. if(buffer) free(buffer);
  877. }
  878. uint16_t* GFXcanvas16::getBuffer(void) {
  879. return buffer;
  880. }
  881. void GFXcanvas16::drawPixel(int16_t x, int16_t y, uint16_t color) {
  882. if(buffer) {
  883. if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
  884. int16_t t;
  885. switch(rotation) {
  886. case 1:
  887. t = x;
  888. x = WIDTH - 1 - y;
  889. y = t;
  890. break;
  891. case 2:
  892. x = WIDTH - 1 - x;
  893. y = HEIGHT - 1 - y;
  894. break;
  895. case 3:
  896. t = x;
  897. x = y;
  898. y = HEIGHT - 1 - t;
  899. break;
  900. }
  901. buffer[x + y * WIDTH] = color;
  902. }
  903. }
  904. void GFXcanvas16::fillScreen(uint16_t color) {
  905. if(buffer) {
  906. uint8_t hi = color >> 8, lo = color & 0xFF;
  907. if(hi == lo) {
  908. memset(buffer, lo, WIDTH * HEIGHT * 2);
  909. } else {
  910. uint16_t i, pixels = WIDTH * HEIGHT;
  911. for(i=0; i<pixels; i++) buffer[i] = color;
  912. }
  913. }
  914. }