/ examples / displayio_ssd1305_simpletest.py
displayio_ssd1305_simpletest.py
 1  """
 2  This test will initialize the display using displayio and draw a solid white
 3  background, a smaller black rectangle, and some white text.
 4  """
 5  
 6  import board
 7  import displayio
 8  import terminalio
 9  from adafruit_display_text import label
10  import adafruit_displayio_ssd1305
11  
12  displayio.release_displays()
13  
14  # Reset is usedfor both SPI and I2C
15  oled_reset = board.D9
16  
17  # Use for SPI
18  spi = board.SPI()
19  oled_cs = board.D5
20  oled_dc = board.D6
21  display_bus = displayio.FourWire(
22      spi, command=oled_dc, chip_select=oled_cs, baudrate=1000000, reset=oled_reset
23  )
24  
25  # Use for I2C
26  # i2c = board.I2C()
27  # display_bus = displayio.I2CDisplay(i2c, device_address=0x3c, reset=oled_reset)
28  
29  WIDTH = 128
30  HEIGHT = 64  # Change to 32 if needed
31  BORDER = 8
32  FONTSCALE = 1
33  
34  display = adafruit_displayio_ssd1305.SSD1305(display_bus, width=WIDTH, height=HEIGHT)
35  
36  # Make the display context
37  splash = displayio.Group(max_size=10)
38  display.show(splash)
39  
40  color_bitmap = displayio.Bitmap(display.width, display.height, 1)
41  color_palette = displayio.Palette(1)
42  color_palette[0] = 0xFFFFFF  # White
43  
44  bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
45  splash.append(bg_sprite)
46  
47  # Draw a smaller inner rectangle
48  inner_bitmap = displayio.Bitmap(
49      display.width - BORDER * 2, display.height - BORDER * 2, 1
50  )
51  inner_palette = displayio.Palette(1)
52  inner_palette[0] = 0x000000  # Black
53  inner_sprite = displayio.TileGrid(
54      inner_bitmap, pixel_shader=inner_palette, x=BORDER, y=BORDER
55  )
56  splash.append(inner_sprite)
57  
58  # Draw a label
59  text = "Hello World!"
60  text_area = label.Label(terminalio.FONT, text=text, color=0xFFFFFF)
61  text_width = text_area.bounding_box[2] * FONTSCALE
62  text_group = displayio.Group(
63      max_size=10,
64      scale=FONTSCALE,
65      x=display.width // 2 - text_width // 2,
66      y=display.height // 2,
67  )
68  text_group.append(text_area)  # Subgroup for text scaling
69  splash.append(text_group)
70  
71  while True:
72      pass