Each device-specific display library will have its own constructors and initialization functions. These are documented in the individual tutorials for each display type, or oftentimes are evident in the specific library header file. The remainder of this tutorial covers the common graphics functions that work the same regardless of the display type.
The function descriptions below are merely prototypes â thereâs an assumption that a display object is declared and initialized as needed by the device-specific library. Look at the example code with each library to see it in actual use. For example, where we show print(1234.56), your actual code would place the object name before this, e.g. it might read screen.print(1234.56) (if you have declared your display object with the name screen).
Drawing pixels (points)
First up is the most basic pixel pusher. You can call this with X, Y coordinates and a color and it will make a single dot:
void drawPixel(uint16_t x, uint16_t y, uint16_t color);
Drawing lines
You can also draw lines, with a starting and end point and color:
void drawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t color);
For horizontal or vertical lines, there are optimized line-drawing functions that avoid the angular calculations:
void drawFastVLine(uint16_t x0, uint16_t y0, uint16_t length, uint16_t color); void drawFastHLine(uint8_t x0, uint8_t y0, uint8_t length, uint16_t color);
Rectangles
Next up, rectangles and squares can be drawn and filled using the following procedures. Each accepts an X, Y pair for the top-left corner of the rectangle, a width and height (in pixels), and a color. drawRect() renders just the frame (outline) of the rectangle â the interior is unaffected â while fillRect() fills the entire area with a given color:
void drawRect(uint16_t x0, uint16_t y0, uint16_t w, uint16_t h, uint16_t color); void fillRect(uint16_t x0, uint16_t y0, uint16_t w, uint16_t h, uint16_t color);
To create a solid rectangle with a contrasting outline, use fillRect() first, then drawRect() over it.
Circles
Likewise, for circles, you can draw and fill. Each function accepts an X, Y pair for the center point, a radius in pixels, and a color:
void drawCircle(uint16_t x0, uint16_t y0, uint16_t r, uint16_t color); void fillCircle(uint16_t x0, uint16_t y0, uint16_t r, uint16_t color);
Rounded rectangles
For rectangles with rounded corners, both draw and fill functions are again available. Each begins with an X, Y, width and height (just like normal rectangles), then thereâs a corner radius (in pixels) and finally the color value:
void drawRoundRect(uint16_t x0, uint16_t y0, uint16_t w, uint16_t h, uint16_t radius, uint16_t color); void fillRoundRect(uint16_t x0, uint16_t y0, uint16_t w, uint16_t h, uint16_t radius, uint16_t color);
Hereâs an added bonus trick: because the circle functions are always drawn relative to a center pixel, the resulting circle diameter will always be an odd number of pixels. If an even-sized circle is required (which would place the center point between pixels), this can be achieved using one of the rounded rectangle functions: pass an identical width and height that are even values, and a corner radius thatâs exactly half this value.
Triangles
With triangles, once again there are the draw and fill functions. Each requires a full seven parameters: the X, Y coordinates for three corner points defining the triangle, followed by a color:
void drawTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color); void fillTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color);
Characters and text
There are two basic string drawing procedures for adding text. The first is just for a single character. You can place this character at any location and with any color. An optional size parameter can be passed which scales the font by this factor (e.g. size=2 will render the default font at 10x16 pixels per character). Itâs a little blocky that way but having just a single font helps keep the program size down.
void drawChar(uint16_t x, uint16_t y, char c, uint16_t color, uint16_t bg, uint8_t size);
Text is very flexible but operates a bit differently. Instead of one procedure, the text size, color and position are set up in separate functions and then the print() function is used â this makes it easy and provides all of the same string and number formatting capabilities of Arduinoâs familiar Serial.print() and println() functions! But you precede these with the display object instead of Serial.
void setCursor(int16_t x0, int16_t y0); void setTextColor(uint16_t color); void setTextColor(uint16_t color, uint16_t backgroundcolor); void setTextSize(uint8_t size); void setTextWrap(boolean w);
Begin with setCursor(x, y), which will place the top left corner of the text wherever you please. Initially this is set to (0,0) (the top-left corner of the screen). Then set the text color with setTextColor(color) â by default this is white. Text is normally drawn âclearâ â the open parts of each character show the original background contents, but if you want the text to block out whatâs underneath, a background color can be specified as an optional second parameter to setTextColor(). Finally, setTextSize(size) will multiply the scale of the text by a given integer factor. Below you can see scales of 1 (the default), 2 and 3. It appears blocky at larger sizes because we only ship the library with a single simple font, to save space.
For some OLED displays, 'display.setTextColor(WHITE, BLACK);' seems to work better than specifying 'display.setTextColor(0xFFFFFF, 0x000000);'
After setting everything up, you can use print() or println() â just like you do with Serial printing! For example, to print a string, use print("Hello world") - thatâs the first line of the image above. You can also use print() for numbers and variables â the second line above is the output of print(1234.56) and the third line is print(0xDEADBEEF, HEX).
By default, long lines of text are set to automatically âwrapâ back to the leftmost column. To override this behavior (so text will run off the right side of the display â useful for scrolling marquee effects), use setTextWrap(false). The normal wrapping behavior is restored with setTextWrap(true).
Extended Characters, CP437 and a Lurking Bug
The standard built-in font includes a number of symbols and accented characters outside the normal letters and numbers youâd use in print() strings. These can be accessed with drawChar(), passing an 8-bit value (0â255, though commonly expressed in hexadecimal, 0x00â0xFF) for the third argument.
The built-in font is based on the original IBM PC character set, known as Code Page 437 (CP437 for short). Many embedded systems still use this as itâs compact and well established.
Years ago, when originally transcribing CP437 into the GFX library, one symbol was accidentally omitted. Nothing fatal, code runs fine, but every subsequent symbol was then off by one compared to the ârealâ CP437 character set. By the time this was discovered, so much code had been written â projects shared online but also in fixed media like books and magazines â that fixing the bug would break every existing project that relied on those extended characters!
So the error has been left in place, on purpose, but this creates a different issue if one is adapting code from elsewhere that relies on the correct CP437 symbol values.
A compromise solution is a function that enables or disables the ârealâ CP437 sequence. By default this is off, the off-by-one order is used, so that all the old GFX projects in books work without modification. The correct order can be enabled with:
display.cp437(true);
Unless you need to switch back and forth, this typically only needs to be done one time, in the setup()
function.
Hereâs a map of the built-in character set, both the standard erroneous version, and the corrected version used when one calls cp437(true)
. Notice this only affects the last five rows of symbols; everything prior to character 0xB0 is unaffected:
The presence of the extended Code Page 437 symbols is only guaranteed in the built-in font. Custom fonts (explained elsewhere) rarely include these.
Extended characters typically canât be directly printed in code; most editors may support Unicode strings but this does not directly map to CP437. Typically one calls the write()
function with individual character numbers. The GFX library harkens back to an earlier time when Unicode support was not widespread.
Consider the German word Schön (beautiful). One might print this like so:
display.cp437(true); // Use correct CP437 character codes display.print("Scho"); // Print the plain ASCII first part display.write(0x94); // Print the o-with-umlauts display.println("n"); // Print the last part
Likewise with accessing the math symbolsâŠ
display.cp437(true); // Use correct CP437 character codes display.print("Temperature: "); display.print(number); display.write(0xF8); // Print the degrees symbol display.println(); // New line
Compiler support for some (not all) 32-bit microcontrollers provides the printf()
function, which can allow these characters to be placed inline via the %c
(character) formatting identifier:
display.cp437(true); display.printf("Temperature: %d%c\n", number, 0xF8); display.printf("Sch%cn\n", 0x94);
This is nice and compact but isnât compatible with all microcontrollers, certainly not early Arduino Uno-class devices, so consider how you might be sharing code and use it with care.
See the âUsing Fontsâ page for additional text features in the latest GFX library.
Bitmaps
You can draw small monochrome (single color) bitmaps, good for sprites and other mini-animations or icons:
void drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h, uint16_t color);
This issues a contiguous block of bits to the display, where each '1' bit sets the corresponding pixel to 'color,' while each '0' bit is skipped. x, y is the top-left corner where the bitmap is drawn, w, h are the width and height in pixels.
The bitmap data must be located in program memory using the PROGMEM directive. This is a somewhat advanced function and beginners are best advised to come back to this later. For an introduction, see the Arduino tutorial on PROGMEM usage.
Here's a handy webtool for generating bitmap -> memorymaps
Clearing or filling the screen
The fillScreen() function will set the entire display to a given color, erasing any existing content:
void fillScreen(uint16_t color);
Hardware-Specific functions
Some displays may have unique features like screen invert or hardware-based scrolling. Documentation for those functions can be found in the corresponding display-specific guide. Since these are not common features across all GFX-compatible displays, they are not described here.
Text editor powered by tinymce.