Adafruit_GFX.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  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. // BITMAP / XBITMAP / GRAYSCALE / RGB BITMAP FUNCTIONS ---------------------
  406. // Draw a PROGMEM-resident 1-bit image at the specified (x,y) position,
  407. // using the specified foreground color (unset bits are transparent).
  408. void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
  409. PROGMEM const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color) {
  410. int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
  411. uint8_t byte = 0;
  412. startWrite();
  413. for(int16_t j=0; j<h; j++, y++) {
  414. for(int16_t 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, color);
  418. }
  419. }
  420. endWrite();
  421. }
  422. // Draw a PROGMEM-resident 1-bit image at the specified (x,y) position,
  423. // using the specified foreground (for set bits) and background (unset
  424. // bits) colors.
  425. void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
  426. PROGMEM const uint8_t bitmap[], int16_t w, int16_t h,
  427. uint16_t color, uint16_t bg) {
  428. int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
  429. uint8_t byte = 0;
  430. startWrite();
  431. for(int16_t j=0; j<h; j++, y++) {
  432. for(int16_t i=0; i<w; i++ ) {
  433. if(i & 7) byte <<= 1;
  434. else byte = pgm_read_byte(&bitmap[j * byteWidth + i / 8]);
  435. writePixel(x+i, y, (byte & 0x80) ? color : bg);
  436. }
  437. }
  438. endWrite();
  439. }
  440. // Draw a RAM-resident 1-bit image at the specified (x,y) position,
  441. // using the specified foreground color (unset bits are transparent).
  442. void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
  443. uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) {
  444. int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
  445. uint8_t byte = 0;
  446. startWrite();
  447. for(int16_t j=0; j<h; j++, y++) {
  448. for(int16_t i=0; i<w; i++ ) {
  449. if(i & 7) byte <<= 1;
  450. else byte = bitmap[j * byteWidth + i / 8];
  451. if(byte & 0x80) writePixel(x+i, y, color);
  452. }
  453. }
  454. endWrite();
  455. }
  456. // Draw a RAM-resident 1-bit image at the specified (x,y) position,
  457. // using the specified foreground (for set bits) and background (unset
  458. // bits) colors.
  459. void Adafruit_GFX::drawBitmap(int16_t x, int16_t y,
  460. uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg) {
  461. int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
  462. uint8_t byte = 0;
  463. startWrite();
  464. for(int16_t j=0; j<h; j++, y++) {
  465. for(int16_t i=0; i<w; i++ ) {
  466. if(i & 7) byte <<= 1;
  467. else byte = bitmap[j * byteWidth + i / 8];
  468. writePixel(x+i, y, (byte & 0x80) ? color : bg);
  469. }
  470. }
  471. endWrite();
  472. }
  473. // Draw PROGMEM-resident XBitMap Files (*.xbm), exported from GIMP,
  474. // Usage: Export from GIMP to *.xbm, rename *.xbm to *.c and open in editor.
  475. // C Array can be directly used with this function.
  476. // There is no RAM-resident version of this function; if generating bitmaps
  477. // in RAM, use the format defined by drawBitmap() and call that instead.
  478. void Adafruit_GFX::drawXBitmap(int16_t x, int16_t y,
  479. PROGMEM const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color) {
  480. int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
  481. uint8_t byte = 0;
  482. startWrite();
  483. for(int16_t j=0; j<h; j++, y++) {
  484. for(int16_t i=0; i<w; i++ ) {
  485. if(i & 7) byte >>= 1;
  486. else byte = pgm_read_byte(&bitmap[j * byteWidth + i / 8]);
  487. // Nearly identical to drawBitmap(), only the bit order
  488. // is reversed here (left-to-right = LSB to MSB):
  489. if(byte & 0x01) writePixel(x+i, y, color);
  490. }
  491. }
  492. endWrite();
  493. }
  494. // Draw a PROGMEM-resident 8-bit image (grayscale) at the specified (x,y)
  495. // pos. Specifically for 8-bit display devices such as IS31FL3731;
  496. // no color reduction/expansion is performed.
  497. void Adafruit_GFX::drawGrayscaleBitmap(int16_t x, int16_t y,
  498. PROGMEM const uint8_t bitmap[], int16_t w, int16_t h) {
  499. startWrite();
  500. for(int16_t j=0; j<h; j++, y++) {
  501. for(int16_t i=0; i<w; i++ ) {
  502. writePixel(x+i, y, (uint8_t)pgm_read_byte(&bitmap[j * w + i]));
  503. }
  504. }
  505. endWrite();
  506. }
  507. // Draw a RAM-resident 8-bit image (grayscale) at the specified (x,y)
  508. // pos. Specifically for 8-bit display devices such as IS31FL3731;
  509. // no color reduction/expansion is performed.
  510. void Adafruit_GFX::drawGrayscaleBitmap(int16_t x, int16_t y,
  511. uint8_t *bitmap, int16_t w, int16_t h) {
  512. startWrite();
  513. for(int16_t j=0; j<h; j++, y++) {
  514. for(int16_t i=0; i<w; i++ ) {
  515. writePixel(x+i, y, bitmap[j * w + i]);
  516. }
  517. }
  518. endWrite();
  519. }
  520. // Draw a PROGMEM-resident 8-bit image (grayscale) with a 1-bit mask
  521. // (set bits = opaque, unset bits = clear) at the specified (x,y) position.
  522. // BOTH buffers (grayscale and mask) must be PROGMEM-resident.
  523. // Specifically for 8-bit display devices such as IS31FL3731;
  524. // no color reduction/expansion is performed.
  525. void Adafruit_GFX::drawGrayscaleBitmap(int16_t x, int16_t y,
  526. PROGMEM const uint8_t bitmap[], PROGMEM const uint8_t mask[],
  527. int16_t w, int16_t h) {
  528. int16_t bw = (w + 7) / 8; // Bitmask scanline pad = whole byte
  529. uint8_t byte = 0;
  530. startWrite();
  531. for(int16_t j=0; j<h; j++, y++) {
  532. for(int16_t i=0; i<w; i++ ) {
  533. if(i & 7) byte <<= 1;
  534. else byte = pgm_read_byte(&mask[j * bw + i / 8]);
  535. if(byte & 0x80) {
  536. writePixel(x+i, y, (uint8_t)pgm_read_byte(&bitmap[j * w + i]));
  537. }
  538. }
  539. }
  540. endWrite();
  541. }
  542. // Draw a RAM-resident 8-bit image (grayscale) with a 1-bit mask
  543. // (set bits = opaque, unset bits = clear) at the specified (x,y) pos.
  544. // BOTH buffers (grayscale and mask) must be RAM-resident, no mix-and-
  545. // match. Specifically for 8-bit display devices such as IS31FL3731;
  546. // no color reduction/expansion is performed.
  547. void Adafruit_GFX::drawGrayscaleBitmap(int16_t x, int16_t y,
  548. uint8_t *bitmap, uint8_t *mask, int16_t w, int16_t h) {
  549. int16_t bw = (w + 7) / 8; // Bitmask scanline pad = whole byte
  550. uint8_t byte = 0;
  551. startWrite();
  552. for(int16_t j=0; j<h; j++, y++) {
  553. for(int16_t i=0; i<w; i++ ) {
  554. if(i & 7) byte <<= 1;
  555. else byte = mask[j * bw + i / 8];
  556. if(byte & 0x80) {
  557. writePixel(x+i, y, bitmap[j * w + i]);
  558. }
  559. }
  560. }
  561. endWrite();
  562. }
  563. // Draw a PROGMEM-resident 16-bit image (RGB 5/6/5) at the specified (x,y)
  564. // position. For 16-bit display devices; no color reduction performed.
  565. void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y,
  566. PROGMEM const uint16_t bitmap[], int16_t w, int16_t h) {
  567. startWrite();
  568. for(int16_t j=0; j<h; j++, y++) {
  569. for(int16_t i=0; i<w; i++ ) {
  570. writePixel(x+i, y, pgm_read_word(&bitmap[j * w + i]));
  571. }
  572. }
  573. endWrite();
  574. }
  575. // Draw a RAM-resident 16-bit image (RGB 5/6/5) at the specified (x,y)
  576. // position. For 16-bit display devices; no color reduction performed.
  577. void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y,
  578. uint16_t *bitmap, int16_t w, int16_t h) {
  579. startWrite();
  580. for(int16_t j=0; j<h; j++, y++) {
  581. for(int16_t i=0; i<w; i++ ) {
  582. writePixel(x+i, y, bitmap[j * w + i]);
  583. }
  584. }
  585. endWrite();
  586. }
  587. // Draw a PROGMEM-resident 16-bit image (RGB 5/6/5) with a 1-bit mask
  588. // (set bits = opaque, unset bits = clear) at the specified (x,y) position.
  589. // BOTH buffers (color and mask) must be PROGMEM-resident.
  590. // For 16-bit display devices; no color reduction performed.
  591. void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y,
  592. PROGMEM const uint16_t bitmap[], PROGMEM const uint8_t mask[],
  593. int16_t w, int16_t h) {
  594. int16_t bw = (w + 7) / 8; // Bitmask scanline pad = whole byte
  595. uint8_t byte = 0;
  596. startWrite();
  597. for(int16_t j=0; j<h; j++, y++) {
  598. for(int16_t i=0; i<w; i++ ) {
  599. if(i & 7) byte <<= 1;
  600. else byte = pgm_read_byte(&mask[j * bw + i / 8]);
  601. if(byte & 0x80) {
  602. writePixel(x+i, y, pgm_read_word(&bitmap[j * w + i]));
  603. }
  604. }
  605. }
  606. endWrite();
  607. }
  608. // Draw a RAM-resident 16-bit image (RGB 5/6/5) with a 1-bit mask
  609. // (set bits = opaque, unset bits = clear) at the specified (x,y) pos.
  610. // BOTH buffers (color and mask) must be RAM-resident, no mix-and-match.
  611. // For 16-bit display devices; no color reduction performed.
  612. void Adafruit_GFX::drawRGBBitmap(int16_t x, int16_t y,
  613. uint16_t *bitmap, uint8_t *mask, int16_t w, int16_t h) {
  614. int16_t bw = (w + 7) / 8; // Bitmask scanline pad = whole byte
  615. uint8_t byte = 0;
  616. startWrite();
  617. for(int16_t j=0; j<h; j++, y++) {
  618. for(int16_t i=0; i<w; i++ ) {
  619. if(i & 7) byte <<= 1;
  620. else byte = mask[j * bw + i / 8];
  621. if(byte & 0x80) {
  622. writePixel(x+i, y, bitmap[j * w + i]);
  623. }
  624. }
  625. }
  626. endWrite();
  627. }
  628. // TEXT- AND CHARACTER-HANDLING FUNCTIONS ----------------------------------
  629. // Draw a character
  630. void Adafruit_GFX::drawChar(int16_t x, int16_t y, unsigned char c,
  631. uint16_t color, uint16_t bg, uint8_t size) {
  632. if(!gfxFont) { // 'Classic' built-in font
  633. if((x >= _width) || // Clip right
  634. (y >= _height) || // Clip bottom
  635. ((x + 6 * size - 1) < 0) || // Clip left
  636. ((y + 8 * size - 1) < 0)) // Clip top
  637. return;
  638. if(!_cp437 && (c >= 176)) c++; // Handle 'classic' charset behavior
  639. startWrite();
  640. for(int8_t i=0; i<5; i++ ) { // Char bitmap = 5 columns
  641. uint8_t line = pgm_read_byte(&font[c * 5 + i]);
  642. for(int8_t j=0; j<8; j++, line >>= 1) {
  643. if(line & 1) {
  644. if(size == 1)
  645. writePixel(x+i, y+j, color);
  646. else
  647. writeFillRect(x+i*size, y+j*size, size, size, color);
  648. } else if(bg != color) {
  649. if(size == 1)
  650. writePixel(x+i, y+j, bg);
  651. else
  652. writeFillRect(x+i*size, y+j*size, size, size, bg);
  653. }
  654. }
  655. }
  656. if(bg != color) { // If opaque, draw vertical line for last column
  657. if(size == 1) writeFastVLine(x+5, y, 8, bg);
  658. else writeFillRect(x+5*size, y, size, 8*size, bg);
  659. }
  660. endWrite();
  661. } else { // Custom font
  662. // Character is assumed previously filtered by write() to eliminate
  663. // newlines, returns, non-printable characters, etc. Calling
  664. // drawChar() directly with 'bad' characters of font may cause mayhem!
  665. c -= (uint8_t)pgm_read_byte(&gfxFont->first);
  666. GFXglyph *glyph = &(((GFXglyph *)pgm_read_pointer(&gfxFont->glyph))[c]);
  667. uint8_t *bitmap = (uint8_t *)pgm_read_pointer(&gfxFont->bitmap);
  668. uint16_t bo = pgm_read_word(&glyph->bitmapOffset);
  669. uint8_t w = pgm_read_byte(&glyph->width),
  670. h = pgm_read_byte(&glyph->height);
  671. int8_t xo = pgm_read_byte(&glyph->xOffset),
  672. yo = pgm_read_byte(&glyph->yOffset);
  673. uint8_t xx, yy, bits = 0, bit = 0;
  674. int16_t xo16 = 0, yo16 = 0;
  675. if(size > 1) {
  676. xo16 = xo;
  677. yo16 = yo;
  678. }
  679. // Todo: Add character clipping here
  680. // NOTE: THERE IS NO 'BACKGROUND' COLOR OPTION ON CUSTOM FONTS.
  681. // THIS IS ON PURPOSE AND BY DESIGN. The background color feature
  682. // has typically been used with the 'classic' font to overwrite old
  683. // screen contents with new data. This ONLY works because the
  684. // characters are a uniform size; it's not a sensible thing to do with
  685. // proportionally-spaced fonts with glyphs of varying sizes (and that
  686. // may overlap). To replace previously-drawn text when using a custom
  687. // font, use the getTextBounds() function to determine the smallest
  688. // rectangle encompassing a string, erase the area with fillRect(),
  689. // then draw new text. This WILL infortunately 'blink' the text, but
  690. // is unavoidable. Drawing 'background' pixels will NOT fix this,
  691. // only creates a new set of problems. Have an idea to work around
  692. // this (a canvas object type for MCUs that can afford the RAM and
  693. // displays supporting setAddrWindow() and pushColors()), but haven't
  694. // implemented this yet.
  695. startWrite();
  696. for(yy=0; yy<h; yy++) {
  697. for(xx=0; xx<w; xx++) {
  698. if(!(bit++ & 7)) {
  699. bits = pgm_read_byte(&bitmap[bo++]);
  700. }
  701. if(bits & 0x80) {
  702. if(size == 1) {
  703. writePixel(x+xo+xx, y+yo+yy, color);
  704. } else {
  705. writeFillRect(x+(xo16+xx)*size, y+(yo16+yy)*size,
  706. size, size, color);
  707. }
  708. }
  709. bits <<= 1;
  710. }
  711. }
  712. endWrite();
  713. } // End classic vs custom font
  714. }
  715. #if ARDUINO >= 100
  716. size_t Adafruit_GFX::write(uint8_t c) {
  717. #else
  718. void Adafruit_GFX::write(uint8_t c) {
  719. #endif
  720. if(!gfxFont) { // 'Classic' built-in font
  721. if(c == '\n') { // Newline?
  722. cursor_x = 0; // Reset x to zero,
  723. cursor_y += textsize * 8; // advance y one line
  724. } else if(c != '\r') { // Ignore carriage returns
  725. if(wrap && ((cursor_x + textsize * 6) > _width)) { // Off right?
  726. cursor_x = 0; // Reset x to zero,
  727. cursor_y += textsize * 8; // advance y one line
  728. }
  729. drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
  730. cursor_x += textsize * 6; // Advance x one char
  731. }
  732. } else { // Custom font
  733. if(c == '\n') {
  734. cursor_x = 0;
  735. cursor_y += (int16_t)textsize *
  736. (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
  737. } else if(c != '\r') {
  738. uint8_t first = pgm_read_byte(&gfxFont->first);
  739. if((c >= first) && (c <= (uint8_t)pgm_read_byte(&gfxFont->last))) {
  740. GFXglyph *glyph = &(((GFXglyph *)pgm_read_pointer(
  741. &gfxFont->glyph))[c - first]);
  742. uint8_t w = pgm_read_byte(&glyph->width),
  743. h = pgm_read_byte(&glyph->height);
  744. if((w > 0) && (h > 0)) { // Is there an associated bitmap?
  745. int16_t xo = (int8_t)pgm_read_byte(&glyph->xOffset); // sic
  746. if(wrap && ((cursor_x + textsize * (xo + w)) > _width)) {
  747. cursor_x = 0;
  748. cursor_y += (int16_t)textsize *
  749. (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
  750. }
  751. drawChar(cursor_x, cursor_y, c, textcolor, textbgcolor, textsize);
  752. }
  753. cursor_x += (uint8_t)pgm_read_byte(&glyph->xAdvance) * (int16_t)textsize;
  754. }
  755. }
  756. }
  757. #if ARDUINO >= 100
  758. return 1;
  759. #endif
  760. }
  761. void Adafruit_GFX::setCursor(int16_t x, int16_t y) {
  762. cursor_x = x;
  763. cursor_y = y;
  764. }
  765. int16_t Adafruit_GFX::getCursorX(void) const {
  766. return cursor_x;
  767. }
  768. int16_t Adafruit_GFX::getCursorY(void) const {
  769. return cursor_y;
  770. }
  771. void Adafruit_GFX::setTextSize(uint8_t s) {
  772. textsize = (s > 0) ? s : 1;
  773. }
  774. void Adafruit_GFX::setTextColor(uint16_t c) {
  775. // For 'transparent' background, we'll set the bg
  776. // to the same as fg instead of using a flag
  777. textcolor = textbgcolor = c;
  778. }
  779. void Adafruit_GFX::setTextColor(uint16_t c, uint16_t b) {
  780. textcolor = c;
  781. textbgcolor = b;
  782. }
  783. void Adafruit_GFX::setTextWrap(boolean w) {
  784. wrap = w;
  785. }
  786. uint8_t Adafruit_GFX::getRotation(void) const {
  787. return rotation;
  788. }
  789. void Adafruit_GFX::setRotation(uint8_t x) {
  790. rotation = (x & 3);
  791. switch(rotation) {
  792. case 0:
  793. case 2:
  794. _width = WIDTH;
  795. _height = HEIGHT;
  796. break;
  797. case 1:
  798. case 3:
  799. _width = HEIGHT;
  800. _height = WIDTH;
  801. break;
  802. }
  803. }
  804. // Enable (or disable) Code Page 437-compatible charset.
  805. // There was an error in glcdfont.c for the longest time -- one character
  806. // (#176, the 'light shade' block) was missing -- this threw off the index
  807. // of every character that followed it. But a TON of code has been written
  808. // with the erroneous character indices. By default, the library uses the
  809. // original 'wrong' behavior and old sketches will still work. Pass 'true'
  810. // to this function to use correct CP437 character values in your code.
  811. void Adafruit_GFX::cp437(boolean x) {
  812. _cp437 = x;
  813. }
  814. void Adafruit_GFX::setFont(const GFXfont *f) {
  815. if(f) { // Font struct pointer passed in?
  816. if(!gfxFont) { // And no current font struct?
  817. // Switching from classic to new font behavior.
  818. // Move cursor pos down 6 pixels so it's on baseline.
  819. cursor_y += 6;
  820. }
  821. } else if(gfxFont) { // NULL passed. Current font struct defined?
  822. // Switching from new to classic font behavior.
  823. // Move cursor pos up 6 pixels so it's at top-left of char.
  824. cursor_y -= 6;
  825. }
  826. gfxFont = (GFXfont *)f;
  827. }
  828. // Broke this out as it's used by both the PROGMEM- and RAM-resident
  829. // getTextBounds() functions.
  830. void Adafruit_GFX::charBounds(char c, int16_t *x, int16_t *y,
  831. int16_t *minx, int16_t *miny, int16_t *maxx, int16_t *maxy) {
  832. if(gfxFont) {
  833. if(c == '\n') { // Newline?
  834. *x = 0; // Reset x to zero, advance y by one line
  835. *y += textsize * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
  836. } else if(c != '\r') { // Not a carriage return; is normal char
  837. uint8_t first = pgm_read_byte(&gfxFont->first),
  838. last = pgm_read_byte(&gfxFont->last);
  839. if((c >= first) && (c <= last)) { // Char present in this font?
  840. GFXglyph *glyph = &(((GFXglyph *)pgm_read_pointer(
  841. &gfxFont->glyph))[c - first]);
  842. uint8_t gw = pgm_read_byte(&glyph->width),
  843. gh = pgm_read_byte(&glyph->height),
  844. xa = pgm_read_byte(&glyph->xAdvance);
  845. int8_t xo = pgm_read_byte(&glyph->xOffset),
  846. yo = pgm_read_byte(&glyph->yOffset);
  847. if(wrap && ((*x+(((int16_t)xo+gw)*textsize)) > _width)) {
  848. *x = 0; // Reset x to zero, advance y by one line
  849. *y += textsize * (uint8_t)pgm_read_byte(&gfxFont->yAdvance);
  850. }
  851. int16_t ts = (int16_t)textsize,
  852. x1 = *x + xo * ts,
  853. y1 = *y + yo * ts,
  854. x2 = x1 + gw * ts - 1,
  855. y2 = y1 + gh * ts - 1;
  856. if(x1 < *minx) *minx = x1;
  857. if(y1 < *miny) *miny = y1;
  858. if(x2 > *maxx) *maxx = x2;
  859. if(y2 > *maxy) *maxy = y2;
  860. *x += xa * ts;
  861. }
  862. }
  863. } else { // Default font
  864. if(c == '\n') { // Newline?
  865. *x = 0; // Reset x to zero,
  866. *y += textsize * 8; // advance y one line
  867. // min/max x/y unchaged -- that waits for next 'normal' character
  868. } else if(c != '\r') { // Normal char; ignore carriage returns
  869. if(wrap && ((*x + textsize * 6) > _width)) { // Off right?
  870. *x = 0; // Reset x to zero,
  871. *y += textsize * 8; // advance y one line
  872. }
  873. int x2 = *x + textsize * 6 - 1, // Lower-right pixel of char
  874. y2 = *y + textsize * 8 - 1;
  875. if(x2 > *maxx) *maxx = x2; // Track max x, y
  876. if(y2 > *maxy) *maxy = y2;
  877. if(*x < *minx) *minx = *x; // Track min x, y
  878. if(*y < *miny) *miny = *y;
  879. *x += textsize * 6; // Advance x one char
  880. }
  881. }
  882. }
  883. // Pass string and a cursor position, returns UL corner and W,H.
  884. void Adafruit_GFX::getTextBounds(char *str, int16_t x, int16_t y,
  885. int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
  886. uint8_t c; // Current character
  887. *x1 = x;
  888. *y1 = y;
  889. *w = *h = 0;
  890. int16_t minx = _width, miny = _height, maxx = -1, maxy = -1;
  891. while((c = *str++))
  892. charBounds(c, &x, &y, &minx, &miny, &maxx, &maxy);
  893. if(maxx >= minx) {
  894. *x1 = minx;
  895. *w = maxx - minx + 1;
  896. }
  897. if(maxy >= miny) {
  898. *y1 = miny;
  899. *h = maxy - miny + 1;
  900. }
  901. }
  902. // Same as above, but for PROGMEM strings
  903. void Adafruit_GFX::getTextBounds(const __FlashStringHelper *str,
  904. int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h) {
  905. uint8_t *s = (uint8_t *)str, c;
  906. *x1 = x;
  907. *y1 = y;
  908. *w = *h = 0;
  909. int16_t minx = _width, miny = _height, maxx = -1, maxy = -1;
  910. while((c = pgm_read_byte(s++)))
  911. charBounds(c, &x, &y, &minx, &miny, &maxx, &maxy);
  912. if(maxx >= minx) {
  913. *x1 = minx;
  914. *w = maxx - minx + 1;
  915. }
  916. if(maxy >= miny) {
  917. *y1 = miny;
  918. *h = maxy - miny + 1;
  919. }
  920. }
  921. // Return the size of the display (per current rotation)
  922. int16_t Adafruit_GFX::width(void) const {
  923. return _width;
  924. }
  925. int16_t Adafruit_GFX::height(void) const {
  926. return _height;
  927. }
  928. void Adafruit_GFX::invertDisplay(boolean i) {
  929. // Do nothing, must be subclassed if supported by hardware
  930. }
  931. /***************************************************************************/
  932. // code for the GFX button UI element
  933. Adafruit_GFX_Button::Adafruit_GFX_Button(void) {
  934. _gfx = 0;
  935. }
  936. // Classic initButton() function: pass center & size
  937. void Adafruit_GFX_Button::initButton(
  938. Adafruit_GFX *gfx, int16_t x, int16_t y, uint16_t w, uint16_t h,
  939. uint16_t outline, uint16_t fill, uint16_t textcolor,
  940. char *label, uint8_t textsize)
  941. {
  942. // Tweak arguments and pass to the newer initButtonUL() function...
  943. initButtonUL(gfx, x - (w / 2), y - (h / 2), w, h, outline, fill,
  944. textcolor, label, textsize);
  945. }
  946. // Newer function instead accepts upper-left corner & size
  947. void Adafruit_GFX_Button::initButtonUL(
  948. Adafruit_GFX *gfx, int16_t x1, int16_t y1, uint16_t w, uint16_t h,
  949. uint16_t outline, uint16_t fill, uint16_t textcolor,
  950. char *label, uint8_t textsize)
  951. {
  952. _x1 = x1;
  953. _y1 = y1;
  954. _w = w;
  955. _h = h;
  956. _outlinecolor = outline;
  957. _fillcolor = fill;
  958. _textcolor = textcolor;
  959. _textsize = textsize;
  960. _gfx = gfx;
  961. strncpy(_label, label, 9);
  962. }
  963. void Adafruit_GFX_Button::drawButton(boolean inverted) {
  964. uint16_t fill, outline, text;
  965. if(!inverted) {
  966. fill = _fillcolor;
  967. outline = _outlinecolor;
  968. text = _textcolor;
  969. } else {
  970. fill = _textcolor;
  971. outline = _outlinecolor;
  972. text = _fillcolor;
  973. }
  974. uint8_t r = min(_w, _h) / 4; // Corner radius
  975. _gfx->fillRoundRect(_x1, _y1, _w, _h, r, fill);
  976. _gfx->drawRoundRect(_x1, _y1, _w, _h, r, outline);
  977. _gfx->setCursor(_x1 + (_w/2) - (strlen(_label) * 3 * _textsize),
  978. _y1 + (_h/2) - (4 * _textsize));
  979. _gfx->setTextColor(text);
  980. _gfx->setTextSize(_textsize);
  981. _gfx->print(_label);
  982. }
  983. boolean Adafruit_GFX_Button::contains(int16_t x, int16_t y) {
  984. return ((x >= _x1) && (x < (_x1 + _w)) &&
  985. (y >= _y1) && (y < (_y1 + _h)));
  986. }
  987. void Adafruit_GFX_Button::press(boolean p) {
  988. laststate = currstate;
  989. currstate = p;
  990. }
  991. boolean Adafruit_GFX_Button::isPressed() { return currstate; }
  992. boolean Adafruit_GFX_Button::justPressed() { return (currstate && !laststate); }
  993. boolean Adafruit_GFX_Button::justReleased() { return (!currstate && laststate); }
  994. // -------------------------------------------------------------------------
  995. // GFXcanvas1, GFXcanvas8 and GFXcanvas16 (currently a WIP, don't get too
  996. // comfy with the implementation) provide 1-, 8- and 16-bit offscreen
  997. // canvases, the address of which can be passed to drawBitmap() or
  998. // pushColors() (the latter appears only in a couple of GFX-subclassed TFT
  999. // libraries at this time). This is here mostly to help with the recently-
  1000. // added proportionally-spaced fonts; adds a way to refresh a section of the
  1001. // screen without a massive flickering clear-and-redraw...but maybe you'll
  1002. // find other uses too. VERY RAM-intensive, since the buffer is in MCU
  1003. // memory and not the display driver...GXFcanvas1 might be minimally useful
  1004. // on an Uno-class board, but this and the others are much more likely to
  1005. // require at least a Mega or various recent ARM-type boards (recommended,
  1006. // as the text+bitmap draw can be pokey). GFXcanvas1 requires 1 bit per
  1007. // pixel (rounded up to nearest byte per scanline), GFXcanvas8 is 1 byte
  1008. // per pixel (no scanline pad), and GFXcanvas16 uses 2 bytes per pixel (no
  1009. // scanline pad).
  1010. // NOT EXTENSIVELY TESTED YET. MAY CONTAIN WORST BUGS KNOWN TO HUMANKIND.
  1011. GFXcanvas1::GFXcanvas1(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
  1012. uint16_t bytes = ((w + 7) / 8) * h;
  1013. if((buffer = (uint8_t *)malloc(bytes))) {
  1014. memset(buffer, 0, bytes);
  1015. }
  1016. }
  1017. GFXcanvas1::~GFXcanvas1(void) {
  1018. if(buffer) free(buffer);
  1019. }
  1020. uint8_t* GFXcanvas1::getBuffer(void) {
  1021. return buffer;
  1022. }
  1023. void GFXcanvas1::drawPixel(int16_t x, int16_t y, uint16_t color) {
  1024. #ifdef __AVR__
  1025. // Bitmask tables of 0x80>>X and ~(0x80>>X), because X>>Y is slow on AVR
  1026. static const uint8_t PROGMEM
  1027. GFXsetBit[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 },
  1028. GFXclrBit[] = { 0x7F, 0xBF, 0xDF, 0xEF, 0xF7, 0xFB, 0xFD, 0xFE };
  1029. #endif
  1030. if(buffer) {
  1031. if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
  1032. int16_t t;
  1033. switch(rotation) {
  1034. case 1:
  1035. t = x;
  1036. x = WIDTH - 1 - y;
  1037. y = t;
  1038. break;
  1039. case 2:
  1040. x = WIDTH - 1 - x;
  1041. y = HEIGHT - 1 - y;
  1042. break;
  1043. case 3:
  1044. t = x;
  1045. x = y;
  1046. y = HEIGHT - 1 - t;
  1047. break;
  1048. }
  1049. uint8_t *ptr = &buffer[(x / 8) + y * ((WIDTH + 7) / 8)];
  1050. #ifdef __AVR__
  1051. if(color) *ptr |= pgm_read_byte(&GFXsetBit[x & 7]);
  1052. else *ptr &= pgm_read_byte(&GFXclrBit[x & 7]);
  1053. #else
  1054. if(color) *ptr |= 0x80 >> (x & 7);
  1055. else *ptr &= ~(0x80 >> (x & 7));
  1056. #endif
  1057. }
  1058. }
  1059. void GFXcanvas1::fillScreen(uint16_t color) {
  1060. if(buffer) {
  1061. uint16_t bytes = ((WIDTH + 7) / 8) * HEIGHT;
  1062. memset(buffer, color ? 0xFF : 0x00, bytes);
  1063. }
  1064. }
  1065. GFXcanvas8::GFXcanvas8(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
  1066. uint32_t bytes = w * h;
  1067. if((buffer = (uint8_t *)malloc(bytes))) {
  1068. memset(buffer, 0, bytes);
  1069. }
  1070. }
  1071. GFXcanvas8::~GFXcanvas8(void) {
  1072. if(buffer) free(buffer);
  1073. }
  1074. uint8_t* GFXcanvas8::getBuffer(void) {
  1075. return buffer;
  1076. }
  1077. void GFXcanvas8::drawPixel(int16_t x, int16_t y, uint16_t color) {
  1078. if(buffer) {
  1079. if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
  1080. int16_t t;
  1081. switch(rotation) {
  1082. case 1:
  1083. t = x;
  1084. x = WIDTH - 1 - y;
  1085. y = t;
  1086. break;
  1087. case 2:
  1088. x = WIDTH - 1 - x;
  1089. y = HEIGHT - 1 - y;
  1090. break;
  1091. case 3:
  1092. t = x;
  1093. x = y;
  1094. y = HEIGHT - 1 - t;
  1095. break;
  1096. }
  1097. buffer[x + y * WIDTH] = color;
  1098. }
  1099. }
  1100. void GFXcanvas8::fillScreen(uint16_t color) {
  1101. if(buffer) {
  1102. memset(buffer, color, WIDTH * HEIGHT);
  1103. }
  1104. }
  1105. void GFXcanvas8::writeFastHLine(int16_t x, int16_t y,
  1106. int16_t w, uint16_t color) {
  1107. if((x >= _width) || (y < 0) || (y >= _height)) return;
  1108. int16_t x2 = x + w - 1;
  1109. if(x2 < 0) return;
  1110. // Clip left/right
  1111. if(x < 0) {
  1112. x = 0;
  1113. w = x2 + 1;
  1114. }
  1115. if(x2 >= _width) w = _width - x;
  1116. int16_t t;
  1117. switch(rotation) {
  1118. case 1:
  1119. t = x;
  1120. x = WIDTH - 1 - y;
  1121. y = t;
  1122. break;
  1123. case 2:
  1124. x = WIDTH - 1 - x;
  1125. y = HEIGHT - 1 - y;
  1126. break;
  1127. case 3:
  1128. t = x;
  1129. x = y;
  1130. y = HEIGHT - 1 - t;
  1131. break;
  1132. }
  1133. memset(buffer + y * WIDTH + x, color, w);
  1134. }
  1135. GFXcanvas16::GFXcanvas16(uint16_t w, uint16_t h) : Adafruit_GFX(w, h) {
  1136. uint32_t bytes = w * h * 2;
  1137. if((buffer = (uint16_t *)malloc(bytes))) {
  1138. memset(buffer, 0, bytes);
  1139. }
  1140. }
  1141. GFXcanvas16::~GFXcanvas16(void) {
  1142. if(buffer) free(buffer);
  1143. }
  1144. uint16_t* GFXcanvas16::getBuffer(void) {
  1145. return buffer;
  1146. }
  1147. void GFXcanvas16::drawPixel(int16_t x, int16_t y, uint16_t color) {
  1148. if(buffer) {
  1149. if((x < 0) || (y < 0) || (x >= _width) || (y >= _height)) return;
  1150. int16_t t;
  1151. switch(rotation) {
  1152. case 1:
  1153. t = x;
  1154. x = WIDTH - 1 - y;
  1155. y = t;
  1156. break;
  1157. case 2:
  1158. x = WIDTH - 1 - x;
  1159. y = HEIGHT - 1 - y;
  1160. break;
  1161. case 3:
  1162. t = x;
  1163. x = y;
  1164. y = HEIGHT - 1 - t;
  1165. break;
  1166. }
  1167. buffer[x + y * WIDTH] = color;
  1168. }
  1169. }
  1170. void GFXcanvas16::fillScreen(uint16_t color) {
  1171. if(buffer) {
  1172. uint8_t hi = color >> 8, lo = color & 0xFF;
  1173. if(hi == lo) {
  1174. memset(buffer, lo, WIDTH * HEIGHT * 2);
  1175. } else {
  1176. uint32_t i, pixels = WIDTH * HEIGHT;
  1177. for(i=0; i<pixels; i++) buffer[i] = color;
  1178. }
  1179. }
  1180. }