Adafruit_GFX.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #ifndef _ADAFRUIT_GFX_H
  2. #define _ADAFRUIT_GFX_H
  3. #if ARDUINO >= 100
  4. #include "Arduino.h"
  5. #include "Print.h"
  6. #else
  7. #include "WProgram.h"
  8. #endif
  9. #include "gfxfont.h"
  10. /// A generic graphics superclass that can handle all sorts of drawing. At a
  11. /// minimum you can subclass and provide drawPixel(). At a maximum you can do a
  12. /// ton of overriding to optimize. Used for any/all Adafruit displays!
  13. class Adafruit_GFX : public Print {
  14. public:
  15. Adafruit_GFX(int16_t w, int16_t h); // Constructor
  16. /**********************************************************************/
  17. /*!
  18. @brief Draw to the screen/framebuffer/etc.
  19. Must be overridden in subclass.
  20. @param x X coordinate in pixels
  21. @param y Y coordinate in pixels
  22. @param color 16-bit pixel color.
  23. */
  24. /**********************************************************************/
  25. virtual void drawPixel(int16_t x, int16_t y, uint16_t color) = 0;
  26. // TRANSACTION API / CORE DRAW API
  27. // These MAY be overridden by the subclass to provide device-specific
  28. // optimized code. Otherwise 'generic' versions are used.
  29. virtual void startWrite(void);
  30. virtual void writePixel(int16_t x, int16_t y, uint16_t color);
  31. virtual void writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h,
  32. uint16_t color);
  33. virtual void writeFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
  34. virtual void writeFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
  35. virtual void writeLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
  36. uint16_t color);
  37. virtual void endWrite(void);
  38. // CONTROL API
  39. // These MAY be overridden by the subclass to provide device-specific
  40. // optimized code. Otherwise 'generic' versions are used.
  41. virtual void setRotation(uint8_t r);
  42. virtual void invertDisplay(bool i);
  43. // BASIC DRAW API
  44. // These MAY be overridden by the subclass to provide device-specific
  45. // optimized code. Otherwise 'generic' versions are used.
  46. // It's good to implement those, even if using transaction API
  47. virtual void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
  48. virtual void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
  49. virtual void fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
  50. uint16_t color);
  51. virtual void fillScreen(uint16_t color);
  52. // Optional and probably not necessary to change
  53. virtual void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
  54. uint16_t color);
  55. virtual void drawRect(int16_t x, int16_t y, int16_t w, int16_t h,
  56. uint16_t color);
  57. // These exist only with Adafruit_GFX (no subclass overrides)
  58. void drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
  59. void drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername,
  60. uint16_t color);
  61. void fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
  62. void fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername,
  63. int16_t delta, uint16_t color);
  64. void drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2,
  65. int16_t y2, uint16_t color);
  66. void fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2,
  67. int16_t y2, uint16_t color);
  68. void drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h,
  69. int16_t radius, uint16_t color);
  70. void fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h,
  71. int16_t radius, uint16_t color);
  72. void drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w,
  73. int16_t h, uint16_t color);
  74. void drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w,
  75. int16_t h, uint16_t color, uint16_t bg);
  76. void drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h,
  77. uint16_t color);
  78. void drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h,
  79. uint16_t color, uint16_t bg);
  80. void drawXBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w,
  81. int16_t h, uint16_t color);
  82. void drawGrayscaleBitmap(int16_t x, int16_t y, const uint8_t bitmap[],
  83. int16_t w, int16_t h);
  84. void drawGrayscaleBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w,
  85. int16_t h);
  86. void drawGrayscaleBitmap(int16_t x, int16_t y, const uint8_t bitmap[],
  87. const uint8_t mask[], int16_t w, int16_t h);
  88. void drawGrayscaleBitmap(int16_t x, int16_t y, uint8_t *bitmap, uint8_t *mask,
  89. int16_t w, int16_t h);
  90. void drawRGBBitmap(int16_t x, int16_t y, const uint16_t bitmap[], int16_t w,
  91. int16_t h);
  92. void drawRGBBitmap(int16_t x, int16_t y, uint16_t *bitmap, int16_t w,
  93. int16_t h);
  94. void drawRGBBitmap(int16_t x, int16_t y, const uint16_t bitmap[],
  95. const uint8_t mask[], int16_t w, int16_t h);
  96. void drawRGBBitmap(int16_t x, int16_t y, uint16_t *bitmap, uint8_t *mask,
  97. int16_t w, int16_t h);
  98. void drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color,
  99. uint16_t bg, uint8_t size);
  100. void drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color,
  101. uint16_t bg, uint8_t size_x, uint8_t size_y);
  102. void getTextBounds(const char *string, int16_t x, int16_t y, int16_t *x1,
  103. int16_t *y1, uint16_t *w, uint16_t *h);
  104. void getTextBounds(const __FlashStringHelper *s, int16_t x, int16_t y,
  105. int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h);
  106. void getTextBounds(const String &str, int16_t x, int16_t y, int16_t *x1,
  107. int16_t *y1, uint16_t *w, uint16_t *h);
  108. void setTextSize(uint8_t s);
  109. void setTextSize(uint8_t sx, uint8_t sy);
  110. void setFont(const GFXfont *f = NULL);
  111. /**********************************************************************/
  112. /*!
  113. @brief Set text cursor location
  114. @param x X coordinate in pixels
  115. @param y Y coordinate in pixels
  116. */
  117. /**********************************************************************/
  118. void setCursor(int16_t x, int16_t y) {
  119. cursor_x = x;
  120. cursor_y = y;
  121. }
  122. /**********************************************************************/
  123. /*!
  124. @brief Set text font color with transparant background
  125. @param c 16-bit 5-6-5 Color to draw text with
  126. @note For 'transparent' background, background and foreground
  127. are set to same color rather than using a separate flag.
  128. */
  129. /**********************************************************************/
  130. void setTextColor(uint16_t c) { textcolor = textbgcolor = c; }
  131. /**********************************************************************/
  132. /*!
  133. @brief Set text font color with custom background color
  134. @param c 16-bit 5-6-5 Color to draw text with
  135. @param bg 16-bit 5-6-5 Color to draw background/fill with
  136. */
  137. /**********************************************************************/
  138. void setTextColor(uint16_t c, uint16_t bg) {
  139. textcolor = c;
  140. textbgcolor = bg;
  141. }
  142. /**********************************************************************/
  143. /*!
  144. @brief Set whether text that is too long for the screen width should
  145. automatically wrap around to the next line (else clip right).
  146. @param w true for wrapping, false for clipping
  147. */
  148. /**********************************************************************/
  149. void setTextWrap(bool w) { wrap = w; }
  150. /**********************************************************************/
  151. /*!
  152. @brief Enable (or disable) Code Page 437-compatible charset.
  153. There was an error in glcdfont.c for the longest time -- one
  154. character (#176, the 'light shade' block) was missing -- this
  155. threw off the index of every character that followed it.
  156. But a TON of code has been written with the erroneous
  157. character indices. By default, the library uses the original
  158. 'wrong' behavior and old sketches will still work. Pass
  159. 'true' to this function to use correct CP437 character values
  160. in your code.
  161. @param x true = enable (new behavior), false = disable (old behavior)
  162. */
  163. /**********************************************************************/
  164. void cp437(bool x = true) { _cp437 = x; }
  165. using Print::write;
  166. #if ARDUINO >= 100
  167. virtual size_t write(uint8_t);
  168. #else
  169. virtual void write(uint8_t);
  170. #endif
  171. /************************************************************************/
  172. /*!
  173. @brief Get width of the display, accounting for current rotation
  174. @returns Width in pixels
  175. */
  176. /************************************************************************/
  177. int16_t width(void) const { return _width; };
  178. /************************************************************************/
  179. /*!
  180. @brief Get height of the display, accounting for current rotation
  181. @returns Height in pixels
  182. */
  183. /************************************************************************/
  184. int16_t height(void) const { return _height; }
  185. /************************************************************************/
  186. /*!
  187. @brief Get rotation setting for display
  188. @returns 0 thru 3 corresponding to 4 cardinal rotations
  189. */
  190. /************************************************************************/
  191. uint8_t getRotation(void) const { return rotation; }
  192. // get current cursor position (get rotation safe maximum values,
  193. // using: width() for x, height() for y)
  194. /************************************************************************/
  195. /*!
  196. @brief Get text cursor X location
  197. @returns X coordinate in pixels
  198. */
  199. /************************************************************************/
  200. int16_t getCursorX(void) const { return cursor_x; }
  201. /************************************************************************/
  202. /*!
  203. @brief Get text cursor Y location
  204. @returns Y coordinate in pixels
  205. */
  206. /************************************************************************/
  207. int16_t getCursorY(void) const { return cursor_y; };
  208. protected:
  209. void charBounds(unsigned char c, int16_t *x, int16_t *y, int16_t *minx,
  210. int16_t *miny, int16_t *maxx, int16_t *maxy);
  211. int16_t WIDTH; ///< This is the 'raw' display width - never changes
  212. int16_t HEIGHT; ///< This is the 'raw' display height - never changes
  213. int16_t _width; ///< Display width as modified by current rotation
  214. int16_t _height; ///< Display height as modified by current rotation
  215. int16_t cursor_x; ///< x location to start print()ing text
  216. int16_t cursor_y; ///< y location to start print()ing text
  217. uint16_t textcolor; ///< 16-bit background color for print()
  218. uint16_t textbgcolor; ///< 16-bit text color for print()
  219. uint8_t textsize_x; ///< Desired magnification in X-axis of text to print()
  220. uint8_t textsize_y; ///< Desired magnification in Y-axis of text to print()
  221. uint8_t rotation; ///< Display rotation (0 thru 3)
  222. bool wrap; ///< If set, 'wrap' text at right edge of display
  223. bool _cp437; ///< If set, use correct CP437 charset (default is off)
  224. GFXfont *gfxFont; ///< Pointer to special font
  225. };
  226. /// A simple drawn button UI element
  227. class Adafruit_GFX_Button {
  228. public:
  229. Adafruit_GFX_Button(void);
  230. // "Classic" initButton() uses center & size
  231. void initButton(Adafruit_GFX *gfx, int16_t x, int16_t y, uint16_t w,
  232. uint16_t h, uint16_t outline, uint16_t fill,
  233. uint16_t textcolor, char *label, uint8_t textsize);
  234. void initButton(Adafruit_GFX *gfx, int16_t x, int16_t y, uint16_t w,
  235. uint16_t h, uint16_t outline, uint16_t fill,
  236. uint16_t textcolor, char *label, uint8_t textsize_x,
  237. uint8_t textsize_y);
  238. // New/alt initButton() uses upper-left corner & size
  239. void initButtonUL(Adafruit_GFX *gfx, int16_t x1, int16_t y1, uint16_t w,
  240. uint16_t h, uint16_t outline, uint16_t fill,
  241. uint16_t textcolor, char *label, uint8_t textsize);
  242. void initButtonUL(Adafruit_GFX *gfx, int16_t x1, int16_t y1, uint16_t w,
  243. uint16_t h, uint16_t outline, uint16_t fill,
  244. uint16_t textcolor, char *label, uint8_t textsize_x,
  245. uint8_t textsize_y);
  246. void drawButton(bool inverted = false);
  247. bool contains(int16_t x, int16_t y);
  248. /**********************************************************************/
  249. /*!
  250. @brief Sets button state, should be done by some touch function
  251. @param p True for pressed, false for not.
  252. */
  253. /**********************************************************************/
  254. void press(bool p) {
  255. laststate = currstate;
  256. currstate = p;
  257. }
  258. bool justPressed();
  259. bool justReleased();
  260. /**********************************************************************/
  261. /*!
  262. @brief Query whether the button is currently pressed
  263. @returns True if pressed
  264. */
  265. /**********************************************************************/
  266. bool isPressed(void) { return currstate; };
  267. private:
  268. Adafruit_GFX *_gfx;
  269. int16_t _x1, _y1; // Coordinates of top-left corner
  270. uint16_t _w, _h;
  271. uint8_t _textsize_x;
  272. uint8_t _textsize_y;
  273. uint16_t _outlinecolor, _fillcolor, _textcolor;
  274. char _label[10];
  275. bool currstate, laststate;
  276. };
  277. /// A GFX 1-bit canvas context for graphics
  278. class GFXcanvas1 : public Adafruit_GFX {
  279. public:
  280. GFXcanvas1(uint16_t w, uint16_t h);
  281. ~GFXcanvas1(void);
  282. void drawPixel(int16_t x, int16_t y, uint16_t color);
  283. void fillScreen(uint16_t color);
  284. bool getPixel(int16_t x, int16_t y) const;
  285. /**********************************************************************/
  286. /*!
  287. @brief Get a pointer to the internal buffer memory
  288. @returns A pointer to the allocated buffer
  289. */
  290. /**********************************************************************/
  291. uint8_t *getBuffer(void) const { return buffer; }
  292. protected:
  293. bool getRawPixel(int16_t x, int16_t y) const;
  294. private:
  295. uint8_t *buffer;
  296. #ifdef __AVR__
  297. // Bitmask tables of 0x80>>X and ~(0x80>>X), because X>>Y is slow on AVR
  298. static const uint8_t PROGMEM GFXsetBit[], GFXclrBit[];
  299. #endif
  300. };
  301. /// A GFX 8-bit canvas context for graphics
  302. class GFXcanvas8 : public Adafruit_GFX {
  303. public:
  304. GFXcanvas8(uint16_t w, uint16_t h);
  305. ~GFXcanvas8(void);
  306. void drawPixel(int16_t x, int16_t y, uint16_t color);
  307. void fillScreen(uint16_t color);
  308. void writeFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
  309. uint8_t getPixel(int16_t x, int16_t y) const;
  310. /**********************************************************************/
  311. /*!
  312. @brief Get a pointer to the internal buffer memory
  313. @returns A pointer to the allocated buffer
  314. */
  315. /**********************************************************************/
  316. uint8_t *getBuffer(void) const { return buffer; }
  317. protected:
  318. uint8_t getRawPixel(int16_t x, int16_t y) const;
  319. private:
  320. uint8_t *buffer;
  321. };
  322. /// A GFX 16-bit canvas context for graphics
  323. class GFXcanvas16 : public Adafruit_GFX {
  324. public:
  325. GFXcanvas16(uint16_t w, uint16_t h);
  326. ~GFXcanvas16(void);
  327. void drawPixel(int16_t x, int16_t y, uint16_t color);
  328. void fillScreen(uint16_t color);
  329. void byteSwap(void);
  330. uint16_t getPixel(int16_t x, int16_t y) const;
  331. /**********************************************************************/
  332. /*!
  333. @brief Get a pointer to the internal buffer memory
  334. @returns A pointer to the allocated buffer
  335. */
  336. /**********************************************************************/
  337. uint16_t *getBuffer(void) const { return buffer; }
  338. protected:
  339. uint16_t getRawPixel(int16_t x, int16_t y) const;
  340. private:
  341. uint16_t *buffer;
  342. };
  343. #endif // _ADAFRUIT_GFX_H