Simple test

Ensure your device works with this simple test.

examples/displayio_ssd1305_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# Starting in CircuitPython 9.x fourwire will be a seperate internal library
13# rather than a component of the displayio library
14try:
15    from fourwire import FourWire
16
17    # Use for I2C
18    # from i2cdisplaybus import I2CDisplayBus
19except ImportError:
20    from displayio import FourWire
21
22    # from displayio import I2CDisplay as I2CDisplayBus
23import terminalio
24from adafruit_display_text import label
25import adafruit_displayio_ssd1305
26
27displayio.release_displays()
28
29# Reset is usedfor both SPI and I2C
30oled_reset = board.D9
31
32# Use for SPI
33spi = board.SPI()
34oled_cs = board.D5
35oled_dc = board.D6
36display_bus = FourWire(
37    spi, command=oled_dc, chip_select=oled_cs, baudrate=1000000, reset=oled_reset
38)
39
40# Use for I2C
41# i2c = board.I2C()  # uses board.SCL and board.SDA
42# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
43# display_bus = I2CDisplayBus(i2c, device_address=0x3c, reset=oled_reset)
44
45WIDTH = 128
46HEIGHT = 64  # Change to 32 if needed
47BORDER = 8
48FONTSCALE = 1
49
50display = adafruit_displayio_ssd1305.SSD1305(display_bus, width=WIDTH, height=HEIGHT)
51
52# Make the display context
53splash = displayio.Group()
54display.root_group = splash
55
56color_bitmap = displayio.Bitmap(display.width, display.height, 1)
57color_palette = displayio.Palette(1)
58color_palette[0] = 0xFFFFFF  # White
59
60bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
61splash.append(bg_sprite)
62
63# Draw a smaller inner rectangle
64inner_bitmap = displayio.Bitmap(
65    display.width - BORDER * 2, display.height - BORDER * 2, 1
66)
67inner_palette = displayio.Palette(1)
68inner_palette[0] = 0x000000  # Black
69inner_sprite = displayio.TileGrid(
70    inner_bitmap, pixel_shader=inner_palette, x=BORDER, y=BORDER
71)
72splash.append(inner_sprite)
73
74# Draw a label
75text = "Hello World!"
76text_area = label.Label(terminalio.FONT, text=text, color=0xFFFFFF)
77text_width = text_area.bounding_box[2] * FONTSCALE
78text_group = displayio.Group(
79    scale=FONTSCALE,
80    x=display.width // 2 - text_width // 2,
81    y=display.height // 2,
82)
83text_group.append(text_area)  # Subgroup for text scaling
84splash.append(text_group)
85
86while True:
87    pass