You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.8 KiB
Python

"""ILI9341 demo (bouncing boxes)."""
from machine import Pin, SPI
from random import random, seed
from ili9341 import Display, color565
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.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 = 5.0 - r if r < 5.0 else r - 10.0
r = random() * 10.0
self.y_speed = 5.0 - r if r < 5.0 else r - 10.0
self.x = self.w / 2.0
self.y = self.h / 2.0
self.prev_x = self.x
self.prev_y = self.y
def update_pos(self):
"""Update box position and speed."""
x = self.x
y = self.y
size = self.size
w = self.w
h = self.h
x_speed = abs(self.x_speed)
y_speed = abs(self.y_speed)
self.prev_x = x
self.prev_y = y
if x + size >= w - x_speed:
self.x_speed = -x_speed
elif x - size <= x_speed + 1:
self.x_speed = x_speed
if y + size >= h - y_speed:
self.y_speed = -y_speed
elif y - size <= y_speed + 1:
self.y_speed = y_speed
self.x = x + self.x_speed
self.y = y + self.y_speed
def draw(self):
"""Draw box."""
x = int(self.x)
y = int(self.y)
size = self.size
prev_x = int(self.prev_x)
prev_y = int(self.prev_y)
self.display.fill_hrect(prev_x - size,
prev_y - size,
size, size, 0)
self.display.fill_hrect(x - size,
y - size,
size, size, self.color)
def test():
"""Bouncing box."""
try:
# Baud rate of 40000000 seems about the max
spi = SPI(0,
baudrate=10000000, # pico 10Mhz
polarity=1, # what voltage level to set the clock signal to when in standby mode (not sending data)
phase=1, # Tells SPI wether it needs to sample data on first or second edge of the clock value.
bits=8, # Sets bits per data value
firstbit=SPI.MSB, # In which order should multiple byte data be sent. LCD panel expects Big Endian. So Most significant byte first
sck=Pin(18),
mosi=Pin(19),
miso=Pin(16)) # in this data you aren't sending any data back.
display = Display(spi, dc=Pin(15), cs=Pin(17), rst=Pin(14))
display.clear()
colors = [color565(255, 0, 0),
color565(0, 255, 0),
color565(0, 0, 255),
color565(255, 255, 0),
color565(0, 255, 255),
color565(255, 0, 255)]
sizes = [12, 11, 10, 9, 8, 7]
boxes = [Box(239, 319, sizes[i], display,
colors[i]) for i in range(6)]
while True:
timer = ticks_us()
for b in boxes:
b.update_pos()
b.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()
for i in range(0, 10, -1):
print(f"Starting in: {i}")
sleep_us(1000000)
test()