arduinoprojects

git clone https://git.tarina.org/arduinoprojects
Log | Files | Refs

Adafruit_GFX.h (16835B)


      1 #ifndef _ADAFRUIT_GFX_H
      2 #define _ADAFRUIT_GFX_H
      3 
      4 #if ARDUINO >= 100
      5 #include "Arduino.h"
      6 #include "Print.h"
      7 #else
      8 #include "WProgram.h"
      9 #endif
     10 #include "gfxfont.h"
     11 
     12 /// A generic graphics superclass that can handle all sorts of drawing. At a
     13 /// minimum you can subclass and provide drawPixel(). At a maximum you can do a
     14 /// ton of overriding to optimize. Used for any/all Adafruit displays!
     15 class Adafruit_GFX : public Print {
     16 
     17 public:
     18   Adafruit_GFX(int16_t w, int16_t h); // Constructor
     19 
     20   /**********************************************************************/
     21   /*!
     22     @brief  Draw to the screen/framebuffer/etc.
     23     Must be overridden in subclass.
     24     @param  x    X coordinate in pixels
     25     @param  y    Y coordinate in pixels
     26     @param color  16-bit pixel color.
     27   */
     28   /**********************************************************************/
     29   virtual void drawPixel(int16_t x, int16_t y, uint16_t color) = 0;
     30 
     31   // TRANSACTION API / CORE DRAW API
     32   // These MAY be overridden by the subclass to provide device-specific
     33   // optimized code.  Otherwise 'generic' versions are used.
     34   virtual void startWrite(void);
     35   virtual void writePixel(int16_t x, int16_t y, uint16_t color);
     36   virtual void writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h,
     37                              uint16_t color);
     38   virtual void writeFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
     39   virtual void writeFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
     40   virtual void writeLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
     41                          uint16_t color);
     42   virtual void endWrite(void);
     43 
     44   // CONTROL API
     45   // These MAY be overridden by the subclass to provide device-specific
     46   // optimized code.  Otherwise 'generic' versions are used.
     47   virtual void setRotation(uint8_t r);
     48   virtual void invertDisplay(bool i);
     49 
     50   // BASIC DRAW API
     51   // These MAY be overridden by the subclass to provide device-specific
     52   // optimized code.  Otherwise 'generic' versions are used.
     53 
     54   // It's good to implement those, even if using transaction API
     55   virtual void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
     56   virtual void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
     57   virtual void fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
     58                         uint16_t color);
     59   virtual void fillScreen(uint16_t color);
     60   // Optional and probably not necessary to change
     61   virtual void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
     62                         uint16_t color);
     63   virtual void drawRect(int16_t x, int16_t y, int16_t w, int16_t h,
     64                         uint16_t color);
     65 
     66   // These exist only with Adafruit_GFX (no subclass overrides)
     67   void drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
     68   void drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername,
     69                         uint16_t color);
     70   void fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
     71   void fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername,
     72                         int16_t delta, uint16_t color);
     73   void drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2,
     74                     int16_t y2, uint16_t color);
     75   void fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2,
     76                     int16_t y2, uint16_t color);
     77   void drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h,
     78                      int16_t radius, uint16_t color);
     79   void fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h,
     80                      int16_t radius, uint16_t color);
     81   void drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w,
     82                   int16_t h, uint16_t color);
     83   void drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w,
     84                   int16_t h, uint16_t color, uint16_t bg);
     85   void drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h,
     86                   uint16_t color);
     87   void drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h,
     88                   uint16_t color, uint16_t bg);
     89   void drawXBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w,
     90                    int16_t h, uint16_t color);
     91   void drawGrayscaleBitmap(int16_t x, int16_t y, const uint8_t bitmap[],
     92                            int16_t w, int16_t h);
     93   void drawGrayscaleBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w,
     94                            int16_t h);
     95   void drawGrayscaleBitmap(int16_t x, int16_t y, const uint8_t bitmap[],
     96                            const uint8_t mask[], int16_t w, int16_t h);
     97   void drawGrayscaleBitmap(int16_t x, int16_t y, uint8_t *bitmap, uint8_t *mask,
     98                            int16_t w, int16_t h);
     99   void drawRGBBitmap(int16_t x, int16_t y, const uint16_t bitmap[], int16_t w,
    100                      int16_t h);
    101   void drawRGBBitmap(int16_t x, int16_t y, uint16_t *bitmap, int16_t w,
    102                      int16_t h);
    103   void drawRGBBitmap(int16_t x, int16_t y, const uint16_t bitmap[],
    104                      const uint8_t mask[], int16_t w, int16_t h);
    105   void drawRGBBitmap(int16_t x, int16_t y, uint16_t *bitmap, uint8_t *mask,
    106                      int16_t w, int16_t h);
    107   void drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color,
    108                 uint16_t bg, uint8_t size);
    109   void drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color,
    110                 uint16_t bg, uint8_t size_x, uint8_t size_y);
    111   void getTextBounds(const char *string, int16_t x, int16_t y, int16_t *x1,
    112                      int16_t *y1, uint16_t *w, uint16_t *h);
    113   void getTextBounds(const __FlashStringHelper *s, int16_t x, int16_t y,
    114                      int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h);
    115   void getTextBounds(const String &str, int16_t x, int16_t y, int16_t *x1,
    116                      int16_t *y1, uint16_t *w, uint16_t *h);
    117   void setTextSize(uint8_t s);
    118   void setTextSize(uint8_t sx, uint8_t sy);
    119   void setFont(const GFXfont *f = NULL);
    120 
    121   /**********************************************************************/
    122   /*!
    123     @brief  Set text cursor location
    124     @param  x    X coordinate in pixels
    125     @param  y    Y coordinate in pixels
    126   */
    127   /**********************************************************************/
    128   void setCursor(int16_t x, int16_t y) {
    129     cursor_x = x;
    130     cursor_y = y;
    131   }
    132 
    133   /**********************************************************************/
    134   /*!
    135     @brief   Set text font color with transparant background
    136     @param   c   16-bit 5-6-5 Color to draw text with
    137     @note    For 'transparent' background, background and foreground
    138              are set to same color rather than using a separate flag.
    139   */
    140   /**********************************************************************/
    141   void setTextColor(uint16_t c) { textcolor = textbgcolor = c; }
    142 
    143   /**********************************************************************/
    144   /*!
    145     @brief   Set text font color with custom background color
    146     @param   c   16-bit 5-6-5 Color to draw text with
    147     @param   bg  16-bit 5-6-5 Color to draw background/fill with
    148   */
    149   /**********************************************************************/
    150   void setTextColor(uint16_t c, uint16_t bg) {
    151     textcolor = c;
    152     textbgcolor = bg;
    153   }
    154 
    155   /**********************************************************************/
    156   /*!
    157   @brief  Set whether text that is too long for the screen width should
    158           automatically wrap around to the next line (else clip right).
    159   @param  w  true for wrapping, false for clipping
    160   */
    161   /**********************************************************************/
    162   void setTextWrap(bool w) { wrap = w; }
    163 
    164   /**********************************************************************/
    165   /*!
    166     @brief  Enable (or disable) Code Page 437-compatible charset.
    167             There was an error in glcdfont.c for the longest time -- one
    168             character (#176, the 'light shade' block) was missing -- this
    169             threw off the index of every character that followed it.
    170             But a TON of code has been written with the erroneous
    171             character indices. By default, the library uses the original
    172             'wrong' behavior and old sketches will still work. Pass
    173             'true' to this function to use correct CP437 character values
    174             in your code.
    175     @param  x  true = enable (new behavior), false = disable (old behavior)
    176   */
    177   /**********************************************************************/
    178   void cp437(bool x = true) { _cp437 = x; }
    179 
    180   using Print::write;
    181 #if ARDUINO >= 100
    182   virtual size_t write(uint8_t);
    183 #else
    184   virtual void write(uint8_t);
    185 #endif
    186 
    187   /************************************************************************/
    188   /*!
    189     @brief      Get width of the display, accounting for current rotation
    190     @returns    Width in pixels
    191   */
    192   /************************************************************************/
    193   int16_t width(void) const { return _width; };
    194 
    195   /************************************************************************/
    196   /*!
    197     @brief      Get height of the display, accounting for current rotation
    198     @returns    Height in pixels
    199   */
    200   /************************************************************************/
    201   int16_t height(void) const { return _height; }
    202 
    203   /************************************************************************/
    204   /*!
    205     @brief      Get rotation setting for display
    206     @returns    0 thru 3 corresponding to 4 cardinal rotations
    207   */
    208   /************************************************************************/
    209   uint8_t getRotation(void) const { return rotation; }
    210 
    211   // get current cursor position (get rotation safe maximum values,
    212   // using: width() for x, height() for y)
    213   /************************************************************************/
    214   /*!
    215     @brief  Get text cursor X location
    216     @returns    X coordinate in pixels
    217   */
    218   /************************************************************************/
    219   int16_t getCursorX(void) const { return cursor_x; }
    220 
    221   /************************************************************************/
    222   /*!
    223     @brief      Get text cursor Y location
    224     @returns    Y coordinate in pixels
    225   */
    226   /************************************************************************/
    227   int16_t getCursorY(void) const { return cursor_y; };
    228 
    229 protected:
    230   void charBounds(unsigned char c, int16_t *x, int16_t *y, int16_t *minx,
    231                   int16_t *miny, int16_t *maxx, int16_t *maxy);
    232   int16_t WIDTH;        ///< This is the 'raw' display width - never changes
    233   int16_t HEIGHT;       ///< This is the 'raw' display height - never changes
    234   int16_t _width;       ///< Display width as modified by current rotation
    235   int16_t _height;      ///< Display height as modified by current rotation
    236   int16_t cursor_x;     ///< x location to start print()ing text
    237   int16_t cursor_y;     ///< y location to start print()ing text
    238   uint16_t textcolor;   ///< 16-bit background color for print()
    239   uint16_t textbgcolor; ///< 16-bit text color for print()
    240   uint8_t textsize_x;   ///< Desired magnification in X-axis of text to print()
    241   uint8_t textsize_y;   ///< Desired magnification in Y-axis of text to print()
    242   uint8_t rotation;     ///< Display rotation (0 thru 3)
    243   bool wrap;            ///< If set, 'wrap' text at right edge of display
    244   bool _cp437;          ///< If set, use correct CP437 charset (default is off)
    245   GFXfont *gfxFont;     ///< Pointer to special font
    246 };
    247 
    248 /// A simple drawn button UI element
    249 class Adafruit_GFX_Button {
    250 
    251 public:
    252   Adafruit_GFX_Button(void);
    253   // "Classic" initButton() uses center & size
    254   void initButton(Adafruit_GFX *gfx, int16_t x, int16_t y, uint16_t w,
    255                   uint16_t h, uint16_t outline, uint16_t fill,
    256                   uint16_t textcolor, char *label, uint8_t textsize);
    257   void initButton(Adafruit_GFX *gfx, int16_t x, int16_t y, uint16_t w,
    258                   uint16_t h, uint16_t outline, uint16_t fill,
    259                   uint16_t textcolor, char *label, uint8_t textsize_x,
    260                   uint8_t textsize_y);
    261   // New/alt initButton() uses upper-left corner & size
    262   void initButtonUL(Adafruit_GFX *gfx, int16_t x1, int16_t y1, uint16_t w,
    263                     uint16_t h, uint16_t outline, uint16_t fill,
    264                     uint16_t textcolor, char *label, uint8_t textsize);
    265   void initButtonUL(Adafruit_GFX *gfx, int16_t x1, int16_t y1, uint16_t w,
    266                     uint16_t h, uint16_t outline, uint16_t fill,
    267                     uint16_t textcolor, char *label, uint8_t textsize_x,
    268                     uint8_t textsize_y);
    269   void drawButton(bool inverted = false);
    270   bool contains(int16_t x, int16_t y);
    271 
    272   /**********************************************************************/
    273   /*!
    274     @brief    Sets button state, should be done by some touch function
    275     @param    p  True for pressed, false for not.
    276   */
    277   /**********************************************************************/
    278   void press(bool p) {
    279     laststate = currstate;
    280     currstate = p;
    281   }
    282 
    283   bool justPressed();
    284   bool justReleased();
    285 
    286   /**********************************************************************/
    287   /*!
    288     @brief    Query whether the button is currently pressed
    289     @returns  True if pressed
    290   */
    291   /**********************************************************************/
    292   bool isPressed(void) { return currstate; };
    293 
    294 private:
    295   Adafruit_GFX *_gfx;
    296   int16_t _x1, _y1; // Coordinates of top-left corner
    297   uint16_t _w, _h;
    298   uint8_t _textsize_x;
    299   uint8_t _textsize_y;
    300   uint16_t _outlinecolor, _fillcolor, _textcolor;
    301   char _label[10];
    302 
    303   bool currstate, laststate;
    304 };
    305 
    306 /// A GFX 1-bit canvas context for graphics
    307 class GFXcanvas1 : public Adafruit_GFX {
    308 public:
    309   GFXcanvas1(uint16_t w, uint16_t h);
    310   ~GFXcanvas1(void);
    311   void drawPixel(int16_t x, int16_t y, uint16_t color);
    312   void fillScreen(uint16_t color);
    313   void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
    314   void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
    315   bool getPixel(int16_t x, int16_t y) const;
    316   /**********************************************************************/
    317   /*!
    318     @brief    Get a pointer to the internal buffer memory
    319     @returns  A pointer to the allocated buffer
    320   */
    321   /**********************************************************************/
    322   uint8_t *getBuffer(void) const { return buffer; }
    323 
    324 protected:
    325   bool getRawPixel(int16_t x, int16_t y) const;
    326   void drawFastRawVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
    327   void drawFastRawHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
    328 
    329 private:
    330   uint8_t *buffer;
    331 
    332 #ifdef __AVR__
    333   // Bitmask tables of 0x80>>X and ~(0x80>>X), because X>>Y is slow on AVR
    334   static const uint8_t PROGMEM GFXsetBit[], GFXclrBit[];
    335 #endif
    336 };
    337 
    338 /// A GFX 8-bit canvas context for graphics
    339 class GFXcanvas8 : public Adafruit_GFX {
    340 public:
    341   GFXcanvas8(uint16_t w, uint16_t h);
    342   ~GFXcanvas8(void);
    343   void drawPixel(int16_t x, int16_t y, uint16_t color);
    344   void fillScreen(uint16_t color);
    345   void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
    346   void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
    347   uint8_t getPixel(int16_t x, int16_t y) const;
    348   /**********************************************************************/
    349   /*!
    350    @brief    Get a pointer to the internal buffer memory
    351    @returns  A pointer to the allocated buffer
    352   */
    353   /**********************************************************************/
    354   uint8_t *getBuffer(void) const { return buffer; }
    355 
    356 protected:
    357   uint8_t getRawPixel(int16_t x, int16_t y) const;
    358   void drawFastRawVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
    359   void drawFastRawHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
    360 
    361 private:
    362   uint8_t *buffer;
    363 };
    364 
    365 ///  A GFX 16-bit canvas context for graphics
    366 class GFXcanvas16 : public Adafruit_GFX {
    367 public:
    368   GFXcanvas16(uint16_t w, uint16_t h);
    369   ~GFXcanvas16(void);
    370   void drawPixel(int16_t x, int16_t y, uint16_t color);
    371   void fillScreen(uint16_t color);
    372   void byteSwap(void);
    373   void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
    374   void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
    375   uint16_t getPixel(int16_t x, int16_t y) const;
    376   /**********************************************************************/
    377   /*!
    378     @brief    Get a pointer to the internal buffer memory
    379     @returns  A pointer to the allocated buffer
    380   */
    381   /**********************************************************************/
    382   uint16_t *getBuffer(void) const { return buffer; }
    383 
    384 protected:
    385   uint16_t getRawPixel(int16_t x, int16_t y) const;
    386   void drawFastRawVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
    387   void drawFastRawHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
    388 
    389 private:
    390   uint16_t *buffer;
    391 };
    392 
    393 #endif // _ADAFRUIT_GFX_H