Adafruit_GFX.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  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. // Draw colored bitmap (each pixel is a uint16_t, with colors defined by
  568. // Draw colored bitmap (each pixel is a uint16_t, with colors defined by
  569. // the drawpixel method of your graphical backend.
  570. // progmem defaults to true for bitmaps defined as static const uint16_t PROGMEM
  571. // but you can set it to false to send a bitmap array in RAM.
  572. void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y,
  573. const uint16_t *bitmap, int16_t w, int16_t h, bool progmem) {
  574. int16_t i, j;
  575. for(j=0; j<h; j++) {
  576. for(i=0; i<w; i++ ) {
  577. if (progmem) {
  578. drawPixel(x+i, y+j, pgm_read_word(bitmap + j * w + i));
  579. } else {
  580. drawPixel(x+i, y+j, (uint16_t) *(bitmap + j * w + i));
  581. }
  582. }
  583. }
  584. }
  585. #if ARDUINO >= 100
  586. size_t Adafruit_GFX::write(uint8_t c) {
  587. #else
  588. void Adafruit_GFX::write(uint8_t c) {
  589. #endif
  590. if(!gfxFont) { // 'Classic' built-in font
  591. if(c == '\n') {
  592. cursor_y += textsize*8;
  593. cursor_x = 0;
  594. } else if(c == '\r') {
  595. // skip em
  596. } else {
  597. if(wrap && ((cursor_x + textsize * 6) >= _width)) { // Heading off edge?
  598. cursor_x = 0; // Reset x to zero
  599. cursor_y += textsize * 8; // Advance y one line
  600. }
  601. drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
  602. cursor_x += textsize * 6;
  603. }
  604. } else { // Custom font
  605. if(c == '\n') {
  606. cursor_x = 0;
  607. cursor_y += (int16_t)textsize *
  608. (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
  609. } else if(c != '\r') {
  610. uint8_t first = pgm_read_byte(&gfxFont->first);
  611. if((c >= first) && (c <= (uint8_t)pgm_read_byte(&gfxFont->last))) {
  612. uint8_t c2 = c - pgm_read_byte(&gfxFont->first);
  613. GFXglyph *glyph = &(((GFXglyph *)pgm_read_pointer(&gfxFont->glyph))[c2]);
  614. uint8_t w = pgm_read_byte(&glyph->width),
  615. h = pgm_read_byte(&glyph->height);
  616. if((w > 0) && (h > 0)) { // Is there an associated bitmap?
  617. int16_t xo = (int8_t)pgm_read_byte(&glyph->xOffset); // sic
  618. if(wrap && ((cursor_x + textsize * (xo + w)) >= _width)) {
  619. // Drawing character would go off right edge; wrap to new line
  620. cursor_x = 0;
  621. cursor_y += (int16_t)textsize *
  622. (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
  623. }
  624. drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
  625. }
  626. cursor_x += pgm_read_byte(&glyph->xAdvance) * (int16_t)textsize;
  627. }
  628. }
  629. }
  630. #if ARDUINO >= 100
  631. return 1;
  632. #endif
  633. }
  634. void Adafruit_GFX::setCursor(int16_t x, int16_t y) {
  635. cursor_x = x;
  636. cursor_y = y;
  637. }
  638. int16_t Adafruit_GFX::getCursorX(void) const {
  639. return cursor_x;
  640. }
  641. int16_t Adafruit_GFX::getCursorY(void) const {
  642. return cursor_y;
  643. }
  644. void Adafruit_GFX::setTextSize(uint8_t s) {
  645. textsize = (s > 0) ? s : 1;
  646. }
  647. void Adafruit_GFX::setTextColor(uint16_t c) {
  648. // For 'transparent' background, we'll set the bg
  649. // to the same as fg instead of using a flag
  650. textcolor = textbgcolor = c;
  651. }
  652. void Adafruit_GFX::setTextColor(uint16_t c, uint16_t b) {
  653. textcolor = c;
  654. textbgcolor = b;
  655. }
  656. void Adafruit_GFX::setTextWrap(boolean w) {
  657. wrap = w;
  658. }
  659. uint8_t Adafruit_GFX::getRotation(void) const {
  660. return rotation;
  661. }
  662. void Adafruit_GFX::setRotation(uint8_t x) {
  663. rotation = (x & 3);
  664. switch(rotation) {
  665. case 0:
  666. case 2:
  667. _width = WIDTH;
  668. _height = HEIGHT;
  669. break;
  670. case 1:
  671. case 3:
  672. _width = HEIGHT;
  673. _height = WIDTH;
  674. break;
  675. }
  676. }
  677. // Enable (or disable) Code Page 437-compatible charset.
  678. // There was an error in glcdfont.c for the longest time -- one character
  679. // (#176, the 'light shade' block) was missing -- this threw off the index
  680. // of every character that followed it. But a TON of code has been written
  681. // with the erroneous character indices. By default, the library uses the
  682. // original 'wrong' behavior and old sketches will still work. Pass 'true'
  683. // to this function to use correct CP437 character values in your code.
  684. void Adafruit_GFX::cp437(boolean x) {
  685. _cp437 = x;
  686. }
  687. void Adafruit_GFX::setFont(const GFXfont *f) {
  688. if(f) { // Font struct pointer passed in?
  689. if(!gfxFont) { // And no current font struct?
  690. // Switching from classic to new font behavior.
  691. // Move cursor pos down 6 pixels so it's on baseline.
  692. cursor_y += 6;
  693. }
  694. } else if(gfxFont) { // NULL passed. Current font struct defined?
  695. // Switching from new to classic font behavior.
  696. // Move cursor pos up 6 pixels so it's at top-left of char.
  697. cursor_y -= 6;
  698. }
  699. gfxFont = (GFXfont *)f;
  700. }
  701. // Pass string and a cursor position, returns UL corner and W,H.
  702. void Adafruit_GFX::getTextBounds(char *str, int16_t x, int16_t y,
  703. int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
  704. uint8_t c; // Current character
  705. *x1 = x;
  706. *y1 = y;
  707. *w = *h = 0;
  708. if(gfxFont) {
  709. GFXglyph *glyph;
  710. uint8_t first = pgm_read_byte(&gfxFont->first),
  711. last = pgm_read_byte(&gfxFont->last),
  712. gw, gh, xa;
  713. int8_t xo, yo;
  714. int16_t minx = _width, miny = _height, maxx = -1, maxy = -1,
  715. gx1, gy1, gx2, gy2, ts = (int16_t)textsize,
  716. ya = ts * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
  717. while((c = *str++)) {
  718. if(c != '\n') { // Not a newline
  719. if(c != '\r') { // Not a carriage return, is normal char
  720. if((c >= first) && (c <= last)) { // Char present in current font
  721. c -= first;
  722. glyph = &(((GFXglyph *)pgm_read_pointer(&gfxFont->glyph))[c]);
  723. gw = pgm_read_byte(&glyph->width);
  724. gh = pgm_read_byte(&glyph->height);
  725. xa = pgm_read_byte(&glyph->xAdvance);
  726. xo = pgm_read_byte(&glyph->xOffset);
  727. yo = pgm_read_byte(&glyph->yOffset);
  728. if(wrap && ((x + (((int16_t)xo + gw) * ts)) >= _width)) {
  729. // Line wrap
  730. x = 0; // Reset x to 0
  731. y += ya; // Advance y by 1 line
  732. }
  733. gx1 = x + xo * ts;
  734. gy1 = y + yo * ts;
  735. gx2 = gx1 + gw * ts - 1;
  736. gy2 = gy1 + gh * ts - 1;
  737. if(gx1 < minx) minx = gx1;
  738. if(gy1 < miny) miny = gy1;
  739. if(gx2 > maxx) maxx = gx2;
  740. if(gy2 > maxy) maxy = gy2;
  741. x += xa * ts;
  742. }
  743. } // Carriage return = do nothing
  744. } else { // Newline
  745. x = 0; // Reset x
  746. y += ya; // Advance y by 1 line
  747. }
  748. }
  749. // End of string
  750. *x1 = minx;
  751. *y1 = miny;
  752. if(maxx >= minx) *w = maxx - minx + 1;
  753. if(maxy >= miny) *h = maxy - miny + 1;
  754. } else { // Default font
  755. uint16_t lineWidth = 0, maxWidth = 0; // Width of current, all lines
  756. while((c = *str++)) {
  757. if(c != '\n') { // Not a newline
  758. if(c != '\r') { // Not a carriage return, is normal char
  759. if(wrap && ((x + textsize * 6) >= _width)) {
  760. x = 0; // Reset x to 0
  761. y += textsize * 8; // Advance y by 1 line
  762. if(lineWidth > maxWidth) maxWidth = lineWidth; // Save widest line
  763. lineWidth = textsize * 6; // First char on new line
  764. } else { // No line wrap, just keep incrementing X
  765. lineWidth += textsize * 6; // Includes interchar x gap
  766. }
  767. } // Carriage return = do nothing
  768. } else { // Newline
  769. x = 0; // Reset x to 0
  770. y += textsize * 8; // Advance y by 1 line
  771. if(lineWidth > maxWidth) maxWidth = lineWidth; // Save widest line
  772. lineWidth = 0; // Reset lineWidth for new line
  773. }
  774. }
  775. // End of string
  776. if(lineWidth) y += textsize * 8; // Add height of last (or only) line
  777. if(lineWidth > maxWidth) maxWidth = lineWidth; // Is the last or only line the widest?
  778. *w = maxWidth - 1; // Don't include last interchar x gap
  779. *h = y - *y1;
  780. } // End classic vs custom font
  781. }
  782. // Same as above, but for PROGMEM strings
  783. void Adafruit_GFX::getTextBounds(const __FlashStringHelper *str,
  784. int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
  785. uint8_t *s = (uint8_t *)str, c;
  786. *x1 = x;
  787. *y1 = y;
  788. *w = *h = 0;
  789. if(gfxFont) {
  790. GFXglyph *glyph;
  791. uint8_t first = pgm_read_byte(&gfxFont->first),
  792. last = pgm_read_byte(&gfxFont->last),
  793. gw, gh, xa;
  794. int8_t xo, yo;
  795. int16_t minx = _width, miny = _height, maxx = -1, maxy = -1,
  796. gx1, gy1, gx2, gy2, ts = (int16_t)textsize,
  797. ya = ts * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
  798. while((c = pgm_read_byte(s++))) {
  799. if(c != '\n') { // Not a newline
  800. if(c != '\r') { // Not a carriage return, is normal char
  801. if((c >= first) && (c <= last)) { // Char present in current font
  802. c -= first;
  803. glyph = &(((GFXglyph *)pgm_read_pointer(&gfxFont->glyph))[c]);
  804. gw = pgm_read_byte(&glyph->width);
  805. gh = pgm_read_byte(&glyph->height);
  806. xa = pgm_read_byte(&glyph->xAdvance);
  807. xo = pgm_read_byte(&glyph->xOffset);
  808. yo = pgm_read_byte(&glyph->yOffset);
  809. if(wrap && ((x + (((int16_t)xo + gw) * ts)) >= _width)) {
  810. // Line wrap
  811. x = 0; // Reset x to 0
  812. y += ya; // Advance y by 1 line
  813. }
  814. gx1 = x + xo * ts;
  815. gy1 = y + yo * ts;
  816. gx2 = gx1 + gw * ts - 1;
  817. gy2 = gy1 + gh * ts - 1;
  818. if(gx1 < minx) minx = gx1;
  819. if(gy1 < miny) miny = gy1;
  820. if(gx2 > maxx) maxx = gx2;
  821. if(gy2 > maxy) maxy = gy2;
  822. x += xa * ts;
  823. }
  824. } // Carriage return = do nothing
  825. } else { // Newline
  826. x = 0; // Reset x
  827. y += ya; // Advance y by 1 line
  828. }
  829. }
  830. // End of string
  831. *x1 = minx;
  832. *y1 = miny;
  833. if(maxx >= minx) *w = maxx - minx + 1;
  834. if(maxy >= miny) *h = maxy - miny + 1;
  835. } else { // Default font
  836. uint16_t lineWidth = 0, maxWidth = 0; // Width of current, all lines
  837. while((c = pgm_read_byte(s++))) {
  838. if(c != '\n') { // Not a newline
  839. if(c != '\r') { // Not a carriage return, is normal char
  840. if(wrap && ((x + textsize * 6) >= _width)) {
  841. x = 0; // Reset x to 0
  842. y += textsize * 8; // Advance y by 1 line
  843. if(lineWidth > maxWidth) maxWidth = lineWidth; // Save widest line
  844. lineWidth = textsize * 6; // First char on new line
  845. } else { // No line wrap, just keep incrementing X
  846. lineWidth += textsize * 6; // Includes interchar x gap
  847. }
  848. } // Carriage return = do nothing
  849. } else { // Newline
  850. x = 0; // Reset x to 0
  851. y += textsize * 8; // Advance y by 1 line
  852. if(lineWidth > maxWidth) maxWidth = lineWidth; // Save widest line
  853. lineWidth = 0; // Reset lineWidth for new line
  854. }
  855. }
  856. // End of string
  857. if(lineWidth) y += textsize * 8; // Add height of last (or only) line
  858. if(lineWidth > maxWidth) maxWidth = lineWidth; // Is the last or only line the widest?
  859. *w = maxWidth - 1; // Don't include last interchar x gap
  860. *h = y - *y1;
  861. } // End classic vs custom font
  862. }
  863. // Return the size of the display (per current rotation)
  864. int16_t Adafruit_GFX::width(void) const {
  865. return _width;
  866. }
  867. int16_t Adafruit_GFX::height(void) const {
  868. return _height;
  869. }
  870. void Adafruit_GFX::invertDisplay(boolean i) {
  871. // Do nothing, must be subclassed if supported by hardware
  872. }
  873. /***************************************************************************/
  874. // code for the GFX button UI element
  875. Adafruit_GFX_Button::Adafruit_GFX_Button(void) {
  876. _gfx = 0;
  877. }
  878. // Classic initButton() function: pass center & size
  879. void Adafruit_GFX_Button::initButton(
  880. Adafruit_GFX *gfx, int16_t x, int16_t y, uint16_t w, uint16_t h,
  881. uint16_t outline, uint16_t fill, uint16_t textcolor,
  882. char *label, uint8_t textsize)
  883. {
  884. // Tweak arguments and pass to the newer initButtonUL() function...
  885. initButtonUL(gfx, x - (w / 2), y - (h / 2), w, h, outline, fill,
  886. textcolor, label, textsize);
  887. }
  888. // Newer function instead accepts upper-left corner & size
  889. void Adafruit_GFX_Button::initButtonUL(
  890. Adafruit_GFX *gfx, int16_t x1, int16_t y1, uint16_t w, uint16_t h,
  891. uint16_t outline, uint16_t fill, uint16_t textcolor,
  892. char *label, uint8_t textsize)
  893. {
  894. _x1 = x1;
  895. _y1 = y1;
  896. _w = w;
  897. _h = h;
  898. _outlinecolor = outline;
  899. _fillcolor = fill;
  900. _textcolor = textcolor;
  901. _textsize = textsize;
  902. _gfx = gfx;
  903. strncpy(_label, label, 9);
  904. }
  905. void Adafruit_GFX_Button::drawButton(boolean inverted) {
  906. uint16_t fill, outline, text;
  907. if(!inverted) {
  908. fill = _fillcolor;
  909. outline = _outlinecolor;
  910. text = _textcolor;
  911. } else {
  912. fill = _textcolor;
  913. outline = _outlinecolor;
  914. text = _fillcolor;
  915. }
  916. uint8_t r = min(_w, _h) / 4; // Corner radius
  917. _gfx->fillRoundRect(_x1, _y1, _w, _h, r, fill);
  918. _gfx->drawRoundRect(_x1, _y1, _w, _h, r, outline);
  919. _gfx->setCursor(_x1 + (_w/2) - (strlen(_label) * 3 * _textsize),
  920. _y1 + (_h/2) - (4 * _textsize));
  921. _gfx->setTextColor(text);
  922. _gfx->setTextSize(_textsize);
  923. _gfx->print(_label);
  924. }
  925. boolean Adafruit_GFX_Button::contains(int16_t x, int16_t y) {
  926. return ((x >= _x1) && (x < (_x1 + _w)) &&
  927. (y >= _y1) && (y < (_y1 + _h)));
  928. }
  929. void Adafruit_GFX_Button::press(boolean p) {
  930. laststate = currstate;
  931. currstate = p;
  932. }
  933. boolean Adafruit_GFX_Button::isPressed() { return currstate; }
  934. boolean Adafruit_GFX_Button::justPressed() { return (currstate && !laststate); }
  935. boolean Adafruit_GFX_Button::justReleased() { return (!currstate && laststate); }
  936. // -------------------------------------------------------------------------
  937. // GFXcanvas1, GFXcanvas8 and GFXcanvas16 (currently a WIP, don't get too
  938. // comfy with the implementation) provide 1-, 8- and 16-bit offscreen
  939. // canvases, the address of which can be passed to drawBitmap() or
  940. // pushColors() (the latter appears only in a couple of GFX-subclassed TFT
  941. // libraries at this time). This is here mostly to help with the recently-
  942. // added proportionally-spaced fonts; adds a way to refresh a section of the
  943. // screen without a massive flickering clear-and-redraw...but maybe you'll
  944. // find other uses too. VERY RAM-intensive, since the buffer is in MCU
  945. // memory and not the display driver...GXFcanvas1 might be minimally useful
  946. // on an Uno-class board, but this and the others are much more likely to
  947. // require at least a Mega or various recent ARM-type boards (recommended,
  948. // as the text+bitmap draw can be pokey). GFXcanvas1 requires 1 bit per
  949. // pixel (rounded up to nearest byte per scanline), GFXcanvas8 is 1 byte
  950. // per pixel (no scanline pad), and GFXcanvas16 uses 2 bytes per pixel (no
  951. // scanline pad).
  952. // NOT EXTENSIVELY TESTED YET. MAY CONTAIN WORST BUGS KNOWN TO HUMANKIND.
  953. GFXcanvas1::GFXcanvas1(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
  954. uint16_t bytes = ((w + 7) / 8) * h;
  955. if((buffer = (uint8_t *)malloc(bytes))) {
  956. memset(buffer, 0, bytes);
  957. }
  958. }
  959. GFXcanvas1::~GFXcanvas1(void) {
  960. if(buffer) free(buffer);
  961. }
  962. uint8_t* GFXcanvas1::getBuffer(void) {
  963. return buffer;
  964. }
  965. void GFXcanvas1::drawPixel(int16_t x, int16_t y, uint8_t color) {
  966. #ifdef __AVR__
  967. // Bitmask tables of 0x80>>X and ~(0x80>>X), because X>>Y is slow on AVR
  968. static const uint8_t PROGMEM
  969. GFXsetBit[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 },
  970. GFXclrBit[] = { 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0xFE };
  971. #endif
  972. if(buffer) {
  973. if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
  974. int16_t t;
  975. switch(rotation) {
  976. case 1:
  977. t = x;
  978. x = WIDTH - 1 - y;
  979. y = t;
  980. break;
  981. case 2:
  982. x = WIDTH - 1 - x;
  983. y = HEIGHT - 1 - y;
  984. break;
  985. case 3:
  986. t = x;
  987. x = y;
  988. y = HEIGHT - 1 - t;
  989. break;
  990. }
  991. uint8_t *ptr = &buffer[(x / 8) + y * ((WIDTH + 7) / 8)];
  992. #ifdef __AVR__
  993. if(color) *ptr |= pgm_read_byte(&GFXsetBit[x & 7]);
  994. else *ptr &= pgm_read_byte(&GFXclrBit[x & 7]);
  995. #else
  996. if(color) *ptr |= 0x80 >> (x & 7);
  997. else *ptr &= ~(0x80 >> (x & 7));
  998. #endif
  999. }
  1000. }
  1001. void GFXcanvas1::fillScreen(uint8_t color) {
  1002. if(buffer) {
  1003. uint16_t bytes = ((WIDTH + 7) / 8) * HEIGHT;
  1004. memset(buffer, color ? 0xFF : 0x00, bytes);
  1005. }
  1006. }
  1007. GFXcanvas8::GFXcanvas8(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
  1008. uint32_t bytes = w * h;
  1009. if((buffer = (uint8_t *)malloc(bytes))) {
  1010. memset(buffer, 0, bytes);
  1011. }
  1012. }
  1013. GFXcanvas8::~GFXcanvas8(void) {
  1014. if(buffer) free(buffer);
  1015. }
  1016. uint8_t* GFXcanvas8::getBuffer(void) {
  1017. return buffer;
  1018. }
  1019. void GFXcanvas8::drawPixel(int16_t x, int16_t y, uint8_t color) {
  1020. if(buffer) {
  1021. if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
  1022. int16_t t;
  1023. switch(rotation) {
  1024. case 1:
  1025. t = x;
  1026. x = WIDTH - 1 - y;
  1027. y = t;
  1028. break;
  1029. case 2:
  1030. x = WIDTH - 1 - x;
  1031. y = HEIGHT - 1 - y;
  1032. break;
  1033. case 3:
  1034. t = x;
  1035. x = y;
  1036. y = HEIGHT - 1 - t;
  1037. break;
  1038. }
  1039. buffer[x + y * WIDTH] = color;
  1040. }
  1041. }
  1042. void GFXcanvas8::fillScreen(uint8_t color) {
  1043. if(buffer) {
  1044. memset(buffer, color, WIDTH * HEIGHT);
  1045. }
  1046. }
  1047. GFXcanvas16::GFXcanvas16(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
  1048. uint32_t bytes = w * h * 2;
  1049. if((buffer = (uint16_t *)malloc(bytes))) {
  1050. memset(buffer, 0, bytes);
  1051. }
  1052. }
  1053. GFXcanvas16::~GFXcanvas16(void) {
  1054. if(buffer) free(buffer);
  1055. }
  1056. uint16_t* GFXcanvas16::getBuffer(void) {
  1057. return buffer;
  1058. }
  1059. void GFXcanvas16::drawPixel(int16_t x, int16_t y, uint16_t color) {
  1060. if(buffer) {
  1061. if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
  1062. int16_t t;
  1063. switch(rotation) {
  1064. case 1:
  1065. t = x;
  1066. x = WIDTH - 1 - y;
  1067. y = t;
  1068. break;
  1069. case 2:
  1070. x = WIDTH - 1 - x;
  1071. y = HEIGHT - 1 - y;
  1072. break;
  1073. case 3:
  1074. t = x;
  1075. x = y;
  1076. y = HEIGHT - 1 - t;
  1077. break;
  1078. }
  1079. buffer[x + y * WIDTH] = color;
  1080. }
  1081. }
  1082. void GFXcanvas16::fillScreen(uint16_t color) {
  1083. if(buffer) {
  1084. uint8_t hi = color >> 8, lo = color & 0xFF;
  1085. if(hi == lo) {
  1086. memset(buffer, lo, WIDTH * HEIGHT * 2);
  1087. } else {
  1088. uint32_t i, pixels = WIDTH * HEIGHT;
  1089. for(i=0; i<pixels; i++) buffer[i] = color;
  1090. }
  1091. }
  1092. }