kopia lustrzana https://github.com/russhughes/st7789py_mpy
additional examples, renames/reorganization
rodzic
fe2f827b86
commit
1f0d890f3f
|
@ -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
|
||||
-----
|
||||
|
|
|
@ -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()
|
|
@ -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()
|
|
@ -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()
|
|
@ -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()
|
|
@ -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()
|
|
@ -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()
|
|
@ -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()
|
|
@ -1,5 +1,5 @@
|
|||
"""
|
||||
ttgo_fonts.py
|
||||
fonts.py
|
||||
|
||||
Pages through all characters of four fonts on the LILYGO® TTGO T-Display.
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
"""
|
||||
ttgo_hello.py
|
||||
hello.py
|
||||
|
||||
Writes "Hello!" in random colors at random locations on a
|
||||
LILYGO® TTGO T-Display.
|
|
@ -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
|
Ładowanie…
Reference in New Issue