Simple test¶
Ensure your device works with this simple test.
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import board
5import displayio
6from adafruit_display_shapes.rect import Rect
7from adafruit_display_shapes.circle import Circle
8from adafruit_display_shapes.roundrect import RoundRect
9from adafruit_display_shapes.triangle import Triangle
10from adafruit_display_shapes.line import Line
11from adafruit_display_shapes.polygon import Polygon
12
13# Make the display context
14splash = displayio.Group()
15board.DISPLAY.root_group = splash
16
17# Make a background color fill
18color_bitmap = displayio.Bitmap(320, 240, 1)
19color_palette = displayio.Palette(1)
20color_palette[0] = 0xFFFFFF
21bg_sprite = displayio.TileGrid(color_bitmap, x=0, y=0, pixel_shader=color_palette)
22splash.append(bg_sprite)
23##########################################################################
24
25splash.append(Line(220, 130, 270, 210, 0xFF0000))
26splash.append(Line(270, 210, 220, 210, 0xFF0000))
27splash.append(Line(220, 210, 270, 130, 0xFF0000))
28splash.append(Line(270, 130, 220, 130, 0xFF0000))
29
30# Draw a blue star
31polygon = Polygon(
32 [
33 (255, 40),
34 (262, 62),
35 (285, 62),
36 (265, 76),
37 (275, 100),
38 (255, 84),
39 (235, 100),
40 (245, 76),
41 (225, 62),
42 (248, 62),
43 ],
44 outline=0x0000FF,
45)
46splash.append(polygon)
47
48triangle = Triangle(170, 50, 120, 140, 210, 160, fill=0x00FF00, outline=0xFF00FF)
49splash.append(triangle)
50
51rect = Rect(80, 20, 41, 41, fill=0x0)
52splash.append(rect)
53
54circle = Circle(100, 100, 20, fill=0x00FF00, outline=0xFF00FF)
55splash.append(circle)
56
57rect2 = Rect(50, 100, 61, 81, outline=0x0, stroke=3)
58splash.append(rect2)
59
60roundrect = RoundRect(10, 10, 61, 81, 10, fill=0x0, outline=0xFF00FF, stroke=6)
61splash.append(roundrect)
62
63
64while True:
65 pass
Simple test MagTag¶
Simple test with the MagTag.
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import board
5import displayio
6from adafruit_display_shapes.rect import Rect
7from adafruit_display_shapes.circle import Circle
8from adafruit_display_shapes.roundrect import RoundRect
9from adafruit_display_shapes.triangle import Triangle
10from adafruit_display_shapes.line import Line
11from adafruit_display_shapes.polygon import Polygon
12
13# use built in display (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# Make the display context
19splash = displayio.Group()
20display.root_group = splash
21
22# Make a background color fill
23color_bitmap = displayio.Bitmap(display.width, display.height, 1)
24color_palette = displayio.Palette(1)
25color_palette[0] = 0xFFFFFF
26bg_sprite = displayio.TileGrid(color_bitmap, x=0, y=0, pixel_shader=color_palette)
27splash.append(bg_sprite)
28##########################################################################
29
30splash.append(Line(5, 74, 10, 110, 0x000000))
31splash.append(Line(15, 74, 20, 110, 0x000000))
32splash.append(Line(25, 74, 30, 110, 0x000000))
33splash.append(Line(35, 74, 40, 110, 0x000000))
34
35# Draw star
36polygon = Polygon(
37 [
38 (255, 40),
39 (262, 62),
40 (285, 62),
41 (265, 76),
42 (275, 100),
43 (255, 84),
44 (235, 100),
45 (245, 76),
46 (225, 62),
47 (248, 62),
48 ],
49 outline=0x000000,
50)
51splash.append(polygon)
52
53triangle = Triangle(170, 20, 140, 90, 210, 100, fill=0x999999, outline=0x000000)
54splash.append(triangle)
55
56rect = Rect(80, 20, 41, 41, fill=0x999999, outline=0x666666)
57splash.append(rect)
58
59circle = Circle(100, 100, 20, fill=0xFFFFFF, outline=0x000000)
60splash.append(circle)
61
62rect2 = Rect(70, 85, 61, 30, outline=0x0, stroke=3)
63splash.append(rect2)
64
65roundrect = RoundRect(10, 10, 61, 51, 10, fill=0x666666, outline=0x000000, stroke=6)
66splash.append(roundrect)
67
68display.refresh()
69while True:
70 pass
Sparkline Simple Test¶
Simple test with Sparklines
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# class of sparklines in CircuitPython
5# created by Kevin Matocha - Copyright 2020 (C)
6
7# See the bottom for a code example using the `sparkline` Class.
8
9# # File: display_shapes_sparkline.py
10# A sparkline is a scrolling line graph, where any values added to sparkline using
11# `add_value` are plotted.
12#
13# The `sparkline` class creates an element suitable for adding to the display using
14# `display.root_group = mySparkline`
15# or adding to a `displayio.Group` to be displayed.
16#
17# When creating the sparkline, identify the number of `max_items` that will be
18# included in the graph.
19# When additional elements are added to the sparkline and the number of items has
20# exceeded max_items, any excess values are removed from the left of the graph,
21# and new values are added to the right.
22
23
24# The following is an example that shows the
25
26# setup display
27# instance sparklines
28# add to the display
29# Loop the following steps:
30# add new values to sparkline `add_value`
31# update the sparklines `update`
32
33import time
34import random
35import board
36import displayio
37
38
39from adafruit_display_shapes.sparkline import Sparkline
40
41if "DISPLAY" not in dir(board):
42 # Setup the LCD display with driver
43 # You may need to change this to match the display driver for the chipset
44 # used on your display
45 from adafruit_ili9341 import ILI9341
46
47 displayio.release_displays()
48
49 # setup the SPI bus
50 spi = board.SPI()
51 tft_cs = board.D9 # arbitrary, pin not used
52 tft_dc = board.D10
53 tft_backlight = board.D12
54 tft_reset = board.D11
55
56 while not spi.try_lock():
57 spi.configure(baudrate=32000000)
58
59 spi.unlock()
60
61 display_bus = displayio.FourWire(
62 spi,
63 command=tft_dc,
64 chip_select=tft_cs,
65 reset=tft_reset,
66 baudrate=32000000,
67 polarity=1,
68 phase=1,
69 )
70
71 print("spi.frequency: {}".format(spi.frequency))
72
73 # Number of pixels in the display
74 DISPLAY_WIDTH = 320
75 DISPLAY_HEIGHT = 240
76
77 # create the display
78 display = ILI9341(
79 display_bus,
80 width=DISPLAY_WIDTH,
81 height=DISPLAY_HEIGHT,
82 rotation=180, # The rotation can be adjusted to match your configuration.
83 auto_refresh=True,
84 native_frames_per_second=90,
85 )
86
87 # reset the display to show nothing.
88 display.root_group = None
89else:
90 # built-in display
91 display = board.DISPLAY
92
93##########################################
94# Create background bitmaps and sparklines
95##########################################
96
97# Baseline size of the sparkline chart, in pixels.
98chart_width = display.width
99chart_height = display.height
100
101# sparkline1 uses a vertical y range between 0 to 10 and will contain a
102# maximum of 40 items
103sparkline1 = Sparkline(
104 width=chart_width, height=chart_height, max_items=40, y_min=0, y_max=10, x=0, y=0
105)
106
107# Create a group to hold the sparkline and append the sparkline into the
108# group (my_group)
109#
110# Note: In cases where display elements will overlap, then the order the elements
111# are added to the group will set which is on top. Latter elements are displayed
112# on top of former elements.
113my_group = displayio.Group()
114
115# add the sparkline into my_group
116my_group.append(sparkline1)
117
118
119# Add my_group (containing the sparkline) to the display
120display.root_group = my_group
121
122# Start the main loop
123while True:
124 # turn off the auto_refresh of the display while modifying the sparkline
125 display.auto_refresh = False
126
127 # add_value: add a new value to a sparkline
128 # Note: The y-range for mySparkline1 is set to 0 to 10, so all these random
129 # values (between 0 and 10) will fit within the visible range of this sparkline
130 sparkline1.add_value(random.uniform(0, 10))
131
132 # turn the display auto_refresh back on
133 display.auto_refresh = True
134
135 # The display seems to be less jittery if a small sleep time is provided
136 # You can adjust this to see if it has any effect
137 time.sleep(0.01)
Sparkline Ticks Example¶
Example using tick with the Sparkline class
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# class of sparklines in CircuitPython
5# created by Kevin Matocha - Copyright 2020 (C)
6
7# See the bottom for a code example using the `sparkline` Class.
8
9# # File: display_shapes_sparkline.py
10# A sparkline is a scrolling line graph, where any values added to sparkline
11# using `add_value` are plotted.
12#
13# The `sparkline` class creates an element suitable for adding to the display
14# using `display.root_group = mySparkline` or adding to a `displayio.Group` to be displayed.
15#
16# When creating the sparkline, identify the number of `max_items` that will be
17# included in the graph.
18# When additional elements are added to the sparkline and the number of items
19# has exceeded max_items, any excess values are removed from the left of the
20# graph, and new values are added to the right.
21
22
23# The following is an example that shows the
24
25# setup display
26# instance sparklines
27# add to the display
28# Loop the following steps:
29# add new values to sparkline `add_value`
30# update the sparklines `update`
31
32import random
33import time
34import board
35import displayio
36import terminalio
37from adafruit_display_text import label
38from adafruit_display_shapes.sparkline import Sparkline
39from adafruit_display_shapes.line import Line
40from adafruit_display_shapes.rect import Rect
41
42
43if "DISPLAY" not in dir(board):
44 # Setup the LCD display with driver
45 # You may need to change this to match the display driver for the chipset
46 # used on your display
47 from adafruit_ili9341 import ILI9341
48
49 displayio.release_displays()
50
51 # setup the SPI bus
52 spi = board.SPI()
53 tft_cs = board.D9 # arbitrary, pin not used
54 tft_dc = board.D10
55 tft_backlight = board.D12
56 tft_reset = board.D11
57
58 while not spi.try_lock():
59 spi.configure(baudrate=32000000)
60 spi.unlock()
61
62 display_bus = displayio.FourWire(
63 spi,
64 command=tft_dc,
65 chip_select=tft_cs,
66 reset=tft_reset,
67 baudrate=32000000,
68 polarity=1,
69 phase=1,
70 )
71
72 print("spi.frequency: {}".format(spi.frequency))
73
74 # Number of pixels in the display
75 DISPLAY_WIDTH = 320
76 DISPLAY_HEIGHT = 240
77
78 # create the display
79 display = ILI9341(
80 display_bus,
81 width=DISPLAY_WIDTH,
82 height=DISPLAY_HEIGHT,
83 rotation=180, # The rotation can be adjusted to match your configuration.
84 auto_refresh=True,
85 native_frames_per_second=90,
86 )
87
88 # reset the display to show nothing.
89 display.root_group = None
90else:
91 # built-in display
92 display = board.DISPLAY
93
94##########################################
95# Create background bitmaps and sparklines
96##########################################
97
98# Baseline size of the sparkline chart, in pixels.
99chart_width = display.width - 50
100chart_height = display.height - 50
101
102font = terminalio.FONT
103
104line_color = 0xFFFFFF
105
106# Setup the first bitmap and sparkline
107# This sparkline has no background bitmap
108# mySparkline1 uses a vertical y range between 0 to 10 and will contain a
109# maximum of 40 items
110sparkline1 = Sparkline(
111 width=chart_width,
112 height=chart_height,
113 max_items=40,
114 y_min=0,
115 y_max=10,
116 x=40,
117 y=30,
118 color=line_color,
119)
120
121# Label the y-axis range
122
123text_xoffset = -10
124text_label1a = label.Label(
125 font=font, text=str(sparkline1.y_top), color=line_color
126) # yTop label
127text_label1a.anchor_point = (1, 0.5) # set the anchorpoint at right-center
128text_label1a.anchored_position = (
129 sparkline1.x + text_xoffset,
130 sparkline1.y,
131) # set the text anchored position to the upper right of the graph
132
133text_label1b = label.Label(
134 font=font, text=str(sparkline1.y_bottom), color=line_color
135) # yTop label
136text_label1b.anchor_point = (1, 0.5) # set the anchorpoint at right-center
137text_label1b.anchored_position = (
138 sparkline1.x + text_xoffset,
139 sparkline1.y + chart_height,
140) # set the text anchored position to the upper right of the graph
141
142
143bounding_rectangle = Rect(
144 sparkline1.x, sparkline1.y, chart_width, chart_height, outline=line_color
145)
146
147
148# Create a group to hold the sparkline, text, rectangle and tickmarks
149# append them into the group (my_group)
150#
151# Note: In cases where display elements will overlap, then the order the
152# elements are added to the group will set which is on top. Latter elements
153# are displayed on top of former elemtns.
154
155my_group = displayio.Group()
156
157my_group.append(sparkline1)
158my_group.append(text_label1a)
159my_group.append(text_label1b)
160my_group.append(bounding_rectangle)
161
162total_ticks = 10
163
164for i in range(total_ticks + 1):
165 x_start = sparkline1.x - 5
166 x_end = sparkline1.x
167 y_both = int(round(sparkline1.y + (i * (chart_height) / (total_ticks))))
168 if y_both > sparkline1.y + chart_height - 1:
169 y_both = sparkline1.y + chart_height - 1
170 my_group.append(Line(x_start, y_both, x_end, y_both, color=line_color))
171
172
173# Set the display to show my_group that contains the sparkline and other graphics
174display.root_group = my_group
175
176# Start the main loop
177while True:
178 # Turn off auto_refresh to prevent partial updates of the screen during updates
179 # of the sparkline drawing
180 display.auto_refresh = False
181
182 # add_value: add a new value to a sparkline
183 # Note: The y-range for mySparkline1 is set to 0 to 10, so all these random
184 # values (between 0 and 10) will fit within the visible range of this sparkline
185 sparkline1.add_value(random.uniform(0, 10))
186
187 # Turn on auto_refresh for the display
188 display.auto_refresh = True
189
190 # The display seems to be less jittery if a small sleep time is provided
191 # You can adjust this to see if it has any effect
192 time.sleep(0.01)
Sparkline Triple Test¶
reate background bitmaps and sparklines
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# class of sparklines in CircuitPython
5# created by Kevin Matocha - Copyright 2020 (C)
6
7# See the bottom for a code example using the `sparkline` Class.
8
9# # File: display_shapes_sparkline.py
10# A sparkline is a scrolling line graph, where any values added to sparkline
11# using `add_value` are plotted.
12#
13# The `sparkline` class creates an element suitable for adding to the display
14# using `display.root_group = mySparkline` or adding to a `displayio.Group` to be displayed.
15#
16# When creating the sparkline, identify the number of `max_items` that will be
17# included in the graph.
18# When additional elements are added to the sparkline and the number of items
19# has exceeded max_items, any excess values are removed from the left of the
20# graph, and new values are added to the right.
21
22
23# The following is an example that shows the
24
25# setup display
26# instance sparklines
27# add to the display
28# Loop the following steps:
29# add new values to sparkline `add_value`
30# update the sparklines `update`
31
32import random
33import time
34import board
35import displayio
36import terminalio
37from adafruit_display_text import label
38from adafruit_display_shapes.sparkline import Sparkline
39
40
41if "DISPLAY" not in dir(board):
42 # Setup the LCD display with driver
43 # You may need to change this to match the display driver for the chipset
44 # used on your display
45 from adafruit_ili9341 import ILI9341
46
47 displayio.release_displays()
48
49 # setup the SPI bus
50 spi = board.SPI()
51 tft_cs = board.D9 # arbitrary, pin not used
52 tft_dc = board.D10
53 tft_backlight = board.D12
54 tft_reset = board.D11
55
56 while not spi.try_lock():
57 spi.configure(baudrate=32000000)
58 spi.unlock()
59
60 display_bus = displayio.FourWire(
61 spi,
62 command=tft_dc,
63 chip_select=tft_cs,
64 reset=tft_reset,
65 baudrate=32000000,
66 polarity=1,
67 phase=1,
68 )
69
70 print("spi.frequency: {}".format(spi.frequency))
71
72 # Number of pixels in the display
73 DISPLAY_WIDTH = 320
74 DISPLAY_HEIGHT = 240
75
76 # create the display
77 display = ILI9341(
78 display_bus,
79 width=DISPLAY_WIDTH,
80 height=DISPLAY_HEIGHT,
81 rotation=180, # The rotation can be adjusted to match your configuration.
82 auto_refresh=True,
83 native_frames_per_second=90,
84 )
85
86 # reset the display to show nothing.
87 display.root_group = None
88else:
89 # built-in display
90 display = board.DISPLAY
91 DISPLAY_WIDTH = board.DISPLAY.width
92
93##########################################
94# Create background bitmaps and sparklines
95##########################################
96
97# Baseline size of the sparkline chart, in pixels.
98chart_width = 50
99chart_height = 50
100
101font = terminalio.FONT
102
103# Setup the first bitmap and sparkline
104# This sparkline has no background bitmap
105# sparkline1 uses a vertical y range between -1 to +1.25 and will contain a maximum of 40 items
106sparkline1 = Sparkline(
107 width=chart_width,
108 height=chart_height,
109 max_items=40,
110 y_min=-1,
111 y_max=1.25,
112 x=10,
113 y=10,
114)
115
116# Label the y-axis range
117text_label1a = label.Label(
118 font=font, text=str(sparkline1.y_top), color=0xFFFFFF
119) # y_top label
120text_label1a.anchor_point = (0, 0.5) # set the anchorpoint
121text_label1a.anchored_position = (
122 10 + chart_width,
123 10,
124) # set the text anchored position to the upper right of the graph
125
126text_label1b = label.Label(
127 font=font, text=str(sparkline1.y_bottom), color=0xFFFFFF
128) # y_bottom label
129text_label1b.anchor_point = (0, 0.5) # set the anchorpoint
130text_label1b.anchored_position = (
131 10 + chart_width,
132 10 + chart_height,
133) # set the text anchored position to the upper right of the graph
134
135
136# Setup the second bitmap and sparkline
137# sparkline2 uses a vertical y range between 0 to 1, and will contain a
138# maximum of 10 items
139#
140palette2 = displayio.Palette(1) # color palette used for bitmap2 (one color)
141palette2[0] = 0x0000FF
142
143bitmap2 = displayio.Bitmap(chart_width * 2, chart_height * 2, 1) # create bitmap2
144tilegrid2 = displayio.TileGrid(
145 bitmap2, pixel_shader=palette2, x=150, y=10
146) # Add bitmap2 to tilegrid2
147sparkline2 = Sparkline(
148 width=chart_width * 2,
149 height=chart_height * 2,
150 max_items=10,
151 y_min=0,
152 y_max=1,
153 x=150,
154 y=10,
155 color=0xFF00FF,
156)
157
158
159# Setup the third bitmap and third sparkline
160# sparkline3 contains a maximum of 10 items
161# since y_min and y_max are not specified, sparkline3 uses autoranging for both
162# the top and bottom of the y-axis.
163# Note1: Any unspecified edge limit (y_min or y_max) will autorange that edge based
164# on the data in the list.
165# Note2: You can read back the current value of the y-axis limits by using
166# sparkline3.y_bottom or sparkline3.y_top
167
168
169palette3 = displayio.Palette(1) # color palette used for bitmap (one color)
170palette3[0] = 0x11FF44
171bitmap3 = displayio.Bitmap(DISPLAY_WIDTH - 30, chart_height * 2, 1) # create bitmap3
172tilegrid3 = displayio.TileGrid(
173 bitmap3, pixel_shader=palette3, x=0, y=120
174) # Add bitmap3 to tilegrid3
175
176sparkline3 = Sparkline(
177 width=DISPLAY_WIDTH - 30,
178 height=chart_height * 2,
179 max_items=10,
180 x=0,
181 y=120,
182 color=0xFFFFFF,
183)
184
185# Initialize the y-axis labels for mySparkline3 with no text
186text_label3a = label.Label(font=font, text="", color=0x11FF44) # y_top label
187text_label3a.anchor_point = (0, 0.5) # set the anchorpoint
188text_label3a.anchored_position = (
189 sparkline3.width,
190 120,
191) # set the text anchored position to the upper right of the graph
192
193text_label3b = label.Label(font=font, text="", color=0x11FF44) # y_bottom label
194text_label3b.anchor_point = (0, 0.5) # set the anchorpoint
195text_label3b.anchored_position = (
196 sparkline3.width,
197 120 + sparkline3.height,
198) # set the text anchored position to the upper right of the graph
199
200# Create a group to hold the three bitmap TileGrids and the three sparklines and
201# append them into the group (my_group)
202#
203# Note: In cases where display elements will overlap, then the order the elements
204# are added to the group will set which is on top. Latter elements are displayed
205# on top of former elements.
206my_group = displayio.Group()
207
208my_group.append(sparkline1)
209my_group.append(text_label1a)
210my_group.append(text_label1b)
211
212my_group.append(tilegrid2)
213my_group.append(sparkline2)
214
215my_group.append(tilegrid3)
216my_group.append(sparkline3)
217my_group.append(text_label3a)
218my_group.append(text_label3b)
219
220# Set the display to show my_group that contains all the bitmap TileGrids and
221# sparklines
222display.root_group = my_group
223
224i = 0 # This is a counter for changing the random values for mySparkline3
225
226# Start the main loop
227while True:
228 # Turn off auto_refresh to prevent partial updates of the screen during updates
229 # of the sparklines
230 display.auto_refresh = False
231
232 # add_value: add a new value to a sparkline
233 # Note: The y-range for sparkline1 is set to -1 to 1.25, so all these random
234 # values (between 0 and 1) will fit within the visible range of this sparkline
235 sparkline1.add_value(random.uniform(0, 1))
236
237 # Note: For sparkline2, the y-axis range is set from 0 to 1.
238 # With the random values set between -1 and +2, the values will sometimes
239 # be out of the y-range. This example shows how the fixed y-range (0 to 1)
240 # will "clip" values (it will not display them) that are above or below the
241 # y-range.
242 sparkline2.add_value(random.uniform(-1, 2))
243
244 # sparkline3 is set autoranging for both the top and bottom of the Y-axis
245
246 # In this example, for 15 values, this adds points in the range from 0 to 1.
247 # Then, for the next 15 values, it adds points in the range of 0 to 10.
248 # This is to highlight the autoranging of the y-axis.
249 # Notice how the y-axis labels show that the y-scale is changing.
250 #
251 # An exercise for the reader: You can set only one or the other sparkline axis
252 # to autoranging by setting its value to None.
253 if i < 15:
254 sparkline3.add_value(random.uniform(0, 1))
255 else:
256 sparkline3.add_value(random.uniform(0, 10))
257 text_label3a.text = str(sparkline3.y_top)
258 text_label3b.text = str(sparkline3.y_bottom)
259 i += 1 # increment the counter
260 if i > 30: # After 30 times through the loop, reset the counter
261 i = 0
262
263 # Turn on auto_refresh for the display
264 display.auto_refresh = True
265
266 # The display seems to be less jittery if a small sleep time is provided
267 # You can adjust this to see if it has any effect
268 time.sleep(0.01)
Circle Animation¶
Example showing the features of the new Circle setter
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4"""
5This is an animation to demonstrate the use of Circle Setter Attribute.
6"""
7
8import time
9import gc
10import board
11import displayio
12from adafruit_display_shapes.circle import Circle
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# Make the display context
20main_group = displayio.Group()
21
22# Make a background color fill
23color_bitmap = displayio.Bitmap(display.width, display.height, 1)
24color_palette = displayio.Palette(1)
25color_palette[0] = 0xFFFFFF
26bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
27main_group.append(bg_sprite)
28
29# Setting up the Circle starting position
30posx = 50
31posy = 50
32
33# Define Circle characteristics
34circle_radius = 20
35circle = Circle(posx, posy, circle_radius, fill=0x00FF00, outline=0xFF00FF)
36main_group.append(circle)
37
38# Define Circle Animation Steps
39delta_x = 2
40delta_y = 2
41
42# Showing the items on the screen
43display.root_group = main_group
44
45while True:
46 if circle.y + circle_radius >= display.height - circle_radius:
47 delta_y = -1
48 if circle.x + circle_radius >= display.width - circle_radius:
49 delta_x = -1
50 if circle.x - circle_radius <= 0 - circle_radius:
51 delta_x = 1
52 if circle.y - circle_radius <= 0 - circle_radius:
53 delta_y = 1
54
55 circle.x = circle.x + delta_x
56 circle.y = circle.y + delta_y
57
58 time.sleep(0.02)
59 gc.collect()
Arc Simple Test¶
Example demonstrating various arcs.
1# SPDX-FileCopyrightText: 2023 Bernhard Bablok
2# SPDX-License-Identifier: MIT
3
4import time
5import board
6
7import displayio
8from adafruit_display_shapes.arc import Arc
9from adafruit_display_shapes.circle import Circle
10
11# use built in display (PyPortal, PyGamer, PyBadge, CLUE, etc.)
12# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
13# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
14display = board.DISPLAY
15
16w2 = int(display.width / 2)
17h2 = int(display.height / 2)
18
19WHITE = 0xFFFFFF
20RED = 0xFF0000
21GREEN = 0x00FF00
22BLUE = 0x0000FF
23
24# Make the display context
25group = displayio.Group()
26display.root_group = group
27
28# little circle in the center of all arcs
29circle = Circle(w2, h2, 5, fill=0xFF0000, outline=0xFF0000)
30group.append(circle)
31
32# red arc with white outline, 10 pixels wide
33arc1 = Arc(
34 x=w2,
35 y=h2,
36 radius=min(display.width, display.height) / 4,
37 angle=90,
38 direction=90,
39 segments=10,
40 arc_width=10,
41 outline=WHITE,
42 fill=RED,
43)
44group.append(arc1)
45
46# green arc (single line)
47arc2 = Arc(
48 x=w2,
49 y=h2,
50 radius=min(display.width, display.height) / 4 + 5,
51 angle=180,
52 direction=90,
53 segments=20,
54 arc_width=1,
55 outline=GREEN,
56)
57group.append(arc2)
58
59# blue arc (or pie)
60arc3 = Arc(
61 x=w2,
62 y=h2,
63 radius=min(display.width, display.height) / 4,
64 angle=90,
65 direction=-90,
66 segments=10,
67 arc_width=min(display.width, display.height) / 4 - 5,
68 outline=BLUE,
69)
70group.append(arc3)
71
72while True:
73 time.sleep(0.1)