Adafruit_GFX.cpp 31 KB

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