Home

Awesome

Packed Font

Memory efficient MicroPython fonts for the Pico Pi and SSD1306 OLED Display

MicroPython comes with an 8 x 8 pixel font which allows for a surprising amount of text to be displayed on the tiny OLED display. However the font is very small, especially for those of us getting older.

A Packed Font has the following features:

Quick Start

There is an example application in the ./display/ folder which you can copy to your Pico Pi and run using Thonny. It runs through a few different examples of rendering text and icons.

Welcome Screen
128 Pixel Icon and 16 Pixel Font

The above screen is rendered as follows:

from enhanced_display import Enhanced_Display

if __name__ == "__main__":

    display = Enhanced_Display()

    # Load the list of fonts to use
    display.load_fonts(['digits-30', 'text-16', 'icons-32', 'icons-128'])

    # Display the Welcome screen
    display.fill(0)                         # Clear the screen

    display.select_font('icons-128')
    display.text('s', 0, 0)                 # The 's' character is the Star icon
    display.select_font('text-16')
    display.text('Welcome', 0, 0, 1, 1)     # Center the text both horizontally and vertically.

    display.save_screenshot("title.bmp")    # Take a screenshot and save to file.

    display.show()
Right-aligned Text
16 Pixel Text Font right-aligned

The above is rendered as follows:

    # Display the right aligned text screen
    display.fill(0)

    display.text('right, top', 0, 0, 2, 0)
    display.text('right, center', 0, 0, 2, 1)
    display.text('right, bottom', 0, 0, 2, 2)

    display.show()
Temperature Screen
30 Pixel Digits Font, 8 Pixel Built in Font and 32 Pixel Icon

The above screen is rendered as follows:

    # Display the Temperature screen
    display.fill(0)

    display.select_font('digits-30')
    degrees = '\u00b0'  # Character code for the degrees symbol
    display.text(f'12.3{degrees}', 0, 0, 1, 1)
    display.select_font('icons-32')
    display.text('t', 0, 0, 2)          # The 't' character contains the temperature icon
    display.select_font(None)           # Select the built in 8 pixel font
    display.text('Temperature', 0, 0, 1, 2)

    display.show()

If you haven't programmed your Pico Pi using Thonny before, here is a great article to get you started.

How to setup a Raspberry Pi Pico and Code with Thonny

Example Fonts

There are two example fonts available for immediate use:

  1. A 'text-16.pf' font based on the Arial font with characters in the ASCII range 32-126.
  2. A 'digits-30.pf' font based on the GillSansNova font containing the digits 0-9, the minus symbol and the degrees symbol.

To use these in your application, you will need the following files from the ./display/ folder:

Have a look at main.py for examples on how to load, select and render packed fonts.

Enhanced Display Class

The easiest way to use packed fonts on the SSD1306 OLED Display, is to use the Enhanced Display class. This class has the same interface as the PiicoDev_SSD1306 class but has the following additional features:

Creating your own Fonts

It's easy to create custom fonts for use in your own applications. The advantage of creating your own fonts is you can choose just the characters you need. There are two Python scripts in the create/ folder used for this purpose. Note that both require the Python Image Processing Library Pillow (PIL) library to be installed.

Creating a font from an existing TrueType font on your PC is a two step process.

  1. Run the create-font.py script to convert a TrueType font into a font definition file (font_name.json) and a series of bitmaps (1 per character).
  2. Run the pack-font.py script to convert the font definition file and character bitmaps into a single font_name.pf packed font file. You can then copy the packed font file onto your Pico Pi for use in your application.

There are some example PowerShell scripts in the create/ folder which were used to create the example packed fonts used by the example application.

Notes