Adafruit_GFX.h 4.8 KB

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