Adafruit_GFX.cpp 38 KB

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