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 time
5from random import randrange
6import displayio
7from adafruit_gizmo import tft_gizmo
8
9# Create the TFT Gizmo display
10display = tft_gizmo.TFT_Gizmo()
11
12# You can now use the display to do whatever you want
13# Here we show how to draw random pixels
14
15# Create a bitmap with two colors
16bitmap = displayio.Bitmap(display.width, display.height, 2)
17
18# Create a two color palette
19palette = displayio.Palette(2)
20palette[0] = 0x000000
21palette[1] = 0xFFFFFF
22
23# Create a TileGrid using the Bitmap and Palette
24tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette)
25
26# Create a Group
27group = displayio.Group()
28
29# Add the TileGrid to the Group
30group.append(tile_grid)
31
32# Add the Group to the Display
33display.root_group = group
34
35# Draw pixels
36while True:
37 for _ in range(200):
38 x = randrange(0, display.width)
39 y = randrange(0, display.height)
40 bitmap[x, y] = 1
41 time.sleep(0.1)
42 for i in range(display.width * display.height):
43 bitmap[i] = 0
Other examples¶
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4"""
5This test will initialize the display using displayio and draw a solid green
6background, a smaller purple rectangle, and some yellow text.
7"""
8import displayio
9import terminalio
10from adafruit_display_text import label
11from adafruit_gizmo import tft_gizmo
12
13# Create the TFT Gizmo display
14display = tft_gizmo.TFT_Gizmo()
15
16# Make the display context
17splash = displayio.Group()
18display.root_group = splash
19
20color_bitmap = displayio.Bitmap(240, 240, 1)
21color_palette = displayio.Palette(1)
22color_palette[0] = 0x00FF00 # Bright Green
23
24bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
25splash.append(bg_sprite)
26
27# Draw a smaller inner rectangle
28inner_bitmap = displayio.Bitmap(200, 200, 1)
29inner_palette = displayio.Palette(1)
30inner_palette[0] = 0xAA0088 # Purple
31inner_sprite = displayio.TileGrid(inner_bitmap, pixel_shader=inner_palette, x=20, y=20)
32splash.append(inner_sprite)
33
34# Draw a label
35text_group = displayio.Group(scale=2, x=50, y=120)
36text = "Hello World!"
37text_area = label.Label(terminalio.FONT, text=text, color=0xFFFF00)
38text_group.append(text_area) # Subgroup for text scaling
39splash.append(text_group)
40
41while True:
42 pass
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4"""
5This example will initialize the display using displayio and draw a bmp image
6background, and overlay text containing the value read from the on-board temperature sensor.
7User may press the A button to switch between celsius and fahrenheit units.
8
9Required libraries:
10* Adafruit_CircuitPython_Gizmo
11* Adafruit_CircuitPython_ST7789
12* Adafruit_CircuitPython_Display_Text
13* Adafruit_CircuitPython_CircuitPlayground
14"""
15import time
16from adafruit_circuitplayground import cp
17import displayio
18import terminalio
19from adafruit_display_text import label
20from adafruit_gizmo import tft_gizmo
21
22display = tft_gizmo.TFT_Gizmo()
23
24
25# text scaling factor
26TEXT_SCALE = 2
27
28# previous iteration button value
29old_a_val = cp.button_a
30
31# boolean for current unit type
32show_c_units = True
33
34
35# function to convert celsius degrees to fahrenheit
36def c_to_f(c_val):
37 return (c_val * 9 / 5) + 32
38
39
40# Open the background image file
41with open("/thermometer_background.bmp", "rb") as bitmap_file:
42 # Setup the file as the bitmap data source
43 bitmap = displayio.OnDiskBitmap(bitmap_file)
44
45 # Create a TileGrid to hold the bitmap
46 # CircuitPython 6 & 7 compatible
47 tile_grid = displayio.TileGrid(
48 bitmap,
49 pixel_shader=getattr(bitmap, "pixel_shader", displayio.ColorConverter()),
50 )
51 # CircuitPython 7 compatible only
52 # tile_grid = displayio.TileGrid(bitmap, pixel_shader=bitmap.pixel_shader)
53
54 # Create a Group to hold the TileGrid
55 group = displayio.Group()
56
57 # Add the TileGrid to the Group
58 group.append(tile_grid)
59
60 # variable with initial text value, temperature rounded to 2 places
61 text = "%.2f C" % (round(cp.temperature, 2))
62
63 # Create a Group for the text so we can scale it
64 text_group = displayio.Group(scale=TEXT_SCALE, x=0, y=0)
65
66 # Create a Label to show the initial temperature value
67 text_area = label.Label(terminalio.FONT, text=text, color=0xFFFFFF)
68
69 # Set the anchor_point for center,top
70 text_area.anchor_point = (0.5, 0.0)
71
72 # Set the location to center of display, accounting for text_scale
73 text_area.anchored_position = (240 / (2 * TEXT_SCALE), 240 / (2 * TEXT_SCALE))
74
75 # Subgroup for text scaling
76 text_group.append(text_area)
77
78 # Add the text_group to main Group
79 group.append(text_group)
80
81 # Add the main Group to the Display
82 display.root_group = group
83
84 # Loop forever
85 while True:
86 # set current button state to variable
87 cur_a_val = cp.button_a
88 if cur_a_val and not old_a_val: # if the button was released
89 print("Just released")
90 # flip the units boolean to the opposite value
91 show_c_units = not show_c_units
92
93 if show_c_units:
94 # Update the text
95 text_area.text = "%.2f C" % (round(cp.temperature, 2))
96 else: # show f units
97 # Update the text
98 text_area.text = "%.2f F" % (round(c_to_f(cp.temperature), 2))
99
100 # set previous button value for next time
101 old_a_val = cur_a_val
102 # Wait a little bit
103 time.sleep(0.2)
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5import displayio
6from adafruit_gizmo import eink_gizmo
7
8display = eink_gizmo.EInk_Gizmo()
9# Use the below line instead for the 200x200 E-Ink Gizmo
10# display = eink_gizmo.EInk_HD_Gizmo()
11
12# Create a display group for our screen objects
13display_group = displayio.Group()
14
15# Display a ruler graphic from the root directory of the CIRCUITPY drive
16with open("/display-ruler.bmp", "rb") as file:
17 picture = displayio.OnDiskBitmap(file)
18 # Create a Tilegrid with the bitmap and put in the displayio group
19 # CircuitPython 6 & 7 compatible
20 sprite = displayio.TileGrid(
21 picture,
22 pixel_shader=getattr(picture, "pixel_shader", displayio.ColorConverter()),
23 )
24 # CircuitPython 7 compatible only
25 # sprite = displayio.TileGrid(picture, pixel_shader=bitmap.pixel_shader)
26 display_group.append(sprite)
27
28 # Place the display group on the screen
29 display.root_group = display_group
30
31 # Refresh the display to have it actually show the image
32 # NOTE: Do not refresh eInk displays sooner than 180 seconds
33 display.refresh()
34 print("refreshed")
35
36 time.sleep(180)