-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_bouncing_text.py
117 lines (99 loc) · 3.42 KB
/
demo_bouncing_text.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""SSD1322 demo (bouncing text)."""
from machine import Pin, SPI # type: ignore
from utime import sleep_us, ticks_us, ticks_diff # type: ignore
from ssd1322 import Display
from xglcd_font import XglcdFont
class BouncingText(object):
"""Bouncing Text."""
def __init__(self, text, font, screen_width, screen_height,
speed, display, gs=15):
"""Initialize text.
Args:
text (string): Message to display
font (FrameBuffer): xglcd font
screen_width (int): Width of screen
screen_height (int): Width of height
speed(int): Initial XY-Speed of sprite
display (SSD1351): OLED display object
gs (int): Grayscale 0 - 15
"""
self.text = text
self.font = font
self.w = font.measure_text(text)
self.h = font.height
self.screen_width = screen_width
self.screen_height = screen_height
self.display = display
self.gs = gs
self.x_speed = speed
self.y_speed = speed
self.x = 0
self.y = 0
self.prev_x = self.x
self.prev_y = self.y
def update_pos(self):
"""Update sprite speed and position."""
x = self.x
y = self.y
w = self.w
h = self.h
x_speed = abs(self.x_speed)
y_speed = abs(self.y_speed)
if x + w + x_speed >= self.screen_width:
self.x_speed = -x_speed
elif x - x_speed < 0:
self.x_speed = x_speed
if y + h + y_speed >= self.screen_height:
self.y_speed = -y_speed
elif y - y_speed <= 0:
self.y_speed = y_speed
self.prev_x = x
self.prev_y = y
self.x = x + self.x_speed
self.y = y + self.y_speed
def draw(self):
"""Draw text."""
x = self.x
y = self.y
prev_x = self.prev_x
prev_y = self.prev_y
w = self.w
h = self.h
x_speed = abs(self.x_speed)
y_speed = abs(self.y_speed)
# Determine direction and remove previous portion of sprite
if prev_x > x:
# Left
self.display.fill_rectangle(x + w, prev_y, x_speed, h, gs=0)
elif prev_x < x:
# Right
self.display.fill_rectangle(x - x_speed, prev_y, x_speed, h, gs=0)
if prev_y > y:
# Upward
self.display.fill_rectangle(prev_x, y + h, w, y_speed, gs=0)
elif prev_y < y:
# Downward
self.display.fill_rectangle(prev_x, y - y_speed, w, y_speed, gs=0)
self.display.draw_text(x, y, self.text, self.font, gs=self.gs)
self.display.present()
def test():
"""Bouncing sprite."""
try:
spi = SPI(0, baudrate=25000000, sck=Pin(18), mosi=Pin(19))
display = Display(spi, dc=Pin(20), cs=Pin(17), rst=Pin(21))
display.clear()
unispace = XglcdFont('fonts/Unispace12x24.c', 12, 24)
# Load text object
bouncing_text = BouncingText('256x64 OLED Display',
unispace, 256, 64, 1, display)
while True:
timer = ticks_us()
bouncing_text.update_pos()
bouncing_text.draw()
# Attempt to set framerate to 30 FPS
timer_dif = 33333 - ticks_diff(ticks_us(), timer)
if timer_dif > 0:
sleep_us(timer_dif)
except KeyboardInterrupt:
display.cleanup()
test()