Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Adafruit_GFX.h@0:cb63da12fdcc, 2019-11-26 (annotated)
- Committer:
- wakabanban
- Date:
- Tue Nov 26 04:49:45 2019 +0000
- Revision:
- 0:cb63da12fdcc
ADC Sample
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wakabanban | 0:cb63da12fdcc | 1 | /*********************************** |
wakabanban | 0:cb63da12fdcc | 2 | This is a our graphics core library, for all our displays. |
wakabanban | 0:cb63da12fdcc | 3 | We'll be adapting all the |
wakabanban | 0:cb63da12fdcc | 4 | existing libaries to use this core to make updating, support |
wakabanban | 0:cb63da12fdcc | 5 | and upgrading easier! |
wakabanban | 0:cb63da12fdcc | 6 | |
wakabanban | 0:cb63da12fdcc | 7 | Adafruit invests time and resources providing this open source code, |
wakabanban | 0:cb63da12fdcc | 8 | please support Adafruit and open-source hardware by purchasing |
wakabanban | 0:cb63da12fdcc | 9 | products from Adafruit! |
wakabanban | 0:cb63da12fdcc | 10 | |
wakabanban | 0:cb63da12fdcc | 11 | Written by Limor Fried/Ladyada for Adafruit Industries. |
wakabanban | 0:cb63da12fdcc | 12 | BSD license, check license.txt for more information |
wakabanban | 0:cb63da12fdcc | 13 | All text above must be included in any redistribution |
wakabanban | 0:cb63da12fdcc | 14 | ****************************************/ |
wakabanban | 0:cb63da12fdcc | 15 | |
wakabanban | 0:cb63da12fdcc | 16 | /* |
wakabanban | 0:cb63da12fdcc | 17 | * Modified by Neal Horman 7/14/2012 for use in mbed |
wakabanban | 0:cb63da12fdcc | 18 | */ |
wakabanban | 0:cb63da12fdcc | 19 | |
wakabanban | 0:cb63da12fdcc | 20 | #ifndef _ADAFRUIT_GFX_H_ |
wakabanban | 0:cb63da12fdcc | 21 | #define _ADAFRUIT_GFX_H_ |
wakabanban | 0:cb63da12fdcc | 22 | |
wakabanban | 0:cb63da12fdcc | 23 | #include "Adafruit_GFX_Config.h" |
wakabanban | 0:cb63da12fdcc | 24 | |
wakabanban | 0:cb63da12fdcc | 25 | static inline void swap(int16_t &a, int16_t &b) |
wakabanban | 0:cb63da12fdcc | 26 | { |
wakabanban | 0:cb63da12fdcc | 27 | int16_t t = a; |
wakabanban | 0:cb63da12fdcc | 28 | |
wakabanban | 0:cb63da12fdcc | 29 | a = b; |
wakabanban | 0:cb63da12fdcc | 30 | b = t; |
wakabanban | 0:cb63da12fdcc | 31 | } |
wakabanban | 0:cb63da12fdcc | 32 | |
wakabanban | 0:cb63da12fdcc | 33 | #ifndef _BV |
wakabanban | 0:cb63da12fdcc | 34 | #define _BV(bit) (1<<(bit)) |
wakabanban | 0:cb63da12fdcc | 35 | #endif |
wakabanban | 0:cb63da12fdcc | 36 | |
wakabanban | 0:cb63da12fdcc | 37 | #define BLACK 0 |
wakabanban | 0:cb63da12fdcc | 38 | #define WHITE 1 |
wakabanban | 0:cb63da12fdcc | 39 | |
wakabanban | 0:cb63da12fdcc | 40 | /** |
wakabanban | 0:cb63da12fdcc | 41 | * This is a Text and Graphics element drawing class. |
wakabanban | 0:cb63da12fdcc | 42 | * These functions draw to the display buffer. |
wakabanban | 0:cb63da12fdcc | 43 | * |
wakabanban | 0:cb63da12fdcc | 44 | * Display drivers should be derived from here. |
wakabanban | 0:cb63da12fdcc | 45 | * The Display drivers push the display buffer to the |
wakabanban | 0:cb63da12fdcc | 46 | * hardware based on application control. |
wakabanban | 0:cb63da12fdcc | 47 | * |
wakabanban | 0:cb63da12fdcc | 48 | */ |
wakabanban | 0:cb63da12fdcc | 49 | class Adafruit_GFX : public Stream |
wakabanban | 0:cb63da12fdcc | 50 | { |
wakabanban | 0:cb63da12fdcc | 51 | public: |
wakabanban | 0:cb63da12fdcc | 52 | Adafruit_GFX(int16_t w, int16_t h) |
wakabanban | 0:cb63da12fdcc | 53 | : _rawWidth(w) |
wakabanban | 0:cb63da12fdcc | 54 | , _rawHeight(h) |
wakabanban | 0:cb63da12fdcc | 55 | , _width(w) |
wakabanban | 0:cb63da12fdcc | 56 | , _height(h) |
wakabanban | 0:cb63da12fdcc | 57 | , cursor_x(0) |
wakabanban | 0:cb63da12fdcc | 58 | , cursor_y(0) |
wakabanban | 0:cb63da12fdcc | 59 | , textcolor(WHITE) |
wakabanban | 0:cb63da12fdcc | 60 | , textbgcolor(BLACK) |
wakabanban | 0:cb63da12fdcc | 61 | , textsize(1) |
wakabanban | 0:cb63da12fdcc | 62 | , rotation(0) |
wakabanban | 0:cb63da12fdcc | 63 | , wrap(true) |
wakabanban | 0:cb63da12fdcc | 64 | {}; |
wakabanban | 0:cb63da12fdcc | 65 | |
wakabanban | 0:cb63da12fdcc | 66 | /// Paint one BLACK or WHITE pixel in the display buffer |
wakabanban | 0:cb63da12fdcc | 67 | // this must be defined by the subclass |
wakabanban | 0:cb63da12fdcc | 68 | virtual void drawPixel(int16_t x, int16_t y, uint16_t color) = 0; |
wakabanban | 0:cb63da12fdcc | 69 | // this is optional |
wakabanban | 0:cb63da12fdcc | 70 | virtual void invertDisplay(bool i) {}; |
wakabanban | 0:cb63da12fdcc | 71 | |
wakabanban | 0:cb63da12fdcc | 72 | // Stream implementation - provides printf() interface |
wakabanban | 0:cb63da12fdcc | 73 | // You would otherwise be forced to use writeChar() |
wakabanban | 0:cb63da12fdcc | 74 | virtual int _putc(int value) { return writeChar(value); }; |
wakabanban | 0:cb63da12fdcc | 75 | virtual int _getc() { return -1; }; |
wakabanban | 0:cb63da12fdcc | 76 | |
wakabanban | 0:cb63da12fdcc | 77 | #ifdef GFX_WANT_ABSTRACTS |
wakabanban | 0:cb63da12fdcc | 78 | // these are 'generic' drawing functions, so we can share them! |
wakabanban | 0:cb63da12fdcc | 79 | |
wakabanban | 0:cb63da12fdcc | 80 | /** Draw a Horizontal Line |
wakabanban | 0:cb63da12fdcc | 81 | * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h |
wakabanban | 0:cb63da12fdcc | 82 | */ |
wakabanban | 0:cb63da12fdcc | 83 | virtual void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color); |
wakabanban | 0:cb63da12fdcc | 84 | /** Draw a rectangle |
wakabanban | 0:cb63da12fdcc | 85 | * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h |
wakabanban | 0:cb63da12fdcc | 86 | */ |
wakabanban | 0:cb63da12fdcc | 87 | virtual void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color); |
wakabanban | 0:cb63da12fdcc | 88 | /** Fill the entire display |
wakabanban | 0:cb63da12fdcc | 89 | * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h |
wakabanban | 0:cb63da12fdcc | 90 | */ |
wakabanban | 0:cb63da12fdcc | 91 | virtual void fillScreen(uint16_t color); |
wakabanban | 0:cb63da12fdcc | 92 | |
wakabanban | 0:cb63da12fdcc | 93 | /** Draw a circle |
wakabanban | 0:cb63da12fdcc | 94 | * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h |
wakabanban | 0:cb63da12fdcc | 95 | */ |
wakabanban | 0:cb63da12fdcc | 96 | void drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color); |
wakabanban | 0:cb63da12fdcc | 97 | void drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, uint16_t color); |
wakabanban | 0:cb63da12fdcc | 98 | |
wakabanban | 0:cb63da12fdcc | 99 | /** Draw and fill a circle |
wakabanban | 0:cb63da12fdcc | 100 | * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h |
wakabanban | 0:cb63da12fdcc | 101 | */ |
wakabanban | 0:cb63da12fdcc | 102 | void fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color); |
wakabanban | 0:cb63da12fdcc | 103 | void fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, uint16_t color); |
wakabanban | 0:cb63da12fdcc | 104 | |
wakabanban | 0:cb63da12fdcc | 105 | /** Draw a triangle |
wakabanban | 0:cb63da12fdcc | 106 | * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h |
wakabanban | 0:cb63da12fdcc | 107 | */ |
wakabanban | 0:cb63da12fdcc | 108 | void drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color); |
wakabanban | 0:cb63da12fdcc | 109 | /** Draw and fill a triangle |
wakabanban | 0:cb63da12fdcc | 110 | * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h |
wakabanban | 0:cb63da12fdcc | 111 | */ |
wakabanban | 0:cb63da12fdcc | 112 | void fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color); |
wakabanban | 0:cb63da12fdcc | 113 | |
wakabanban | 0:cb63da12fdcc | 114 | /** Draw a rounded rectangle |
wakabanban | 0:cb63da12fdcc | 115 | * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h |
wakabanban | 0:cb63da12fdcc | 116 | */ |
wakabanban | 0:cb63da12fdcc | 117 | void drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color); |
wakabanban | 0:cb63da12fdcc | 118 | /** Draw and fill a rounded rectangle |
wakabanban | 0:cb63da12fdcc | 119 | * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h |
wakabanban | 0:cb63da12fdcc | 120 | */ |
wakabanban | 0:cb63da12fdcc | 121 | void fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color); |
wakabanban | 0:cb63da12fdcc | 122 | /** Draw a bitmap |
wakabanban | 0:cb63da12fdcc | 123 | * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h |
wakabanban | 0:cb63da12fdcc | 124 | */ |
wakabanban | 0:cb63da12fdcc | 125 | void drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color); |
wakabanban | 0:cb63da12fdcc | 126 | #endif |
wakabanban | 0:cb63da12fdcc | 127 | |
wakabanban | 0:cb63da12fdcc | 128 | #if defined(GFX_WANT_ABSTRACTS) || defined(GFX_SIZEABLE_TEXT) |
wakabanban | 0:cb63da12fdcc | 129 | /** Draw a line |
wakabanban | 0:cb63da12fdcc | 130 | * @note GFX_WANT_ABSTRACTS or GFX_SIZEABLE_TEXT must be defined in Adafruit_GFX_config.h |
wakabanban | 0:cb63da12fdcc | 131 | */ |
wakabanban | 0:cb63da12fdcc | 132 | virtual void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color); |
wakabanban | 0:cb63da12fdcc | 133 | /** Draw a vertical line |
wakabanban | 0:cb63da12fdcc | 134 | * @note GFX_WANT_ABSTRACTS or GFX_SIZEABLE_TEXT must be defined in Adafruit_GFX_config.h |
wakabanban | 0:cb63da12fdcc | 135 | */ |
wakabanban | 0:cb63da12fdcc | 136 | virtual void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color); |
wakabanban | 0:cb63da12fdcc | 137 | /** Draw and fill a rectangle |
wakabanban | 0:cb63da12fdcc | 138 | * @note GFX_WANT_ABSTRACTS or GFX_SIZEABLE_TEXT must be defined in Adafruit_GFX_config.h |
wakabanban | 0:cb63da12fdcc | 139 | */ |
wakabanban | 0:cb63da12fdcc | 140 | virtual void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color); |
wakabanban | 0:cb63da12fdcc | 141 | #endif |
wakabanban | 0:cb63da12fdcc | 142 | |
wakabanban | 0:cb63da12fdcc | 143 | /// Draw a text character at a specified pixel location |
wakabanban | 0:cb63da12fdcc | 144 | void drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, uint8_t size); |
wakabanban | 0:cb63da12fdcc | 145 | /// Draw a text character at the text cursor location |
wakabanban | 0:cb63da12fdcc | 146 | size_t writeChar(uint8_t); |
wakabanban | 0:cb63da12fdcc | 147 | |
wakabanban | 0:cb63da12fdcc | 148 | /// Get the width of the display in pixels |
wakabanban | 0:cb63da12fdcc | 149 | inline int16_t width(void) { return _width; }; |
wakabanban | 0:cb63da12fdcc | 150 | /// Get the height of the display in pixels |
wakabanban | 0:cb63da12fdcc | 151 | inline int16_t height(void) { return _height; }; |
wakabanban | 0:cb63da12fdcc | 152 | |
wakabanban | 0:cb63da12fdcc | 153 | /// Set the text cursor location, based on the size of the text |
wakabanban | 0:cb63da12fdcc | 154 | inline void setTextCursor(int16_t x, int16_t y) { cursor_x = x; cursor_y = y; }; |
wakabanban | 0:cb63da12fdcc | 155 | #if defined(GFX_WANT_ABSTRACTS) || defined(GFX_SIZEABLE_TEXT) |
wakabanban | 0:cb63da12fdcc | 156 | /** Set the size of the text to be drawn |
wakabanban | 0:cb63da12fdcc | 157 | * @note Make sure to enable either GFX_SIZEABLE_TEXT or GFX_WANT_ABSTRACTS |
wakabanban | 0:cb63da12fdcc | 158 | */ |
wakabanban | 0:cb63da12fdcc | 159 | inline void setTextSize(uint8_t s) { textsize = (s > 0) ? s : 1; }; |
wakabanban | 0:cb63da12fdcc | 160 | #endif |
wakabanban | 0:cb63da12fdcc | 161 | /// Set the text foreground and background colors to be the same |
wakabanban | 0:cb63da12fdcc | 162 | inline void setTextColor(uint16_t c) { textcolor = c; textbgcolor = c; } |
wakabanban | 0:cb63da12fdcc | 163 | /// Set the text foreground and background colors independantly |
wakabanban | 0:cb63da12fdcc | 164 | inline void setTextColor(uint16_t c, uint16_t b) { textcolor = c; textbgcolor = b; }; |
wakabanban | 0:cb63da12fdcc | 165 | /// Set text wraping mode true or false |
wakabanban | 0:cb63da12fdcc | 166 | inline void setTextWrap(bool w) { wrap = w; }; |
wakabanban | 0:cb63da12fdcc | 167 | |
wakabanban | 0:cb63da12fdcc | 168 | /// Set the display rotation, 1, 2, 3, or 4 |
wakabanban | 0:cb63da12fdcc | 169 | void setRotation(uint8_t r); |
wakabanban | 0:cb63da12fdcc | 170 | /// Get the current rotation |
wakabanban | 0:cb63da12fdcc | 171 | inline uint8_t getRotation(void) { rotation %= 4; return rotation; }; |
wakabanban | 0:cb63da12fdcc | 172 | |
wakabanban | 0:cb63da12fdcc | 173 | protected: |
wakabanban | 0:cb63da12fdcc | 174 | int16_t _rawWidth, _rawHeight; // this is the 'raw' display w/h - never changes |
wakabanban | 0:cb63da12fdcc | 175 | int16_t _width, _height; // dependent on rotation |
wakabanban | 0:cb63da12fdcc | 176 | int16_t cursor_x, cursor_y; |
wakabanban | 0:cb63da12fdcc | 177 | uint16_t textcolor, textbgcolor; |
wakabanban | 0:cb63da12fdcc | 178 | uint8_t textsize; |
wakabanban | 0:cb63da12fdcc | 179 | uint8_t rotation; |
wakabanban | 0:cb63da12fdcc | 180 | bool wrap; // If set, 'wrap' text at right edge of display |
wakabanban | 0:cb63da12fdcc | 181 | }; |
wakabanban | 0:cb63da12fdcc | 182 | |
wakabanban | 0:cb63da12fdcc | 183 | #endif |