work.

Dependencies:   Blynk mbed

Committer:
lixianyu
Date:
Thu Jun 16 08:12:33 2016 +0000
Revision:
4:e5018e5ba340
Parent:
1:0e75de2a5d21
ok

Who changed what in which revision?

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