forked from getis/pi-pico-spi-lcd-ili9341-st7789
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbouncing_boxes_area.py
138 lines (112 loc) · 3.78 KB
/
bouncing_boxes_area.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
"""
ILI9341 demo (bouncing boxes).
Modified to allow you to restrict the active screen area
"""
import math
import machine
from machine import Pin, SPI
from random import random, seed
from ili9341 import Display, color565
from random import random, seed, randint
from utime import sleep_us, ticks_cpu, ticks_us, ticks_diff
class Box(object):
"""Bouncing box."""
def __init__(self, screen_width, screen_height, size, display, color):
"""Initialize box.
Args:
screen_width (int): Width of screen.
screen_height (int): Width of height.
size (int): Square side length.
display (ILI9341): display object.
color (int): RGB565 color value.
"""
self.size = size
self.w = screen_width
self.h = screen_height
self.x_offset = math.floor((320 - self.w)/2)
self.y_offset = math.floor((240 - self.h)/2)
self.display = display
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
self.prev_x = self.x
self.prev_y = self.y
def update_pos(self):
"""Update box position and speed."""
# store current position
self.prev_x = self.x
self.prev_y = self.y
# 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
def draw(self):
"""Draw box."""
x = int(self.x) + self.x_offset
y = int(self.y) + self.y_offset
size = self.size
prev_x = int(self.prev_x) + self.x_offset
prev_y = int(self.prev_y) + self.y_offset
self.display.fill_hrect(prev_x,
prev_y,
size, size, 0)
self.display.fill_hrect(x,
y,
size, size, self.color)
def test():
"""Bouncing box."""
# set landscape screen
screen_width = 320
screen_height = 240
screen_rotation = 90
try:
# Baud rate of 31250000 is max at standard clock speed
spi = SPI(0,
baudrate=31250000,
polarity=1,
phase=1,
bits=8,
firstbit=SPI.MSB,
sck=Pin(18),
mosi=Pin(19),
miso=Pin(16))
display = Display(spi, dc=Pin(15), cs=Pin(17), rst=Pin(14),
width=screen_width, height=screen_height,
rotation=screen_rotation)
display.clear()
boxes = [Box(239, 135, randint(7, 40), display,
color565(randint(30, 256), randint(30, 256), randint(30, 256))) for i in range(50)]
start_time = ticks_us()
frame_count = 0
while True:
timer = ticks_us()
for b in boxes:
b.update_pos()
b.draw()
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:
display.cleanup()
test()