""" noto_fonts Writes the names of three Noto fonts centered on the display using the font. The fonts were converted from True Type fonts using the font2bitmap utility. """ import st7789 import tft_config import NotoSans_32 as noto_sans import NotoSerif_32 as noto_serif import NotoSansMono_32 as noto_mono tft = tft_config.config(1, buffer_size=16 * 32 * 2) def center(font, s, row, color=st7789.WHITE): screen = tft.width() # get screen width width = tft.write_len(font, s) # get the width of the string if width and width < screen: # if the string < display col = tft.width() // 2 - width // 2 # find the column to center else: # otherwise col = 0 # left justify tft.write(font, s, col, row, color) # and write the string def main(): # init display tft.init() tft.fill(st7789.BLACK)
fonts.py Cycles through all characters of four bitmap fonts on the display """ import utime import st7789 import tft_config import vga1_8x8 as font1 import vga1_8x16 as font2 import vga1_bold_16x16 as font3 import vga1_bold_16x32 as font4 tft = tft_config.config(0) def main(): tft.init() while True: for font in (font1, font2, font3, font4): tft.fill(st7789.BLUE) line = 0 col = 0 for char in range(font.FIRST, font.LAST): tft.text(font, chr(char), col, line, st7789.WHITE, st7789.BLUE) col += font.WIDTH if col > tft.width() - font.WIDTH: col = 0
""" chango.py proportional font test for font2bitmap converter. """ import time import gc import st7789 import tft_config tft = tft_config.config(1) # # Large fonts take alot of memory, they should be frozen in the # firmaware or compiled using the mpy-cross compiler. # import chango_16 as font_16 import chango_32 as font_32 import chango_64 as font_64 gc.collect() def display_font(font): tft.fill(st7789.BLUE) # clear the screen column = 0 # first column row = 0 # first row for char in font.MAP: # for each character in the font map width = tft.write_len(font, char) # get the width of the character
def main(): ''' The big show! ''' tft = tft_config.config(1) # configure driver to rotate screen 90 degrees tft.init() # initialize display height = tft.height() # height of display in pixels width = tft.width() # width if display in pixels tfa = tft_config.TFA # top free area when scrolling bfa = tft_config.BFA # bottom free area when scrolling scroll = 0 # scroll position wheel = 0 # color wheel position tft.vscrdef(tfa, width, bfa) # set scroll area tft.vscsad(scroll + tfa) # set scroll position tft.fill(st7789.BLACK) # clear screen half = (height >> 1) - 1 # half the height of the dislay interval = 0 # steps between new points increment = 0 # increment per step counter = 1 # step counter, overflow to start current_y = 0 # current_y value (right point) last_y = 0 # last_y value (left point) # segment offsets x_offsets = [x * (width // 8) -1 for x in range(2,9)] while True: # when the counter exceeds the interval, save current_y to last_y, # choose a new random value for current_y between 0 and 1/2 the # height of the display, choose a new random interval then reset # the counter to 0 if counter > interval: last_y = current_y current_y = random.randint(0, half) counter = 0 interval = random.randint(10, 100) increment = 1 / interval # clear the first column of the display and scroll it tft.vline(scroll, 0, height, st7789.BLACK) tft.vscsad(scroll + tfa) # get the next point between last_y and current_y tween = int(between(last_y, current_y, counter * increment)) # draw mirrored pixels across the display at the offsets using the color_wheel effect for i, x_offset in enumerate(x_offsets): tft.pixel((scroll + x_offset) % width, half + tween, color_wheel(wheel+(i<<2))) tft.pixel((scroll + x_offset) % width, half - tween, color_wheel(wheel+(i<<2))) # increment scroll, counter, and wheel scroll = (scroll + 1) % width wheel = (wheel + 1) % 256 counter += 1 # pause to slow down scrolling utime.sleep(0.005)
def main(): class Toast(): ''' Toast class to keep track of toaster and toast sprites ''' def __init__(self, sprites, bitmaps, frames): '''create new sprite in random location that does not overlap other sprites''' self.num = len(sprites) self.bitmaps = bitmaps self.frames = frames self.steps = len(frames) self.col, self.row = random_start(tft, sprites, bitmaps, self.num) self.width = bitmaps.WIDTH self.height = bitmaps.HEIGHT self.last_col = self.col self.last_row = self.row self.step = random.randint(0, self.steps) self.dir_col = -random.randint(2, 5) self.dir_row = 2 self.prev_dir_col = self.dir_col self.prev_dir_row = self.dir_row self.iceberg = 0 def clear(self): '''clear above and behind sprite''' tft.fill_rect(self.col, self.row - 1, self.width, self.dir_row + 1, st7789.BLACK) tft.fill_rect(self.col + self.width + self.dir_col, self.row, -self.dir_col, self.height, st7789.BLACK) def erase(self): '''erase last postion of sprite''' tft.fill_rect(self.last_col, self.last_row, self.width, self.height, st7789.BLACK) def move(self, sprites): '''step frame and move sprite''' if self.steps: self.step = (self.step + 1) % self.steps self.last_col = self.col self.last_row = self.row new_col = self.col + self.dir_col new_row = self.row + self.dir_row # if new location collides with another sprite, change direction for 32 frames for sprite in sprites: if (self.num != sprite.num and collide( new_col, new_row, self.width, self.height, sprite.col, sprite.row, sprite.width, sprite.height, ) and (self.col > sprite.col)): self.iceberg = 32 self.dir_col = -1 self.dir_row = 3 new_col = self.col + self.dir_col new_row = self.row + self.dir_row self.col = new_col self.row = new_row # if new location touches edge of screen, erase then set new start location if self.col <= 0 or self.row > tft.height() - self.height: self.erase() self.dir_col = -random.randint(2, 5) self.dir_row = 2 self.col, self.row = random_start(tft, sprites, self.bitmaps, self.num) # Track post collision direction change if self.iceberg: self.iceberg -= 1 if self.iceberg == 1: self.dir_col = self.prev_dir_col self.dir_row = self.prev_dir_row def draw(self): '''if the location is not 0,0 draw current frame of sprite at it's location''' if self.col and self.row: tft.bitmap(self.bitmaps, self.col, self.row, self.frames[self.step]) tft = tft_config.config(1, buffer_size=64 * 62 * 2) # configure display driver # init and clear screen tft.init() tft.fill(st7789.BLACK) # create toast spites and set animation frames sprites = [] sprites.append(Toast(sprites, toast_bitmaps, TOAST_FRAMES)) sprites.append(Toast(sprites, toast_bitmaps, TOASTER_FRAMES)) sprites.append(Toast(sprites, toast_bitmaps, TOASTER_FRAMES)) # move and draw sprites while True: for sprite in sprites: sprite.clear() sprite.move(sprites) sprite.draw() gc.collect() time.sleep(0.05)
""" roids.py - Asteroids style game demo using polygons. """ import math import random import utime import micropython import st7789 import tft_config import tft_buttons as Buttons tft = tft_config.config(1, buffer_size=64 * 64 * 2) buttons = Buttons.Buttons() def main(): ''' Game on! ''' class Poly(): ''' Poly class to keep track of a polygon based sprite ''' def __init__( self, # list (x,y) tuples of convex polygon, must be closed polygon, x=None, # x location of polygon y=None, # y location of polygon v_x=None, # velocity in x axis