U8g2 for Seeeduino boards

U8g2_Seeed

Overview

U8g2 is a monochrome graphics library for embedded devices. Provided by Olikraus

                             U8g2

  • 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

Install U8g2

The Arduino library U8g2 can be installed from the library manager of the Arduino IDE.

  • Open Arduino IDE
  • Open the Library Manager (Sketch > Include Library > manage Libraries…)

1

  • Enter "u8g2" into the search field
  • Select latest version
  • Click "Install" button

2

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. 3

The U8g2 library can be seen in Examples after successful installation

4

 

 

 

How to connect a display to U8g2 Library?

Identify the display

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.

Select a physical bus

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 initialization

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:

Start a U8g2 Arduino project on K210 PI

5

In order for K210 PI to run the U8g2 library, must download the latest K210 PI library and U8g2 library.

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.

 

 

 

API Reference

U8g2 library provides a large number of apis for users to use. The following table shows the most commonly used apis and their descriptions.

APIDescription
beginstart a display.
clearClears all pixel on the display and the buffer.
initDisplayReset and configure the display.
sendBufferSend the content of the memory frame buffer to the display.
clearBufferClears all pixel in the memory frame buffer.
clearDisplayClears all pixel in the internal buffer AND on the connected display.
drawPixelDraw a pixel at the specified x/y position.
drawLineDraw a line between two points.
drawBoxDraw a box (filled frame), starting at x/y position (upper left edge).
drawCircleDraw a circle with radus rad at position (x0, y0).
drawDiscDraw a filled circle with radus rad at position (x0, y0).
drawEllipseDraw ellipse with radus rx and 'ry' at position (x0, y0).
drawFilledEllipseDraw a filled ellipse with radus rx and 'ry' at position (x0, y0).
drawFrameDraw a frame (empty box), starting at x/y
drawTriangleDraw a triangle (filled polygon)position (upper left edge).
setFontDefine a u8g2 font for the glyph and string drawing functions.
drawGlyphDraw a single character.
drawStrDraw a string. The first character is placed at position x andy.
drawBitmapDraw a bitmap at the specified x/y position (upper left corner of the bitmap). ==deprecated==
drawXBMDraw a XBM Bitmap. Position (x,y) is the upper left corner of the bitmap.
firstPageThis command is part of the (picture) loop which renders the content of the display.
nextPageThis command is part of the (picture) loop which renders the content of the display.
setBitmapModeSet the bitmap transparent or soild.
setCursorDefine the cursor for the print function. Any output of the print function will start at this position.
setDisplayRotationChanges the display rotation.

More information about U8g2 API can be found here.

Demonstration

Basic

The following shows how to use and display the basic drawing functions provided by U8g2.

src

demo

String

U8g2 provides powerful text output capabilities, it can not only change the size of the output text but also the font format.

  • setFont(const uint8_t *font). more
  • drawStr(u8g2_uint_t x, u8g2_uint_t y, const char *s). more

src

demo

string

Image

U8g2 library provides two functions to display images.

  • drawBitmap()(deprecated).
  • drawXBM().

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.

logo

src

demo

u8g2

Performance

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.

demo

We can notice that FPS is around 15,which can meet a large number of application scenarios.

demo

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.

This article demonstrates some of the most common features of the U8g2 library. For more information, click here.

About Author

Calendar

July 2019
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
293031