Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save darianbjohnson/d7ccc8a62a29dcc09a6108a48a76a1f9 to your computer and use it in GitHub Desktop.
Save darianbjohnson/d7ccc8a62a29dcc09a6108a48a76a1f9 to your computer and use it in GitHub Desktop.
Creating nested rounded rectangles (in order to mimic a star trek display)
stBLACK = 0x000000
stRED = 0xFF0000
stBLUE = 0x0000FF
tftWidth =480
tftHeight = 320
import board
import terminalio
import displayio
import time
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
from adafruit_hx8357 import HX8357
from adafruit_display_shapes.rect import Rect
from adafruit_display_shapes.circle import Circle
from adafruit_display_shapes.roundrect import RoundRect
def createHeader(bgColor, title):
for _ in range(len(headerDisplay) - 1):
headerDisplay.pop()
charHeight = 20
#create outer roundeded rectangle
mainHeaderOuterRad= 40
mainHeaderOuterX = -1*mainHeaderOuterRad*2
mainHeaderOuterY = 0
mainHeaderOuterWidth = 460
mainHeaderOuterHeight = tftHeight+mainHeaderOuterRad
mainHeaderOuter = RoundRect(mainHeaderOuterX, mainHeaderOuterY, mainHeaderOuterWidth, mainHeaderOuterHeight, mainHeaderOuterRad, fill=bgColor, outline=None, stroke=1)
#create inner roundeded rectangle
mainHeaderInnerRad= 20
mainHeaderInnerX = -1*mainHeaderInnerRad*2
mainHeaderInnerY = mainHeaderOuterY + charHeight
mainHeaderInnerWidth = 360
mainHeaderInnerHeight = tftHeight+mainHeaderInnerRad
mainHeaderInner = RoundRect(mainHeaderInnerX, mainHeaderInnerY, mainHeaderInnerWidth, mainHeaderInnerHeight, mainHeaderInnerRad, fill=stBLACK, outline=None, stroke=1)
#create roundeded ends
rectXY = (int)(charHeight)
endRectA = Rect(0, 0, rectXY, rectXY, fill=stBLACK)
endRectB = RoundRect(0, 0, rectXY*2, rectXY, (int)(rectXY/2), fill=bgColor, outline=None, stroke=1)
headerDisplay.append(mainHeaderOuter)
headerDisplay.append(mainHeaderInner)
headerDisplay.append(endRectA)
headerDisplay.append(endRectB)
# Set text, font, and color
#font = bitmap_font.load_font("/Okuda-29.bdf")
font = terminalio.FONT
mainTitleGroup = displayio.Group(max_size=10 , scale=1, x=rectXY, y=(int)(rectXY/2))
mainTitleLabel = label.Label(font, text=title, color=bgColor, background_color=stBLACK)
(_, _, width, _) = mainTitleLabel.bounding_box
textMargin = 5
textRect = Rect(rectXY-textMargin, 0, width + textMargin*2, rectXY, fill=stBLACK)
headerDisplay.append(textRect)
mainTitleGroup.append(mainTitleLabel)
headerDisplay.append(mainTitleGroup)
# Release any resources currently in use for the displays
displayio.release_displays()
# Init the board
spi = board.SPI()
tft_cs = board.D9
tft_dc = board.D10
display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs)
display = HX8357(display_bus, width=480, height=320)
# Make the main display context
splash = displayio.Group(max_size=20)
color_bitmap = displayio.Bitmap(480, 320, 1)
color_palette = displayio.Palette(1)
color_palette[0] = stBLACK
bg_sprite = displayio.TileGrid(color_bitmap,pixel_shader=color_palette, x=0, y=0)
splash.append(bg_sprite)
statsDisplay = displayio.Group(max_size=15)
headerDisplay = displayio.Group(max_size=15)
splash.append(statsDisplay)
splash.append(headerDisplay)
display.show(splash)
while True:
createHeader(stRED, "SCHEMATICS")
time.sleep(3)
createHeader(stBLUE, "WELCOME")
time.sleep(3)
@darianbjohnson
Copy link
Author

Note - using a itsy bitsy M4 connected to a TFT 3.5 via SPI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment