Adafruit_GFX.h 17 KB

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