MBED Import of ADAFRUIT graphics library, BSD License
Dependents: GP9002adafruit GP9002af_gray
Notes on this library
This was imported into MBED specifically to support the GP9002 VFD, as a result it has some "hacks" to optimize it for the GP9002.
Due to the GP9002 internal organization it draws vertical lines much faster than horizontal (assuming you orient it "landscape"). This is likely to be true of other displays that have bits in a byte arranged vertically, which seems a common theme in small GLCDs. Some types may have a more CGA-like memory organization and will draw faster horizontally.
On a vertical-organised display the graphics functions are often substantially faster if X and Y are exchanged, especially with the dot-write optimization. This is because vertical lines can be written byte-at-a-time with no need for read-modify-write, and even when individual bits are written a significant number will "land" in the byte previously written. In contrast a horizontal line would require changing one bit of each byte in turn, requiring a sequence of set-address,read,write operations for each dot in turn.
I've hacked this in the library. I forget exactly how but I believe I simply exchanged X with W in the code for drawing filled shapes.
I would like to come up with a more generic way to do this, such as having internal coordinates that are not defined as X and Y, then leaving it up to the display library to "wrap" them in the way that is best for that display.
I would like to apologize for abandoning this project, but the test harness still exists and I might return to it, though I'm more interested in TFT systems like the STM discovery now.
Adafruit_GFX.h@0:3bf8ef959338, 2016-05-07 (annotated)
- Committer:
- oliverb
- Date:
- Sat May 07 12:50:37 2016 +0000
- Revision:
- 0:3bf8ef959338
Converting to Library, this is a base class that needs extending for a specific device. Note that as it stands it favors devices with vertical bit-organisation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
oliverb | 0:3bf8ef959338 | 1 | #ifndef _ADAFRUIT_GFX_H |
oliverb | 0:3bf8ef959338 | 2 | #define _ADAFRUIT_GFX_H |
oliverb | 0:3bf8ef959338 | 3 | |
oliverb | 0:3bf8ef959338 | 4 | #include "mbed.h" |
oliverb | 0:3bf8ef959338 | 5 | |
oliverb | 0:3bf8ef959338 | 6 | #include "gfxfont.h" |
oliverb | 0:3bf8ef959338 | 7 | |
oliverb | 0:3bf8ef959338 | 8 | class Adafruit_GFX : public Stream { |
oliverb | 0:3bf8ef959338 | 9 | |
oliverb | 0:3bf8ef959338 | 10 | public: |
oliverb | 0:3bf8ef959338 | 11 | |
oliverb | 0:3bf8ef959338 | 12 | Adafruit_GFX(int16_t w, int16_t h); // Constructor |
oliverb | 0:3bf8ef959338 | 13 | |
oliverb | 0:3bf8ef959338 | 14 | // This MUST be defined by the subclass: |
oliverb | 0:3bf8ef959338 | 15 | virtual void drawPixel(int16_t x, int16_t y, uint16_t color) = 0; |
oliverb | 0:3bf8ef959338 | 16 | |
oliverb | 0:3bf8ef959338 | 17 | // These MAY be overridden by the subclass to provide device-specific |
oliverb | 0:3bf8ef959338 | 18 | // optimized code. Otherwise 'generic' versions are used. |
oliverb | 0:3bf8ef959338 | 19 | virtual void |
oliverb | 0:3bf8ef959338 | 20 | drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color), |
oliverb | 0:3bf8ef959338 | 21 | drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color), |
oliverb | 0:3bf8ef959338 | 22 | drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color), |
oliverb | 0:3bf8ef959338 | 23 | drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color), |
oliverb | 0:3bf8ef959338 | 24 | fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color), |
oliverb | 0:3bf8ef959338 | 25 | fillScreen(uint16_t color), |
oliverb | 0:3bf8ef959338 | 26 | invertDisplay(bool i); |
oliverb | 0:3bf8ef959338 | 27 | |
oliverb | 0:3bf8ef959338 | 28 | // These exist only with Adafruit_GFX (no subclass overrides) |
oliverb | 0:3bf8ef959338 | 29 | void |
oliverb | 0:3bf8ef959338 | 30 | drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color), |
oliverb | 0:3bf8ef959338 | 31 | drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, |
oliverb | 0:3bf8ef959338 | 32 | uint16_t color), |
oliverb | 0:3bf8ef959338 | 33 | fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color), |
oliverb | 0:3bf8ef959338 | 34 | fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, |
oliverb | 0:3bf8ef959338 | 35 | int16_t delta, uint16_t color), |
oliverb | 0:3bf8ef959338 | 36 | drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, |
oliverb | 0:3bf8ef959338 | 37 | int16_t x2, int16_t y2, uint16_t color), |
oliverb | 0:3bf8ef959338 | 38 | fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, |
oliverb | 0:3bf8ef959338 | 39 | int16_t x2, int16_t y2, uint16_t color), |
oliverb | 0:3bf8ef959338 | 40 | drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, |
oliverb | 0:3bf8ef959338 | 41 | int16_t radius, uint16_t color), |
oliverb | 0:3bf8ef959338 | 42 | fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, |
oliverb | 0:3bf8ef959338 | 43 | int16_t radius, uint16_t color), |
oliverb | 0:3bf8ef959338 | 44 | drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, |
oliverb | 0:3bf8ef959338 | 45 | int16_t w, int16_t h, uint16_t color), |
oliverb | 0:3bf8ef959338 | 46 | drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, |
oliverb | 0:3bf8ef959338 | 47 | int16_t w, int16_t h, uint16_t color, uint16_t bg), |
oliverb | 0:3bf8ef959338 | 48 | drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, |
oliverb | 0:3bf8ef959338 | 49 | int16_t w, int16_t h, uint16_t color), |
oliverb | 0:3bf8ef959338 | 50 | drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, |
oliverb | 0:3bf8ef959338 | 51 | int16_t w, int16_t h, uint16_t color, uint16_t bg), |
oliverb | 0:3bf8ef959338 | 52 | drawXBitmap(int16_t x, int16_t y, const uint8_t *bitmap, |
oliverb | 0:3bf8ef959338 | 53 | int16_t w, int16_t h, uint16_t color), |
oliverb | 0:3bf8ef959338 | 54 | drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, |
oliverb | 0:3bf8ef959338 | 55 | uint16_t bg, uint8_t size), |
oliverb | 0:3bf8ef959338 | 56 | setCursor(int16_t x, int16_t y), |
oliverb | 0:3bf8ef959338 | 57 | setTextColor(uint16_t c), |
oliverb | 0:3bf8ef959338 | 58 | setTextColor(uint16_t c, uint16_t bg), |
oliverb | 0:3bf8ef959338 | 59 | setTextSize(uint8_t s), |
oliverb | 0:3bf8ef959338 | 60 | setTextWrap(bool w), |
oliverb | 0:3bf8ef959338 | 61 | setRotation(uint8_t r), |
oliverb | 0:3bf8ef959338 | 62 | cp437(bool x=true), |
oliverb | 0:3bf8ef959338 | 63 | setFont(const GFXfont *f = NULL), |
oliverb | 0:3bf8ef959338 | 64 | getTextBounds(const char *string, int16_t x, int16_t y, |
oliverb | 0:3bf8ef959338 | 65 | int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h); |
oliverb | 0:3bf8ef959338 | 66 | /* getTextBounds(const __FlashStringHelper *s, int16_t x, int16_t y, |
oliverb | 0:3bf8ef959338 | 67 | int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h); */ |
oliverb | 0:3bf8ef959338 | 68 | #if DOXYGEN_ONLY |
oliverb | 0:3bf8ef959338 | 69 | /** Write a character to the LCD |
oliverb | 0:3bf8ef959338 | 70 | * |
oliverb | 0:3bf8ef959338 | 71 | * @param c The character to write to the display |
oliverb | 0:3bf8ef959338 | 72 | */ |
oliverb | 0:3bf8ef959338 | 73 | int putc(int c); |
oliverb | 0:3bf8ef959338 | 74 | |
oliverb | 0:3bf8ef959338 | 75 | /** Write a formatted string to the LCD |
oliverb | 0:3bf8ef959338 | 76 | * |
oliverb | 0:3bf8ef959338 | 77 | * @param format A printf-style format string, followed by the |
oliverb | 0:3bf8ef959338 | 78 | * variables to use in formatting the string. |
oliverb | 0:3bf8ef959338 | 79 | */ |
oliverb | 0:3bf8ef959338 | 80 | int printf(const char* format, ...); |
oliverb | 0:3bf8ef959338 | 81 | #endif |
oliverb | 0:3bf8ef959338 | 82 | |
oliverb | 0:3bf8ef959338 | 83 | int16_t height(void) const; |
oliverb | 0:3bf8ef959338 | 84 | int16_t width(void) const; |
oliverb | 0:3bf8ef959338 | 85 | |
oliverb | 0:3bf8ef959338 | 86 | uint8_t getRotation(void) const; |
oliverb | 0:3bf8ef959338 | 87 | |
oliverb | 0:3bf8ef959338 | 88 | // get current cursor position (get rotation safe maximum values, using: width() for x, height() for y) |
oliverb | 0:3bf8ef959338 | 89 | int16_t getCursorX(void) const; |
oliverb | 0:3bf8ef959338 | 90 | int16_t getCursorY(void) const; |
oliverb | 0:3bf8ef959338 | 91 | |
oliverb | 0:3bf8ef959338 | 92 | protected: |
oliverb | 0:3bf8ef959338 | 93 | const int16_t |
oliverb | 0:3bf8ef959338 | 94 | WIDTH, HEIGHT; // This is the 'raw' display w/h - never changes |
oliverb | 0:3bf8ef959338 | 95 | int16_t |
oliverb | 0:3bf8ef959338 | 96 | _width, _height, // Display w/h as modified by current rotation |
oliverb | 0:3bf8ef959338 | 97 | cursor_x, cursor_y; |
oliverb | 0:3bf8ef959338 | 98 | uint16_t |
oliverb | 0:3bf8ef959338 | 99 | textcolor, textbgcolor; |
oliverb | 0:3bf8ef959338 | 100 | uint8_t |
oliverb | 0:3bf8ef959338 | 101 | textsize, |
oliverb | 0:3bf8ef959338 | 102 | rotation; |
oliverb | 0:3bf8ef959338 | 103 | bool |
oliverb | 0:3bf8ef959338 | 104 | wrap, // If set, 'wrap' text at right edge of display |
oliverb | 0:3bf8ef959338 | 105 | _cp437; // If set, use correct CP437 charset (default is off) |
oliverb | 0:3bf8ef959338 | 106 | GFXfont |
oliverb | 0:3bf8ef959338 | 107 | *gfxFont; |
oliverb | 0:3bf8ef959338 | 108 | // Stream implementation functions |
oliverb | 0:3bf8ef959338 | 109 | virtual int _putc(int value); |
oliverb | 0:3bf8ef959338 | 110 | virtual int _getc(); |
oliverb | 0:3bf8ef959338 | 111 | |
oliverb | 0:3bf8ef959338 | 112 | }; |
oliverb | 0:3bf8ef959338 | 113 | |
oliverb | 0:3bf8ef959338 | 114 | class Adafruit_GFX_Button { |
oliverb | 0:3bf8ef959338 | 115 | |
oliverb | 0:3bf8ef959338 | 116 | public: |
oliverb | 0:3bf8ef959338 | 117 | Adafruit_GFX_Button(void); |
oliverb | 0:3bf8ef959338 | 118 | void initButton(Adafruit_GFX *gfx, int16_t x, int16_t y, |
oliverb | 0:3bf8ef959338 | 119 | uint8_t w, uint8_t h, uint16_t outline, uint16_t fill, |
oliverb | 0:3bf8ef959338 | 120 | uint16_t textcolor, char *label, uint8_t textsize); |
oliverb | 0:3bf8ef959338 | 121 | void drawButton(bool inverted = false); |
oliverb | 0:3bf8ef959338 | 122 | bool contains(int16_t x, int16_t y); |
oliverb | 0:3bf8ef959338 | 123 | |
oliverb | 0:3bf8ef959338 | 124 | void press(bool p); |
oliverb | 0:3bf8ef959338 | 125 | bool isPressed(); |
oliverb | 0:3bf8ef959338 | 126 | bool justPressed(); |
oliverb | 0:3bf8ef959338 | 127 | bool justReleased(); |
oliverb | 0:3bf8ef959338 | 128 | |
oliverb | 0:3bf8ef959338 | 129 | private: |
oliverb | 0:3bf8ef959338 | 130 | Adafruit_GFX *_gfx; |
oliverb | 0:3bf8ef959338 | 131 | int16_t _x, _y; |
oliverb | 0:3bf8ef959338 | 132 | uint16_t _w, _h; |
oliverb | 0:3bf8ef959338 | 133 | uint8_t _textsize; |
oliverb | 0:3bf8ef959338 | 134 | uint16_t _outlinecolor, _fillcolor, _textcolor; |
oliverb | 0:3bf8ef959338 | 135 | char _label[10]; |
oliverb | 0:3bf8ef959338 | 136 | |
oliverb | 0:3bf8ef959338 | 137 | bool currstate, laststate; |
oliverb | 0:3bf8ef959338 | 138 | }; |
oliverb | 0:3bf8ef959338 | 139 | |
oliverb | 0:3bf8ef959338 | 140 | class GFXcanvas1 : public Adafruit_GFX { |
oliverb | 0:3bf8ef959338 | 141 | |
oliverb | 0:3bf8ef959338 | 142 | public: |
oliverb | 0:3bf8ef959338 | 143 | GFXcanvas1(uint16_t w, uint16_t h); |
oliverb | 0:3bf8ef959338 | 144 | virtual ~GFXcanvas1(void); |
oliverb | 0:3bf8ef959338 | 145 | virtual void drawPixel(int16_t x, int16_t y, uint16_t color), |
oliverb | 0:3bf8ef959338 | 146 | fillScreen(uint16_t color); |
oliverb | 0:3bf8ef959338 | 147 | uint8_t *getBuffer(void); |
oliverb | 0:3bf8ef959338 | 148 | private: |
oliverb | 0:3bf8ef959338 | 149 | uint8_t *buffer; |
oliverb | 0:3bf8ef959338 | 150 | }; |
oliverb | 0:3bf8ef959338 | 151 | |
oliverb | 0:3bf8ef959338 | 152 | class GFXcanvas16 : public Adafruit_GFX { |
oliverb | 0:3bf8ef959338 | 153 | GFXcanvas16(uint16_t w, uint16_t h); |
oliverb | 0:3bf8ef959338 | 154 | virtual ~GFXcanvas16(void); |
oliverb | 0:3bf8ef959338 | 155 | virtual void drawPixel(int16_t x, int16_t y, uint16_t color), |
oliverb | 0:3bf8ef959338 | 156 | fillScreen(uint16_t color); |
oliverb | 0:3bf8ef959338 | 157 | uint16_t *getBuffer(void); |
oliverb | 0:3bf8ef959338 | 158 | private: |
oliverb | 0:3bf8ef959338 | 159 | uint16_t *buffer; |
oliverb | 0:3bf8ef959338 | 160 | }; |
oliverb | 0:3bf8ef959338 | 161 | |
oliverb | 0:3bf8ef959338 | 162 | #endif // _ADAFRUIT_GFX_H |