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
10
11# NOTE: You must copy the fonts/ folder from the
12# examples in the repo to your CIRCUITPY drive.
13# you can change this to a different bdf or pcf font file
14font_file = "fonts/LeagueSpartan-Bold-16.bdf"
15
16# you can change the string that will get printed here
17message = "<3 Blinka"
18
19font = bitmap_font.load_font(font_file)
20
21_, height, _, dy = font.get_bounding_box()
22font.load_glyphs(message)
23
24for y in range(height):
25 for c in message:
26 glyph = font.get_glyph(ord(c))
27 if not glyph:
28 continue
29 glyph_y = y + (glyph.height - (height + dy)) + glyph.dy
30 pixels = []
31 if 0 <= glyph_y < glyph.height:
32 for i in range(glyph.width):
33 value = glyph.bitmap[i, glyph_y]
34 pixel = " "
35 if value > 0:
36 pixel = "#"
37 pixels.append(pixel)
38 else:
39 pixels = ""
40 print("".join(pixels) + " " * (glyph.shift_x - len(pixels)), end="")
41 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
11
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
19# NOTE: You must copy the fonts/ folder from the
20# examples in the repo to your CIRCUITPY drive.
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"
27font = bitmap_font.load_font(font_file)
28color = 0xFF00FF
29
30# Create the tet label
31text_area = label.Label(font, text=text, color=color)
32
33# Set the location
34text_area.x = 20
35text_area.y = 20
36
37# Show it
38display.root_group = text_area
39
40while True:
41 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
12import board
13import displayio
14
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# NOTE: You must copy the fonts/ folder from the
23# examples in the repo to your CIRCUITPY drive.
24# try uncommenting different font files if you like
25font_file = "fonts/LeagueSpartan-Bold-16.bdf"
26# font_file = "fonts/Junction-regular-24.pcf"
27
28font = bitmap_font.load_font(font_file)
29
30bitmap = displayio.Bitmap(display.width, display.height, 2)
31
32palette = displayio.Palette(2)
33
34palette[0] = 0x000000
35palette[1] = 0xFFFFFF
36
37_, height, _, dy = font.get_bounding_box()
38for y in range(height):
39 pixels = []
40 for c in "Adafruit CircuitPython":
41 glyph = font.get_glyph(ord(c))
42 if not glyph:
43 continue
44 glyph_y = y + (glyph.height - (height + dy)) + glyph.dy
45
46 if 0 <= glyph_y < glyph.height:
47 for i in range(glyph.width):
48 value = glyph.bitmap[i, glyph_y]
49 pixel = 0
50 if value > 0:
51 pixel = 1
52 pixels.append(pixel)
53 else:
54 # empty section for this glyph
55 for i in range(glyph.width):
56 pixels.append(0)
57
58 # one space between glyph
59 pixels.append(0)
60
61 if pixels:
62 for x, pixel in enumerate(pixels):
63 bitmap[x, y] = pixel
64
65# Create a TileGrid to hold the bitmap
66tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette)
67
68# Create a Group to hold the TileGrid
69group = displayio.Group()
70
71group.x = 20
72# Add the TileGrid to the Group
73group.append(tile_grid)
74
75# Add the Group to the Display
76display.root_group = group
77
78while True:
79 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"""
9
10import time
11
12import board
13from adafruit_display_text import label
14
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# wait until we can refresh the display
22time.sleep(display.time_to_refresh)
23
24# NOTE: You must copy the fonts/ folder from the
25# examples in the repo to your CIRCUITPY drive.
26# try uncommenting different font files if you like
27font_file = "fonts/LeagueSpartan-Bold-16.bdf"
28# font_file = "fonts/Junction-regular-24.pcf"
29
30# Set text, font, and color
31text = "HELLO WORLD\nbitmap_font example"
32font = bitmap_font.load_font(font_file)
33color = 0xFFFFFF
34background_color = 0x999999
35
36# Create the tet label
37text_area = label.Label(
38 font,
39 text=text,
40 color=color,
41 background_color=background_color,
42 padding_top=3,
43 padding_bottom=3,
44 padding_right=4,
45 padding_left=4,
46)
47text_area.line_spacing = 1.0
48# Set the location
49text_area.x = 20
50text_area.y = 20
51
52# Show it and refresh
53display.root_group = text_area
54display.refresh()
55while True:
56 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 adafruit_display_text import label
13from bitmap_font_forkawesome_icons import microchip, python, terminal
14
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# NOTE: You must copy the fonts/ folder from the
23# examples in the repo to your CIRCUITPY drive.
24font_file = "fonts/forkawesome-42.pcf"
25
26# Set text, font, and color
27text = f"{terminal} {python} {microchip}"
28font = bitmap_font.load_font(font_file)
29color = 0xFF00FF
30
31# Create the tet label
32text_area = label.Label(font, text=text, color=color)
33
34# Set the location
35text_area.anchor_point = (0.5, 0.5)
36text_area.anchored_position = (display.width // 2, display.height // 2)
37
38# Show it
39display.root_group = text_area
40
41while True:
42 pass