Forked from Neal Horman: Adafruit_GFX, a derived version of the BSD licensed Adafrut GFX library for the SSD1306 controller for an OLED 128x32 or 128x64 display using SPI or I2C. Now it is adopted also for the SH1106 I2C 128x64 display as well...

Dependents:   Lab06_oled_i2c Lab06_BME280_oled Lab06_oled_clock I2C_SSD1306andSH1106_nucleo_F446RE

Committer:
cspista
Date:
Wed Feb 02 17:55:38 2022 +0000
Revision:
20:da33cca77ce5
Parent:
19:1b773847a04b
SH1106 initialization was added

Who changed what in which revision?

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