diff --git a/examples/atom_s3/feathers.py b/examples/atom_s3/feathers.py new file mode 100644 index 0000000..6c80633 --- /dev/null +++ b/examples/atom_s3/feathers.py @@ -0,0 +1,108 @@ +""" +feathers.py + + Smoothly scroll mirrored rainbow colored random curves across the display. + +""" + +import random +import math +import utime +from machine import Pin, SPI +import st7789py as st7789 + + +def between(left, right, along): + """returns a point along the curve from left to right""" + dist = (1 - math.cos(along * math.pi)) / 2 + return left * (1 - dist) + right * dist + + +def color_wheel(position): + """returns a 565 color from the given position of the color wheel""" + position = (255 - position) % 255 + + if position < 85: + return st7789.color565(255 - position * 3, 0, position * 3) + + if position < 170: + position -= 85 + return st7789.color565(0, position * 3, 255 - position * 3) + + position -= 170 + return st7789.color565(position * 3, 255 - position * 3, 0) + + +def main(): + ''' + The big show! + ''' + #enable display and clear screen + spi = SPI(2, baudrate=40000000, sck=Pin(17), mosi=Pin(21), miso=None) + tft = st7789.ST7789( + spi, + 128, + 128, + reset=Pin(34, Pin.OUT), + cs=Pin(15, Pin.OUT), + dc=Pin(33, Pin.OUT), + backlight=Pin(16, Pin.OUT), + rotation=1, + color_order=st7789.BGR) + + + height = tft.height # height of display in pixels + width = tft.width # width if display in pixels + + tfa = 1 # top free area when scrolling + bfa = 3 # 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 # increment per step + + # 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 + + +main() diff --git a/examples/atom_s3/rotations.py b/examples/atom_s3/rotations.py new file mode 100644 index 0000000..92c68cd --- /dev/null +++ b/examples/atom_s3/rotations.py @@ -0,0 +1,53 @@ +""" +feathers.py + + Smoothly scroll mirrored rainbow colored random curves across the display. + +""" + +import random +import math +import time +from machine import Pin, SPI +import st7789py as st7789 +import vga1_8x8 as font + + +def center_on(display, font, text, y, fg, bg): + ''' + Center the text on the display + ''' + x = (display.width - len(text) * font.WIDTH) // 2 + display.text(font, text, x, y, fg, bg) + +def main(): + ''' + The big show! + ''' + #enable display and clear screen + + spi = SPI(2, baudrate=40000000, sck=Pin(17), mosi=Pin(21), miso=None) + tft = st7789.ST7789( + spi, + 128, + 128, + reset=Pin(34, Pin.OUT), + cs=Pin(15, Pin.OUT), + dc=Pin(33, Pin.OUT), + backlight=Pin(16, Pin.OUT), + rotation=1, + color_order=st7789.BGR) + + height = tft.height # height of display in pixels + width = tft.width # width if display in pixels + + while True: + for rotation in range(4): + tft.rotation(rotation) + tft.fill(st7789.BLACK) + tft.rect(0, 0, width, height, st7789.RED) + center_on(tft, font, "Rotation", height // 2 - font.HEIGHT, st7789.WHITE, st7789.BLACK) + center_on(tft, font, str(rotation), height // 2 + font.HEIGHT, st7789.WHITE, st7789.BLACK) + time.sleep(1) + +main() diff --git a/examples/atom_s3/scroll.py b/examples/atom_s3/scroll.py new file mode 100644 index 0000000..de12aee --- /dev/null +++ b/examples/atom_s3/scroll.py @@ -0,0 +1,70 @@ +""" +scroll.py + + Smoothly scrolls all font characters up the screen on the LILYGO® TTGO + T-Display. Only works with fonts with heights that are even multiples of + the screen height, (i.e. 8 or 16 pixels high) + +""" +import time +import random +from machine import Pin, SPI +import st7789py as st7789 + +# choose a font + +# from romfonts import vga1_8x8 as font +# from romfonts import vga2_8x8 as font +# from romfonts import vga1_8x16 as font +# from romfonts import vga2_8x16 as font +# from romfonts import vga1_16x16 as font +# from romfonts import vga1_bold_16x16 as font +# from romfonts import vga2_16x16 as font +from romfonts import vga2_bold_16x16 as font + + +def main(): + spi = SPI(2, baudrate=40000000, sck=Pin(17), mosi=Pin(21), miso=None) + tft = st7789.ST7789( + spi, + 128, + 128, + reset=Pin(34, Pin.OUT), + cs=Pin(15, Pin.OUT), + dc=Pin(33, Pin.OUT), + backlight=Pin(16, Pin.OUT), + rotation=0, + color_order=st7789.BGR) + + last_line = tft.height - font.HEIGHT + tfa = 1 # top free area when scrolling + bfa = 3 # bottom free area when scrolling + + tft.vscrdef(tfa, tft.height, bfa) + + tft.fill(st7789.BLUE) + scroll = 0 + character = 0 + while True: + tft.fill_rect(0, scroll, tft.width, 1, st7789.BLUE) + + if scroll % font.HEIGHT == 0: + tft.text( + font, + f' x{character:02x}= {chr(character)} ', + 0, + (scroll + last_line) % tft.height, + st7789.WHITE, + st7789.BLUE) + + character = (character + 1) % (font.LAST+1) + + tft.vscsad(scroll + tfa) + scroll += 1 + time.sleep_ms(10) + + if scroll == tft.height: + scroll = 0 + + +main()