Adafruit_GFX.cpp 31 KB

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