|
| 1 | +# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Unlicense |
| 4 | +""" |
| 5 | +Based on example by Mark Roberts (mdroberts1243). |
| 6 | +
|
| 7 | +This example writes text to the display, and draws a series of squares and a rectangle. |
| 8 | +""" |
| 9 | + |
| 10 | +import board |
| 11 | +import displayio |
| 12 | +import terminalio |
| 13 | +from adafruit_display_text import bitmap_label as label |
| 14 | +from adafruit_displayio_sh1107 import SH1107, DISPLAY_OFFSET_ADAFRUIT_128x128_OLED_5297 |
| 15 | + |
| 16 | +displayio.release_displays() |
| 17 | + |
| 18 | +# For I2C |
| 19 | +display_bus = displayio.I2CDisplay(board.I2C(), device_address=0x3D) |
| 20 | + |
| 21 | +# For SPI: |
| 22 | +# import busio |
| 23 | +# spi_bus = busio.SPI(board.SCK, board.MOSI) |
| 24 | +# display_bus = displayio.FourWire(spi_bus, command=board.D6, chip_select=board.D5, reset=board.D9) |
| 25 | + |
| 26 | +# Width, height and rotation for Monochrome 1.12" 128x128 OLED |
| 27 | +WIDTH = 128 |
| 28 | +HEIGHT = 128 |
| 29 | +ROTATION = 90 |
| 30 | + |
| 31 | +# Border width |
| 32 | +BORDER = 2 |
| 33 | + |
| 34 | +display = SH1107( |
| 35 | + display_bus, |
| 36 | + width=WIDTH, |
| 37 | + height=HEIGHT, |
| 38 | + display_offset=DISPLAY_OFFSET_ADAFRUIT_128x128_OLED_5297, |
| 39 | + rotation=ROTATION, |
| 40 | +) |
| 41 | + |
| 42 | +# Make the display context |
| 43 | +splash = displayio.Group() |
| 44 | +display.show(splash) |
| 45 | + |
| 46 | +color_bitmap = displayio.Bitmap(WIDTH, HEIGHT, 1) |
| 47 | +color_palette = displayio.Palette(1) |
| 48 | +color_palette[0] = 0xFFFFFF # White |
| 49 | + |
| 50 | +bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0) |
| 51 | +splash.append(bg_sprite) |
| 52 | + |
| 53 | +# Draw a smaller inner rectangle in black |
| 54 | +inner_bitmap = displayio.Bitmap(WIDTH - BORDER * 2, HEIGHT - BORDER * 2, 1) |
| 55 | +inner_palette = displayio.Palette(1) |
| 56 | +inner_palette[0] = 0x000000 # Black |
| 57 | +inner_sprite = displayio.TileGrid( |
| 58 | + inner_bitmap, pixel_shader=inner_palette, x=BORDER, y=BORDER |
| 59 | +) |
| 60 | +splash.append(inner_sprite) |
| 61 | + |
| 62 | +# Draw some white squares |
| 63 | +small_bitmap = displayio.Bitmap(8, 8, 1) |
| 64 | +small_square = displayio.TileGrid(small_bitmap, pixel_shader=color_palette, x=58, y=17) |
| 65 | +splash.append(small_square) |
| 66 | + |
| 67 | +medium_bitmap = displayio.Bitmap(16, 16, 1) |
| 68 | +medium_square = displayio.TileGrid( |
| 69 | + medium_bitmap, pixel_shader=color_palette, x=71, y=15 |
| 70 | +) |
| 71 | +splash.append(medium_square) |
| 72 | + |
| 73 | +large_bitmap = displayio.Bitmap(32, 32, 1) |
| 74 | +large_square = displayio.TileGrid(large_bitmap, pixel_shader=color_palette, x=91, y=28) |
| 75 | +splash.append(large_square) |
| 76 | + |
| 77 | +bottom_bitmap = displayio.Bitmap(110, 50, 1) |
| 78 | +bottom_rectangle = displayio.TileGrid( |
| 79 | + bottom_bitmap, pixel_shader=color_palette, x=10, y=69 |
| 80 | +) |
| 81 | +splash.append(bottom_rectangle) |
| 82 | + |
| 83 | +# Draw some label text |
| 84 | +name_text = "Monochrome 1.12in" |
| 85 | +name_text_area = label.Label(terminalio.FONT, text=name_text, color=0xFFFFFF, x=8, y=8) |
| 86 | +splash.append(name_text_area) |
| 87 | +size_text = "128x128" |
| 88 | +size_text_area = label.Label(terminalio.FONT, text=size_text, color=0xFFFFFF, x=8, y=25) |
| 89 | +splash.append(size_text_area) |
| 90 | +oled_text = "OLED" |
| 91 | +oled_text_area = label.Label( |
| 92 | + terminalio.FONT, text=oled_text, scale=2, color=0xFFFFFF, x=9, y=44 |
| 93 | +) |
| 94 | +splash.append(oled_text_area) |
| 95 | + |
| 96 | +while True: |
| 97 | + pass |
0 commit comments