Adafruit_GFX.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #define adagfxswap(a, b) { int16_t t = a; a = b; b = t; }
  11. #if !defined(ESP8266)
  12. #define swap(a, b) adagfxswap(a, b)
  13. #endif
  14. class Adafruit_GFX : public Print {
  15. public:
  16. Adafruit_GFX(int16_t w, int16_t h); // Constructor
  17. // This MUST be defined by the subclass:
  18. virtual void drawPixel(int16_t x, int16_t y, uint16_t color) = 0;
  19. // These MAY be overridden by the subclass to provide device-specific
  20. // optimized code. Otherwise 'generic' versions are used.
  21. virtual void
  22. drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color),
  23. drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color),
  24. drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color),
  25. drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color),
  26. fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color),
  27. fillScreen(uint16_t color),
  28. invertDisplay(boolean i);
  29. // These exist only with Adafruit_GFX (no subclass overrides)
  30. void
  31. drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color),
  32. drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername,
  33. uint16_t color),
  34. fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color),
  35. fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername,
  36. int16_t delta, uint16_t color),
  37. drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
  38. int16_t x2, int16_t y2, uint16_t color),
  39. fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
  40. int16_t x2, int16_t y2, uint16_t color),
  41. drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h,
  42. int16_t radius, uint16_t color),
  43. fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h,
  44. int16_t radius, uint16_t color),
  45. drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap,
  46. int16_t w, int16_t h, uint16_t color),
  47. drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap,
  48. int16_t w, int16_t h, uint16_t color, uint16_t bg),
  49. drawBitmap(int16_t x, int16_t y, uint8_t *bitmap,
  50. int16_t w, int16_t h, uint16_t color),
  51. drawBitmap(int16_t x, int16_t y, uint8_t *bitmap,
  52. int16_t w, int16_t h, uint16_t color, uint16_t bg),
  53. drawXBitmap(int16_t x, int16_t y, const uint8_t *bitmap,
  54. int16_t w, int16_t h, uint16_t color),
  55. drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color,
  56. uint16_t bg, uint8_t size),
  57. setCursor(int16_t x, int16_t y),
  58. setTextColor(uint16_t c),
  59. setTextColor(uint16_t c, uint16_t bg),
  60. setTextSize(uint8_t s),
  61. setTextWrap(boolean w),
  62. setRotation(uint8_t r),
  63. cp437(boolean x=true),
  64. setFont(const GFXfont *f = NULL),
  65. getTextBounds(char *string, int16_t x, int16_t y,
  66. int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h),
  67. getTextBounds(const __FlashStringHelper *s, int16_t x, int16_t y,
  68. int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h);
  69. #if ARDUINO >= 100
  70. virtual size_t write(uint8_t);
  71. #else
  72. virtual void write(uint8_t);
  73. #endif
  74. int16_t height(void) const;
  75. int16_t width(void) const;
  76. uint8_t getRotation(void) const;
  77. // get current cursor position (get rotation safe maximum values, using: width() for x, height() for y)
  78. int16_t getCursorX(void) const;
  79. int16_t getCursorY(void) const;
  80. protected:
  81. const int16_t
  82. WIDTH, HEIGHT; // This is the 'raw' display w/h - never changes
  83. int16_t
  84. _width, _height, // Display w/h as modified by current rotation
  85. cursor_x, cursor_y;
  86. uint16_t
  87. textcolor, textbgcolor;
  88. uint8_t
  89. textsize,
  90. rotation;
  91. boolean
  92. wrap, // If set, 'wrap' text at right edge of display
  93. _cp437; // If set, use correct CP437 charset (default is off)
  94. GFXfont
  95. *gfxFont;
  96. };
  97. class Adafruit_GFX_Button {
  98. public:
  99. Adafruit_GFX_Button(void);
  100. void initButton(Adafruit_GFX *gfx, int16_t x, int16_t y,
  101. uint8_t w, uint8_t h, uint16_t outline, uint16_t fill,
  102. uint16_t textcolor, char *label, uint8_t textsize);
  103. void drawButton(boolean inverted = false);
  104. boolean contains(int16_t x, int16_t y);
  105. void press(boolean p);
  106. boolean isPressed();
  107. boolean justPressed();
  108. boolean justReleased();
  109. private:
  110. Adafruit_GFX *_gfx;
  111. int16_t _x, _y;
  112. uint16_t _w, _h;
  113. uint8_t _textsize;
  114. uint16_t _outlinecolor, _fillcolor, _textcolor;
  115. char _label[10];
  116. boolean currstate, laststate;
  117. };
  118. class GFXcanvas1 : public Adafruit_GFX {
  119. public:
  120. GFXcanvas1(uint16_t w, uint16_t h);
  121. ~GFXcanvas1(void);
  122. void drawPixel(int16_t x, int16_t y, uint16_t color),
  123. fillScreen(uint16_t color);
  124. uint8_t *getBuffer(void);
  125. private:
  126. uint8_t *buffer;
  127. };
  128. class GFXcanvas16 : public Adafruit_GFX {
  129. GFXcanvas16(uint16_t w, uint16_t h);
  130. ~GFXcanvas16(void);
  131. void drawPixel(int16_t x, int16_t y, uint16_t color),
  132. fillScreen(uint16_t color);
  133. uint16_t *getBuffer(void);
  134. private:
  135. uint16_t *buffer;
  136. };
  137. #endif // _ADAFRUIT_GFX_H