Simple test¶
Ensure your device works with this simple test.
1# SPDX-FileCopyrightText: 2020 Tim C
2#
3# SPDX-License-Identifier: Unlicense
4"""
5Make green and purple rectangles and a
6"Hello World" label.
7"""
8import time
9
10import displayio
11import rainbowio
12import terminalio
13from adafruit_display_text import label
14from blinka_displayio_pygamedisplay import PyGameDisplay
15
16
17# Make the display context
18display = PyGameDisplay(icon="blinka.png", width=400, height=300)
19
20# Make the display context
21splash = displayio.Group()
22display.show(splash)
23
24# Draw a green background
25color_bitmap = displayio.Bitmap(display.width, display.height, 1)
26color_palette = displayio.Palette(1)
27color_palette[0] = 0x00FF00 # Bright Green
28
29bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
30
31splash.append(bg_sprite)
32
33# Draw a smaller inner rectangle
34inner_bitmap = displayio.Bitmap(display.width - 40, display.height - 40, 1)
35inner_palette = displayio.Palette(1)
36inner_palette[0] = 0xAA0088 # Purple
37inner_sprite = displayio.TileGrid(inner_bitmap, pixel_shader=inner_palette, x=20, y=20)
38splash.append(inner_sprite)
39
40# Draw a label
41text_group = displayio.Group()
42text_area = label.Label(terminalio.FONT, text="Hello World!", color=0xFFFF00, scale=4)
43text_area.anchor_point = (0.5, 0.5)
44text_area.anchored_position = (display.width // 2, display.height // 2)
45
46# text_group.append(text_area) # Subgroup for text scaling
47splash.append(text_area)
48
49color_num = 0
50while True:
51 text_area.color = rainbowio.colorwheel(color_num)
52 color_num += 1
53 if color_num > 255:
54 color_num = 0
55 print(time.monotonic())
56 time.sleep(0.05)
57
58 if display.check_quit():
59 break
Anchor test¶
Testing anchor point of adafruit_display_text label.
1# SPDX-FileCopyrightText: 2020 Tim C
2#
3# SPDX-License-Identifier: Unlicense
4"""
5Testing anchor point of adafruit_display_text label.
6"""
7import terminalio
8import displayio
9from adafruit_display_text import label
10from blinka_displayio_pygamedisplay import PyGameDisplay
11
12display = PyGameDisplay(width=1770, height=920)
13
14text_area = label.Label(terminalio.FONT, text="Hello world", scale=3)
15
16text_area.anchor_point = (0.5, 0.5)
17text_area.anchored_position = (display.width // 2, display.height // 2)
18print(text_area.bounding_box)
19print(f"{text_area.x}, {text_area.y}")
20main_group = displayio.Group()
21main_group.append(text_area)
22display.show(main_group)
23
24# text_area.y = 37
25while True:
26 if display.check_quit():
27 break
Imageload BMP¶
Use adafruit_imageload to show a bitmap on the screen
1# SPDX-FileCopyrightText: 2020 Tim C
2#
3# SPDX-License-Identifier: Unlicense
4"""
5Use adafruit_imageload to show a bitmap on the screen
6"""
7import displayio
8import adafruit_imageload
9from blinka_displayio_pygamedisplay import PyGameDisplay
10
11display = PyGameDisplay(icon="blinka.png", width=800, height=600)
12
13bitmap, palette = adafruit_imageload.load(
14 "robot_friend.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette
15)
16
17# Create a TileGrid to hold the bitmap
18tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette)
19
20# Create a Group to hold the TileGrid
21img_group = displayio.Group()
22
23# Add the TileGrid to the Group
24img_group.append(tile_grid)
25
26# Add the Group to the Display
27display.show(img_group)
28
29# Loop forever so you can enjoy your image
30while True:
31 if display.check_quit():
32 break
PartyParrot¶
Party parrot animation code adapted from: https://github.com/adafruit/Adafruit_Learning_System_Guides/tree/master/IoT_Party_Parrot
1# SPDX-FileCopyrightText: 2020 Tim C
2#
3# SPDX-License-Identifier: Unlicense
4"""
5Party parrot animation code adapted from:
6https://github.com/adafruit/Adafruit_Learning_System_Guides/tree/master/IoT_Party_Parrot
7
8Thank you @BlitzCityDIY
9"""
10import time
11import adafruit_imageload
12import displayio
13from blinka_displayio_pygamedisplay import PyGameDisplay
14
15display = PyGameDisplay(width=320, height=320)
16
17group = displayio.Group(scale=10)
18
19# get the spritesheet from here:
20# https://github.com/adafruit/Adafruit_Learning_System_Guides/tree/master/IoT_Party_Parrot
21
22# load in party parrot bitmap
23parrot_bit, parrot_pal = adafruit_imageload.load(
24 "partyParrotsTweet.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette
25)
26
27parrot_grid = displayio.TileGrid(
28 parrot_bit,
29 pixel_shader=parrot_pal,
30 width=1,
31 height=1,
32 tile_height=32,
33 tile_width=32,
34 default_tile=10,
35 x=0,
36 y=0,
37)
38
39group.append(parrot_grid)
40
41display.show(group)
42
43parrot = True # state to track if an animation is currently running
44party = 0 # time.monotonic() holder
45p = 0 # index for tilegrid
46party_count = 0 # count for animation cycles
47
48while True:
49 # when a new tweet comes in...
50 if parrot:
51 # every 0.1 seconds...
52 if (party + 0.1) < time.monotonic():
53 # the party parrot animation cycles
54 parrot_grid[0] = p
55 # p is the tilegrid index location
56 p += 1
57 party = time.monotonic()
58 # if an animation cycle ends
59 if p > 9:
60 # index is reset
61 p = 0
62 # animation cycle count is updated
63 party_count += 1
64 print("party parrot", party_count)
65
66 if display.check_quit():
67 break
Progressbar¶
Example showing the use of adafruit_progressbar
1# SPDX-FileCopyrightText: 2020 Tim C
2#
3# SPDX-License-Identifier: Unlicense
4"""
5example showing the use of adafruit_progressbar
6"""
7import time
8import displayio
9from adafruit_progressbar.adafruit_progressbar import ProgressBar
10from blinka_displayio_pygamedisplay import PyGameDisplay
11
12# Make the display context
13splash = displayio.Group(scale=2)
14
15display = PyGameDisplay(width=480, height=320)
16
17color_bitmap = displayio.Bitmap(display.width, display.height, 1)
18color_palette = displayio.Palette(1)
19color_palette[0] = 0x0000FF
20
21bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
22splash.append(bg_sprite)
23
24display.show(splash)
25
26# set progress bar width and height relative to board's display
27width = display.width - 40
28height = 30
29
30x = display.width // 2 - width // 2
31y = display.height // 3
32
33# Create a new progress_bar object at (x, y)
34progress_bar = ProgressBar(x, y, width, height, 1.0)
35
36# Append progress_bar to the splash group
37splash.append(progress_bar)
38
39current_progress = 0.0
40while True:
41 # range end is exclusive so we need to use 1 bigger than max number that we want
42 for current_progress in range(0, 101, 1):
43 print("Progress: {}%".format(current_progress))
44 progress_bar.progress = current_progress / 100 # convert to decimal
45 time.sleep(0.01)
46 time.sleep(0.3)
47 progress_bar.progress = 0.0
48 time.sleep(0.3)
49
50 if display.check_quit():
51 break
Pyportal Bitcoin¶
This example will access the coindesk API, grab a number like bitcoin value in USD and display it on a screen If you can find something that spits out JSON data, we can display it! You can find any resources in the associated Learn Guide at: https://learn.adafruit.com/pyportal-bitcoin-value-display
1# SPDX-FileCopyrightText: 2020 Tim C
2#
3# SPDX-License-Identifier: Unlicense
4"""
5This example will access the coindesk API, grab a number like bitcoin value in
6USD and display it on a screen
7If you can find something that spits out JSON data, we can display it!
8You can find any resources in the associated Learn Guide at:
9https://learn.adafruit.com/pyportal-bitcoin-value-display
10"""
11import os
12import time
13from adafruit_pyportal import PyPortal
14from secret_credentials import secrets
15from blinka_displayio_pygamedisplay import PyGameDisplay
16
17# Make the display context
18display = PyGameDisplay(icon="blinka.png", width=320, height=240)
19# You can display in 'GBP', 'EUR' or 'USD'
20CURRENCY = "USD"
21# Set up where we'll be fetching data from
22DATA_SOURCE = "https://api.coindesk.com/v1/bpi/currentprice.json"
23DATA_LOCATION = ["bpi", CURRENCY, "rate_float"]
24
25
26def text_transform(val):
27 """Format value with currency symbol"""
28 if CURRENCY == "USD":
29 return "$%d" % val
30 if CURRENCY == "EUR":
31 return "€%d" % val
32 if CURRENCY == "GBP":
33 return "£%d" % val
34 return "%d" % val
35
36
37# the current working directory (where this file is)
38try:
39 cwd = os.path.dirname(os.path.realpath(__file__))
40except AttributeError:
41 cwd = ("/" + __file__).rsplit("/", 1)[0]
42
43pyportal = PyPortal(
44 external_spi="fake",
45 url=DATA_SOURCE,
46 json_path=DATA_LOCATION,
47 default_bg=cwd + "/bitcoin_background.bmp",
48 text_font=cwd + "/fonts/Arial-Bold-24-Complete.bdf",
49 text_position=(195, 130),
50 text_color=0x0,
51 text_transform=text_transform,
52 display=display,
53 secrets=secrets,
54)
55pyportal.preload_font(b"$012345789") # preload numbers
56pyportal.preload_font((0x00A3, 0x20AC)) # preload gbp/euro symbol
57
58while True:
59 try:
60 value = pyportal.fetch()
61 print("Response is", value)
62 except (ValueError, RuntimeError) as e:
63 print("Some error occured, retrying! -", e)
64
65 time.sleep(3 * 60) # wait 3 minutes
66
67 if display.check_quit():
68 break
Readme Example¶
Initialize the PyGame display and fill it with green
1# SPDX-FileCopyrightText: 2020 Tim C
2#
3# SPDX-License-Identifier: Unlicense
4"""
5Initialize the PyGame display and fill it with green
6"""
7import displayio
8from blinka_displayio_pygamedisplay import PyGameDisplay
9
10display = PyGameDisplay(width=320, height=240)
11splash = displayio.Group()
12display.show(splash)
13
14color_bitmap = displayio.Bitmap(display.width, display.height, 1)
15color_palette = displayio.Palette(1)
16color_palette[0] = 0x00FF00 # Bright Green
17
18bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
19splash.append(bg_sprite)
20
21
22while True:
23 if display.check_quit():
24 break
SETUP Only¶
Make green and purple rectangles and a “Hello World” label.
1# SPDX-FileCopyrightText: 2020 Tim C
2#
3# SPDX-License-Identifier: Unlicense
4"""
5Make green and purple rectangles and a
6"Hello World" label.
7"""
8import displayio
9from blinka_displayio_pygamedisplay import PyGameDisplay
10
11
12# Make the display context. Change size if you want
13display = PyGameDisplay(width=320, height=240)
14
15# Make the display context
16main_group = displayio.Group()
17display.show(main_group)
18
19
20while True:
21 if display.check_quit():
22 break
Shapes¶
This is adapted from an example in the shapes library to work with pygame display. It shows how to draw various different shapes and place them on the screen
1# SPDX-FileCopyrightText: 2020 Tim C
2#
3# SPDX-License-Identifier: Unlicense
4
5"""
6This is adapted from an example in the shapes library to work with pygame display.
7It shows how to draw various different shapes and place them on the screen.
8"""
9
10import displayio
11from adafruit_display_shapes.rect import Rect
12from adafruit_display_shapes.circle import Circle
13from adafruit_display_shapes.roundrect import RoundRect
14from adafruit_display_shapes.triangle import Triangle
15from adafruit_display_shapes.line import Line
16from adafruit_display_shapes.polygon import Polygon
17from blinka_displayio_pygamedisplay import PyGameDisplay
18
19# Make the display context
20splash = displayio.Group(scale=2)
21
22display = PyGameDisplay(icon="blinka.png", width=640, height=480)
23display.show(splash)
24
25# Make a background color fill
26color_bitmap = displayio.Bitmap(320, 240, 1)
27color_palette = displayio.Palette(1)
28color_palette[0] = 0xFFFFFF
29bg_sprite = displayio.TileGrid(color_bitmap, x=0, y=0, pixel_shader=color_palette)
30splash.append(bg_sprite)
31##########################################################################
32
33splash.append(Line(220, 130, 270, 210, 0xFF0000))
34splash.append(Line(270, 210, 220, 210, 0xFF0000))
35splash.append(Line(220, 210, 270, 130, 0xFF0000))
36splash.append(Line(270, 130, 220, 130, 0xFF0000))
37
38# Draw a blue star
39polygon = Polygon(
40 [
41 (255, 40),
42 (262, 62),
43 (285, 62),
44 (265, 76),
45 (275, 100),
46 (255, 84),
47 (235, 100),
48 (245, 76),
49 (225, 62),
50 (248, 62),
51 ],
52 outline=0x0000FF,
53)
54polygon.x += 150
55polygon.y += 50
56splash.append(polygon)
57
58triangle = Triangle(170, 50, 120, 140, 210, 160, fill=0x00FF00, outline=0xFF00FF)
59triangle.x += 240
60triangle.y += 180
61splash.append(triangle)
62
63rect = Rect(80, 20, 41, 41, fill=0x0)
64splash.append(rect)
65
66circle = Circle(100, 30, 20, fill=0x00FF00, outline=0xFF00FF)
67circle.x += 200
68splash.append(circle)
69
70print(circle.fill)
71
72rect2 = Rect(50, 100, 61, 81, outline=0x0, stroke=3)
73rect2.y += 10
74splash.append(rect2)
75
76
77roundrect = RoundRect(10, 10, 61, 81, 10, fill=0x0, outline=0xFF00FF, stroke=6)
78roundrect.y += 270
79splash.append(roundrect)
80
81
82while True:
83 if display.check_quit():
84 break
Shapes Sparkline¶
This example has been adapted from the example in the shapes library to work with pygame display.
1# SPDX-FileCopyrightText: 2020 Kevin Matocha, Tim C
2#
3# SPDX-License-Identifier: Unlicense
4"""
5This example has been adapted from the example in the shapes
6library to work with pygame display.
7"""
8
9# class of sparklines in CircuitPython
10# created by Kevin Matocha - Copyright 2020 (C)
11
12# See the bottom for a code example using the `sparkline` Class.
13
14# # File: display_shapes_sparkline.py
15# A sparkline is a scrolling line graph, where any values added to sparkline
16# using `add_value` are plotted.
17#
18# The `sparkline` class creates an element suitable for adding to the display
19# using `display.show(mySparkline)` or adding to a `displayio.Group` to be displayed.
20#
21# When creating the sparkline, identify the number of `max_items` that will be
22# included in the graph.
23# When additional elements are added to the sparkline and the number of items
24# has exceeded max_items, any excess values are removed from the left of the
25# graph, and new values are added to the right.
26
27
28# The following is an example that shows the
29
30# setup display
31# instance sparklines
32# add to the display
33# Loop the following steps:
34# add new values to sparkline `add_value`
35# update the sparklines `update`
36
37
38import random
39import time
40import displayio
41import terminalio
42from adafruit_display_text import label
43from adafruit_display_shapes.sparkline import Sparkline
44from adafruit_display_shapes.line import Line
45from adafruit_display_shapes.rect import Rect
46from blinka_displayio_pygamedisplay import PyGameDisplay
47
48
49##########################################
50# Create background bitmaps and sparklines
51##########################################
52
53display = PyGameDisplay(icon="blinka.png", width=640, height=480)
54
55# Baseline size of the sparkline chart, in pixels.
56chart_width = display.width - 50
57chart_height = display.height - 50
58
59font = terminalio.FONT
60
61LINE_COLOR = 0xFFFFFF
62
63# Setup the first bitmap and sparkline
64# This sparkline has no background bitmap
65# mySparkline1 uses a vertical y range between 0 to 10 and will contain a
66# maximum of 40 items
67sparkline1 = Sparkline(
68 width=chart_width,
69 height=chart_height,
70 max_items=40,
71 y_min=0,
72 y_max=10,
73 x=40,
74 y=30,
75 color=LINE_COLOR,
76)
77
78# Label the y-axis range
79
80TEXT_XOFFSET = -10
81text_label1a = label.Label(
82 font=font, text=str(sparkline1.y_top), color=LINE_COLOR
83) # yTop label
84text_label1a.anchor_point = (1, 0.5) # set the anchorpoint at right-center
85text_label1a.anchored_position = (
86 sparkline1.x + TEXT_XOFFSET,
87 sparkline1.y,
88) # set the text anchored position to the upper right of the graph
89
90text_label1b = label.Label(
91 font=font, text=str(sparkline1.y_bottom), color=LINE_COLOR
92) # yTop label
93text_label1b.anchor_point = (1, 0.5) # set the anchorpoint at right-center
94text_label1b.anchored_position = (
95 sparkline1.x + TEXT_XOFFSET,
96 sparkline1.y + chart_height,
97) # set the text anchored position to the upper right of the graph
98
99
100bounding_rectangle = Rect(
101 sparkline1.x, sparkline1.y, chart_width, chart_height, outline=LINE_COLOR
102)
103
104
105# Create a group to hold the sparkline, text, rectangle and tickmarks
106# append them into the group (my_group)
107#
108# Note: In cases where display elements will overlap, then the order the
109# elements are added to the group will set which is on top. Latter elements
110# are displayed on top of former elemtns.
111
112my_group = displayio.Group()
113
114my_group.append(sparkline1)
115my_group.append(text_label1a)
116my_group.append(text_label1b)
117my_group.append(bounding_rectangle)
118
119TOTAL_TICKS = 10
120
121for i in range(TOTAL_TICKS + 1):
122 x_start = sparkline1.x - 5
123 x_end = sparkline1.x
124 y_both = int(round(sparkline1.y + (i * (chart_height) / (TOTAL_TICKS))))
125 if y_both > sparkline1.y + chart_height - 1:
126 y_both = sparkline1.y + chart_height - 1
127 my_group.append(Line(x_start, y_both, x_end, y_both, color=LINE_COLOR))
128
129
130# Set the display to show my_group that contains the sparkline and other graphics
131display.show(my_group)
132
133# Start the main loop
134while True:
135
136 # Turn off auto_refresh to prevent partial updates of the screen during updates
137 # of the sparkline drawing
138 # display.auto_refresh = False
139
140 # add_value: add a new value to a sparkline
141 # Note: The y-range for mySparkline1 is set to 0 to 10, so all these random
142 # values (between 0 and 10) will fit within the visible range of this sparkline
143 sparkline1.add_value(random.uniform(0, 10))
144
145 # Turn on auto_refresh for the display
146 # display.auto_refresh = True
147
148 # The display seems to be less jittery if a small sleep time is provided
149 # You can adjust this to see if it has any effect
150 time.sleep(0.01)
151
152 if display.check_quit():
153 break
BitmapLabel Ascent and Descent Test¶
Make green and purple rectangles and a “Hello World” label.
1# SPDX-FileCopyrightText: 2020 Tim C
2#
3# SPDX-License-Identifier: Unlicense
4"""
5Make green and purple rectangles and a
6"Hello World" label.
7"""
8import displayio
9
10from adafruit_bitmap_font import bitmap_font
11
12from adafruit_display_text import bitmap_label, label
13from blinka_displayio_pygamedisplay import PyGameDisplay
14
15# Make the display context. Change size if you want
16display = PyGameDisplay(width=320, height=240)
17
18font = bitmap_font.load_font("font/forkawesome-36.pcf")
19w, h, dx, dy = font.get_bounding_box()
20
21glyphs = "".join(chr(0xF000 + i) for i in range(8))
22
23group = displayio.Group()
24
25label = bitmap_label.Label(
26 font=font, text=glyphs, background_color=0x0000DD, background_tight=True
27)
28# label = label.Label(font=font, text=glyphs, background_color=0x0000DD, background_tight=True)
29
30label.anchor_point = (0, 0)
31label.anchored_position = (0, 20)
32
33group.append(label)
34display.show(group)
35
36
37while True:
38 if display.check_quit():
39 break