Download: https://github.com/olikraus/U8g2_for_Adafruit_GFX/archive/master.zip
U8g2 is a graphics library for monochrome displays. U8g2 supports many
displays, some of them are also supported by Adafruit GFX based libraries.
Others are supported by Adafruit GFX libraries, but are not supported by U8g2.
U8g2_for_Adafruit_GFX
connects to an existing Adafruit Library and adds support for
U8g2 fonts to all Adafruit GFX based libraries.
All libraries based on the Adafruit GFX engine are supported. This is a incomplete list of such libraries. Probably many more are supported:
This is a complete example for U8g2_for_Adafruit_GFX
connected to Adafruit SSD1306
library:
#include <Adafruit_SSD1306.h>
#include <U8g2_for_Adafruit_GFX.h>
Adafruit_SSD1306 display(/*MOSI*/ 11, /*CLK*/ 13, /*DC*/ 9, /*RESET*/ 8, /*CS*/ 10);
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
void setup() {
display.begin(SSD1306_SWITCHCAPVCC);
u8g2_for_adafruit_gfx.begin(display); // connect u8g2 procedures to Adafruit GFX
}
void loop() {
display.clearDisplay(); // clear the graphcis buffer
u8g2_for_adafruit_gfx.setFontMode(1); // use u8g2 transparent mode (this is default)
u8g2_for_adafruit_gfx.setFontDirection(0); // left to right (this is default)
u8g2_for_adafruit_gfx.setForegroundColor(WHITE); // apply Adafruit GFX color
u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR14_tf); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2_for_adafruit_gfx.setCursor(0,20); // start writing at this position
u8g2_for_adafruit_gfx.print(F("Hello World"));
u8g2_for_adafruit_gfx.setCursor(0,40); // start writing at this position
u8g2_for_adafruit_gfx.print(F("Umlaut ÄÖÜ")); // UTF-8 string with german umlaut chars
display.display(); // make everything visible
delay(2000);
}
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx
: Constructor call. This must be
the first command. No paramenters are required.void u8g2_for_adafruit_gfx.begin(Adafruit_GFX &gfx)
: Connect to Adafruit GFX libraryvoid u8g2_for_adafruit_gfx.setFont(const uint8_t *font)
: Assign u8g2 font, see here for a list.
Note: The list from the u8g2 project might be a little bit newer, so maybe not allways all fonts are available for this project.
See also: https://github.com/olikraus/u8g2/wiki/u8g2reference#setfontvoid u8g2_for_adafruit_gfx.setFontMode(uint8_t is_transparent)
: Define, whether the background color should be drawn or not.
See also: https://github.com/olikraus/u8g2/wiki/u8g2reference#setfontmodevoid u8g2_for_adafruit_gfx.setFontDirection(uint8_t dir)
: Identical to the u8g2 command
https://github.com/olikraus/u8g2/wiki/u8g2reference#setfontdirectionvoid u8g2_for_adafruit_gfx.setForegroundColor(uint16_t color)
: Set the foreground color for the text.void u8g2_for_adafruit_gfx.setBackgroundColor(uint16_t color)
: Set the background color for the text. This is only required if the font mode is not set to transparent.void u8g2_for_adafruit_gfx.setCursor(int16_t x, int16_t y)
: Specify the x/y position for the next print() command. This x/y position is the lower left baseline of a character.
For "Arduino" this will be the lower left corner of the A character.void u8g2_for_adafruit_gfx.print(...)
: This is the standard Arduino print function. Any text given here will be output on the display via Adafruit GFX library.
See here for further description. Note: This function supports UTF-8.int8_t u8g2_for_adafruit_gfx.getFontAscent(void)
: The height of the character 'A' in the current font.int8_t u8g2_for_adafruit_gfx.getFontDescent(void)
: The extend of 'g' below baseline. This value usually is negative.int16_t u8g2_for_adafruit_gfx.getUTF8Width(const char *str)
: Return the width of the provided string.
It is assumed, that str
is encoded in UTF8 (this is default for Arduino IDE and gcc/g++).There are some minor differences to the U8g2 library:
setDrawColor()
function. print()
command supports UTF-8 by default. It also can not be disabled. For U8g2 the UTF-8 support must be enabled first.