forked from getis/pi-pico-spi-lcd-ili9341-st7789
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboxes_st7789_final.py
177 lines (133 loc) · 4.45 KB
/
boxes_st7789_final.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""
Threaded bouncing boxes with frame buffer
Uses a single shot function for second core SPI handler.
This cleans itself when the function exits removing the
need for a garbage collection call.
"""
import math
from machine import Pin, SPI
import framebuf
from random import random, seed, randint
from utime import sleep_us, ticks_cpu, ticks_us
import _thread
import st7789 as st7789
class Box(object):
"""Bouncing box."""
def __init__(self, screen_width, screen_height, size, color):
"""Initialize box.
Args:
screen_width (int): Width of screen.
screen_height (int): Width of height.
size (int): Square side length.
color (int): RGB565 color value.
"""
self.size = size
self.w = screen_width
self.h = screen_height
self.color = color
# Generate non-zero random speeds between -5.0 and 5.0
seed(ticks_cpu())
r = random() * 10.0
self.x_speed = r - 5
r = random() * 10.0
self.y_speed = r - 5
self.x = self.w / 2
self.y = self.h / 2
def update_pos(self):
"""Update box position and speed."""
# update position
self.x += self.x_speed
self.y += self.y_speed
# limit checking
if self.x < 0:
self.x = 0
self.x_speed = -self.x_speed
elif self.x > (self.w - self.size):
self.x = self.w - self.size
self.x_speed = -self.x_speed
if self.y < 0:
self.y = 0
self.y_speed = -self.y_speed
elif self.y > (self.h - self.size):
self.y = self.h - self.size
self.y_speed = -self.y_speed
# extra processing load
for num in range(1, 50):
num2 = math.sqrt(num)
def draw(self):
"""Draw box."""
global fbuf
x = int(self.x)
y = int(self.y)
size = self.size
fbuf.fill_rect(x, y, size, size, self.color)
# set landscape screen
screen_width = 135
screen_height = 240
screen_rotation = 1
spi = SPI(1,
baudrate=31250000,
polarity=1,
phase=1,
bits=8,
firstbit=SPI.MSB,
sck=Pin(10),
mosi=Pin(11))
display = st7789.ST7789(
spi,
screen_width,
screen_height,
reset=Pin(12, Pin.OUT),
cs=Pin(9, Pin.OUT),
dc=Pin(8, Pin.OUT),
backlight=Pin(13, Pin.OUT),
rotation=screen_rotation)
print(spi)
# FrameBuffer needs 2 bytes for every RGB565 pixel
buffer_width = 240
buffer_height = 136
buffer = bytearray(buffer_width * buffer_height * 2)
fbuf = framebuf.FrameBuffer(buffer, buffer_width, buffer_height, framebuf.RGB565)
render_frame = False
def main_loop():
"""Test code."""
global fbuf, buffer, buffer_width, buffer_height
global render_frame
render_frame = False
try:
boxes = [Box(buffer_width - 1, buffer_height - 1, randint(7, 40),
st7789.color565(randint(30, 256), randint(30, 256), randint(30, 256))) for i in range(25)]
start_time = ticks_us()
frame_count = 0
while True:
for b in boxes:
b.update_pos()
while render_frame:
# previous frame still rendering
pass
for b in boxes:
b.draw()
# render frame to lcd
render_frame = True
# start spi handler on core 1
spi_thread = _thread.start_new_thread(render_thread, ())
frame_count += 1
if frame_count == 100:
frame_rate = 100 / ((ticks_us() - start_time) / 1000000)
print(frame_rate)
start_time = ticks_us()
frame_count = 0
except KeyboardInterrupt:
pass
def render_thread():
global fbuf, buffer, buffer_width, buffer_height, render_frame, spi
global display, screen_width, screen_height, screen_rotation
# No need to wait for start signal as thread only started when buffer is ready
# render display
display.blit_buffer(buffer, 0, 0, buffer_width, buffer_height)
# clear buffer
fbuf.fill(0)
# signal finished back to main thread
render_frame = False
# thread will exit and self clean removing need for garbage collection
main_loop()