Library for Modtronix im4OLED board with 128x64 OLED and 4 buttons. For details, see product page http://modtronix.com/im4oled.html. Is a clone of Adafruit_GFX library, with some additional code added.

Fork of Adafruit_GFX by Neal Horman

Committer:
modtronix-com
Date:
Fri Aug 19 15:44:25 2016 +1000
Revision:
27:dd7d538d3849
Parent:
26:ef08580c35df
Added tag v1.1 for changeset ef08580c35df

Who changed what in which revision?

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