Simple test

Ensure your device works with this simple test.

examples/displayio_ssd1306_simpletest.py
 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 white
 6background, a smaller black rectangle, and some white text.
 7"""
 8
 9import board
10import displayio
11
12# Compatibility with both CircuitPython 8.x.x and 9.x.x.
13# Remove after 8.x.x is no longer a supported release.
14try:
15    from i2cdisplaybus import I2CDisplayBus
16
17    # from fourwire import FourWire
18except ImportError:
19    from displayio import I2CDisplay as I2CDisplayBus
20
21    # from displayio import FourWire
22
23import terminalio
24from adafruit_display_text import label
25import adafruit_displayio_ssd1306
26
27displayio.release_displays()
28
29oled_reset = board.D9
30
31# Use for I2C
32i2c = board.I2C()  # uses board.SCL and board.SDA
33# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
34display_bus = I2CDisplayBus(i2c, device_address=0x3C, reset=oled_reset)
35
36# Use for SPI
37# spi = board.SPI()
38# oled_cs = board.D5
39# oled_dc = board.D6
40# display_bus = FourWire(spi, command=oled_dc, chip_select=oled_cs,
41#                                 reset=oled_reset, baudrate=1000000)
42
43WIDTH = 128
44HEIGHT = 32  # Change to 64 if needed
45BORDER = 5
46
47display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=WIDTH, height=HEIGHT)
48
49# Make the display context
50splash = displayio.Group()
51display.root_group = splash
52
53color_bitmap = displayio.Bitmap(WIDTH, HEIGHT, 1)
54color_palette = displayio.Palette(1)
55color_palette[0] = 0xFFFFFF  # White
56
57bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
58splash.append(bg_sprite)
59
60# Draw a smaller inner rectangle
61inner_bitmap = displayio.Bitmap(WIDTH - BORDER * 2, HEIGHT - BORDER * 2, 1)
62inner_palette = displayio.Palette(1)
63inner_palette[0] = 0x000000  # Black
64inner_sprite = displayio.TileGrid(
65    inner_bitmap, pixel_shader=inner_palette, x=BORDER, y=BORDER
66)
67splash.append(inner_sprite)
68
69# Draw a label
70text = "Hello World!"
71text_area = label.Label(
72    terminalio.FONT, text=text, color=0xFFFFFF, x=28, y=HEIGHT // 2 - 1
73)
74splash.append(text_area)
75
76while True:
77    pass