Skip to content

Instantly share code, notes, and snippets.

@kmatch98
Created August 19, 2020 19:59
Show Gist options
  • Save kmatch98/db2e3f5f48c71caba2bd8b96c6ee4996 to your computer and use it in GitHub Desktop.
Save kmatch98/db2e3f5f48c71caba2bd8b96c6ee4996 to your computer and use it in GitHub Desktop.
Rainbow Notice for CircuitPython Day 9/9/2020
import board
import terminalio
import displayio
from adafruit_display_text import bitmap_label as label
def wheel(pos):
# input a value 0 to 255 to get a color value
# the colors are a transition r-g-b-back to r.
if pos < 1 or pos > 255:
return (0,0,0)
if pos < 85:
return (255-pos*3, pos*3, 0)
if pos < 170:
pos -=85
return (0, 255-pos*3, pos *3)
pos -= 170
return (pos *3, 0, 255-pos *3)
def increment_palette(target_bitmap, xshift, source_bitmap):
# copy source_bitmap into the target_bitmap
# with an offset, including wrap-around
xshift = xshift % source_bitmap.width
if xshift <= target_bitmap.width:
target_bitmap.blit(xshift, 0, source_bitmap)
target_bitmap.blit(0,0, source_bitmap, x1=(source_bitmap.width-xshift))
if "DISPLAY" not in dir(board):
# Setup the LCD display with driver
# You may need to change this to match the display driver for the chipset
# used on your display
from adafruit_ili9341 import ILI9341
displayio.release_displays()
# setup the SPI bus
spi = board.SPI()
tft_cs = board.D9 # arbitrary, pin not used
tft_dc = board.D10
tft_backlight = board.D12
tft_reset = board.D11
while not spi.try_lock():
spi.configure(baudrate=32000000)
spi.unlock()
display_bus = displayio.FourWire(
spi,
command=tft_dc,
chip_select=tft_cs,
reset=tft_reset,
baudrate=32000000,
polarity=1,
phase=1,
)
# Number of pixels in the display
DISPLAY_WIDTH = 320
DISPLAY_HEIGHT = 240
# create the display
display = ILI9341(
display_bus,
width=DISPLAY_WIDTH,
height=DISPLAY_HEIGHT,
rotation=180, # The rotation can be adjusted to match your configuration.
auto_refresh=True,
native_frames_per_second=90,
)
# reset the display to show nothing.
display.show(None)
else:
# built-in display
display = board.DISPLAY
# Setup file locations for BDF font files
font_files = [
'fonts/Helvetica-Bold-16.bdf',
#'fonts/Fayette-HandwrittenScript-64.bdf',
#'fonts/Cornerstone-84.bdf',
'fonts/GothamBlack-54.bdf'
]
print('Preparing for CircuitPython Day...')
font_list = []
for i, font_file in enumerate(font_files):
from adafruit_bitmap_font import bitmap_font
this_font = bitmap_font.load_font(font_file)
font_list.append(this_font)
import time
# create the rainbow text outline
text = "2020"
text_area = label.Label(font_list[1], text=text,
color= None, # use transparent text, black background
background_color=0x000000, background_tight=True,
anchor_point=(0.5,0.5),
max_glyphs=10,
anchored_position=(display.width/2, display.height/2),
line_spacing=1.0,
scale=1
)
bitmap_width=text_area.bounding_box[2]
bitmap_height=text_area.bounding_box[3]
# create a rainbow colored bitmap that will be copied into a bitmap behind the text.
rainbow_bitmap = displayio.Bitmap(255, bitmap_height, 255)
palette=displayio.Palette(255)
for i in range (0,255):
palette[i]=int(''.join('%02x'%i for i in wheel(i)),16)
for y in range(rainbow_bitmap.height):
for x in range(rainbow_bitmap.width):
rainbow_bitmap[x,y]=max(1,(x+1)%255)
# this is the bitmap behind the text
display_bitmap = displayio.Bitmap(bitmap_width, bitmap_height, 255)
display_bitmap.blit(0,0, rainbow_bitmap) # initialize with the rainbow color
# Tilegrid that holds the bitmap behind the text
rainbow_tile=displayio.TileGrid(display_bitmap, pixel_shader=palette,
x=text_area.x+text_area.bounding_box[0],
y=text_area.y+text_area.bounding_box[1])
# create the purple text
text2='CircuitPython Day'
text_area2 = label.Label(font_list[0], text=text2,
color= 0xFF00FF,
background_color=None, background_tight=True,
anchor_point=(0.5,0.5),
max_glyphs=10,
anchored_position=(display.width/2, display.height/4),
line_spacing=1.0,
scale=2
)
text3='09/09/2020'
text_area3 = label.Label(font_list[0], text=text3,
color= 0xFF00FF,
background_color=None, background_tight=True,
anchor_point=(0.5,0.5),
max_glyphs=10,
anchored_position=(display.width/2, display.height*5/6),
line_spacing=1.0,
scale=2
)
my_group=displayio.Group(max_size=4)
my_group.append(rainbow_tile)
my_group.append(text_area)
my_group.append(text_area2)
my_group.append(text_area3)
display.show(my_group)
i=1
while True:
increment_palette(display_bitmap, (5*i), rainbow_bitmap)
i += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment