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

Committer:
oliverb
Date:
2016-05-07
Revision:
0:3bf8ef959338

File content as of revision 0:3bf8ef959338:

#ifndef _ADAFRUIT_GFX_H
#define _ADAFRUIT_GFX_H

#include "mbed.h"

#include "gfxfont.h"

class Adafruit_GFX : public Stream {

 public:

  Adafruit_GFX(int16_t w, int16_t h); // Constructor

  // This MUST be defined by the subclass:
  virtual void drawPixel(int16_t x, int16_t y, uint16_t color) = 0;

  // These MAY be overridden by the subclass to provide device-specific
  // optimized code.  Otherwise 'generic' versions are used.
  virtual void
    drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color),
    drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color),
    drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color),
    drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color),
    fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color),
    fillScreen(uint16_t color),
    invertDisplay(bool i);

  // These exist only with Adafruit_GFX (no subclass overrides)
  void
    drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color),
    drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername,
      uint16_t color),
    fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color),
    fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername,
      int16_t delta, uint16_t color),
    drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
      int16_t x2, int16_t y2, uint16_t color),
    fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
      int16_t x2, int16_t y2, uint16_t color),
    drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h,
      int16_t radius, uint16_t color),
    fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h,
      int16_t radius, uint16_t color),
    drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap,
      int16_t w, int16_t h, uint16_t color),
    drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap,
      int16_t w, int16_t h, uint16_t color, uint16_t bg),
    drawBitmap(int16_t x, int16_t y, uint8_t *bitmap,
      int16_t w, int16_t h, uint16_t color),
    drawBitmap(int16_t x, int16_t y, uint8_t *bitmap,
      int16_t w, int16_t h, uint16_t color, uint16_t bg),
    drawXBitmap(int16_t x, int16_t y, const uint8_t *bitmap,
      int16_t w, int16_t h, uint16_t color),
    drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color,
      uint16_t bg, uint8_t size),
    setCursor(int16_t x, int16_t y),
    setTextColor(uint16_t c),
    setTextColor(uint16_t c, uint16_t bg),
    setTextSize(uint8_t s),
    setTextWrap(bool w),
    setRotation(uint8_t r),
    cp437(bool x=true),
    setFont(const GFXfont *f = NULL),
    getTextBounds(const char *string, int16_t x, int16_t y,
      int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h);
/*    getTextBounds(const __FlashStringHelper *s, int16_t x, int16_t y,
      int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h); */
#if DOXYGEN_ONLY
    /** Write a character to the LCD
     *
     * @param c The character to write to the display
     */
    int putc(int c);
 
    /** Write a formatted string to the LCD
     *
     * @param format A printf-style format string, followed by the
     *               variables to use in formatting the string.
     */
    int printf(const char* format, ...);   
#endif

  int16_t height(void) const;
  int16_t width(void) const;

  uint8_t getRotation(void) const;

  // get current cursor position (get rotation safe maximum values, using: width() for x, height() for y)
  int16_t getCursorX(void) const;
  int16_t getCursorY(void) const;

 protected:
  const int16_t
    WIDTH, HEIGHT;   // This is the 'raw' display w/h - never changes
  int16_t
    _width, _height, // Display w/h as modified by current rotation
    cursor_x, cursor_y;
  uint16_t
    textcolor, textbgcolor;
  uint8_t
    textsize,
    rotation;
  bool
    wrap,   // If set, 'wrap' text at right edge of display
    _cp437; // If set, use correct CP437 charset (default is off)
  GFXfont
    *gfxFont;
// Stream implementation functions
    virtual int _putc(int value);
    virtual int _getc();

};

class Adafruit_GFX_Button {

 public:
  Adafruit_GFX_Button(void);
  void initButton(Adafruit_GFX *gfx, int16_t x, int16_t y,
   uint8_t w, uint8_t h, uint16_t outline, uint16_t fill,
   uint16_t textcolor, char *label, uint8_t textsize);
  void drawButton(bool inverted = false);
  bool contains(int16_t x, int16_t y);

  void press(bool p);
  bool isPressed();
  bool justPressed();
  bool justReleased();

 private:
  Adafruit_GFX *_gfx;
  int16_t _x, _y;
  uint16_t _w, _h;
  uint8_t _textsize;
  uint16_t _outlinecolor, _fillcolor, _textcolor;
  char _label[10];

  bool currstate, laststate;
};

class GFXcanvas1 : public Adafruit_GFX {

 public:
  GFXcanvas1(uint16_t w, uint16_t h);
  virtual ~GFXcanvas1(void);
  virtual void     drawPixel(int16_t x, int16_t y, uint16_t color),
                   fillScreen(uint16_t color);
  uint8_t *getBuffer(void);
 private:
  uint8_t *buffer;
};

class GFXcanvas16 : public Adafruit_GFX {
  GFXcanvas16(uint16_t w, uint16_t h);
  virtual ~GFXcanvas16(void);
  virtual void      drawPixel(int16_t x, int16_t y, uint16_t color),
                    fillScreen(uint16_t color);
  uint16_t *getBuffer(void);
 private:
  uint16_t *buffer;
};

#endif // _ADAFRUIT_GFX_H