Guide to the API

lbutils

Utilities and Drivers for MicroPython used at Leeds Beckett University. The library is organised into the following modules, which are designed to be stand- alone.

  • drivers: Classes aimed at low-level support of I2C, SPI and other devices requiring board-level support.
  • fonts: Classes providing glyph and character support for display drivers.
  • helpers: Functions and classes which help replace boiler-plate code for tasks such as setting up network access.
  • pmod: Drivers and support for the Digilent peripheral modules.

Diagrammatically these modules (and classes) are organised as follows

Package and Class Diagram for lbutils

Each module can be imported in its entirety to expose the individual drivers, for instance as

from lbutils.drivers import SegHexDisplay

In most cases the drivers are organised into 'sub-sets', e.g. those drivers which deal with seven segment displays. These sub-sets are not exposed directly as Python modules, but instead are for convenience to organise the library. These sub-sets can be found linked in the side-bar under the 'Reference' area. In either case, see the individual module documentation for the specific classes which the module exposes.

Note

In some cases loading the entire module into memory may not be a good idea, as some of the modules can consume significant memory resources. For production code it is better to import specific drivers directly, referencing the file the driver is contained in as

from lbutils.drivers.seven_segment import SegDisplay

Known Implementations

  • Raspberry Pi Pico W (MicroPython 3.4)
  • CPython (3.10)

lbutils.drivers

Drivers for low-level components, including those using the SPI or I2C busses. This is mostly a collection of drivers, some third-party, which provide low-level access to devices. In most cases additional code will be required to use these devices: the focus of the code in here is only on providing access to those devices.

Examples for how to use the library can be found in the 'examples' folder: or in the documentation for specific classes. In some cases the examples will require a specific example circuit: where this is the case, in most cases classes will additionally provide an example on WokWi.

Note

The Digilent 'pmod' devices are split into their own special section, and should be imported using the 'pmod' libraries.

Tested Implementations

This version is written for MicroPython 3.4, and has been tested on:

  • Raspberry Pi Pico H/W

Classes

Drivers for the display of a single digit

  • SegDisplay. Display a single decimal digit, using seven GPIO pins.
  • SegHexDisplay. Display a single hexadecimal digit, using seven GPIO pins.

lbutils.graphics.fonts

Fonts for the LED and OLED displays; especially the those using the 'pmod' packages. These fonts are based on open-source font files, and converted with the fontconvert utility written by Daniel Perron.S.

Most of the work of this library consists of rebuilding the font representation as described below. This reconstruction is undertaken by the BaseFont class, and abstract class which must be sub-classed for a specific font. Currently the fonts (and subclasses) exposed in this library are

  • Font06. 6x6 pixel sans-serif font.
  • Font08. 8x8 pixel sans-serif font.
  • Org01. A tiny, stylized font with all characters within a 6 pixel height. Created by fontconvert, from the Org_v01 by Orgdot.

All fonts are provided in a single package, lbutils.graphics.fonts, with most of the functionality implemented by the the BaseFont class. Together the BaseFont class and the classes above providing specific fonts are organised as follows

Graphics Package Diagram

Rebuilding the Fonts

Rebuilding the fonts requires the fontconvert utility to be built in C with [the FreeType library] (https://freetype.org). Once built the fonts can be converted using fontconvert as

./fontconvert ~/Library/Fonts/FreeSans.ttf 18 > FreeSans18pt7b.py

Currently the fontconvert utility only extracts the printable 7-bit ASCII characters of a font: for the exact character set extracted see the resulting Python file.

Tested Implementations

  • Raspberry Pi Pico W (MicroPython 3.4)
  • CPython (3.10)

Classes

Classes

  • BaseFont. Base class, used by the font display routines.
  • Font06. Six Pixel bitmap font.
  • Font08. Eight Pixel bitmap font.
  • Org01. Alternative bitmap font.

lbutils.helpers

Helper code and utilities, mostly helping to automate common tasks and replace boiler-plate code. Many of the classes and utilities in this collection are aimed at supporting the Leeds Beckett micro-controller development board, but may also be helpful for bare Pico H/W support.

Tested Implementations

This version is written for MicroPython 3.4, and has been tested on:

  • Raspberry Pi Pico H/W

Functions

  • scan_i2c_bus. Scan for I2C devices on the listed bus, printing out the found device addresses to the console.