Skip to content

Instantly share code, notes, and snippets.

@gallaugher
Created May 21, 2025 17:56
Show Gist options
  • Save gallaugher/62aeb900fa3154b5d280c98290f1721e to your computer and use it in GitHub Desktop.
Save gallaugher/62aeb900fa3154b5d280c98290f1721e to your computer and use it in GitHub Desktop.
Test-128x128-stemma-qt-grayscale.py
# Creates a pusing set of rings under text.
import time
import math
import board
import displayio
import busdisplay
import i2cdisplaybus
import terminalio
from adafruit_display_text import label
import adafruit_ssd1327
# === Release any displays that might already be in use ===
displayio.release_displays()
# === Setup I2C display using STEMMA QT ===
i2c = board.STEMMA_I2C() # Uses default I2C pins for QT Py RP2040
display_bus = i2cdisplaybus.I2CDisplayBus(i2c, device_address=0x3D)
WIDTH = 128
HEIGHT = 128
# === Initialize the SSD1327 OLED Display ===
display = adafruit_ssd1327.SSD1327(
display_bus, width=WIDTH, height=HEIGHT, rotation=0
)
# === Create the main display group ===
main_group = displayio.Group()
display.root_group = main_group
# === Add a text label with the display name ===
title = label.Label(
terminalio.FONT,
text="Adafruit 1.5in Grayscale OLED",
scale=1,
x=5,
y=10,
)
main_group.append(title)
# === Create a 64x64 bitmap and grayscale palette ===
size = 64
bitmap = displayio.Bitmap(size, size, 256)
palette = displayio.Palette(256)
for i in range(256):
palette[i] = (i << 16) | (i << 8) | i # Grayscale color ramp
tile = displayio.TileGrid(
bitmap,
pixel_shader=palette,
x=(WIDTH - size) // 2,
y=(HEIGHT - size) // 2 + 16
)
main_group.append(tile)
# === Function to animate a grayscale pulse ===
def animate_pulse(frame):
for y in range(size):
for x in range(size):
dx = x - size // 2
dy = y - size // 2
dist = math.sqrt(dx * dx + dy * dy)
brightness = int(127 + 127 * math.sin(dist / 3 - frame / 5)) % 256
bitmap[x, y] = brightness
# === Main loop ===
frame = 0
while True:
animate_pulse(frame)
frame += 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment