From 1f0d890f3f537e0d18c3442144a3e3524bd88cfd Mon Sep 17 00:00:00 2001 From: russhughes Date: Thu, 23 Dec 2021 00:41:22 -0800 Subject: [PATCH] additional examples, renames/reorganization --- README.md | 3 +- examples/esp32_320x240/feathers.py | 116 ++++++++++++++++++ examples/esp32_320x240/fonts.py | 70 +++++++++++ examples/esp32_320x240/hello.py | 70 +++++++++++ examples/esp32_320x240/lines.py | 66 ++++++++++ examples/esp32_320x240/scroll.py | 76 ++++++++++++ examples/scroll_font.py | 66 ---------- examples/ttgo_tdisplay/feathers.py | 116 ++++++++++++++++++ .../{ttgo_fonts.py => ttgo_tdisplay/fonts.py} | 2 +- .../{ttgo_hello.py => ttgo_tdisplay/hello.py} | 2 +- .../{ttgo_lines.py => ttgo_tdisplay/lines.py} | 0 .../scroll.py} | 2 +- examples/{ => ttgo_tdisplay}/toasters/t1.py | 0 examples/{ => ttgo_tdisplay}/toasters/t2.py | 0 examples/{ => ttgo_tdisplay}/toasters/t3.py | 0 examples/{ => ttgo_tdisplay}/toasters/t4.py | 0 examples/{ => ttgo_tdisplay}/toasters/t5.py | 0 .../{ => ttgo_tdisplay}/toasters/toasters.py | 0 .../{ => ttgo_tdisplay}/truetype/chango.py | 0 .../truetype/noto_fonts.py | 0 lib/__init__.py | 0 21 files changed, 519 insertions(+), 70 deletions(-) create mode 100644 examples/esp32_320x240/feathers.py create mode 100644 examples/esp32_320x240/fonts.py create mode 100644 examples/esp32_320x240/hello.py create mode 100644 examples/esp32_320x240/lines.py create mode 100644 examples/esp32_320x240/scroll.py delete mode 100644 examples/scroll_font.py create mode 100644 examples/ttgo_tdisplay/feathers.py rename examples/{ttgo_fonts.py => ttgo_tdisplay/fonts.py} (99%) rename examples/{ttgo_hello.py => ttgo_tdisplay/hello.py} (99%) rename examples/{ttgo_lines.py => ttgo_tdisplay/lines.py} (100%) rename examples/{ttgo_scroll.py => ttgo_tdisplay/scroll.py} (99%) rename examples/{ => ttgo_tdisplay}/toasters/t1.py (100%) rename examples/{ => ttgo_tdisplay}/toasters/t2.py (100%) rename examples/{ => ttgo_tdisplay}/toasters/t3.py (100%) rename examples/{ => ttgo_tdisplay}/toasters/t4.py (100%) rename examples/{ => ttgo_tdisplay}/toasters/t5.py (100%) rename examples/{ => ttgo_tdisplay}/toasters/toasters.py (100%) rename examples/{ => ttgo_tdisplay}/truetype/chango.py (100%) rename examples/{ => ttgo_tdisplay}/truetype/noto_fonts.py (100%) create mode 100644 lib/__init__.py diff --git a/README.md b/README.md index 69a24e2..cfec3cb 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,8 @@ and at https://penfold.owt.com/st7789py. Examples -------- -See the examples directory for example programs that run on the LILYGO® TTGO T-Display. +See the examples directory for example programs that run on the LILYGO® TTGO T-Display and +a generic 320x240 display connected to an ESP32. Fonts ----- diff --git a/examples/esp32_320x240/feathers.py b/examples/esp32_320x240/feathers.py new file mode 100644 index 0000000..8ebc6e5 --- /dev/null +++ b/examples/esp32_320x240/feathers.py @@ -0,0 +1,116 @@ +""" +feathers.py + + Smoothly scroll mirrored rainbow colored random curves across the display. + +""" + +import random +import math +import utime +from machine import Pin, SoftSPI +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 = SoftSPI( + baudrate=20000000, + polarity=1, + phase=0, + sck=Pin(18), + mosi=Pin(19), + miso=Pin(13)) + + tft = st7789.ST7789( + spi, + 320, + 240, + reset=Pin(4, Pin.OUT), + cs=Pin(13, Pin.OUT), + dc=Pin(12, Pin.OUT), + backlight=Pin(15, Pin.OUT), + rotation=1) + + tft.fill(st7789.BLACK) # clear screen + + height = tft.height # height of display in pixels + width = tft.width # width if display in pixels + + tfa = 0 # top free area when scrolling + bfa = 0 # 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/esp32_320x240/fonts.py b/examples/esp32_320x240/fonts.py new file mode 100644 index 0000000..310ea74 --- /dev/null +++ b/examples/esp32_320x240/fonts.py @@ -0,0 +1,70 @@ +""" +fonts.py + + Pages through all characters of four fonts on the display. + +""" +import utime +from machine import Pin, SoftSPI +import st7789py as st7789 + +# Choose fonts + +# from romfonts import vga1_8x8 as font +from romfonts import vga2_8x8 as font1 +# from romfonts import vga1_8x16 as font +from romfonts import vga2_8x16 as font2 +# 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 font3 +# from romfonts import vga1_16x32 as font +# from romfonts import vga1_bold_16x32 as font +# from romfonts import vga2_16x32 as font +from romfonts import vga2_bold_16x32 as font4 + + +def main(): + spi = SoftSPI( + baudrate=20000000, + polarity=1, + phase=0, + sck=Pin(18), + mosi=Pin(19), + miso=Pin(13)) + + tft = st7789.ST7789( + spi, + 320, + 240, + reset=Pin(4, Pin.OUT), + cs=Pin(13, Pin.OUT), + dc=Pin(12, Pin.OUT), + backlight=Pin(15, Pin.OUT), + rotation=0) + + tft.vscrdef(40, 240, 40) + + 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 + line += font.HEIGHT + + if line > tft.height-font.HEIGHT: + utime.sleep(3) + tft.fill(st7789.BLUE) + line = 0 + col = 0 + + utime.sleep(3) + + +main() diff --git a/examples/esp32_320x240/hello.py b/examples/esp32_320x240/hello.py new file mode 100644 index 0000000..4aceee9 --- /dev/null +++ b/examples/esp32_320x240/hello.py @@ -0,0 +1,70 @@ +""" +hello.py + + Writes "Hello!" in random colors at random locations on the display. + +""" +import random +from machine import Pin, SoftSPI +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 +# from romfonts import vga1_16x32 as font +# from romfonts import vga1_bold_16x32 as font +# from romfonts import vga2_16x32 as font +from romfonts import vga2_bold_16x32 as font + + +def main(): + spi = SoftSPI( + baudrate=20000000, + polarity=1, + phase=0, + sck=Pin(18), + mosi=Pin(19), + miso=Pin(13)) + + tft = st7789.ST7789( + spi, + 320, + 240, + reset=Pin(4, Pin.OUT), + cs=Pin(13, Pin.OUT), + dc=Pin(12, Pin.OUT), + backlight=Pin(15, Pin.OUT), + rotation=0) + + while True: + for rotation in range(4): + tft.rotation(rotation) + tft.fill(0) + col_max = tft.width - font.WIDTH*6 + row_max = tft.height - font.HEIGHT + + for _ in range(100): + tft.text( + font, + "Hello!", + random.randint(0, col_max), + random.randint(0, row_max), + st7789.color565( + random.getrandbits(8), + random.getrandbits(8), + random.getrandbits(8)), + st7789.color565( + random.getrandbits(8), + random.getrandbits(8), + random.getrandbits(8)) + ) + + +main() diff --git a/examples/esp32_320x240/lines.py b/examples/esp32_320x240/lines.py new file mode 100644 index 0000000..fdda78a --- /dev/null +++ b/examples/esp32_320x240/lines.py @@ -0,0 +1,66 @@ +""" +lines.py + + Draws lines and rectangles in random colors at random locations on the + display. + +""" +import random +from machine import Pin, SoftSPI +import st7789py as st7789 + + +def main(): + # configure display + + spi = SoftSPI( + baudrate=20000000, + polarity=1, + phase=0, + sck=Pin(18), + mosi=Pin(19), + miso=Pin(13)) + + tft = st7789.ST7789( + spi, + 320, + 240, + reset=Pin(4, Pin.OUT), + cs=Pin(13, Pin.OUT), + dc=Pin(12, Pin.OUT), + backlight=Pin(15, Pin.OUT), + rotation=0) + + tft.fill(st7789.BLUE) + + while True: + tft.line( + random.randint(0, tft.width), + random.randint(0, tft.height), + random.randint(0, tft.width), + random.randint(0, tft.height), + st7789.color565( + random.getrandbits(8), + random.getrandbits(8), + random.getrandbits(8) + ) + ) + + width = random.randint(0, tft.width // 2) + height = random.randint(0, tft.height // 2) + col = random.randint(0, tft.width - width) + row = random.randint(0, tft.height - height) + tft.fill_rect( + col, + row, + width, + height, + st7789.color565( + random.getrandbits(8), + random.getrandbits(8), + random.getrandbits(8) + ) + ) + + +main() diff --git a/examples/esp32_320x240/scroll.py b/examples/esp32_320x240/scroll.py new file mode 100644 index 0000000..8761b11 --- /dev/null +++ b/examples/esp32_320x240/scroll.py @@ -0,0 +1,76 @@ +""" +fonts.py + + Smoothly scrolls all font characters up the screen on the display. + Only works with fonts with heights that are even multiples of + the screen height, (i.e. 8 or 16 pixels high) + +""" +import utime +import random +from machine import Pin, SoftSPI +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 = SoftSPI( + baudrate=20000000, + polarity=1, + phase=0, + sck=Pin(18), + mosi=Pin(19), + miso=Pin(13)) + + tft = st7789.ST7789( + spi, + 320, + 240, + reset=Pin(4, Pin.OUT), + cs=Pin(13, Pin.OUT), + dc=Pin(12, Pin.OUT), + backlight=Pin(15, Pin.OUT), + rotation=0) + + last_line = tft.height - font.HEIGHT + tfa = 0 + tfb = 0 + tft.vscrdef(tfa, 240, tfb) + + 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, + '\\x{:02x}= {:s} '.format(character, chr(character)), + 0, + (scroll + last_line) % tft.height, + st7789.WHITE, + st7789.BLUE) + + character = character + 1 if character < 256 else 0 + + tft.vscsad(scroll + tfa) + scroll += 1 + + if scroll == tft.height: + scroll = 0 + + utime.sleep(0.01) + + +main() diff --git a/examples/scroll_font.py b/examples/scroll_font.py deleted file mode 100644 index f1372fe..0000000 --- a/examples/scroll_font.py +++ /dev/null @@ -1,66 +0,0 @@ -""" -ttgo_fonts.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 utime -import random -from machine import Pin, SPI -import st7789py as st7789 - -# choose a font - -# import vga1_8x8 as font -# import vga2_8x8 as font -# import vga1_8x16 as font -# import vga2_8x16 as font -# import vga1_16x16 as font -# import vga1_bold_16x16 as font -# import vga2_16x16 as font -import vga2_bold_16x16 as font - -def main(): - tft = st7789.ST7789( - SPI(2, baudrate=30000000, polarity=1, phase=1, sck=Pin(18), mosi=Pin(19)), - 135, - 240, - reset=Pin(23, Pin.OUT), - cs=Pin(5, Pin.OUT), - dc=Pin(16, Pin.OUT), - backlight=Pin(4, Pin.OUT), - rotation=0) - - last_line = tft.height - font.HEIGHT - tfa = 40 - tfb = 40 - tft.vscrdef(tfa, 240, tfb) - - 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, - '\\x{:02x}= {:s} '.format(character, chr(character)), - 0, - (scroll + last_line) % tft.height, - st7789.WHITE, - st7789.BLUE) - - character = character +1 if character < 256 else 0 - - tft.vscsad(scroll+tfa) - scroll +=1 - - if scroll == tft.height: - scroll = 0 - - utime.sleep(0.01) - -main() diff --git a/examples/ttgo_tdisplay/feathers.py b/examples/ttgo_tdisplay/feathers.py new file mode 100644 index 0000000..04d6ae3 --- /dev/null +++ b/examples/ttgo_tdisplay/feathers.py @@ -0,0 +1,116 @@ +""" +feathers.py + + Smoothly scroll mirrored rainbow colored random curves across the display. + +""" + +import random +import math +import utime +from machine import Pin, SoftSPI +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 = SoftSPI( + baudrate=20000000, + polarity=1, + phase=0, + sck=Pin(18), + mosi=Pin(19), + miso=Pin(13)) + + tft = st7789.ST7789( + spi, + 135, + 240, + reset=Pin(23, Pin.OUT), + cs=Pin(5, Pin.OUT), + dc=Pin(16, Pin.OUT), + backlight=Pin(4, Pin.OUT), + rotation=1) + + tft.fill(st7789.BLACK) # clear screen + + height = tft.height # height of display in pixels + width = tft.width # width if display in pixels + + tfa = 40 # top free area when scrolling + bfa = 40 # 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/ttgo_fonts.py b/examples/ttgo_tdisplay/fonts.py similarity index 99% rename from examples/ttgo_fonts.py rename to examples/ttgo_tdisplay/fonts.py index 74d85cc..5c2db82 100644 --- a/examples/ttgo_fonts.py +++ b/examples/ttgo_tdisplay/fonts.py @@ -1,5 +1,5 @@ """ -ttgo_fonts.py +fonts.py Pages through all characters of four fonts on the LILYGO® TTGO T-Display. diff --git a/examples/ttgo_hello.py b/examples/ttgo_tdisplay/hello.py similarity index 99% rename from examples/ttgo_hello.py rename to examples/ttgo_tdisplay/hello.py index affae67..19cdf2b 100644 --- a/examples/ttgo_hello.py +++ b/examples/ttgo_tdisplay/hello.py @@ -1,5 +1,5 @@ """ -ttgo_hello.py +hello.py Writes "Hello!" in random colors at random locations on a LILYGO® TTGO T-Display. diff --git a/examples/ttgo_lines.py b/examples/ttgo_tdisplay/lines.py similarity index 100% rename from examples/ttgo_lines.py rename to examples/ttgo_tdisplay/lines.py diff --git a/examples/ttgo_scroll.py b/examples/ttgo_tdisplay/scroll.py similarity index 99% rename from examples/ttgo_scroll.py rename to examples/ttgo_tdisplay/scroll.py index ff8ab60..99b208e 100644 --- a/examples/ttgo_scroll.py +++ b/examples/ttgo_tdisplay/scroll.py @@ -1,5 +1,5 @@ """ -ttgo_fonts.py +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 diff --git a/examples/toasters/t1.py b/examples/ttgo_tdisplay/toasters/t1.py similarity index 100% rename from examples/toasters/t1.py rename to examples/ttgo_tdisplay/toasters/t1.py diff --git a/examples/toasters/t2.py b/examples/ttgo_tdisplay/toasters/t2.py similarity index 100% rename from examples/toasters/t2.py rename to examples/ttgo_tdisplay/toasters/t2.py diff --git a/examples/toasters/t3.py b/examples/ttgo_tdisplay/toasters/t3.py similarity index 100% rename from examples/toasters/t3.py rename to examples/ttgo_tdisplay/toasters/t3.py diff --git a/examples/toasters/t4.py b/examples/ttgo_tdisplay/toasters/t4.py similarity index 100% rename from examples/toasters/t4.py rename to examples/ttgo_tdisplay/toasters/t4.py diff --git a/examples/toasters/t5.py b/examples/ttgo_tdisplay/toasters/t5.py similarity index 100% rename from examples/toasters/t5.py rename to examples/ttgo_tdisplay/toasters/t5.py diff --git a/examples/toasters/toasters.py b/examples/ttgo_tdisplay/toasters/toasters.py similarity index 100% rename from examples/toasters/toasters.py rename to examples/ttgo_tdisplay/toasters/toasters.py diff --git a/examples/truetype/chango.py b/examples/ttgo_tdisplay/truetype/chango.py similarity index 100% rename from examples/truetype/chango.py rename to examples/ttgo_tdisplay/truetype/chango.py diff --git a/examples/truetype/noto_fonts.py b/examples/ttgo_tdisplay/truetype/noto_fonts.py similarity index 100% rename from examples/truetype/noto_fonts.py rename to examples/ttgo_tdisplay/truetype/noto_fonts.py diff --git a/lib/__init__.py b/lib/__init__.py new file mode 100644 index 0000000..e69de29