Simple test

Ensure your device works with this simple test.

examples/bitmap_font_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4"""
 5This example loads a font and uses it to print an
 6ASCII art representation of the given string specimen
 7"""
 8
 9from adafruit_bitmap_font import bitmap_font  # pylint: disable=wrong-import-position
10
11# you can change this to a different bdf or pcf font file
12font_file = "fonts/LeagueSpartan-Bold-16.bdf"
13
14# you can change the string that will get printed here
15message = "<3 Blinka"
16
17font = bitmap_font.load_font(font_file)
18
19_, height, _, dy = font.get_bounding_box()
20font.load_glyphs(message)
21
22for y in range(height):
23    for c in message:
24        glyph = font.get_glyph(ord(c))
25        if not glyph:
26            continue
27        glyph_y = y + (glyph.height - (height + dy)) + glyph.dy
28        pixels = []
29        if 0 <= glyph_y < glyph.height:
30            for i in range(glyph.width):
31                value = glyph.bitmap[i, glyph_y]
32                pixel = " "
33                if value > 0:
34                    pixel = "#"
35                pixels.append(pixel)
36        else:
37            pixels = ""
38        print("".join(pixels) + " " * (glyph.shift_x - len(pixels)), end="")
39    print()

Label simple test

This example uses adafruit_display_text.label to display text using a custom font loaded by adafruit_bitmap_font

examples/bitmap_font_label_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4"""
 5This example uses adafruit_display_text.label to display text using a custom font
 6loaded by adafruit_bitmap_font
 7"""
 8
 9import board
10from adafruit_display_text import label
11from adafruit_bitmap_font import bitmap_font
12
13# use built in display (MagTag, PyPortal, PyGamer, PyBadge, CLUE, etc.)
14# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
15# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
16display = board.DISPLAY
17
18# try uncommenting different font files if you like
19font_file = "fonts/LeagueSpartan-Bold-16.bdf"
20# font_file = "fonts/Junction-regular-24.pcf"
21
22# Set text, font, and color
23text = "HELLO WORLD"
24font = bitmap_font.load_font(font_file)
25color = 0xFF00FF
26
27# Create the tet label
28text_area = label.Label(font, text=text, color=color)
29
30# Set the location
31text_area.x = 20
32text_area.y = 20
33
34# Show it
35display.root_group = text_area
36
37while True:
38    pass

Displayio simple test

This example uses adafruit_bitmap_font to load a font and fill a bitmap with pixels matching glyphs from a given String

examples/bitmap_font_displayio_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4"""
 5This example runs on PyPortal, or any Circuit Python device
 6with a built-in screen.
 7
 8It will use adafruit_bitmap_font to load a font and fill a
 9bitmap with pixels matching glyphs from a given String
10"""
11
12
13import board
14import displayio
15from adafruit_bitmap_font import bitmap_font
16
17# use built in display (MagTag, PyPortal, PyGamer, PyBadge, CLUE, etc.)
18# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
19# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
20display = board.DISPLAY
21
22# try uncommenting different font files if you like
23font_file = "fonts/LeagueSpartan-Bold-16.bdf"
24# font_file = "fonts/Junction-regular-24.pcf"
25
26font = bitmap_font.load_font(font_file)
27
28bitmap = displayio.Bitmap(display.width, display.height, 2)
29
30palette = displayio.Palette(2)
31
32palette[0] = 0x000000
33palette[1] = 0xFFFFFF
34
35_, height, _, dy = font.get_bounding_box()
36for y in range(height):
37    pixels = []
38    for c in "Adafruit CircuitPython":
39        glyph = font.get_glyph(ord(c))
40        if not glyph:
41            continue
42        glyph_y = y + (glyph.height - (height + dy)) + glyph.dy
43
44        if 0 <= glyph_y < glyph.height:
45            for i in range(glyph.width):
46                value = glyph.bitmap[i, glyph_y]
47                pixel = 0
48                if value > 0:
49                    pixel = 1
50                pixels.append(pixel)
51        else:
52            # empty section for this glyph
53            for i in range(glyph.width):
54                pixels.append(0)
55
56        # one space between glyph
57        pixels.append(0)
58
59    if pixels:
60        for x, pixel in enumerate(pixels):
61            bitmap[x, y] = pixel
62
63# Create a TileGrid to hold the bitmap
64tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette)
65
66# Create a Group to hold the TileGrid
67group = displayio.Group()
68
69group.x = 20
70# Add the TileGrid to the Group
71group.append(tile_grid)
72
73# Add the Group to the Display
74display.root_group = group
75
76while True:
77    pass

Label MagTag

This example uses adafruit_display_text.label to display text using a custom font loaded by adafruit_bitmap_font.

examples/bitmap_font_label_magtag.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4"""
 5This example uses adafruit_display_text.label to display text using a custom font
 6loaded by adafruit_bitmap_font.
 7Adapted for use on MagTag
 8"""
 9import time
10import board
11from adafruit_display_text import label
12from adafruit_bitmap_font import bitmap_font
13
14# use built in display (MagTag, PyPortal, PyGamer, PyBadge, CLUE, etc.)
15# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
16# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
17display = board.DISPLAY
18# wait until we can refresh the display
19time.sleep(display.time_to_refresh)
20
21# try uncommenting different font files if you like
22font_file = "fonts/LeagueSpartan-Bold-16.bdf"
23# font_file = "fonts/Junction-regular-24.pcf"
24
25# Set text, font, and color
26text = "HELLO WORLD\nbitmap_font example"
27font = bitmap_font.load_font(font_file)
28color = 0xFFFFFF
29background_color = 0x999999
30
31# Create the tet label
32text_area = label.Label(
33    font,
34    text=text,
35    color=color,
36    background_color=background_color,
37    padding_top=3,
38    padding_bottom=3,
39    padding_right=4,
40    padding_left=4,
41)
42text_area.line_spacing = 1.0
43# Set the location
44text_area.x = 20
45text_area.y = 20
46
47# Show it and refresh
48display.root_group = text_area
49display.refresh()
50while True:
51    pass

Fork Awesome icons

This example uses adafruit_display_text.label to display fork awesome icons.

examples/bitmap_font_label_forkawesome.py
 1# SPDX-FileCopyrightText: 2021 Tim Cocks
 2# SPDX-License-Identifier: MIT
 3
 4"""
 5This example uses adafruit_display_text.label to display fork awesome
 6icons.
 7
 8More info here: https://emergent.unpythonic.net/01606790241
 9"""
10
11import board
12from bitmap_font_forkawesome_icons import microchip, python, terminal
13from adafruit_display_text import label
14from adafruit_bitmap_font import bitmap_font
15
16# use built in display (MagTag, PyPortal, PyGamer, PyBadge, CLUE, etc.)
17# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
18# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
19display = board.DISPLAY
20
21font_file = "fonts/forkawesome-42.pcf"
22
23# Set text, font, and color
24text = "{}  {}  {}".format(terminal, python, microchip)
25font = bitmap_font.load_font(font_file)
26color = 0xFF00FF
27
28# Create the tet label
29text_area = label.Label(font, text=text, color=color)
30
31# Set the location
32text_area.anchor_point = (0.5, 0.5)
33text_area.anchored_position = (display.width // 2, display.height // 2)
34
35# Show it
36display.root_group = text_area
37
38while True:
39    pass