U8g2 is a monochrome graphics library for embedded devices. Provided by Olikraus
- Supported Display Controller: SSD1305, SSD1306, SSD1309, SSD1322, SSD1325, SSD1327, SSD1329, SSD1606, SSD1607, SH1106, SH1107, SH1108, SH1122, T6963, RA8835, LC7981, PCD8544, PCF8812, HX1230, UC1601, UC1604, UC1608, UC1610, UC1611, UC1701, ST7565, ST7567, ST7588, ST75256, NT7534, IST3020, ST7920, LD7032, KS0108, SED1520, SBN1661, IL3820, MAX7219 (see here for a full list)
U8g2 also includes U8x8 library. Features for U8g2 and U8x8 are:
- U8g2 Includes all graphics procedures (line/box/circle draw). Supports many fonts. (Almost) no restriction on the font height. Requires some memory in the microcontroller to render the display.
- U8x8 Text output only (character) device. Only fonts allowed with fixed size per character (8×8 pixel). Writes directly to the display. No buffer in the microcontroller required. There is one more class/sub-library "U8log", which emulates an output terminal (like Arduino serial monitor): U8log Reference Manual
- Open Arduino IDE
- Open the Library Manager (Sketch > Include Library > manage Libraries…)
- Enter "u8g2" into the search field
- Select latest version
- Click "Install" button
If you want to use the latest U8g2 library, click on the link: U8g2_Arduino
- Open Arduino IDE
- Open the Library Manager (Sketch > Include Library > Add .ZIP Library…).
- Select the most recently downloaded U8g2 library.
You need to know the controller and the size of your display (Note: "Controllerless" displays are not supported by U8g2). In this example, we will use a Seeed_128x128 128×64 display, and K210 Pi as controller.
For U8g2 you have to select a matching constructor for the correct initialization of the display. Select a constructor from this page: C++ Setup/Arduino Constructor List.
Graphics information has to be sent to the display. This information is received by the display controller through a physical bus (usually two or more data lines), a communication protocol and a command sequence. Often a display supports more than one physical bus. You have to select and setup the correct bus. Typically the busses are:
- 3SPI, 3-wire SPI: Serial Peripheral Interface with three signals: Clock, Data and Chip-Select.
- 4SPI, 4-Wire SPI: Same as 3SPI, but with one additional line for commands and data (often labeled as D/C, RS or A0)
- I2C, IIC or TWI: Inter-Integrated Circuit Bus which has two signals: Clock (SCL) and Data (SDA).
- 8080: A 8-Bit bus which requires 8 data lines, chip select and a write strobe signal.
- 6800: Another 8-Bit bus, but with a different protocol.
Because Seeed_128X128 display USES I2C as the physical bus, we connect OLED display with K210 PI through I2C.
U8g2 needs to know which output pin is connected to which input of the display: The pin numbers are provided as list of arguments to the U8g2 constructor. The position within the list is important and indicates the function/purpose of the pin.
From the table here, the constructor has the following template:
The first parameter is the rotation option, the constructor expects the pin number for the clock line(SCL) and the data line(SDA) . The reset line can be provided optionally.If you are using hardware I2C, you do not need to specify SCL and SDA.
Get more Info: U8g2 Setup
A complete example will look like this:
In addition, you may get an error prompt of "The command line is too long" at compile time. You can solve this problem by deleting the.c file in the \src\clib directory for display configuration,and only keep the current display file.
- The format of the display configuration file is: u8x8_d_display_size.c.
The bold part is the specific display model and size.
For example, this time we used the following display configuration file is: u8x8_d_sh1107.c.
U8g2 library provides a large number of apis for users to use. The following table shows the most commonly used apis and their descriptions.
|begin||start a display.|
|clear||Clears all pixel on the display and the buffer.|
|initDisplay||Reset and configure the display.|
|sendBuffer||Send the content of the memory frame buffer to the display.|
|clearBuffer||Clears all pixel in the memory frame buffer.|
|clearDisplay||Clears all pixel in the internal buffer AND on the connected display.|
|drawPixel||Draw a pixel at the specified x/y position.|
|drawLine||Draw a line between two points.|
|drawBox||Draw a box (filled frame), starting at x/y position (upper left edge).|
|drawCircle||Draw a circle with radus rad at position (x0, y0).|
|drawDisc||Draw a filled circle with radus rad at position (x0, y0).|
|drawEllipse||Draw ellipse with radus rx and 'ry' at position (x0, y0).|
|drawFilledEllipse||Draw a filled ellipse with radus rx and 'ry' at position (x0, y0).|
|drawFrame||Draw a frame (empty box), starting at x/y|
|drawTriangle||Draw a triangle (filled polygon)position (upper left edge).|
|setFont||Define a u8g2 font for the glyph and string drawing functions.|
|drawGlyph||Draw a single character.|
|drawStr||Draw a string. The first character is placed at position x andy.|
|Draw a bitmap at the specified x/y position (upper left corner of the bitmap). ==deprecated==|
|drawXBM||Draw a XBM Bitmap. Position (x,y) is the upper left corner of the bitmap.|
|firstPage||This command is part of the (picture) loop which renders the content of the display.|
|nextPage||This command is part of the (picture) loop which renders the content of the display.|
|setBitmapMode||Set the bitmap transparent or soild.|
|setCursor||Define the cursor for the print function. Any output of the print function will start at this position.|
|setDisplayRotation||Changes the display rotation.|
More information about U8g2 API can be found here.
The following shows how to use and display the basic drawing functions provided by U8g2.
U8g2 provides powerful text output capabilities, it can not only change the size of the output text but also the font format.
U8g2 library provides two functions to display images.
In order to display the image on monochrome OLED, we need to convert the image to XBM format. You can convert online via the WEB, or use any tool you know.
According to the kendryte k210 datasheet, I2C can work in following modes:
- Standard mode (0 to 100Kb/s).
- Fast mode (<= 400Kb/s).
U8g2 Examples include Example of performance testing.
- Open the example: FPS (File > Examples > U8g2 > full_buffer > FPS).
- Unccomment the Corresponding contructor line.
- Call u8g2.setBusClock() before the first call to u8g2.begin().
- Compile and run to see the results.
We can notice that FPS is around 15，which can meet a large number of application scenarios.
When the working frequency of I2C is in 400Khz mode, the FPS reaches about 25.In practice, I2C frequencies can be even faster, but the CPU may be unstable, so use it with caution.