Simple test

Ensure your device works with this simple test.

examples/display_shapes_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import board
 5import displayio
 6
 7from adafruit_display_shapes.circle import Circle
 8from adafruit_display_shapes.line import Line
 9from adafruit_display_shapes.polygon import Polygon
10from adafruit_display_shapes.rect import Rect
11from adafruit_display_shapes.roundrect import RoundRect
12from adafruit_display_shapes.triangle import Triangle
13
14# Make the display context
15splash = displayio.Group()
16board.DISPLAY.root_group = splash
17
18# Make a background color fill
19color_bitmap = displayio.Bitmap(320, 240, 1)
20color_palette = displayio.Palette(1)
21color_palette[0] = 0xFFFFFF
22bg_sprite = displayio.TileGrid(color_bitmap, x=0, y=0, pixel_shader=color_palette)
23splash.append(bg_sprite)
24##########################################################################
25
26splash.append(Line(220, 130, 270, 210, 0xFF0000))
27splash.append(Line(270, 210, 220, 210, 0xFF0000))
28splash.append(Line(220, 210, 270, 130, 0xFF0000))
29splash.append(Line(270, 130, 220, 130, 0xFF0000))
30
31# Draw a blue star
32polygon = Polygon(
33    [
34        (255, 40),
35        (262, 62),
36        (285, 62),
37        (265, 76),
38        (275, 100),
39        (255, 84),
40        (235, 100),
41        (245, 76),
42        (225, 62),
43        (248, 62),
44    ],
45    outline=0x0000FF,
46)
47splash.append(polygon)
48
49triangle = Triangle(170, 50, 120, 140, 210, 160, fill=0x00FF00, outline=0xFF00FF)
50splash.append(triangle)
51
52rect = Rect(80, 20, 41, 41, fill=0x0)
53splash.append(rect)
54
55circle = Circle(100, 100, 20, fill=0x00FF00, outline=0xFF00FF)
56splash.append(circle)
57
58rect2 = Rect(50, 100, 61, 81, outline=0x0, stroke=3)
59splash.append(rect2)
60
61roundrect = RoundRect(10, 10, 61, 81, 10, fill=0x0, outline=0xFF00FF, stroke=6)
62splash.append(roundrect)
63
64
65while True:
66    pass

Simple test MagTag

Simple test with the MagTag.

examples/display_shapes_simpletest_magtag.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import board
 5import displayio
 6
 7from adafruit_display_shapes.circle import Circle
 8from adafruit_display_shapes.line import Line
 9from adafruit_display_shapes.polygon import Polygon
10from adafruit_display_shapes.rect import Rect
11from adafruit_display_shapes.roundrect import RoundRect
12from adafruit_display_shapes.triangle import Triangle
13
14# use built in display (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
20splash = displayio.Group()
21display.root_group = splash
22
23# Make a background color fill
24color_bitmap = displayio.Bitmap(display.width, display.height, 1)
25color_palette = displayio.Palette(1)
26color_palette[0] = 0xFFFFFF
27bg_sprite = displayio.TileGrid(color_bitmap, x=0, y=0, pixel_shader=color_palette)
28splash.append(bg_sprite)
29##########################################################################
30
31splash.append(Line(5, 74, 10, 110, 0x000000))
32splash.append(Line(15, 74, 20, 110, 0x000000))
33splash.append(Line(25, 74, 30, 110, 0x000000))
34splash.append(Line(35, 74, 40, 110, 0x000000))
35
36# Draw star
37polygon = Polygon(
38    [
39        (255, 40),
40        (262, 62),
41        (285, 62),
42        (265, 76),
43        (275, 100),
44        (255, 84),
45        (235, 100),
46        (245, 76),
47        (225, 62),
48        (248, 62),
49    ],
50    outline=0x000000,
51)
52splash.append(polygon)
53
54triangle = Triangle(170, 20, 140, 90, 210, 100, fill=0x999999, outline=0x000000)
55splash.append(triangle)
56
57rect = Rect(80, 20, 41, 41, fill=0x999999, outline=0x666666)
58splash.append(rect)
59
60circle = Circle(100, 100, 20, fill=0xFFFFFF, outline=0x000000)
61splash.append(circle)
62
63rect2 = Rect(70, 85, 61, 30, outline=0x0, stroke=3)
64splash.append(rect2)
65
66roundrect = RoundRect(10, 10, 61, 51, 10, fill=0x666666, outline=0x000000, stroke=6)
67splash.append(roundrect)
68
69display.refresh()
70while True:
71    pass

Sparkline Simple Test

Simple test with Sparklines

examples/display_shapes_sparkline_simpletest.py
  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 random
 34import time
 35
 36import board
 37import displayio
 38import fourwire
 39
 40from adafruit_display_shapes.sparkline import Sparkline
 41
 42if "DISPLAY" not in dir(board):
 43    # Setup the LCD display with driver
 44    # You may need to change this to match the display driver for the chipset
 45    # used on your display
 46    from adafruit_ili9341 import ILI9341
 47
 48    displayio.release_displays()
 49
 50    # setup the SPI bus
 51    spi = board.SPI()
 52    tft_cs = board.D9  # arbitrary, pin not used
 53    tft_dc = board.D10
 54    tft_backlight = board.D12
 55    tft_reset = board.D11
 56
 57    while not spi.try_lock():
 58        spi.configure(baudrate=32000000)
 59
 60    spi.unlock()
 61
 62    display_bus = fourwire.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(f"spi.frequency: {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
100chart_height = display.height
101
102# sparkline1 uses a vertical y range between 0 to 10 and will contain a
103# maximum of 40 items
104sparkline1 = Sparkline(
105    width=chart_width, height=chart_height, max_items=40, y_min=0, y_max=10, x=0, y=0
106)
107
108# Create a group to hold the sparkline and append the sparkline into the
109# group (my_group)
110#
111# Note: In cases where display elements will overlap, then the order the elements
112# are added to the group will set which is on top.  Latter elements are displayed
113# on top of former elements.
114my_group = displayio.Group()
115
116# add the sparkline into my_group
117my_group.append(sparkline1)
118
119
120# Add my_group (containing the sparkline) to the display
121display.root_group = my_group
122
123# Start the main loop
124while True:
125    # turn off the auto_refresh of the display while modifying the sparkline
126    display.auto_refresh = False
127
128    # add_value: add a new value to a sparkline
129    # Note: The y-range for mySparkline1 is set to 0 to 10, so all these random
130    # values (between 0 and 10) will fit within the visible range of this sparkline
131    sparkline1.add_value(random.uniform(0, 10))
132
133    # turn the display auto_refresh back on
134    display.auto_refresh = True
135
136    # The display seems to be less jittery if a small sleep time is provided
137    # You can adjust this to see if it has any effect
138    time.sleep(0.01)

Sparkline Ticks Example

Example using tick with the Sparkline class

examples/display_shapes_sparkline_ticks.py
  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
 34
 35import board
 36import displayio
 37import fourwire
 38import terminalio
 39from adafruit_display_text import label
 40
 41from adafruit_display_shapes.line import Line
 42from adafruit_display_shapes.rect import Rect
 43from adafruit_display_shapes.sparkline import Sparkline
 44
 45if "DISPLAY" not in dir(board):
 46    # Setup the LCD display with driver
 47    # You may need to change this to match the display driver for the chipset
 48    # used on your display
 49    from adafruit_ili9341 import ILI9341
 50
 51    displayio.release_displays()
 52
 53    # setup the SPI bus
 54    spi = board.SPI()
 55    tft_cs = board.D9  # arbitrary, pin not used
 56    tft_dc = board.D10
 57    tft_backlight = board.D12
 58    tft_reset = board.D11
 59
 60    while not spi.try_lock():
 61        spi.configure(baudrate=32000000)
 62    spi.unlock()
 63
 64    display_bus = fourwire.FourWire(
 65        spi,
 66        command=tft_dc,
 67        chip_select=tft_cs,
 68        reset=tft_reset,
 69        baudrate=32000000,
 70        polarity=1,
 71        phase=1,
 72    )
 73
 74    print(f"spi.frequency: {spi.frequency}")
 75
 76    # Number of pixels in the display
 77    DISPLAY_WIDTH = 320
 78    DISPLAY_HEIGHT = 240
 79
 80    # create the display
 81    display = ILI9341(
 82        display_bus,
 83        width=DISPLAY_WIDTH,
 84        height=DISPLAY_HEIGHT,
 85        rotation=180,  # The rotation can be adjusted to match your configuration.
 86        auto_refresh=True,
 87        native_frames_per_second=90,
 88    )
 89
 90    # reset the display to show nothing.
 91    display.root_group = None
 92else:
 93    # built-in display
 94    display = board.DISPLAY
 95
 96##########################################
 97# Create background bitmaps and sparklines
 98##########################################
 99
100# Baseline size of the sparkline chart, in pixels.
101chart_width = display.width - 50
102chart_height = display.height - 50
103
104font = terminalio.FONT
105
106line_color = 0xFFFFFF
107
108# Setup the first bitmap and sparkline
109# This sparkline has no background bitmap
110# mySparkline1 uses a vertical y range between 0 to 10 and will contain a
111# maximum of 40 items
112sparkline1 = Sparkline(
113    width=chart_width,
114    height=chart_height,
115    max_items=40,
116    y_min=0,
117    y_max=10,
118    x=40,
119    y=30,
120    color=line_color,
121)
122
123# Label the y-axis range
124
125text_xoffset = -10
126text_label1a = label.Label(font=font, text=str(sparkline1.y_top), color=line_color)  # 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(font=font, text=str(sparkline1.y_bottom), color=line_color)  # yTop label
134text_label1b.anchor_point = (1, 0.5)  # set the anchorpoint at right-center
135text_label1b.anchored_position = (
136    sparkline1.x + text_xoffset,
137    sparkline1.y + chart_height,
138)  # set the text anchored position to the upper right of the graph
139
140
141bounding_rectangle = Rect(sparkline1.x, sparkline1.y, chart_width, chart_height, outline=line_color)
142
143
144# Create a group to hold the sparkline, text, rectangle and tickmarks
145# append them into the group (my_group)
146#
147# Note: In cases where display elements will overlap, then the order the
148# elements are added to the group will set which is on top.  Latter elements
149# are displayed on top of former elemtns.
150
151my_group = displayio.Group()
152
153my_group.append(sparkline1)
154my_group.append(text_label1a)
155my_group.append(text_label1b)
156my_group.append(bounding_rectangle)
157
158total_ticks = 10
159
160for i in range(total_ticks + 1):
161    x_start = sparkline1.x - 5
162    x_end = sparkline1.x
163    y_both = int(round(sparkline1.y + (i * (chart_height) / (total_ticks))))
164    y_both = min(y_both, sparkline1.y + chart_height - 1)
165    my_group.append(Line(x_start, y_both, x_end, y_both, color=line_color))
166
167
168# Set the display to show my_group that contains the sparkline and other graphics
169display.root_group = my_group
170
171# Start the main loop
172while True:
173    # Turn off auto_refresh to prevent partial updates of the screen during updates
174    # of the sparkline drawing
175    display.auto_refresh = False
176
177    # add_value: add a new value to a sparkline
178    # Note: The y-range for mySparkline1 is set to 0 to 10, so all these random
179    # values (between 0 and 10) will fit within the visible range of this sparkline
180    sparkline1.add_value(random.uniform(0, 10))
181
182    # Turn on auto_refresh for the display
183    display.auto_refresh = True
184
185    # The display seems to be less jittery if a small sleep time is provided
186    # You can adjust this to see if it has any effect
187    time.sleep(0.01)

Sparkline Triple Test

reate background bitmaps and sparklines

examples/display_shapes_sparkline_triple.py
  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
 34
 35import board
 36import displayio
 37import fourwire
 38import terminalio
 39from adafruit_display_text import label
 40
 41from adafruit_display_shapes.sparkline import Sparkline
 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 = fourwire.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(f"spi.frequency: {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    DISPLAY_WIDTH = board.DISPLAY.width
 94
 95##########################################
 96# Create background bitmaps and sparklines
 97##########################################
 98
 99# Baseline size of the sparkline chart, in pixels.
100chart_width = 50
101chart_height = 50
102
103font = terminalio.FONT
104
105# Setup the first bitmap and sparkline
106# This sparkline has no background bitmap
107# sparkline1 uses a vertical y range between -1 to +1.25 and will contain a maximum of 40 items
108sparkline1 = Sparkline(
109    width=chart_width,
110    height=chart_height,
111    max_items=40,
112    y_min=-1,
113    y_max=1.25,
114    x=10,
115    y=10,
116)
117
118# Label the y-axis range
119text_label1a = label.Label(font=font, text=str(sparkline1.y_top), color=0xFFFFFF)  # 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

examples/display_shapes_circle_animation.py
 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 gc
 9import time
10
11import board
12import displayio
13
14from adafruit_display_shapes.circle import Circle
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
21# Make the display context
22main_group = displayio.Group()
23
24# Make a background color fill
25color_bitmap = displayio.Bitmap(display.width, display.height, 1)
26color_palette = displayio.Palette(1)
27color_palette[0] = 0xFFFFFF
28bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
29main_group.append(bg_sprite)
30
31# Setting up the Circle starting position
32posx = 50
33posy = 50
34
35# Define Circle characteristics
36circle_radius = 20
37circle = Circle(posx, posy, circle_radius, fill=0x00FF00, outline=0xFF00FF)
38main_group.append(circle)
39
40# Define Circle Animation Steps
41delta_x = 2
42delta_y = 2
43
44# Showing the items on the screen
45display.root_group = main_group
46
47while True:
48    if circle.y + circle_radius >= display.height - circle_radius:
49        delta_y = -1
50    if circle.x + circle_radius >= display.width - circle_radius:
51        delta_x = -1
52    if circle.x - circle_radius <= 0 - circle_radius:
53        delta_x = 1
54    if circle.y - circle_radius <= 0 - circle_radius:
55        delta_y = 1
56
57    circle.x += delta_x
58    circle.y += delta_y
59
60    time.sleep(0.02)
61    gc.collect()

Arc Simple Test

Example demonstrating various arcs.

examples/display_shapes_arc.py
 1# SPDX-FileCopyrightText: 2023 Bernhard Bablok
 2# SPDX-License-Identifier: MIT
 3
 4import time
 5
 6import board
 7import displayio
 8
 9from adafruit_display_shapes.arc import Arc
10from adafruit_display_shapes.circle import Circle
11
12# use built in display (PyPortal, PyGamer, PyBadge, CLUE, etc.)
13# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
14# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
15display = board.DISPLAY
16
17w2 = int(display.width / 2)
18h2 = int(display.height / 2)
19
20WHITE = 0xFFFFFF
21RED = 0xFF0000
22GREEN = 0x00FF00
23BLUE = 0x0000FF
24
25# Make the display context
26group = displayio.Group()
27display.root_group = group
28
29# little circle in the center of all arcs
30circle = Circle(w2, h2, 5, fill=0xFF0000, outline=0xFF0000)
31group.append(circle)
32
33# red arc with white outline, 10 pixels wide
34arc1 = Arc(
35    x=w2,
36    y=h2,
37    radius=min(display.width, display.height) / 4,
38    angle=90,
39    direction=90,
40    segments=10,
41    arc_width=10,
42    outline=WHITE,
43    fill=RED,
44)
45group.append(arc1)
46
47# green arc (single line)
48arc2 = Arc(
49    x=w2,
50    y=h2,
51    radius=min(display.width, display.height) / 4 + 5,
52    angle=180,
53    direction=90,
54    segments=20,
55    arc_width=1,
56    outline=GREEN,
57)
58group.append(arc2)
59
60# blue arc (or pie)
61arc3 = Arc(
62    x=w2,
63    y=h2,
64    radius=min(display.width, display.height) / 4,
65    angle=90,
66    direction=-90,
67    segments=10,
68    arc_width=min(display.width, display.height) / 4 - 5,
69    outline=BLUE,
70)
71group.append(arc3)
72
73while True:
74    time.sleep(0.1)

Filled Polygon Simple Test

Example demonstrating a filled polygon

examples/display_shapes_filled_polygon_simpletest.py
 1# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4import board
 5import displayio
 6
 7from adafruit_display_shapes.filled_polygon import FilledPolygon
 8
 9# Make the display context
10splash = displayio.Group()
11board.DISPLAY.root_group = splash
12
13# Make a background color fill
14color_bitmap = displayio.Bitmap(320, 240, 1)
15color_palette = displayio.Palette(1)
16color_palette[0] = 0xFFFFFF
17bg_sprite = displayio.TileGrid(color_bitmap, x=0, y=0, pixel_shader=color_palette)
18splash.append(bg_sprite)
19##########################################################################
20
21# Draw a star with blue outline and pink fill
22polygon = FilledPolygon(
23    [
24        (55, 40),
25        (62, 62),
26        (85, 62),
27        (65, 76),
28        (75, 100),
29        (55, 84),
30        (35, 100),
31        (45, 76),
32        (25, 62),
33        (48, 62),
34    ],
35    outline=0x0000FF,
36    stroke=4,
37    fill=0xFF00FF,
38)
39splash.append(polygon)
40
41while True:
42    pass