OLED Display

Dependents:   UltraschallSensor_LowLevelV2

Committer:
marcel1691
Date:
Fri May 27 16:40:07 2016 +0000
Revision:
0:1dee22d82bd2
OLED Display for IoTKit

Who changed what in which revision?

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