Adafruit GFX Library
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Adafruit_GrayOLED Class Referenceabstract

Class that stores state and functions for interacting with generic grayscale OLED displays. More...

#include <Adafruit_GrayOLED.h>

Inheritance diagram for Adafruit_GrayOLED:
Adafruit_GFX

Public Member Functions

 Adafruit_GrayOLED (uint8_t bpp, uint16_t w, uint16_t h, TwoWire *twi=&Wire, int8_t rst_pin=-1, uint32_t preclk=400000, uint32_t postclk=100000)
 Constructor for I2C-interfaced OLED displays. More...
 
 Adafruit_GrayOLED (uint8_t bpp, uint16_t w, uint16_t h, int8_t mosi_pin, int8_t sclk_pin, int8_t dc_pin, int8_t rst_pin, int8_t cs_pin)
 Constructor for SPI GrayOLED displays, using software (bitbang) SPI. More...
 
 Adafruit_GrayOLED (uint8_t bpp, uint16_t w, uint16_t h, SPIClass *spi, int8_t dc_pin, int8_t rst_pin, int8_t cs_pin, uint32_t bitrate=8000000UL)
 Constructor for SPI GrayOLED displays, using native hardware SPI. More...
 
 ~Adafruit_GrayOLED (void)
 Destructor for Adafruit_GrayOLED object.
 
virtual void display (void)=0
 The function that sub-classes define that writes out the buffer to the display over I2C or SPI.
 
void clearDisplay (void)
 Clear contents of display buffer (set all pixels to off). More...
 
void invertDisplay (bool i)
 Enable or disable display invert mode (white-on-black vs black-on-white). Handy for testing! More...
 
void setContrast (uint8_t contrastlevel)
 Adjust the display contrast. More...
 
void drawPixel (int16_t x, int16_t y, uint16_t color)
 Set/clear/invert a single pixel. This is also invoked by the Adafruit_GFX library in generating many higher-level graphics primitives. More...
 
bool getPixel (int16_t x, int16_t y)
 Return color of a single pixel in display buffer. More...
 
uint8_t * getBuffer (void)
 Get base address of display buffer for direct reading or writing. More...
 
void oled_command (uint8_t c)
 Issue single command byte to OLED, using I2C or hard/soft SPI as needed. More...
 
bool oled_commandList (const uint8_t *c, uint8_t n)
 Issue multiple bytes of commands OLED, using I2C or hard/soft SPI as needed. More...
 
- Public Member Functions inherited from Adafruit_GFX
 Adafruit_GFX (int16_t w, int16_t h)
 Instatiate a GFX context for graphics! Can only be done by a superclass. More...
 
virtual void startWrite (void)
 Start a display-writing routine, overwrite in subclasses.
 
virtual void writePixel (int16_t x, int16_t y, uint16_t color)
 Write a pixel, overwrite in subclasses if startWrite is defined! More...
 
virtual void writeFillRect (int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
 Write a rectangle completely with one color, overwrite in subclasses if startWrite is defined! More...
 
virtual void writeFastVLine (int16_t x, int16_t y, int16_t h, uint16_t color)
 Write a perfectly vertical line, overwrite in subclasses if startWrite is defined! More...
 
virtual void writeFastHLine (int16_t x, int16_t y, int16_t w, uint16_t color)
 Write a perfectly horizontal line, overwrite in subclasses if startWrite is defined! More...
 
virtual void writeLine (int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color)
 Write a line. Bresenham's algorithm - thx wikpedia. More...
 
virtual void endWrite (void)
 End a display-writing routine, overwrite in subclasses if startWrite is defined!
 
virtual void setRotation (uint8_t r)
 Set rotation setting for display. More...
 
virtual void drawFastVLine (int16_t x, int16_t y, int16_t h, uint16_t color)
 Draw a perfectly vertical line (this is often optimized in a subclass!) More...
 
virtual void drawFastHLine (int16_t x, int16_t y, int16_t w, uint16_t color)
 Draw a perfectly horizontal line (this is often optimized in a subclass!) More...
 
virtual void fillRect (int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
 Fill a rectangle completely with one color. Update in subclasses if desired! More...
 
virtual void fillScreen (uint16_t color)
 Fill the screen completely with one color. Update in subclasses if desired! More...
 
virtual void drawLine (int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color)
 Draw a line. More...
 
virtual void drawRect (int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
 Draw a rectangle with no fill color. More...
 
void drawCircle (int16_t x0, int16_t y0, int16_t r, uint16_t color)
 Draw a circle outline. More...
 
void drawCircleHelper (int16_t x0, int16_t y0, int16_t r, uint8_t cornername, uint16_t color)
 Quarter-circle drawer, used to do circles and roundrects. More...
 
void fillCircle (int16_t x0, int16_t y0, int16_t r, uint16_t color)
 Draw a circle with filled color. More...
 
void fillCircleHelper (int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, uint16_t color)
 Quarter-circle drawer with fill, used for circles and roundrects. More...
 
void drawTriangle (int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color)
 Draw a triangle with no fill color. More...
 
void fillTriangle (int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color)
 Draw a triangle with color-fill. More...
 
void drawRoundRect (int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color)
 Draw a rounded rectangle with no fill color. More...
 
void fillRoundRect (int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color)
 Draw a rounded rectangle with fill color. More...
 
void drawBitmap (int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color)
 Draw a PROGMEM-resident 1-bit image at the specified (x,y) position, using the specified foreground color (unset bits are transparent). More...
 
void drawBitmap (int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color, uint16_t bg)
 Draw a PROGMEM-resident 1-bit image at the specified (x,y) position, using the specified foreground (for set bits) and background (unset bits) colors. More...
 
void drawBitmap (int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h, uint16_t color)
 Draw a RAM-resident 1-bit image at the specified (x,y) position, using the specified foreground color (unset bits are transparent). More...
 
void drawBitmap (int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg)
 Draw a RAM-resident 1-bit image at the specified (x,y) position, using the specified foreground (for set bits) and background (unset bits) colors. More...
 
void drawXBitmap (int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color)
 Draw PROGMEM-resident XBitMap Files (*.xbm), exported from GIMP. Usage: Export from GIMP to *.xbm, rename *.xbm to *.c and open in editor. C Array can be directly used with this function. There is no RAM-resident version of this function; if generating bitmaps in RAM, use the format defined by drawBitmap() and call that instead. More...
 
void drawGrayscaleBitmap (int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h)
 Draw a PROGMEM-resident 8-bit image (grayscale) at the specified (x,y) pos. Specifically for 8-bit display devices such as IS31FL3731; no color reduction/expansion is performed. More...
 
void drawGrayscaleBitmap (int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h)
 Draw a RAM-resident 8-bit image (grayscale) at the specified (x,y) pos. Specifically for 8-bit display devices such as IS31FL3731; no color reduction/expansion is performed. More...
 
void drawGrayscaleBitmap (int16_t x, int16_t y, const uint8_t bitmap[], const uint8_t mask[], int16_t w, int16_t h)
 Draw a PROGMEM-resident 8-bit image (grayscale) with a 1-bit mask (set bits = opaque, unset bits = clear) at the specified (x,y) position. BOTH buffers (grayscale and mask) must be PROGMEM-resident. Specifically for 8-bit display devices such as IS31FL3731; no color reduction/expansion is performed. More...
 
void drawGrayscaleBitmap (int16_t x, int16_t y, uint8_t *bitmap, uint8_t *mask, int16_t w, int16_t h)
 Draw a RAM-resident 8-bit image (grayscale) with a 1-bit mask (set bits = opaque, unset bits = clear) at the specified (x,y) position. BOTH buffers (grayscale and mask) must be RAM-residentt, no mix-and-match Specifically for 8-bit display devices such as IS31FL3731; no color reduction/expansion is performed. More...
 
void drawRGBBitmap (int16_t x, int16_t y, const uint16_t bitmap[], int16_t w, int16_t h)
 Draw a PROGMEM-resident 16-bit image (RGB 5/6/5) at the specified (x,y) position. For 16-bit display devices; no color reduction performed. More...
 
void drawRGBBitmap (int16_t x, int16_t y, uint16_t *bitmap, int16_t w, int16_t h)
 Draw a RAM-resident 16-bit image (RGB 5/6/5) at the specified (x,y) position. For 16-bit display devices; no color reduction performed. More...
 
void drawRGBBitmap (int16_t x, int16_t y, const uint16_t bitmap[], const uint8_t mask[], int16_t w, int16_t h)
 Draw a PROGMEM-resident 16-bit image (RGB 5/6/5) with a 1-bit mask (set bits = opaque, unset bits = clear) at the specified (x,y) position. BOTH buffers (color and mask) must be PROGMEM-resident. For 16-bit display devices; no color reduction performed. More...
 
void drawRGBBitmap (int16_t x, int16_t y, uint16_t *bitmap, uint8_t *mask, int16_t w, int16_t h)
 Draw a RAM-resident 16-bit image (RGB 5/6/5) with a 1-bit mask (set bits = opaque, unset bits = clear) at the specified (x,y) position. BOTH buffers (color and mask) must be RAM-resident. For 16-bit display devices; no color reduction performed. More...
 
void drawChar (int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, uint8_t size)
 Draw a single character. More...
 
void drawChar (int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, uint8_t size_x, uint8_t size_y)
 Draw a single character. More...
 
void getTextBounds (const char *string, int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h)
 Helper to determine size of a string with current font/size. Pass string and a cursor position, returns UL corner and W,H. More...
 
void getTextBounds (const __FlashStringHelper *s, int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h)
 Helper to determine size of a PROGMEM string with current font/size. Pass string and a cursor position, returns UL corner and W,H. More...
 
void getTextBounds (const String &str, int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h)
 Helper to determine size of a string with current font/size. Pass string and a cursor position, returns UL corner and W,H. More...
 
void setTextSize (uint8_t s)
 Set text 'magnification' size. Each increase in s makes 1 pixel that much bigger. More...
 
void setTextSize (uint8_t sx, uint8_t sy)
 Set text 'magnification' size. Each increase in s makes 1 pixel that much bigger. More...
 
void setFont (const GFXfont *f=NULL)
 Set the font to display when print()ing, either custom or default. More...
 
void setCursor (int16_t x, int16_t y)
 Set text cursor location. More...
 
void setTextColor (uint16_t c)
 Set text font color with transparant background. More...
 
void setTextColor (uint16_t c, uint16_t bg)
 Set text font color with custom background color. More...
 
void setTextWrap (bool w)
 Set whether text that is too long for the screen width should automatically wrap around to the next line (else clip right). More...
 
void cp437 (bool x=true)
 Enable (or disable) Code Page 437-compatible charset. There was an error in glcdfont.c for the longest time – one character (#176, the 'light shade' block) was missing – this threw off the index of every character that followed it. But a TON of code has been written with the erroneous character indices. By default, the library uses the original 'wrong' behavior and old sketches will still work. Pass 'true' to this function to use correct CP437 character values in your code. More...
 
virtual void write (uint8_t)
 Print one byte/character of data, used to support print() More...
 
int16_t width (void) const
 Get width of the display, accounting for current rotation. More...
 
int16_t height (void) const
 Get height of the display, accounting for current rotation. More...
 
uint8_t getRotation (void) const
 Get rotation setting for display. More...
 
int16_t getCursorX (void) const
 Get text cursor X location. More...
 
int16_t getCursorY (void) const
 Get text cursor Y location. More...
 

Protected Member Functions

bool _init (uint8_t i2caddr=0x3C, bool reset=true)
 Allocate RAM for image buffer, initialize peripherals and pins. Note that subclasses must call this before other begin() init. More...
 
- Protected Member Functions inherited from Adafruit_GFX
void charBounds (unsigned char c, int16_t *x, int16_t *y, int16_t *minx, int16_t *miny, int16_t *maxx, int16_t *maxy)
 Helper to determine size of a character with current font/size. Broke this out as it's used by both the PROGMEM- and RAM-resident getTextBounds() functions. More...
 

Protected Attributes

Adafruit_SPIDevice * spi_dev = NULL
 The SPI interface BusIO device.
 
Adafruit_I2CDevice * i2c_dev = NULL
 The I2C interface BusIO device.
 
int32_t i2c_preclk = 400000
 Configurable 'high speed' I2C rate.
 
int32_t i2c_postclk = 100000
 Configurable 'low speed' I2C rate.
 
uint8_t * buffer = NULL
 Internal 1:1 framebuffer of display mem.
 
int16_t window_x1
 Dirty tracking window minimum x.
 
int16_t window_y1
 Dirty tracking window minimum y.
 
int16_t window_x2
 Dirty tracking window maximum x.
 
int16_t window_y2
 Dirty tracking window maximum y.
 
int dcPin
 The Arduino pin connected to D/C (for SPI)
 
int csPin
 The Arduino pin connected to CS (for SPI)
 
int rstPin
 The Arduino pin connected to reset (-1 if unused)
 
uint8_t _bpp = 1
 Bits per pixel color for this display.
 
- Protected Attributes inherited from Adafruit_GFX
int16_t WIDTH
 This is the 'raw' display width - never changes.
 
int16_t HEIGHT
 This is the 'raw' display height - never changes.
 
int16_t _width
 Display width as modified by current rotation.
 
int16_t _height
 Display height as modified by current rotation.
 
int16_t cursor_x
 x location to start print()ing text
 
int16_t cursor_y
 y location to start print()ing text
 
uint16_t textcolor
 16-bit background color for print()
 
uint16_t textbgcolor
 16-bit text color for print()
 
uint8_t textsize_x
 Desired magnification in X-axis of text to print()
 
uint8_t textsize_y
 Desired magnification in Y-axis of text to print()
 
uint8_t rotation
 Display rotation (0 thru 3)
 
bool wrap
 If set, 'wrap' text at right edge of display.
 
bool _cp437
 If set, use correct CP437 charset (default is off)
 
GFXfontgfxFont
 Pointer to special font.
 

Detailed Description

Class that stores state and functions for interacting with generic grayscale OLED displays.

Constructor & Destructor Documentation

◆ Adafruit_GrayOLED() [1/3]

Adafruit_GrayOLED::Adafruit_GrayOLED ( uint8_t  bpp,
uint16_t  w,
uint16_t  h,
TwoWire *  twi = &Wire,
int8_t  rst_pin = -1,
uint32_t  clkDuring = 400000,
uint32_t  clkAfter = 100000 
)

Constructor for I2C-interfaced OLED displays.

Parameters
bppBits per pixel, 1 for monochrome, 4 for 16-gray
wDisplay width in pixels
hDisplay height in pixels
twiPointer to an existing TwoWire instance (e.g. &Wire, the microcontroller's primary I2C bus).
rst_pinReset pin (using Arduino pin numbering), or -1 if not used (some displays might be wired to share the microcontroller's reset pin).
clkDuringSpeed (in Hz) for Wire transmissions in library calls. Defaults to 400000 (400 KHz), a known 'safe' value for most microcontrollers, and meets the OLED datasheet spec. Some systems can operate I2C faster (800 KHz for ESP32, 1 MHz for many other 32-bit MCUs), and some (perhaps not all) Many OLED's can work with this – so it's optionally be specified here and is not a default behavior. (Ignored if using pre-1.5.7 Arduino software, which operates I2C at a fixed 100 KHz.)
clkAfterSpeed (in Hz) for Wire transmissions following library calls. Defaults to 100000 (100 KHz), the default Arduino Wire speed. This is done rather than leaving it at the 'during' speed because other devices on the I2C bus might not be compatible with the faster rate. (Ignored if using pre-1.5.7 Arduino software, which operates I2C at a fixed 100 KHz.)
Note
Call the object's begin() function before use – buffer allocation is performed there!

◆ Adafruit_GrayOLED() [2/3]

Adafruit_GrayOLED::Adafruit_GrayOLED ( uint8_t  bpp,
uint16_t  w,
uint16_t  h,
int8_t  mosi_pin,
int8_t  sclk_pin,
int8_t  dc_pin,
int8_t  rst_pin,
int8_t  cs_pin 
)

Constructor for SPI GrayOLED displays, using software (bitbang) SPI.

Parameters
bppBits per pixel, 1 for monochrome, 4 for 16-gray
wDisplay width in pixels
hDisplay height in pixels
mosi_pinMOSI (master out, slave in) pin (using Arduino pin numbering). This transfers serial data from microcontroller to display.
sclk_pinSCLK (serial clock) pin (using Arduino pin numbering). This clocks each bit from MOSI.
dc_pinData/command pin (using Arduino pin numbering), selects whether display is receiving commands (low) or data (high).
rst_pinReset pin (using Arduino pin numbering), or -1 if not used (some displays might be wired to share the microcontroller's reset pin).
cs_pinChip-select pin (using Arduino pin numbering) for sharing the bus with other devices. Active low.
Note
Call the object's begin() function before use – buffer allocation is performed there!

◆ Adafruit_GrayOLED() [3/3]

Adafruit_GrayOLED::Adafruit_GrayOLED ( uint8_t  bpp,
uint16_t  w,
uint16_t  h,
SPIClass *  spi,
int8_t  dc_pin,
int8_t  rst_pin,
int8_t  cs_pin,
uint32_t  bitrate = 8000000UL 
)

Constructor for SPI GrayOLED displays, using native hardware SPI.

Parameters
bppBits per pixel, 1 for monochrome, 4 for 16-gray
wDisplay width in pixels
hDisplay height in pixels
spiPointer to an existing SPIClass instance (e.g. &SPI, the microcontroller's primary SPI bus).
dc_pinData/command pin (using Arduino pin numbering), selects whether display is receiving commands (low) or data (high).
rst_pinReset pin (using Arduino pin numbering), or -1 if not used (some displays might be wired to share the microcontroller's reset pin).
cs_pinChip-select pin (using Arduino pin numbering) for sharing the bus with other devices. Active low.
bitrateSPI clock rate for transfers to this display. Default if unspecified is 8000000UL (8 MHz).
Note
Call the object's begin() function before use – buffer allocation is performed there!

Member Function Documentation

◆ clearDisplay()

void Adafruit_GrayOLED::clearDisplay ( void  )

Clear contents of display buffer (set all pixels to off).

Note
Changes buffer contents only, no immediate effect on display. Follow up with a call to display(), or with other graphics commands as needed by one's own application.

◆ invertDisplay()

void Adafruit_GrayOLED::invertDisplay ( bool  i)
virtual

Enable or disable display invert mode (white-on-black vs black-on-white). Handy for testing!

Parameters
iIf true, switch to invert mode (black-on-white), else normal mode (white-on-black).
Note
This has an immediate effect on the display, no need to call the display() function – buffer contents are not changed, rather a different pixel mode of the display hardware is used. When enabled, drawing MONOOLED_BLACK (value 0) pixels will actually draw white, MONOOLED_WHITE (value 1) will draw black.

Reimplemented from Adafruit_GFX.

◆ setContrast()

void Adafruit_GrayOLED::setContrast ( uint8_t  level)

Adjust the display contrast.

Parameters
levelThe contrast level from 0 to 0x7F
Note
This has an immediate effect on the display, no need to call the display() function – buffer contents are not changed.

◆ drawPixel()

void Adafruit_GrayOLED::drawPixel ( int16_t  x,
int16_t  y,
uint16_t  color 
)
virtual

Set/clear/invert a single pixel. This is also invoked by the Adafruit_GFX library in generating many higher-level graphics primitives.

Parameters
xColumn of display – 0 at left to (screen width - 1) at right.
yRow of display – 0 at top to (screen height -1) at bottom.
colorPixel color, one of: MONOOLED_BLACK, MONOOLED_WHITE or MONOOLED_INVERT.
Note
Changes buffer contents only, no immediate effect on display. Follow up with a call to display(), or with other graphics commands as needed by one's own application.

Implements Adafruit_GFX.

◆ getPixel()

bool Adafruit_GrayOLED::getPixel ( int16_t  x,
int16_t  y 
)

Return color of a single pixel in display buffer.

Parameters
xColumn of display – 0 at left to (screen width - 1) at right.
yRow of display – 0 at top to (screen height -1) at bottom.
Returns
true if pixel is set (usually MONOOLED_WHITE, unless display invert mode is enabled), false if clear (MONOOLED_BLACK).
Note
Reads from buffer contents; may not reflect current contents of screen if display() has not been called.

◆ getBuffer()

uint8_t * Adafruit_GrayOLED::getBuffer ( void  )

Get base address of display buffer for direct reading or writing.

Returns
Pointer to an unsigned 8-bit array, column-major, columns padded to full byte boundary if needed.

◆ oled_command()

void Adafruit_GrayOLED::oled_command ( uint8_t  c)

Issue single command byte to OLED, using I2C or hard/soft SPI as needed.

Parameters
cThe single byte command

◆ oled_commandList()

bool Adafruit_GrayOLED::oled_commandList ( const uint8_t *  c,
uint8_t  n 
)

Issue multiple bytes of commands OLED, using I2C or hard/soft SPI as needed.

Parameters
cPointer to the command array
nThe number of bytes in the command array
Returns
True for success on ability to write the data in I2C.

◆ _init()

bool Adafruit_GrayOLED::_init ( uint8_t  addr = 0x3C,
bool  reset = true 
)
protected

Allocate RAM for image buffer, initialize peripherals and pins. Note that subclasses must call this before other begin() init.

Parameters
addrI2C address of corresponding oled display. SPI displays (hardware or software) do not use addresses, but this argument is still required. Default if unspecified is 0x3C.
resetIf true, and if the reset pin passed to the constructor is valid, a hard reset will be performed before initializing the display. If using multiple oled displays on the same bus, and if they all share the same reset pin, you should only pass true on the first display being initialized, false on all others, else the already-initialized displays would be reset. Default if unspecified is true.
Returns
true on successful allocation/init, false otherwise. Well-behaved code should check the return value before proceeding.
Note
MUST call this function before any drawing or updates!

The documentation for this class was generated from the following files: