Adafruit_GFX.cpp 38 KB

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